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
|
#include "common.h"
HWND hAddNodeDlg, hChangeNodeDlg;
bool UpdateListFlag = false;
INT_PTR CToxProto::MainOptionsProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
CToxProto *proto = (CToxProto*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
switch (uMsg)
{
case WM_INITDIALOG:
TranslateDialogDefault(hwnd);
{
proto = (CToxProto*)lParam;
SetWindowLongPtr(hwnd, GWLP_USERDATA, lParam);
ptrT nick(proto->getTStringA("Nick"));
SetDlgItemText(hwnd, IDC_NAME, nick);
ptrT pass(proto->getTStringA("Password"));
SetDlgItemText(hwnd, IDC_PASSWORD, pass);
ptrA address(proto->getStringA(TOX_SETTINGS_ID));
if (address != NULL)
{
SetDlgItemTextA(hwnd, IDC_TOXID, address);
}
ptrT group(proto->getTStringA(TOX_SETTINGS_GROUP));
SetDlgItemText(hwnd, IDC_GROUP, group);
SendDlgItemMessage(hwnd, IDC_GROUP, EM_LIMITTEXT, 64, 0);
CheckDlgButton(hwnd, IDC_DISABLE_UDP, proto->getBool("DisableUDP", 0));
CheckDlgButton(hwnd, IDC_DISABLE_IPV6, proto->getBool("DisableIPv6", 0));
}
return TRUE;
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case IDC_NAME:
case IDC_GROUP:
case IDC_PASSWORD:
if ((HWND)lParam == GetFocus())
{
if (HIWORD(wParam) != EN_CHANGE) return 0;
SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0);
}
break;
case IDC_DISABLE_UDP:
case IDC_DISABLE_IPV6:
SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0);
break;
}
case IDC_CLIPBOARD:
{
char toxId[TOX_FRIEND_ADDRESS_SIZE * 2 + 1];
GetDlgItemTextA(hwnd, IDC_TOXID, toxId, SIZEOF(toxId));
if (OpenClipboard(GetDlgItem(hwnd, IDC_TOXID)))
{
EmptyClipboard();
HGLOBAL hMem = GlobalAlloc(GMEM_FIXED, sizeof(toxId));
memcpy(GlobalLock(hMem), toxId, sizeof(toxId));
GlobalUnlock(hMem);
SetClipboardData(CF_TEXT, hMem);
CloseClipboard();
}
}
break;
}
break;
case WM_NOTIFY:
if (reinterpret_cast<NMHDR*>(lParam)->code == PSN_APPLY)
{
TCHAR nick[TOX_MAX_NAME_LENGTH];
GetDlgItemText(hwnd, IDC_NAME, nick, TOX_MAX_NAME_LENGTH);
proto->setTString("Nick", nick);
TCHAR password[MAX_PATH];
GetDlgItemText(hwnd, IDC_PASSWORD, password, SIZEOF(password));
proto->setTString("Password", password);
if (proto->password != NULL)
{
mir_free(proto->password);
proto->password = NULL;
}
proto->password = mir_utf8encodeW(password);
TCHAR group[64];
GetDlgItemText(hwnd, IDC_GROUP, group, SIZEOF(group));
if (_tcslen(group) > 0)
{
proto->setTString(NULL, TOX_SETTINGS_GROUP, group);
Clist_CreateGroup(0, group);
}
else
{
proto->delSetting(NULL, TOX_SETTINGS_GROUP);
}
proto->setByte("DisableUDP", (BYTE)IsDlgButtonChecked(hwnd, IDC_DISABLE_UDP));
proto->setByte("DisableIPv6", (BYTE)IsDlgButtonChecked(hwnd, IDC_DISABLE_IPV6));
if (proto->IsOnline())
{
//proto->SaveToxProfile();
}
return TRUE;
}
break;
}
return FALSE;
}
void CreateList(HWND hwndList)
{
SendMessage(hwndList, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
LVCOLUMN lvc = { 0 };
// Initialize the LVCOLUMN structure.
// The mask specifies that the format, width, text, and
// subitem members of the structure are valid.
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
lvc.fmt = LVCFMT_LEFT;
lvc.iSubItem = 0;
lvc.pszText = TranslateT("IPv4");
lvc.cx = 100; // width of column in pixels
ListView_InsertColumn(hwndList, 0, &lvc);
lvc.iSubItem = 1;
lvc.pszText = TranslateT("IPv6");
lvc.cx = 100; // width of column in pixels
ListView_InsertColumn(hwndList, 1, &lvc);
lvc.iSubItem = 2;
lvc.pszText = TranslateT("Port");
lvc.cx = 100; // width of column in pixels
ListView_InsertColumn(hwndList, 2, &lvc);
lvc.iSubItem = 3;
lvc.pszText = TranslateT("Client ID");
lvc.cx = 100; // width of column in pixels
ListView_InsertColumn(hwndList, 3, &lvc);
}
void UpdateList(HWND hwndList)
{
LVITEM lvI = { 0 };
int NodeCount = db_get_b(NULL, "TOX", "NodeCount", 0);
for (int i = 0; i < NodeCount; i++) {
UpdateListFlag = true;
lvI.mask = LVIF_TEXT;
lvI.iSubItem = 0;
char buff[MAX_PATH];
mir_snprintf(buff, SIZEOF(buff), "Node_%d_IPv4", i + 1);
TCHAR *ptszIPv4 = db_get_tsa(NULL, "TOX", buff);
mir_snprintf(buff, SIZEOF(buff), "Node_%d_IPv6", i + 1);
TCHAR *ptszIPv6 = db_get_tsa(NULL, "TOX", buff);
mir_snprintf(buff, SIZEOF(buff), "Node_%d_ClientID", i + 1);
TCHAR *ptszClientID = db_get_tsa(NULL, "TOX", buff);
mir_snprintf(buff, SIZEOF(buff), "Node_%d_Port", i + 1);
DWORD PortNum = db_get_dw(NULL, "TOX", buff, 0);
TCHAR ptszPort[10];
_itot(PortNum, ptszPort, 10);
if (ptszIPv4 && ptszIPv6 && ptszClientID && ptszPort) {
lvI.pszText = ptszIPv4;
lvI.iItem = i;
ListView_InsertItem(hwndList, &lvI);
lvI.iSubItem = 1;
lvI.pszText = ptszIPv6;
ListView_SetItem(hwndList, &lvI);
lvI.iSubItem = 2;
lvI.pszText = ptszPort;
ListView_SetItem(hwndList, &lvI);
lvI.iSubItem = 3;
lvI.pszText = ptszClientID;
ListView_SetItem(hwndList, &lvI);
mir_free(ptszIPv4);
mir_free(ptszIPv6);
mir_free(ptszClientID);
}
}
UpdateListFlag = false;
}
void DeleteAllItems(HWND hwndList)
{
ListView_DeleteAllItems(hwndList);
}
INT_PTR CALLBACK AddNodeDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg) {
case WM_INITDIALOG:
TranslateDialogDefault(hwndDlg);
SetWindowLongPtr(hwndDlg, GWLP_USERDATA, (LONG_PTR)lParam);
SetWindowText(hwndDlg, TranslateT("Add node"));
Utils_RestoreWindowPositionNoSize(hwndDlg, NULL, MODULE, "AddNodeDlg");
return TRUE;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDOK:
TCHAR str[MAX_PATH];
{
if (!GetDlgItemText(hwndDlg, IDC_IPV4, str, SIZEOF(str))) {
MessageBox(hwndDlg, TranslateT("Enter IPv4"), TranslateT("Error"), MB_OK);
break;
}
if (!GetDlgItemText(hwndDlg, IDC_IPV6, str, SIZEOF(str))) {
MessageBox(hwndDlg, TranslateT("Enter IPv6"), TranslateT("Error"), MB_OK);
break;
}
if (!GetDlgItemInt(hwndDlg, IDC_PORT, NULL, false)) {
MessageBox(hwndDlg, TranslateT("Enter port"), TranslateT("Error"), MB_OK);
break;
}
if (!GetDlgItemText(hwndDlg, IDC_CLIENTID, str, SIZEOF(str))) {
MessageBox(hwndDlg, TranslateT("Enter client ID"), TranslateT("Error"), MB_OK);
break;
}
int NodeCount = db_get_b(NULL, "TOX", "NodeCount", 0);
char buff[MAX_PATH];
GetDlgItemText(hwndDlg, IDC_IPV4, str, SIZEOF(str));
mir_snprintf(buff, SIZEOF(buff), "Node_%d_IPv4", NodeCount + 1);
db_set_ts(NULL, "TOX", buff, str);
GetDlgItemText(hwndDlg, IDC_IPV6, str, SIZEOF(str));
mir_snprintf(buff, SIZEOF(buff), "Node_%d_IPv6", NodeCount + 1);
db_set_ts(NULL, "TOX", buff, str);
GetDlgItemText(hwndDlg, IDC_CLIENTID, str, SIZEOF(str));
mir_snprintf(buff, SIZEOF(buff), "Node_%d_ClientID", NodeCount + 1);
db_set_ts(NULL, "TOX", buff, str);
mir_snprintf(buff, SIZEOF(buff), "Node_%d_Port", NodeCount + 1);
db_set_dw(NULL, "TOX", buff, (DWORD)GetDlgItemInt(hwndDlg, IDC_PORT, NULL, false));
db_set_b(NULL, "TOX", "NodeCount", NodeCount + 1);
HWND hwndList = (HWND)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
DeleteAllItems(hwndList);
UpdateList(hwndList);
}
// fall through
case IDCANCEL:
DestroyWindow(hwndDlg);
break;
}
break;
case WM_CLOSE:
DestroyWindow(hwndDlg);
break;
case WM_DESTROY:
hAddNodeDlg = 0;
Utils_SaveWindowPosition(hwndDlg, NULL, MODULE, "AddNodeDlg");
break;
}
return FALSE;
}
INT_PTR CALLBACK ChangeNodeDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg) {
case WM_INITDIALOG:
TranslateDialogDefault(hwndDlg);
{
ItemInfo &SelItem = *(ItemInfo*)lParam;
ItemInfo *nSelItem = new ItemInfo(SelItem);
SetWindowLongPtr(hwndDlg, GWLP_USERDATA, (LONG_PTR)nSelItem);
SetWindowText(hwndDlg, TranslateT("Change node"));
char buff[MAX_PATH];
mir_snprintf(buff, SIZEOF(buff), "Node_%d_IPv4", SelItem.SelNumber + 1);
ptrT dbIPv4(db_get_tsa(NULL, "TOX", buff));
if (dbIPv4 == NULL)
break;
mir_snprintf(buff, SIZEOF(buff), "Node_%d_IPv6", SelItem.SelNumber + 1);
ptrT dbIPv6(db_get_tsa(NULL, "TOX", buff));
if (dbIPv6 == NULL)
break;
mir_snprintf(buff, SIZEOF(buff), "Node_%d_ClientID", SelItem.SelNumber + 1);
ptrT dbClientID(db_get_tsa(NULL, "TOX", buff));
if (dbClientID == NULL)
break;
mir_snprintf(buff, SIZEOF(buff), "Node_%d_Port", SelItem.SelNumber + 1);
DWORD Port = db_get_dw(NULL, "TOX", buff, 0);
if (Port == 0)
break;
SetDlgItemText(hwndDlg, IDC_IPV4, dbIPv4);
SetDlgItemText(hwndDlg, IDC_IPV6, dbIPv6);
SetDlgItemText(hwndDlg, IDC_CLIENTID, dbClientID);
SetDlgItemInt(hwndDlg, IDC_PORT, Port, TRUE);
Utils_RestoreWindowPositionNoSize(hwndDlg, NULL, MODULE, "ChangeNodeDlg");
}
return TRUE;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDOK:
TCHAR str[MAX_PATH];
{
ItemInfo *SelItem = (ItemInfo*)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
if (!GetDlgItemText(hwndDlg, IDC_IPV4, str, SIZEOF(str))) {
MessageBox(hwndDlg, TranslateT("Enter IPv4"), TranslateT("Error"), MB_OK);
break;
}
if (!GetDlgItemText(hwndDlg, IDC_IPV6, str, SIZEOF(str))) {
MessageBox(hwndDlg, TranslateT("Enter IPv6"), TranslateT("Error"), MB_OK);
break;
}
if (!GetDlgItemInt(hwndDlg, IDC_PORT, NULL, false)) {
MessageBox(hwndDlg, TranslateT("Enter port"), TranslateT("Error"), MB_OK);
break;
}
if (!GetDlgItemText(hwndDlg, IDC_CLIENTID, str, SIZEOF(str))) {
MessageBox(hwndDlg, TranslateT("Enter client ID"), TranslateT("Error"), MB_OK);
break;
}
char buff[MAX_PATH];
GetDlgItemText(hwndDlg, IDC_IPV4, str, SIZEOF(str));
mir_snprintf(buff, SIZEOF(buff), "Node_%d_IPv4", SelItem->SelNumber + 1);
db_set_ts(NULL, "TOX", buff, str);
GetDlgItemText(hwndDlg, IDC_IPV6, str, SIZEOF(str));
mir_snprintf(buff, SIZEOF(buff), "Node_%d_IPv6", SelItem->SelNumber + 1);
db_set_ts(NULL, "TOX", buff, str);
GetDlgItemText(hwndDlg, IDC_CLIENTID, str, SIZEOF(str));
mir_snprintf(buff, SIZEOF(buff), "Node_%d_ClientID", SelItem->SelNumber + 1);
db_set_ts(NULL, "TOX", buff, str);
mir_snprintf(buff, SIZEOF(buff), "Node_%d_Port", SelItem->SelNumber + 1);
db_set_dw(NULL, "TOX", buff, (DWORD)GetDlgItemInt(hwndDlg, IDC_PORT, NULL, false));
DeleteAllItems(SelItem->hwndList);
UpdateList(SelItem->hwndList);
}
// fall through
case IDCANCEL:
DestroyWindow(hwndDlg);
break;
}
break;
case WM_CLOSE:
DestroyWindow(hwndDlg);
break;
case WM_DESTROY:
ItemInfo *SelItem = (ItemInfo *)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
hChangeNodeDlg = 0;
Utils_SaveWindowPosition(hwndDlg, NULL, MODULE, "ChangeNodeDlg");
delete SelItem;
break;
}
return FALSE;
}
INT_PTR CALLBACK ToxNodesOptionsProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
HWND hwndList = GetDlgItem(hwndDlg, IDC_NODESLIST);
int sel;
switch (msg) {
case WM_INITDIALOG:
TranslateDialogDefault(hwndDlg);
SetWindowLongPtr(hwndDlg, GWLP_USERDATA, 0);
CreateList(hwndList);
UpdateList(hwndList);
return TRUE;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDC_ADDNODE:
if (hAddNodeDlg == 0)
hAddNodeDlg = CreateDialogParam(g_hInstance, MAKEINTRESOURCE(IDD_ADDNODE), hwndDlg, AddNodeDlgProc, (LPARAM)hwndList);
return FALSE;
case IDC_CHANGE:
if (hChangeNodeDlg == 0) {
ItemInfo SelItem = { 0 };
SelItem.hwndList = hwndList;
SelItem.SelNumber = ListView_GetSelectionMark(hwndList);
hChangeNodeDlg = CreateDialogParam(g_hInstance, MAKEINTRESOURCE(IDD_ADDNODE), hwndDlg, ChangeNodeDlgProc, (LPARAM)&SelItem);
}
return FALSE;
case IDC_REMOVE:
if (MessageBox(hwndDlg, TranslateT("Are you sure?"), TranslateT("Node deleting"), MB_YESNO | MB_ICONWARNING) == IDYES) {
int sel = ListView_GetSelectionMark(hwndList);
char buff[MAX_PATH];
mir_snprintf(buff, SIZEOF(buff), "Node_%d_IPv4", sel + 1);
db_unset(NULL, "TOX", buff);
mir_snprintf(buff, SIZEOF(buff), "Node_%d_IPv6", sel + 1);
db_unset(NULL, "TOX", buff);
mir_snprintf(buff, SIZEOF(buff), "Node_%d_ClientID", sel + 1);
db_unset(NULL, "TOX", buff);
mir_snprintf(buff, SIZEOF(buff), "Node_%d_Port", sel + 1);
db_unset(NULL, "TOX", buff);
int NodeCount = db_get_b(NULL, "TOX", "NodeCount", 0);
for (int i = sel + 1; i < NodeCount; i++) {
mir_snprintf(buff, SIZEOF(buff), "Node_%d_IPv4", i + 1);
TCHAR *ptszIPv4 = db_get_tsa(NULL, "TOX", buff);
db_unset(NULL, "TOX", buff);
mir_snprintf(buff, SIZEOF(buff), "Node_%d_IPv4", i);
db_set_ts(NULL, "TOX", buff, ptszIPv4);
mir_snprintf(buff, SIZEOF(buff), "Node_%d_IPv6", i + 1);
TCHAR *ptszIPv6 = db_get_tsa(NULL, "TOX", buff);
db_unset(NULL, "TOX", buff);
mir_snprintf(buff, SIZEOF(buff), "Node_%d_IPv6", i);
db_set_ts(NULL, "TOX", buff, ptszIPv6);
mir_snprintf(buff, SIZEOF(buff), "Node_%d_ClientID", i + 1);
TCHAR *ptszClientID = db_get_tsa(NULL, "TOX", buff);
db_unset(NULL, "TOX", buff);
mir_snprintf(buff, SIZEOF(buff), "Node_%d_ClientID", i);
db_set_ts(NULL, "TOX", buff, ptszClientID);
mir_snprintf(buff, SIZEOF(buff), "Node_%d_Port", i + 1);
DWORD Port = db_get_dw(NULL, "TOX", buff, 0);
db_unset(NULL, "TOX", buff);
mir_snprintf(buff, SIZEOF(buff), "Node_%d_Port", i);
db_set_dw(NULL, "TOX", buff, Port);
}
db_set_b(NULL, "TOX", "NodeCount", NodeCount - 1);
ListView_DeleteItem(hwndList, sel);
}
return FALSE;
}
break;
case WM_NOTIFY:
NMHDR *hdr = (NMHDR *)lParam;
switch (hdr->code) {
case NM_DBLCLK:
sel = ListView_GetHotItem(hwndList);
if (sel != -1) {
ItemInfo SelItem = { 0 };
SelItem.hwndList = hwndList;
SelItem.SelNumber = sel;
CreateDialogParam(g_hInstance, MAKEINTRESOURCE(IDD_ADDNODE), hwndDlg, ChangeNodeDlgProc, (LPARAM)&SelItem);
}
break;
case LVN_ITEMCHANGED:
NMLISTVIEW *nmlv = (NMLISTVIEW *)lParam;
if (((nmlv->uNewState ^ nmlv->uOldState) & LVIS_STATEIMAGEMASK) && !UpdateListFlag)
SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0);
break;
}
}
return FALSE;
}
|