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

int saveAsType(HWND hwnd, int original)
{
    if (!IsWindowVisible(GetDlgItem(hwnd, GRP_TYPE)))
    	return original;

	if (IsDlgButtonChecked(hwnd, CHK_BYTE))
		return DBVT_BYTE;
	else if (IsDlgButtonChecked(hwnd, CHK_WORD))
		return DBVT_WORD;
	else if (IsDlgButtonChecked(hwnd, CHK_DWORD))
		return DBVT_DWORD;
	else if (IsDlgButtonChecked(hwnd, CHK_STRING))
		return DBVT_ASCIIZ;

	return original; 
}


INT_PTR CALLBACK EditSettingDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
	case WM_INITDIALOG:
	{
		SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)lParam);

		struct DBsetting *dbsetting = (struct DBsetting*)lParam;

		char val[16] = {0};
		int convert = 0;

		switch (dbsetting->dbv.type)
		{
		case DBVT_BYTE:
			CheckRadioButton(hwnd, CHK_BYTE, CHK_STRING, CHK_BYTE);
			ShowWindow(GetDlgItem(hwnd, IDC_STRING), SW_HIDE);
			CheckRadioButton(hwnd, CHK_HEX, CHK_DECIMAL, (g_Hex & HEX_BYTE) ? CHK_HEX : CHK_DECIMAL);
			if (dbsetting->setting) mir_snprintf(val, (g_Hex & HEX_BYTE) ? "0x%02X" : "%u", dbsetting->dbv.bVal);
			break;		
		case DBVT_WORD:
			CheckRadioButton(hwnd, CHK_BYTE, CHK_STRING, CHK_WORD);
			ShowWindow(GetDlgItem(hwnd, IDC_STRING), SW_HIDE);
			CheckRadioButton(hwnd, CHK_HEX, CHK_DECIMAL, (g_Hex & HEX_WORD) ? CHK_HEX : CHK_DECIMAL);
			if (dbsetting->setting) mir_snprintf(val, (g_Hex & HEX_WORD) ? "0x%04X" : "%u", dbsetting->dbv.wVal);
			break;
		case DBVT_DWORD:
			CheckRadioButton(hwnd, CHK_BYTE, CHK_STRING, CHK_DWORD);
			ShowWindow(GetDlgItem(hwnd, IDC_STRING), SW_HIDE);
			CheckRadioButton(hwnd, CHK_HEX, CHK_DECIMAL, (g_Hex & HEX_DWORD) ? CHK_HEX : CHK_DECIMAL);
			if (dbsetting->setting) mir_snprintf(val, (g_Hex & HEX_DWORD) ? "0x%08X" : "%u", dbsetting->dbv.dVal);
			break;

		case DBVT_ASCIIZ:
		case DBVT_UTF8:
		case DBVT_WCHAR:
			ShowWindow(GetDlgItem(hwnd, IDC_STRING), SW_SHOW);
			ShowWindow(GetDlgItem(hwnd, IDC_SETTINGVALUE), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, CHK_HEX), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, CHK_DECIMAL), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, GRP_BASE), SW_HIDE);
			break;

		case DBVT_BLOB:
			ShowWindow(GetDlgItem(hwnd, IDC_STRING), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, IDC_SETTINGVALUE), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, IDC_BLOB), SW_SHOW);
			ShowWindow(GetDlgItem(hwnd, CHK_HEX), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, CHK_DECIMAL), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, GRP_BASE), SW_HIDE);
			break;

		case DBVT_DELETED: // resident
			CheckRadioButton(hwnd, CHK_BYTE, CHK_STRING, CHK_STRING);
			CheckRadioButton(hwnd, CHK_HEX, CHK_DECIMAL, CHK_DECIMAL);
			convert = 1;
			break;

		default: 
			msg(TranslateT("Unknown DBVariant type!"));
			DestroyWindow(hwnd);
			return TRUE;              
		}


		if (dbsetting->setting) {
			SetDlgItemTextA(hwnd, IDC_SETTINGNAME, dbsetting->setting);

			switch (dbsetting->dbv.type)
			{
			case DBVT_BYTE:
			case DBVT_WORD:
			case DBVT_DWORD:
				SetDlgItemTextA(hwnd, IDC_SETTINGVALUE, val);
				convert = 1;
				break;

			case DBVT_ASCIIZ:
				SetDlgItemTextA(hwnd, IDC_STRING, dbsetting->dbv.pszVal);
				break;

			case DBVT_WCHAR:
				SetDlgItemTextW(hwnd, IDC_STRING, dbsetting->dbv.pwszVal);
				break;

			case DBVT_UTF8:
				{
				    ptrW tmp(mir_utf8decodeW(dbsetting->dbv.pszVal));
					SetDlgItemTextW(hwnd, IDC_STRING, tmp);
					break;
				}

			case DBVT_BLOB:
				{
					ptrA tmp(StringFromBlob(dbsetting->dbv.pbVal, dbsetting->dbv.cpbVal));
					SetDlgItemTextA(hwnd, IDC_BLOB, tmp);
					break;
				}
			}
		}

		if (!convert) {
			ShowWindow(GetDlgItem(hwnd, GRP_TYPE), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, CHK_BYTE), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, CHK_WORD), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, CHK_DWORD), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, CHK_STRING), SW_HIDE);
		}

		TranslateDialogDefault(hwnd);
		{
			wchar_t text[MSG_SIZE];
			mir_sntprintf(text, dbsetting->setting?TranslateT("Edit setting (%s)"):TranslateT("New setting (%s)"), DBVType(dbsetting->dbv.type));
			SetWindowText(hwnd, text);
		}
	}
	return TRUE;

	case WM_COMMAND:
		switch (LOWORD(wParam))
		{
		case CHK_BYTE:
		case CHK_WORD:
		case CHK_DWORD:
			EnableWindow(GetDlgItem(hwnd, CHK_HEX), 1);
			EnableWindow(GetDlgItem(hwnd, CHK_DECIMAL), 1);
			CheckRadioButton(hwnd, CHK_BYTE, CHK_STRING, LOWORD(wParam));
			break;
		case CHK_STRING:
			EnableWindow(GetDlgItem(hwnd, CHK_HEX), 0);
			EnableWindow(GetDlgItem(hwnd, CHK_DECIMAL), 0);
			CheckRadioButton(hwnd, CHK_BYTE, CHK_STRING, LOWORD(wParam));
			break;

		case CHK_HEX:
		case CHK_DECIMAL:
			CheckRadioButton(hwnd, CHK_HEX, CHK_DECIMAL, LOWORD(wParam));
			{
				wchar_t *setting, text[32];
				int settingLength, tmp;
				settingLength = GetWindowTextLength(GetDlgItem(hwnd, IDC_SETTINGVALUE));
				if (settingLength)
				{
					setting = (wchar_t*)mir_alloc((settingLength+1)*sizeof(wchar_t));
					GetDlgItemText(hwnd, IDC_SETTINGVALUE, setting, settingLength + 1);
					if (LOWORD(wParam) == CHK_DECIMAL && IsDlgButtonChecked(hwnd, CHK_DECIMAL))
					{
						swscanf(setting, L"%X", &tmp);
						mir_sntprintf(text, L"%u", tmp);
					}
					else
					{
						swscanf(setting, L"%u", &tmp);
						mir_sntprintf(text, L"%X", tmp);
					}
					SetDlgItemText(hwnd, IDC_SETTINGVALUE, text);
					mir_free(setting);
				}
			}
			break;

		case IDC_SETTINGNAME:
			EnableWindow(GetDlgItem(hwnd, IDOK), GetWindowTextLength(GetDlgItem(hwnd, IDC_SETTINGNAME)));
			break;

		case IDOK:
		{
			struct DBsetting *dbsetting = (struct DBsetting*)GetWindowLongPtr(hwnd, GWLP_USERDATA);

			wchar_t settingname[FLD_SIZE];
			GetDlgItemText(hwnd, IDC_SETTINGNAME, settingname, _countof(settingname));

			if (settingname[0])
			{
				int valueID = 0;

				switch (dbsetting->dbv.type)
				{
				case DBVT_BYTE:
				case DBVT_WORD:
				case DBVT_DWORD:
				case DBVT_DELETED:				
					valueID = IDC_SETTINGVALUE;
					break;

				case DBVT_ASCIIZ:
				case DBVT_UTF8:
				case DBVT_WCHAR:
					valueID = IDC_STRING;
					break;

				case DBVT_BLOB:
					valueID = IDC_BLOB;
					break;
				default:
					break;
				}

				if (!valueID)
					break;

				int len = GetWindowTextLength(GetDlgItem(hwnd, valueID))+1;
				wchar_t *value = (wchar_t*)mir_alloc(len*sizeof(wchar_t));

				GetDlgItemText(hwnd, valueID, value, len);
				_T2A setting(settingname);

				int type = saveAsType(hwnd, dbsetting->dbv.type); 
				int res = 0;				

				// write the setting
				switch (type)
				{
				case DBVT_BYTE:
				case DBVT_WORD:
				case DBVT_DWORD:
					res = setNumericValue(dbsetting->hContact, dbsetting->module, setting, wcstoul(value, NULL, IsDlgButtonChecked(hwnd, CHK_HEX)? 16 : 10), type);
					break;
				case DBVT_ASCIIZ:
				case DBVT_UTF8:
				case DBVT_WCHAR:
					res = setTextValue(dbsetting->hContact, dbsetting->module, setting, value, type);
					break;
				case DBVT_BLOB:
					res = WriteBlobFromString(dbsetting->hContact, dbsetting->module, setting, _T2A(value), len);
					break;
				}

				mir_free(value);

				if (!res) {
					msg(TranslateT("Unable to store value in this data type!"));
					break;
				}

				// delete old setting
				if (dbsetting->setting && mir_strcmp(setting, dbsetting->setting))
					db_unset(dbsetting->hContact, dbsetting->module, dbsetting->setting);
			} 

		} // fall through
		case IDCANCEL:
		{
			struct DBsetting *dbsetting = (struct DBsetting*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
			mir_free(dbsetting->module);
			mir_free(dbsetting->setting);
			db_free(&dbsetting->dbv);
			mir_free(dbsetting);
			DestroyWindow(hwnd);
		}
		break;
		}
	}
	return 0;
}


