summaryrefslogtreecommitdiff
path: root/plugins/HTTPServer/src/GuiElements.cpp
blob: a3b60ea1445ad19ed9a49330dfd08e18f46e79ff (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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
//This file is part of HTTPServer a Miranda IM plugin
//Copyright (C)2002 Kennet Nielsen
//
//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., 675 Mass Ave, Cambridge, MA 02139, USA.


#include "Glob.h"

#define MS_SHARE_NEW_FILE "HTTPServer/ShareNewFile"
#define MS_SHOW_STATISTICS_VIEW "HTTPServer/ShowStatisticsView"

#define WM_RELOAD_STATISTICS (WM_USER+10)

static HANDLE hShareNewFileService = 0;
static HANDLE hShowStatisticsViewService = 0;

static HANDLE hShareNewFileMenuItem = 0;
static HANDLE hShowStatisticsViewMenuItem = 0;

static HANDLE hEventOptionsInitialize = 0;

HWND hwndStatsticView = 0;
bool bLastAutoRefress = false;
HANDLE hMainThread;

bool bShowPopups = true;

#define szDefaultExternalSrvName _T("http://%ExternalIP%:%Port%%SrvPath%")

#define szDefaultUrlAddress _T("http://checkip.dyndns.org")
#define szDefaultPageKeyword _T("Current IP Address: ")

tstring sUrlAddress = szDefaultUrlAddress;
tstring sPageKeyword = szDefaultPageKeyword;


/////////////////////////////////////////////////////////////////////
// Member Function : ReplaceAll
// Type            : Global
// Parameters      : sSrc       - string to replace in
//                   pszReplace - what to replace
//                   sNew       - the string to insert insted of pszReplace
// Returns         : void
// Description     : will replace all acurances of a string with another string
//                   used to replace %user%, and other user
// References      : -
// Remarks         : -
// Created         : 020422, 22 April 2002
// Developer       : KN
/////////////////////////////////////////////////////////////////////

void ReplaceAll(tstring &sSrc, const TCHAR * pszReplace, const tstring &sNew) {
	tstring::size_type nCur = 0;
	int nRepalceLen = (int)_tcslen(pszReplace);
	while ((nCur = sSrc.find(pszReplace, nCur)) != sSrc.npos) {
		sSrc.replace(nCur, nRepalceLen, sNew);
		nCur += sNew.size();
	}
}

void ReplaceAll(tstring &sSrc, const TCHAR * pszReplace, const TCHAR * pszNew) {
	tstring sNew = pszNew;
	ReplaceAll(sSrc, pszReplace, sNew);
}


/////////////////////////////////////////////////////////////////////
// Member Function : DBGetString
// Type            : Global
// Parameters      : hContact  - ?
//                   szModule  - ?
//                   szSetting - ?
//                   pszError  - ?
// Returns         : string
// Description     : Reads a string from the database
//                   Just like those in database.h
// References      : -
// Remarks         : -
// Created         : 020422, 22 April 2002
// Developer       : KN
/////////////////////////////////////////////////////////////////////

tstring DBGetString(MCONTACT hContact, const char *szModule, const char *szSetting, const TCHAR * pszError) {
	tstring ret;
	ptrT tszStr(db_get_tsa(hContact, szModule, szSetting));
	if (tszStr != NULL)
		return LPTSTR(tszStr);

	MessageBox(NULL, _T("DB: Attempt to get wrong type of value, string"), MSG_BOX_TITEL, MB_OK);
	return pszError;
}


/////////////////////////////////////////////////////////////////////
// Member Function : UpdateStatisticsView
// Type            : Global
// Parameters      : None
// Returns         : void
// Description     :
//
// References      : -
// Remarks         : -
// Created         : 030904, 04 september 2003
// Developer       : KN
/////////////////////////////////////////////////////////////////////

void UpdateStatisticsView() {
	if (hwndStatsticView) {
		PostMessage(hwndStatsticView, WM_RELOAD_STATISTICS, 0, 0);
	}
}


/////////////////////////////////////////////////////////////////////
// Member Function : GetExternIP
// Type            : Global
// Parameters      : szURL     - The url of the page where IP is written
//                   szPattern - The text before the IP in page (can be "")
// Returns         : External IP or 0 if error
// Description     :
//
// References      : -
// Remarks         : -
// Created         : 031113, 13 november 2003
// Developer       : KN
/////////////////////////////////////////////////////////////////////

unsigned long GetExternIP(const char *szURL, const char *szPattern) {
	HCURSOR hPrevCursor = ::SetCursor(::LoadCursor(0, IDC_WAIT));

	NETLIBHTTPREQUEST nlhr = { 0 };
	nlhr.cbSize = sizeof(nlhr);
	nlhr.requestType = REQUEST_GET;
	nlhr.flags = NLHRF_DUMPASTEXT;
	nlhr.szUrl = (char*)szURL;

	IN_ADDR externIP;
	externIP.s_addr = 0;

	NETLIBHTTPREQUEST *nlreply = (NETLIBHTTPREQUEST *) CallService(MS_NETLIB_HTTPTRANSACTION, (WPARAM) hNetlibUser, (LPARAM) & nlhr);
	if (nlreply) {
		if (nlreply->resultCode >= 200 && nlreply->resultCode < 300) {
			nlreply->pData[nlreply->dataLength] = 0;// make sure its null terminated
			char * pszIp = strstr(nlreply->pData, szPattern);
			if (pszIp == NULL)
				pszIp = nlreply->pData;
			else
				pszIp += strlen(szPattern);
			while ((*pszIp < '0' || *pszIp > '9') && *pszIp)
				pszIp++;

			char * pszEnd = pszIp;
			while ((*pszEnd >= '0' && *pszEnd <= '9') || *pszEnd == '.')
				pszEnd++;
			*pszEnd = NULL;

			if ((externIP.s_addr = inet_addr(pszIp)) == INADDR_NONE)
				externIP.s_addr = 0;
		}
		CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM) nlreply);
	}
	::SetCursor(hPrevCursor);
	return ntohl(externIP.s_addr);
}



/////////////////////////////////////////////////////////////////////
// Member Function : sCreateLink
// Type            : Global
// Parameters      : pszSrvPath - ?
//                   pszIndexPath - ?
// Returns         : string
// Description     :
//
// References      : -
// Remarks         : -
// Created         : 030915, 15 september 2003
// Developer       : KN
/////////////////////////////////////////////////////////////////////

tstring sCreateLink(const TCHAR * pszSrvPath) {
	TCHAR szTemp[30];
	tstring sLink = DBGetString(NULL, MODULE, "ExternalSrvName", szDefaultExternalSrvName);
	mir_sntprintf(szTemp, SIZEOF(szTemp), _T("%d.%d.%d.%d"), SplitIpAddress(dwLocalIpAddress));
	ReplaceAll(sLink, _T("%LocalIP%"), szTemp);

	if (sLink.find(_T("%ExternalIP%")) != sLink.npos) {
		static DWORD dwExternalIpAddressGenerated = 0;

		// Get the IP again after 10 minutes
		if (! dwExternalIpAddress || GetTickCount() - dwExternalIpAddressGenerated > 10 * 60 * 1000) {
			dwExternalIpAddress = GetExternIP(_T2A(sUrlAddress.c_str()), _T2A(sPageKeyword.c_str()));

			dwExternalIpAddressGenerated = GetTickCount();
		}

		mir_sntprintf(szTemp, SIZEOF(szTemp), _T("%d.%d.%d.%d"), SplitIpAddress(dwExternalIpAddress));
		ReplaceAll(sLink, _T("%ExternalIP%"), szTemp);
	}

	mir_sntprintf(szTemp, SIZEOF(szTemp), _T("%d"), dwLocalPortUsed);
	ReplaceAll(sLink, _T("%Port%"), szTemp);

	tstring sSrvPath = pszSrvPath;
	ReplaceAll(sSrvPath, _T(" "), _T("%20"));
	ReplaceAll(sLink, _T("%SrvPath%"), sSrvPath);
	return sLink;
}

/////////////////////////////////////////////////////////////////////
// Member Function : ShareNewFileDialogHook
// Type            : Global
// Parameters      : hdlg      - ?
//                   uiMsg     - ?
//                   wParam    - ?
//                   parameter - ?
// Returns         : UINT_PTR CALLBACK
// Description     :
//
// References      : -
// Remarks         : -
// Created         : 030829, 29 august 2003
// Developer       : KN, Houdini
/////////////////////////////////////////////////////////////////////

