summaryrefslogtreecommitdiff
path: root/plugins/BuddyPounce/src/dialog.cpp
blob: 024d51f8ad43382abae1bf8e1ab32c28390d95c5 (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
610
611
612
613
614
#include "stdafx.h"

void populateSettingsList(HWND hwnd2List)
{
	SendMessage(hwnd2List, LB_ADDSTRING, 0, (LPARAM)TranslateT("Send If My Status Is..."));
	SendMessage(hwnd2List, LB_ADDSTRING, 0, (LPARAM)TranslateT("Send If They Change Status to..."));
	SendMessage(hwnd2List, LB_ADDSTRING, 0, (LPARAM)L"----------------------------");
	SendMessage(hwnd2List, LB_ADDSTRING, 0, (LPARAM)TranslateT("Reuse Pounce"));
	SendMessage(hwnd2List, LB_ADDSTRING, 0, (LPARAM)TranslateT("Give Up delay"));
	SendMessage(hwnd2List, LB_ADDSTRING, 0, (LPARAM)TranslateT("Confirmation Window"));
}

void populateContacts(MCONTACT BPhContact, HWND hwnd2CB)
{
	for (MCONTACT hContact = db_find_first(); hContact; hContact = db_find_next(hContact)) {
		char *szProto = GetContactProto(hContact);
		if (szProto && (CallProtoService(szProto, PS_GETCAPS, PFLAGNUM_1, 0) & PF1_IM)) {
			wchar_t name[300];
			mir_snwprintf(name, L"%s (%s)", pcli->pfnGetContactDisplayName(hContact, 0), _A2T(szProto));
			int index = SendMessage(hwnd2CB, CB_ADDSTRING, 0, (LPARAM)name);
			SendMessage(hwnd2CB, CB_SETITEMDATA, index, hContact);
			if (BPhContact == hContact)
				SendMessage(hwnd2CB, CB_SETCURSEL, index, 0);
		}
	}
}

void saveLastSetting(MCONTACT hContact, HWND hwnd)
{
	wchar_t number[8];//, string[1024];//for sending file name
	switch (db_get_b(hContact, modname, "LastSetting", 2)) { // nothing to do
	case 0: // Send If My Status Is...
		break;
	case 1: // Send If They Change status to
		break;
	case 2: // ------, fall through
	case 6: // nothing to do...
		break;
	case 3: // Reuse Pounce
		GetDlgItemText(hwnd, IDC_SETTINGNUMBER, number, _countof(number));
		db_set_b(hContact, modname, "Reuse", (BYTE)_wtoi(number));
		break;
	case 4: // Give Up delay
		GetDlgItemText(hwnd, IDC_SETTINGNUMBER, number, _countof(number));
		db_set_b(hContact, modname, "GiveUpDays", (BYTE)_wtoi(number));
		{
			db_set_dw(hContact, modname, "GiveUpDate", (DWORD)(_wtoi(number)*SECONDSINADAY));
		}
		break;
	case 5:	// confirm window
		GetDlgItemText(hwnd, IDC_SETTINGNUMBER, number, _countof(number));
		db_set_w(hContact, modname, "ConfirmTimeout", (WORD)_wtoi(number));
		break;
	}
}

void hideAll(HWND hwnd)
{
	ShowWindow(GetDlgItem(hwnd, IDC_SETTINGMSG), SW_HIDE);
	ShowWindow(GetDlgItem(hwnd, IDC_SETTINGMSG2), SW_HIDE);
	ShowWindow(GetDlgItem(hwnd, IDC_SETTINGNUMBER), SW_HIDE);
	ShowWindow(GetDlgItem(hwnd, IDC_SPIN), SW_HIDE);
}

void getDefaultMessage(HWND hwnd, UINT control, MCONTACT hContact)
{
	DBVARIANT dbv;
	if (!db_get_ws(hContact, modname, "PounceMsg", &dbv)) {
		SetDlgItemText(hwnd, control, dbv.ptszVal);
		db_free(&dbv);
	}
	else if (!db_get_ws(NULL, modname, "PounceMsg", &dbv)) {
		SetDlgItemText(hwnd, control, dbv.ptszVal);
		db_free(&dbv);
	}
}

INT_PTR CALLBACK StatusModesDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg) {
	case WM_INITDIALOG:
		SetWindowLongPtr(hwnd, GWLP_USERDATA, lParam); // windowInfo*
		TranslateDialogDefault(hwnd);

		return FALSE;
	case WM_COMMAND:
		switch(LOWORD(wParam)) {
		case IDOK:
		case IDCANCEL:
			windowInfo *wi = (windowInfo *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
			wchar_t type[32];
			GetDlgItemText(hwnd, IDC_CHECK1, type, _countof(type));

			WORD flag = (IsDlgButtonChecked(hwnd, IDC_CHECK1))
				|(IsDlgButtonChecked(hwnd, IDC_CHECK2)<<1)
				|(IsDlgButtonChecked(hwnd, IDC_CHECK3)<<2)
				|(IsDlgButtonChecked(hwnd, IDC_CHECK4)<<3)
				|(IsDlgButtonChecked(hwnd, IDC_CHECK5)<<4)
				|(IsDlgButtonChecked(hwnd, IDC_CHECK6)<<5)
				|(IsDlgButtonChecked(hwnd, IDC_CHECK7)<<6)
				|(IsDlgButtonChecked(hwnd, IDC_CHECK8)<<7)
				|(IsDlgButtonChecked(hwnd, IDC_CHECK9)<<8)
				|(IsDlgButtonChecked(hwnd, IDC_CHECK10)<<9);

			if (!mir_wstrcmp(type, L"Any")) {
				if (LOWORD(wParam) == IDOK)
					db_set_w(wi->hContact, modname, "SendIfMyStatusIsFLAG", flag);
				wi->SendIfMy = nullptr;
			}
			else {
				if (LOWORD(wParam) == IDOK)
					db_set_w(wi->hContact, modname, "SendIfTheirStatusIsFLAG", flag);
				wi->SendWhenThey = nullptr;
			}				
			DestroyWindow(hwnd);
		}
		break;
	}
	return FALSE;
}
void statusModes(windowInfo *wi, int myStatusMode) // myStatusMode=1 sendIfMyStatusFlag
{
	int statusFlag;
	HWND hwnd;

	if (myStatusMode) {
		if (wi->SendIfMy) {
			SetForegroundWindow(wi->SendIfMy);
			return;
		}
		else {
			hwnd = wi->SendIfMy = CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_STATUSMODES), nullptr, StatusModesDlgProc, (LPARAM)wi);
			statusFlag = db_get_w(wi->hContact, modname, "SendIfMyStatusIsFLAG", 0);
			SetWindowText(hwnd, TranslateT("Send If My Status Is"));
			SetDlgItemText(hwnd, IDC_CHECK1, TranslateT("Any"));
			SetDlgItemText(hwnd, IDC_CHECK2, TranslateT("Online"));
			SetDlgItemText(hwnd, IDC_CHECK3, TranslateT("Away"));
			SetDlgItemText(hwnd, IDC_CHECK4, TranslateT("Not available"));
			SetDlgItemText(hwnd, IDC_CHECK5, TranslateT("Occupied"));
			SetDlgItemText(hwnd, IDC_CHECK6, TranslateT("Do not disturb"));
			SetDlgItemText(hwnd, IDC_CHECK7, TranslateT("Free for chat"));
			SetDlgItemText(hwnd, IDC_CHECK8, TranslateT("Invisible"));
			SetDlgItemText(hwnd, IDC_CHECK9, TranslateT("On the phone"));
			SetDlgItemText(hwnd, IDC_CHECK10, TranslateT("Out to lunch"));
		}
	}
	else {
		if (wi->SendWhenThey) {
			SetForegroundWindow(wi->SendWhenThey);
			return;
		}
		else {
			hwnd = wi->SendWhenThey = CreateDialogParam(hInst,MAKEINTRESOURCE(IDD_STATUSMODES),nullptr,StatusModesDlgProc, (LPARAM)wi);
			statusFlag = db_get_w(wi->hContact, modname, "SendIfTheirStatusIsFLAG", 0);
			SetWindowText(hwnd, TranslateT("Send If Their Status changes"));
			SetDlgItemText(hwnd, IDC_CHECK1, TranslateT("From Offline"));
			SetDlgItemText(hwnd, IDC_CHECK2, TranslateT("To Online"));
			SetDlgItemText(hwnd, IDC_CHECK3, TranslateT("To Away"));
			SetDlgItemText(hwnd, IDC_CHECK4, TranslateT("To Not available"));
			SetDlgItemText(hwnd, IDC_CHECK5, TranslateT("To Occupied"));
			SetDlgItemText(hwnd, IDC_CHECK6, TranslateT("To Do not disturb"));
			SetDlgItemText(hwnd, IDC_CHECK7, TranslateT("To Free for chat"));
			SetDlgItemText(hwnd, IDC_CHECK8, TranslateT("To Invisible"));
			SetDlgItemText(hwnd, IDC_CHECK9, TranslateT("To On the phone"));
			SetDlgItemText(hwnd, IDC_CHECK10, TranslateT("To Out to lunch"));
		}
	}
	CheckDlgButton(hwnd, IDC_CHECK1, statusFlag&ANY ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(hwnd, IDC_CHECK2, (statusFlag&ONLINE)>>1 ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(hwnd, IDC_CHECK3, (statusFlag&AWAY)>>2 ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(hwnd, IDC_CHECK4, (statusFlag&NA)>>3 ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(hwnd, IDC_CHECK5, (statusFlag&OCCUPIED)>>4 ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(hwnd, IDC_CHECK6, (statusFlag&DND)>>5 ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(hwnd, IDC_CHECK7, (statusFlag&FFC)>>6 ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(hwnd, IDC_CHECK8, (statusFlag&INVISIBLE)>>7 ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(hwnd, IDC_CHECK9, (statusFlag&PHONE)>>8 ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(hwnd, IDC_CHECK10, (statusFlag&LUNCH)>>9 ? BST_CHECKED : BST_UNCHECKED);
}

void deletePounce(MCONTACT hContact)
{
	db_unset(hContact,modname, "PounceMsg");
	db_unset(hContact,modname, "SendIfMyStatusIsFLAG");
	db_unset(hContact,modname, "SendIfTheirStatusIsFLAG");
	db_unset(hContact,modname, "Reuse");
	db_unset(hContact, modname, "GiveUpDays");
	db_unset(hContact, modname, "GiveUpDate");
	db_unset(hContact, modname, "ConfirmTimeout");
	db_unset(hContact, modname, "FileToSend");
}

INT_PTR CALLBACK BuddyPounceSimpleDlgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	MCONTACT hContact = (MCONTACT)GetWindowLongPtr(hwnd, GWLP_USERDATA);
	wchar_t msg[1024];

	switch(uMsg) {
	case WM_INITDIALOG:
		TranslateDialogDefault(hwnd);
		hContact = lParam;
		SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)lParam);

		getDefaultMessage(hwnd, IDC_MESSAGE, hContact);
		mir_snwprintf(msg, TranslateT("The Message    (%d Characters)"), GetWindowTextLength(GetDlgItem(hwnd, IDC_MESSAGE)));
		SetDlgItemText(hwnd, GRP_MSG, msg);	
		return FALSE;

	case WM_COMMAND:
		switch(LOWORD(wParam)) {
		case IDC_MESSAGE:
			if (HIWORD(wParam) == EN_CHANGE) {
				int length = GetWindowTextLength(GetDlgItem(hwnd, IDC_MESSAGE));
				mir_snwprintf(msg, TranslateT("The Message    (%d Characters)"), length);
				SetDlgItemText(hwnd, GRP_MSG, msg);
			}
			break;

		case IDC_ADVANCED:
			// fall through

		case IDOK:
			{
				int length = GetWindowTextLength(GetDlgItem(hwnd, IDC_MESSAGE)) + 1;
				if (length>1) {
					wchar_t *text = (wchar_t*)_alloca(length*sizeof(wchar_t));
					GetDlgItemText(hwnd, IDC_MESSAGE, text, length);
					db_set_ws(hContact, modname, "PounceMsg", text);
				}
				else db_unset(hContact, modname, "PounceMsg");
			}
			// fall through
		case IDCANCEL:
			if (LOWORD(wParam) == IDC_ADVANCED)
				CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_POUNCE), nullptr, BuddyPounceDlgProc, GetWindowLongPtr(hwnd, GWLP_USERDATA));
			DestroyWindow(hwnd);
		}
		break;
	}
	return FALSE;
}

INT_PTR CALLBACK BuddyPounceDlgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	windowInfo *wi = (windowInfo *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
	wchar_t msg[1024];

	switch(uMsg) {
	case WM_INITDIALOG:
		TranslateDialogDefault(hwnd);
		wi = (windowInfo *)mir_alloc(sizeof(windowInfo));
		if (!wi) {
			msg(TranslateT("error......"), TranslateT("Buddy Pounce"));
			DestroyWindow(hwnd);
		}
		wi->hContact = lParam;
		wi->SendIfMy = nullptr;
		wi->SendWhenThey = nullptr;
		SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)wi);
		getDefaultMessage(hwnd, IDC_MESSAGE, wi->hContact);

		mir_snwprintf(msg, TranslateT("The Message    (%d Characters)"), GetWindowTextLength(GetDlgItem(hwnd, IDC_MESSAGE)));
		SetDlgItemText(hwnd, GRP_MSG, msg);	

		populateSettingsList(GetDlgItem(hwnd, IDC_SETTINGS));
		populateContacts(wi->hContact, GetDlgItem(hwnd, IDC_CONTACTS));
		SendDlgItemMessage(hwnd, IDC_SPIN, UDM_SETRANGE, 0, (LPARAM)MAKELONG((short)1024, (short)0));
		db_set_b(wi->hContact, modname, "LastSetting", 0);
		return FALSE;

	case WM_COMMAND:
		switch(LOWORD(wParam)) {
		case IDC_MESSAGE:
			if (HIWORD(wParam) == EN_CHANGE) {
				int length = GetWindowTextLength(GetDlgItem(hwnd, IDC_MESSAGE));
				mir_snwprintf(msg, TranslateT("The Message    (%d Characters)"), length);
				SetDlgItemText(hwnd, GRP_MSG, msg);
			}
			break;

		case IDC_SIMPLE:
		case IDOK:
			{
				MCONTACT hContact = (MCONTACT)SendDlgItemMessage(hwnd, IDC_CONTACTS, CB_GETITEMDATA, SendDlgItemMessage(hwnd, IDC_CONTACTS, CB_GETCURSEL, 0, 0), 0);
				int length = GetWindowTextLength(GetDlgItem(hwnd, IDC_MESSAGE))+1;
				if (length>1) {
					wchar_t *text = (wchar_t*)mir_alloc(length*sizeof(wchar_t));
					if (!text) {
						msg(TranslateT("Couldn't allocate enough memory"), L"");
						break;
					}
					GetDlgItemText(hwnd, IDC_MESSAGE, text, length);
					db_set_ws(hContact, modname, "PounceMsg", text);
					mir_free(text);
				}
				else db_unset(hContact, modname, "PounceMsg");
				saveLastSetting(hContact, hwnd);
			} // fall through
			if (LOWORD(wParam) == IDC_SIMPLE)
				CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_POUNCE_SIMPLE), nullptr, BuddyPounceSimpleDlgProc, (LPARAM)((windowInfo *)GetWindowLongPtr(hwnd, GWLP_USERDATA))->hContact);
			// fall through

		case IDCANCEL:
			if (wi->SendIfMy) DestroyWindow(wi->SendIfMy);
			if (wi->SendWhenThey) DestroyWindow(wi->SendWhenThey);
			mir_free(wi);
			DestroyWindow(hwnd);
			break;

		case IDC_DELETE:
			deletePounce(wi->hContact);
			SetDlgItemText(hwnd, IDC_MESSAGE, L"");
			SetDlgItemText(hwnd, GRP_MSG, TranslateT("The Message    (0 Characters)"));
			break;

		case IDC_DEFAULT:
			getDefaultMessage(hwnd, IDC_MESSAGE, wi->hContact);

			mir_snwprintf(msg, TranslateT("The Message    (%d Characters)"), GetWindowTextLength(GetDlgItem(hwnd, IDC_MESSAGE)));
			SetDlgItemText(hwnd, GRP_MSG, msg);

			db_set_w(wi->hContact, modname, "SendIfMyStatusIsFLAG", (WORD)db_get_w(NULL, modname, "SendIfMyStatusIsFLAG",0));
			db_set_w(wi->hContact, modname, "SendIfTheirStatusIsFLAG", (WORD)db_get_w(NULL, modname, "SendIfTheirStatusIsFLAG",0));
			db_set_b(wi->hContact, modname, "Reuse",(BYTE)db_get_b(NULL, modname, "Reuse",0));
			db_set_b(wi->hContact, modname, "GiveUpDays", (BYTE)db_get_b(NULL, modname, "GiveUpDays",0));
			db_set_dw(wi->hContact, modname, "GiveUpDate", (DWORD)db_get_dw(NULL, modname, "GiveUpDate",0));
			db_set_w(wi->hContact, modname, "ConfirmTimeout", (WORD)db_get_w(NULL, modname, "ConfirmTimeout",0));
			break;

		case IDC_SETTINGS:
			if (HIWORD(wParam) == LBN_SELCHANGE) {	
				int item = SendDlgItemMessage(hwnd, IDC_SETTINGS, LB_GETCURSEL, 0, 0);
				wchar_t temp[5];
				saveLastSetting(wi->hContact, hwnd);
				hideAll(hwnd);
				switch (item) {
				case 0: // Send If My Status Is...
					statusModes(wi, 1);
					break;
				case 1: // Send If They Change status to
					statusModes(wi, 0);
					break;
				case 3: // Reuse Pounce
					ShowWindow(GetDlgItem(hwnd, IDC_SETTINGMSG), SW_SHOW);
					SetDlgItemText(hwnd, IDC_SETTINGMSG, TranslateT("Reuse this message? (0 to use it once)"));
					ShowWindow(GetDlgItem(hwnd, IDC_SETTINGMSG2), SW_SHOW);
					SetDlgItemText(hwnd, IDC_SETTINGMSG2, TranslateT("Times"));
					SetDlgItemText(hwnd, IDC_SETTINGNUMBER, _itow(db_get_b(wi->hContact, modname, "Reuse", 0), temp, 10));
					ShowWindow(GetDlgItem(hwnd, IDC_SETTINGNUMBER), SW_SHOW);
					ShowWindow(GetDlgItem(hwnd, IDC_SPIN), SW_SHOW);
					break;
				case 4: // Give Up delay
					ShowWindow(GetDlgItem(hwnd, IDC_SETTINGMSG), SW_SHOW);
					SetDlgItemText(hwnd, IDC_SETTINGMSG, TranslateT("Give up after... (0 to not give up)"));
					ShowWindow(GetDlgItem(hwnd, IDC_SETTINGMSG2), SW_SHOW);
					SetDlgItemText(hwnd, IDC_SETTINGMSG2, TranslateT("Days"));
					SetDlgItemText(hwnd, IDC_SETTINGNUMBER, _itow(db_get_b(wi->hContact, modname, "GiveUpDays", 0), temp, 10));
					ShowWindow(GetDlgItem(hwnd, IDC_SETTINGNUMBER), SW_SHOW);
					ShowWindow(GetDlgItem(hwnd, IDC_SPIN), SW_SHOW);
					break;
				case 5:	// confirm window
					ShowWindow(GetDlgItem(hwnd, IDC_SETTINGMSG), SW_SHOW);
					SetDlgItemText(hwnd, IDC_SETTINGMSG, TranslateT("Show confirmation window? (0 to not Show)"));
					ShowWindow(GetDlgItem(hwnd, IDC_SETTINGMSG2), SW_SHOW);
					SetDlgItemText(hwnd, IDC_SETTINGMSG2, TranslateT("Seconds to wait before sending"));
					SetDlgItemText(hwnd, IDC_SETTINGNUMBER, _itow(db_get_w(wi->hContact, modname, "ConfirmTimeout", 0), temp, 10));
					ShowWindow(GetDlgItem(hwnd, IDC_SETTINGNUMBER), SW_SHOW);
					ShowWindow(GetDlgItem(hwnd, IDC_SPIN), SW_SHOW);
					break;
				}
				db_set_b(wi->hContact, modname, "LastSetting", (BYTE)item);
			}
		}
		break;
	}
	return FALSE;
}

