summaryrefslogtreecommitdiff
path: root/plugins/UserInfoEx/src/mir_db.cpp
blob: 9f9d3b8ed64d8ed1e088279ffd2b25a1e3d3b100 (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
/*
UserinfoEx plugin for Miranda IM

Copyright:
© 2006-2010 DeathAxe, Yasnovidyashii, Merlin, K. Romanov, Kreol

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include "commonheaders.h"

namespace DB {

namespace MetaContact {

/**
 *
 *
 **/
INT_PTR	SubCount(HANDLE hMetaContact)
{
	INT_PTR result = CallService(MS_MC_GETNUMCONTACTS, (WPARAM) hMetaContact, 0);
	return (result == CALLSERVICE_NOTFOUND) ? -1 : result;
}

/**
 *
 *
 **/
INT_PTR	SubDefNum(HANDLE hMetaContact)
{
	INT_PTR result = CallService(MS_MC_GETDEFAULTCONTACTNUM, (WPARAM) hMetaContact, 0);
	return (result == CALLSERVICE_NOTFOUND) ? -1 : result;
}

/**
 *
 *
 **/
HANDLE	Sub(HANDLE hMetaContact, int idx)
{
	if (idx != -1) {
		INT_PTR result = CallService(MS_MC_GETSUBCONTACT, (WPARAM) hMetaContact, (LPARAM) idx);
		return (result == CALLSERVICE_NOTFOUND) ? NULL : (HANDLE) result;
	}
	return NULL;
}

/**
 *
 *
 **/
BYTE	IsSub(HANDLE hContact)
{
	return	myGlobals.szMetaProto &&
			DB::Setting::GetByte(myGlobals.szMetaProto, "Enabled", TRUE) &&
			DB::Setting::GetByte(hContact, myGlobals.szMetaProto, "IsSubcontact", FALSE);
}

/**
 *
 *
 **/
HANDLE	GetMeta(HANDLE hContact)
{
	HANDLE result;
	if (myGlobals.szMetaProto){
		result = (HANDLE)CallService(MS_MC_GETMETACONTACT, (WPARAM) hContact, 0);
		if (result == (HANDLE)CALLSERVICE_NOTFOUND) {
			result = NULL;
		}
	}
	else {
		result = NULL;
	}
	return (HANDLE) result;
}

} /* namespace MetaContact */

/**
* This namespace contains all functions used to access or modify contacts in the database.
**/
namespace Contact {

/**
 * This function retrieves the display name for a contact.
 * @param	hContact	- handle to the contact
 * @return	Returns the display name of a contact.
 **/
LPTSTR	DisplayName(HANDLE hContact)
{
	return (LPTSTR) CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM) hContact, GCDNF_TCHAR);
}

/**
 * This function is used to retrieve a contact's basic protocol
 * @param	hContact	- handle to the contact
 * @return	This function returns the basic protocol of a contact.
 **/
LPSTR	Proto(HANDLE hContact)
{
	if (hContact) {
		INT_PTR result;
		result = CallService(MS_PROTO_GETCONTACTBASEACCOUNT, (WPARAM) hContact, NULL);
		return (LPSTR) ((result == CALLSERVICE_NOTFOUND) ? NULL : result);
	}
	return NULL;
}

/**
 * Gets the number of contacts in the database, which does not count the user
 * @param	hContact	- handle to the contact
 * @return	Returns the number of contacts. They can be retrieved using
 *			contact/findfirst and contact/findnext
 **/
INT_PTR	GetCount()
{
	return CallService(MS_DB_CONTACT_GETCOUNT, 0, 0);
}

/**
 * This function searches the first contact in the database and returns its handle.
 * @retval	HANDLE		- handle of the next contact in the database	
 * @retval	NULL		- no more contacts in the database
 **/
HANDLE	FindFirst()
{
	return db_find_first();
}

/**
 * This function searches the next contact in the database and returns its handle.
 * @param	hContact	- handle to the contact
 * @retval	HANDLE		- handle of the next contact in the database
 * @retval	NULL		- no more contacts in the database
 **/
HANDLE	FindNext(HANDLE hContact)
{
	return db_find_next(hContact);
}

/**
 * Simply adds a new contact without setting up any protocol or something else
 * @return	HANDLE		The function returns the HANDLE of the new contact
 **/
HANDLE	Add()
{
	return (HANDLE) CallService(MS_DB_CONTACT_ADD, 0, 0);
}

/**
 * This function deletes a contact from the database.
 * @param	hContact	- handle to the contact
 **/
BYTE	Delete(HANDLE hContact)
{
	return CallService(MS_DB_CONTACT_DELETE, (WPARAM) hContact, 0) != 0;
}

/**
 * This function trys to guess, when an ICQ contact was added to database.
 **/
DWORD	WhenAdded(DWORD dwUIN, LPCSTR pszProto)
{
	DBEVENTINFO		dbei; 
	HANDLE			edbe;
	DWORD			dwEvtUIN;

	ZeroMemory(&dbei, sizeof(dbei));
	dbei.cbSize = sizeof(dbei);
	for (edbe = db_event_first(NULL); edbe != NULL; edbe = db_event_next(edbe)) {
		// get eventtype and compare
		if (!DB::Event::GetInfo(edbe, &dbei) && dbei.eventType == EVENTTYPE_ADDED) {
			if (!DB::Event::GetInfoWithData(edbe, &dbei)) {
				// extract UIN and compare with given one
				CopyMemory(&dwEvtUIN, dbei.pBlob, sizeof(DWORD));
				MIR_FREE(dbei.pBlob);
				if (dwEvtUIN == dwUIN) {
					return dbei.timestamp;
				}
			}
		}
	}
	return 0;
}

} /* Contact */

