summaryrefslogtreecommitdiff
path: root/plugins/Popup/src/notifications.cpp
blob: 413959315afa8ea7b0943b7d03f4693fc41229a5 (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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
Popup Plus plugin for Miranda IM

Copyright	© 2002 Luca Santarelli,
© 2004-2007 Victor Pavlychko
© 2010 MPK
© 2010 Merlin_de

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; either version 2
of the License, or (at your option) any later version.

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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "headers.h"

HANDLE g_hntfError, g_hntfWarning, g_hntfNotification;

#define PopupNotificationData_SIGNATURE		0x11BEDA1A

int TreeDataSortFunc(const POPUPTREEDATA *p1, const POPUPTREEDATA *p2)
{
	if (int cmp = mir_tstrcmp(p1->pszTreeRoot, p2->pszTreeRoot))
		return cmp;
	return mir_tstrcmp(p1->pszDescription, p2->pszDescription);
}

LIST<POPUPTREEDATA> gTreeData(20, TreeDataSortFunc);

// interface
void LoadNotifications()
{
	POPUPNOTIFICATION notification = { 0 };
	notification.cbSize = sizeof(notification);
	notification.actionCount = 0;
	notification.lpActions = 0;

	mir_strncpy(notification.lpzGroup, "Misc", sizeof(notification.lpzName));
	mir_strncpy(notification.lpzName, "Warning", sizeof(notification.lpzName));
	notification.lchIcon = (HICON)LoadImage(hInst, MAKEINTRESOURCE(IDI_MB_WARN), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR | LR_SHARED);
	notification.colorBack = RGB(210, 210, 150);
	notification.colorText = RGB(0, 0, 0);
	notification.iSeconds = 10;
	g_hntfWarning = RegisterNotification(&notification);

	mir_strncpy(notification.lpzGroup, "Misc", sizeof(notification.lpzName));
	mir_strncpy(notification.lpzName, "Notification", sizeof(notification.lpzName));
	notification.lchIcon = (HICON)LoadImage(hInst, MAKEINTRESOURCE(IDI_MB_INFO), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR | LR_SHARED);
	notification.colorBack = RGB(230, 230, 230);
	notification.colorText = RGB(0, 0, 0);
	notification.iSeconds = 7;
	g_hntfNotification = RegisterNotification(&notification);

	mir_strncpy(notification.lpzGroup, "Misc", sizeof(notification.lpzName));
	mir_strncpy(notification.lpzName, "Error", sizeof(notification.lpzName));
	notification.lchIcon = (HICON)LoadImage(hInst, MAKEINTRESOURCE(IDI_MB_STOP), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR | LR_SHARED);
	notification.colorBack = RGB(191, 0, 0);
	notification.colorText = RGB(255, 245, 225);
	notification.iSeconds = -1;
	g_hntfError = RegisterNotification(&notification);
}

void FreePopupClass(POPUPTREEDATA *ptd)
{
	if (ptd->typ == 2) {
		mir_free(ptd->pupClass.pszName);
		mir_free(ptd->pupClass.pszDescription);
	}
	mir_free(ptd->pszTreeRoot);
	mir_free(ptd->pszDescription);
	mir_free(ptd);
}

void UnloadTreeData()
{
	for (int i = 0; i < gTreeData.getCount(); ++i)
		FreePopupClass(gTreeData[i]);
	gTreeData.destroy();
}

void SaveNotificationSettings(POPUPTREEDATA *ptd, char* szModul)
{
	if (ptd->typ == 1) {
		char setting[2 * MAXMODULELABELLENGTH];

		mir_snprintf(setting, "{%s/%s}Timeout",
			ptd->notification.lpzGroup,
			ptd->notification.lpzName);
		db_set_w(NULL, szModul, setting, ptd->notification.iSeconds);

		mir_snprintf(setting, "{%s/%s}enabled",
			ptd->notification.lpzGroup,
			ptd->notification.lpzName);
		db_set_b(NULL, szModul, setting, ptd->enabled);

		mir_snprintf(setting, "{%s/%s}TimeoutVal",
			ptd->notification.lpzGroup,
			ptd->notification.lpzName);
		db_set_w(NULL, szModul, setting, ptd->timeoutValue);

		mir_snprintf(setting, "{%s/%s}disableWhen",
			ptd->notification.lpzGroup,
			ptd->notification.lpzName);
		db_set_b(NULL, szModul, setting, ptd->disableWhen);

		mir_snprintf(setting, "{%s/%s}leftAction",
			ptd->notification.lpzGroup,
			ptd->notification.lpzName);
		db_set_s(NULL, szModul, setting, ptd->leftAction);

		mir_snprintf(setting, "{%s/%s}rightAction",
			ptd->notification.lpzGroup,
			ptd->notification.lpzName);
		db_set_s(NULL, szModul, setting, ptd->rightAction);

		for (int i = 0; i < ptd->notification.actionCount; ++i) {
			POPUPNOTIFYACTION &p = ptd->notification.lpActions[i];
			if (!mir_strcmp(ptd->leftAction, p.lpzTitle))
				db_set(NULL, p.lpzLModule, p.lpzLSetting, &p.dbvLData);

			if (!mir_strcmp(ptd->rightAction, p.lpzTitle))
				db_set(NULL, p.lpzRModule, p.lpzRSetting, &p.dbvRData);
		}
	}
}

void LoadNotificationSettings(POPUPTREEDATA *ptd, char* szModul)
{
	if (ptd->typ == 1) {
		char setting[2 * MAXMODULELABELLENGTH];
		char *szTmp = NULL;

		mir_snprintf(setting, "{%s/%s}enabled", ptd->notification.lpzGroup, ptd->notification.lpzName);
		ptd->enabled =
			(signed char)db_get_b(NULL, szModul, setting, TRUE);

		mir_snprintf(setting, "{%s/%s}Timeout", ptd->notification.lpzGroup, ptd->notification.lpzName);
		ptd->notification.iSeconds =
			(signed char)db_get_w(NULL, szModul, setting, ptd->notification.iSeconds);

		mir_snprintf(setting, "{%s/%s}TimeoutVal", ptd->notification.lpzGroup, ptd->notification.lpzName);
		ptd->timeoutValue =
			(signed char)db_get_w(NULL, szModul, setting,
			ptd->notification.iSeconds ? ptd->notification.iSeconds : 0);

		mir_snprintf(setting, "{%s/%s}disableWhen", ptd->notification.lpzGroup, ptd->notification.lpzName);
		ptd->disableWhen =
			db_get_b(NULL, szModul, setting, 0);

		mir_snprintf(setting, "{%s/%s}leftAction", ptd->notification.lpzGroup, ptd->notification.lpzName);
		szTmp = db_get_s(NULL, szModul, setting, ptd->notification.lpzLAction);
		mir_strncpy(ptd->leftAction, szTmp, sizeof(ptd->leftAction));
		mir_free(szTmp); szTmp = NULL;

		mir_snprintf(setting, "{%s/%s}rightAction", ptd->notification.lpzGroup, ptd->notification.lpzName);
		szTmp = db_get_s(NULL, szModul, setting, ptd->notification.lpzRAction);
		mir_strncpy(ptd->rightAction, szTmp, sizeof(ptd->rightAction));
		mir_free(szTmp); szTmp = NULL;
	}
}

HANDLE RegisterNotification(POPUPNOTIFICATION *notification)
{
	POPUPTREEDATA *ptd = (POPUPTREEDATA *)mir_alloc(sizeof(POPUPTREEDATA));
	ptd->signature = PopupNotificationData_SIGNATURE;
	ptd->typ = 1;
	ptd->pszTreeRoot = mir_a2t(notification->lpzGroup);
	ptd->pszDescription = mir_a2t(notification->lpzName);
	ptd->notification = *notification;
	if (!ptd->notification.lpzLAction) ptd->notification.lpzLAction = POPUP_ACTION_NOTHING;
	if (!ptd->notification.lpzRAction) ptd->notification.lpzRAction = POPUP_ACTION_DISMISS;
	LoadNotificationSettings(ptd, "PopupNotifications");

	// ugly hack to make reset always possible
	SaveNotificationSettings(ptd, "PopupNotifications");

	FontID fontid = { 0 };
	fontid.cbSize = sizeof(fontid);
	mir_snprintf(fontid.group, SIZEOF(fontid.group), PU_FNT_AND_COLOR"/%s", notification->lpzGroup);
	mir_strcpy(fontid.dbSettingsGroup, "PopupNotifications");
	fontid.flags = FIDF_DEFAULTVALID;
	fontid.deffontsettings.charset = DEFAULT_CHARSET;
	fontid.deffontsettings.colour = ptd->notification.colorText;
	fontid.deffontsettings.size = -11;
	mir_strncpy(fontid.deffontsettings.szFace, "MS Shell Dlg", SIZEOF(fontid.deffontsettings.szFace));
	fontid.deffontsettings.style = 0;
	mir_snprintf(fontid.name, SIZEOF(fontid.name), "%s (colors only)", notification->lpzName);
	mir_snprintf(fontid.prefix, SIZEOF(fontid.prefix), "{%s/%s}text", notification->lpzGroup, notification->lpzName);
	fontid.deffontsettings.style = 0;
	FontRegister(&fontid);

	ColourID colourid = { 0 };
	colourid.cbSize = sizeof(colourid);
	mir_snprintf(colourid.group, SIZEOF(colourid.group), PU_FNT_AND_COLOR"/%s", notification->lpzGroup);
	mir_strcpy(colourid.dbSettingsGroup, "PopupNotifications");
	mir_snprintf(colourid.name, SIZEOF(colourid.name), "%s (colors only)", notification->lpzName);
	mir_snprintf(colourid.setting, SIZEOF(colourid.setting), "{%s/%s}backColor", notification->lpzGroup, notification->lpzName);
	colourid.defcolour = ptd->notification.colorBack;
	ColourRegister(&colourid);

	char section[MAXMODULELABELLENGTH], setting[MAXMODULELABELLENGTH];
	mir_snprintf(section, SIZEOF(section), "Popups/%s", notification->lpzGroup);
	mir_snprintf(setting, MODULNAME"_%s_%s", notification->lpzGroup, notification->lpzName);

	SKINICONDESC sid = { 0 };
	sid.section.a = section;
	sid.cx = sid.cy = 16;
	sid.pszName = setting;
	sid.description.a = notification->lpzName;
	sid.hDefaultIcon = notification->lchIcon;
	Skin_AddIcon(&sid);

	gTreeData.insert(ptd);
	return (HANDLE)ptd;
}

HANDLE FindTreeData(LPTSTR group, LPTSTR name, BYTE typ)
{
	for (int i = 0; i < gTreeData.getCount(); i++) {
		POPUPTREEDATA *p = gTreeData[i];
		if (p->typ == typ && (!group || (mir_tstrcmp(p->pszTreeRoot, group) == 0)) && (!name || (mir_tstrcmp(p->pszDescription, name) == 0)))
			return p;
	}
	return NULL;
}

void FillNotificationData(POPUPDATA2 *ppd, DWORD *disableWhen)
{
	if (!IsValidNotification(ppd->lchNotification)) {
		*disableWhen = 0;
		return;
	}

	POPUPTREEDATA *ptd = (POPUPTREEDATA *)ppd->lchNotification;

	ppd->iSeconds = ptd->timeoutValue;
	*disableWhen = ptd->enabled ? ptd->disableWhen : 0xFFFFFFFF;

	LOGFONTA lf; // dummy to make FS happy (use LOGFONTA coz we use MS_FONT_GET)
	FontID fontid = { 0 }; // use ansi version of fontID coz POPUPNOTIFICATION use char
	fontid.cbSize = sizeof(fontid);
	mir_snprintf(fontid.group, SIZEOF(fontid.group), PU_FNT_AND_COLOR"/%s", ptd->notification.lpzGroup);
	mir_snprintf(fontid.name, SIZEOF(fontid.name), "%s (colors only)", ptd->notification.lpzName);
	ppd->colorText = (COLORREF)CallService(MS_FONT_GET, (WPARAM)&fontid, (LPARAM)&lf);

	ColourID colourid = { 0 }; // use ansi version of ColourID coz POPUPNOTIFICATION use char
	colourid.cbSize = sizeof(colourid);
	mir_snprintf(colourid.group, SIZEOF(colourid.group), PU_FNT_AND_COLOR"/%s", ptd->notification.lpzGroup);
	mir_snprintf(colourid.name, SIZEOF(colourid.name), "%s (colors only)", ptd->notification.lpzName);
	ppd->colorBack = (COLORREF)CallService(MS_COLOUR_GET, (WPARAM)&colourid, 0);

	char setting[MAXMODULELABELLENGTH];
	mir_snprintf(setting, MODULNAME"_%s_%s", ptd->notification.lpzGroup, ptd->notification.lpzName);
	ppd->lchIcon = Skin_GetIcon(setting);
}

bool IsValidNotification(HANDLE hNotification)
{
	if (!hNotification) return false;

	bool res = false;
	__try {
		if (((POPUPTREEDATA *)hNotification)->signature == PopupNotificationData_SIGNATURE)
			res = true;
	}
	__except (EXCEPTION_EXECUTE_HANDLER)
	{
		res = false;
	}
	return res;
}

bool PerformAction(HANDLE hNotification, HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
	if (!IsValidNotification(hNotification))
		return false;

	POPUPTREEDATA *ptd = (POPUPTREEDATA *)hNotification;
	char *lpzAction = NULL;
	switch (message) {
	case WM_LBUTTONUP:
	case WM_COMMAND:
		lpzAction = ptd->leftAction;
		break;

	case WM_RBUTTONUP:
	case WM_CONTEXTMENU:
		lpzAction = ptd->rightAction;
		break;

	default:
		return false;
	}

	if (!mir_strcmp(lpzAction, POPUP_ACTION_NOTHING))
		return true;

	if (!mir_strcmp(lpzAction, POPUP_ACTION_DISMISS)) {
		PUDeletePopup(hwnd);
		return true;
	}

	for (int i = 0; i < ptd->notification.actionCount; ++i) {
		if (!(ptd->notification.lpActions[i].dwFlags&PNAF_CALLBACK))
			continue;
		if (mir_strcmp(ptd->notification.lpActions[i].lpzTitle, lpzAction))
			continue;

		ptd->notification.lpActions[i].pfnCallback(hwnd, message, wparam, lparam,
			ptd->notification.lpActions[i].dwCookie);
		return true;
	}

	return false;
}