summaryrefslogtreecommitdiff
path: root/plugins/DbEditorPP/src/watchedvars.cpp
blob: f438f3dba62c8dd35855ab955ba460a4e1951030 (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
#include "stdafx.h"

HWND hwnd2watchedVarsWindow = nullptr;

static WatchListArrayStruct WatchListArray = {};

static int lastColumn = -1;

ColumnsSettings csWatchList[] = {
	{ LPGENW("Contact"), 0, "Watch0width", 100 },
	{ LPGENW("Module"),  1, "Watch1width", 100 },
	{ LPGENW("Setting"), 2, "Watch2width", 100 },
	{ LPGENW("Value"),   3, "Watch3width", 200 },
	{ LPGENW("Type"),    4, "Watch4width", 75 },
	{nullptr}
};



int WatchedArrayIndex(MCONTACT hContact, const char *module, const char *setting, int strict) {
	for (int i = 0; i < WatchListArray.count; i++) {
		if (hContact == WatchListArray.item[i].hContact)
			if (!mir_strcmp(module, WatchListArray.item[i].module))
				// empty setting = module watching
				if ((!strict && !WatchListArray.item[i].setting) || !mir_strcmp(setting, WatchListArray.item[i].setting)) 
					return i;
	}
	return -1;
}


int addSettingToWatchList(MCONTACT hContact, const char *module, const char *setting)
{
	if (WatchListArray.count == WatchListArray.size)
	{
		WatchListArray.size += 32;
		WatchListArray.item = (struct DBsetting*)mir_realloc(WatchListArray.item, sizeof(struct DBsetting)*WatchListArray.size);
	}
	if (!WatchListArray.item) 
		return 0;

	db_get_s(hContact, module, setting, &(WatchListArray.item[WatchListArray.count].dbv), 0);

	WatchListArray.item[WatchListArray.count].hContact = hContact;
	WatchListArray.item[WatchListArray.count].module = mir_strdup(module);

	if (setting) 
		WatchListArray.item[WatchListArray.count].setting = mir_strdup(setting);
	else
		WatchListArray.item[WatchListArray.count].setting = nullptr;

	WatchListArray.count++;
	return 1;
}


void freeWatchListItem(int item)
{
    if (item < 0 || item >= WatchListArray.count) return;
    
	if (WatchListArray.item[item].module) 
		mir_free(WatchListArray.item[item].module);

	WatchListArray.item[item].module = nullptr;

	if (WatchListArray.item[item].setting) 
		mir_free(WatchListArray.item[item].setting);

	WatchListArray.item[item].setting = nullptr;
	db_free(&(WatchListArray.item[item].dbv));
	WatchListArray.item[item].hContact = 0;
}


void addwatchtolist(HWND hwnd, struct DBsetting *lParam)
{
	DBVARIANT *dbv = &(lParam->dbv);

	if (!lParam->module) return;
	
	LVITEM lvItem;
	lvItem.lParam = (LPARAM)lParam->hContact;
	lvItem.mask = LVIF_TEXT | LVIF_PARAM;
	lvItem.iItem = 0;
	lvItem.iSubItem = 0;

	if (!lParam->setting) // add every item in the module and return
	{
		ModuleSettingLL settinglist;
		if (IsModuleEmpty(lParam->hContact, lParam->module) || !EnumSettings(lParam->hContact, lParam->module, &settinglist))
			return;

		struct DBsetting dummy = {0};
		dummy.hContact = lParam->hContact;
		dummy.module = lParam->module;
		
		for (ModSetLinkLinkItem *setting = settinglist.first; setting; setting = setting->next)
		{
			dummy.setting = setting->name;
			addwatchtolist(hwnd, &dummy);
			
		}
		FreeModuleSettingLL(&settinglist);
		return;
	}
	
	db_free(&(lParam->dbv));

	if (db_get_s(lParam->hContact, lParam->module, lParam->setting, &(lParam->dbv), 0))
		return;

	wchar_t data[32], tmp[16], name[NAME_SIZE];

	GetContactName(lParam->hContact, nullptr, name, _countof(name));
	lvItem.pszText = name;

	int index = ListView_InsertItem(hwnd, &lvItem);

	ListView_SetItemText(hwnd, index, 0, lvItem.pszText);
	ListView_SetItemTextA(hwnd, index, 1, lParam->module);
	ListView_SetItemTextA(hwnd, index, 2, lParam->setting);
	ListView_SetItemText(hwnd, index, 4, DBVType(dbv->type));

	data[0] = 0;

	switch (dbv->type) {
	case DBVT_BLOB:
	{
		ptrA str(StringFromBlob(dbv->pbVal, dbv->cpbVal));
		ListView_SetItemTextA(hwnd, index, 3, str);
		break;
	}
	case DBVT_BYTE:
		mir_snwprintf(data, L"0x%02X (%s)", dbv->bVal, _ultow(dbv->bVal, tmp, 10));
		ListView_SetItemText(hwnd, index, 3, data);
		break;
	case DBVT_WORD:
		mir_snwprintf(data, L"0x%04X (%s)", dbv->wVal, _ultow(dbv->wVal, tmp, 10));
		ListView_SetItemText(hwnd, index, 3, data);
		break;

	case DBVT_DWORD:
		mir_snwprintf(data, L"0x%08X (%s)", dbv->dVal, _ultow(dbv->dVal, tmp, 10));
		ListView_SetItemText(hwnd, index, 3, data);
		break;

	case DBVT_ASCIIZ:
		ListView_SetItemTextA(hwnd, index, 3, dbv->pszVal);
		break;

	case DBVT_WCHAR:
	{
		ptrW str(mir_wstrdup(dbv->pwszVal));
		ListView_SetItemText(hwnd, index, 3, str);
		break;
	}

	case DBVT_UTF8:
	{
		ptrW str(mir_utf8decodeW(dbv->pszVal));
		ListView_SetItemText(hwnd, index, 3, str);
		break;
	}
	case DBVT_DELETED:
		if (IsResidentSetting(lParam->module, lParam->setting))
			ListView_SetItemText(hwnd, index, 3, TranslateT("*** resident ***"));
		break;
	} // switch

}


void PopulateWatchedWindow()
{
    if (!hwnd2watchedVarsWindow) return;
    HWND hwnd = GetDlgItem(hwnd2watchedVarsWindow, IDC_VARS);
	ListView_DeleteAllItems(hwnd);
	for (int i = 0; i < WatchListArray.count; i++) {
		addwatchtolist(hwnd, &(WatchListArray.item[i]));
	}

	if (lastColumn != -1) {
		ColumnsSortParams params;
		params.hList = hwnd;
		params.column = lastColumn;
		params.last = -1;
		ListView_SortItemsEx(params.hList, ColumnsCompare, (LPARAM)&params);
	}
}


void freeAllWatches()
{
	ListView_DeleteAllItems(GetDlgItem(hwnd2watchedVarsWindow, IDC_VARS));
	for (int i = 0; i < WatchListArray.count; i++)
	{
		freeWatchListItem(i);
	}
	mir_free(WatchListArray.item);
	WatchListArray.item = nullptr;
	WatchListArray.count = 0;
}


int WatchDialogResize(HWND, LPARAM, UTILRESIZECONTROL *urc)
{
	switch (urc->wId)
	{
	case IDC_VARS:
		urc->rcItem.top = 0;
		urc->rcItem.bottom = urc->dlgNewSize.cy;
		urc->rcItem.left = 0;
		urc->rcItem.right = urc->dlgNewSize.cx;
		return RD_ANCHORY_CUSTOM | RD_ANCHORX_CUSTOM;
	}
	return RD_ANCHORX_LEFT | RD_ANCHORY_TOP;
}


LRESULT CALLBACK WatchSubclassProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg) {

	case WM_KEYUP:
		if (wParam == VK_F5) {
			PopulateWatchedWindow();
		}
		break;
	}
	return mir_callNextSubclass(hwnd, WatchSubclassProc, msg, wParam, lParam);
}