namespace Module {

/**
 * Deletes all settings in the module.
 * @param	hContact	- handle to the contact
 * @param	pszModule	- the module to delete the setting from (e.g. USERINFO)
 * return:	nothing
 **/
void	Delete(HANDLE hContact, LPCSTR pszModule)
{
	CEnumList	Settings;
	if (!Settings.EnumSettings(hContact, pszModule)) {
		int i;
		for (i = 0; i < Settings.getCount(); i++) {
			DB::Setting::Delete(hContact, pszModule, Settings[i]);
		}
	}
}

/**
 * Enum Proc for DBModule_IsEmpty
 * @param	pszSetting	- the setting
 * @param	lParam		- DBCONTACTENUMSETTINGS - (LPARAM)&dbces
 * @retval	TRUE		- always true
 **/
static	int IsEmptyEnumProc(LPCSTR pszSetting, LPARAM lParam)
{
	return 1;
}

/**
 * This function tests, whether a module is empty for the given contact or not
 * @param	hContact	- handle to the contact
 * @param	pszModule	- the module to read the setting from (e.g. USERINFO)
 * @retval	TRUE		- the module is empty
 * @retval	FALSE		- the module contains settings
 **/
BYTE	IsEmpty(HANDLE hContact, LPCSTR pszModule)
{
	DBCONTACTENUMSETTINGS dbces;
	dbces.pfnEnumProc	= IsEmptyEnumProc;
	dbces.szModule		= pszModule;
	dbces.ofsSettings	= 0;
	dbces.lParam		= 0;
	return (0 > CallService(MS_DB_CONTACT_ENUMSETTINGS, (WPARAM)hContact, (LPARAM)&dbces));
}

/**
 * This function tests, whether a module belongs to a metacontact protocol
 * @param	pszModule	- the module to read the setting from (e.g. USERINFO)
 * @retval	TRUE		- the module belongs to a metacontact protocol
 * @retval	FALSE		- the module belongs to a other protocol
 **/
BYTE	IsMeta(LPCSTR pszModule)
{
	if(myGlobals.szMetaProto)
		return !mir_strcmp(pszModule, myGlobals.szMetaProto);
	return !mir_strcmp(pszModule, "MetaContacts");
}

/**
 * This function tests, whether a module is a meta contact, and user wants to scan it for settings
 * @param	pszModule	- the module to read the setting from (e.g. USERINFO)
 * @retval	TRUE		- the module is empty
 * @retval	FALSE		- the module contains settings
 **/
BYTE	IsMetaAndScan	(LPCSTR pszModule)
{
	return DB::Setting::GetByte(SET_META_SCAN, TRUE) && IsMeta(pszModule);
}

} /* namespace Module */

