summaryrefslogtreecommitdiff
path: root/plugins/AvatarHistory/src/AvatarDlg.cpp
blob: 6bf53acbf14b688c87decd3b32e6e458058e2c5c (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
/*
Avatar History Plugin
 Copyright (C) 2006  Matthew Wild - Email: mwild1@gmail.com

 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/

#include "AvatarHistory.h"

HGENMENU hMenu = NULL; 
DWORD WINAPI AvatarDialogThread(LPVOID param);
static INT_PTR CALLBACK AvatarDlgProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam);
int ShowSaveDialog(HWND hwnd, TCHAR* fn,HANDLE hContact = NULL);

BOOL ProtocolEnabled(const char *proto);
int FillAvatarListFromDB(HWND list, HANDLE hContact);
int FillAvatarListFromFolder(HWND list, HANDLE hContact);
int FillAvatarListFromFiles(HWND list, HANDLE hContact);
int CleanupAvatarPic(HWND hwnd);
bool UpdateAvatarPic(HWND hwnd);
TCHAR * GetContactFolder(TCHAR *fn, HANDLE hContact);
BOOL ResolveShortcut(TCHAR *shortcut, TCHAR *file);

static INT_PTR ShowDialogSvc(WPARAM wParam, LPARAM lParam);

struct AvatarDialogData
{
	HANDLE hContact;
	TCHAR fn[MAX_PATH];
	HWND parent;
};


class ListEntry
{
public:
	ListEntry()
	{
		hDbEvent = NULL;
		filename = NULL;
		filelink = NULL;
	}

	~ListEntry()
	{
		mir_free(filename);
		mir_free(filelink);
	}

	HANDLE hDbEvent;
	TCHAR *filename;
	TCHAR *filelink;
};

int OpenAvatarDialog(HANDLE hContact, char* fn)
{
	HWND hAvatarWindow = WindowList_Find(hAvatarWindowsList, hContact);
	if (hAvatarWindow)
	{
		SetForegroundWindow(hAvatarWindow);
		SetFocus(hAvatarWindow);
		return 0;
	}

	DWORD dwId;
	struct AvatarDialogData *avdlg = (struct AvatarDialogData*)malloc(sizeof(struct AvatarDialogData));
	ZeroMemory(avdlg, sizeof(struct AvatarDialogData));
	avdlg->hContact = hContact;
	if (fn == NULL)
	{
		avdlg->fn[0] = _T('\0');
	}
	else
	{
		MultiByteToWideChar(CP_ACP, 0, fn, -1, avdlg->fn, SIZEOF(avdlg->fn));
	}

	CloseHandle(CreateThread(NULL, 0, AvatarDialogThread, (LPVOID)avdlg, 0, &dwId));
	return 0;
}

DWORD WINAPI AvatarDialogThread(LPVOID param)
{
	struct AvatarDialogData* data = (struct AvatarDialogData*)param;
	DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_AVATARDLG), data->parent, AvatarDlgProc, (LPARAM)param);
	return 0;
}

void EnableDisableControls(HWND hwnd)
{
	HWND list = GetDlgItem(hwnd, IDC_AVATARLIST);
	
	int cursel = SendMessage(list, LB_GETCURSEL, 0, 0);
	int count = SendMessage(list, LB_GETCOUNT, 0, 0);

	if (cursel == LB_ERR)
	{
		EnableWindow(GetDlgItem(hwnd, IDC_BACK), count > 0);
		EnableWindow(GetDlgItem(hwnd, IDC_NEXT), count > 0);
	}
	else
	{
		EnableWindow(GetDlgItem(hwnd, IDC_BACK), cursel > 0);
		EnableWindow(GetDlgItem(hwnd, IDC_NEXT), cursel < count-1);
	}
}

static INT_PTR CALLBACK AvatarDlgProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
	HWND hwndList = GetDlgItem(hwnd, IDC_AVATARLIST);

	switch(uMsg) {
	case WM_INITDIALOG:
		{
			AvatarDialogData *data = (struct AvatarDialogData*) lParam;
			SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)createDefaultOverlayedIcon(TRUE));
			SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)createDefaultOverlayedIcon(FALSE));
			if (db_get_b(NULL, MODULE_NAME, "LogToHistory", AVH_DEF_LOGTOHISTORY))
				FillAvatarListFromDB(hwndList, data->hContact);
			else if (opts.log_store_as_hash)
				FillAvatarListFromFolder(hwndList, data->hContact);
			else
				FillAvatarListFromFiles(hwndList, data->hContact);

			TCHAR *displayName = (TCHAR*) CallService(MS_CLIST_GETCONTACTDISPLAYNAME,(WPARAM)data->hContact,GCDNF_TCHAR);
			if (displayName)
			{
				TCHAR title[MAX_PATH];
				mir_sntprintf(title,MAX_PATH,TranslateT("Avatar History for %s"),displayName);
				SetWindowText(hwnd,title);
			}

			SetWindowLongPtr(hwnd, GWLP_USERDATA, (ULONG_PTR)data->hContact);
			UpdateAvatarPic(hwnd);
			CheckDlgButton(hwnd, IDC_LOGUSER, (UINT)db_get_b(data->hContact, MODULE_NAME, "LogToDisk", BST_INDETERMINATE));
			CheckDlgButton(hwnd, IDC_POPUPUSER, (UINT)db_get_b(data->hContact, MODULE_NAME, "AvatarPopups", BST_INDETERMINATE));
			CheckDlgButton(hwnd, IDC_HISTORYUSER, (UINT)db_get_b(data->hContact, MODULE_NAME, "LogToHistory", BST_INDETERMINATE));
			ShowWindow(GetDlgItem(hwnd, IDC_OPENFOLDER), opts.log_per_contact_folders ? SW_SHOW : SW_HIDE);
			Utils_RestoreWindowPositionNoSize(hwnd,NULL,MODULE_NAME,"AvatarHistoryDialog");
			WindowList_Add(hAvatarWindowsList,hwnd,data->hContact);
			TranslateDialogDefault(hwnd);
			EnableDisableControls(hwnd);
			free(data);
			data = NULL;
		}
		break;

	case WM_CLOSE:
		CleanupAvatarPic(hwnd);
		EndDialog(hwnd, 0);
		return TRUE;

	case WM_DESTROY:
		Utils_SaveWindowPosition(hwnd,NULL,MODULE_NAME,"AvatarHistoryDialog");
		WindowList_Remove(hAvatarWindowsList,hwnd);
		DestroyIcon((HICON)SendMessage(hwnd, WM_SETICON, ICON_BIG, 0));
		DestroyIcon((HICON)SendMessage(hwnd, WM_SETICON, ICON_SMALL, 0));
		{
			int count = SendMessage(hwndList, LB_GETCOUNT, 0, 0);
			for(int i = 0; i < count; i++)
				delete (ListEntry*)SendMessage(hwndList, LB_GETITEMDATA, i, 0);
		}
		break;

	case WM_CONTEXTMENU:
		{
			HWND pic = GetDlgItem(hwnd, IDC_AVATAR);
			int pos;

			if ((HANDLE)wParam == hwndList) {
				POINT p;
				p.x = LOWORD(lParam); 
				p.y = HIWORD(lParam); 

				ScreenToClient(hwndList, &p);

				pos = SendMessage(hwndList, LB_ITEMFROMPOINT, 0, MAKELONG(p.x, p.y));
				if (HIWORD(pos))
					break;
				pos = LOWORD(pos);

				int count = SendMessage(hwndList, LB_GETCOUNT, 0, 0);
				if (pos >= count)
					break;

				SendMessage(hwndList, LB_SETCURSEL, pos, 0);
				EnableDisableControls(hwnd);
			}
			else if ((HANDLE) wParam == pic) {
				pos = SendMessage(hwndList, LB_GETCURSEL, 0, 0);
				if (pos == LB_ERR)
					break;
			}
			else break;

			HMENU menu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_MENU1));
			HMENU submenu = GetSubMenu(menu, 0);
			TranslateMenu(submenu);

			if (!UpdateAvatarPic(hwnd)) {
				RemoveMenu(submenu, 2, MF_BYPOSITION);
				RemoveMenu(submenu, 0, MF_BYPOSITION);
			}

			POINT p;
			p.x = LOWORD(lParam); 
			p.y = HIWORD(lParam); 
			int ret = TrackPopupMenu(submenu, TPM_TOPALIGN|TPM_LEFTALIGN|TPM_RIGHTBUTTON|TPM_RETURNCMD, p.x, p.y, 0, hwndList, NULL);
			DestroyMenu(menu);

			ListEntry *le = (ListEntry*) SendMessage(hwndList, LB_GETITEMDATA, pos, 0);
			HANDLE hContact = (HANDLE) GetWindowLongPtr(hwnd, GWLP_USERDATA);
			switch(ret) {
			case ID_AVATARLISTPOPUP_SAVEAS:
				ShowSaveDialog(hwnd, le->filename, hContact);
				break;

			case ID_AVATARLISTPOPUP_DELETE:
				{
					BOOL blDelete;

					if (le->hDbEvent)
						blDelete = MessageBox(hwnd, TranslateT("Are you sure you wish to delete this history entry?\nOnly the entry in history will be deleted, bitmap file will be kept!"), 
						TranslateT("Delete avatar log?"), MB_YESNO|MB_ICONWARNING|MB_DEFBUTTON2|MB_SETFOREGROUND|MB_TOPMOST) == IDYES;
					else
						blDelete = MessageBox(hwnd, TranslateT("Are you sure you wish to delete this avatar shortcut?\nOnly shortcut will be deleted, bitmap file will be kept!"), 
						TranslateT("Delete avatar log?"), MB_YESNO|MB_ICONWARNING|MB_DEFBUTTON2|MB_SETFOREGROUND|MB_TOPMOST) == IDYES;

					if (blDelete) {
						if (le->hDbEvent)
							db_event_delete(hContact, le->hDbEvent);
						else
							DeleteFile(le->filelink);

						delete le;

						SendMessage(hwndList, LB_DELETESTRING, pos, 0);

						int count = SendMessage(hwndList, LB_GETCOUNT, 0, 0);
						if (count > 0) {
							if (pos >= count)
								pos = count -1;
							SendMessage(hwndList, LB_SETCURSEL, pos, 0);
						}

						UpdateAvatarPic(hwnd);
						EnableDisableControls(hwnd);
					}
				}
				break;

			case ID_AVATARLISTPOPUP_DELETE_BOTH:
				BOOL blDelete;

				if (le->hDbEvent)
					blDelete = MessageBox(hwnd, TranslateT("Are you sure you wish to delete this archived avatar?\nThis will delete the history entry and the bitmap file.\nWARNING:This can affect more than one entry in history!"), 
					TranslateT("Delete avatar?"), MB_YESNO|MB_ICONWARNING|MB_DEFBUTTON2|MB_SETFOREGROUND|MB_TOPMOST) == IDYES;
				else
					blDelete = MessageBox(hwnd, TranslateT("Are you sure you wish to delete this archived avatar?\nThis will delete the shortcut and the bitmap file.\nWARNING:This can affect more than one shortcut!"), 
					TranslateT("Delete avatar?"), MB_YESNO|MB_ICONWARNING|MB_DEFBUTTON2|MB_SETFOREGROUND|MB_TOPMOST) == IDYES;

				if (blDelete) {
					DeleteFile(le->filename);

					if (le->hDbEvent)
						db_event_delete(hContact, le->hDbEvent);
					else
						DeleteFile(le->filelink);

					delete le;

					SendMessage(hwndList, LB_DELETESTRING, pos, 0);

					int count = SendMessage(hwndList, LB_GETCOUNT, 0, 0);
					if (count > 0) {
						if (pos >= count)
							pos = count -1;
						SendMessage(hwndList, LB_SETCURSEL, pos, 0);
					}

					UpdateAvatarPic(hwnd);
					EnableDisableControls(hwnd);
				}
			}
		}
		break;

	case WM_COMMAND:
		switch(LOWORD(wParam)) {
		case IDOK:
			if (HIWORD(wParam) == BN_CLICKED) {
				HANDLE hContact = (HANDLE) GetWindowLongPtr(hwnd, GWLP_USERDATA);
				db_set_b(hContact, MODULE_NAME, "AvatarPopups", (BYTE) IsDlgButtonChecked(hwnd, IDC_POPUPUSER));
				db_set_b(hContact, MODULE_NAME, "LogToDisk", (BYTE) IsDlgButtonChecked(hwnd, IDC_LOGUSER));
				db_set_b(hContact, MODULE_NAME, "LogToHistory", (BYTE) IsDlgButtonChecked(hwnd, IDC_HISTORYUSER));

				CleanupAvatarPic(hwnd);
				EndDialog(hwnd, 0);
				return TRUE;
			}
			break;

		case IDC_AVATARLIST:
			if (HIWORD(wParam) == LBN_SELCHANGE) {
				UpdateAvatarPic(hwnd);
				EnableDisableControls(hwnd);
				return TRUE;
			}
			break;

		case IDC_OPENFOLDER:
			if (HIWORD(wParam) == BN_CLICKED && opts.log_per_contact_folders) {
				TCHAR avfolder[MAX_PATH];
				HANDLE hContact = (HANDLE)GetWindowLongPtr(hwnd, GWLP_USERDATA);
				GetContactFolder(avfolder, hContact);
				ShellExecute(NULL, db_get_b(NULL, MODULE_NAME, "OpenFolderMethod", 0) ? _T("explore") : _T("open"), avfolder, NULL, NULL, SW_SHOWNORMAL);
				return TRUE;
			}
			break;

		case IDC_NEXT:
			if (HIWORD(wParam) == BN_CLICKED) {
				SendMessage(hwndList, LB_SETCURSEL, SendMessage(hwndList, LB_GETCURSEL, 0, 0) +1, 0);
				UpdateAvatarPic(hwnd);
				EnableDisableControls(hwnd);
				return TRUE;
			}
			break;

		case IDC_BACK:
			if (HIWORD(wParam) == BN_CLICKED) {
				int cursel = SendMessage(hwndList, LB_GETCURSEL, 0, 0);
				if (cursel == LB_ERR)
					SendMessage(hwndList, LB_SETCURSEL, SendMessage(hwndList, LB_GETCOUNT, 0, 0) -1, 0);
				else
					SendMessage(hwndList, LB_SETCURSEL, cursel -1, 0);
				UpdateAvatarPic(hwnd);
				EnableDisableControls(hwnd);
				return TRUE;
			}
		}
		break;
	}
	return FALSE;
}


int AddFileToList(TCHAR *path,TCHAR *lnk,TCHAR *filename, HWND list)
{
	// Add to list
	ListEntry *le = new ListEntry();
	le->filename = mir_tstrdup(path);
	le->filelink = mir_tstrdup(lnk);

	TCHAR *p = _tcschr(filename, _T('.'));
	if (p != NULL)
		p[0] = _T('\0');
	int max_pos = SendMessage(list, LB_ADDSTRING, 0, (LPARAM)filename);
	SendMessage(list, LB_SETITEMDATA, max_pos, (LPARAM)le);
	return max_pos;
}

int FillAvatarListFromFiles(HWND list, HANDLE hContact)
{
	int max_pos = 0;
	TCHAR dir[MAX_PATH], path[MAX_PATH];
	WIN32_FIND_DATA finddata;

	GetContactFolder(dir, hContact);
	mir_sntprintf(path, MAX_PATH, _T("%s\\*.*"), dir);

	HANDLE hFind = FindFirstFile(path, &finddata);
	if (hFind == INVALID_HANDLE_VALUE)
		return 0;

	do
	{
		if (finddata.cFileName[0] != '.')
		{
			mir_sntprintf(path, MAX_PATH, _T("%s\\%s"), dir, finddata.cFileName);
			max_pos = AddFileToList(path,finddata.cFileName,finddata.cFileName,list);
		}
	}
		while(FindNextFile(hFind, &finddata));
	FindClose(hFind);
	SendMessage(list, LB_SETCURSEL, max_pos, 0); // Set to first item
	return 0;
}

int FillAvatarListFromFolder(HWND list, HANDLE hContact)
{
	int max_pos = 0;
	TCHAR dir[MAX_PATH], path[MAX_PATH];
	WIN32_FIND_DATA finddata;

	GetContactFolder(dir, hContact);
	mir_sntprintf(path, MAX_PATH, _T("%s\\*.lnk"), dir);

	HANDLE hFind = FindFirstFile(path, &finddata);
	if (hFind == INVALID_HANDLE_VALUE)
		return 0;

	do
	{
		if (finddata.cFileName[0] != '.')
		{
			TCHAR lnk[MAX_PATH];
			mir_sntprintf(lnk, MAX_PATH, _T("%s\\%s"), dir, finddata.cFileName);
			if (ResolveShortcut(lnk, path))
				max_pos = AddFileToList(path,lnk,finddata.cFileName,list);
		}
	}
		while(FindNextFile(hFind, &finddata));
	FindClose(hFind);
	SendMessage(list, LB_SETCURSEL, max_pos, 0); // Set to first item
	return 0;
}

int FillAvatarListFromDB(HWND list, HANDLE hContact)
{
	int max_pos = 0;
	BYTE blob[2048];
	for (HANDLE hDbEvent = db_event_first(hContact); hDbEvent; hDbEvent = db_event_next(hDbEvent)) {
		DBEVENTINFO dbei = { sizeof(dbei) };
		dbei.cbBlob = sizeof(blob);
		dbei.pBlob = blob;
		if (db_event_get(hDbEvent, &dbei) != 0) continue;
		if (dbei.eventType != EVENTTYPE_AVATAR_CHANGE) continue;

		// Get time
		TCHAR date[64];
		DBTIMETOSTRINGT tts = {0};
		tts.szFormat = _T("d s");
		tts.szDest = date;
		tts.cbDest = sizeof(date);
		CallService(MS_DB_TIME_TIMESTAMPTOSTRINGT, (WPARAM)dbei.timestamp, (LPARAM)&tts);

		// Get file in disk
		TCHAR path[MAX_PATH];
		mir_ptr<TCHAR> tszStoredPath(mir_utf8decodeT((char*)dbei.pBlob));
		PathToAbsoluteT(tszStoredPath, path);

		// Add to list
		ListEntry *le = new ListEntry();
		le->hDbEvent = hDbEvent;
		le->filename = mir_tstrdup(path);
		max_pos = SendMessage(list,LB_ADDSTRING, 0, (LPARAM)date);
		SendMessage(list, LB_SETITEMDATA, max_pos, (LPARAM)le);
	}

	SendMessage(list, LB_SETCURSEL, max_pos, 0); // Set to first item
	return 0;
}

bool UpdateAvatarPic(HWND hwnd)
{
	HWND hwndpic = GetDlgItem(hwnd, IDC_AVATAR);
	if (!hwnd || !hwndpic)
		return false;

	HWND list = GetDlgItem(hwnd, IDC_AVATARLIST);
	int cursel = SendMessage(list, LB_GETCURSEL, 0, 0);
	if (cursel < 0)
	{
		SetDlgItemText(hwnd,IDC_AVATARPATH,TranslateT("Avatar History is empty!"));
		return false;
	}
	
	ListEntry *le = (ListEntry*) SendMessage(list, LB_GETITEMDATA, cursel, 0);

	if (!le || !le->filename)
	{
		SetDlgItemText(hwnd,IDC_AVATARPATH,TranslateT("avatar path is null."));
		return 0;
	}
	SetDlgItemText(hwnd,IDC_AVATARPATH,le->filename);

	HBITMAP avpic = (HBITMAP) CallService(MS_IMG_LOAD, (WPARAM)le->filename, IMGL_TCHAR);

	bool found_image = (avpic != NULL);

	avpic = (HBITMAP)SendMessage(hwndpic, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)avpic);
	if (avpic)
		DeleteObject(avpic);

	return found_image;
}

int CleanupAvatarPic(HWND hwnd)
{
	HWND hwndpic = GetDlgItem(hwnd, IDC_AVATAR);
	if (!hwnd || !hwndpic)
		return -1;

	HBITMAP avpic = (HBITMAP)SendMessage(hwndpic, STM_GETIMAGE, 0, 0);
	if (avpic)
		DeleteObject(avpic);
	return 0;
}

int PreBuildContactMenu(WPARAM wParam,LPARAM lParam) 
{
	char *proto = GetContactProto((HANDLE)wParam);
	Menu_ShowItem(hMenu, ProtocolEnabled(proto));
	return 0;
}

void InitMenuItem()
{
	CreateServiceFunction(MS_AVATARHISTORY_SHOWDIALOG, ShowDialogSvc);

	CLISTMENUITEM mi = { sizeof(mi) };
	mi.ptszName = LPGENT("View Avatar History");
	mi.flags = CMIF_TCHAR;
	mi.position = 1000090010;
	mi.hIcon = createDefaultOverlayedIcon(FALSE);
	mi.pszService = MS_AVATARHISTORY_SHOWDIALOG;
	hMenu = Menu_AddContactMenuItem(&mi);
	DestroyIcon(mi.hIcon);
}

static INT_PTR ShowDialogSvc(WPARAM wParam, LPARAM lParam)
{
	OpenAvatarDialog((HANDLE)wParam, (char*)lParam);
	return 0;
}


int ShowSaveDialog(HWND hwnd, TCHAR* fn, HANDLE hContact)
{
	TCHAR filter[MAX_PATH];
	TCHAR file[MAX_PATH];
	OPENFILENAME ofn;
	ZeroMemory(&ofn, sizeof(OPENFILENAME));
	ofn.lStructSize = sizeof(OPENFILENAME);
	ofn.hwndOwner = hwnd;
	ofn.hInstance = hInst;

	CallService(MS_UTILS_GETBITMAPFILTERSTRINGST, MAX_PATH, (LPARAM)filter);
	ofn.lpstrFilter = filter;
	
	ofn.nFilterIndex = 1;
	lstrcpyn(file, _tcsrchr(fn, '\\')+1, sizeof(file));
	ofn.lpstrFile = file;

	TCHAR *displayName = (TCHAR*) CallService(MS_CLIST_GETCONTACTDISPLAYNAME,(WPARAM)hContact,GCDNF_TCHAR);
	if (displayName)
	{
		TCHAR title[MAX_PATH];
		mir_sntprintf(title,sizeof(title),TranslateT("Save Avatar for %s"),displayName);
		ofn.lpstrTitle = title;
	}
	else
	{
		ofn.lpstrTitle = TranslateT("Save Avatar");
	}
	ofn.nMaxFile = MAX_PATH;
	ofn.Flags = OFN_PATHMUSTEXIST | OFN_DONTADDTORECENT;
	ofn.lpstrDefExt = _tcsrchr(fn, '.')+1;

	DBVARIANT dbvInitDir = {0};
	if (!db_get_ts(hContact,MODULE_NAME,"SavedAvatarFolder",&dbvInitDir))
	{
		ofn.lpstrInitialDir = dbvInitDir.ptszVal;
		db_free(&dbvInitDir);
	}
	else
	{
		ofn.lpstrInitialDir = _T(".");
	}
	if (GetSaveFileName(&ofn))
	{
		CopyFile(fn, file, FALSE);
		db_set_ts(hContact,MODULE_NAME,"SavedAvatarFolder",file);
	}
	return 0;
}