summaryrefslogtreecommitdiff
path: root/plugins/TabSRMM/src/globals.cpp
blob: 64b280b13e6f99621fac243358ce50974d34e9a9 (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
/////////////////////////////////////////////////////////////////////////////////////////
// Miranda NG: the free IM client for Microsoft* Windows*
//
// Copyright (ñ) 2012-17 Miranda NG project,
// Copyright (c) 2000-09 Miranda ICQ/IM project,
// all portions of this codebase are copyrighted to the people
// listed in contributors.txt.
//
// 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.
//
// part of tabSRMM messaging plugin for Miranda.
//
// (C) 2005-2010 by silvercircle _at_ gmail _dot_ com and contributors
//
// Plugin configuration variables and functions. Implemented as a class
// though there will always be only a single instance.

#include "stdafx.h"

void Chat_ModulesLoaded();

bool g_bShutdown = false;

CGlobals PluginConfig;

static TContainerSettings _cnt_default = { CNT_FLAGS_DEFAULT, CNT_FLAGSEX_DEFAULT, 255, CInfoPanel::DEGRADE_THRESHOLD, 60, 60, L"%n (%s)", 1, 0 };

wchar_t* CGlobals::m_default_container_name = L"default";

extern HANDLE 	hHookButtonPressedEvt;

EXCEPTION_RECORD CGlobals::m_exRecord = { 0 };
CONTEXT          CGlobals::m_exCtx = { 0 };
LRESULT          CGlobals::m_exLastResult = 0;
char             CGlobals::m_exSzFile[MAX_PATH] = "\0";
wchar_t          CGlobals::m_exReason[256] = L"\0";
int              CGlobals::m_exLine = 0;
bool             CGlobals::m_exAllowContinue = false;

/////////////////////////////////////////////////////////////////////////////////////////
// reload system values. These are read ONCE and are not allowed to change
// without a restart

void CGlobals::reloadSystemStartup()
{
	m_WinVerMajor = WinVerMajor();
	m_WinVerMinor = WinVerMinor();
	m_bIsVista = IsWinVerVistaPlus() != 0;
	m_bIsWin7 = IsWinVer7Plus() != 0;

	::RegisterTabCtrlClass();
	CTip::registerClass();

	dwThreadID = GetCurrentThreadId();

	PluginConfig.g_hMenuContext = LoadMenu(g_hInst, MAKEINTRESOURCE(IDR_TABCONTEXT));
	TranslateMenu(g_hMenuContext);

	SkinAddNewSoundEx("RecvMsgActive", LPGEN("Instant messages"), LPGEN("Incoming (focused window)"));
	SkinAddNewSoundEx("RecvMsgInactive", LPGEN("Instant messages"), LPGEN("Incoming (unfocused window)"));
	SkinAddNewSoundEx("AlertMsg", LPGEN("Instant messages"), LPGEN("Incoming (new session)"));
	SkinAddNewSoundEx("SendMsg", LPGEN("Instant messages"), LPGEN("Outgoing"));
	SkinAddNewSoundEx("SendError", LPGEN("Instant messages"), LPGEN("Message send error"));

	hCurSplitNS = LoadCursor(nullptr, IDC_SIZENS);
	hCurSplitWE = LoadCursor(nullptr, IDC_SIZEWE);

	HDC hScrnDC = GetDC(0);
	m_DPIscaleX = GetDeviceCaps(hScrnDC, LOGPIXELSX) / 96.0;
	m_DPIscaleY = GetDeviceCaps(hScrnDC, LOGPIXELSY) / 96.0;
	ReleaseDC(0, hScrnDC);

	reloadSettings(false);
	reloadAdv();
	hookSystemEvents();
}

/////////////////////////////////////////////////////////////////////////////////////////
// this runs ONCE at startup when the Modules Loaded event is fired
// by the core. all plugins are loaded and ready to use.
//
// any initialation for 3rd party plugins must go here.

void CGlobals::reloadSystemModulesChanged()
{
	// smiley add
	if (ServiceExists(MS_SMILEYADD_REPLACESMILEYS)) {
		PluginConfig.g_SmileyAddAvail = 1;
		HookEvent(ME_SMILEYADD_OPTIONSCHANGED, ::SmileyAddOptionsChanged);
	}

	// ieView
	BOOL bIEView = ServiceExists(MS_IEVIEW_WINDOW);
	if (bIEView) {
		BOOL bOldIEView = M.GetByte("ieview_installed", 0);
		if (bOldIEView != bIEView)
			db_set_b(0, SRMSGMOD_T, "default_ieview", 1);
		db_set_b(0, SRMSGMOD_T, "ieview_installed", 1);
		HookEvent(ME_IEVIEW_OPTIONSCHANGED, ::IEViewOptionsChanged);
	}
	else db_set_b(0, SRMSGMOD_T, "ieview_installed", 0);

	m_hwndClist = pcli->hwndContactList;

	g_bPopupAvail = ServiceExists(MS_POPUP_ADDPOPUPT) != 0;

	CMenuItem mi;
	SET_UID(mi, 0x58d8dc1, 0x1c25, 0x49c0, 0xb8, 0x7c, 0xa3, 0x22, 0x2b, 0x3d, 0xf1, 0xd8);
	mi.position = -2000090000;
	mi.flags = CMIF_DEFAULT;
	mi.hIcolibItem = Skin_GetIconHandle(SKINICON_EVENT_MESSAGE);
	mi.name.a = LPGEN("&Message");
	mi.pszService = MS_MSG_SENDMESSAGE;
	PluginConfig.m_hMenuItem = Menu_AddContactMenuItem(&mi);

	m_useAeroPeek = M.GetByte("useAeroPeek", 1);
}

/////////////////////////////////////////////////////////////////////////////////////////
// reload plugin settings on startup and runtime.Most of these setttings can be
// changed while plugin is running.

void CGlobals::reloadSettings(bool fReloadSkins)
{
	m_ncm.cbSize = sizeof(NONCLIENTMETRICS);
	SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &m_ncm, 0);

	m_bSendOnShiftEnter = M.GetBool("sendonshiftenter", false);
	m_bSendOnEnter = M.GetBool(SRMSGSET_SENDONENTER, SRMSGDEFSET_SENDONENTER);
	m_bSendOnDblEnter = M.GetBool("SendOnDblEnter", false);
	m_bAutoLocaleSupport = M.GetBool("al", false);
	m_bAutoSwitchTabs = M.GetBool("autoswitchtabs", true);
	m_iTabNameLimit = db_get_w(0, SRMSGMOD_T, "cut_at", 15);
	m_bCutContactNameOnTabs = M.GetBool("cuttitle", false);
	m_bStatusOnTabs = M.GetBool("tabstatus", true);
	m_bLogStatusChanges = M.GetBool("logstatuschanges", false);
	m_bUseDividers = M.GetBool("usedividers", false);
	m_bDividersUsePopupConfig = M.GetBool("div_popupconfig", false);
	m_MsgTimeout = M.GetDword(SRMSGMOD, SRMSGSET_MSGTIMEOUT, SRMSGDEFSET_MSGTIMEOUT);

	if (m_MsgTimeout < SRMSGSET_MSGTIMEOUT_MIN)
		m_MsgTimeout = SRMSGSET_MSGTIMEOUT_MIN;

	m_EscapeCloses = M.GetByte("escmode", 0);

	m_bHideOnClose = M.GetBool("hideonclose", false);
	m_bAllowTab = M.GetBool("tabmode", false);

	m_bFlashOnClist = M.GetBool("flashcl", false);
	m_bAlwaysFullToolbarWidth = M.GetBool("alwaysfulltoolbar", true);
	m_LimitStaticAvatarHeight = M.GetDword("avatarheight", 96);
	m_SendFormat = M.GetByte("sendformat", 0);
	m_TabAppearance = M.GetDword("tabconfig", TCF_FLASHICON | TCF_SINGLEROWTABCONTROL);
	m_panelHeight = (DWORD)M.GetDword("panelheight", CInfoPanel::DEGRADE_THRESHOLD);
	m_MUCpanelHeight = M.GetDword(CHAT_MODULE, "panelheight", CInfoPanel::DEGRADE_THRESHOLD);
	m_bIdleDetect = M.GetBool("dimIconsForIdleContacts", true);
	m_smcxicon = m_smcyicon = 16;
	m_PasteAndSend = M.GetByte("pasteandsend", 1);
	m_LangPackCP = Langpack_GetDefaultCodePage();
	m_visualMessageSizeIndicator = M.GetByte("msgsizebar", 0);
	m_autoSplit = M.GetByte("autosplit", 0);
	m_FlashOnMTN = M.GetByte(SRMSGMOD, SRMSGSET_SHOWTYPINGWINFLASH, SRMSGDEFSET_SHOWTYPINGWINFLASH);
	if (m_MenuBar == 0) {
		m_MenuBar = ::LoadMenu(g_hInst, MAKEINTRESOURCE(IDR_MENUBAR));
		TranslateMenu(m_MenuBar);
	}

	m_ipBackgroundGradient = M.GetDword(FONTMODULE, "ipfieldsbg", 0x62caff);
	if (0 == m_ipBackgroundGradient)
		m_ipBackgroundGradient = 0x62caff;

	m_ipBackgroundGradientHigh = M.GetDword(FONTMODULE, "ipfieldsbgHigh", 0xf0f0f0);
	if (0 == m_ipBackgroundGradientHigh)
		m_ipBackgroundGradientHigh = 0xf0f0f0;

	m_tbBackgroundHigh = M.GetDword(FONTMODULE, "tbBgHigh", 0);
	m_tbBackgroundLow = M.GetDword(FONTMODULE, "tbBgLow", 0);
	m_fillColor = M.GetDword(FONTMODULE, "fillColor", 0);
	if (CSkin::m_BrushFill) {
		::DeleteObject(CSkin::m_BrushFill);
		CSkin::m_BrushFill = 0;
	}
	m_genericTxtColor = M.GetDword(FONTMODULE, "genericTxtClr", GetSysColor(COLOR_BTNTEXT));
	m_cRichBorders = M.GetDword(FONTMODULE, "cRichBorders", 0);

	::memcpy(&globalContainerSettings, &_cnt_default, sizeof(TContainerSettings));
	Utils::ReadContainerSettingsFromDB(0, &globalContainerSettings);
	globalContainerSettings.fPrivate = false;
	if (fReloadSkins)
		Skin->setupAeroSkins();
}