UINT_PTR CALLBACK ShareNewFileDialogHook(
  HWND hDlg,      // handle to child dialog box
  UINT uiMsg,     // message identifier
  WPARAM wParam,  // message parameter
  LPARAM lParam   // message parameter
) {
	static const TCHAR* pszShareDirStr = TranslateT("Share Current Directory");

	static int nInit = 0;

	STFileShareInfo * pstShare = (STFileShareInfo *)GetWindowLongPtr(hDlg, GWLP_USERDATA);
	switch (uiMsg) {
		case WM_INITDIALOG: {
			pstShare = (STFileShareInfo *)((OPENFILENAME *)lParam)->lCustData;
			SetWindowLongPtr(hDlg, GWLP_USERDATA, (LONG_PTR)pstShare);

			SetDlgItemInt(hDlg, IDC_MAX_DOWNLOADS, pstShare->nMaxDownloads, true);
			SendMessage(GetDlgItem(hDlg, IDC_ALLOWED_IPADDRESS), IPM_SETADDRESS,  0, pstShare->dwAllowedIP);
			SendMessage(GetDlgItem(hDlg, IDC_ALLOWED_IP_MASK), IPM_SETADDRESS,  0, pstShare->dwAllowedMask);

			if (*pstShare->pszSrvPath)
				nInit = 2;
			else
				nInit = 1;

			return false;
		}

		case WM_NOTIFY: {
			OFNOTIFY * pNotify = (OFNOTIFY*)lParam;
			switch (pNotify->hdr.code) {
				case CDN_FOLDERCHANGE:
				case CDN_SELCHANGE: {
					static TCHAR szSelection[MAX_PATH] = _T("");
					HWND hWndFileDlg = GetParent(hDlg);

					*szSelection = '/';
					CommDlg_OpenSave_GetSpec(hWndFileDlg, (LPARAM)(&szSelection[1]), _MAX_PATH);

					HWND hFileName = GetDlgItem(hWndFileDlg, edt1);
					TCHAR pszFileName[MAX_PATH];
					SendMessage(hFileName, WM_GETTEXT, MAX_PATH, (LPARAM)pszFileName);

					if (_tcscmp(pstShare->pszSrvPath, szSelection) &&
					    _tcscmp(pszFileName, pszShareDirStr)) {
						// a file was selected

						// only reenable windows / set default values when a folder was selected before
						if (pstShare->pszSrvPath[_tcslen(pstShare->pszSrvPath)-1] == '/') {
							pNotify->lpOFN->Flags |= OFN_FILEMUSTEXIST;
							EnableWindow(hFileName, TRUE);
							EnableWindow(GetDlgItem(hDlg, IDC_MAX_DOWNLOADS), TRUE);
							SetDlgItemInt(hDlg, IDC_MAX_DOWNLOADS, nDefaultDownloadLimit, true);								
						}
					} else {
						// a directory was selected
						pNotify->lpOFN->Flags &= ~OFN_FILEMUSTEXIST;
						_tcscpy(pNotify->lpOFN->lpstrFile, pszShareDirStr);
						CommDlg_OpenSave_SetControlText(hWndFileDlg, edt1, pszShareDirStr);
						EnableWindow(hFileName, FALSE);
						EnableWindow(GetDlgItem(hDlg, IDC_MAX_DOWNLOADS), FALSE);
						SetDlgItemInt(hDlg, IDC_MAX_DOWNLOADS, (UINT)-1, true);							

						CommDlg_OpenSave_GetFolderPath(hWndFileDlg, szSelection, MAX_PATH);
						TCHAR* pszFolder = szSelection;
						TCHAR* pszTmp    = szSelection;
						while (*pszTmp != '\0') {
							if (*pszTmp == '\\' && *(pszTmp + 1))
								pszFolder = pszTmp + 1;
							pszTmp++;
						}

						pszTmp = _tcschr(szSelection, ':');
						if (pszTmp != NULL)
							*pszTmp = '\0';

						memmove(&szSelection[1], pszFolder, _tcslen(pszFolder) + 1);
						szSelection[0] = '/';
						if (szSelection[_tcslen(szSelection)-1] != '/')
							_tcscat(szSelection, _T("/"));

						// only write to IDC_SHARE_NAME when a file / other folder was selected before
						if (!_tcscmp(szSelection, pstShare->pszSrvPath))
							return false;
					}

					if (nInit != 0) {
						// when the dialog is created a CDN_FOLDERCHANGE and a CDN_SELCHANGE message
						// is posted. When the dialog is used for editting make sure the right server
						// path (not the auto generated) is written to IDC_SHARE_NAME
						if (nInit == 2)
							SetDlgItemText(hDlg, IDC_SHARE_NAME, pstShare->pszSrvPath);

						nInit--;
					} else {
						SetDlgItemText(hDlg, IDC_SHARE_NAME, szSelection);
					}

					_tcscpy(pstShare->pszSrvPath, szSelection);

					return false;
				}

				case CDN_FILEOK: {
					GetDlgItemText(hDlg, IDC_SHARE_NAME, pstShare->pszSrvPath, _MAX_PATH);

					TCHAR* pszTmp = _tcsstr(pstShare->pszRealPath, pszShareDirStr);
					if (pszTmp) {
						*pszTmp = '\0';
						if (pstShare->pszSrvPath[_tcslen(pstShare->pszSrvPath)-1] != '/')
							_tcscat(pstShare->pszSrvPath, _T("/"));
					} else {
						if (pstShare->pszSrvPath[_tcslen(pstShare->pszSrvPath)-1] == '/')
							pstShare->pszSrvPath[_tcslen(pstShare->pszSrvPath)-1] = '\0';
					}

					BOOL bTranslated = false;
					pstShare->nMaxDownloads = GetDlgItemInt(hDlg, IDC_MAX_DOWNLOADS, &bTranslated, true);
					if (pstShare->nMaxDownloads <= 0 && pstShare->nMaxDownloads != -1)
						bTranslated = false;

					SendMessage(GetDlgItem(hDlg, IDC_ALLOWED_IPADDRESS), IPM_GETADDRESS,  0, (LPARAM)&(pstShare->dwAllowedIP));
					SendMessage(GetDlgItem(hDlg, IDC_ALLOWED_IP_MASK), IPM_GETADDRESS,  0, (LPARAM)&(pstShare->dwAllowedMask));

					//if( ! (pstShare->dwAllowedIP & pstShare->dwAllowedMask)

					if (!bTranslated || (_tcslen(pstShare->pszSrvPath) <= 0)) {
						SetWindowLongPtr(hDlg, DWLP_MSGRESULT, 1);
						return true;
					}
					return false;
				}
			}
			break;
		}

		case WM_DROPFILES: {
			HDROP hDrop = (HDROP)wParam;
			TCHAR szDropedFile[MAX_PATH];
			int nLen = DragQueryFile(hDrop, 0, szDropedFile, SIZEOF(szDropedFile));
			if (nLen > 0) {
				TCHAR * psz = _tcsrchr(szDropedFile, '\\');
				if (psz) {
					TCHAR oldNext = psz[1];
					psz[1] = '\0';
					// Fill in the directory
					SendMessage(GetParent(hDlg), CDM_SETCONTROLTEXT, edt1, (LPARAM)szDropedFile);
					// click on the OK button. to move to dir
					SendMessage(GetParent(hDlg), WM_COMMAND, IDOK, 0);
					psz[1] = oldNext;
					// Fill in the file name this will cause a call to the CDN_SELCHANGE
					// and there by set the share name
					SendMessage(GetParent(hDlg), CDM_SETCONTROLTEXT, edt1, (LPARAM)&psz[1]);
				}
			}
			DragFinish(hDrop);
			return 0;
		}

		case WM_COMMAND: {
			switch (LOWORD(wParam)) {
				case IDC_TOGGLE_MASK: {
						DWORD dwCur;
						SendMessage(GetDlgItem(hDlg, IDC_ALLOWED_IP_MASK), IPM_GETADDRESS,  0, (LPARAM)&dwCur);
						SendMessage(GetDlgItem(hDlg, IDC_ALLOWED_IP_MASK), IPM_SETADDRESS,  0, (LPARAM) dwCur == 0xFFFFFFFF ? 0 : 0xFFFFFFFF);
						return TRUE;
					}
			}
		}
	}
	return false;
}


