summaryrefslogtreecommitdiff
path: root/plugins/BossKeyPlus/src/BossKey.cpp
blob: a032cf2454be36a9403d5eb6b01953913638a680 (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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
/*
	BossKey - Hide Miranda from your boss :)
	Copyright (C) 2002-2003 Goblineye Entertainment, (C) 2007-2010 Billy_Bons

	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"

HINSTANCE g_hInstance;
CLIST_INTERFACE *pcli;
HGENMENU g_hMenuItem;
HANDLE g_hHideService;
HWINEVENTHOOK g_hWinHook;
HWND g_hListenWindow, g_hDlgPass, hOldForegroundWindow;
HWND_ITEM *g_pMirWnds; // a pretty simple linked list
HMODULE hDwmApi;
DWORD g_dwMirandaPID;
WORD g_wMask, g_wMaskAdv;
bool g_bWindowHidden, g_fPassRequested, g_TrayIcon;
char g_password[MAXPASSLEN + 1];
HKL oldLangID, oldLayout;
int protoCount, hLangpack;
PROTOACCOUNT **proto;
unsigned *oldStatus;
TCHAR **oldStatusMsg;
BYTE g_bOldSetting;

PFNDwmIsCompositionEnabled dwmIsCompositionEnabled;

static void LanguageChanged(HWND hDlg);

static PLUGININFOEX pluginInfo = {
	sizeof(PLUGININFOEX),
	__PLUGIN_NAME,
	PLUGIN_MAKE_VERSION(__MAJOR_VERSION, __MINOR_VERSION, __RELEASE_NUM, __BUILD_NUM),
	__DESCRIPTION,
	__AUTHOR,
	__AUTHOREMAIL,
	__COPYRIGHT,
	__AUTHORWEB,
	UNICODE_AWARE,
	// {4FAC353D-0A36-44A4-9064-6759C53AE782}
	{ 0x4fac353d, 0x0a36, 0x44a4, { 0x90, 0x64, 0x67, 0x59, 0xc5, 0x3a, 0xe7, 0x82 } }
};

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD, LPVOID)
{
	g_hInstance = hinstDLL;
	return TRUE;
}

extern "C" __declspec(dllexport) PLUGININFOEX* MirandaPluginInfoEx(DWORD)
{
	return &pluginInfo;
}

static BOOL IsAeroMode()
{
	BOOL result;
	return dwmIsCompositionEnabled && (dwmIsCompositionEnabled(&result) == S_OK) && result;
}

INT_PTR CALLBACK DlgStdInProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	static DWORD dwOldIcon = 0;
	HICON hIcon = 0;
	UINT uid;

	switch (uMsg){
	case WM_INITDIALOG:
		g_hDlgPass = hDlg;
		hIcon = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_DLGPASSWD));
		dwOldIcon = SetClassLongPtr(hDlg, GCLP_HICON, (LONG)hIcon); // set alt+tab icon
		SendDlgItemMessage(hDlg, IDC_EDIT1, EM_LIMITTEXT, MAXPASSLEN, 0);

		if (IsAeroMode())
		{
			SetWindowLongPtr(hDlg, GWL_STYLE, GetWindowLongPtr(hDlg, GWL_STYLE) | WS_DLGFRAME | WS_SYSMENU);
			SetWindowLongPtr(hDlg, GWL_EXSTYLE, GetWindowLongPtr(hDlg, GWL_EXSTYLE) | WS_EX_TOOLWINDOW);
			RECT rect;
			GetClientRect(hDlg, &rect);
			SetWindowPos(hDlg, 0, 0, 0, rect.right, rect.bottom + GetSystemMetrics(SM_CYCAPTION), SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOZORDER);
		}
		SendDlgItemMessage(hDlg, IDC_HEADERBAR, WM_SETICON, 0, (LPARAM)hIcon);

		TranslateDialogDefault(hDlg);
		oldLangID = 0;
		SetTimer(hDlg, 1, 200, NULL);

		oldLayout = GetKeyboardLayout(0);
		if (MAKELCID((WORD)oldLayout & 0xffffffff, SORT_DEFAULT) != (LCID)0x00000409)
			ActivateKeyboardLayout((HKL)0x00000409, 0);
		LanguageChanged(hDlg);
		return TRUE;

	case WM_CTLCOLORSTATIC:
		if (GetWindowLongPtr((HWND)lParam, GWLP_ID) != IDC_LANG)
			break;

		SetTextColor((HDC)wParam, GetSysColor(COLOR_HIGHLIGHTTEXT));
		SetBkMode((HDC)wParam, TRANSPARENT);
		return (INT_PTR)GetSysColorBrush(COLOR_HIGHLIGHT);

	case WM_COMMAND:
		uid = LOWORD(wParam);
		if (uid == IDOK){
			char password[MAXPASSLEN + 1] = { 0 };
			int passlen = GetDlgItemTextA(hDlg, IDC_EDIT1, password, _countof(password));
			if (passlen == 0) {
				SetDlgItemText(hDlg, IDC_HEADERBAR, TranslateT("Miranda NG is locked.\nEnter password to unlock it."));
				SendDlgItemMessage(hDlg, IDC_HEADERBAR, WM_NCPAINT, 0, 0);
				break;
			}
			else if (mir_strcmp(password, g_password)) {
				SetDlgItemText(hDlg, IDC_HEADERBAR, TranslateT("Password is not correct!\nPlease, enter correct password."));
				SendDlgItemMessage(hDlg, IDC_HEADERBAR, WM_NCPAINT, 0, 0);
				SetDlgItemText(hDlg, IDC_EDIT1, _T(""));
				break;
			}
			else EndDialog(hDlg, IDOK);
		}
		else if (uid == IDCANCEL)
			EndDialog(hDlg, IDCANCEL);

	case WM_TIMER:
		LanguageChanged(hDlg);
		break;

	case WM_DESTROY:
		KillTimer(hDlg, 1);
		if (GetKeyboardLayout(0) != oldLayout)
			ActivateKeyboardLayout(oldLayout, 0);
		SetClassLongPtr(hDlg, GCLP_HICON, (long)dwOldIcon);
		DestroyIcon(hIcon);
		break;
	}
	return FALSE;
}

static void LanguageChanged(HWND hDlg)
{
	HKL LangID = GetKeyboardLayout(0);
	if (LangID != oldLangID)
	{
		char Lang[3] = { 0 };
		oldLangID = LangID;
		GetLocaleInfoA(MAKELCID(((WORD)LangID & 0xffffffff), SORT_DEFAULT), LOCALE_SABBREVLANGNAME, Lang, 2);
		Lang[0] = toupper(Lang[0]);
		Lang[1] = tolower(Lang[1]);
		SetDlgItemTextA(hDlg, IDC_LANG, Lang);
	}
}

BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM)
{
	DWORD dwWndPID;
	GetWindowThreadProcessId(hWnd, &dwWndPID);

	if ((g_dwMirandaPID == dwWndPID) && hWnd != g_hDlgPass && IsWindowVisible(hWnd))
	{
		TCHAR szTemp[32];
		GetClassName(hWnd, szTemp, 32);

		if (mir_tstrcmp(szTemp, _T("MirandaThumbsWnd")) == 0) // hide floating contacts
		{
			CallService("FloatingContacts/MainHideAllThumbs", 0, 0);
			g_bOldSetting |= OLD_FLTCONT;
		}
		else if (mir_tstrcmp(szTemp, _T("PopupWnd2")) == 0 || mir_tstrcmp(szTemp, _T("YAPPWinClass")) == 0) // destroy opened popups
			PUDeletePopup(hWnd);
		else
		{
			HWND_ITEM *node = new HWND_ITEM;
			node->hWnd = hWnd;
			// add to list
			node->next = g_pMirWnds;
			g_pMirWnds = node;
			ShowWindow(hWnd, SW_HIDE);
		}
	}
	return true;
}

TCHAR* GetDefStatusMsg(unsigned uStatus, const char* szProto)
{
	return (TCHAR*)CallService(MS_AWAYMSG_GETSTATUSMSGT, uStatus, (LPARAM)szProto);
}

void SetStatus(const char* szProto, unsigned status, TCHAR *tszAwayMsg)
{
	if (tszAwayMsg && CallProtoService(szProto, PS_GETCAPS, PFLAGNUM_1, 0) & PF1_MODEMSGSEND)
		CallProtoService(szProto, PS_SETAWAYMSG, status, (LPARAM)tszAwayMsg);

	CallProtoService(szProto, PS_SETSTATUS, status, 0);
}

static int ChangeAllProtoStatuses(unsigned statusMode, TCHAR *msg)
{
	for (int i = 0; i < protoCount; i++)
	{
		unsigned status = CallProtoService(proto[i]->szModuleName, PS_GETSTATUS, 0, 0);
		if (
			(g_wMask & OPT_ONLINEONLY) ? // check "Change only if current status is Online" option
			((status == ID_STATUS_ONLINE) || (status == ID_STATUS_FREECHAT)) // process only "online" and "free for chat"
			:
			((status > ID_STATUS_OFFLINE) && (status < ID_STATUS_IDLE) && (status != ID_STATUS_INVISIBLE))) // process all existing statuses except for "invisible" & "offline"
		{
			if (g_wMask & OPT_SETONLINEBACK){ // need to save old statuses & status messages
				oldStatus[i] = status;
				if (ProtoServiceExists(proto[i]->szModuleName, PS_GETMYAWAYMSG))
					oldStatusMsg[i] = (TCHAR*)CallProtoService(proto[i]->szModuleName, PS_GETMYAWAYMSG, 0, SGMA_TCHAR);
				else
					oldStatusMsg[i] = GetDefStatusMsg(status, proto[i]->szModuleName);
			}
			SetStatus(proto[i]->szModuleName, statusMode, msg);
		}
	}
	return 0;
}

static int BackAllProtoStatuses(void)
{
	for (int i = 0; i < protoCount; i++)
	{
		if (oldStatus[i])
		{
			SetStatus(proto[i]->szModuleName, oldStatus[i], oldStatusMsg[i]);
			if (oldStatusMsg[i])
			{
				mir_free(oldStatusMsg[i]);
				oldStatusMsg[i] = 0;
			}
			oldStatus[i] = 0;
		}
	}
	return 0;
}

static void CreateTrayIcon(bool create)
{
	NOTIFYICONDATA nim;
	DBVARIANT dbVar;
	if (!db_get_ts(NULL, MOD_NAME, "ToolTipText", &dbVar)) {
		_tcsncpy_s(nim.szTip, dbVar.ptszVal, _TRUNCATE);
		db_free(&dbVar);
	}
	else
		_tcsncpy_s(nim.szTip, _T("Miranda NG"), _TRUNCATE);

	nim.cbSize = sizeof(nim);
	nim.hWnd = g_hListenWindow;
	nim.uID = 100;
	nim.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
	nim.hIcon = IcoLib_GetIcon("hidemim");
	nim.uCallbackMessage = WM_USER + 24;
	Shell_NotifyIcon(create ? NIM_ADD : NIM_DELETE, &nim);
	g_TrayIcon = create;
}

static void RestoreOldSettings(void)
{
	if (g_bOldSetting & OLD_POPUP)
		CallService(MS_POPUP_QUERY, PUQS_ENABLEPOPUPS, 0);

	if (g_bOldSetting & OLD_SOUND)
		db_set_b(NULL, "Skin", "UseSound", 1);

	if (g_bOldSetting & OLD_FLTCONT) // show Floating contacts if needed
	{
		if (ServiceExists("FloatingContacts/MainHideAllThumbs"))
			CallService("FloatingContacts/MainHideAllThumbs", 0, 0);
		else
			db_set_b(NULL, "FloatingContacts", "HideAll", 0);
	}
	g_bOldSetting = 0;
}

LRESULT CALLBACK ListenWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg){
	case WM_WTSSESSION_CHANGE:
		if (wParam == WTS_SESSION_LOCK && g_wMaskAdv & OPT_HIDEIFLOCK && !g_bWindowHidden) // Windows locked
			PostMessage(hWnd, WM_USER + 40, 0, 0);
		return 0;

	case WM_USER + 24:
		if (lParam == WM_LBUTTONDBLCLK)
			PostMessage(hWnd, WM_USER + 52, 0, 0);
		return 0;

	case WM_USER + 40: // hide
	{
		if (g_bWindowHidden || g_fOptionsOpen) // already hidden or in options, no hiding
			break;

		DWORD dwWndPID; // remember foreground window
		HWND hForegroundWnd = GetForegroundWindow();
		GetWindowThreadProcessId(hForegroundWnd, &dwWndPID);
		if (g_dwMirandaPID == dwWndPID)
			hOldForegroundWindow = hForegroundWnd;

		EnumWindows(EnumWindowsProc, 0);

		if (g_wMask & OPT_CHANGESTATUS) // is this even needed?
		{
			BYTE bReqMode = db_get_b(NULL, MOD_NAME, "stattype", 2);
			unsigned uMode = (STATUS_ARR_TO_ID[bReqMode]);
			DBVARIANT dbVar;
			if (g_wMask & OPT_USEDEFMSG || db_get_ts(NULL, MOD_NAME, "statmsg", &dbVar))
			{
				TCHAR *ptszDefMsg = GetDefStatusMsg(uMode, 0);
				ChangeAllProtoStatuses(uMode, ptszDefMsg);
				mir_free(ptszDefMsg);
			}
			else
			{
				if (ServiceExists(MS_VARS_FORMATSTRING))
				{
					TCHAR *ptszParsed = variables_parse(dbVar.ptszVal, 0, 0);
					ChangeAllProtoStatuses(uMode, ptszParsed);
					mir_free(ptszParsed);
				}
				else
					ChangeAllProtoStatuses(uMode, dbVar.ptszVal);
				db_free(&dbVar);
			}
		}

		pcli->pfnTrayIconDestroy(pcli->hwndContactList);
		pcli->pfnUninitTray();

		if (g_wMask & OPT_TRAYICON)
			CreateTrayIcon(true);

		// disable popups
		if (CallService(MS_POPUP_QUERY, PUQS_GETSTATUS, 0) == 1)
		{
			// save current
			g_bOldSetting |= OLD_POPUP;
			CallService(MS_POPUP_QUERY, PUQS_DISABLEPOPUPS, 0);
		}

		// disable sounds
		if ((g_wMask & OPT_DISABLESNDS) && db_get_b(NULL, "Skin", "UseSound", 1))
		{
			// save current
			g_bOldSetting |= OLD_SOUND;
			db_set_b(NULL, "Skin", "UseSound", 0);
		}

		g_bWindowHidden = true;

		g_bOldSetting |= OLD_WASHIDDEN;
		db_set_b(NULL, MOD_NAME, "OldSetting", g_bOldSetting);
	}
	return 0;

	case WM_USER + 52: // back
	{
		if (!g_bWindowHidden || g_fPassRequested)
			break;

		if (g_wMask & OPT_REQPASS){  //password request
			DBVARIANT dbVar;
			if (!db_get_s(NULL, MOD_NAME, "password", &dbVar)) {
				g_fPassRequested = true;

				strncpy(g_password, dbVar.pszVal, MAXPASSLEN);
				db_free(&dbVar);

				int res = DialogBox(g_hInstance, (MAKEINTRESOURCE(IDD_PASSDIALOGNEW)), GetForegroundWindow(), DlgStdInProc);

				g_fPassRequested = false;
				if (res != IDOK) return 0;
			}
		}

		if (g_wMask & OPT_CHANGESTATUS && g_wMask & OPT_SETONLINEBACK) // set back to some status
			BackAllProtoStatuses();

		HWND_ITEM *pCurWnd = g_pMirWnds;
		while (pCurWnd != NULL)
		{
			HWND_ITEM *pNextWnd = pCurWnd->next;
			TCHAR szTemp[32];
			GetClassName(pCurWnd->hWnd, szTemp, 32);

			if (IsWindow(pCurWnd->hWnd) && mir_tstrcmp(szTemp, _T("SysShadow")) != 0) // precaution
				ShowWindow(pCurWnd->hWnd, SW_SHOW);

			delete pCurWnd; // bye-bye
			pCurWnd = pNextWnd; // traverse to next item
		}
		g_pMirWnds = NULL;

		if (hOldForegroundWindow)
		{
			SetForegroundWindow(hOldForegroundWindow);
			hOldForegroundWindow = NULL;
		}

		RestoreOldSettings();

		if (g_TrayIcon) CreateTrayIcon(false);

		pcli->pfnInitTray();
		pcli->pfnTrayIconInit(pcli->hwndContactList);

		// force a redraw
		// should prevent drawing problems
		InvalidateRect(pcli->hwndContactList, NULL, true);
		UpdateWindow(pcli->hwndContactList);

		PostMessage(hWnd, WM_MOUSEMOVE, 0, (LPARAM)MAKELONG(2, 2)); // reset core's IDLE
		g_bWindowHidden = false;

		db_set_b(NULL, MOD_NAME, "OldSetting", 0);
	}
	return 0;
	}
	return(DefWindowProc(hWnd, uMsg, wParam, lParam));
}

static int MsgWinOpening(WPARAM, LPARAM) // hiding new message windows
{
	if (g_bWindowHidden)
		EnumWindows(EnumWindowsProc, 0);
	return 0;
}

VOID CALLBACK WinEventProc(HWINEVENTHOOK, DWORD event, HWND hwnd, LONG idObject, LONG, DWORD, DWORD)
{
	if (g_bWindowHidden && idObject == OBJID_WINDOW && (event == EVENT_OBJECT_CREATE || event == EVENT_OBJECT_SHOW) && (IsWindowVisible(hwnd)))
	{
		if (hwnd == pcli->hwndContactList)
			ShowWindow(hwnd, SW_HIDE);
		else
			EnumWindows(EnumWindowsProc, 0);
	}
}

INT_PTR BossKeyHideMiranda(WPARAM, LPARAM) // for service :)
{
	PostMessage(g_hListenWindow, WM_USER + ((g_bWindowHidden) ? (52) : (40)), 0, 0);
	return 0;
}

static TCHAR *HokeyVkToName(WORD vkKey)
{
	static TCHAR buf[32] = { 0 };
	DWORD code = MapVirtualKey(vkKey, 0) << 16;

	switch (vkKey)
	{
	case 0:
	case VK_CONTROL:
	case VK_SHIFT:
	case VK_MENU:
	case VK_LWIN:
	case VK_RWIN:
	case VK_PAUSE:
	case VK_CANCEL:
	case VK_CAPITAL:
		return _T("");

	case VK_DIVIDE:
	case VK_INSERT:
	case VK_HOME:
	case VK_PRIOR:
	case VK_DELETE:
	case VK_END:
	case VK_NEXT:
	case VK_LEFT:
	case VK_RIGHT:
	case VK_UP:
	case VK_DOWN:
	case VK_NUMLOCK:
		code |= (1UL << 24);
	}

	GetKeyNameText(code, buf, _countof(buf));
	return buf;
}

static TCHAR *GetBossKeyText(void)
{
	WORD wHotKey = db_get_w(NULL, "SkinHotKeys", "Hide/Show Miranda", HOTKEYCODE(HOTKEYF_CONTROL, VK_F12));

	BYTE key = LOBYTE(wHotKey);
	BYTE shift = HIBYTE(wHotKey);
	static TCHAR buf[128] = { 0 };

	mir_sntprintf(buf, _T("%s%s%s%s%s"),
		(shift & HOTKEYF_CONTROL) ? _T("Ctrl + ") : _T(""),
		(shift & HOTKEYF_SHIFT) ? _T("Shift + ") : _T(""),
		(shift & HOTKEYF_ALT) ? _T("Alt + ") : _T(""),
		(shift & HOTKEYF_EXT) ? _T("Win + ") : _T(""),
		HokeyVkToName(key));

	return buf;
}

static IconItem iconList[] =
{
	{ LPGEN("Hide Miranda NG"), "hidemim", IDI_DLGPASSWD }
};

static int GenMenuInit(WPARAM, LPARAM) // Modify menu item text before to show the main menu
{
	if (g_hMenuItem) {
		TCHAR buf[128];
		mir_sntprintf(buf, _T("%s [%s]"), TranslateT("Hide"), GetBossKeyText());
		Menu_ModifyItem(g_hMenuItem, buf);
	}
	return 0;
}

void BossKeyMenuItemInit(void) // Add menu item
{
	CMenuItem mi;
	mi.flags = CMIF_TCHAR;
	mi.position = 2000100000;
	mi.hIcolibItem = IcoLib_GetIcon("hidemim");
	mi.name.t = LPGENT("Hide");
	mi.pszService = MS_BOSSKEY_HIDE;
	g_hMenuItem = Menu_AddMainMenuItem(&mi);

	HookEvent(ME_CLIST_PREBUILDMAINMENU, GenMenuInit);
}

void BossKeyMenuItemUnInit(void) // Remove menu item
{
	Menu_RemoveItem(g_hMenuItem);
	g_hMenuItem = 0;
}

void RegisterCoreHotKeys(void)
{
	HOTKEYDESC hotkey = { 0 };
	hotkey.cbSize = sizeof(HOTKEYDESC);
	hotkey.pszName = "Hide/Show Miranda";
	hotkey.pszDescription = LPGEN("Hide/Show Miranda");
	hotkey.pszSection = "BossKey";
	hotkey.pszService = MS_BOSSKEY_HIDE;
	hotkey.DefHotKey = HOTKEYCODE(HOTKEYF_CONTROL, VK_F12);

	Hotkey_Register(&hotkey);
}

static int TopToolbarInit(WPARAM, LPARAM)
{
	TTBButton ttb = { 0 };
	ttb.pszService = MS_BOSSKEY_HIDE;
	ttb.pszTooltipUp = ttb.name = LPGEN("Hide Miranda NG");
	ttb.dwFlags = TTBBF_VISIBLE | TTBBF_SHOWTOOLTIP;
	ttb.hIconHandleUp = iconList[0].hIcolib;
	TopToolbar_AddButton(&ttb);
	return 0;
}

// Tabsrmm toolbar support
static int TabsrmmButtonPressed(WPARAM, LPARAM lParam)
{
	CustomButtonClickData *cbcd = (CustomButtonClickData *)lParam;

	if (!mir_strcmp(cbcd->pszModule, MOD_NAME))
		BossKeyHideMiranda(0, 0);

	return 0;
}

static int TabsrmmButtonsInit(WPARAM, LPARAM)
{
	BBButton bbd = { 0 };

	bbd.cbSize = sizeof(BBButton);
	bbd.pszModuleName = MOD_NAME;
	bbd.dwDefPos = 5000;
	bbd.ptszTooltip = LPGENT("Hide Miranda NG");
	bbd.bbbFlags = BBBF_ISRSIDEBUTTON | BBBF_CANBEHIDDEN;
	bbd.hIcon = iconList[0].hIcolib;
	CallService(MS_BB_ADDBUTTON, 0, (LPARAM)&bbd);

	return 0;
}

static TCHAR *VariablesBossKey(ARGUMENTSINFO *ai) {
	if (ai->cbSize < sizeof(ARGUMENTSINFO))	return NULL;
	if (ai->argc != 1) return NULL;

	ai->flags |= AIF_DONTPARSE;

	return GetBossKeyText();
}

static int EnumProtos(WPARAM, LPARAM)
{
	Proto_EnumAccounts(&protoCount, &proto);

	delete[] oldStatus;
	delete[] oldStatusMsg;

	oldStatus = new unsigned[protoCount];
	oldStatusMsg = new TCHAR*[protoCount];
	for (int i = 0; i < protoCount; i++)
	{
		oldStatus[i] = 0;
		oldStatusMsg[i] = 0;
	}
	return 0;
}

int MirandaLoaded(WPARAM, LPARAM)
{
	g_wMask = db_get_w(NULL, MOD_NAME, "optsmask", DEFAULTSETTING);

	RegisterCoreHotKeys();

	g_hWinHook = SetWinEventHook(EVENT_OBJECT_CREATE, EVENT_OBJECT_SHOW, NULL, WinEventProc, GetCurrentProcessId(), 0, 0);

	HookEvent(ME_TTB_MODULELOADED, TopToolbarInit);
	HookEvent(ME_OPT_INITIALISE, OptsDlgInit);
	HookEvent(ME_MSG_WINDOWEVENT, MsgWinOpening);
	HookEvent(ME_PROTO_ACCLISTCHANGED, EnumProtos);
	HookEvent(ME_MSG_TOOLBARLOADED, TabsrmmButtonsInit);
	HookEvent(ME_MSG_BUTTONPRESSED, TabsrmmButtonPressed);

	GetWindowThreadProcessId(pcli->hwndContactList, &g_dwMirandaPID);

	WNDCLASS winclass = { 0 };
	winclass.lpfnWndProc = ListenWndProc;
	winclass.hInstance = g_hInstance;
	winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	winclass.lpszClassName = BOSSKEY_LISTEN_INFO;

	if (RegisterClass(&winclass))
	{
		g_hListenWindow = CreateWindow(BOSSKEY_LISTEN_INFO, BOSSKEY_LISTEN_INFO, WS_POPUP, 0, 0, 5, 5, pcli->hwndContactList, NULL, g_hInstance, NULL);
		WTSRegisterSessionNotification(g_hListenWindow, 0);
	}

	if (IsWinVerVistaPlus())
	{
		hDwmApi = LoadLibrary(_T("dwmapi.dll"));
		if (hDwmApi)
			dwmIsCompositionEnabled = (PFNDwmIsCompositionEnabled)GetProcAddress(hDwmApi, "DwmIsCompositionEnabled");
	}
	if (g_wMaskAdv & OPT_MENUITEM)
		BossKeyMenuItemInit();

	// Register token for variables plugin
	if (ServiceExists(MS_VARS_REGISTERTOKEN))
	{
		TOKENREGISTER tr = { 0 };
		tr.cbSize = sizeof(TOKENREGISTER);
		tr.memType = TR_MEM_OWNER;
		tr.flags = TRF_FIELD | TRF_TCHAR | TRF_PARSEFUNC;

		tr.tszTokenString = _T("bosskeyname");
		tr.parseFunctionT = VariablesBossKey;
		tr.szHelpText = LPGEN("BossKey") "\t" LPGEN("get the BossKey name");
		CallService(MS_VARS_REGISTERTOKEN, 0, (LPARAM)&tr);
	}

	EnumProtos(0, 0);
	InitIdleTimer();

	if (g_bOldSetting && !(g_wMaskAdv & OPT_RESTORE)) // Restore settings if Miranda was crushed or killed in hidden mode and "Restore hiding on startup after failure" option is disabled
		RestoreOldSettings();

	if ((g_wMaskAdv & OPT_HIDEONSTART) || (g_wMaskAdv & OPT_RESTORE && g_bOldSetting))
		BossKeyHideMiranda(0, 0);

	return 0;
}

extern "C" int __declspec(dllexport) Load(void)
{
	mir_getLP(&pluginInfo);
	mir_getCLI();

	g_wMaskAdv = db_get_w(NULL, MOD_NAME, "optsmaskadv", 0);
	g_bOldSetting = db_get_b(NULL, MOD_NAME, "OldSetting", 0);

	if ((g_bOldSetting & OLD_POPUP) && !(g_wMaskAdv & OPT_RESTORE)) // Restore popup settings if Miranda was crushed or killed in hidden mode and "Restore hiding on startup after failure" option is disabled
	{
		if (db_get_b(NULL, "Popup", "ModuleIsEnabled", 1) == 0)
			db_set_b(NULL, "Popup", "ModuleIsEnabled", 1);
	}
	if (g_wMaskAdv & OPT_HIDEONSTART && db_get_b(NULL, "Popup", "ModuleIsEnabled", 0)) // hack for disabling popup on startup if "Hide Miranda on startup" is enabled
	{
		g_bOldSetting |= OLD_POPUP;
		db_set_b(NULL, "Popup", "ModuleIsEnabled", 0);
	}

	Icon_Register(g_hInstance, "BossKey", iconList, _countof(iconList));

	g_hHideService = CreateServiceFunction(MS_BOSSKEY_HIDE, BossKeyHideMiranda); // Create service

	HookEvent(ME_SYSTEM_MODULESLOADED, MirandaLoaded);
	return 0;
}

extern "C" int __declspec(dllexport) Unload(void)
{
	UninitIdleTimer();

	if (g_hWinHook != 0)
		UnhookWinEvent(g_hWinHook);

	DestroyServiceFunction(g_hHideService);

	if (g_hListenWindow)
	{
		WTSUnRegisterSessionNotification(g_hListenWindow);
		DestroyWindow(g_hListenWindow);
	}

	if (hDwmApi)
		FreeLibrary(hDwmApi);

	// free all sessions
	HWND_ITEM *pTemp = g_pMirWnds;
	while (pTemp != NULL)
	{
		HWND_ITEM *pNext = pTemp->next;
		delete pTemp;
		pTemp = pNext;
	}

	delete[] oldStatus;
	delete[] oldStatusMsg;

	return 0;
}