summaryrefslogtreecommitdiff
path: root/plugins/Clist_nicer/SRC/init.cpp
blob: af06a402ecdf5ed300c7b15e986706f8bab1feba (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
/*
 * astyle --force-indent=tab=4 --brackets=linux --indent-switches
 *		  --pad=oper --one-line=keep-blocks  --unpad=paren
 *
 * Miranda IM: the free IM client for Microsoft* Windows*
 *
 * Copyright 2000-2010 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 clist_nicer plugin for Miranda.
 *
 * (C) 2005-2010 by silvercircle _at_ gmail _dot_ com and contributors
 *
 * $Id: init.cpp 13596 2011-04-15 19:07:23Z george.hazan $
 *
 */

#include <commonheaders.h>
#include "../cluiframes/cluiframes.h"

HINSTANCE g_hInst = 0;
CLIST_INTERFACE* pcli = NULL;
int hLangpack;

#define DEFAULT_TB_VISIBILITY (1 | 2 | 4 | 8 | 16 | 32 | 64 | 8192)
TCHAR *szNoevents = _T("No events...");
//extern HICON im_clienthIcons[NR_CLIENTS];
extern HICON overlayicons[10];

extern int Docking_ProcessWindowMessage(WPARAM wParam, LPARAM lParam);
extern int SetHideOffline(WPARAM wParam, LPARAM lParam);

extern DWORD g_gdiplusToken;
extern HIMAGELIST himlExtraImages;

TIME_API tmi;

HMENU  BuildGroupPopupMenu( struct ClcGroup* group );
struct ClcContact* CreateClcContact( void );
struct CListEvent* fnCreateEvent( void );
void   ReloadThemedOptions();
void   TrayIconUpdateBase(const char *szChangedProto);
void   RegisterCLUIFrameClasses();
void   LoadButtonModule();

void GetDefaultFontSetting(int i, LOGFONT *lf, COLORREF *colour);
int  GetWindowVisibleState(HWND hWnd, int iStepX, int iStepY);
int  ShowHide(WPARAM wParam, LPARAM lParam);
int  ClcShutdown(WPARAM wParam, LPARAM lParam);

void ( *saveLoadClcOptions )(HWND hwnd,struct ClcData *dat);
void LoadClcOptions(HWND hwnd,struct ClcData *dat);

int ( *saveAddContactToGroup )(struct ClcData *dat, struct ClcGroup *group, HANDLE hContact);
int AddContactToGroup(struct ClcData *dat, struct ClcGroup *group, HANDLE hContact);

struct ClcGroup* ( *saveRemoveItemFromGroup )(HWND hwnd, struct ClcGroup *group, struct ClcContact *contact, int updateTotalCount);
struct ClcGroup* RemoveItemFromGroup(HWND hwnd, struct ClcGroup *group, struct ClcContact *contact, int updateTotalCount);

struct CListEvent* ( *saveAddEvent )(CLISTEVENT *cle);
struct CListEvent* AddEvent(CLISTEVENT *cle);

int ( *saveAddInfoItemToGroup )(struct ClcGroup *group, int flags, const TCHAR *pszText);
int AddInfoItemToGroup(struct ClcGroup *group, int flags, const TCHAR *pszText);

struct ClcGroup* ( *saveAddGroup )(HWND hwnd, struct ClcData *dat, const TCHAR *szName, DWORD flags, int groupId, int calcTotalMembers);
struct ClcGroup* AddGroup(HWND hwnd, struct ClcData *dat, const TCHAR *szName, DWORD flags, int groupId, int calcTotalMembers);

LRESULT ( CALLBACK *saveContactListWndProc )(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK ContactListWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

LRESULT ( CALLBACK *saveContactListControlWndProc )(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK ContactListControlWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);

int ( *saveIconFromStatusMode )( const char *szProto, int status, HANDLE hContact );

LRESULT ( *saveProcessExternalMessages )(HWND hwnd, struct ClcData *dat, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT ProcessExternalMessages(HWND hwnd, struct ClcData *dat, UINT msg, WPARAM wParam, LPARAM lParam);

int ( *saveRemoveEvent )(HANDLE hContact, HANDLE hDbEvent);
int RemoveEvent(HANDLE hContact, HANDLE hDbEvent);

INT_PTR ( *saveTrayIconProcessMessage )(WPARAM wParam, LPARAM lParam);
INT_PTR TrayIconProcessMessage(WPARAM wParam, LPARAM lParam);

void ( *saveRecalcScrollBar )(HWND hwnd, struct ClcData *dat);
void RecalcScrollBar(HWND hwnd, struct ClcData *dat);

PLUGININFOEX pluginInfo =
{
	sizeof(PLUGININFOEX),
	"CList Nicer+", 
	PLUGIN_MAKE_VERSION(0, 9, 2, 1),
	"Display contacts, event notifications, protocol status",
	"Pixel, egoDust, cyreve, Nightwish",
	"", 
	"Copyright 2000-2010 Miranda-IM project", 
	"http://nightly.miranda.im/",
	UNICODE_AWARE,
	// {8F79B4EE-EB48-4a03-873E-27BE6B7E9A25}
	{0x8f79b4ee, 0xeb48, 0x4a03, { 0x87, 0x3e, 0x27, 0xbe, 0x6b, 0x7e, 0x9a, 0x25 }}
};


void _DebugTraceW(const wchar_t *fmt, ...)
{
#ifdef _DEBUG
    wchar_t debug[2048];
    int     ibsize = 2047;
    va_list va;
    va_start(va, fmt);

	lstrcpyW(debug, L"CLN: ");

    _vsnwprintf(&debug[5], ibsize - 10, fmt, va);
    OutputDebugStringW(debug);
#endif
}


void _DebugTraceA(const char *fmt, ...)
{
    char    debug[2048];
    int     ibsize = 2047;
    va_list va;
    va_start(va, fmt);

	lstrcpyA(debug, "CLN: ");
	_vsnprintf(&debug[5], ibsize - 10, fmt, va);
#ifdef _DEBUG
    OutputDebugStringA(debug);
#else
    {
        char szLogFileName[MAX_PATH], szDataPath[MAX_PATH];
        FILE *f;

        CallService(MS_DB_GETPROFILEPATH, MAX_PATH, (LPARAM)szDataPath);
        mir_snprintf(szLogFileName, MAX_PATH, "%s\\%s", szDataPath, "clist_nicer.log");
        f = fopen(szLogFileName, "a+");
        if(f) {
            fputs(debug, f);
            fputs("\n", f);
            fclose(f);
        }
    }
#endif
}

BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD dwReason, LPVOID reserved)
{
	g_hInst = hInstDLL;
	DisableThreadLibraryCalls(g_hInst);
	return TRUE;
}

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

extern "C" __declspec(dllexport) const MUUID MirandaInterfaces[] = {MIID_CLIST, MIID_LAST};

int  LoadContactListModule(void);
int  LoadCLCModule(void);
void LoadCLUIModule( void );
void OnCreateClc( void );

static int systemModulesLoaded(WPARAM wParam, LPARAM lParam)
{
	GetSystemTime(&cfg::dat.st);
	SystemTimeToFileTime(&cfg::dat.st, &cfg::dat.ft);
	cfg::dat.bMetaAvail = ServiceExists(MS_MC_GETDEFAULTCONTACT) ? TRUE : FALSE;
	if(cfg::dat.bMetaAvail)
		mir_snprintf(cfg::dat.szMetaName, 256, "%s", (char *)CallService(MS_MC_GETPROTOCOLNAME, 0, 0));
	else
		strncpy(cfg::dat.szMetaName, "MetaContacts", 255);

	if(ServiceExists(MS_MC_DISABLEHIDDENGROUP))
		CallService(MS_MC_DISABLEHIDDENGROUP, 1, 0);
	cfg::dat.bMetaEnabled = cfg::getByte(cfg::dat.szMetaName, "Enabled", 1);

	cfg::dat.bAvatarServiceAvail = ServiceExists(MS_AV_GETAVATARBITMAP) ? TRUE : FALSE;
	if(cfg::dat.bAvatarServiceAvail)
		HookEvent(ME_AV_AVATARCHANGED, AvatarChanged);
	cfg::dat.tabSRMM_Avail = ServiceExists("SRMsg_MOD/GetWindowFlags") ? TRUE : FALSE;

	ZeroMemory((void *)overlayicons, sizeof(HICON) * 10);

	CLN_LoadAllIcons(1);
	return 0;
}

static int fnIconFromStatusMode( const char* szProto, int status, HANDLE hContact )
{	return IconFromStatusMode( szProto, status, hContact, NULL );
}

extern "C" int __declspec(dllexport) CListInitialise()
{
	int rc = 0;
	DBVARIANT dbv;
	int       i;
	char	    szProfilePath[MAX_PATH];

	mir_getTMI(&tmi);
	mir_getLP( &pluginInfo );

	API::onInit();
	RegisterCLUIFrameClasses();

	ZeroMemory((void*) &cfg::dat, sizeof(cfg::dat));

	int iCount = CallService(MS_DB_CONTACT_GETCOUNT, 0, 0);

	iCount += 20;
	if ( iCount < 300 )
		iCount = 300;

	cfg::eCache = reinterpret_cast<TExtraCache *>(malloc(sizeof(TExtraCache) * iCount));
	ZeroMemory(cfg::eCache, sizeof(struct TExtraCache) * iCount);
	cfg::nextCacheEntry = 0;
	cfg::maxCacheEntry = iCount;
	cfg::init();

	cfg::dat.hMenuNotify =             CreatePopupMenu();
	cfg::dat.wNextMenuID =             1;
	cfg::dat.sortTimer =               cfg::getDword("CLC", "SortTimer", 150);
	cfg::dat.szNoEvents =              TranslateTS(szNoevents);
	cfg::dat.avatarBorder =            (COLORREF)cfg::getDword("CLC", "avatarborder", 0);
	cfg::dat.avatarRadius =            (COLORREF)cfg::getDword("CLC", "avatarradius", 4);
	cfg::dat.hBrushAvatarBorder =      CreateSolidBrush(cfg::dat.avatarBorder);
	cfg::dat.avatarSize =              cfg::getWord("CList", "AvatarSize", 24);
	cfg::dat.dualRowMode =             cfg::getByte("CLC", "DualRowMode", 0);
	cfg::dat.avatarPadding =           cfg::getByte("CList", "AvatarPadding", 0);
	cfg::dat.isTransparent =           cfg::getByte("CList", "Transparent", 0);
	cfg::dat.alpha =                   cfg::getByte("CList", "Alpha", SETTING_ALPHA_DEFAULT);
	cfg::dat.autoalpha =               cfg::getByte("CList", "AutoAlpha", SETTING_ALPHA_DEFAULT);
	cfg::dat.fadeinout =               cfg::getByte("CLUI", "FadeInOut", 0);
	cfg::dat.autosize =                cfg::getByte("CLUI", "AutoSize", 0);
	cfg::dat.dwExtraImageMask =        cfg::getDword("CLUI", "ximgmask", 0);
	cfg::dat.bNoOfflineAvatars =       cfg::getByte("CList", "NoOfflineAV", 1);
	cfg::dat.bFullTransparent =        cfg::getByte("CLUI", "fulltransparent", 0);
	cfg::dat.bDblClkAvatars =          cfg::getByte("CLC", "dblclkav", 0);
	cfg::dat.bEqualSections =          cfg::getByte("CLUI", "EqualSections", 0);
	cfg::dat.bCenterStatusIcons =      cfg::getByte("CLC", "si_centered", 1);
	cfg::dat.boldHideOffline =         -1;
	cfg::dat.bSecIMAvail =             ServiceExists("SecureIM/IsContactSecured") ? 1 : 0;
	cfg::dat.bNoTrayTips =             cfg::getByte("CList", "NoTrayTips", 0);
	cfg::dat.bShowLocalTime =          cfg::getByte("CLC", "ShowLocalTime", 1);
	cfg::dat.bShowLocalTimeSelective = cfg::getByte("CLC", "SelectiveLocalTime", 1);
	cfg::dat.bDontSeparateOffline =    cfg::getByte("CList", "DontSeparateOffline", 0);
	cfg::dat.bShowXStatusOnSbar =      cfg::getByte("CLUI", "xstatus_sbar", 0);
	cfg::dat.bLayeredHack =            cfg::getByte("CLUI", "layeredhack", 1);
	cfg::dat.bFirstRun =               cfg::getByte("CLUI", "firstrun", 1);
	cfg::dat.langPackCP =              CallService(MS_LANGPACK_GETCODEPAGE, 0, 0);
	cfg::dat.realTimeSaving =          cfg::getByte("CLUI", "save_pos_always", 0);

	DWORD sortOrder = cfg::getDword("CList", "SortOrder", SORTBY_NAME);
	cfg::dat.sortOrder[0] = LOBYTE(LOWORD(sortOrder));
	cfg::dat.sortOrder[1] = HIBYTE(LOWORD(sortOrder));
	cfg::dat.sortOrder[2] = LOBYTE(HIWORD(sortOrder));

	if(cfg::dat.bFirstRun)
		cfg::writeByte("CLUI", "firstrun", 0);

	if (!cfg::getString(NULL, "CLUI", "exIconOrder", &dbv)) {
		if(lstrlenA(dbv.pszVal) < EXICON_COUNT) {
			for (i = 1; i <= EXICON_COUNT; i++)
				cfg::dat.exIconOrder[i - 1] = i;
		} else {
			for (i = 0; i < EXICON_COUNT; i++)
				if(dbv.pszVal[i] < EXICON_COUNT+1 && dbv.pszVal[i] >0)
					cfg::dat.exIconOrder[i] = dbv.pszVal[i];
				else
					cfg::dat.exIconOrder[i] = i+1;
		}
		DBFreeVariant(&dbv);
	} else {
		for (i = 1; i <= EXICON_COUNT; i++)
			cfg::dat.exIconOrder[i - 1] = i;
	}
	ReloadThemedOptions();
	FLT_ReadOptions();
	Reload3dBevelColors();
	himlExtraImages = ImageList_Create(16, 16, ILC_MASK | (IsWinVerXPPlus() ? ILC_COLOR32 : ILC_COLOR16), 30, 2);
	ImageList_SetIconSize(himlExtraImages, cfg::dat.exIconScale, cfg::dat.exIconScale);

	cfg::dat.dwFlags = cfg::getDword("CLUI", "Frameflags", CLUI_FRAME_STATUSICONS | CLUI_FRAME_SHOWBOTTOMBUTTONS |
	                                                       CLUI_FRAME_BUTTONSFLAT | CLUI_FRAME_CLISTSUNKEN);
	cfg::dat.dwFlags |= (cfg::getByte("CLUI", "ShowSBar", 1) ? CLUI_FRAME_SBARSHOW : 0);
	cfg::dat.soundsOff = cfg::getByte("CLUI", "NoSounds", 0);

	CallService(MS_DB_GETPROFILEPATH, MAX_PATH, (LPARAM)szProfilePath);

	MultiByteToWideChar(CP_ACP, 0, szProfilePath, MAX_PATH, cfg::dat.tszProfilePath, MAX_PATH);
	cfg::dat.tszProfilePath[MAX_PATH - 1] = 0;

	_tcslwr(cfg::dat.tszProfilePath);

	// get the clist interface
	pcli = ( CLIST_INTERFACE* )CallService(MS_CLIST_RETRIEVE_INTERFACE, 0, (LPARAM)g_hInst);
	if ( (INT_PTR)pcli == CALLSERVICE_NOTFOUND ) {
LBL_Error:
		MessageBoxA( NULL, "This plugin requires Miranda IM 0.8.0.9 or later", "Fatal error", MB_OK );
		return 1;
	}
	if ( pcli->version < 6 ) // don't join it with the previous if ()
		goto LBL_Error;

	pcli->pfnBuildGroupPopupMenu = BuildGroupPopupMenu;
	pcli->pfnCluiProtocolStatusChanged = CluiProtocolStatusChanged;
	pcli->pfnCompareContacts = CompareContacts;
	pcli->pfnCreateClcContact = CreateClcContact;
	pcli->pfnCreateEvent = fnCreateEvent;
	pcli->pfnDocking_ProcessWindowMessage = Docking_ProcessWindowMessage;
	pcli->pfnGetDefaultFontSetting = GetDefaultFontSetting;
	pcli->pfnGetRowBottomY = RowHeight::getItemBottomY;
	pcli->pfnGetRowHeight = RowHeight::getHeight;
	pcli->pfnGetRowTopY = RowHeight::getItemTopY;
	pcli->pfnGetRowTotalHeight = RowHeight::getTotalHeight;
	pcli->pfnGetWindowVisibleState = GetWindowVisibleState;
	pcli->pfnHitTest = HitTest;
	pcli->pfnLoadContactTree = LoadContactTree;
	pcli->pfnOnCreateClc = OnCreateClc;
	pcli->pfnPaintClc = PaintClc;
	pcli->pfnRebuildEntireList = RebuildEntireList;
	pcli->pfnRowHitTest = RowHeight::hitTest;
	pcli->pfnScrollTo = ScrollTo;
	pcli->pfnTrayIconUpdateBase = TrayIconUpdateBase;
	pcli->pfnSetHideOffline = SetHideOffline;
	pcli->pfnShowHide = ShowHide;

	saveAddContactToGroup = pcli->pfnAddContactToGroup; pcli->pfnAddContactToGroup = AddContactToGroup;
	saveRemoveItemFromGroup = pcli->pfnRemoveItemFromGroup; pcli->pfnRemoveItemFromGroup = RemoveItemFromGroup;

	saveAddEvent = pcli->pfnAddEvent; pcli->pfnAddEvent = AddEvent;
	saveRemoveEvent = pcli->pfnRemoveEvent; pcli->pfnRemoveEvent = RemoveEvent;

	saveAddGroup = pcli->pfnAddGroup; pcli->pfnAddGroup = AddGroup;
	saveAddInfoItemToGroup = pcli->pfnAddInfoItemToGroup; pcli->pfnAddInfoItemToGroup = AddInfoItemToGroup;
	saveContactListControlWndProc = pcli->pfnContactListControlWndProc; pcli->pfnContactListControlWndProc = ContactListControlWndProc;
	saveContactListWndProc = pcli->pfnContactListWndProc; pcli->pfnContactListWndProc = ContactListWndProc;
	saveIconFromStatusMode = pcli->pfnIconFromStatusMode; pcli->pfnIconFromStatusMode = fnIconFromStatusMode;
	saveLoadClcOptions = pcli->pfnLoadClcOptions; pcli->pfnLoadClcOptions = LoadClcOptions;
	saveProcessExternalMessages = pcli->pfnProcessExternalMessages; pcli->pfnProcessExternalMessages = ProcessExternalMessages;
	saveRecalcScrollBar = pcli->pfnRecalcScrollBar; pcli->pfnRecalcScrollBar = RecalcScrollBar;
	saveTrayIconProcessMessage = pcli->pfnTrayIconProcessMessage; pcli->pfnTrayIconProcessMessage = TrayIconProcessMessage;

	rc = LoadContactListModule();
	if (rc == 0)
		rc = LoadCLCModule();
	LoadCLUIModule();
	LoadButtonModule();
	HookEvent(ME_SYSTEM_MODULESLOADED, systemModulesLoaded);
	return rc;
}

// a plugin loader aware of CList exports will never call this.
extern "C" int __declspec(dllexport) Load(void)
{
	return 1;
}

extern "C" int __declspec(dllexport) Unload(void)
{
	if (IsWindow(pcli->hwndContactList))
		DestroyWindow(pcli->hwndContactList);
	ImageList_Destroy(himlExtraImages);
	ClcShutdown(0, 0);
	UnLoadCLUIFramesModule();
	return 0;
}