/////////////////////////////////////////////////////////////////////
// Member Function : bShowShareNewFileDlg
// Type            : Global
// Parameters      : pstNewShare - ?
// Returns         : Returns true if
// Description     :
//
// References      : -
// Remarks         : -
// Created         : 031011, 11 oktober 2003
// Developer       : KN, Houdini
/////////////////////////////////////////////////////////////////////

bool bShowShareNewFileDlg(HWND hwndOwner, STFileShareInfo * pstNewShare) {
	OPENFILENAME ofn = {0};
	ofn.lStructSize = sizeof(OPENFILENAME);

	TCHAR temp[MAX_PATH];
	mir_sntprintf(temp, SIZEOF(temp), _T("%s (*.*)%c*.*%c%c"), TranslateT("All files"), 0, 0, 0);
	ofn.lpstrFilter = temp;

	ofn.lpstrFile = pstNewShare->pszRealPath;
	ofn.nMaxFile = pstNewShare->dwMaxRealPath;

	TCHAR szInitialDir[MAX_PATH];
	if (ofn.lpstrFile[_tcslen(ofn.lpstrFile)-1] == '\\') {
		ofn.lpstrInitialDir = szInitialDir;
		_tcscpy(szInitialDir, ofn.lpstrFile);
		*ofn.lpstrFile = '\0';
	}

	
	ofn.Flags = /*OFN_DONTADDTORECENT |*/ OFN_NOREADONLYRETURN 
	    | OFN_ENABLEHOOK | OFN_ENABLETEMPLATE | OFN_EXPLORER | OFN_ENABLESIZING
			| OFN_ALLOWMULTISELECT;
	ofn.hwndOwner = hwndOwner;
	ofn.hInstance = hInstance;
	ofn.lpstrTitle = TranslateT("Specify a file to share");
	ofn.lpTemplateName =  MAKEINTRESOURCE(IDD_NEW_SHARE_PROPERTIES);
	ofn.lpfnHook = ShareNewFileDialogHook;
	ofn.lCustData = (LPARAM)pstNewShare;

	if (!GetOpenFileName(&ofn)) {
		DWORD dwError = CommDlgExtendedError();
		if (dwError) {
			TCHAR szTemp[200];
			mir_sntprintf(szTemp, SIZEOF(szTemp), _T("Failed to create File Open dialog the error returned was %d"), dwError);
			MessageBox(NULL, szTemp, MSG_BOX_TITEL, MB_OK);
		}
		return false;
	}

	if (_tcschr(pstNewShare->pszSrvPath, '"')) {
		// multiple files selected
		// Serverpath: "file1" "file2" "file3"
		// move one after the other to front of string (in place)
		// terminate it with \0 append to realpath and add the share		
		TCHAR *pszFileNamePos = pstNewShare->pszSrvPath;
		TCHAR *szRealDirectoryEnd = 
			&pstNewShare->pszRealPath[_tcslen(pstNewShare->pszRealPath)];

		*szRealDirectoryEnd = '\\';
		szRealDirectoryEnd++;
		
		while (pszFileNamePos && *pszFileNamePos) {			
			pszFileNamePos = _tcschr(pszFileNamePos, '"');
			if (pszFileNamePos) {
				pszFileNamePos++;
				TCHAR* start = pszFileNamePos;
				pszFileNamePos = _tcschr(pszFileNamePos, '"');
				if (pszFileNamePos) {					
					TCHAR* end = pszFileNamePos;
					memmove(pstNewShare->pszSrvPath+1, start, end - start);
					*(end - (start - (pstNewShare->pszSrvPath+1)) ) = '\0';
					
					int realPathLen = szRealDirectoryEnd - pstNewShare->pszRealPath;
					_tcsncpy(szRealDirectoryEnd, pstNewShare->pszSrvPath+1, 
						pstNewShare->dwMaxRealPath - realPathLen - 1);
					pstNewShare->pszRealPath[pstNewShare->dwMaxRealPath] = '\0';

					if (CallService(MS_HTTP_ADD_CHANGE_REMOVE, 0, (LPARAM)pstNewShare)) {
						MessageBox(NULL, TranslateT("Failed to share new file"), MSG_BOX_TITEL, MB_OK);
						return false;
					}
					pszFileNamePos++;
				}		
			}
		}

	} else {
		if (CallService(MS_HTTP_ADD_CHANGE_REMOVE, 0, (LPARAM)pstNewShare)) {
			MessageBox(NULL, TranslateT("Failed to share new file"), MSG_BOX_TITEL, MB_OK);
			return false;
		}
	}

	UpdateStatisticsView();
	return true;
}

/////////////////////////////////////////////////////////////////////
// Member Function : UpdateStatisticView
// Type            : Global
// Parameters      : hwndDlg - ?
//                   false   - ?
// Returns         : void
// Description     :
//
// References      : -
// Remarks         : -
// Created         : 030904, 04 september 2003
// Developer       : KN, Houdini
/////////////////////////////////////////////////////////////////////

void UpdateStatisticView(HWND hwndDlg, bool bRefressUsersOnly = false) {
	HWND hShareList = GetDlgItem(hwndDlg, IDC_CURRENT_SHARES);
	HWND hUserList = GetDlgItem(hwndDlg, IDC_CURRENT_USERS);
	bool bShowHiddenShares = IsDlgButtonChecked(hwndDlg, IDC_SHOWHIDDENSHARES) == BST_CHECKED;

	if (! bRefressUsersOnly)
		ListView_DeleteAllItems(hShareList);
	ListView_DeleteAllItems(hUserList);

	CLFileShareListAccess scCrit;

	TCHAR szTmp[50];
	in_addr stAddr;

	bool bAutoRefress = false;

	LVITEM sItem = { 0 };
	int nShareNr = 0;
	int nUserNr = 0;
	for (CLFileShareNode * pclCur = pclFirstNode; pclCur ; pclCur = pclCur->pclNext) {
		if (! bRefressUsersOnly &&
		    (bShowHiddenShares || !_tcsstr(pclCur->st.pszRealPath, _T("\\@")))) {
			sItem.mask = LVIF_TEXT /*| LVIF_PARAM | LVIF_IMAGE*/;
			sItem.iItem = nShareNr;
			sItem.iSubItem = 0;
			sItem.pszText = pclCur->st.pszSrvPath;
			ListView_InsertItem(hShareList, &sItem);


			mir_sntprintf(szTmp, SIZEOF(szTmp), _T("%d"), pclCur->st.nMaxDownloads);
			sItem.iSubItem = 1;
			sItem.pszText = szTmp;
			ListView_SetItem(hShareList, &sItem);

			stAddr.S_un.S_addr = htonl(pclCur->st.dwAllowedIP);
			sItem.iSubItem = 2;
			sItem.pszText = _A2T(inet_ntoa(stAddr));
			ListView_SetItem(hShareList, &sItem);

			stAddr.S_un.S_addr = htonl(pclCur->st.dwAllowedMask);
			sItem.iSubItem = 3;
			sItem.pszText = _A2T(inet_ntoa(stAddr));
			ListView_SetItem(hShareList, &sItem);

			sItem.iSubItem = 4;
			sItem.pszText = pclCur->st.pszRealPath;
			ListView_SetItem(hShareList, &sItem);

			nShareNr++;
		}

		for (CLShareUser * pclCurUser = pclCur->pclGetUsers() ; pclCurUser ; pclCurUser = pclCurUser->pclNext) {
			bAutoRefress = true;

			sItem.mask = LVIF_TEXT /*| LVIF_PARAM | LVIF_IMAGE*/;
			sItem.iItem = nUserNr;
			sItem.iSubItem = 0;
			sItem.pszText = pclCurUser->szCurrentDLSrvPath; //pclCur->st.pszSrvPath;
			ListView_InsertItem(hUserList, &sItem);

			sItem.iSubItem = 1;
			sItem.pszText = _A2T(inet_ntoa(pclCurUser->stAddr));
			ListView_SetItem(hUserList, &sItem);

			sItem.iSubItem = 2;
			sItem.pszText = (TCHAR *)pclCurUser->pszCustomInfo();
			ListView_SetItem(hUserList, &sItem);

			if (pclCurUser->dwTotalSize) {
				mir_sntprintf(szTmp, SIZEOF(szTmp), _T("%d %%"), (pclCurUser->dwCurrentDL * 100) / pclCurUser->dwTotalSize);
			} else {
				_tcscpy(szTmp, _T("? %%"));
			}
			sItem.iSubItem = 3;
			sItem.pszText = szTmp;
			ListView_SetItem(hUserList, &sItem);

			DWORD dwSpeed = pclCurUser->dwGetDownloadSpeed();
			if (dwSpeed > 10000) {
				dwSpeed += 512; // make sure we round ot down correctly.
				dwSpeed /= 1024;
				mir_sntprintf(szTmp, SIZEOF(szTmp), _T("%d KB/Sec"), dwSpeed);
			} else {
				mir_sntprintf(szTmp, SIZEOF(szTmp), _T("%d B/Sec"), dwSpeed);
			}
			sItem.iSubItem = 4;
			sItem.pszText = szTmp;
			ListView_SetItem(hUserList, &sItem);

			nUserNr++;
		}
	}

	if (bLastAutoRefress !=  bAutoRefress) {
		if (bAutoRefress)
			SetTimer(hwndDlg, 0, 1000, NULL);
		else
			KillTimer(hwndDlg, 0);
		bLastAutoRefress = bAutoRefress;
	}
}