/////////////////////////////////////////////////////////////////////////////////////////
// reload "advanced tweaks" that can be applied w / o a restart

void CGlobals::reloadAdv()
{
	m_bSoundOnTyping = M.GetBool("adv_soundontyping", false);
	m_bDontUseDefaultKbd = M.GetBool("adv_leaveKeyboardAlone", true);

	if (m_bSoundOnTyping && m_TypingSoundAdded == false) {
		SkinAddNewSoundEx("SoundOnTyping", LPGEN("Other"), LPGEN("TabSRMM: typing"));
		m_TypingSoundAdded = true;
	}
	m_bAllowOfflineMultisend = M.GetBool("AllowOfflineMultisend", true);
}

const HMENU CGlobals::getMenuBar()
{
	if (m_MenuBar == 0) {
		m_MenuBar = ::LoadMenu(g_hInst, MAKEINTRESOURCE(IDR_MENUBAR));
		TranslateMenu(m_MenuBar);
	}
	return(m_MenuBar);
}

/////////////////////////////////////////////////////////////////////////////////////////
// hook core events.This runs in LoadModule()
// only core events and services are guaranteed to exist at this time

void CGlobals::hookSystemEvents()
{
	HookEvent(ME_SYSTEM_MODULESLOADED, ModulesLoaded);
	HookEvent(ME_SKIN_ICONSCHANGED, ::IconsChanged);
	HookEvent(ME_PROTO_CONTACTISTYPING, CMimAPI::TypingMessage);
	HookEvent(ME_PROTO_ACK, CMimAPI::ProtoAck);
	HookEvent(ME_SYSTEM_PRESHUTDOWN, PreshutdownSendRecv);
	HookEvent(ME_SYSTEM_OKTOEXIT, OkToExit);

	HookEvent(ME_CLIST_PREBUILDCONTACTMENU, CMimAPI::PrebuildContactMenu);

	HookEvent(ME_SKIN2_ICONSCHANGED, ::IcoLibIconsChanged);
	HookEvent(ME_AV_MYAVATARCHANGED, ::MyAvatarChanged);
}

