summaryrefslogtreecommitdiff
path: root/plugins/StatusManager/src/ss_profiles.cpp
blob: 521d498fe90c29832c89594dbf6b47690ed220d9 (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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
	StartupStatus Plugin for Miranda-IM (www.miranda-im.org)
	Copyright 2003-2006 P. Boon

	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 "stdafx.h"

#define MAX_MMITEMS		6

extern HINSTANCE hInst;
extern int protoCount;

static int menuprofiles[MAX_MMITEMS];
static HANDLE hProfileServices[MAX_MMITEMS];
static int mcount = 0;

static PROFILECE *pce = NULL;
static int pceCount = 0;

static UINT_PTR releaseTtbTimerId = 0;

static HANDLE hTBModuleLoadedHook;
static HANDLE hLoadAndSetProfileService;
static HANDLE hMessageHook = NULL;

static HWND hMessageWindow = NULL;
static HKINFO *hkInfo = NULL;
static int hkiCount = 0;

static HANDLE* ttbButtons = NULL;
static int ttbButtonCount = 0;

HANDLE hTTBModuleLoadedHook;

static INT_PTR profileService(WPARAM, LPARAM, LPARAM param)
{
	LoadAndSetProfile((WPARAM)menuprofiles[param], 0);
	return 0;
}

static int CreateMainMenuItems(WPARAM, LPARAM)
{
	CMenuItem mi;
	mi.position = 2000100000;
	mi.flags = CMIF_UNICODE;
	mcount = 0;
	int count = GetProfileCount(0, 0);
	for (int i = 0; i < count && mcount < MAX_MMITEMS; i++) {
		wchar_t profilename[128];
		if (!db_get_b(NULL, SSMODULENAME, OptName(i, SETTING_CREATEMMITEM), 0) || GetProfileName(i, (LPARAM)profilename))
			continue;

		if (db_get_b(NULL, SSMODULENAME, OptName(i, SETTING_INSUBMENU), 1) && !mi.root) {
			mi.root = Menu_CreateRoot(MO_STATUS, LPGENW("Status profiles"), 2000100000);
			Menu_ConfigureItem(mi.root, MCI_OPT_UID, "1AB30D51-BABA-4B27-9288-1A12278BAD8D");
		}

		char servicename[128];
		mir_snprintf(servicename, "%s%d", MS_SS_MENUSETPROFILEPREFIX, mcount);
		hProfileServices[mcount] = CreateServiceFunctionParam(servicename, profileService, mcount);

		mi.name.w = profilename;
		mi.position = 2000100000 + mcount;
		mi.pszService = servicename;
		if (Menu_AddStatusMenuItem(&mi))
			menuprofiles[mcount++] = i;
	}

	return 0;
}

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

INT_PTR GetProfileName(WPARAM wParam, LPARAM lParam)
{
	int profile = (int)wParam;
	if (profile < 0) // get default profile
		profile = db_get_w(NULL, SSMODULENAME, SETTING_DEFAULTPROFILE, 0);

	int count = db_get_w(NULL, SSMODULENAME, SETTING_PROFILECOUNT, 0);
	if (profile >= count && count > 0)
		return -1;

	wchar_t* buf = (wchar_t*)lParam;
	if (count == 0) {
		wcsncpy(buf, TranslateT("default"), 128 - 1);
		return 0;
	}

	DBVARIANT dbv;
	char setting[80];
	mir_snprintf(setting, "%d_%s", profile, SETTING_PROFILENAME);
	if (db_get_ws(NULL, SSMODULENAME, setting, &dbv))
		return -1;

	wcsncpy(buf, dbv.ptszVal, 128 - 1); buf[127] = 0;
	db_free(&dbv);
	return 0;
}

INT_PTR GetProfileCount(WPARAM wParam, LPARAM)
{
	int *def = (int*)wParam;
	int count = db_get_w(NULL, SSMODULENAME, SETTING_PROFILECOUNT, 1);
	if (def != 0) {
		*def = db_get_w(NULL, SSMODULENAME, SETTING_DEFAULTPROFILE, 0);
		if (*def >= count)
			*def = 0;
	}

	return count;
}

wchar_t *GetStatusMessage(int profile, char *szProto)
{
	char dbSetting[80];
	DBVARIANT dbv;

	for (int i = 0; i < pceCount; i++) {
		if ((pce[i].profile == profile) && (!mir_strcmp(pce[i].szProto, szProto))) {
			mir_snprintf(dbSetting, "%d_%s_%s", profile, szProto, SETTING_PROFILE_STSMSG);
			if (!db_get_ws(NULL, SSMODULENAME, dbSetting, &dbv)) { // reload from db
				pce[i].msg = (wchar_t*)realloc(pce[i].msg, sizeof(wchar_t)*(mir_wstrlen(dbv.ptszVal) + 1));
				if (pce[i].msg != NULL) {
					mir_wstrcpy(pce[i].msg, dbv.ptszVal);
				}
				db_free(&dbv);
			}
			else {
				if (pce[i].msg != NULL) {
					free(pce[i].msg);
					pce[i].msg = NULL;
				}
			}
			return pce[i].msg;
		}
	}
	pce = (PROFILECE*)realloc(pce, (pceCount + 1)*sizeof(PROFILECE));
	if (pce == NULL)
		return NULL;

	pce[pceCount].profile = profile;
	pce[pceCount].szProto = _strdup(szProto);
	pce[pceCount].msg = NULL;
	mir_snprintf(dbSetting, "%d_%s_%s", profile, szProto, SETTING_PROFILE_STSMSG);
	if (!db_get_ws(NULL, SSMODULENAME, dbSetting, &dbv)) {
		pce[pceCount].msg = wcsdup(dbv.ptszVal);
		db_free(&dbv);
	}
	pceCount++;

	return pce[pceCount - 1].msg;
}

int GetProfile(int profile, TSettingsList& arSettings)
{
	if (profile < 0) // get default profile
		profile = db_get_w(NULL, SSMODULENAME, SETTING_DEFAULTPROFILE, 0);

	int count = db_get_w(NULL, SSMODULENAME, SETTING_PROFILECOUNT, 0);
	if (profile >= count && count > 0)
		return -1;

	arSettings.destroy();

	// if count == 0, continue so the default profile will be returned
	PROTOACCOUNT** protos;
	Proto_EnumAccounts(&count, &protos);

	for (int i = 0; i < count; i++)
		if (IsSuitableProto(protos[i]))
			arSettings.insert(new TSSSetting(profile, protos[i]));

	return (arSettings.getCount() == 0) ? -1 : 0;
}

static void CALLBACK releaseTtbTimerFunction(HWND, UINT, UINT_PTR, DWORD)
{
	KillTimer(NULL, releaseTtbTimerId);
	for (int i = 0; i < ttbButtonCount; i++)
		CallService(MS_TTB_SETBUTTONSTATE, (WPARAM)ttbButtons[i], 0);
}

INT_PTR LoadAndSetProfile(WPARAM iProfileNo, LPARAM)
{
	// wParam == profile no.
	int profile = (int)iProfileNo;
	TSettingsList profileSettings(10, CompareSettings);
	if (!GetProfile(profile, profileSettings)) {
		profile = (profile >= 0) ? profile : db_get_w(NULL, SSMODULENAME, SETTING_DEFAULTPROFILE, 0);

		char setting[64];
		mir_snprintf(setting, "%d_%s", profile, SETTING_SHOWCONFIRMDIALOG);
		if (!db_get_b(NULL, SSMODULENAME, setting, 0))
			CallService(MS_CS_SETSTATUSEX, (WPARAM)&profileSettings, 0);
		else
			CallService(MS_CS_SHOWCONFIRMDLGEX, (WPARAM)&profileSettings, (LPARAM)db_get_dw(NULL, SSMODULENAME, SETTING_DLGTIMEOUT, 5));
	}

	// add timer here
	if (hTTBModuleLoadedHook)
		releaseTtbTimerId = SetTimer(NULL, 0, 100, releaseTtbTimerFunction);

	return 0;
}

static UINT GetFsModifiers(WORD wHotKey)
{
	UINT fsm = 0;
	if (HIBYTE(wHotKey)&HOTKEYF_ALT)
		fsm |= MOD_ALT;
	if (HIBYTE(wHotKey)&HOTKEYF_CONTROL)
		fsm |= MOD_CONTROL;
	if (HIBYTE(wHotKey)&HOTKEYF_SHIFT)
		fsm |= MOD_SHIFT;
	if (HIBYTE(wHotKey)&HOTKEYF_EXT)
		fsm |= MOD_WIN;

	return fsm;
}

static DWORD CALLBACK MessageWndProc(HWND, UINT msg, WPARAM wParam, LPARAM)
{
	if (msg == WM_HOTKEY) {
		for (int i = 0; i < hkiCount; i++)
			if ((int)hkInfo[i].id == wParam)
				LoadAndSetProfile((WPARAM)hkInfo[i].profile, 0);
	}

	return TRUE;
}

static int UnregisterHotKeys()
{
	if (hkInfo != NULL) {
		for (int i = 0; i < hkiCount; i++) {
			UnregisterHotKey(hMessageWindow, (int)hkInfo[i].id);
			GlobalDeleteAtom(hkInfo[i].id);
		}
		free(hkInfo);
	}
	DestroyWindow(hMessageWindow);

	hkiCount = 0;
	hkInfo = NULL;
	hMessageWindow = NULL;

	return 0;
}

// assumes UnregisterHotKeys was called before
static int RegisterHotKeys()
{
	hMessageWindow = CreateWindowEx(0, L"STATIC", NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
	SetWindowLongPtr(hMessageWindow, GWLP_WNDPROC, (LONG_PTR)MessageWndProc);

	int count = GetProfileCount(0, 0);
	for (int i = 0; i < count; i++) {
		if (!db_get_b(NULL, SSMODULENAME, OptName(i, SETTING_REGHOTKEY), 0))
			continue;

		WORD wHotKey = db_get_w(NULL, SSMODULENAME, OptName(i, SETTING_HOTKEY), 0);
		hkInfo = (HKINFO*)realloc(hkInfo, (hkiCount + 1)*sizeof(HKINFO));
		if (hkInfo == NULL)
			return -1;

		char atomname[255];
		mir_snprintf(atomname, "StatusProfile_%d", i);
		hkInfo[hkiCount].id = GlobalAddAtomA(atomname);
		if (hkInfo[hkiCount].id == 0)
			continue;

		hkInfo[hkiCount].profile = i;
		hkiCount++;
		RegisterHotKey(hMessageWindow, (int)hkInfo[hkiCount - 1].id, GetFsModifiers(wHotKey), LOBYTE(wHotKey));
	}

	if (hkiCount == 0)
		UnregisterHotKeys();

	return 0;
}

int SSLoadMainOptions()
{
	if (hTTBModuleLoadedHook) {
		RemoveTopToolbarButtons();
		CreateTopToolbarButtons(0, 0);
	}

	UnregisterHotKeys();
	RegisterHotKeys();
	return 0;
}

int LoadProfileModule()
{
	hLoadAndSetProfileService = CreateServiceFunction(MS_SS_LOADANDSETPROFILE, LoadAndSetProfile);
	return 0;
}

int InitProfileModule()
{
	hTTBModuleLoadedHook = HookEvent(ME_TTB_MODULELOADED, CreateTopToolbarButtons);

	HookEvent(ME_CLIST_PREBUILDSTATUSMENU, CreateMainMenuItems);

	CreateMainMenuItems(0, 0);
	RegisterHotKeys();
	return 0;
}

int DeinitProfilesModule()
{
	for (int i = 0; i < mcount; i++)
		DestroyServiceFunction(hProfileServices[i]);

	if (pce) {
		for (int i = 0; i < pceCount; i++)
			free(pce[i].szProto);
		free(pce);
	}

	UnregisterHotKeys();
	RemoveTopToolbarButtons();

	DestroyServiceFunction(hLoadAndSetProfileService);
	return 0;
}