summaryrefslogtreecommitdiff
path: root/plugins/Bonsai/dlg_handlers.cpp
blob: 7bceb1026eb9f718fe8e3b4e418f4848f65988b5 (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
/*
Bonsai plugin for Miranda IM

Copyright © 2006 Cristian Libotean

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 "dlg_handlers.h"
#include "commctrl.h"

#define CMP(a, b) ( ((a == b) ? 0 : ((a < b) ? -1 : 1)) )

HWND hOptDlg = NULL;

const char *szColumns[] = {"Plugin", "Current group", "Current title", "Initial group", "Initial title"};
const int cxColumns[] = {100, 120, 120, 120, 120};
const int cColumns = sizeof(szColumns) / sizeof(szColumns[0]);

WNDPROC oldPluginsListProc = NULL;

inline int SettingChanged(WCHAR *defaultSetting, WCHAR *currentSetting, int nullTrueValue, int trueValue, int falseValue)
{
	if (currentSetting == NULL)
	{
		return ((defaultSetting != NULL) && (wcslen(defaultSetting) != 0)) ? nullTrueValue : falseValue;
	}
	else{
		return (_wcsicmp(currentSetting, defaultSetting)) ? trueValue : falseValue;
	}
}

void UpdatePlugin(HWND hList, int index, PPluginOptions plugin, int newlyModified)
{
	const int MAX_SIZE = 256;
	WCHAR buffer[MAX_SIZE];
	
	GetPluginGroup(plugin, buffer, MAX_SIZE);
	ListView_SetItemTextW(hList, index, 1, buffer);
	
	GetPluginTitle(plugin, buffer, MAX_SIZE);
	ListView_SetItemTextW(hList, index, 2, buffer);
	
	if (newlyModified)
	{
		LVITEM item = {0};
		item.iItem = index;
		item.mask = LVIF_IMAGE;
		item.iImage = 3;
		ListView_SetItem(hList, &item);
	}
	
	ListView_SetItemTextW(hList, index, 3, plugin->szGroup);
	
	ListView_SetItemTextW(hList, index, 4, plugin->szTitle);
}

int ItemOptionEntryChanged(int index, PPluginOptions plugin)
{
	const int MAX_SIZE = 256;
	WCHAR buffer[MAX_SIZE];
	WCHAR *res;

	int changed = 0;
	
	res = GetPluginGroup(plugin, buffer, MAX_SIZE);
	changed = SettingChanged(plugin->szGroup, res, 1, 1, 0);
//	changed = (res == NULL) ? plugin->szGroup != NULL : (_wcsicmp(buffer, plugin->szGroup)) ? 1 : 0;
	
	res = GetPluginTitle(plugin, buffer, MAX_SIZE);
	changed = SettingChanged(plugin->szTitle, res, 2, 1, changed);
//	changed = (res == NULL) ? plugin->szTitle != NULL : (_wcsicmp(buffer, plugin->szTitle)) ? 1 : changed;
	
	return changed;
}

void LoadPlugins(HWND hWnd, int bAll = 1)
{
	HWND hList = GetDlgItem(hWnd, IDC_LIST_PLUGINS);

	if (bAll)
	{
		ListView_DeleteAllItems(hList);
	}
	
	int i;
	int init = (bAll) ? 0 : ListView_GetItemCount(hList);
	PPluginOptions plugin = NULL;
	
	LVITEMW item = {0};
	item.mask = LVIF_INDENT | LVIF_IMAGE | LVIF_TEXT | LVIF_PARAM;
	for (i = init; i < lstPluginOptions.Count(); i++)
	{
		plugin = lstPluginOptions[i];
		item.iItem = i;
		item.lParam = (LPARAM) plugin;
		item.pszText = plugin->szPlugin;
		item.iImage = ItemOptionEntryChanged(i, plugin);
		ListView_InsertItemW(hList, &item);
		
		UpdatePlugin(hList, i, plugin, 0);
	}
}

struct SortParams{
	HWND hList;
	int column;
};

static int lastColumn = -1;

int CALLBACK PluginsCompare(LPARAM lParam1, LPARAM lParam2, LPARAM myParam)
{
	SortParams params = *(SortParams *) myParam;
	int res;
	const int MAX_SIZE = 1024;
	
	wchar_t text1[MAX_SIZE];
	wchar_t text2[MAX_SIZE];
	
	ListView_GetItemTextW(params.hList, (int) lParam1, params.column, text1, MAX_SIZE);
	ListView_GetItemTextW(params.hList, (int) lParam2, params.column, text2, MAX_SIZE);
	
	res = _wcsicmp(text1, text2);
	
	res = (params.column == lastColumn) ? -res : res;
	
	return res;
}

int LoadColumns(HWND hList)
{
	int i;
	int start = 0;
	LVCOLUMN col;
	col.mask = LVCF_TEXT | LVCF_WIDTH;
	
	while (ListView_DeleteColumn(hList, 1)) //delete all columns
	{
		start = 1;
	}
	
	for (i = start; i < cColumns; i++)
	{
		col.pszText = TranslateTS(szColumns[i]);
	
		col.cx = cxColumns[i];
	
		ListView_InsertColumn(hList, i, &col);
	}
	
	return 0;
}

void LoadPluginInfo(HWND hWnd, PPluginOptions plugin)
{
	if (plugin)
	{
		const int MAX_SIZE = 256;
		WCHAR buffer[MAX_SIZE];
		
		GetPluginGroup(plugin, buffer, MAX_SIZE);
		SetWindowTextW(GetDlgItem(hWnd, IDC_GROUP), buffer);
		
		GetPluginTitle(plugin, buffer, MAX_SIZE);
		SetWindowTextW(GetDlgItem(hWnd, IDC_TITLE), buffer);
	}
}

PPluginOptions GetSelectedPlugin(HWND hWnd, int *index = NULL)
{
	int i;
	HWND hList = GetDlgItem(hWnd, IDC_LIST_PLUGINS);
	int count = ListView_GetItemCount(hList);
	for (i = 0; i < count; i++)
	{
		if (ListView_GetItemState(hList, i, LVIS_SELECTED) == LVIS_SELECTED)
		{
			LVITEM item = {0};
			item.iItem = i;
			item.mask = LVIF_PARAM;
			
			if (index)
			{
				*index = i;
			}
			
			ListView_GetItem(hList, &item);
			return (PPluginOptions) item.lParam;
		}
	}
	
	if (index)
	{
		*index = -1;
	}
	return NULL;
}

INT_PTR CALLBACK DlgProcOptions(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	static int bInitializing = 0;
	switch (msg)
	{
		case WM_INITDIALOG:
		{
			bInitializing = 1;
			hOptDlg = hWnd;
			HWND hList = GetDlgItem(hWnd, IDC_LIST_PLUGINS);
			
			TranslateDialogDefault(hWnd);

			ListView_SetExtendedListViewStyleEx(hList, LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);
			//ListView_SetImageList(hList, imgList, LVSIL_NORMAL);
			ImageList_SetBkColor(imgList, RGB(255, 255, 255));
			ListView_SetImageList(hList, imgList, LVSIL_SMALL);

			//ListView_SetImageList(hList, imgList, LVSIL_STATE);
			
			LoadColumns(hList);
			
			LoadPlugins(hWnd);
			
			SortParams params = {0};
			params.hList = hList;
			params.column = DBGetContactSettingByte(NULL, ModuleName, "LastSortColumn", -1);
			ListView_SortItemsEx(hList, PluginsCompare, &params);
			
			EnableWindow(GetDlgItem(hWnd, IDC_SAVE), 0);
			EnableWindow(GetDlgItem(hWnd, IDC_CANCEL), 0);
			EnableWindow(GetDlgItem(hWnd, IDC_GROUP), 0);
			EnableWindow(GetDlgItem(hWnd, IDC_TITLE), 0);
			
			bInitializing = 0;
			return TRUE;
			break;
		}
		
		case WM_DESTROY:
		{
			DBWriteContactSettingByte(NULL, ModuleName, "LastSortColumn", lastColumn);
			hOptDlg = NULL;
			
			lastColumn = -1;
		
			break;
		}
		
		case FOM_UPDATEPLUGINS:
		{
			LoadPlugins(hWnd, 0);
			
			break;
		}
		
		case WM_COMMAND:
		{
			switch (LOWORD(wParam))
			{
				case IDC_GROUP:
				case IDC_TITLE:
				{
					if ((HIWORD(wParam) == EN_CHANGE))
					{
						if (!bInitializing)
						{
							EnableWindow(GetDlgItem(hWnd, IDC_SAVE), 1);
							EnableWindow(GetDlgItem(hWnd, IDC_CANCEL), 1);
						}
					}
					
					break;
				}
				
				case IDC_SAVE:
				{
					int index;
					PPluginOptions plugin = GetSelectedPlugin(hWnd, &index);
					if (plugin)
					{
						WCHAR buffer[256];
						int changed = 0;
						
						GetWindowTextW(GetDlgItem(hWnd, IDC_GROUP), buffer, 256);
						SavePluginGroup(plugin, buffer);
						changed = (_wcsicmp(plugin->szGroup, buffer) != 0) ? 1 : 0;
						
						GetWindowTextW(GetDlgItem(hWnd, IDC_TITLE), buffer, 256);
						SavePluginTitle(plugin, buffer);
						changed = (_wcsicmp(plugin->szTitle, buffer) != 0) ? 1 : changed;
						
						UpdatePlugin(GetDlgItem(hWnd, IDC_LIST_PLUGINS), index, plugin, 1);
						
					}
					EnableWindow(GetDlgItem(hWnd, IDC_SAVE), 0);
					
					break;
				}
				
				case IDC_CANCEL:
				{
					int state = GetKeyState(VK_CONTROL);
					if (state & (1 << 8)) //ctrl pressed
					{
						if (MessageBox(hWnd, Translate("This will reset all plugins to their default group and title.\nDo you wish to reset all plugins to their default group and title ?"), Translate("Proceed ?"), MB_YESNO | MB_ICONWARNING) == IDYES)
						{
							int i;
							HWND hList = GetDlgItem(hWnd, IDC_LIST_PLUGINS);
							int count = ListView_GetItemCount(hList);
							LVITEM item = {0};
							item.mask = LVIF_PARAM;
							
							PPluginOptions plugin;
							
							for (i = 0; i < count; i++)
							{
								item.iItem = i;
								ListView_GetItem(hList, &item);
								plugin = (PPluginOptions) item.lParam;
								SavePluginGroup(plugin, plugin->szGroup);
								SavePluginTitle(plugin, plugin->szTitle);
								UpdatePlugin(hList, i, plugin, 1);
							}
							
							LoadPluginInfo(hWnd, GetSelectedPlugin(hWnd));
						}
					}
					else{
						int index;
						PPluginOptions plugin = GetSelectedPlugin(hWnd, &index);
						if (plugin)
						{
							SavePluginGroup(plugin, plugin->szGroup);
							SavePluginTitle(plugin, plugin->szTitle);
							
							UpdatePlugin(GetDlgItem(hWnd, IDC_LIST_PLUGINS), index, plugin, 1);
							LoadPluginInfo(hWnd, GetSelectedPlugin(hWnd));
						}
						
						EnableWindow(GetDlgItem(hWnd, IDC_SAVE), 0);
						EnableWindow(GetDlgItem(hWnd, IDC_CANCEL), 0);
					}
					
					break;
				}
			}
			
			break;
		}
		
		case WM_NOTIFY:
		{
			switch(((LPNMHDR)lParam)->idFrom)
			{
				case 0:
				{
					switch (((LPNMHDR)lParam)->code)
					{
						case PSN_APPLY:
						{
							//<TODO>
						
							break;
						}
					}
					
					break;
				}
				
				case IDC_LIST_PLUGINS:
				{
					switch (((LPNMHDR)lParam)->code)
					{
						case LVN_COLUMNCLICK:
						{
							LPNMLISTVIEW lv = (LPNMLISTVIEW) lParam;
							int column = lv->iSubItem;
							SortParams params = {0};
							params.hList = GetDlgItem(hWnd, IDC_LIST_PLUGINS);
							params.column = column;
							
							ListView_SortItemsEx(params.hList, PluginsCompare, (LPARAM) &params);
							lastColumn = (params.column == lastColumn) ? -1 : params.column;

							break;
						}
						
						case LVN_ITEMCHANGED:
						{
							NMLISTVIEW *data = (NMLISTVIEW *) lParam;
							if (data->uNewState & LVIS_SELECTED) //new item
							{
								LoadPluginInfo(hWnd, GetSelectedPlugin(hWnd));
								
								EnableWindow(GetDlgItem(hWnd, IDC_SAVE), 0);
								EnableWindow(GetDlgItem(hWnd, IDC_CANCEL), 1);
								EnableWindow(GetDlgItem(hWnd, IDC_GROUP), 1);
								EnableWindow(GetDlgItem(hWnd, IDC_TITLE), 1);
							}
							else{ //selected item
								if (data->uOldState & LVIS_SELECTED)
								{
									const int MAX_SIZE = 256;
									PPluginOptions plugin = (PPluginOptions) data->lParam;
									if (plugin)
									{
										WCHAR group[MAX_SIZE];
										WCHAR title[MAX_SIZE];
										WCHAR newGroup[MAX_SIZE];
										WCHAR newTitle[MAX_SIZE];
										
										GetPluginGroup(plugin, group, MAX_SIZE);
										GetPluginTitle(plugin, title, MAX_SIZE);
										GetWindowTextW(GetDlgItem(hWnd, IDC_GROUP), newGroup, MAX_SIZE);
										GetWindowTextW(GetDlgItem(hWnd, IDC_TITLE), newTitle, MAX_SIZE);
										
										if ((wcscmp(group, newGroup)) || (wcscmp(title, newTitle)))
										{
											if (MessageBox(hWnd, Translate("Do you want to save the changes now ?"), Translate("Changes not saved"), MB_YESNO | MB_ICONINFORMATION) == IDYES)
											{
												SavePluginGroup(plugin, newGroup);
												SavePluginTitle(plugin, newTitle);
												
												UpdatePlugin(GetDlgItem(hWnd, IDC_LIST_PLUGINS), data->iItem, plugin, 1);
											}
										}
									}
								}
							}

							break;
						}
					}
				
					break;
				}
			}
			
			break;
		}
	}
	
	return 0;
}