void editSetting(MCONTACT hContact, const char *module, const char *setting)
{
	DBVARIANT dbv = { 0 }; 
	if (!db_get_s(hContact, module, setting, &dbv, 0) || IsResidentSetting(module, setting))
	{
	    // gets free()ed in the window proc
		struct DBsetting *dbsetting = (struct DBsetting *)mir_calloc(sizeof(struct DBsetting)); 

		dbsetting->dbv = dbv; 
		dbsetting->hContact = hContact;
		dbsetting->module = mir_strdup(module);
		dbsetting->setting = mir_strdup(setting);
		
		CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_EDIT_SETTING), hwnd2mainWindow, EditSettingDlgProc, (LPARAM)dbsetting);
	}
}


void copySetting(MCONTACT hContact, const char *module, const char *setting)
{
	DBVARIANT dbv = { 0 }, dbv2; 
	if (db_get_s(hContact, module, setting, &dbv, 0)) return;

	char tmp[FLD_SIZE];

	for (int i = 1; i < 10; i++) { 
		mir_snprintf(tmp, "%s (%d)", setting, i);
		if (!db_get_s(hContact, module, tmp, &dbv2, 0))
			db_free(&dbv2);
		else {
			// gets free()ed in the window proc
			struct DBsetting *dbsetting = (struct DBsetting *)mir_calloc(sizeof(struct DBsetting)); 

			dbsetting->dbv = dbv; 
			dbsetting->hContact = hContact;
			dbsetting->module = mir_strdup(module);
			dbsetting->setting = mir_strdup(tmp);
			CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_EDIT_SETTING), hwnd2mainWindow, EditSettingDlgProc, (LPARAM)dbsetting);
			return;
		}
	}
	db_free(&dbv);
}


void newSetting(MCONTACT hContact, const char *module, int type)
{
	// gets safe_free()ed in the window proc
	DBsetting *dbsetting = (DBsetting*)mir_calloc(sizeof(DBsetting));
	dbsetting->dbv.type = type;
	dbsetting->hContact = hContact;
	dbsetting->module = mir_strdup(module);
	dbsetting->setting = NULL;
	CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_EDIT_SETTING), hwnd2mainWindow, EditSettingDlgProc, (LPARAM)dbsetting);
}