namespace Setting {

/**
 * This function calls MS_DB_CONTACT_GETSETTING_STR service to get database values. 
 * @param	hContact	- handle to the contact
 * @param	pszModule	- the module to read the setting from (e.g. USERINFO)
 * @param	pszSetting	- the setting to read
 * @param	destType	- desired string type (DBVT_ASCIIZ, DBVT_WCHAR, DBVT_UTF8)
 *
 * @retval	0 - success
 * @retval	1 - error
 **/
BYTE	Get(HANDLE hContact, LPCSTR pszModule, LPCSTR pszSetting, DBVARIANT *dbv, const BYTE destType)
{
	BYTE result;
	DBCONTACTGETSETTING dgs;

	dgs.szModule	= pszModule;
	dgs.szSetting	= pszSetting;
	dgs.pValue		= dbv;
	dbv->type		= 0;
	
	// read value without translation to specific type
	result = CallService(MS_DB_CONTACT_GETSETTING_STR, (WPARAM) hContact, (LPARAM) &dgs) != 0;

	// Is value read successfully and destination type set?
	if (!result && destType) {
		result = DB::Variant::ConvertString(dbv, destType);
	}
	return result;
}

/**
 * This function reads a value from the database and returns it as an ansi encoded string.
 * @param	hContact	- handle to the contact
 * @param	pszModule	- the module to read the setting from (e.g. USERINFO)
 * @param	pszSetting	- the setting to read
 *
 * @return	string value
 **/
LPSTR	GetAString(HANDLE hContact, LPCSTR pszModule, LPCSTR pszSetting)
{
	DBVARIANT dbv;
	if (GetAString(hContact, pszModule, pszSetting, &dbv) == 0){
		if (DB::Variant::dbv2String(&dbv, DBVT_WCHAR) == 0) {
			return dbv.pszVal;
		}
		DB::Variant::Free(&dbv);
	}
	return NULL;
}

/**
 * This function reads a value from the database and returns it as an unicode encoded string. 
 * @param	hContact	- handle to the contact
 * @param	pszModule	- the module to read the setting from (e.g. USERINFO)
 * @param	pszSetting	- the setting to read
 *
 * @return	string value
 **/
LPWSTR	GetWString(HANDLE hContact, LPCSTR pszModule, LPCSTR pszSetting)
{
	DBVARIANT dbv;
	if (GetWString(hContact, pszModule, pszSetting, &dbv) == 0) {
		if (DB::Variant::dbv2String(&dbv, DBVT_WCHAR) == 0) {
			return dbv.pwszVal;
		}
		DB::Variant::Free(&dbv);
	}
	return NULL;
}

/**
 * This function calls MS_DB_CONTACT_GETSETTING_STR service to get database values. 
 * It searches in pszModule first and if the setting does not exist there it tries proto to retrieve it.
 * @param	hContact	- handle to the contact
 * @param	pszModule	- the module to read the setting from (e.g. USERINFO)
 * @param	szProto		- the contact's protocol to read the setting from (e.g. ICQ)
 * @param	szSetting	- the setting to read
 * @param	destType	- desired string type (DBVT_ASCIIZ, DBVT_WCHAR, DBVT_UTF8)
 *
 * @retval	0 - success
 * @retval	1 - error
 **/
BYTE	GetEx(HANDLE hContact, LPCSTR pszModule, LPCSTR pszProto, LPCSTR pszSetting, DBVARIANT *dbv, const BYTE destType)
{
	BYTE result;
	result = !pszModule || Get(hContact, pszModule, pszSetting, dbv, destType);
	// try to read setting from the contact's protocol module 
	if (result && pszProto) {
		result = Get(hContact, pszProto, pszSetting, dbv, destType) != 0;
		// try to get setting from a metasubcontact
		if (result && DB::Module::IsMetaAndScan(pszProto)) {
			const INT_PTR def = DB::MetaContact::SubDefNum(hContact);
			HANDLE hSubContact;
			// try to get setting from the default subcontact first
			if (def > -1 && def < INT_MAX) {
				hSubContact = DB::MetaContact::Sub(hContact, def);
				if (hSubContact != NULL) {
					result = DB::Setting::GetEx(hSubContact, pszModule, DB::Contact::Proto(hSubContact), pszSetting, dbv, destType) != 0;
				}
			}
			// scan all subcontacts for the setting
			if (result) {
				const INT_PTR cnt = DB::MetaContact::SubCount(hContact);
				if (cnt < INT_MAX) {
					INT_PTR i;
					for (i = 0; result && i < cnt; i++) {
						if (i != def) {
							hSubContact = DB::MetaContact::Sub(hContact, i);
							if (hSubContact != NULL) {
								result = DB::Setting::GetEx(hSubContact, pszModule, DB::Contact::Proto(hSubContact), pszSetting, dbv, destType) != 0;
	}	}	}	}	}	}	}
	return result;
}

/**
 * This function is used by the controls of the details dialog and calls MS_DB_CONTACT_GETSETTING_STR service
 * to get database values. It searches in pszModule first and if the setting does not exist there it tries proto
 * to retrieve it.
 * @param	hContact		- handle to the contact
 * @param	pszModule		- the module to read the setting from (e.g. USERINFO)
 * @param	pszSubModule	- the module to read the setting from a meta subcontract (e.g. USERINFO)
 * @param	pszProto		- the contact's protocol to read the setting from (e.g. ICQ)
 * @param	pszSetting		- the setting to read
 * @param	destType		- desired string type (DBVT_ASCIIZ, DBVT_WCHAR, DBVT_UTF8)
 *
 * @return	This function returns the WORD which contains the source of information.
 **/
WORD	GetCtrl(HANDLE hContact, LPCSTR pszModule, LPCSTR pszSubModule, LPCSTR pszProto, LPCSTR pszSetting, DBVARIANT *dbv, const BYTE destType)
{
	WORD wFlags = 0;

	// read setting from given module
	if (hContact && pszModule && *pszModule && !Get(hContact, pszModule, pszSetting, dbv, destType)) {
		wFlags |= CTRLF_HASCUSTOM;
		if (Exists(hContact, pszProto, pszSetting)) {
			wFlags |= CTRLF_HASPROTO;
		}
	}
	// read setting from contact's basic protocol
	else if (pszProto && *pszProto) {
		// try to read the setting from the basic protocol
		if (!Get(hContact, pszProto, pszSetting, dbv, destType)) {
			wFlags |= CTRLF_HASPROTO;
		}
		// try to read the setting from the sub contacts' modules
		else if (DB::Module::IsMetaAndScan(pszProto)) {
			const INT_PTR def = DB::MetaContact::SubDefNum(hContact);
			HANDLE hSubContact;
			// try to get setting from the default subcontact first
			if (def > -1 && def < INT_MAX) {
				hSubContact = DB::MetaContact::Sub(hContact, def);
				if (hSubContact != NULL) {
					wFlags = GetCtrl(hSubContact, pszSubModule, NULL, DB::Contact::Proto(hSubContact), pszSetting, dbv, destType);
					if (wFlags != 0) {
						wFlags &= ~CTRLF_HASCUSTOM;
						wFlags |= CTRLF_HASMETA;
					}
				}
			}
			// copy the missing settings from the other subcontacts
			if (wFlags == 0) {
				INT_PTR i;
				const INT_PTR cnt = DB::MetaContact::SubCount(hContact);
				for (i = 0; i < cnt; i++) {
					if (i != def) {
						hSubContact = DB::MetaContact::Sub(hContact, i);
						if (hSubContact != NULL) {
							wFlags = GetCtrl(hSubContact, pszSubModule, NULL, DB::Contact::Proto(hSubContact), pszSetting, dbv, destType);
							if (wFlags != 0) {
								wFlags &= ~CTRLF_HASCUSTOM;
								wFlags |= CTRLF_HASMETA;
								break;
	}	}	}	}	}	}	}
	if (wFlags == 0) {
		dbv->type = DBVT_DELETED;
	}
	return wFlags;
}

/**
 * This function reads a setting from database into a predefined portion of memory
 * and convert numbers into a string, too.
 * @param	hContact		- handle to the contact
 * @param	pszModule		- the module to read the setting from (e.g. USERINFO)
 * @param	pszSetting		- the setting to read
 * @param	pszValue		- buffer, that retrieves the value
 * @param	cchValue		- number of characters the buffer can take
 *
 * @retval	0 - success
 * @retval	1 - error
 **/
BYTE	GetStatic(HANDLE hContact, LPCSTR pszModule, LPCSTR pszSetting, LPSTR pszValue, int cchValue)
{
	DBVARIANT dbv;
	DBCONTACTGETSETTING sVal;

	if (pszValue && cchValue) {
		pszValue[0]	= 0;
		dbv.pszVal	= pszValue;
		dbv.cchVal	= cchValue;
		dbv.type	= DBVT_ASCIIZ;
		
		sVal.pValue		= &dbv;
		sVal.szModule	= pszModule;
		sVal.szSetting	= pszSetting;
		
		if (!CallService(MS_DB_CONTACT_GETSETTINGSTATIC, (WPARAM)hContact, (LPARAM)&sVal)) {
			switch (dbv.type) {
			case DBVT_BYTE:
				_itoa(dbv.bVal, pszValue, 10);
				break;
			case DBVT_WORD:
				_itoa(dbv.wVal, pszValue, 10);
				break;
			case DBVT_DWORD:
				_itoa(dbv.dVal, pszValue, 10);
			}
			return (pszValue[0] == 0);
		}
	}
	return 1;
}

/**
 * This function reads a byte from the database. If required it converts it to a byte value
 * @param	hContact		- handle to the contact
 * @param	pszModule		- the module to read the setting from (e.g. USERINFO)
 * @param	pszSetting		- the setting to read
 * @param	errorValue		- value to return if something goes wrong
 *
 * @return	byte value
 **/
BYTE	GetByte(HANDLE hContact, LPCSTR pszModule, LPCSTR pszSetting, BYTE errorValue)
{
	DBVARIANT dbv;
	BYTE result;
	if (GetAsIs(hContact, pszModule, pszSetting, &dbv)) {
		result = errorValue;
	}
	else {
		switch (dbv.type) {
		case DBVT_BYTE:
			result = dbv.bVal;
			break;
		case DBVT_WORD:
			result = (dbv.wVal < 0x0100) ? (BYTE) dbv.wVal : errorValue;
			break;
		case DBVT_DWORD:
			result = (dbv.wVal < 0x00000100) ? (BYTE) dbv.dVal : errorValue;
			break;
		default:
			DB::Variant::Free(&dbv);
			result = errorValue;
		}
	}
	return result;
}		

/**
 * This function reads a word from the database. If required it converts it to a word value
 * @param	hContact		- handle to the contact
 * @param	pszModule		- the module to read the setting from (e.g. USERINFO)
 * @param	pszSetting		- the setting to read
 * @param	errorValue		- value to return if something goes wrong
 *
 * @return	word value
 **/
WORD	GetWord(HANDLE hContact, LPCSTR pszModule, LPCSTR pszSetting, WORD errorValue)
{
	DBVARIANT dbv;
	WORD result;
	if (GetAsIs(hContact, pszModule, pszSetting, &dbv)) {
		result = errorValue;
	}
	else {
		switch (dbv.type) {
		case DBVT_BYTE:
			result = 0x00ff & dbv.bVal;
			break;
		case DBVT_WORD:
			result = dbv.wVal;
			break;
		case DBVT_DWORD:
			result = (dbv.wVal < 0x00010000) ? (WORD) dbv.dVal : errorValue;
			break;
		default:
			DB::Variant::Free(&dbv);
			result = errorValue;
		}
	}
	return result;
}	

/**
 * This function reads a double word from the database. If required it converts it to a double word value
 * @param	hContact		- handle to the contact
 * @param	pszModule		- the module to read the setting from (e.g. USERINFO)
 * @param	pszSetting		- the setting to read
 * @param	errorValue		- value to return if something goes wrong
 *
 * @return	double word value
 **/
DWORD	GetDWord(HANDLE hContact, LPCSTR pszModule, LPCSTR pszSetting, DWORD errorValue)
{
	DBVARIANT dbv;
	DWORD result;
	if (GetAsIs(hContact, pszModule, pszSetting, &dbv)) {
		result = errorValue;
	}
	else {
		switch (dbv.type) {
		case DBVT_BYTE:
			result = 0x000000ff & dbv.bVal;
			break;
		case DBVT_WORD:
			result = 0x0000ffff & dbv.wVal;
			break;
		case DBVT_DWORD:
			result = dbv.dVal;
			break;
		default:
			DB::Variant::Free(&dbv);
			result = errorValue;
		}
	}
	return result;
}

/**
 * This function calls MS_DB_CONTACT_WRITESETTING to write a DBVARIANT structure to the database.
 * @param	hContact		- handle to the contact
 * @param	pszModule		- the module to read the setting from (e.g. USERINFO)
 * @param	pszSetting		- the setting to write
 * @param	dbv				- the DBVARIANT to store
 *
 * @retval	0 - success
 * @retval	1 - error
 **/
BYTE	WriteVariant(HANDLE hContact, LPCSTR pszModule, LPCSTR pszSetting, const DBVARIANT *dbv)
{
	DBCONTACTWRITESETTING cws;

	cws.szModule	= pszModule;
	cws.szSetting	= pszSetting;
	memcpy(&cws.value, dbv, sizeof(DBVARIANT));
	return CallService(MS_DB_CONTACT_WRITESETTING, (WPARAM) hContact, (LPARAM) &cws) != 0;
}

/**
 * This function calls MS_DB_CONTACT_WRITESETTING to write a BYTE to the database.
 * @param	hContact		- handle to the contact
 * @param	pszModule		- the module to read the setting from (e.g. USERINFO)
 * @param	pszSetting		- the setting to write
 * @param	value			- the byte to store
 *
 * @retval	0 - success
 * @retval	1 - error
 **/
BYTE	WriteByte(HANDLE hContact, LPCSTR pszModule, LPCSTR pszSetting, BYTE value)
{
	DBCONTACTWRITESETTING cws;

	cws.szModule	= pszModule;
	cws.szSetting	= pszSetting;
	cws.value.type	= DBVT_BYTE;
	cws.value.bVal	= value;
	return CallService(MS_DB_CONTACT_WRITESETTING, (WPARAM) hContact, (LPARAM) &cws) != 0;
}

/**
 * This function calls MS_DB_CONTACT_WRITESETTING to write a WORD to the database.
 * @param	hContact		- handle to the contact
 * @param	pszModule		- the module to read the setting from (e.g. USERINFO)
 * @param	pszSetting		- the setting to write
 * @param	value			- the word to store
 *
 * @retval	0 - success
 * @retval	1 - error
 **/
BYTE	WriteWord(HANDLE hContact, LPCSTR pszModule, LPCSTR pszSetting, WORD value)
{
	DBCONTACTWRITESETTING cws;

	cws.szModule	= pszModule;
	cws.szSetting	= pszSetting;
	cws.value.type	= DBVT_WORD;
	cws.value.wVal	= value;
	return CallService(MS_DB_CONTACT_WRITESETTING, (WPARAM) hContact, (LPARAM) &cws) != 0;
}

/**
 * This function calls MS_DB_CONTACT_WRITESETTING to write a DWORD to the database.
 * @param	hContact		- handle to the contact
 * @param	pszModule		- the module to read the setting from (e.g. USERINFO)
 * @param	pszSetting		- the setting to write
 * @param	value			- the double word to store
 *
 * @retval	0 - success
 * @retval	1 - error
 **/
BYTE	WriteDWord(HANDLE hContact, LPCSTR pszModule, LPCSTR pszSetting, DWORD value)
{
	DBCONTACTWRITESETTING cws;

	cws.szModule	= pszModule;
	cws.szSetting	= pszSetting;
	cws.value.type	= DBVT_DWORD;
	cws.value.dVal	= value;
	return CallService(MS_DB_CONTACT_WRITESETTING, (WPARAM) hContact, (LPARAM) &cws) != 0;
}

/**
 * This function calls MS_DB_CONTACT_WRITESETTING to write an ansi string to the database.
 * @param		hContact	- handle to the contact
 * @param		pszModule	- the module to read the setting from (e.g. USERINFO)
 * @param		pszSetting	- the setting to write
 * @param		value		- the string to store
 *
 * @retval	0 - success
 * @retval	1 - error
 **/
BYTE	WriteAString(HANDLE hContact, LPCSTR pszModule, LPCSTR pszSetting, LPSTR value)
{
	DBCONTACTWRITESETTING cws;
	cws.szModule		= pszModule;
	cws.szSetting		= pszSetting;
	cws.value.type		= DBVT_ASCIIZ;
	cws.value.pszVal	= value;
	return CallService(MS_DB_CONTACT_WRITESETTING, (WPARAM) hContact, (LPARAM) &cws) != 0;
}

/**
 * This function calls MS_DB_CONTACT_WRITESETTING to write an unicode string to the database.
 * @param	hContact		- handle to the contact
 * @param	pszModule		- the module to read the setting from (e.g. USERINFO)
 * @param	pszSetting		- the setting to write
 * @param	value			- the string to store
 *
 * @retval	0 - success
 * @retval	1 - error
 **/
BYTE	WriteWString(HANDLE hContact, LPCSTR pszModule, LPCSTR pszSetting, LPWSTR value)
{
	DBCONTACTWRITESETTING cws;
	cws.szModule		= pszModule;
	cws.szSetting		= pszSetting;
	cws.value.type		= DBVT_WCHAR;
	cws.value.pwszVal	= value;
	return CallService(MS_DB_CONTACT_WRITESETTING, (WPARAM) hContact, (LPARAM) &cws) != 0;
}

/**
 * This function calls MS_DB_CONTACT_WRITESETTING to write an utf8 string to the database.
 * @param	hContact		- handle to the contact
 * @param	pszModule		- the module to read the setting from (e.g. USERINFO)
 * @param	pszSetting		- the setting to write
 * @param	value			- the string to store
 *
 * @retval	0 - success
 * @retval	1 - error
 **/
BYTE	WriteUString(HANDLE hContact, LPCSTR pszModule, LPCSTR pszSetting, LPSTR value)
{
	DBCONTACTWRITESETTING cws;
	cws.szModule		= pszModule;
	cws.szSetting		= pszSetting;
	cws.value.type		= DBVT_UTF8;
	cws.value.pszVal	= value;
	return CallService(MS_DB_CONTACT_WRITESETTING, (WPARAM) hContact, (LPARAM) &cws) != 0;
}

/**
 * This function checks for the existence of the given setting in the database
 * @param	hContact		- handle to the contact
 * @param	pszModule		- the module to read the setting from (e.g. USERINFO)
 * @param	pszSetting		- the setting to check
 *
 * @retval	TRUE			- setting exists
 * @retval	FALSE			- setting does not exist
 **/
BYTE	Exists(HANDLE hContact, LPCSTR pszModule, LPCSTR pszSetting)
{
	if (pszModule && pszSetting) {
		DBCONTACTGETSETTING cgs;
		DBVARIANT dbv;
		CHAR szDummy[1];

		dbv.pszVal		= szDummy;
		dbv.cchVal		= sizeof(szDummy);
		dbv.type		= 0;
		cgs.pValue		= &dbv;
		cgs.szModule	= pszModule;
		cgs.szSetting	= pszSetting;
		if (!CallService(MS_DB_CONTACT_GETSETTINGSTATIC, (WPARAM) hContact, (LPARAM) &cgs)) {
			return (dbv.type > DBVT_DELETED);
		}
	}
	return FALSE;
}

/**
 * This function deletes the given setting from database 
 * @param	hContact		- handle to the contact
 * @param	pszModule		- the module to read the setting from (e.g. USERINFO)
 * @param	pszSetting		- the setting to read
 *
 * @retval	0 - success
 * @retval	1 - failure
 **/
BYTE	Delete(HANDLE hContact, LPCSTR pszModule, LPCSTR pszSetting)
{
	DBCONTACTGETSETTING cgs;
	cgs.szModule	= pszModule;
	cgs.szSetting	= pszSetting;
	return CallService(MS_DB_CONTACT_DELETESETTING, (WPARAM) hContact, (LPARAM) &cgs) != 0;
}

/**
 * This function deletes all reluctant settings of an setting array such as My-phoneXX.
 * @param	hContact		- handle to the contact
 * @param	pszModule		- the module to read the setting from (e.g. USERINFO)
 * @param	pszFormat		- the format, telling what a array of settings is ment
 * @param	iStart			- the first index of the setting to delete
 *
 * @return	nothing
 **/
void	DeleteArray(HANDLE hContact, LPCSTR pszModule, LPCSTR pszFormat, int iStart)
{
	CHAR pszSetting[MAXSETTING];
	do {
		mir_snprintf(pszSetting, MAXSETTING, pszFormat, iStart++);
	}
	while (!DB::Setting::Delete(hContact, pszModule, pszSetting));
}

/**
 * This function can prevent a setting from being stored to database permanently.
 * @param	pszSetting		- the setting to read
 * @param	enabled			- if set to 'true' the setting will not be stored in database
 *
 * @retval	0 - success
 * @retval	1 - failure
 **/
BYTE	Resident(LPCSTR pszSetting, const bool enabled)
{
	return CallService(MS_DB_SETSETTINGRESIDENT, (WPARAM) enabled, (LPARAM) pszSetting) != 0;
}

} /* namespace Setting */