/////////////////////////////////////////////////////////////////////
// Member Function : SetWindowsCtrls
// Type            : Global
// Parameters      : hwndDlg - ?
// Returns         : void
// Description     :
//
// References      : -
// Remarks         : -
// Created         : 030904, 04 september 2003
// Developer       : KN
/////////////////////////////////////////////////////////////////////

void SetWindowsCtrls(HWND hwndDlg) {
	RECT rNewSize;
	GetClientRect(hwndDlg, &rNewSize);

	const int nSpacing = 8;
	int nCtrlHight = (rNewSize.bottom - (nSpacing * 3)) / 3 - 20;

	SetWindowPos(GetDlgItem(hwndDlg, IDC_CURRENT_SHARES), 0 ,
	    nSpacing,
	    35,
	    rNewSize.right - (nSpacing * 2) ,
	    nCtrlHight*2,
	    SWP_NOZORDER);

	SetWindowPos(GetDlgItem(hwndDlg, IDC_CURRENT_USERS), 0 ,
	    nSpacing ,
	    (nSpacing * 2) + nCtrlHight*2 + 25,
	    rNewSize.right - (nSpacing * 2) ,
	    nCtrlHight + 35,
	    SWP_NOZORDER);
}

/////////////////////////////////////////////////////////////////////
// Member Function : DlgProcStatsticView
// Type            : Global
// Parameters      : hwndDlg - ?
//                   msg     - ?
//                   wParam  - ?
//                   lParam  - ?
// Returns         : static BOOL CALLBACK
// Description     :
//
// References      : -
// Remarks         : -
// Created         : 030904, 04 september 2003
// Developer       : KN, Houdini
/////////////////////////////////////////////////////////////////////

