summaryrefslogtreecommitdiff
path: root/protocols/Weather/src/weather_proto.cpp
blob: fdb1fe443d90737089038365c36a00cfa024c804 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
Copyright (C) 2012-25 Miranda NG team (https://miranda-ng.org)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 2
of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "stdafx.h"

CWeatherProto::CWeatherProto(const char *protoName, const wchar_t *userName) :
	PROTO<CWeatherProto>(protoName, userName),
	m_impl(*this),
	m_bPopups(m_szModuleName, "UsePopup", true),
	m_szApiKey(m_szModuleName, "ApiKey", L"")
{
	m_hProtoIcon = g_plugin.getIconHandle(IDI_ICON);

	CreateProtoService(PS_GETAVATARINFO, &CWeatherProto::GetAvatarInfoSvc);
	CreateProtoService(PS_GETADVANCEDSTATUSICON, &CWeatherProto::AdvancedStatusIconSvc);

	HookProtoEvent(ME_OPT_INITIALISE, &CWeatherProto::OptInit);
	HookProtoEvent(ME_CLIST_DOUBLECLICKED, &CWeatherProto::BriefInfoEvt);

	// load options and set defaults
	LoadOptions();

	// reset the weather data at startup for individual contacts
	EraseAllInfo();

	// popup initialization
	CMStringW wszTitle(FORMAT, L"%s %s", m_tszUserName, TranslateT("notifications"));
	g_plugin.addPopupOption(wszTitle, m_bPopups);

	// netlib
	NETLIBUSER nlu = {};
	nlu.flags = NUF_OUTGOING | NUF_HTTPCONNS | NUF_NOHTTPSOPTION | NUF_UNICODE;
	nlu.szSettingsModule = m_szModuleName;
	nlu.szDescriptiveName.w = m_tszUserName;
	m_hNetlibUser = Netlib_RegisterUser(&nlu);
}

CWeatherProto::~CWeatherProto()
{
	DestroyMwin();
	DestroyUpdateList();
}

void CWeatherProto::OnModulesLoaded()
{
	InitMwin();
	InitMenuItems();

	// timer for the first update
	m_impl.m_start.Start(5000);  // first update is 5 sec after load

	// weather user detail
	HookProtoEvent(ME_USERINFO_INITIALISE, &CWeatherProto::UserInfoInit);
	HookProtoEvent(ME_TTB_MODULELOADED, &CWeatherProto::OnToolbarLoaded);
}

int CWeatherProto::OnToolbarLoaded(WPARAM, LPARAM)
{
	CMStringA szName(FORMAT, "%s/Enabled", m_szModuleName);

	TTBButton ttb = {};
	ttb.name = LPGEN("Enable/disable auto update");
	ttb.pszService = szName.GetBuffer();
	ttb.pszTooltipUp = LPGEN("Auto Update Enabled");
	ttb.pszTooltipDn = LPGEN("Auto Update Disabled");
	ttb.hIconHandleUp = g_plugin.getIconHandle(IDI_ICON);
	ttb.hIconHandleDn = g_plugin.getIconHandle(IDI_DISABLED);
	ttb.dwFlags = (getByte("AutoUpdate", 1) ? 0 : TTBBF_PUSHED) | TTBBF_ASPUSHBUTTON | TTBBF_VISIBLE;
	hTBButton = g_plugin.addTTB(&ttb);
	return 0;
}

void CWeatherProto::OnShutdown()
{
	m_impl.m_update.Stop();

	SaveOptions(); // save options once more
}

/////////////////////////////////////////////////////////////////////////////////////////

int CWeatherProto::SetStatus(int new_status)
{
	// if we don't want to show status for default station
	if (m_iStatus != new_status) {
		int old_status = m_iStatus;
		m_iStatus = new_status != ID_STATUS_OFFLINE ? ID_STATUS_ONLINE : ID_STATUS_OFFLINE;

		ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)old_status, m_iStatus);

		UpdateMenu(m_iStatus != ID_STATUS_OFFLINE);
		if (m_iStatus != ID_STATUS_OFFLINE)
			UpdateAll(FALSE, FALSE);
	}

	return 0;
}

// get capabilities protocol service function
INT_PTR CWeatherProto::GetCaps(int type, MCONTACT)
{
	switch (type) {
	case PFLAGNUM_1:
		// support search and visible list
		return PF1_BASICSEARCH | PF1_ADDSEARCHRES | PF1_EXTSEARCH | PF1_MODEMSGRECV;

	case PFLAGNUM_2:
	case PFLAGNUM_5:
		return PF2_ONLINE | PF2_INVISIBLE | PF2_SHORTAWAY | PF2_LONGAWAY | PF2_LIGHTDND | PF2_HEAVYDND | PF2_FREECHAT;

	case PFLAGNUM_4:
		return PF4_AVATARS | PF4_NOCUSTOMAUTH | PF4_NOAUTHDENYREASON | PF4_FORCEAUTH;

	case PFLAG_UNIQUEIDTEXT:
		return (INT_PTR)TranslateT("Coordinates");
	}
	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////
// nothing to do here because weather proto do not need to retrieve contact info form network
// so just return a 0

void CWeatherProto::AckThreadProc(void *param)
{
	Sleep(100);

	ProtoBroadcastAck((DWORD_PTR)param, ACKTYPE_GETINFO, ACKRESULT_SUCCESS, (HANDLE)1);
}

int CWeatherProto::GetInfo(MCONTACT hContact, int)
{
	ForkThread(&CWeatherProto::AckThreadProc, (void *)hContact);
	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////

void __cdecl CWeatherProto::GetAwayMsgThread(void *arg)
{
	Sleep(100);

	MCONTACT hContact = (DWORD_PTR)arg;
	ptrW wszStatus(db_get_wsa(hContact, "CList", "StatusMsg"));
	ProtoBroadcastAck(hContact, ACKTYPE_AWAYMSG, ACKRESULT_SUCCESS, this, wszStatus);
}

HANDLE CWeatherProto::GetAwayMsg(MCONTACT hContact)
{
	ForkThread(&CWeatherProto::GetAwayMsgThread, (void*)hContact);
	return this;
}