namespace Variant {

BYTE	Free(DBVARIANT *dbv)
{
	return CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM) dbv) != 0;
}

/**
 * This function converts a string value of the DBVARIANT to the destination type
 * but keeps all other values as is.
 * @param		dbv			- pointer to DBVARIANT structure which is to manipulate
 * @param		destType	- one of (DBVT_ASCIIZ, DBVT_UTF8 or DBVT_WCHAR)
 *
 * @retval		0			- success
 * @retval		1			- error
 **/
BYTE	ConvertString(DBVARIANT* dbv, const BYTE destType)
{
	if (dbv) {
		switch (dbv->type) {
		// source value is of type "ascii"
		case DBVT_ASCIIZ:
			{
				switch (destType) {
				// destination type is "utf8"
				case DBVT_UTF8:
					{
						LPSTR tmpBuf = mir_utf8encode(dbv->pszVal);
						mir_free(dbv->pszVal);
						dbv->pszVal = tmpBuf;
						dbv->type = (dbv->pszVal) ? destType : DBVT_DELETED;
					} break;
				// destination type is "wchar"
				case DBVT_WCHAR:
					{
						LPWSTR tmpBuf = mir_a2u(dbv->pszVal);
						mir_free(dbv->pszVal);
						dbv->pwszVal = tmpBuf;
						dbv->type = (dbv->pwszVal) ? destType : DBVT_DELETED;
					}
				}
			} break;
		// source value is of type "utf8"
		case DBVT_UTF8:
			{
				switch (destType) {
				// destination type is "ascii"
				case DBVT_ASCIIZ:
					{
						mir_utf8decode(dbv->pszVal, NULL);
						dbv->type = (dbv->pszVal) ? destType : DBVT_DELETED;
					} break;
				// destination type is "wchar"
				case DBVT_WCHAR:
					{
						LPSTR savePtr = dbv->pszVal;
						dbv->pszVal = NULL;
						mir_utf8decode(savePtr, &dbv->pwszVal);
						mir_free(savePtr);
						dbv->type = (dbv->pwszVal) ? destType : DBVT_DELETED;
					}
				}
			} break;
		// source value is of type "wchar"
		case DBVT_WCHAR:
			{
			switch (destType) {
				// destination type is "ascii"
				case DBVT_ASCIIZ:
					{
						LPSTR tmpBuf = mir_u2a(dbv->pwszVal);
						mir_free(dbv->pwszVal);
						dbv->pszVal = tmpBuf;
						dbv->type = (dbv->pszVal) ? destType : DBVT_DELETED;
					} break;
				// destination type is "utf8"
				case DBVT_UTF8:
					{
						LPSTR tmpBuf = mir_utf8encodeW(dbv->pwszVal);
						mir_free(dbv->pwszVal);
						dbv->pszVal = tmpBuf;
						dbv->type = (dbv->pszVal) ? destType : DBVT_DELETED;
					}
				}
			}
		}
		return dbv->type == DBVT_DELETED;
	}
	return 1;
}