static INT_PTR CALLBACK DlgProcStatsticView(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) {
	switch (msg) {
		case WM_INITDIALOG: {
			SendMessage(hwndDlg, WM_SETICON, ICON_BIG,
			    (LPARAM)LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SHARE_NEW_FILE)));

			TranslateDialogDefault(hwndDlg);

			HWND hShareList = GetDlgItem(hwndDlg, IDC_CURRENT_SHARES);
			HWND hUserList = GetDlgItem(hwndDlg, IDC_CURRENT_USERS);

			{ // init adv. win styles
				DWORD dw = ListView_GetExtendedListViewStyle(hShareList);
				dw |= LVS_EX_HEADERDRAGDROP | LVS_EX_FULLROWSELECT;
				ListView_SetExtendedListViewStyle(hShareList, dw /*| LVS_EX_LABELTIP*/);
			}
			{ // init adv. win styles
				DWORD dw = ListView_GetExtendedListViewStyle(hUserList);
				dw |= LVS_EX_HEADERDRAGDROP | LVS_EX_FULLROWSELECT;
				ListView_SetExtendedListViewStyle(hUserList, dw /*| LVS_EX_LABELTIP*/);
			}

			LVCOLUMN cCol = { 0 };
			cCol.mask = LVCF_TEXT | LVCF_WIDTH;
			cCol.cx = db_get_w(NULL, MODULE, "StatWnd_cx1", 126);
			cCol.pszText = TranslateT("Share name");
			ListView_InsertColumn(hShareList, 0, &cCol);
			cCol.cx = db_get_w(NULL, MODULE, "StatWnd_cx2", 48);
			cCol.pszText = TranslateT("Max Downloads");
			ListView_InsertColumn(hShareList, 1, &cCol);
			cCol.cx = db_get_w(NULL, MODULE, "StatWnd_cx3", 96);
			cCol.pszText = TranslateT("Allowed IP");
			ListView_InsertColumn(hShareList, 2, &cCol);
			cCol.cx = db_get_w(NULL, MODULE, "StatWnd_cx4", 104);
			cCol.pszText = TranslateT("Allowed Mask");
			ListView_InsertColumn(hShareList, 3, &cCol);
			cCol.cx = db_get_w(NULL, MODULE, "StatWnd_cx5", 252);
			cCol.pszText = TranslateT("Real path");
			ListView_InsertColumn(hShareList, 4, &cCol);

			cCol.mask = LVCF_TEXT | LVCF_WIDTH;
			cCol.cx = db_get_w(NULL, MODULE, "StatWnd_cx6", 142);
			cCol.pszText = TranslateT("Share name");
			ListView_InsertColumn(hUserList, 0, &cCol);
			cCol.cx = db_get_w(NULL, MODULE, "StatWnd_cx7", 111);
			cCol.pszText = TranslateT("User");
			ListView_InsertColumn(hUserList, 1, &cCol);
			cCol.cx = db_get_w(NULL, MODULE, "StatWnd_cx8", 100);
			cCol.pszText = TranslateT("Agent");
			ListView_InsertColumn(hUserList, 2, &cCol);
			cCol.cx = db_get_w(NULL, MODULE, "StatWnd_cx9", 100);
			cCol.pszText = TranslateT("Completed");
			ListView_InsertColumn(hUserList, 3, &cCol);
			cCol.cx = db_get_w(NULL, MODULE, "StatWnd_cx10", 100);
			cCol.pszText = TranslateT("Speed");
			ListView_InsertColumn(hUserList, 4, &cCol);

			bool b = db_get_b(NULL, MODULE, "StatWnd_ShowHidden", 0) != 0;
			CheckDlgButton(hwndDlg, IDC_SHOWHIDDENSHARES, b ? BST_CHECKED : BST_UNCHECKED);

			bLastAutoRefress = false;
			UpdateStatisticView(hwndDlg);
			Utils_RestoreWindowPosition(hwndDlg, 0, MODULE, "StatWnd_");
			SetWindowsCtrls(hwndDlg);
			return TRUE;
		}

		case WM_SIZE:
		case WM_SIZING: {
			SetWindowsCtrls(hwndDlg);
			return TRUE;
		}

		case WM_TIMER: {
			UpdateStatisticView(hwndDlg, true);
			return TRUE;
		}

		case WM_RELOAD_STATISTICS: {
			UpdateStatisticView(hwndDlg);
			return TRUE;
		}

		case WM_DESTROY: {
			hwndStatsticView = NULL;
			return 0;
		}

		case WM_DROPFILES: {
			HDROP hDrop = (HDROP)wParam;
			TCHAR szDropedFile[MAX_PATH];
			TCHAR szServPath[MAX_PATH] = {0};

			int nLen = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0);
			for (int i = 0; i < nLen; i++) {
				DragQueryFile(hDrop, i, szDropedFile, SIZEOF(szDropedFile));

				STFileShareInfo stNewShare = {0};
				stNewShare.lStructSize = sizeof(STFileShareInfo);
				stNewShare.nMaxDownloads = nDefaultDownloadLimit;
				stNewShare.pszRealPath = szDropedFile;
				stNewShare.dwMaxRealPath = SIZEOF(szDropedFile);
				stNewShare.pszSrvPath = szServPath;
				stNewShare.dwMaxSrvPath = SIZEOF(szServPath);

				szServPath[0] = '/';
				TCHAR* fileName = _tcsrchr(szDropedFile, '\\');
				if (fileName)
					_tcsncpy(&szServPath[1], fileName+1, MAX_PATH-2);

				if (CallService(MS_HTTP_ADD_CHANGE_REMOVE, 0, (LPARAM)&stNewShare)) {
					MessageBox(NULL, TranslateT("Failed to share new file"), MSG_BOX_TITEL, MB_OK);
					return false;
				}			
			}

			UpdateStatisticsView();
			DragFinish(hDrop);
			return 0;
		}

		case WM_CONTEXTMENU: {
			if (wParam != (WPARAM)GetDlgItem(hwndDlg, IDC_CURRENT_SHARES))
				return FALSE;

			HWND hShareList = GetDlgItem(hwndDlg, IDC_CURRENT_SHARES);
			if (ListView_GetNextItem(hShareList, -1, LVIS_SELECTED) == -1) 
				return FALSE;

			HMENU hMainMenu = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_MENU1));
			if (hMainMenu) {
				HMENU hMenu = GetSubMenu(hMainMenu, 0);

				POINT pt;
				pt.x = (short)LOWORD(lParam);
				pt.y = (short)HIWORD(lParam);
				if (pt.x == -1 && pt.y == -1) {
					HWND hMapUser = GetDlgItem(hwndDlg, IDC_CURRENT_SHARES);
					int nFirst = ListView_GetNextItem(hMapUser, -1, LVNI_FOCUSED);
					if (nFirst >= 0) {
						ListView_GetItemPosition(hMapUser, nFirst, &pt);
					}

					if (pt.y < 16)
						pt.y = 16;
					else {
						RECT rUserList;
						GetClientRect(hMapUser, &rUserList);
						if (pt.y > rUserList.bottom - 16)
							pt.y = rUserList.bottom - 16;
						else
							pt.y += 8;
					}
					pt.x = 8;
					ClientToScreen(hMapUser, &pt);
				}

				TranslateMenu(hMenu);
				TrackPopupMenu(hMenu, TPM_TOPALIGN | TPM_LEFTALIGN | TPM_RIGHTBUTTON, 
					pt.x, pt.y, 0, hwndDlg, NULL);

				DestroyMenu(hMainMenu);
			}
			return TRUE;
		}

		case WM_COMMAND: {
			HWND hShareList = GetDlgItem(hwndDlg, IDC_CURRENT_SHARES);
			TCHAR szTmp[MAX_PATH];
			LVITEM sItem = { 0 };
			sItem.mask = LVIF_TEXT;
			sItem.pszText = szTmp;
			sItem.cchTextMax = SIZEOF(szTmp);


			switch (LOWORD(wParam)) {
				case IDC_SHOWHIDDENSHARES: {
					UpdateStatisticView(hwndDlg);
					return TRUE;
				}

				case ID_SHARELIST_NEWSHARE: {
					CallService(MS_SHARE_NEW_FILE, 0, (long)hwndDlg);
					return TRUE;
				}

				case ID_SHARELIST_EDITSHARE:
				case ID_SHARELIST_REMOVESHARE: {
					STFileShareInfo stShareInfo = {0};
					stShareInfo.lStructSize = sizeof(STFileShareInfo);
					stShareInfo.pszSrvPath = szTmp;
					stShareInfo.dwMaxSrvPath = SIZEOF(szTmp);

					sItem.iItem = ListView_GetNextItem(hShareList, -1, LVIS_SELECTED);
					while (sItem.iItem != -1) {
						if (ListView_GetItem(hShareList, &sItem)) {
							if (LOWORD(wParam) == ID_SHARELIST_REMOVESHARE) {
								CallService(MS_HTTP_ADD_CHANGE_REMOVE, 0, (LPARAM)&stShareInfo);
							} else {
								TCHAR szRealPath[MAX_PATH];
								stShareInfo.pszRealPath = szRealPath;
								stShareInfo.dwMaxRealPath = SIZEOF(szRealPath);
								CallService(MS_HTTP_GET_SHARE, 0, (LPARAM)&stShareInfo);
								bShowShareNewFileDlg(hwndDlg, &stShareInfo);
							}
						}
						sItem.iItem = ListView_GetNextItem(hShareList, sItem.iItem, LVIS_SELECTED);
					}
					UpdateStatisticView(hwndDlg);
					return TRUE;
				}

				case ID_SHARELIST_OPEN:
				case ID_SHARELIST_COPYLINK: {
					sItem.iItem = ListView_GetNextItem(hShareList, -1, LVIS_SELECTED);
					if (sItem.iItem != -1) {
						if (ListView_GetItem(hShareList, &sItem)) {
							tstring sLink = sCreateLink(sItem.pszText);
							if (sLink.size() <= 0) {
								MessageBox(hwndDlg, TranslateT("Selected link size is 0"), MSG_BOX_TITEL, MB_OK);
								return TRUE;
							}

							if (LOWORD(wParam) == ID_SHARELIST_COPYLINK) {
								if (!OpenClipboard(hwndDlg)) {
									MessageBox(hwndDlg, TranslateT("Failed to get access to clipboard"), MSG_BOX_TITEL, MB_OK);
									return TRUE;
								}

								if (!EmptyClipboard()) {
									MessageBox(hwndDlg, TranslateT("Failed to get close the clipboard"), MSG_BOX_TITEL, MB_OK);
									return TRUE;
								}

								HGLOBAL hglbCopy = GlobalAlloc(GMEM_MOVEABLE, sLink.size() + 1);
								// Lock the handle and copy the text to the buffer.
								TCHAR *lptstrCopy = (TCHAR *)GlobalLock(hglbCopy);
								_tcscpy(lptstrCopy, sLink.c_str());
								GlobalUnlock(hglbCopy);

								// Place the handle on the clipboard.

								HANDLE hMyData = SetClipboardData(CF_TEXT, hglbCopy);
								if (! hMyData)
									MessageBox(hwndDlg, TranslateT("Failed to set clipboard data"), MSG_BOX_TITEL, MB_OK);

								CloseClipboard();
							} else {
								CallService(MS_UTILS_OPENURL, 0, (LPARAM)(const char*)sLink.c_str());
							}
						} else {
							MessageBox(hwndDlg, TranslateT("ListView_GetItem failed"), MSG_BOX_TITEL, MB_OK);
						}
					} else {
						MessageBox(hwndDlg, TranslateT("No share selected"), MSG_BOX_TITEL, MB_OK);
					}
					return TRUE;
				}
				/*
				case IDCANCEL:
				case IDOK:
				DestroyWindow(hwndDlg);
				return TRUE;*/
			}
			break;
		}

		case WM_CLOSE: {
			HWND hShareList = GetDlgItem(hwndDlg, IDC_CURRENT_SHARES);
			HWND hUserList = GetDlgItem(hwndDlg, IDC_CURRENT_USERS);

			db_set_w(NULL, MODULE, "StatWnd_cx1", (WORD)ListView_GetColumnWidth(hShareList, 0));
			db_set_w(NULL, MODULE, "StatWnd_cx2", (WORD)ListView_GetColumnWidth(hShareList, 1));
			db_set_w(NULL, MODULE, "StatWnd_cx3", (WORD)ListView_GetColumnWidth(hShareList, 2));
			db_set_w(NULL, MODULE, "StatWnd_cx4", (WORD)ListView_GetColumnWidth(hShareList, 3));
			db_set_w(NULL, MODULE, "StatWnd_cx5", (WORD)ListView_GetColumnWidth(hShareList, 4));
			db_set_w(NULL, MODULE, "StatWnd_cx6", (WORD)ListView_GetColumnWidth(hUserList, 0));
			db_set_w(NULL, MODULE, "StatWnd_cx7", (WORD)ListView_GetColumnWidth(hUserList, 1));
			db_set_w(NULL, MODULE, "StatWnd_cx8", (WORD)ListView_GetColumnWidth(hUserList, 2));
			db_set_w(NULL, MODULE, "StatWnd_cx9", (WORD)ListView_GetColumnWidth(hUserList, 3));
			db_set_w(NULL, MODULE, "StatWnd_cx10", (WORD)ListView_GetColumnWidth(hUserList, 4));

			bool b = IsDlgButtonChecked(hwndDlg, IDC_SHOWHIDDENSHARES) == BST_CHECKED;
			db_set_b(NULL, MODULE, "StatWnd_ShowHidden", b);

			Utils_SaveWindowPosition(hwndDlg, 0, MODULE, "StatWnd_");
			DestroyWindow(hwndDlg);
			return TRUE;
		}
	}
	return FALSE;
	//return DefDlgProc( hwndDlg, msg, wParam, lParam );
}


/////////////////////////////////////////////////////////////////////
// Member Function : SendLinkToUser
// Type            : Global
// Parameters      : wParam - (MCONTACT)hContact
//                   lParam - ?
// Returns         : static int
// Description     : Send the link to the given contact
//
// References      : -
// Remarks         : -
// Created         : 030826, 26 august 2003
// Developer       : Sérgio Rolanski
/////////////////////////////////////////////////////////////////////