INT_PTR CALLBACK BuddyPounceOptionsDlgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	wchar_t msg[1024];

	switch(uMsg) {
	case WM_INITDIALOG:
		TranslateDialogDefault(hwnd);
		{
			windowInfo *wi = (windowInfo *)mir_alloc(sizeof(windowInfo));
			wi->hContact = 0;
			wi->SendIfMy = nullptr;
			wi->SendWhenThey = nullptr;
			SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)wi);

			getDefaultMessage(hwnd, IDC_MESSAGE, wi->hContact);
			mir_snwprintf(msg, TranslateT("The Message    (%d Characters)"), GetWindowTextLength(GetDlgItem(hwnd, IDC_MESSAGE)));
			SetDlgItemText(hwnd, GRP_MSG, msg);	
			populateSettingsList(GetDlgItem(hwnd, IDC_SETTINGS));
			SendDlgItemMessage(hwnd, IDC_SPIN, UDM_SETRANGE, 0, (LPARAM)MAKELONG((short)1024, (short)0));
			CheckDlgButton(hwnd, IDC_USEADVANCED, db_get_b(NULL, modname, "UseAdvanced", 0) ? BST_CHECKED : BST_UNCHECKED);
			CheckDlgButton(hwnd, IDC_SHOWDELIVERYMSGS, db_get_b(NULL, modname, "ShowDeliveryMessages", 1) ? BST_CHECKED : BST_UNCHECKED);
			db_set_b(wi->hContact, modname, "LastSetting", 0);
		}
		return FALSE;

	case WM_NOTIFY:
		switch(((LPNMHDR)lParam)->idFrom) {
		case 0:
			switch (((LPNMHDR)lParam)->code) {
			case PSN_APPLY:
				windowInfo *wi = (windowInfo *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
				MCONTACT hContact = ((windowInfo *)GetWindowLongPtr(hwnd, GWLP_USERDATA))->hContact;
				int length = GetWindowTextLength(GetDlgItem(hwnd, IDC_MESSAGE))+1;
				if (length > 1) {
					wchar_t *text = (wchar_t*)mir_alloc(length*sizeof(wchar_t));
					if (!text) {
						msg(TranslateT("Couldn't allocate enough memory"), L"");
						break;
					}
					GetDlgItemText(hwnd, IDC_MESSAGE, text, length);
					db_set_ws(hContact, modname, "PounceMsg", text);
					mir_free(text);
				}
				else db_unset(hContact, modname, "PounceMsg");
				db_set_b(NULL, modname, "UseAdvanced", (BYTE)IsDlgButtonChecked(hwnd, IDC_USEADVANCED));
				db_set_b(NULL, modname, "ShowDeliveryMessages", (BYTE)IsDlgButtonChecked(hwnd, IDC_SHOWDELIVERYMSGS));

				if (wi->SendIfMy) DestroyWindow(wi->SendIfMy);
				if (wi->SendWhenThey) DestroyWindow(wi->SendWhenThey);
			}
		}
		break;

	case WM_COMMAND:
		switch(LOWORD(wParam)) {
		case IDC_MESSAGE:
			if (HIWORD(wParam) == EN_CHANGE) {
				int length = GetWindowTextLength(GetDlgItem(hwnd, IDC_MESSAGE));
				mir_snwprintf(msg, TranslateT("The Message    (%d Characters)"), length);
				SetDlgItemText(hwnd, GRP_MSG, msg);
				SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0);
			}
			break;

		case IDC_SHOWDELIVERYMSGS:
		case IDC_USEADVANCED:
			SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0);
			break;

		case IDC_SETTINGS:
			if (HIWORD(wParam) == LBN_SELCHANGE) {	
				windowInfo *wi = (windowInfo *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
				int item = SendDlgItemMessage(hwnd, IDC_SETTINGS, LB_GETCURSEL, 0, 0);
				wchar_t temp[5];
				SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0);
				saveLastSetting(wi->hContact, hwnd);
				hideAll(hwnd);
				switch (item) {
				case 0: // Send If My Status Is...
					statusModes(wi, 1);
					break;
				case 1: // Send If They Change status to
					statusModes(wi, 0);
					break;
				case 3: // Reuse Pounce
					ShowWindow(GetDlgItem(hwnd, IDC_SETTINGMSG), SW_SHOW);
					SetDlgItemText(hwnd, IDC_SETTINGMSG, TranslateT("Reuse this message? (0 to use it once)"));
					ShowWindow(GetDlgItem(hwnd, IDC_SETTINGMSG2), SW_SHOW);
					SetDlgItemText(hwnd, IDC_SETTINGMSG2, TranslateT("Times"));
					SetDlgItemText(hwnd, IDC_SETTINGNUMBER, _itow(db_get_b(wi->hContact, modname, "Reuse", 0), temp, 10));
					ShowWindow(GetDlgItem(hwnd, IDC_SETTINGNUMBER), SW_SHOW);
					ShowWindow(GetDlgItem(hwnd, IDC_SPIN), SW_SHOW);
					break;
				case 4: // Give Up delay
					ShowWindow(GetDlgItem(hwnd, IDC_SETTINGMSG), SW_SHOW);
					SetDlgItemText(hwnd, IDC_SETTINGMSG, TranslateT("Give up after... (0 to not give up)"));
					ShowWindow(GetDlgItem(hwnd, IDC_SETTINGMSG2), SW_SHOW);
					SetDlgItemText(hwnd, IDC_SETTINGMSG2, TranslateT("Days"));
					SetDlgItemText(hwnd, IDC_SETTINGNUMBER, _itow(db_get_b(wi->hContact, modname, "GiveUpDays", 0), temp, 10));
					ShowWindow(GetDlgItem(hwnd, IDC_SETTINGNUMBER), SW_SHOW);
					ShowWindow(GetDlgItem(hwnd, IDC_SPIN), SW_SHOW);
					break;
				case 5:	// confirm window
					ShowWindow(GetDlgItem(hwnd, IDC_SETTINGMSG), SW_SHOW);
					SetDlgItemText(hwnd, IDC_SETTINGMSG, TranslateT("Show confirmation window? (0 to not Show)"));
					ShowWindow(GetDlgItem(hwnd, IDC_SETTINGMSG2), SW_SHOW);
					SetDlgItemText(hwnd, IDC_SETTINGMSG2, TranslateT("Seconds to wait before sending"));
					SetDlgItemText(hwnd, IDC_SETTINGNUMBER, _itow(db_get_w(wi->hContact, modname, "ConfirmTimeout", 0), temp, 10));
					ShowWindow(GetDlgItem(hwnd, IDC_SETTINGNUMBER), SW_SHOW);
					ShowWindow(GetDlgItem(hwnd, IDC_SPIN), SW_SHOW);
					break;
				}
				db_set_b(wi->hContact, modname, "LastSetting", (BYTE)item);
			}
		}
		break;
	}
	return FALSE;
}