/**
 * This function completely converts a DBVARIANT to the destination string type.
 * It includes BYTE, WORD, DWORD and all string types
 * @param		dbv			- pointer to DBVARIANT structure which is to manipulate
 * @param		destType	- one of (DBVT_ASCIIZ, DBVT_UTF8 or DBVT_WCHAR)
 *
 * @retval	0 - success
 * @retval	1 - error
 **/
BYTE	dbv2String(DBVARIANT* dbv, const BYTE destType)
{
	if (dbv) {
		switch (destType) {
		// destination type is "utf8" or "ascii"
		case DBVT_ASCIIZ:
		case DBVT_UTF8:
			{
				CHAR buf[32];
				switch (dbv->type) {
				// source value is of type "byte"
				case DBVT_BYTE:
					{
						_ultoa(dbv->bVal, buf, 10);
						dbv->pszVal = mir_strdup(buf);
						dbv->type = (dbv->pszVal) ? destType : DBVT_DELETED;
					} break;
				// source value is of type "word"
				case DBVT_WORD:
					{
						_ultoa(dbv->wVal, buf, 10);
						dbv->pszVal = mir_strdup(buf);
						dbv->type = (dbv->pszVal) ? destType : DBVT_DELETED;
					} break;
				// source value is of type "dword"
				case DBVT_DWORD:
					{
						_ultoa(dbv->dVal, buf, 10);
						dbv->pszVal = mir_strdup(buf);
						dbv->type = (dbv->pszVal) ? destType : DBVT_DELETED;
					} break;
				// source value is of any string type
				case DBVT_ASCIIZ:
				case DBVT_WCHAR:
				case DBVT_UTF8:
					{
						return ConvertString(dbv, destType);
					}
				}
			} break;
		// destination type is "wchar"
		case DBVT_WCHAR:
			{
				WCHAR buf[32];
				switch (dbv->type) {
				// source value is of type "byte"
				case DBVT_BYTE:
					{
						_ultow(dbv->bVal, buf, 10);
						dbv->pwszVal = mir_wcsdup(buf);
						dbv->type = (dbv->pwszVal) ? destType : DBVT_DELETED;
					} break;
				// source value is of type "word"
				case DBVT_WORD:
					{
						_ultow(dbv->wVal, buf, 10);
						dbv->pwszVal = mir_wcsdup(buf);
						dbv->type = (dbv->pwszVal) ? destType : DBVT_DELETED;
					} break;
				// source value is of type "dword"
				case DBVT_DWORD:
					{
						_ultow(dbv->dVal, buf, 10);
						dbv->pwszVal = mir_wcsdup(buf);
						dbv->type = (dbv->pwszVal) ? destType : DBVT_DELETED;
					} break;
				// source value is of any string type
				case DBVT_ASCIIZ:
				case DBVT_WCHAR:
				case DBVT_UTF8:
					{
						return ConvertString(dbv, destType);
					}
				}
			}
		}
		return dbv->type != destType;
	}
	return 1;
}

} /* namespace Variant */