void SendLinkToUser(WPARAM wParam, TCHAR *pszSrvPath)
{
	tstring sLink = sCreateLink(pszSrvPath);
	CallService(MS_MSG_SENDMESSAGE, (WPARAM)wParam, (LPARAM)sLink.c_str());
}

/////////////////////////////////////////////////////////////////////
// Member Function : nShareNewFile
// Type            : Global
// Parameters      : wParam - (MCONTACT)hContact
//                   lParam - ?
// Returns         : static int

// Description     : Called when user selects my menu item
//
// References      : -
// Remarks         : -
// Created         : 030826, 26 august 2003
// Developer       : KN, Houdini
/////////////////////////////////////////////////////////////////////

static INT_PTR nShareNewFile(WPARAM hContact, LPARAM lParam)
{
	// used to be _MAX_PATH
	// changed it since selecting multiple files requires a bigger buffer
	TCHAR szNewFile[10000] = {0};
	TCHAR szSrvPath[10000] = {0}; 

	STFileShareInfo stNewShare = {0};
	stNewShare.lStructSize = sizeof(STFileShareInfo);
	stNewShare.nMaxDownloads = 1;
	stNewShare.pszRealPath = szNewFile;
	stNewShare.dwMaxRealPath = SIZEOF(szNewFile);
	stNewShare.pszSrvPath = szSrvPath;
	stNewShare.dwMaxSrvPath = SIZEOF(szSrvPath);

	if (hContact) {
		// Try to locate an IP address.
		DBVARIANT dbv = {0};
		if (! db_get(hContact, "Protocol", "p", &dbv)) {
			if (dbv.type == DBVT_ASCIIZ) {
				stNewShare.dwAllowedIP = db_get_dw(hContact, dbv.pszVal, "IP", 0);
				if (! stNewShare.dwAllowedIP)
					stNewShare.dwAllowedIP = db_get_dw(hContact, dbv.pszVal, "RealIP", 0);
				if (! stNewShare.dwAllowedIP)
					stNewShare.dwAllowedIP = db_get_dw(hContact, MODULE, "LastUsedIP", 0);
			}
		}
		db_free(&dbv);

		stNewShare.dwAllowedMask = db_get_dw(hContact, MODULE, "LastUsedMask", 0);
	}
	if (! stNewShare.dwAllowedMask) {
		if (stNewShare.dwAllowedIP)
			stNewShare.dwAllowedMask = 0xFFFFFFFF;
		else
			stNewShare.dwAllowedMask = 0;
	}

	if (! bShowShareNewFileDlg((HWND)(lParam ? lParam : CallService(MS_CLUI_GETHWND, 0, 0)), &stNewShare))
		return 0;

	if (stNewShare.dwAllowedIP)
		db_set_dw(hContact, MODULE, "LastUsedIP", stNewShare.dwAllowedIP);
	else
		db_unset(hContact, MODULE, "LastUsedIP");

	if (stNewShare.dwAllowedMask && stNewShare.dwAllowedMask != 0xFFFFFFFF)
		db_set_dw(hContact, MODULE, "LastUsedMask", stNewShare.dwAllowedMask);
	else
		db_unset(hContact, MODULE, "LastUsedMask");

	SendLinkToUser(hContact, stNewShare.pszSrvPath);
	return 0;
}


static INT_PTR nShowStatisticsView(WPARAM /*wParam*/, LPARAM /*lParam*/) {
	if (hwndStatsticView) {
		BringWindowToTop(hwndStatsticView);
		return 0;
	}
	hwndStatsticView = CreateDialogParam(hInstance, MAKEINTRESOURCE(IDD_STATISTICS_VIEW), NULL, DlgProcStatsticView, (LPARAM)NULL);
	ShowWindow(hwndStatsticView, SW_SHOWNORMAL);
	return 0;
}


/////////////////////////////////////////////////////////////////////
// Member Function : OptionsDlgProc
// Type            : Global
// Parameters      : hwndDlg - ?
//                   msg     - ?
//                   wParam  - ?
//                   lParam  - ?
// Returns         : static BOOL CALLBACK
// Description     :
//
// References      : -
// Remarks         : -
// Created         : 030911, 11 september 2003
// Developer       : KN, Houdini
/////////////////////////////////////////////////////////////////////