int CGlobals::TopToolbarLoaded(WPARAM, LPARAM)
{
	TTBButton ttb = { 0 };
	ttb.dwFlags = TTBBF_SHOWTOOLTIP | TTBBF_VISIBLE;
	ttb.pszService = MS_TABMSG_TRAYSUPPORT;
	ttb.name = "TabSRMM session list";
	ttb.pszTooltipUp = LPGEN("TabSRMM session list");
	ttb.hIconHandleUp = IcoLib_GetIcon("tabSRMM_sb_slist");
	TopToolbar_AddButton(&ttb);

	ttb.name = "TabSRMM Menu";
	ttb.pszTooltipUp = LPGEN("TabSRMM menu");
	ttb.lParamUp = ttb.lParamDown = 1;
	ttb.hIconHandleUp = IcoLib_GetIcon("tabSRMM_container");
	TopToolbar_AddButton(&ttb);

	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////
// second part of the startup initialisation.All plugins are now fully loaded

int CGlobals::ModulesLoaded(WPARAM, LPARAM)
{
	M.configureCustomFolders();

	Skin->Init(true);
	CSkin::initAeroEffect();

	for (int i = 0; i < NR_BUTTONBARICONS; i++)
		PluginConfig.g_buttonBarIcons[i] = 0;
	::LoadIconTheme();
	::CreateImageList(TRUE);

	MENUITEMINFOA mii = { 0 };
	mii.cbSize = sizeof(mii);
	mii.fMask = MIIM_BITMAP;
	mii.hbmpItem = HBMMENU_CALLBACK;
	HMENU submenu = GetSubMenu(PluginConfig.g_hMenuContext, 7);
	for (int k = 0; k <= 8; k++)
		SetMenuItemInfoA(submenu, (UINT_PTR)k, TRUE, &mii);

	PluginConfig.reloadSystemModulesChanged();

	::Chat_ModulesLoaded();
	::BuildContainerMenu();

	::ModPlus_Init();

	if (M.GetByte("avatarmode", -1) == -1)
		db_set_b(0, SRMSGMOD_T, "avatarmode", 2);

	PluginConfig.g_hwndHotkeyHandler = CreateWindowEx(0, L"TSHK", L"", WS_POPUP,
		0, 0, 40, 40, 0, 0, g_hInst, nullptr);

	::CreateTrayMenus(TRUE);
	if (nen_options.bTraySupport)
		::CreateSystrayIcon(TRUE);

	CMenuItem mi;

	SET_UID(mi, 0x9f68b822, 0xff97, 0x477d, 0xb7, 0x6d, 0xa5, 0x59, 0x33, 0x1c, 0x54, 0x40);
	mi.position = -500050005;
	mi.hIcolibItem = PluginConfig.g_iconContainer;
	mi.name.a = LPGEN("&Messaging settings...");
	mi.pszService = MS_TABMSG_SETUSERPREFS;
	PluginConfig.m_UserMenuItem = Menu_AddContactMenuItem(&mi);

	if (sendLater->isAvail()) {
		SET_UID(mi, 0x8f32b04e, 0x314e, 0x42eb, 0x89, 0xc6, 0x56, 0x21, 0xf5, 0x1a, 0x2f, 0x22);
		mi.position = -500050006;
		mi.hIcolibItem = 0;
		mi.name.a = LPGEN("&Send later job list...");
		mi.pszService = MS_TABMSG_SLQMGR;
		PluginConfig.m_UserMenuItem = Menu_AddMainMenuItem(&mi);
	}
	RestoreUnreadMessageAlerts();

	::RegisterFontServiceFonts();
	::CacheLogFonts();
	if (PluginConfig.g_bPopupAvail)
		TN_ModuleInit();

	HookEvent(ME_DB_CONTACT_SETTINGCHANGED, DBSettingChanged);
	HookEvent(ME_DB_CONTACT_DELETED, DBContactDeleted);

	HookEvent(ME_DB_EVENT_ADDED, CMimAPI::DispatchNewEvent);
	HookEvent(ME_DB_EVENT_ADDED, CMimAPI::MessageEventAdded);
	HookEvent(ME_FONT_RELOAD, ::FontServiceFontsChanged);
	HookEvent(ME_TTB_MODULELOADED, TopToolbarLoaded);

	HookEvent(ME_MC_ENABLED, &CContactCache::cacheUpdateMetaChanged);
	HookEvent(ME_MC_DEFAULTTCHANGED, MetaContactEvent);
	HookEvent(ME_MC_SUBCONTACTSCHANGED, MetaContactEvent);
	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////
// watches various important database settings and reacts accordingly
// needed to catch status, nickname and other changes in order to update open message
// sessions.

int CGlobals::DBSettingChanged(WPARAM hContact, LPARAM lParam)
{
	DBCONTACTWRITESETTING *cws = (DBCONTACTWRITESETTING *)lParam;
	const char *setting = cws->szSetting;

	if (hContact == 0) {
		if (!strcmp("Nick", setting))
			M.BroadcastMessage(DM_OWNNICKCHANGED, 0, (LPARAM)cws->szModule);
		return 0;
	}

	CContactCache *c = CContactCache::getContactCache(hContact);
	const char *szProto = c->getProto();
	if (szProto == nullptr)
		return 0;

	if (!c->isValid())
		c->resetMeta(); // restart constructor

	// catch own relevant settings
	if (!strcmp(cws->szModule, SRMSGMOD_T))
		if (!strcmp(setting, "isFavorite") || !strcmp(setting, "isRecent"))
			c->updateFavorite();

	// neither clist nor contact's settings -> skip
	if (strcmp(cws->szModule, "CList") && strcmp(cws->szModule, szProto))
		return 0;

	if (!strcmp(cws->szModule, META_PROTO))
		if (!strcmp(setting, "Nick"))      // filter out this setting to avoid infinite loops while trying to obtain the most online contact
			return 0;

	HWND hwnd = M.FindWindow(hContact);
	bool fChanged = false, fExtendedStatusChange = false;
	if (!strcmp(cws->szSetting, "Status"))
		fChanged = c->updateStatus(cws->value.wVal);

	fChanged |= c->updateNick();

	if (strlen(setting) > 6 && strlen(setting) < 9 && !strncmp(setting, "Status", 6)) {
		fChanged = true;
		c->updateMeta();
		c->updateUIN();
	}
	if (strlen(setting) > 6 && strstr("StatusMsg,XStatusMsg,XStatusName,XStatusId,ListeningTo", setting)) {
		c->updateStatusMsg(setting);
		fExtendedStatusChange = true;
	}
	if (!strcmp(setting, "display_uid")) {
		c->updateUIN();
		if (hwnd)
			PostMessage(hwnd, DM_UPDATEUIN, 0, 0);
	}

	if (hwnd != nullptr) {
		CTabBaseDlg *dat = c->getDat();
		if (!strcmp(setting, "MirVer"))
			PostMessage(hwnd, DM_CLIENTCHANGED, 0, 0);
		if (dat && (fChanged || fExtendedStatusChange))
			dat->UpdateTitle();
		if (fExtendedStatusChange)
			PostMessage(hwnd, DM_UPDATESTATUSMSG, 0, 0);
		if (fChanged) {
			if (dat && c->getStatus() == ID_STATUS_OFFLINE) {			// clear typing notification in the status bar when contact goes offline
				dat->m_nTypeSecs = 0;
				dat->m_bShowTyping = 0;
				dat->m_wszStatusBar[0] = 0;
				PostMessage(dat->GetHwnd(), DM_UPDATELASTMESSAGE, 0, 0);
			}
			PostMessage(PluginConfig.g_hwndHotkeyHandler, DM_LOGSTATUSCHANGE, MAKELONG(c->getStatus(), c->getOldStatus()), (LPARAM)c);
		}
	}

	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////
// event fired when a contact has been deleted. Make sure to close its message session

int CGlobals::DBContactDeleted(WPARAM hContact, LPARAM)
{
	if (hContact) {
		CContactCache *c = CContactCache::getContactCache(hContact);
		c->deletedHandler();
	}
	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////
// Handle events from metacontacts protocol.Basically, just update
// our contact cache and, if a message window exists, tell it to update
// relevant information.

int CGlobals::MetaContactEvent(WPARAM hContact, LPARAM)
{
	if (hContact) {
		CContactCache *c = CContactCache::getContactCache(hContact);
		c->updateMeta();
		CTabBaseDlg *pDlg = c->getDat();
		if (pDlg) {
			pDlg->UpdateTitle();
			::PostMessage(pDlg->GetHwnd(), DM_UPDATEPICLAYOUT, 0, 0);
			InvalidateRect(pDlg->GetHwnd(), 0, TRUE); // force redraw
		}
	}
	return 0;
}

int CGlobals::PreshutdownSendRecv(WPARAM, LPARAM)
{
	g_bShutdown = true;

	::TN_ModuleDeInit();
	::CloseAllContainers();

	for (MCONTACT hContact = db_find_first(); hContact; hContact = db_find_next(hContact))
		db_set_dw(hContact, SRMSGMOD_T, "messagecount", 0);

	::SI_DeinitStatusIcons();

	// the event API
	DestroyHookableEvent(PluginConfig.m_event_MsgWin);
	DestroyHookableEvent(PluginConfig.m_event_MsgPopup);
	DestroyHookableEvent(PluginConfig.m_event_WriteEvent);

	::NEN_WriteOptions(&nen_options);
	::DestroyWindow(PluginConfig.g_hwndHotkeyHandler);

	::UnregisterClass(L"TSStatusBarClass", g_hInst);
	::UnregisterClass(L"SideBarClass", g_hInst);
	::UnregisterClass(L"TSTabCtrlClass", g_hInst);
	::UnregisterClass(L"RichEditTipClass", g_hInst);
	::UnregisterClass(L"TSHK", g_hInst);
	return 0;
}

int CGlobals::OkToExit(WPARAM, LPARAM)
{
	::CreateSystrayIcon(0);
	::CreateTrayMenus(0);

	CWarning::destroyAll();

	CMimAPI::m_shutDown = true;

	PluginConfig.globalContainerSettings.fPrivate = false;
	Utils::WriteContainerSettingsToDB(0, &PluginConfig.globalContainerSettings, nullptr);
	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////
// used on startup to restore flashing tray icon if one or more messages are still "unread"

struct MSavedEvent
{
	MSavedEvent(MCONTACT _hContact, MEVENT _hEvent) :
		hContact(_hContact),
		hEvent(_hEvent)
	{}

	MEVENT   hEvent;
	MCONTACT hContact;
};

void CGlobals::RestoreUnreadMessageAlerts(void)
{
	OBJLIST<MSavedEvent> arEvents(10, NumericKeySortT);

	for (MCONTACT hContact = db_find_first(); hContact; hContact = db_find_next(hContact)) {
		if (db_get_dw(hContact, "SendLater", "count", 0))
			sendLater->addContact(hContact);

		for (MEVENT hDbEvent = db_event_firstUnread(hContact); hDbEvent; hDbEvent = db_event_next(hContact, hDbEvent)) {
			DBEVENTINFO dbei = {};
			db_event_get(hDbEvent, &dbei);
			if (!dbei.markedRead() && dbei.eventType == EVENTTYPE_MESSAGE) {
				if (M.FindWindow(hContact) != nullptr)
					continue;

				arEvents.insert(new MSavedEvent(hContact, hDbEvent));
			}
		}
	}

	wchar_t toolTip[256];

	CLISTEVENT cle = {};
	cle.hIcon = Skin_LoadIcon(SKINICON_EVENT_MESSAGE);
	cle.pszService = "SRMsg/ReadMessage";
	cle.flags = CLEF_UNICODE;
	cle.szTooltip.w = toolTip;

	for (int i = 0; i < arEvents.getCount(); i++) {
		MSavedEvent &e = arEvents[i];
		mir_snwprintf(toolTip, TranslateT("Message from %s"), pcli->pfnGetContactDisplayName(e.hContact, 0));
		cle.hContact = e.hContact;
		cle.hDbEvent = e.hEvent;
		pcli->pfnAddEvent(&cle);
	}
}

void CGlobals::logStatusChange(WPARAM wParam, const CContactCache *c)
{
	if (c == 0)
		return;

	CSrmmWindow *dat = c->getDat();
	if (dat == nullptr || !c->isValid())
		return;

	MCONTACT hContact = c->getContact();
	if (!PluginConfig.m_bLogStatusChanges && !M.GetByte(hContact, "logstatuschanges", 0))
		return;

	// don't log them if WE are logging off
	if (CallProtoService(c->getProto(), PS_GETSTATUS, 0, 0) == ID_STATUS_OFFLINE)
		return;

	WORD wStatus = LOWORD(wParam);
	WORD wOldStatus = HIWORD(wParam);
	if (wStatus == wOldStatus)
		return;

	wchar_t *szOldStatus = pcli->pfnGetStatusModeDescription(wOldStatus, 0);
	wchar_t *szNewStatus = pcli->pfnGetStatusModeDescription(wStatus, 0);
	if (szOldStatus == 0 || szNewStatus == 0)
		return;

	CMStringW text;
	if (wStatus == ID_STATUS_OFFLINE)
		text = TranslateT("signed off.");
	else if (wOldStatus == ID_STATUS_OFFLINE)
		text.Format(TranslateT("signed on and is now %s."), szNewStatus);
	else
		text.Format(TranslateT("changed status from %s to %s."), szOldStatus, szNewStatus);

	T2Utf szMsg(text);
	DBEVENTINFO dbei = {};
	dbei.pBlob = (PBYTE)(char*)szMsg;
	dbei.cbBlob = (int)mir_strlen(szMsg) + 1;
	dbei.flags = DBEF_UTF | DBEF_READ;
	dbei.eventType = EVENTTYPE_STATUSCHANGE;
	dbei.timestamp = time(nullptr);
	dbei.szModule = (char*)c->getProto();
	dat->StreamInEvents(0, 1, 1, &dbei);
}

/////////////////////////////////////////////////////////////////////////////////////////
// on Windows 7, when using new task bar features (grouping mode and per tab
// previews), autoswitching does not work relieably, so it is disabled.
//
// @return: true if configuration dictates autoswitch

bool CGlobals::haveAutoSwitch()
{
	if (m_bIsWin7 && m_useAeroPeek && !CSkin::m_skinEnabled)
		return false;

	return m_bAutoSwitchTabs;
}