namespace Event {

/**
 * This function initializes the DBEVENTINFO structure and calls 
 * db_event_get() to retrieve information about an event.
 * @param	hEvent			- the handle of the event to get information for
 * @param	dbei			- the pointer to a DBEVENTINFO structure, which retrieves all information.
 *
 * @retval	0				- success
 * @retval	nonezero		- failure
 **/
BYTE	GetInfo(HANDLE hEvent, DBEVENTINFO *dbei)
{
	dbei->cbSize = sizeof(DBEVENTINFO);
	dbei->cbBlob = 0;
	dbei->pBlob  = NULL;
	return db_event_get(hEvent, dbei) != 0;
}

/**
 * This function initializes the DBEVENTINFO structure and calls 
 * db_event_get() to retrieve information about an event.
 * @param	hEvent			- the handle of the event to get information for
 * @param	dbei			- the pointer to a DBEVENTINFO structure, which retrieves all information.
 *
 * @retval	0				- success
 * @retval	1				- failure
 **/
BYTE	GetInfoWithData(HANDLE hEvent, DBEVENTINFO *dbei)
{
	dbei->cbSize = sizeof(DBEVENTINFO);
	if (!dbei->cbBlob) {
		INT_PTR size = BlobSizeOf(hEvent);
		dbei->cbBlob = (size != -1) ? (DWORD)size : 0;
	}
	if(dbei->cbBlob) {
		dbei->pBlob = (PBYTE) mir_alloc(dbei->cbBlob);
		if (dbei->pBlob == NULL) {
			dbei->cbBlob = 0;
		}
	}
	else dbei->pBlob = NULL;

	BYTE result = db_event_get(hEvent, dbei) != 0;
	if (result && dbei->pBlob) {
		mir_free(dbei->pBlob);
		dbei->pBlob = NULL;
	}
	return result;
}

/**
 * This function returns the timestamp for the given event.
 * @param	hEvent			- the handle of the event to get the timestamp for
 *
 * @retval	0 if no timestamp is available
 * @retval	timestamp
 **/
DWORD	TimeOf(HANDLE hEvent)
{
	DBEVENTINFO dbei;
	if (!GetInfo(hEvent, &dbei)) {
		return dbei.timestamp;
	}
	return 0;
}

/**
 * This function returns the number of bytes required to retrieve
 * binary data associated with the event.
 * @param	hEvent			- the handle of the event to get the number of bytes for
 *
 * @retval	size of event data
 * @retval	-1 if hEvent is invalid
 **/
INT_PTR	BlobSizeOf(HANDLE hEvent)
{
	return db_event_getBlobSize(hEvent);
}

/**
 * This function compares two DBEVENTINFO structures against each other.
 * It compares the timestamp, eventType and module names.
 * @param	d1				- pointer to the first DBEVENTINFO structure
 * @param	d2				- pointer to the second DBEVENTINFO structure
 * @param	Data			- default false, if true compare also blob data
.*
 * @retval	TRUE			- The structures describe the same event.
 * @retval	FALSE			- The two structure's events differ from each other.
 **/
static	FORCEINLINE
BYTE	IsEqual(const DBEVENTINFO *d1, const DBEVENTINFO *d2, bool Data)
{
	BYTE res = d1 && d2 && 
				(d1->timestamp == d2->timestamp) && 
				(d1->eventType == d2->eventType) &&
				(d1->cbBlob == d2->cbBlob) && 
				(!d1->szModule || !d2->szModule || !_stricmp(d1->szModule, d2->szModule))
				;
	if(Data) {
			return res &&
				(!d1->pBlob || !d2->pBlob || !memcmp(d1->pBlob,d2->pBlob,d1->cbBlob))
				;
	}
	return res;
}

/**
 * This functions searches for an existing event in the database, which matches
 * the information provided by 'dbei'. In order to fasten up the search e.g. 
 * while checking many events, this function stars searching from the last
 * found event.
 * @param	hContact			- the handle of the contact to search events for
 * @param	hDbExistingEvent	- an existing database event to start the search from.
 * @param	dbei				- the pointer to a DBEVENTINFO structure
 *
 * @retval	TRUE	- the event identified by its information in @c dbei exists.
 * @retval	FALSE	- no event with the information of @c dbei exists.
 *
 **/
BYTE	Exists(HANDLE hContact, HANDLE& hDbExistingEvent, DBEVENTINFO *dbei)
{
	BYTE		result = FALSE;
	DBEVENTINFO	edbei;
	HANDLE		sdbe,
				edbe;

	if (!hDbExistingEvent) {
		hDbExistingEvent = db_event_first(hContact);
		if (hDbExistingEvent) {
			if (!GetInfo(hDbExistingEvent, &edbei)) {
				if ((dbei->timestamp < edbei.timestamp)) {
					return FALSE;
				}
				if(IsEqual(dbei, &edbei, false)) {
					if (!GetInfoWithData(hDbExistingEvent, &edbei)) {
						if(IsEqual(dbei, &edbei, true)) {
							mir_free(edbei.pBlob);
							return TRUE;
						}
						mir_free(edbei.pBlob);
					}
				}
			}
			edbe = db_event_last(hContact);
			if (edbe == hDbExistingEvent)
				return FALSE;

			hDbExistingEvent = edbe;
		}
	}
	if (hDbExistingEvent) {
		sdbe = hDbExistingEvent;
		for (	edbe = sdbe;
				edbe && !GetInfo(edbe, &edbei) && (dbei->timestamp <= edbei.timestamp);
				edbe = db_event_prev(edbe)) {
			hDbExistingEvent = edbe;
			//compare without data (faster)
			if ( result = IsEqual(dbei, &edbei, false)) {
				if(NULL == (result = !GetInfoWithData(edbe, &edbei))) continue;
				//compare with data
				result = IsEqual(dbei, &edbei, true);
				mir_free(edbei.pBlob);
				if (result) {
					break;
				}
			}
		} /*end for*/

		if (!result) {
			for (	edbe = db_event_next(sdbe);
					edbe && !GetInfo(edbe, &edbei) && (dbei->timestamp >= edbei.timestamp);
					edbe = db_event_next(edbe)) {
				hDbExistingEvent = edbe;
				//compare without data (faster)
				if ( result = IsEqual(dbei, &edbei, false)) {
					if(NULL == (result = !GetInfoWithData(edbe, &edbei))) continue;
					//compare with data
					result = IsEqual(dbei, &edbei, true);
					mir_free(edbei.pBlob);
					if (result) {
						break;
					}
				}
			}
		}
	}
	return result;
}

} /* namespace Events */