static INT_PTR CALLBACK OptionsDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) {
	switch (msg) {
		case WM_INITDIALOG: {
				tstring sDefExt = DBGetString(NULL, MODULE, "ExternalSrvName", szDefaultExternalSrvName);
				SetDlgItemText(hwndDlg, IDC_EXTERNAL_SRV_NAME, sDefExt.c_str());

				bool b = db_get_b(NULL, MODULE, "AddStatisticsMenuItem", 1) != 0;
				CheckDlgButton(hwndDlg, IDC_ADD_STATISTICS_MENU_ITEM, b ? BST_CHECKED : BST_UNCHECKED);

				b = db_get_b(NULL, MODULE, "AddAcceptConMenuItem", 1) != 0;
				CheckDlgButton(hwndDlg, IDC_ACCEPT_COM_MENU_ITEM, b ? BST_CHECKED : BST_UNCHECKED);

				b = db_get_b(NULL, MODULE, "WriteLogFile", 0) != 0;
				CheckDlgButton(hwndDlg, IDC_WRITE_LOG_FILE, b ? BST_CHECKED : BST_UNCHECKED);

				CheckDlgButton(hwndDlg, IDC_SHOW_POPUPS, bShowPopups ? BST_CHECKED : BST_UNCHECKED);
				CheckDlgButton(hwndDlg, IDC_LIMIT_ONLY_WHEN_ONLINE, bLimitOnlyWhenOnline ? BST_CHECKED : BST_UNCHECKED);


				{// Url Address
					SetDlgItemText(hwndDlg, IDC_URL_ADDRESS, sUrlAddress.c_str());
					HWND hComboBox = GetDlgItem(hwndDlg, IDC_URL_ADDRESS);
					SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)_T("http://checkip.dyndns.org"));
					SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)_T("http://checkip.dyndns.org:8245/"));
					SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)_T("http://dynamic.zoneedit.com/checkip.html"));
					SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)_T("http://ipdetect.dnspark.com/"));
					SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)_T("http://update.dynu.com/basic/ipcheck.asp"));
					SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)_T("http://www.dnsart.com/myip.php"));
					SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)_T("http://www.dnsart.com:7777/myip.php"));
				}

				{// Page keyword
					SetDlgItemText(hwndDlg, IDC_PAGE_KEYWORD, sPageKeyword.c_str());
					HWND hComboBox = GetDlgItem(hwndDlg, IDC_PAGE_KEYWORD);
					SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)_T(""));
					SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)LPGENT("Current IP Address: "));
					SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)LPGENT("Current Address: "));
					SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)LPGENT("IP Adress: "));
					SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)LPGENT("You are browsing from"));
					SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)_T("<HTML><BODY>"));
				}

				SetDlgItemInt(hwndDlg, IDC_MAX_SPEED, nMaxUploadSpeed >> 10, true);
				SetDlgItemInt(hwndDlg, IDC_MAX_CONN_TOTAL, nMaxConnectionsTotal, true);
				SetDlgItemInt(hwndDlg, IDC_MAX_CONN_PER_USER, nMaxConnectionsPerUser, true);
				SetDlgItemInt(hwndDlg, IDC_DEFAULT_DOWNLOAD_LIMIT, nDefaultDownloadLimit, true);

				indexCreationMode =
				  (eIndexCreationMode)db_get_b(NULL, MODULE, "IndexCreationMode", 3);

				switch (indexCreationMode) {
					case INDEX_CREATION_HTML:
						CheckRadioButton(hwndDlg, IDC_INDEX_OFF, IDC_INDEX_DETECT, IDC_INDEX_HTML);
						break;
					case INDEX_CREATION_XML:
						CheckRadioButton(hwndDlg, IDC_INDEX_OFF, IDC_INDEX_DETECT, IDC_INDEX_XML);
						break;
					case INDEX_CREATION_DETECT:
						CheckRadioButton(hwndDlg, IDC_INDEX_OFF, IDC_INDEX_DETECT, IDC_INDEX_DETECT);
						break;
					default: // INDEX_CREATION_DISABLE
						CheckRadioButton(hwndDlg, IDC_INDEX_OFF, IDC_INDEX_DETECT, IDC_INDEX_OFF);
						break;
				}

				TranslateDialogDefault(hwndDlg);
				return TRUE;
			}
		case WM_COMMAND: {
				switch (LOWORD(wParam)) {
					case IDC_MAX_SPEED:
					case IDC_MAX_CONN_PER_USER:
					case IDC_MAX_CONN_TOTAL:
					case IDC_EXTERNAL_SRV_NAME: {
							if (HIWORD(wParam) == EN_CHANGE)
								SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0);
							return TRUE;
						}
					case IDC_URL_ADDRESS:
					case IDC_PAGE_KEYWORD: {
							/*if( !bWindowTextSet )
							return TRUE;*/

							if (HIWORD(wParam) == CBN_EDITUPDATE || HIWORD(wParam) == CBN_SELCHANGE) {
								SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0);
							}
							return TRUE;
						}

					case IDC_INDEX_HTML:
					case IDC_INDEX_OFF:
					case IDC_INDEX_XML:
					case IDC_INDEX_DETECT:
					case IDC_LIMIT_ONLY_WHEN_ONLINE:
					case IDC_SHOW_POPUPS:
					case IDC_WRITE_LOG_FILE:
					case IDC_ADD_STATISTICS_MENU_ITEM:
					case IDC_ACCEPT_COM_MENU_ITEM: {
							if (HIWORD(wParam) == BN_CLICKED) {
								SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0);
							}
							return TRUE;
						}
					case IDC_EXTERNAL_SRV_DEFAULT: {
							if (HIWORD(wParam) == BN_CLICKED) {
								SetDlgItemText(hwndDlg, IDC_EXTERNAL_SRV_NAME, szDefaultExternalSrvName);
								SetDlgItemText(hwndDlg, IDC_URL_ADDRESS,  szDefaultUrlAddress);
								SetDlgItemText(hwndDlg, IDC_PAGE_KEYWORD,  szDefaultPageKeyword);
								SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0);
							}
							return TRUE;
						}
					case IDC_OPEN_LOG: {
							bOpenLogFile();
							return TRUE;
						}
					case IDC_TEST_EXTERNALIP: {
							TCHAR szUrl[500];
							TCHAR szKeyWord[1000];
							GetDlgItemText(hwndDlg, IDC_URL_ADDRESS, szUrl, SIZEOF(szUrl));
							GetDlgItemText(hwndDlg, IDC_PAGE_KEYWORD, szKeyWord, SIZEOF(szKeyWord));
							DWORD dwExternalIP = GetExternIP(_T2A(szUrl), _T2A(szKeyWord));

							mir_sntprintf(szKeyWord, SIZEOF(szKeyWord), TranslateT("Your external IP was detected as %d.%d.%d.%d\r\nby: %s") ,
							    SplitIpAddress(dwExternalIP) ,
							    szUrl);
							MessageBox(hwndDlg, szKeyWord, MSG_BOX_TITEL, MB_OK);
						}
				}
				break;
			}
		case WM_NOTIFY: {
				NMHDR * p = ((LPNMHDR)lParam);
				switch (p->code) {
					case PSN_APPLY: {
							TCHAR szTemp[500];
							if (GetDlgItemText(hwndDlg, IDC_EXTERNAL_SRV_NAME, szTemp, SIZEOF(szTemp)))
								db_set_ts(NULL, MODULE, "ExternalSrvName", szTemp);

							bool b = db_get_b(NULL, MODULE, "AddStatisticsMenuItem", 1) != 0;
							bool bNew = IsDlgButtonChecked(hwndDlg, IDC_ADD_STATISTICS_MENU_ITEM) == BST_CHECKED;
							if (b != bNew) {
								db_set_b(NULL, MODULE, "AddStatisticsMenuItem", bNew);
								MessageBox(hwndDlg, TranslateT("You need to restart Miranda to change the main menu"), MSG_BOX_TITEL, MB_OK);
							}

							b = db_get_b(NULL, MODULE, "AddAcceptConMenuItem", 1) != 0;
							bNew = IsDlgButtonChecked(hwndDlg, IDC_ACCEPT_COM_MENU_ITEM) == BST_CHECKED;
							if (b != bNew) {
								db_set_b(NULL, MODULE, "AddAcceptConMenuItem", bNew);
								MessageBox(hwndDlg, TranslateT("You need to restart Miranda to change the main menu"), MSG_BOX_TITEL, MB_OK);
							}

							bNew = IsDlgButtonChecked(hwndDlg, IDC_WRITE_LOG_FILE) == BST_CHECKED;
							db_set_b(NULL, MODULE, "WriteLogFile", bNew);

							bShowPopups = IsDlgButtonChecked(hwndDlg, IDC_SHOW_POPUPS) == BST_CHECKED;
							db_set_b(NULL, MODULE, "ShowPopups", bShowPopups);

							GetDlgItemText(hwndDlg, IDC_URL_ADDRESS, szTemp, SIZEOF(szTemp));
							sUrlAddress = szTemp;
							db_set_ts(NULL, MODULE, "UrlAddress", sUrlAddress.c_str());

							GetDlgItemText(hwndDlg, IDC_PAGE_KEYWORD, szTemp, SIZEOF(szTemp));
							sPageKeyword = szTemp;
							db_set_ts(NULL, MODULE, "PageKeyword", sPageKeyword.c_str());
							dwExternalIpAddress = 0;

							BOOL bTranslated = false;
							int nTemp = GetDlgItemInt(hwndDlg, IDC_MAX_SPEED, &bTranslated, true);
							if (bTranslated) {
								nMaxUploadSpeed = nTemp << 10;
								db_set_dw(NULL, MODULE, "MaxUploadSpeed", nMaxUploadSpeed);
							}

							nTemp = GetDlgItemInt(hwndDlg, IDC_MAX_CONN_TOTAL, &bTranslated, true);
							if (bTranslated) {
								nMaxConnectionsTotal = nTemp;
								db_set_dw(NULL, MODULE, "MaxConnectionsTotal", nMaxConnectionsTotal);
							}

							nTemp = GetDlgItemInt(hwndDlg, IDC_MAX_CONN_PER_USER, &bTranslated, true);
							if (bTranslated) {
								nMaxConnectionsPerUser = nTemp;
								db_set_dw(NULL, MODULE, "MaxConnectionsPerUser", nMaxConnectionsPerUser);
							}

							nTemp = GetDlgItemInt(hwndDlg, IDC_DEFAULT_DOWNLOAD_LIMIT, &bTranslated, true);
							if (bTranslated) {
								nDefaultDownloadLimit = nTemp;
								db_set_dw(NULL, MODULE, "DefaultDownloadLimit", nDefaultDownloadLimit);
							}

							bLimitOnlyWhenOnline = IsDlgButtonChecked(hwndDlg, IDC_LIMIT_ONLY_WHEN_ONLINE) == BST_CHECKED;
							db_set_b(NULL, MODULE, "LimitOnlyWhenOnline", bLimitOnlyWhenOnline);

							if (IsDlgButtonChecked(hwndDlg, IDC_INDEX_HTML) == BST_CHECKED ||
							    IsDlgButtonChecked(hwndDlg, IDC_INDEX_DETECT) == BST_CHECKED) {
								if (IsDlgButtonChecked(hwndDlg, IDC_INDEX_HTML) == BST_CHECKED)
									indexCreationMode = INDEX_CREATION_HTML;
								else
									indexCreationMode = INDEX_CREATION_DETECT;

								if (!LoadIndexHTMLTemplate()) {
									CheckRadioButton(hwndDlg, IDC_INDEX_OFF, IDC_INDEX_XML, IDC_INDEX_OFF);
									indexCreationMode = INDEX_CREATION_DISABLE;
								}
							} else if (IsDlgButtonChecked(hwndDlg, IDC_INDEX_XML) == BST_CHECKED) {
								FreeIndexHTMLTemplate();
								indexCreationMode = INDEX_CREATION_XML;
							} else {
								FreeIndexHTMLTemplate();
								indexCreationMode = INDEX_CREATION_DISABLE;
							}

							db_set_b(NULL, MODULE, "IndexCreationMode", (BYTE)indexCreationMode);

							return TRUE;
						}
				}
				break;
			}
	}
	return FALSE;
}


/////////////////////////////////////////////////////////////////////
// Member Function : OptionsInitialize
// Type            : Global
// Parameters      : wParam - ?
//                   LPARAM - ?
// Returns         : int
// Description     :
//
// References      : -
// Remarks         : -
// Created         : 030911, 11 september 2003
// Developer       : KN
/////////////////////////////////////////////////////////////////////