INT_PTR CALLBACK WatchDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg) {
	case WM_INITDIALOG:
		hwnd2watchedVarsWindow = hwnd;
		// do the icon
		SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)LoadIcon(hInst, MAKEINTRESOURCE(ICO_REGEDIT)));
		TranslateMenu(GetMenu(hwnd));
		TranslateMenu(GetSubMenu(GetMenu(hwnd), 0));
		TranslateDialogDefault(hwnd);

		SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_APPWINDOW); // taskbar icon

		ListView_SetExtendedListViewStyle(GetDlgItem(hwnd, IDC_VARS), 32 | LVS_EX_LABELTIP); // LVS_EX_GRIDLINES

		loadListSettings(GetDlgItem(hwnd, IDC_VARS), csWatchList);
		Utils_RestoreWindowPositionNoMove(hwnd, NULL, modname, "Watch_");

		mir_subclassWindow(GetDlgItem(hwnd, IDC_VARS), WatchSubclassProc);

		PopulateWatchedWindow();
		return TRUE;

	// for the resize
	case WM_GETMINMAXINFO:
		{
			MINMAXINFO *mmi = (MINMAXINFO*)lParam;
			mmi->ptMinTrackSize.x = 500;
			mmi->ptMinTrackSize.y = 300;
		}
		return 0;

	case WM_SIZE:
		Utils_ResizeDialog(hwnd, hInst, MAKEINTRESOURCEA(IDD_WATCH_DIAG), WatchDialogResize);
		break;

	case WM_COMMAND:
		switch (LOWORD(wParam)) {
		case MENU_REMALL_WATCHES:
			freeAllWatches();
			break;
		case MENU_EXIT:
		case IDCANCEL:
			hwnd2watchedVarsWindow = nullptr;
			DestroyWindow(hwnd);
			break;
		case MENU_REFRESH:
			PopulateWatchedWindow();
		}
		break;

	case WM_NOTIFY:
		switch (LOWORD(wParam)) {
		case IDC_VARS:
			switch (((NMHDR*)lParam)->code) {
			case NM_DBLCLK:
				LVHITTESTINFO hti;
				LVITEM lvi;
				{
					HWND hwndVars = GetDlgItem(hwnd, IDC_VARS);
					hti.pt = ((NMLISTVIEW*)lParam)->ptAction;
					if (ListView_SubItemHitTest(hwndVars, &hti) > -1)
					{
						if (hti.flags&LVHT_ONITEM)
						{
							lvi.mask = LVIF_PARAM;
							lvi.iItem = hti.iItem;
							lvi.iSubItem = 0;
							if (ListView_GetItem(hwndVars, &lvi))
							{
								ItemInfo ii;
								ii.hContact = (MCONTACT)lvi.lParam;
								ListView_GetItemTextA(hwndVars, hti.iItem, 1, ii.module, _countof(ii.module));
								ListView_GetItemTextA(hwndVars, hti.iItem, 2, ii.setting, _countof(ii.setting));
								ii.type = FW_SETTINGNAME;
								SendMessage(hwnd2mainWindow, WM_FINDITEM, (WPARAM)&ii, 0);
							}
						}
					}
				}
				break;

			case LVN_COLUMNCLICK:
				LPNMLISTVIEW lv = (LPNMLISTVIEW)lParam;
				ColumnsSortParams params;
				params.hList = GetDlgItem(hwnd, IDC_VARS);
				params.column = lv->iSubItem;
				params.last = lastColumn;
				ListView_SortItemsEx(params.hList, ColumnsCompare, (LPARAM)&params);
				lastColumn = (params.column == lastColumn) ? -1 : params.column;
				break;
			}
			break;
		}
		break;

	case WM_DESTROY:
		ListView_DeleteAllItems(GetDlgItem(hwnd, IDC_VARS));
		saveListSettings(GetDlgItem(hwnd, IDC_VARS), csWatchList);
		Utils_SaveWindowPosition(hwnd, NULL, modname, "Watch_");
		hwnd2watchedVarsWindow = nullptr;
		break;
	}
	return 0;
}

