summaryrefslogtreecommitdiff
path: root/plugins/Utils/mir_options.cpp
blob: 8062c1180e60ed3ee3456727b71cbbdf39492cbe (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
/*
Copyright (C) 2005-2009 Ricardo Pescuma Domenecci

This is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This 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
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this file; see the file license.txt.  If
not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  
*/

#define _CRT_SECURE_NO_WARNINGS

#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include <tchar.h>

#define MIRANDA_VER 0x0A00
#include <newpluginapi.h>
#include <m_database.h>
#include <m_utils.h>
#include <m_langpack.h>
#include <m_protocols.h>
#include <m_protosvc.h>
#include <m_system.h>

#include "mir_options.h"
#include "mir_memory.h"

#define SIZEOF(_A_) ( sizeof(_A_) / sizeof(_A_[0]))

static TCHAR* MyDBGetContactSettingTString(HANDLE hContact, char* module, char* setting, TCHAR* out, size_t len, TCHAR *def)
{
	DBVARIANT dbv = {0};

	out[0] = _T('\0');

	if ( !DBGetContactSettingTString(hContact, module, setting, &dbv))
	{
		lstrcpyn(out, dbv.ptszVal, (int)len);
		DBFreeVariant(&dbv);
	}
	else
	{
		if (def != NULL)
			lstrcpyn(out, def, (int)len);
	}

	return out;
}


static TCHAR dbPath[MAX_PATH] = {0};		// database profile path (read at startup only)


static int PathIsAbsolute(const TCHAR *path)
{
	if ( !path || !(lstrlen(path) > 2))
		return 0;
	if ((path[1]==_T(':') && path[2]==_T('\\')) || (path[0]==_T('\\')&&path[1]==_T('\\'))) 
		return 1;
	return 0;
}

static void PathToRelative(TCHAR *pOut, size_t outSize, const TCHAR *pSrc)
{
	if ( !PathIsAbsolute(pSrc)) 
		lstrcpyn(pOut, pSrc, (int)outSize);
	else {
		if (dbPath[0] == _T('\0')) {
			char tmp[1024];
			CallService(MS_DB_GETPROFILEPATH, SIZEOF(tmp), (LPARAM) tmp);
			mir_sntprintf(dbPath, SIZEOF(dbPath), _T(TCHAR_STR_PARAM) _T("\\"), tmp);
		}

		size_t len = lstrlen(dbPath);
		if (_tcsnicmp(pSrc, dbPath, len)) 
			mir_sntprintf(pOut, outSize, _T("%s"), pSrc + len);
		else 
			lstrcpyn(pOut, pSrc, (int)outSize);
	}
}

static void PathToAbsolute(TCHAR *pOut, size_t outSize, const TCHAR *pSrc)
{
	if (PathIsAbsolute(pSrc) || !isalnum(pSrc[0])) 
		lstrcpyn(pOut, pSrc, (int)outSize);
	else {
		if (dbPath[0] == _T('\0'))
		{
			char tmp[1024];
			CallService(MS_DB_GETPROFILEPATH, SIZEOF(tmp), (LPARAM) tmp);
			mir_sntprintf(dbPath, SIZEOF(dbPath), _T(TCHAR_STR_PARAM) _T("\\"), tmp);
		}

		mir_sntprintf(pOut, outSize, _T("%s%s"), dbPath, pSrc);
	}
}

static void LoadOpt(OptPageControl *ctrl, char *module)
{
	if (ctrl->var == NULL)
		return;
	
	switch(ctrl->type) {
	case CONTROL_CHECKBOX:
		*((BYTE *) ctrl->var) = db_get_b(NULL, module, ctrl->setting, ctrl->dwDefValue);
		break;

	case CONTROL_SPIN:
		*((WORD *) ctrl->var) = DBGetContactSettingWord(NULL, module, ctrl->setting, ctrl->dwDefValue);
		break;

	case CONTROL_COLOR:
		*((COLORREF *) ctrl->var) = (COLORREF) DBGetContactSettingDword(NULL, module, ctrl->setting, ctrl->dwDefValue);
		break;

	case CONTROL_RADIO:
		*((WORD *) ctrl->var) = DBGetContactSettingWord(NULL, module, ctrl->setting, ctrl->dwDefValue);
		break;

	case CONTROL_COMBO:
		*((WORD *) ctrl->var) = DBGetContactSettingWord(NULL, module, ctrl->setting, ctrl->dwDefValue);
		break;

	case CONTROL_PROTOCOL_LIST:
		break;

	case CONTROL_TEXT:
		MyDBGetContactSettingTString(NULL, module, ctrl->setting, ((TCHAR *) ctrl->var), min(ctrl->max <= 0 ? 1024 : ctrl->max, 1024), ctrl->tszDefValue == NULL ? NULL : TranslateTS(ctrl->tszDefValue));
		break;

	case CONTROL_PASSWORD:
		{
			char tmp[1024];
			tmp[0]=0;

			DBVARIANT dbv = {0};
			if ( !DBGetContactSettingString(NULL, module, ctrl->setting, &dbv))
			{
				lstrcpynA(tmp, dbv.pszVal, SIZEOF(tmp));
				DBFreeVariant(&dbv);
			}

			if (tmp[0] != 0)
				CallService(MS_DB_CRYPT_DECODESTRING, SIZEOF(tmp), (LPARAM) tmp);
			else if (ctrl->szDefValue != NULL)
				lstrcpynA(tmp, ctrl->szDefValue, SIZEOF(tmp));

			char *var = (char *) ctrl->var;
			int size = min(ctrl->max <= 0 ? 1024 : ctrl->max, 1024);
			lstrcpynA(var, tmp, size);
		}
		break;

	case CONTROL_INT:
		*((int *) ctrl->var) = (int) DBGetContactSettingDword(NULL, module, ctrl->setting, ctrl->dwDefValue);
		break;

	case CONTROL_FILE:
		{
			TCHAR tmp[1024];
			MyDBGetContactSettingTString(NULL, module, ctrl->setting, tmp, 1024, ctrl->tszDefValue == NULL ? NULL : ctrl->tszDefValue);
			PathToAbsolute(((TCHAR *) ctrl->var), min(ctrl->max <= 0 ? 1024 : ctrl->max, 1024), tmp);
		}
		break;

	case CONTROL_COMBO_TEXT:
	case CONTROL_COMBO_ITEMDATA:
		MyDBGetContactSettingTString(NULL, module, ctrl->setting, ((TCHAR *) ctrl->var), min(ctrl->max <= 0 ? 1024 : ctrl->max, 1024), ctrl->tszDefValue == NULL ? NULL : TranslateTS(ctrl->tszDefValue));
		break;
	}
}

void LoadOpts(OptPageControl *controls, int controlsSize, char *module)
{
	for (int i = 0 ; i < controlsSize ; i++)
		LoadOpt(&controls[i], module);
}

INT_PTR CALLBACK SaveOptsDlgProc(OptPageControl *controls, int controlsSize, char *module, HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg) {
	case WM_INITDIALOG:
		TranslateDialogDefault(hwndDlg);
		{
			for (int i = 0 ; i < controlsSize ; i++) {
				OptPageControl *ctrl = &controls[i];

				if (GetDlgItem(hwndDlg, ctrl->nID) == NULL)
					continue;

				switch(ctrl->type) {
				case CONTROL_CHECKBOX:
					CheckDlgButton(hwndDlg, ctrl->nID, db_get_b(NULL, module, ctrl->setting, ctrl->dwDefValue) == 1 ? BST_CHECKED : BST_UNCHECKED);
					break;

				case CONTROL_SPIN:
					SendDlgItemMessage(hwndDlg, ctrl->nIDSpin, UDM_SETBUDDY, (WPARAM)GetDlgItem(hwndDlg, ctrl->nID),0);
					SendDlgItemMessage(hwndDlg, ctrl->nIDSpin, UDM_SETRANGE, 0, MAKELONG(ctrl->max, ctrl->min));
					SendDlgItemMessage(hwndDlg, ctrl->nIDSpin, UDM_SETPOS,0, MAKELONG(DBGetContactSettingWord(NULL, module, ctrl->setting, ctrl->dwDefValue), 0));
					break;

				case CONTROL_COLOR:
					SendDlgItemMessage(hwndDlg, ctrl->nID, CPM_SETCOLOUR, 0, (COLORREF) DBGetContactSettingDword(NULL, module, ctrl->setting, ctrl->dwDefValue));
					break;

				case CONTROL_RADIO:
					CheckDlgButton(hwndDlg, ctrl->nID, DBGetContactSettingWord(NULL, module, ctrl->setting, ctrl->dwDefValue) == ctrl->value ? BST_CHECKED : BST_UNCHECKED);
					break;

				case CONTROL_COMBO:
					SendDlgItemMessage(hwndDlg, ctrl->nID, CB_SETCURSEL, DBGetContactSettingWord(NULL, module, ctrl->setting, ctrl->dwDefValue), 0);
					break;

				case CONTROL_PROTOCOL_LIST:
					{
						// Fill list view
						HWND hwndProtocols = GetDlgItem(hwndDlg, ctrl->nID);
						LVCOLUMN lvc;
						LVITEM lvi;

						ListView_SetExtendedListViewStyle(hwndProtocols, LVS_EX_CHECKBOXES);

						ZeroMemory(&lvc, sizeof(lvc));
						lvc.mask = LVCF_FMT;
						lvc.fmt = LVCFMT_IMAGE | LVCFMT_LEFT;
						ListView_InsertColumn(hwndProtocols, 0, &lvc);

						ZeroMemory(&lvi, sizeof(lvi));
						lvi.mask = LVIF_TEXT | LVIF_PARAM;
						lvi.iSubItem = 0;
						lvi.iItem = 1000;

						PROTOACCOUNT **protos;
						int count;

						BOOL hasAccounts = ServiceExists(MS_PROTO_ENUMACCOUNTS);
						if (hasAccounts)
							CallService(MS_PROTO_ENUMACCOUNTS, (WPARAM)&count, (LPARAM)&protos);
						else
							CallService(MS_PROTO_ENUMPROTOCOLS, (WPARAM)&count, (LPARAM)&protos);

						for (int i = 0; i < count; i++) {
							if (protos[i]->type != PROTOTYPE_PROTOCOL)
								continue;

							if (protos[i]->szModuleName == NULL || protos[i]->szModuleName[0] == '\0')
								continue;

							if (ctrl->allowProtocol != NULL && !ctrl->allowProtocol(protos[i]->szModuleName))
								continue;

							TCHAR *name;
							if (hasAccounts)
								name = mir_tstrdup(protos[i]->tszAccountName);
							else {
								char szName[128];
								CallProtoService(protos[i]->szModuleName, PS_GETNAME, sizeof(szName), (LPARAM)szName);
								name = mir_a2t(szName);
							}

							char *setting = (char *) mir_alloc(128 * sizeof(char));
							mir_snprintf(setting, 128, ctrl->setting, protos[i]->szModuleName);

							BOOL show = (BOOL)db_get_b(NULL, module, setting, ctrl->dwDefValue);

							lvi.lParam = (LPARAM)setting;
							lvi.pszText = TranslateTS(name);
							lvi.iItem = ListView_InsertItem(hwndProtocols, &lvi);
							ListView_SetItemState(hwndProtocols, lvi.iItem, INDEXTOSTATEIMAGEMASK(show?2:1), LVIS_STATEIMAGEMASK);

							mir_free(name);
						}

						ListView_SetColumnWidth(hwndProtocols, 0, LVSCW_AUTOSIZE);
						ListView_Arrange(hwndProtocols, LVA_ALIGNLEFT | LVA_ALIGNTOP);
					}
					break;
				case CONTROL_TEXT:
					{
						TCHAR tmp[1024];
						SetDlgItemText(hwndDlg, ctrl->nID, MyDBGetContactSettingTString(NULL, module, ctrl->setting, tmp, 1024, ctrl->tszDefValue == NULL ? NULL : TranslateTS(ctrl->tszDefValue)));
						SendDlgItemMessage(hwndDlg, ctrl->nID, EM_LIMITTEXT, min(ctrl->max <= 0 ? 1024 : ctrl->max, 1024), 0);
					}
					break;
				case CONTROL_PASSWORD:
					{
						char tmp[1024];
						tmp[0]=0;

						DBVARIANT dbv = {0};
						if ( !DBGetContactSettingString(NULL, module, ctrl->setting, &dbv)) {
							lstrcpynA(tmp, dbv.pszVal, SIZEOF(tmp));
							DBFreeVariant(&dbv);
						}

						if (tmp[0] != 0)
							CallService(MS_DB_CRYPT_DECODESTRING, SIZEOF(tmp), (LPARAM) tmp);
						else if (ctrl->szDefValue != NULL)
							lstrcpynA(tmp, ctrl->szDefValue, SIZEOF(tmp));

						SetDlgItemTextA(hwndDlg, ctrl->nID, tmp);
						SendDlgItemMessage(hwndDlg, ctrl->nID, EM_LIMITTEXT, min(ctrl->max <= 0 ? 1024 : ctrl->max, 1024), 0);
					}
					break;
				case CONTROL_INT:
					{
						DWORD var = DBGetContactSettingDword(NULL, module, ctrl->setting, ctrl->dwDefValue);
						SetDlgItemInt(hwndDlg, ctrl->nID, var, ctrl->min <= 0);
						SendDlgItemMessage(hwndDlg, ctrl->nID, EM_LIMITTEXT, 9, 0);
					}
					break;
				case CONTROL_FILE:
					{
						TCHAR tmp[1024];
						MyDBGetContactSettingTString(NULL, module, ctrl->setting, tmp, 1024, ctrl->tszDefValue == NULL ? NULL : ctrl->tszDefValue);
						TCHAR abs[1024];
						PathToAbsolute(abs, 1024, tmp);

						SetDlgItemText(hwndDlg, ctrl->nID, abs);
						SendDlgItemMessage(hwndDlg, ctrl->nID, EM_LIMITTEXT, min(ctrl->max <= 0 ? 1024 : ctrl->max, 1024), 0);
					}
					break;
				case CONTROL_COMBO_TEXT:
					{
						TCHAR tmp[1024];
						MyDBGetContactSettingTString(NULL, module, ctrl->setting, tmp, 1024, ctrl->tszDefValue == NULL ? NULL : TranslateTS(ctrl->tszDefValue));
						SendDlgItemMessage(hwndDlg, ctrl->nID, CB_SELECTSTRING, 0, (WPARAM) tmp);
					}
					break;
				case CONTROL_COMBO_ITEMDATA:
					{
						TCHAR tmp[1024];
						MyDBGetContactSettingTString(NULL, module, ctrl->setting, tmp, 1024, ctrl->tszDefValue == NULL ? NULL : TranslateTS(ctrl->tszDefValue));
						int count = SendDlgItemMessage(hwndDlg, ctrl->nID, CB_GETCOUNT, 0, 0);
						int i;
						for (i = 0; i < count; i++)
						{
							TCHAR *id = (TCHAR *) SendDlgItemMessage(hwndDlg, ctrl->nID, CB_GETITEMDATA, (WPARAM) i, 0);
							if (lstrcmp(id, tmp) == 0)
								break;
						}
						if (i < count)
							SendDlgItemMessage(hwndDlg, ctrl->nID, CB_SETCURSEL, i, 0);
					}
					break;
				}
			}
			break;
		}
	case WM_COMMAND:
		{
			for (int i = 0 ; i < controlsSize ; i++) {
				OptPageControl *ctrl = &controls[i];

				if (LOWORD(wParam) == ctrl->nID) {
					switch(ctrl->type) {
					case CONTROL_TEXT:
					case CONTROL_SPIN:
					case CONTROL_INT:
					case CONTROL_PASSWORD:
						// Don't make apply enabled during buddy set
						if (HIWORD(wParam) != EN_CHANGE || (HWND)lParam != GetFocus())
							return 0;

						break;

					case CONTROL_COMBO_ITEMDATA:
					case CONTROL_COMBO_TEXT:
					case CONTROL_COMBO:
						if (HIWORD(wParam) != CBN_SELCHANGE || (HWND)lParam != GetFocus())
							return 0;
						break;
					}

					SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0);
				}
			}
		}
		break;

	case WM_NOTIFY:
		{
			LPNMHDR lpnmhdr = (LPNMHDR)lParam;
			if (lpnmhdr->idFrom == 0 && lpnmhdr->code == PSN_APPLY) {
				for (int i = 0 ; i < controlsSize ; i++) {
					OptPageControl *ctrl = &controls[i];

					if (GetDlgItem(hwndDlg, ctrl->nID) == NULL)
						continue;

					switch(ctrl->type) {
					case CONTROL_CHECKBOX:
						DBWriteContactSettingByte(NULL, module, ctrl->setting, (BYTE) IsDlgButtonChecked(hwndDlg, ctrl->nID));
						break;

					case CONTROL_SPIN:
						DBWriteContactSettingWord(NULL, module, ctrl->setting, (WORD) SendDlgItemMessage(hwndDlg, ctrl->nIDSpin, UDM_GETPOS, 0, 0));
						break;

					case CONTROL_COLOR:
						DBWriteContactSettingDword(NULL, module, ctrl->setting, (DWORD) SendDlgItemMessage(hwndDlg, ctrl->nID, CPM_GETCOLOUR, 0, 0));
						break;

					case CONTROL_RADIO:
						if (IsDlgButtonChecked(hwndDlg, ctrl->nID))
							DBWriteContactSettingWord(NULL, module, ctrl->setting, (BYTE) ctrl->value);
						break;

					case CONTROL_COMBO:
						DBWriteContactSettingWord(NULL, module, ctrl->setting, (WORD) SendDlgItemMessage(hwndDlg, ctrl->nID, CB_GETCURSEL, 0, 0));
						break;

					case CONTROL_PROTOCOL_LIST:
						{
							LVITEM lvi = {0};
							HWND hwndProtocols = GetDlgItem(hwndDlg, ctrl->nID);
							int i;

							lvi.mask = (UINT) LVIF_PARAM;

							for (i = 0; i < ListView_GetItemCount(hwndProtocols); i++)
							{
								lvi.iItem = i;
								ListView_GetItem(hwndProtocols, &lvi);

								char *setting = (char *)lvi.lParam;
								DBWriteContactSettingByte(NULL, module, setting, (BYTE)ListView_GetCheckState(hwndProtocols, i));
							}
						}
						break;

					case CONTROL_TEXT:
						{
							TCHAR tmp[1024];
							GetDlgItemText(hwndDlg, ctrl->nID, tmp, SIZEOF(tmp));
							DBWriteContactSettingTString(NULL, module, ctrl->setting, tmp);
						}
						break;

					case CONTROL_PASSWORD:
						{
							char tmp[1024];
							GetDlgItemTextA(hwndDlg, ctrl->nID, tmp, SIZEOF(tmp));

							if (ctrl->var != NULL) {
								char *var = (char *) ctrl->var;
								int size = min(ctrl->max <= 0 ? 1024 : ctrl->max, 1024);
								lstrcpynA(var, tmp, size);
							}

							if (ctrl->checkboxID != 0 && !IsDlgButtonChecked(hwndDlg, ctrl->checkboxID)) {
								db_unset(NULL, module, ctrl->setting);
								continue;
							}

							CallService(MS_DB_CRYPT_ENCODESTRING, SIZEOF(tmp), (LPARAM) tmp);
							DBWriteContactSettingString(NULL, module, ctrl->setting, tmp);

							// Don't load from DB
							continue;
						}
					case CONTROL_INT:
						{
							BOOL trans;
							int val = GetDlgItemInt(hwndDlg, ctrl->nID, &trans, ctrl->min <= 0);
							if ( !trans)
								val = ctrl->dwDefValue;
							if (ctrl->max != 0)
								val = min(val, ctrl->max);
							if (ctrl->min != 0)
								val = max(val, ctrl->min);
							DBWriteContactSettingDword(NULL, module, ctrl->setting, val);
						}
						break;
					case CONTROL_FILE:
						{
							TCHAR tmp[1024];
							GetDlgItemText(hwndDlg, ctrl->nID, tmp, 1024);
							TCHAR rel[1024];
							PathToRelative(rel, 1024, tmp);
							DBWriteContactSettingTString(NULL, module, ctrl->setting, rel);
						}
						break;
					case CONTROL_COMBO_TEXT:
						{
							TCHAR tmp[1024];
							GetDlgItemText(hwndDlg, ctrl->nID, tmp, 1024);
							DBWriteContactSettingTString(NULL, module, ctrl->setting, tmp);
						}
						break;
					case CONTROL_COMBO_ITEMDATA:
						{
							int sel = SendDlgItemMessage(hwndDlg, ctrl->nID, CB_GETCURSEL, 0, 0);
							DBWriteContactSettingTString(NULL, module, ctrl->setting, (TCHAR *) SendDlgItemMessage(hwndDlg, ctrl->nID, CB_GETITEMDATA, (WPARAM) sel, 0));
						}
						break;
					}

					LoadOpt(ctrl, module);
				}

				return TRUE;
			}
			else if (lpnmhdr->idFrom != 0 && lpnmhdr->code == LVN_ITEMCHANGED)
			{
				// Changed for protocols
				for (int i = 0 ; i < controlsSize ; i++) {
					OptPageControl *ctrl = &controls[i];

					if (ctrl->type == CONTROL_PROTOCOL_LIST && ctrl->nID == lpnmhdr->idFrom) {
						NMLISTVIEW *nmlv = (NMLISTVIEW *)lParam;

						if (IsWindowVisible(GetDlgItem(hwndDlg, ctrl->nID)) && ((nmlv->uNewState ^ nmlv->uOldState) & LVIS_STATEIMAGEMASK))
							SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0);

						break;
					}
				}
			}
			break;
		}
	case WM_DESTROY:
		{
			for (int i = 0 ; i < controlsSize ; i++) {
				OptPageControl *ctrl = &controls[i];

				if (GetDlgItem(hwndDlg, ctrl->nID) == NULL)
					continue;

				switch(ctrl->type) {
				case CONTROL_PROTOCOL_LIST:
					{
						LVITEM lvi = {0};
						HWND hwndProtocols = GetDlgItem(hwndDlg, ctrl->nID);
						int i;

						lvi.mask = (UINT) LVIF_PARAM;

						for (i = 0; i < ListView_GetItemCount(hwndProtocols); i++) {
							lvi.iItem = i;
							ListView_GetItem(hwndProtocols, &lvi);
							mir_free((char *) lvi.lParam);
						}
						break;
					}
				}
			}
			break;
		}
	}

	return 0;
}