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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
|
/*
Miranda NG: the free IM client for Microsoft* Windows*
Copyright (c) 2012-18 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"
/////////////////////////////////////////////////////////////////////////////////////////
INT_PTR GetAvatarBitmap(WPARAM hContact, LPARAM)
{
if (hContact == 0 || g_shutDown)
return 0;
hContact = GetContactThatHaveTheAvatar(hContact);
// Get the node
CacheNode *node = FindAvatarInCache(hContact, true);
if (node == nullptr || !node->loaded)
return (INT_PTR)GetProtoDefaultAvatar(hContact);
return (INT_PTR)node;
}
/////////////////////////////////////////////////////////////////////////////////////////
INT_PTR ProtectAvatar(WPARAM hContact, LPARAM lParam)
{
BYTE was_locked = db_get_b(hContact, "ContactPhoto", "Locked", 0);
if (was_locked == (BYTE)lParam) // no need for redundant lockings...
return 0;
if (hContact) {
if (!was_locked)
MakePathRelative(hContact);
db_set_b(hContact, "ContactPhoto", "Locked", lParam ? 1 : 0);
if (lParam == 0)
MakePathRelative(hContact);
ChangeAvatar(hContact, true);
}
return 0;
}
/*
* set an avatar (service function)
* if lParam == NULL, a open file dialog will be opened, otherwise, lParam is taken as a FULL
* image filename (will be checked for existance, though)
*/
struct OpenFileSubclassData
{
BYTE *locking_request;
BYTE setView;
};
UINT_PTR CALLBACK OpenFileSubclass(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
OpenFileSubclassData *data = (OpenFileSubclassData *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
switch (msg) {
case WM_INITDIALOG:
TranslateDialogDefault(hwnd);
{
OPENFILENAME *ofn = (OPENFILENAME *)lParam;
data = (OpenFileSubclassData *)malloc(sizeof(OpenFileSubclassData));
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)data);
data->locking_request = (BYTE *)ofn->lCustData;
data->setView = TRUE;
CheckDlgButton(hwnd, IDC_PROTECTAVATAR, *(data->locking_request) ? BST_CHECKED : BST_UNCHECKED);
}
break;
case WM_COMMAND:
if (LOWORD(wParam) == IDC_PROTECTAVATAR)
*(data->locking_request) = IsDlgButtonChecked(hwnd, IDC_PROTECTAVATAR) ? TRUE : FALSE;
break;
case WM_NOTIFY:
if (data->setView) {
HWND hwndParent = GetParent(hwnd);
HWND hwndLv = FindWindowEx(hwndParent, nullptr, L"SHELLDLL_DefView", nullptr);
if (hwndLv != nullptr) {
SendMessage(hwndLv, WM_COMMAND, SHVIEW_THUMBNAIL, 0);
data->setView = FALSE;
}
}
break;
case WM_NCDESTROY:
free((OpenFileSubclassData *)data);
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)0);
break;
}
return FALSE;
}
INT_PTR SetAvatar(WPARAM hContact, LPARAM lParam)
{
wchar_t FileName[MAX_PATH];
wchar_t *szFinalName;
BYTE locking_request;
if (hContact == NULL)
return 0;
int is_locked = db_get_b(hContact, "ContactPhoto", "Locked", 0);
wchar_t *tszPath = (wchar_t*)lParam;
if (tszPath == nullptr) {
wchar_t filter[256];
Bitmap_GetFilter(filter, _countof(filter));
OPENFILENAME ofn = { 0 };
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = nullptr;
ofn.lpstrFile = FileName;
ofn.lpstrFilter = filter;
ofn.nMaxFile = MAX_PATH;
ofn.nMaxFileTitle = MAX_PATH;
ofn.Flags = OFN_FILEMUSTEXIST | OFN_ENABLETEMPLATE | OFN_EXPLORER | OFN_ENABLESIZING | OFN_ENABLEHOOK;
ofn.lpstrInitialDir = L".";
*FileName = '\0';
ofn.lpstrDefExt = L"";
ofn.hInstance = g_hInst;
ofn.lpTemplateName = MAKEINTRESOURCE(IDD_OPENSUBCLASS);
ofn.lpfnHook = OpenFileSubclass;
locking_request = is_locked;
ofn.lCustData = (LPARAM)&locking_request;
if (!GetOpenFileName(&ofn))
return 0;
szFinalName = FileName;
is_locked = locking_request ? 1 : is_locked;
}
else szFinalName = tszPath;
// filename is now set, check it and perform all needed action
if (_waccess(szFinalName, 4) == -1)
return 0;
// file exists...
wchar_t szBackupName[MAX_PATH];
PathToRelativeW(szFinalName, szBackupName, g_szDataPath);
db_set_ws(hContact, "ContactPhoto", "Backup", szBackupName);
db_set_b(hContact, "ContactPhoto", "Locked", is_locked);
db_set_ws(hContact, "ContactPhoto", "File", szFinalName);
MakePathRelative(hContact, szFinalName);
// Fix cache
ChangeAvatar(hContact, true);
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
// see if is possible to set the avatar for the expecified protocol
static INT_PTR CanSetMyAvatar(WPARAM wParam, LPARAM)
{
char *protocol = (char *)wParam;
if (protocol == nullptr)
return 0;
return ProtoServiceExists(protocol, PS_SETMYAVATAR);
}
/////////////////////////////////////////////////////////////////////////////////////////
// set an avatar for a protocol(service function)
// if lParam == NULL, a open file dialog will be opened, otherwise, lParam is taken as a FULL
// image filename (will be checked for existance, though)
static int InternalRemoveMyAvatar(char *protocol)
{
SetIgnoreNotify(protocol, TRUE);
// Remove avatar
int ret = 0;
if (protocol != nullptr) {
if (ProtoServiceExists(protocol, PS_SETMYAVATAR))
ret = SaveAvatar(protocol, nullptr);
else
ret = -3;
if (ret == 0) {
// Has global avatar?
DBVARIANT dbv = { 0 };
if (!db_get_ws(NULL, AVS_MODULE, "GlobalUserAvatarFile", &dbv)) {
db_free(&dbv);
db_set_b(NULL, AVS_MODULE, "GlobalUserAvatarNotConsistent", 1);
DeleteGlobalUserAvatar();
}
}
}
else {
PROTOACCOUNT **accs;
int i, count;
Proto_EnumAccounts(&count, &accs);
for (i = 0; i < count; i++) {
if (!ProtoServiceExists(accs[i]->szModuleName, PS_SETMYAVATAR))
continue;
if (!Proto_IsAvatarsEnabled(accs[i]->szModuleName))
continue;
// Found a protocol
int retTmp = SaveAvatar(accs[i]->szModuleName, nullptr);
if (retTmp != 0)
ret = retTmp;
}
DeleteGlobalUserAvatar();
if (ret)
db_set_b(NULL, AVS_MODULE, "GlobalUserAvatarNotConsistent", 1);
else
db_set_b(NULL, AVS_MODULE, "GlobalUserAvatarNotConsistent", 0);
}
SetIgnoreNotify(protocol, FALSE);
ReportMyAvatarChanged(WPARAM((protocol == nullptr) ? "" : protocol), 0);
return ret;
}
static void FilterGetStrings(CMStringW &filter, BOOL xml, BOOL swf)
{
filter.AppendFormat(L"%s (*.bmp;*.jpg;*.gif;*.png", TranslateT("All files"));
if (swf) filter.Append(L";*.swf");
if (xml) filter.Append(L";*.xml");
filter.AppendFormat(L")%c*.BMP;*.RLE;*.JPG;*.JPEG;*.GIF;*.PNG", 0);
if (swf) filter.Append(L";*.SWF");
if (xml) filter.Append(L";*.XML");
filter.AppendChar(0);
filter.AppendFormat(L"%s (*.bmp;*.rle)%c*.BMP;*.RLE%c", TranslateT("Windows bitmaps"), 0, 0);
filter.AppendFormat(L"%s (*.jpg;*.jpeg)%c*.JPG;*.JPEG%c", TranslateT("JPEG bitmaps"), 0, 0);
filter.AppendFormat(L"%s (*.gif)%c*.GIF%c", TranslateT("GIF bitmaps"), 0, 0);
filter.AppendFormat(L"%s (*.png)%c*.PNG%c", TranslateT("PNG bitmaps"), 0, 0);
if (swf)
filter.AppendFormat(L"%s (*.swf)%c*.SWF%c", TranslateT("Flash animations"), 0, 0);
if (xml)
filter.AppendFormat(L"%s (*.xml)%c*.XML%c", TranslateT("XML files"), 0, 0);
filter.AppendChar(0);
}
/////////////////////////////////////////////////////////////////////////////////////////
// Callback to set thumbnaill view to open dialog
static UINT_PTR CALLBACK SetMyAvatarHookProc(HWND hwnd, UINT msg, WPARAM, LPARAM lParam)
{
OPENFILENAME *ofn = (OPENFILENAME *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
SetMyAvatarHookData *data;
switch (msg) {
case WM_INITDIALOG:
hwndSetMyAvatar = hwnd;
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)lParam);
ofn = (OPENFILENAME *)lParam;
data = (SetMyAvatarHookData *)ofn->lCustData;
data->thumbnail = TRUE;
SetDlgItemText(hwnd, IDC_MAKE_SQUARE, TranslateT("Make the avatar square"));
SetDlgItemText(hwnd, IDC_GROW, TranslateT("Grow avatar to fit max allowed protocol size"));
CheckDlgButton(hwnd, IDC_MAKE_SQUARE, data->square ? BST_CHECKED : BST_UNCHECKED);
CheckDlgButton(hwnd, IDC_GROW, data->grow ? BST_CHECKED : BST_UNCHECKED);
if (data->protocol != nullptr && (Proto_AvatarImageProportion(data->protocol) & PIP_SQUARE))
EnableWindow(GetDlgItem(hwnd, IDC_MAKE_SQUARE), FALSE);
break;
case WM_NOTIFY:
data = (SetMyAvatarHookData *)ofn->lCustData;
if (data->thumbnail) {
HWND hwndParent = GetParent(hwnd);
HWND hwndLv = FindWindowEx(hwndParent, nullptr, L"SHELLDLL_DefView", nullptr);
if (hwndLv != nullptr) {
SendMessage(hwndLv, WM_COMMAND, SHVIEW_THUMBNAIL, 0);
data->thumbnail = FALSE;
}
}
break;
case WM_DESTROY:
data = (SetMyAvatarHookData *)ofn->lCustData;
data->square = IsDlgButtonChecked(hwnd, IDC_MAKE_SQUARE);
data->grow = IsDlgButtonChecked(hwnd, IDC_GROW);
hwndSetMyAvatar = nullptr;
break;
}
return 0;
}
struct SaveProtocolData
{
DWORD max_size;
wchar_t image_file_name[MAX_PATH];
BOOL saved;
BOOL need_smaller_size;
int width;
int height;
wchar_t temp_file[MAX_PATH];
HBITMAP hBmpProto;
};
void SaveImage(SaveProtocolData &d, char *protocol, int format)
{
if (!Proto_IsAvatarFormatSupported(protocol, format))
return;
mir_snwprintf(d.image_file_name, L"%s%s", d.temp_file, ProtoGetAvatarExtension(format));
if (BmpFilterSaveBitmap(d.hBmpProto, d.image_file_name, format == PA_FORMAT_JPEG ? JPEG_QUALITYSUPERB : 0))
return;
if (d.max_size != 0 && GetFileSize(d.image_file_name) > d.max_size) {
DeleteFile(d.image_file_name);
if (format == PA_FORMAT_JPEG) {
// Try with lower quality
if (!BmpFilterSaveBitmap(d.hBmpProto, d.image_file_name, JPEG_QUALITYGOOD)) {
if (GetFileSize(d.image_file_name) > d.max_size) {
DeleteFile(d.image_file_name);
d.need_smaller_size = TRUE;
}
else d.saved = TRUE;
}
}
else d.need_smaller_size = TRUE;
}
else d.saved = TRUE;
}
static int SetProtoMyAvatar(char *protocol, HBITMAP hBmp, wchar_t *originalFilename, int originalFormat, BOOL square, BOOL grow)
{
if (!ProtoServiceExists(protocol, PS_SETMYAVATAR))
return -1;
// If is swf or xml, just set it
if (originalFormat == PA_FORMAT_SWF) {
if (!Proto_IsAvatarFormatSupported(protocol, PA_FORMAT_SWF))
return -1;
return SaveAvatar(protocol, originalFilename);
}
if (originalFormat == PA_FORMAT_XML) {
if (!Proto_IsAvatarFormatSupported(protocol, PA_FORMAT_XML))
return -1;
return SaveAvatar(protocol, originalFilename);
}
// Get protocol info
SaveProtocolData d = { 0 };
d.max_size = (DWORD)Proto_GetAvatarMaxFileSize(protocol);
Proto_GetAvatarMaxSize(protocol, &d.width, &d.height);
int orig_width = d.width;
int orig_height = d.height;
if (Proto_AvatarImageProportion(protocol) & PIP_SQUARE)
square = TRUE;
// Try to save until a valid image is found or we give up
int num_tries = 0;
do {
// Lets do it
int fit = (grow ? 0 : RESIZEBITMAP_FLAG_DONT_GROW) | (square ? RESIZEBITMAP_MAKE_SQUARE : RESIZEBITMAP_KEEP_PROPORTIONS);
d.hBmpProto = Image_Resize(hBmp, fit, d.height, d.width);
if (d.hBmpProto == nullptr) {
if (d.temp_file[0] != '\0')
DeleteFile(d.temp_file);
return -1;
}
// Check if can use original image
if (d.hBmpProto == hBmp
&& Proto_IsAvatarFormatSupported(protocol, originalFormat)
&& (d.max_size == 0 || GetFileSize(originalFilename) < d.max_size)) {
if (d.temp_file[0] != '\0')
DeleteFile(d.temp_file);
// Use original image
return SaveAvatar(protocol, originalFilename);
}
// Create a temporary file (if was not created already)
if (d.temp_file[0] == '\0') {
d.temp_file[0] = '\0';
if (GetTempPath(MAX_PATH, d.temp_file) == 0
|| GetTempFileName(d.temp_file, L"mir_av_", 0, d.temp_file) == 0) {
DeleteObject(d.hBmpProto);
return -1;
}
}
// Which format?
// First try to use original format
if (originalFormat != PA_FORMAT_BMP)
SaveImage(d, protocol, originalFormat);
if (!d.saved && originalFormat != PA_FORMAT_PNG)
SaveImage(d, protocol, PA_FORMAT_PNG);
if (!d.saved && originalFormat != PA_FORMAT_JPEG)
SaveImage(d, protocol, PA_FORMAT_JPEG);
if (!d.saved && originalFormat != PA_FORMAT_GIF)
SaveImage(d, protocol, PA_FORMAT_GIF);
if (!d.saved)
SaveImage(d, protocol, PA_FORMAT_BMP);
num_tries++;
if (!d.saved && d.need_smaller_size && num_tries < 4) {
// Cleanup
if (d.hBmpProto != hBmp)
DeleteObject(d.hBmpProto);
// use a smaller size
d.width = orig_width * (4 - num_tries) / 4;
d.height = orig_height * (4 - num_tries) / 4;
}
} while (!d.saved && d.need_smaller_size && num_tries < 4);
int ret;
if (d.saved) {
// Call proto service
ret = SaveAvatar(protocol, d.image_file_name);
DeleteFile(d.image_file_name);
}
else ret = -1;
if (d.temp_file[0] != '\0')
DeleteFile(d.temp_file);
if (d.hBmpProto != hBmp)
DeleteObject(d.hBmpProto);
return ret;
}
static int InternalSetMyAvatar(char *protocol, wchar_t *szFinalName, SetMyAvatarHookData &data, BOOL allAcceptXML, BOOL allAcceptSWF)
{
int format = ProtoGetAvatarFormat(szFinalName);
if (format == PA_FORMAT_UNKNOWN || _waccess(szFinalName, 4) == -1)
return -3;
// file exists...
HBITMAP hBmp = nullptr;
if (format == PA_FORMAT_SWF) {
if (!allAcceptSWF)
return -4;
}
else if (format == PA_FORMAT_XML) {
if (!allAcceptXML)
return -4;
}
else {
// Try to open if is not a flash or XML
hBmp = Bitmap_Load(szFinalName);
if (hBmp == nullptr)
return -4;
}
SetIgnoreNotify(protocol, TRUE);
int ret = 0;
if (protocol != nullptr) {
ret = SetProtoMyAvatar(protocol, hBmp, szFinalName, format, data.square, data.grow);
if (ret == 0) {
DeleteGlobalUserAvatar();
db_set_b(NULL, AVS_MODULE, "GlobalUserAvatarNotConsistent", 1);
}
}
else {
int count;
PROTOACCOUNT **accs;
Proto_EnumAccounts(&count, &accs);
for (int i = 0; i < count; i++) {
if (!ProtoServiceExists(accs[i]->szModuleName, PS_SETMYAVATAR))
continue;
if (!Proto_IsAvatarsEnabled(accs[i]->szModuleName))
continue;
int retTmp = SetProtoMyAvatar(accs[i]->szModuleName, hBmp, szFinalName, format, data.square, data.grow);
if (retTmp != 0)
ret = retTmp;
}
DeleteGlobalUserAvatar();
if (ret)
db_set_b(NULL, AVS_MODULE, "GlobalUserAvatarNotConsistent", 1);
else {
// Copy avatar file to store as global one
wchar_t globalFile[1024];
BOOL saved = TRUE;
if (FoldersGetCustomPathT(hGlobalAvatarFolder, globalFile, _countof(globalFile), L"")) {
mir_snwprintf(globalFile, L"%s%s", g_szDataPath, L"GlobalAvatar");
CreateDirectory(globalFile, nullptr);
}
wchar_t *ext = wcsrchr(szFinalName, '.'); // Can't be NULL here
if (format == PA_FORMAT_XML || format == PA_FORMAT_SWF) {
mir_snwprintf(globalFile, L"%s\\my_global_avatar%s", globalFile, ext);
CopyFile(szFinalName, globalFile, FALSE);
}
else {
// Resize (to avoid too big avatars)
int fit = (data.grow ? 0 : RESIZEBITMAP_FLAG_DONT_GROW) | (data.square ? RESIZEBITMAP_MAKE_SQUARE : RESIZEBITMAP_KEEP_PROPORTIONS);
HBITMAP hBmpTmp = Image_Resize(hBmp, fit, 300, 300);
if (hBmpTmp == hBmp || hBmpTmp == nullptr) {
// Use original image
mir_snwprintf(globalFile, L"%s\\my_global_avatar%s", globalFile, ext);
CopyFile(szFinalName, globalFile, FALSE);
}
else {
// Save as PNG
mir_snwprintf(globalFile, L"%s\\my_global_avatar.png", globalFile);
if (BmpFilterSaveBitmap(hBmpTmp, globalFile, 0))
saved = FALSE;
DeleteObject(hBmpTmp);
}
}
if (saved) {
wchar_t relFile[1024];
if (PathToRelativeW(globalFile, relFile, g_szDataPath))
db_set_ws(NULL, AVS_MODULE, "GlobalUserAvatarFile", relFile);
else
db_set_ws(NULL, AVS_MODULE, "GlobalUserAvatarFile", globalFile);
db_set_b(NULL, AVS_MODULE, "GlobalUserAvatarNotConsistent", 0);
}
else db_set_b(NULL, AVS_MODULE, "GlobalUserAvatarNotConsistent", 1);
}
}
DeleteObject(hBmp);
SetIgnoreNotify(protocol, FALSE);
ReportMyAvatarChanged(WPARAM((protocol == nullptr) ? "" : protocol), 0);
return ret;
}
INT_PTR SetMyAvatar(WPARAM wParam, LPARAM lParam)
{
wchar_t FileName[MAX_PATH];
wchar_t *szFinalName = nullptr;
BOOL allAcceptXML;
BOOL allAcceptSWF;
// Protocol allow seting of avatar?
char* protocol = (char*)wParam;
if (protocol != nullptr && !CanSetMyAvatar((WPARAM)protocol, 0))
return -1;
wchar_t* tszPath = (wchar_t*)lParam;
if (tszPath == nullptr && hwndSetMyAvatar != nullptr) {
SetForegroundWindow(hwndSetMyAvatar);
SetFocus(hwndSetMyAvatar);
ShowWindow(hwndSetMyAvatar, SW_SHOW);
return -2;
}
SetMyAvatarHookData data = {};
// Check for XML and SWF
if (protocol == nullptr) {
allAcceptXML = TRUE;
allAcceptSWF = TRUE;
int count;
PROTOACCOUNT **accs;
Proto_EnumAccounts(&count, &accs);
for (int i = 0; i < count; i++) {
if (!ProtoServiceExists(accs[i]->szModuleName, PS_SETMYAVATAR))
continue;
if (!Proto_IsAvatarsEnabled(accs[i]->szModuleName))
continue;
allAcceptXML = allAcceptXML && Proto_IsAvatarFormatSupported(accs[i]->szModuleName, PA_FORMAT_XML);
allAcceptSWF = allAcceptSWF && Proto_IsAvatarFormatSupported(accs[i]->szModuleName, PA_FORMAT_SWF);
}
data.square = db_get_b(0, AVS_MODULE, "SetAllwaysMakeSquare", 0);
}
else {
allAcceptXML = Proto_IsAvatarFormatSupported(protocol, PA_FORMAT_XML);
allAcceptSWF = Proto_IsAvatarFormatSupported(protocol, PA_FORMAT_SWF);
data.protocol = protocol;
data.square = (Proto_AvatarImageProportion(protocol) & PIP_SQUARE)
|| db_get_b(0, AVS_MODULE, "SetAllwaysMakeSquare", 0);
}
if (tszPath == nullptr) {
data.protocol = protocol;
CMStringW filter;
FilterGetStrings(filter, allAcceptXML, allAcceptSWF);
wchar_t inipath[1024];
FoldersGetCustomPathT(hMyAvatarsFolder, inipath, _countof(inipath), L".");
OPENFILENAME ofn = { 0 };
ofn.lStructSize = sizeof(ofn);
ofn.lpstrFile = FileName;
ofn.lpstrFilter = filter;
ofn.nMaxFile = MAX_PATH;
ofn.nMaxFileTitle = MAX_PATH;
ofn.Flags = OFN_FILEMUSTEXIST | OFN_ENABLETEMPLATE | OFN_EXPLORER | OFN_ENABLESIZING | OFN_ENABLEHOOK;
ofn.lpstrInitialDir = inipath;
ofn.lpTemplateName = MAKEINTRESOURCE(IDD_SET_OWN_SUBCLASS);
ofn.lpfnHook = SetMyAvatarHookProc;
ofn.lCustData = (LPARAM)&data;
*FileName = '\0';
ofn.lpstrDefExt = L"";
ofn.hInstance = g_hInst;
wchar_t title[256];
if (protocol == nullptr)
mir_snwprintf(title, TranslateT("Set my avatar"));
else {
wchar_t* prototmp = mir_a2u(protocol);
mir_snwprintf(title, TranslateT("Set my avatar for %s"), prototmp);
mir_free(prototmp);
}
ofn.lpstrTitle = title;
if (!GetOpenFileName(&ofn))
return 1;
szFinalName = FileName;
}
else szFinalName = (wchar_t*)tszPath;
// filename is now set, check it and perform all needed action
if (szFinalName[0] == '\0')
return InternalRemoveMyAvatar(protocol);
return InternalSetMyAvatar(protocol, szFinalName, data, allAcceptXML, allAcceptSWF);
}
/////////////////////////////////////////////////////////////////////////////////////////
INT_PTR CALLBACK DlgProcAvatarOptions(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam);
static INT_PTR ContactOptions(WPARAM wParam, LPARAM)
{
if (wParam)
CreateDialogParam(g_hInst, MAKEINTRESOURCE(IDD_AVATAROPTIONS), nullptr, DlgProcAvatarOptions, (LPARAM)wParam);
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
INT_PTR DrawAvatarPicture(WPARAM, LPARAM lParam)
{
AVATARCACHEENTRY *ace = nullptr;
AVATARDRAWREQUEST *r = (AVATARDRAWREQUEST*)lParam;
if (r == nullptr || IsBadReadPtr((void *)r, sizeof(AVATARDRAWREQUEST)))
return 0;
if (r->cbSize != sizeof(AVATARDRAWREQUEST))
return 0;
if (r->dwFlags & AVDRQ_PROTOPICT) {
if (r->szProto == nullptr)
return 0;
for (auto &p : g_ProtoPictures) {
if (!mir_strcmp(p->szProtoname, r->szProto) && mir_strlen(r->szProto) == mir_strlen(p->szProtoname) && p->hbmPic != nullptr) {
ace = p;
break;
}
}
}
else if (r->dwFlags & AVDRQ_OWNPIC) {
if (r->szProto == nullptr)
return 0;
if (r->szProto[0] == '\0' && db_get_b(NULL, AVS_MODULE, "GlobalUserAvatarNotConsistent", 1))
return -1;
ace = (AVATARCACHEENTRY *)GetMyAvatar(0, (LPARAM)r->szProto);
}
else ace = (AVATARCACHEENTRY *)GetAvatarBitmap((WPARAM)r->hContact, 0);
if (ace && (!(r->dwFlags & AVDRQ_RESPECTHIDDEN) || !(ace->dwFlags & AVS_HIDEONCLIST))) {
ace->t_lastAccess = time(nullptr);
if (ace->bmHeight == 0 || ace->bmWidth == 0 || ace->hbmPic == nullptr)
return 0;
InternalDrawAvatar(r, ace->hbmPic, ace->bmWidth, ace->bmHeight, ace->dwFlags);
return 1;
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
INT_PTR GetMyAvatar(WPARAM wParam, LPARAM lParam)
{
if (wParam || g_shutDown)
return 0;
char *szProto = (char *)lParam;
if (lParam == 0 || IsBadReadPtr(szProto, 4))
return 0;
for (auto &it : g_MyAvatars)
if (!mir_strcmp(szProto, it->szProtoname) && it->hbmPic != nullptr)
return (INT_PTR)it;
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
static void ReloadMyAvatar(LPVOID lpParam)
{
Thread_SetName("AVS: ReloadMyAvatar");
char *szProto = (char *)lpParam;
mir_sleep(500);
if (g_shutDown)
return;
for (auto &it : g_MyAvatars) {
char *myAvatarProto = it->szProtoname;
if (szProto[0] == 0) {
// Notify to all possibles
if (mir_strcmp(myAvatarProto, szProto)) {
if (!ProtoServiceExists(myAvatarProto, PS_SETMYAVATAR))
continue;
if (!Proto_IsAvatarsEnabled(myAvatarProto))
continue;
}
}
else if (mir_strcmp(myAvatarProto, szProto))
continue;
if (it->hbmPic)
DeleteObject(it->hbmPic);
if (CreateAvatarInCache(INVALID_CONTACT_ID, it, myAvatarProto) != -1)
NotifyEventHooks(hMyAvatarChanged, (WPARAM)myAvatarProto, (LPARAM)it);
else
NotifyEventHooks(hMyAvatarChanged, (WPARAM)myAvatarProto, 0);
}
free(lpParam);
}
INT_PTR ReportMyAvatarChanged(WPARAM wParam, LPARAM)
{
const char *proto = (const char*)wParam;
if (proto == nullptr)
return -1;
for (auto &it : g_MyAvatars) {
if (it->dwFlags & AVS_IGNORENOTIFY)
continue;
if (!mir_strcmp(it->szProtoname, proto)) {
LPVOID lpParam = (void *)malloc(mir_strlen(it->szProtoname) + 2);
mir_strcpy((char *)lpParam, it->szProtoname);
mir_forkthread(ReloadMyAvatar, lpParam);
return 0;
}
}
return -2;
}
/////////////////////////////////////////////////////////////////////////////////////////
void InitServices()
{
CreateServiceFunction(MS_AV_GETAVATARBITMAP, GetAvatarBitmap);
CreateServiceFunction(MS_AV_PROTECTAVATAR, ProtectAvatar);
CreateServiceFunction(MS_AV_SETAVATARW, SetAvatar);
CreateServiceFunction(MS_AV_SETMYAVATARW, SetMyAvatar);
CreateServiceFunction(MS_AV_CANSETMYAVATAR, CanSetMyAvatar);
CreateServiceFunction(MS_AV_CONTACTOPTIONS, ContactOptions);
CreateServiceFunction(MS_AV_DRAWAVATAR, DrawAvatarPicture);
CreateServiceFunction(MS_AV_GETMYAVATAR, GetMyAvatar);
CreateServiceFunction(MS_AV_REPORTMYAVATARCHANGED, ReportMyAvatarChanged);
}
|