void openWatchedVarWindow()
{
	if (!hwnd2watchedVarsWindow) 
		CreateDialog(hInst, MAKEINTRESOURCE(IDD_WATCH_DIAG), nullptr, WatchDlgProc);
	else
		SetForegroundWindow(hwnd2watchedVarsWindow);
}


void popupWatchedVar(MCONTACT hContact, const char *module, const char *setting)
{
	COLORREF colorBack = db_get_dw(NULL, modname, "PopupColour", RGB(255, 0, 0));
	COLORREF colorText = db_get_dw(NULL, modname, "PopupTextColour", RGB(0, 0, 0));
	int timeout = db_get_b(NULL, modname, "PopupDelay", 3);

	wchar_t name[NAME_SIZE], text[MAX_SECONDLINE], value[MAX_SECONDLINE];
	GetContactName(hContact, nullptr, name, _countof(name));

	// 2nd line
	int type = GetValue(hContact, module, setting, value, _countof(value));
	mir_snwprintf(text, TranslateT("Database Setting Changed: \nModule: \"%s\", Setting: \"%s\"\nNew Value (%s): \"%s\""), _A2T(module), _A2T(setting), DBVType(type), value);

	POPUPDATAT ppd = { 0 };
	ppd.lchContact = (MCONTACT)hContact;
	ppd.lchIcon = LoadIcon(hInst, MAKEINTRESOURCE(ICO_REGEDIT));
	mir_wstrncpy(ppd.lptzContactName, name, _countof(ppd.lptzContactName));
	mir_wstrncpy(ppd.lptzText, text, _countof(ppd.lptzText));
	ppd.colorBack = colorBack;
	ppd.colorText = colorText;
	ppd.iSeconds = timeout ? timeout : -1;
	PUAddPopupT(&ppd);
}