int CEnumList::EnumProc(LPCSTR pszName, DWORD ofsModuleName, LPARAM lParam)
{
	if (pszName) {
		((CEnumList*)lParam)->Insert(pszName);
	}
	return 0;
}

int CEnumList::EnumSettingsProc(LPCSTR pszName, LPARAM lParam)
{
	return EnumProc(pszName, 0, lParam);
}

int CEnumList::CompareProc(LPCSTR p1, LPCSTR p2)
{
	if (p1) {
		if (p2) {
			return strcmp(p1, p2);
		}
		return 1;
	}
	return 0;
}

CEnumList::CEnumList()	: LIST<CHAR>(50, (FTSortFunc)CEnumList::CompareProc)
{
}

CEnumList::~CEnumList() 
{ 
	int i, cnt;
	LPSTR p;

	for (i = 0, cnt = getCount(); i < cnt; i++) {
		p = (*this)[i]; 
		if (p) {
			mir_free(p);
		}
	}
	destroy();
}

LPSTR CEnumList::Insert(LPCSTR str)
{
	LPSTR p = mir_strdup(str);
	if (p && !insert(p)) {
		mir_free(p);
		p = NULL;
	}
	return p;
}

INT_PTR CEnumList::EnumModules()
{
	return CallService(MS_DB_MODULES_ENUM, (WPARAM)this, (LPARAM)CEnumList::EnumProc);
}

/**
 * @retval	-1	- no settings to enumerate
 * @retval	 0	- success
 **/
INT_PTR CEnumList::EnumSettings(HANDLE hContact, LPCSTR pszModule)
{
	DBCONTACTENUMSETTINGS dbces;
	
	dbces.pfnEnumProc = (DBSETTINGENUMPROC)CEnumList::EnumSettingsProc;
	dbces.szModule = pszModule;
	dbces.lParam = (LPARAM)this;
	dbces.ofsSettings = 0;
	return CallService(MS_DB_CONTACT_ENUMSETTINGS, (WPARAM)hContact, (LPARAM)&dbces);
}

} /* namespace DB */