int OptionsInitialize(WPARAM wParam, LPARAM /*lParam*/)
{
	OPTIONSDIALOGPAGE odp = { sizeof(odp) };
	odp.position = 900000000;
	odp.hInstance = hInstance;
	odp.pszTemplate = MAKEINTRESOURCEA(IDD_OPT_HTTP_SERVER);
	odp.flags = ODPF_BOLDGROUPS | ODPF_TCHAR;
	odp.ptszTitle = LPGENT("HTTP Server");
	odp.ptszGroup = LPGENT("Network");
	odp.pfnDlgProc = OptionsDlgProc;
	Options_AddPage(wParam, &odp);
	return 0;
}

/////////////////////////////////////////////////////////////////////
// Member Function : MainThreadCallback
// Type            : Global
// Parameters      : dwParam - ?
// Returns         : void CALLBACK
// Description     : Called from main thread.
//                   Because MS_POPUP_ADDPOPUP may only be called from that
//                   ShowPopupWindow activates this method via APC
// References      : -
// Remarks         : -
// Created         : 030902, 02 september 2003
// Developer       : KN
/////////////////////////////////////////////////////////////////////

void CALLBACK MainThreadCallback(ULONG_PTR dwParam) {
	POPUPDATAT *pclData = (POPUPDATAT*)dwParam;
	if (db_get_b(NULL, MODULE, "WriteLogFile", 0) != 0) {
		LogEvent(pclData->lptzContactName, pclData->lptzText);
	}
	PUAddPopupT(pclData);
	delete pclData;
}

/////////////////////////////////////////////////////////////////////
// Member Function : PopupWindowProc
// Type            : Global
// Parameters      : hWnd    - ?
//                   message - ?
//                   wParam  - ?
//                   lParam  - ?
// Returns         : LRESULT CALLBACK
// Description     :
//
// References      : -
// Remarks         : -
// Created         : 031011, 11 oktober 2003
// Developer       : KN, Houdini
/////////////////////////////////////////////////////////////////////

void CALLBACK OpenStatisticViewFromPopupProc(ULONG_PTR /* dwParam */) {
	nShowStatisticsView(0, 0);
}

LRESULT CALLBACK PopupWindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
	PUGetPluginData(hWnd);
	//HANDLE hData = (HANDLE)CallService(MS_POPUP_GETPLUGINDATA, (WPARAM)hWnd, (LPARAM)&hData);
	
	switch (message) {
		//case WM_LBUTTONUP:
		//case WM_LBUTTONDBLCLK: // These don't work I can't undestande why !!
		case WM_LBUTTONDOWN:

			//nShowStatisticsView(0,0);
			// has to be called from the right thread
			//QueueUserAPC(OpenStatisticViewFromPopupProc, hMainThread, 0);

			QueueUserAPC(OpenStatisticViewFromPopupProc, hMainThread, 0);
			PUDeletePopup( hWnd );
			return 0;

		case WM_CONTEXTMENU: {
				PUDeletePopup( hWnd );
				return 0;
			}
	}
	return DefWindowProc(hWnd, message, wParam, lParam);
}


/////////////////////////////////////////////////////////////////////
// Member Function : ShowPopupWindow
// Type            : Global
// Parameters      : pszText - ?
// Returns         : void
// Description     :
//
// References      : -
// Remarks         : -
// Created         : 030831, 31 august 2003
// Developer       : KN
/////////////////////////////////////////////////////////////////////

void ShowPopupWindow(const TCHAR * pszName, const TCHAR * pszText, COLORREF ColorBack /*= 0*/) {
	if (! bShowPopups)
		return;

	POPUPDATAT *pclData = new POPUPDATAT;
	memset(pclData, 0, sizeof(POPUPDATAT));
	pclData->lchIcon = LoadIcon(hInstance,  MAKEINTRESOURCE(IDI_SHARE_NEW_FILE));
	_tcsncpy(pclData->lptzContactName, pszName, SIZEOF(pclData->lptzContactName) - 1);   // -1 so that there aways will be a null termination !!
	_tcsncpy(pclData->lptzText, pszText, SIZEOF(pclData->lptzText) - 1);
	pclData->colorBack = ColorBack;
	//ppd.colorText = colorText;
	pclData->PluginWindowProc = PopupWindowProc;
	//Now that every field has been filled, we want to see the popup.

	//CallService(MS_POPUP_ADDPOPUP, (WPARAM)&ppd, 0);
	QueueUserAPC(MainThreadCallback, hMainThread, (ULONG_PTR)pclData);
}

/////////////////////////////////////////////////////////////////////
// Member Function : InitGuiElements
// Type            : Global
// Parameters      : None
// Returns         : void
// Description     :
//
// References      : -
// Remarks         : -
// Created         : 030902, 02 september 2003
// Developer       : KN
/////////////////////////////////////////////////////////////////////

void InitGuiElements() {
	INITCOMMONCONTROLSEX stInitCom;
	stInitCom.dwSize = sizeof(INITCOMMONCONTROLSEX);
	stInitCom.dwICC = ICC_INTERNET_CLASSES;
	InitCommonControlsEx(&stInitCom);

	//hMainThread = GetCurrentThread();
	DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(), &hMainThread, THREAD_SET_CONTEXT, FALSE, 0);


	sUrlAddress = DBGetString(NULL, MODULE, "UrlAddress", szDefaultUrlAddress);
	sPageKeyword = DBGetString(NULL, MODULE, "PageKeyword", szDefaultPageKeyword);

	hShareNewFileService = CreateServiceFunction(MS_SHARE_NEW_FILE, nShareNewFile);
	if (! hShareNewFileService) {
		MessageBox(NULL, TranslateT("Failed to CreateServiceFunction MS_SHARE_NEW_FILE"), MSG_BOX_TITEL, MB_OK);
		return;
	}

	hShowStatisticsViewService = CreateServiceFunction(MS_SHOW_STATISTICS_VIEW, nShowStatisticsView);
	if (! hShowStatisticsViewService) {
		MessageBox(NULL, TranslateT("Failed to CreateServiceFunction MS_SHOW_STATISTICS_VIEW"), MSG_BOX_TITEL, MB_OK);
		return;
	}

	CLISTMENUITEM mi = { 0 };
	mi.cbSize = sizeof(mi);
	mi.flags = CMIF_TCHAR;
	mi.pszContactOwner = NULL;  //all contacts
	mi.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SHARE_NEW_FILE));
	mi.position = -2000019955;
	mi.ptszName = LPGENT("HTTP Share new file");
	mi.pszService = MS_SHARE_NEW_FILE;

	hShareNewFileMenuItem = Menu_AddContactMenuItem(&mi);
	if (!hShareNewFileMenuItem) {
		MessageBox(NULL, TranslateT("Failed to add contact menu item"), MSG_BOX_TITEL, MB_OK);
		return;
	}


	if (db_get_b(NULL, MODULE, "AddStatisticsMenuItem", 1) != 0) {
		mi.position = 1000085005;
		mi.flags = CMIF_TCHAR;
		//mi.hIcon=LoadIcon(hInstance,MAKEINTRESOURCE(IDI_SHARE_NEW_FILE));
		mi.pszContactOwner = NULL;
		mi.ptszName = LPGENT("Show HTTP server statistics");
		mi.pszService = MS_SHOW_STATISTICS_VIEW;
		hShowStatisticsViewMenuItem = Menu_AddMainMenuItem(&mi);
	}


	hEventOptionsInitialize = HookEvent(ME_OPT_INITIALISE, OptionsInitialize);
	if (!hEventOptionsInitialize)
		MessageBox(NULL, _T("Failed to HookEvent ME_OPT_INITIALISE"), MSG_BOX_TITEL, MB_OK);

	bShowPopups = db_get_b(NULL, MODULE, "ShowPopups", bShowPopups) != 0;
	/*
	#ifdef _DEBUG
	nShowStatisticsView(0,0);
	#endif*/
}


/////////////////////////////////////////////////////////////////////
// Member Function : UninitGuiElements
// Type            : Global
// Parameters      : None
// Returns         : void
// Description     :
//
// References      : -
// Remarks         : -
// Created         : 031011, 11 oktober 2003
// Developer       : KN
/////////////////////////////////////////////////////////////////////

void UnInitGuiElements() {
	CloseHandle(hMainThread);
}