summaryrefslogtreecommitdiff
path: root/protocols/Weather/src/proto.h
blob: b44c193110b74fbeaddb4f008367c4547a9c70be (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#pragma once

struct WIDATAITEM
{
	WIDATAITEM(const wchar_t *_1, const wchar_t *_2, const CMStringW &_3) :
		Name(_1),
		Unit(_2),
		Value(_3)
	{}

	const wchar_t *Name, *Unit;
	CMStringW Value;
};

struct WIDATAITEMLIST : public OBJLIST<WIDATAITEM>
{
	WIDATAITEMLIST() :
		OBJLIST<WIDATAITEM>(20)
	{}

	WIDATAITEM* Find(const wchar_t *pwszName)
	{
		for (auto &it : *this)
			if (!mir_wstrcmp(it->Name, pwszName))
				return it;
		return 0;
	}
};

struct WEATHERINFO
{
	MCONTACT hContact;
	TCHAR id[128];
	TCHAR city[128];
	TCHAR update[64];
	TCHAR cond[128];
	TCHAR temp[16];
	TCHAR low[16];
	TCHAR high[16];
	TCHAR feel[16];
	TCHAR wind[16];
	TCHAR winddir[64];
	TCHAR dewpoint[16];
	TCHAR pressure[16];
	TCHAR humid[16];
	TCHAR vis[16];
	TCHAR sunrise[32];
	TCHAR sunset[32];
};

struct MYOPTIONS
{
	// main options
	uint8_t AutoUpdate;
	uint8_t CAutoUpdate;
	uint8_t UpdateOnlyConditionChanged;
	uint8_t RemoveOldData;
	uint8_t MakeItalic;

	uint16_t UpdateTime;
	uint16_t AvatarSize;

	// units
	uint16_t tUnit;
	uint16_t wUnit;
	uint16_t vUnit;
	uint16_t pUnit;
	uint16_t dUnit;
	uint16_t eUnit;
	wchar_t DegreeSign[4];
	uint8_t DoNotAppendUnit;
	uint8_t NoFrac;

	// advanced
	uint8_t DisCondIcon;

	// popup options
	uint8_t UpdatePopup;
	uint8_t AlertPopup;
	uint8_t PopupOnChange;
	uint8_t ShowWarnings;

	// popup colors
	uint8_t UseWinColors;
	COLORREF BGColour;
	COLORREF TextColour;

	// popup actions
	uint32_t LeftClickAction;
	uint32_t RightClickAction;

	// popup delay
	uint32_t pDelay;

	// other misc stuff
	wchar_t Default[64];
	MCONTACT DefStn;
};

/////////////////////////////////////////////////////////////////////////////////////////
// Protocol class

class CWeatherProto : public PROTO<CWeatherProto>
{
	friend class CEditDlg;
	friend class CPopupOptsDlg;
	friend class CBriefInfoDlg;
	friend class COptionsTextDlg;
	friend class CGeneralOptionsDlg;
	friend class WeatherUserInfoDlg;

	class CProtoImpl
	{
		friend class CWeatherProto;
		CWeatherProto &m_proto;

		CTimer m_start, m_update;

		void OnStart(CTimer *pTimer)
		{
			pTimer->Stop();
			m_proto.StartUpdate();
		}

		void OnUpdate(CTimer *)
		{
			m_proto.DoUpdate();
		}

		CProtoImpl(CWeatherProto &pro) :
			m_proto(pro),
			m_start(Miranda_GetSystemWindow(), UINT_PTR(this) + 1),
			m_update(Miranda_GetSystemWindow(), UINT_PTR(this) + 2)
		{
			m_start.OnEvent = Callback(this, &CProtoImpl::OnStart);
			m_update.OnEvent = Callback(this, &CProtoImpl::OnUpdate);
		}
	}
		m_impl;

	// avatars
	void AvatarDownloaded(MCONTACT hContact);

	INT_PTR __cdecl AdvancedStatusIconSvc(WPARAM, LPARAM);
	INT_PTR __cdecl GetAvatarInfoSvc(WPARAM, LPARAM);

	// contacts
	INT_PTR __cdecl ViewLog(WPARAM, LPARAM);
	INT_PTR __cdecl LoadForecast(WPARAM, LPARAM);
	INT_PTR __cdecl WeatherMap(WPARAM, LPARAM);
	INT_PTR __cdecl EditSettings(WPARAM, LPARAM);

	int __cdecl BriefInfoEvt(WPARAM, LPARAM);

	bool IsMyContact(MCONTACT hContact);

	// conversions
	void numToStr(double num, wchar_t *str, size_t strSize);

	void GetTemp(const wchar_t *tempchar, const wchar_t *unit, wchar_t *str);
	void GetSpeed(const wchar_t *tempchar, const wchar_t *unit, wchar_t *str);
	void GetPressure(const wchar_t *tempchar, const wchar_t *unit, wchar_t *str);
	void GetDist(const wchar_t *tempchar, const wchar_t *unit, wchar_t *str);
	void GetElev(const wchar_t *tempchar, const wchar_t *unit, wchar_t *str);

	// data
	void ConvertDataValue(WIDATAITEM *UpdateData);
	void EraseAllInfo(void);
	WEATHERINFO LoadWeatherInfo(MCONTACT hContact);

	MHttpResponse* RunQuery(const wchar_t *id, int days);

	// menu items
	HGENMENU hEnableDisableMenu;

	void InitMenuItems();
	void UpdateMenu(BOOL State);

	INT_PTR __cdecl EnableDisableCmd(WPARAM, LPARAM);

	// mwin
	void AddFrameWindow(MCONTACT hContact);
	void RemoveFrameWindow(MCONTACT hContact);

	void InitMwin(void);
	void DestroyMwin(void);

	INT_PTR __cdecl Mwin_MenuClicked(WPARAM, LPARAM);

	// options
	void LoadOptions();
	void SaveOptions();
	void RestartTimer();
	void InitPopupOptions(WPARAM);

	int __cdecl OptInit(WPARAM, LPARAM);

	CMStringW GetTextValue(int c);
	void GetVarsDescr(CMStringW &str);

	// popups
	int WPShowMessage(const wchar_t *lpzText, int kind);
	int WeatherPopup(MCONTACT hContact, bool bNew);

	// search
	bool CheckSearch();

	int IDSearch(wchar_t *id, int searchId);
	int NameSearch(wchar_t *name, int searchId);

	void __cdecl NameSearchThread(void *);
	void __cdecl BasicSearchThread(void *);

	// update weather
	std::vector<MCONTACT> m_updateList;

	// check if weather is currently updating
	bool m_bThreadRunning;
	mir_cs m_csUpdate;

	void DoUpdate();
	void StartUpdate();

	int  GetWeatherData(MCONTACT hContact);
	int  UpdateWeather(MCONTACT hContact);
	void UpdateListAdd(MCONTACT hContact);
	MCONTACT UpdateGetFirst();
	void DestroyUpdateList(void);

	void __cdecl UpdateThread(void *);
	void UpdateAll(BOOL AutoUpdate, BOOL RemoveOld);

	INT_PTR __cdecl UpdateSingleStation(WPARAM, LPARAM);
	INT_PTR __cdecl UpdateSingleRemove(WPARAM, LPARAM);

	INT_PTR __cdecl UpdateAllInfo(WPARAM, LPARAM);
	INT_PTR __cdecl UpdateAllRemove(WPARAM, LPARAM);

	// user info
	int __cdecl UserInfoInit(WPARAM, LPARAM);

public:
	CWeatherProto(const char *protoName, const wchar_t *userName);
	~CWeatherProto();

	MYOPTIONS opt;

	CMOption<bool> m_bPopups;
	CMOption<wchar_t *> m_szApiKey;
	INT_PTR __cdecl BriefInfo(WPARAM, LPARAM);

	int MapCondToStatus(MCONTACT hContact);
	HICON GetStatusIcon(MCONTACT hContact);
	HICON GetStatusIconBig(MCONTACT hContact);

	static LRESULT CALLBACK CWeatherProto::PopupWndProc(HWND hWnd, UINT uMsg, WPARAM, LPARAM);

	// PROTO_INTERFACE
	MCONTACT AddToList(int flags, PROTOSEARCHRESULT *psr) override;
	HANDLE   SearchBasic(const wchar_t *id) override;
	HANDLE   SearchAdvanced(MWindow owner) override;
	MWindow  CreateExtendedSearchUI(MWindow owner) override;

	INT_PTR  GetCaps(int type, MCONTACT hContact) override;
	int      SetStatus(int iNewStatus) override;

	void __cdecl GetAwayMsgThread(void *arg);
	HANDLE   GetAwayMsg(MCONTACT hContact) override;

	void __cdecl AckThreadProc(void *arg);
	int      GetInfo(MCONTACT hContact, int) override;

	bool     OnContactDeleted(MCONTACT, uint32_t flags) override;
	void     OnModulesLoaded() override;
	void     OnShutdown() override;

	static void GlobalMenuInit();
	int __cdecl BuildContactMenu(MCONTACT);

	int __cdecl OnToolbarLoaded(WPARAM, LPARAM);
};

typedef CProtoDlgBase<CWeatherProto> CWeatherDlgBase;

/////////////////////////////////////////////////////////////////////////////////////////
// Plugin class

struct CMPlugin : public ACCPROTOPLUGIN<CWeatherProto>
{
	CMPlugin();

	HINSTANCE hIconsDll = nullptr;

	int Load() override;
	int Unload() override;
};