summaryrefslogtreecommitdiff
path: root/plugins/AVS/src/main.cpp
blob: f99af3102bd91c08e4ab8bf548314ac691293c98 (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
/*

Miranda NG: the free IM client for Microsoft* Windows*

Copyright (C) 2012-22 Miranda NG team (https://miranda-ng.org)
Copyright (c) 2000-04 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.
*/

#include "stdafx.h"

HICON    g_hIcon = nullptr;
bool     g_shutDown = false;

CMPlugin g_plugin;

wchar_t  g_szDataPath[MAX_PATH];		// user datae path (read at startup only)
BOOL     g_AvatarHistoryAvail = FALSE;
HWND     hwndSetMyAvatar = nullptr;

HANDLE   hMyAvatarsFolder;
HANDLE   hGlobalAvatarFolder;
HANDLE   hLoaderEvent, hLoaderThread, hShutdownEvent;
HANDLE   hEventChanged, hEventContactAvatarChanged, hMyAvatarChanged;

char *g_szMetaName = nullptr;

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

static PLUGININFOEX pluginInfoEx =
{
	sizeof(PLUGININFOEX),
	__PLUGIN_NAME,
	PLUGIN_MAKE_VERSION(__MAJOR_VERSION, __MINOR_VERSION, __RELEASE_NUM, __BUILD_NUM),
	__DESCRIPTION,
	__AUTHOR,
	__COPYRIGHT,
	__AUTHORWEB,
	UNICODE_AWARE,
	// {E00F1643-263C-4599-B84B-053E5C511D29}
	{ 0xe00f1643, 0x263c, 0x4599, { 0xb8, 0x4b, 0x5, 0x3e, 0x5c, 0x51, 0x1d, 0x29 } }
};

CMPlugin::CMPlugin() :
	PLUGIN<CMPlugin>("AVS_Settings", pluginInfoEx)
{}

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

static int ComparePicture(const protoPicCacheEntry *p1, const protoPicCacheEntry *p2)
{
	if (p1->cacheType != p2->cacheType)
		return p1->cacheType - p2->cacheType;

	return mir_strcmp(p1->szProtoname, p2->szProtoname);
}

OBJLIST<protoPicCacheEntry> g_ProtoPictures(10, ComparePicture), g_MyAvatars(10, ComparePicture);

// Stores the id of the dialog

static int ProtocolAck(WPARAM, LPARAM lParam)
{
	ACKDATA *ack = (ACKDATA*)lParam;
	if (ack != nullptr && ack->type == ACKTYPE_AVATAR && !db_mc_isMeta(ack->hContact)) {
		if (ack->result == ACKRESULT_SUCCESS) {
			if (ack->hProcess == nullptr)
				ProcessAvatarInfo(ack->hContact, GAIR_NOAVATAR, nullptr, ack->szModule);
			else
				ProcessAvatarInfo(ack->hContact, GAIR_SUCCESS, (PROTO_AVATAR_INFORMATION*)ack->hProcess, ack->szModule);
		}
		else if (ack->result == ACKRESULT_FAILED) {
			ProcessAvatarInfo(ack->hContact, GAIR_FAILED, (PROTO_AVATAR_INFORMATION*)ack->hProcess, ack->szModule);
		}
		else if (ack->result == ACKRESULT_STATUS) {
			char *szProto = Proto_GetBaseAccountName(ack->hContact);
			if (szProto == nullptr || Proto_NeedDelaysForAvatars(szProto)) {
				// Queue
				db_set_b(ack->hContact, "ContactPhoto", "NeedUpdate", 1);
				QueueAdd(ack->hContact);
			}
			else // Fetch it now
				FetchAvatarFor(ack->hContact, szProto);
		}
	}
	return 0;
}

static int MetaChanged(WPARAM hMeta, LPARAM hSubContact)
{
	if (g_shutDown)
		return 0;

	AVATARCACHEENTRY *ace;

	// Get the node
	CacheNode *node = FindAvatarInCache(hSubContact, true);
	if (node == nullptr || !node->bLoaded) {
		ace = (AVATARCACHEENTRY*)GetProtoDefaultAvatar(hSubContact);
		QueueAdd(hSubContact);
	}
	else ace = node;

	NotifyEventHooks(hEventChanged, hMeta, (LPARAM)ace);
	return 0;
}

static void LoadDefaultInfo()
{
	protoPicCacheEntry *pce = new protoPicCacheEntry(PCE_TYPE_GLOBAL, AVS_DEFAULT);
	if (CreateAvatarInCache(0, pce, AVS_DEFAULT) != 1)
		db_unset(0, PPICT_MODULE, AVS_DEFAULT);

	g_ProtoPictures.insert(pce);
}

static void LoadProtoInfo(PROTOCOLDESCRIPTOR *proto)
{
	if (proto->type != PROTOTYPE_PROTOWITHACCS)
		return;

	char protoName[MAX_PATH];
	mir_snprintf(protoName, "Global avatar for %s accounts", proto->szName);

	protoPicCacheEntry *pce = new protoPicCacheEntry(PCE_TYPE_PROTO, protoName);
	if (CreateAvatarInCache(0, pce, protoName) != 1)
		db_unset(0, PPICT_MODULE, protoName);

	pce->pd = proto;
	g_ProtoPictures.insert(pce);
}

static void LoadAccountInfo(PROTOACCOUNT *acc)
{
	protoPicCacheEntry *pce = new protoPicCacheEntry(PCE_TYPE_ACCOUNT, acc->szModuleName);
	if (CreateAvatarInCache(0, pce, acc->szModuleName) != 1)
		db_unset(0, PPICT_MODULE, acc->szModuleName);

	pce->pa = acc;
	g_ProtoPictures.insert(pce);

	pce = new protoPicCacheEntry(PCE_TYPE_ACCOUNT, acc->szModuleName);
	CreateAvatarInCache(INVALID_CONTACT_ID, pce, acc->szModuleName);
	g_MyAvatars.insert(pce);
}

static int OnAccChanged(WPARAM wParam, LPARAM lParam)
{
	PROTOACCOUNT *pa = (PROTOACCOUNT*)lParam;

	switch (wParam) {
	case PRAC_ADDED:
		LoadAccountInfo(pa);
		break;

	case PRAC_REMOVED:
		// little perversion not to call the object's destructor
		protoPicCacheEntry *tmp = (protoPicCacheEntry *)_alloca(sizeof(protoPicCacheEntry));
		tmp->cacheType = PCE_TYPE_ACCOUNT;
		tmp->szProtoname = pa->szModuleName;

		int idx;
		if ((idx = g_ProtoPictures.getIndex(tmp)) != -1)
			g_ProtoPictures.remove(idx);
		if ((idx = g_MyAvatars.getIndex(tmp)) != -1)
			g_MyAvatars.remove(idx);
		break;
	}

	return 0;
}

static int ContactSettingChanged(WPARAM hContact, LPARAM lParam)
{
	DBCONTACTWRITESETTING *cws = (DBCONTACTWRITESETTING *)lParam;
	if (cws == nullptr || g_shutDown)
		return 0;

	if (hContact == 0)
		if (!strcmp(cws->szSetting, "AvatarFile") || !strcmp(cws->szSetting, "PictObject") || !strcmp(cws->szSetting, "AvatarHash"))
			ReportMyAvatarChanged((WPARAM)cws->szModule, 0);

	return 0;
}

static int ContactDeleted(WPARAM wParam, LPARAM)
{
	DeleteAvatarFromCache(wParam, TRUE);
	return 0;
}

static int ShutdownProc(WPARAM, LPARAM)
{
	g_shutDown = true;
	SetEvent(hLoaderEvent);
	SetEvent(hShutdownEvent);
	return 0;
}

void InternalDrawAvatar(AVATARDRAWREQUEST *r, HBITMAP hbm, LONG bmWidth, LONG bmHeight, uint32_t dwFlags)
{
	int targetWidth = r->rcDraw.right - r->rcDraw.left;
	int targetHeight = r->rcDraw.bottom - r->rcDraw.top;

	HDC hdcAvatar = CreateCompatibleDC(r->hTargetDC);
	HBITMAP hbmMem = (HBITMAP)SelectObject(hdcAvatar, hbm);

	float dScale;
	int newHeight, newWidth;
	if ((r->dwFlags & AVDRQ_DONTRESIZEIFSMALLER) && bmHeight <= targetHeight && bmWidth <= targetWidth) {
		dScale = 0;
		newHeight = bmHeight;
		newWidth = bmWidth;
	}
	else if (bmHeight >= bmWidth) {
		dScale = targetHeight / (float)bmHeight;
		newHeight = targetHeight;
		newWidth = (int)(bmWidth * dScale);
	}
	else {
		dScale = targetWidth / (float)bmWidth;
		newWidth = targetWidth;
		newHeight = (int)(bmHeight * dScale);
	}

	uint32_t topoffset = targetHeight > newHeight ? (targetHeight - newHeight) / 2 : 0;
	uint32_t leftoffset = targetWidth > newWidth ? (targetWidth - newWidth) / 2 : 0;

	// create the region for the avatar border - use the same region for clipping, if needed.
	HRGN oldRgn = CreateRectRgn(0, 0, 1, 1);

	if (GetClipRgn(r->hTargetDC, oldRgn) != 1) {
		DeleteObject(oldRgn);
		oldRgn = nullptr;
	}

	HRGN rgn;
	if (r->dwFlags & AVDRQ_ROUNDEDCORNER)
		rgn = CreateRoundRectRgn(r->rcDraw.left + leftoffset, r->rcDraw.top + topoffset, r->rcDraw.left + leftoffset + newWidth + 1, r->rcDraw.top + topoffset + newHeight + 1, 2 * r->radius, 2 * r->radius);
	else
		rgn = CreateRectRgn(r->rcDraw.left + leftoffset, r->rcDraw.top + topoffset, r->rcDraw.left + leftoffset + newWidth, r->rcDraw.top + topoffset + newHeight);

	ExtSelectClipRgn(r->hTargetDC, rgn, RGN_AND);

	BLENDFUNCTION bf = { 0 };
	bf.SourceConstantAlpha = r->alpha > 0 ? r->alpha : 255;
	bf.AlphaFormat = dwFlags & AVS_PREMULTIPLIED ? AC_SRC_ALPHA : 0;

	if (!(r->dwFlags & AVDRQ_AERO))
		SetStretchBltMode(r->hTargetDC, HALFTONE);
	//else
	//	FillRect(r->hTargetDC, &r->rcDraw, (HBRUSH)GetStockObject(BLACK_BRUSH));

	if (r->dwFlags & AVDRQ_FORCEFASTALPHA && !(r->dwFlags & AVDRQ_AERO))
		GdiAlphaBlend(r->hTargetDC, r->rcDraw.left + leftoffset, r->rcDraw.top + topoffset, newWidth, newHeight, hdcAvatar, 0, 0, bmWidth, bmHeight, bf);
	else {
		if (bf.SourceConstantAlpha == 255 && bf.AlphaFormat == 0 && !(r->dwFlags & AVDRQ_FORCEALPHA) && !(r->dwFlags & AVDRQ_AERO))
			StretchBlt(r->hTargetDC, r->rcDraw.left + leftoffset, r->rcDraw.top + topoffset, newWidth, newHeight, hdcAvatar, 0, 0, bmWidth, bmHeight, SRCCOPY);
		else {
			// get around SUCKY AlphaBlend() rescaling quality...
			FIBITMAP *fb = FreeImage_CreateDIBFromHBITMAP(hbm);
			FIBITMAP *fbResized = FreeImage_Rescale(fb, newWidth, newHeight, FILTER_BICUBIC);
			HBITMAP hbmResized = FreeImage_CreateHBITMAPFromDIB(fbResized);
			FreeImage_Unload(fb);
			FreeImage_Unload(fbResized);

			HBITMAP hbmTempOld;
			HDC hdcTemp = CreateCompatibleDC(r->hTargetDC);
			hbmTempOld = (HBITMAP)SelectObject(hdcTemp, hbmResized);

			GdiAlphaBlend(
				r->hTargetDC, r->rcDraw.left + leftoffset, r->rcDraw.top + topoffset, newWidth, newHeight,
				hdcTemp, 0, 0, newWidth, newHeight, bf);

			SelectObject(hdcTemp, hbmTempOld);
			DeleteObject(hbmResized);
			DeleteDC(hdcTemp);
		}

		if ((r->dwFlags & AVDRQ_DRAWBORDER) && !((r->dwFlags & AVDRQ_HIDEBORDERONTRANSPARENCY) && (dwFlags & AVS_HASTRANSPARENCY))) {
			HBRUSH br = CreateSolidBrush(r->clrBorder);
			HBRUSH brOld = (HBRUSH)SelectObject(r->hTargetDC, br);
			FrameRgn(r->hTargetDC, rgn, br, 1, 1);
			SelectObject(r->hTargetDC, brOld);
			DeleteObject(br);
		}

		SelectClipRgn(r->hTargetDC, oldRgn);
		DeleteObject(rgn);
		if (oldRgn) DeleteObject(oldRgn);

		SelectObject(hdcAvatar, hbmMem);
		DeleteDC(hdcAvatar);
	}
}

static int ModulesLoaded(WPARAM, LPARAM)
{
	wchar_t szEventName[100];
	mir_snwprintf(szEventName, L"avs_loaderthread_%d", GetCurrentThreadId());
	hLoaderEvent = CreateEvent(nullptr, TRUE, FALSE, szEventName);

	SetThreadPriority(mir_forkthread(PicLoader), THREAD_PRIORITY_IDLE);

	// Folders plugin support
	hMyAvatarsFolder = FoldersRegisterCustomPathW(LPGEN("Avatars"), LPGEN("My Avatars"), MIRANDA_USERDATAW L"\\Avatars");
	hGlobalAvatarFolder = FoldersRegisterCustomPathW(LPGEN("Avatars"), LPGEN("My Global Avatar Cache"), MIRANDA_USERDATAW L"\\Avatars");

	g_AvatarHistoryAvail = ServiceExists(MS_AVATARHISTORY_ENABLED);

	LoadDefaultInfo();

	int protoCount;
	PROTOCOLDESCRIPTOR **proto;
	Proto_EnumProtocols(&protoCount, &proto);
	for (int i = 0; i < protoCount; i++)
		LoadProtoInfo(proto[i]);
	
	for (auto &it : Accounts())
		LoadAccountInfo(it);

	// Load global avatar
	protoPicCacheEntry *pce = new protoPicCacheEntry(PCE_TYPE_GLOBAL, "");
	CreateAvatarInCache(INVALID_CONTACT_ID, pce, "");
	g_MyAvatars.insert(pce);

	HookEvent(ME_PROTO_ACCLISTCHANGED, OnAccChanged);
	HookEvent(ME_SYSTEM_PRESHUTDOWN, ShutdownProc);
	HookEvent(ME_USERINFO_INITIALISE, OnDetailsInit);
	return 0;
}

static IconItem iconList[] =
{
	{ LPGEN("Avatar"), "main", IDI_AVATAR },
};

int CMPlugin::Load()
{
	registerIcon("AVS", iconList);

	LoadACC();

	hShutdownEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr);

	HookEvent(ME_OPT_INITIALISE, OptInit);
	HookEvent(ME_SYSTEM_MODULESLOADED, ModulesLoaded);
	HookEvent(ME_DB_CONTACT_SETTINGCHANGED, ContactSettingChanged);
	HookEvent(ME_DB_CONTACT_DELETED, ContactDeleted);
	HookEvent(ME_PROTO_ACK, ProtocolAck);
	HookEvent(ME_MC_DEFAULTTCHANGED, MetaChanged);
	HookEvent(ME_MC_SUBCONTACTSCHANGED, MetaChanged);

	hEventChanged = CreateHookableEvent(ME_AV_AVATARCHANGED);
	hEventContactAvatarChanged = CreateHookableEvent(ME_AV_CONTACTAVATARCHANGED);
	hMyAvatarChanged = CreateHookableEvent(ME_AV_MYAVATARCHANGED);

	InitServices();
	InitPolls();

	wcsncpy_s(g_szDataPath, _countof(g_szDataPath), VARSW(L"%miranda_userdata%\\"), _TRUNCATE);
	wcslwr(g_szDataPath);
	return 0;
}

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

int CMPlugin::Unload()
{
	UnregisterClassW(AVATAR_CONTROL_CLASS, 0);

	UninitPolls();
	UnloadCache();

	DestroyHookableEvent(hEventChanged);
	DestroyHookableEvent(hEventContactAvatarChanged);
	DestroyHookableEvent(hMyAvatarChanged);

	if (hLoaderThread)
		WaitForSingleObject(hLoaderThread, INFINITE);

	CloseHandle(hLoaderEvent);
	CloseHandle(hShutdownEvent);
	return 0;
}