INT_PTR CALLBACK SendPounceDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	SendPounceDlgProcStruct *spdps = (SendPounceDlgProcStruct *)GetWindowLongPtr(hwnd, GWLP_USERDATA);

	switch(msg) {
	case WM_INITDIALOG:
		TranslateDialogDefault(hwnd);
		spdps = (SendPounceDlgProcStruct*)lParam;
		if (!spdps)
			DestroyWindow(hwnd);

		spdps->timer = db_get_w(spdps->hContact, modname, "ConfirmTimeout", 0);
		SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)spdps);
		{
			DBVARIANT dbv;
			if (db_get_ws(spdps->hContact, modname, "PounceMsg", &dbv))
				DestroyWindow(hwnd);
			else {
				SetDlgItemText(hwnd,IDC_MESSAGE, dbv.ptszVal);
				db_free(&dbv);
			}
		}
		SetTimer(hwnd,1,1000,nullptr);
		SendMessage(hwnd,WM_TIMER,0,0);
		break;

	case WM_TIMER:
		{
			wchar_t message[1024];
			mir_snwprintf(message, TranslateT("Pounce being sent to %s in %d seconds"), pcli->pfnGetContactDisplayName(spdps->hContact, 0), spdps->timer);
			SetDlgItemText(hwnd, LBL_CONTACT, message);
		}
		spdps->timer--;
		if (spdps->timer < 0) {
			KillTimer(hwnd,1);
			SendPounce(spdps->message, spdps->hContact);
			DestroyWindow(hwnd);
		}
		break;

	case WM_COMMAND:
		switch(LOWORD(wParam)) {
		case IDOK:
			KillTimer(hwnd,1);
			SendPounce(spdps->message, spdps->hContact);
			// fall through
		case IDCANCEL:
			KillTimer(hwnd,1);
			DestroyWindow(hwnd);
			break;
		}
		break;

	case WM_DESTROY:
		mir_free(spdps->message);
		mir_free(spdps);
		break;
	}
	return 0;
}

INT_PTR CALLBACK PounceSentDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	MCONTACT hContact = (MCONTACT)GetWindowLongPtr(hwnd, GWLP_USERDATA);

	switch(msg) {
	case WM_INITDIALOG:
		SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)lParam);
		TranslateDialogDefault(hwnd);
		hContact = lParam;
		{
			DBVARIANT dbv;
			if (db_get_ws(hContact, modname, "PounceMsg", &dbv))
				DestroyWindow(hwnd);
			else {
				SetDlgItemText(hwnd, IDC_MESSAGE, dbv.ptszVal);
				db_free(&dbv);
			}
		}
		break;

	case WM_COMMAND:
		switch(LOWORD(wParam)) {
		case IDOK:
			{
				wchar_t text[2048];
				GetDlgItemText(hwnd, IDOK, text, _countof(text));
				if (!mir_wstrcmp(text, TranslateT("Retry"))) {
					GetDlgItemText(hwnd, IDC_MESSAGE, text, _countof(text));
					SendPounce(text, hContact);
				}
			}
			// fall through
		case IDCANCEL:
			DestroyWindow(hwnd);
		}
		break;
	}
	return 0;
}

void CreateMessageAcknowlegedWindow(MCONTACT hContact, int SentSuccess)
{
	HWND hwnd = CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_CONFIRMSEND), nullptr, PounceSentDlgProc, hContact);
	wchar_t msg[256];
	if (SentSuccess) {
		mir_snwprintf(msg, TranslateT("Message successfully sent to %s"), pcli->pfnGetContactDisplayName(hContact, 0));
		SetDlgItemText(hwnd, IDOK, TranslateT("OK"));
		ShowWindow(GetDlgItem(hwnd, IDCANCEL), 0);
	}
	else {
		mir_snwprintf(msg, TranslateT("Message failed to send to %s"), pcli->pfnGetContactDisplayName(hContact, 0));
		SetDlgItemText(hwnd, IDOK, TranslateT("Retry"));
	}
	SetDlgItemText(hwnd, LBL_CONTACT, msg);
	SetWindowText(hwnd, TranslateT(modFullname));
}