summaryrefslogtreecommitdiff
path: root/plugins/MirandaG15/src/CContactList.cpp
blob: 7b99448c126b6f7c0009f24ed7a41f3bbd59c436 (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
#include "stdafx.h"
#include "CContactList.h"
#include "CConfig.h"
#include "CAppletManager.h"

const int aiStatusPriority[] = {	0,	// ID_STATUS_OFFLINE               40071
							9,	// ID_STATUS_ONLINE                40072
							8,	// ID_STATUS_AWAY                  40073
							1,	// ID_STATUS_DND                   40074
							7,	// ID_STATUS_NA                    40075
							6,	// ID_STATUS_OCCUPIED              40076
							10,	// ID_STATUS_FREECHAT              40077
							9,	// ID_STATUS_INVISIBLE             40078
							8,	// ID_STATUS_ONTHEPHONE            40079
							8 	// ID_STATUS_OUTTOLUNCH            40080
							};

//************************************************************************
// constructor
//************************************************************************
CContactList::CContactList()
{
	m_bUseGroups = false;
	m_bUseMetaContacts = false;
	m_dwLastScroll = 0;
}

//************************************************************************
// destructor
//************************************************************************
CContactList::~CContactList()
{
}

//************************************************************************
// initializes the list
//************************************************************************
bool CContactList::Initialize()
{
	if(!CLCDList<CContactListEntry*,CContactListGroup*>::Initialize())
		return false;

	InitializeGroupObjects();

	RefreshList();

	return true;
}

//************************************************************************
// deinitializes the list
//************************************************************************
bool CContactList::Shutdown()
{
	if(!CLCDList<CContactListEntry*,CContactListGroup*>::Shutdown())
		return false;

	UninitializeGroupObjects();

	return false;
}



//************************************************************************
// returns the contacts ccontactlistentry class
//************************************************************************
CContactListEntry *CContactList::GetContactData(CListEntry<CContactListEntry*,CContactListGroup*> *pEntry)
{
	if(pEntry->GetType() == ITEM)
		return ((CListItem<CContactListEntry*,CContactListGroup*>*)pEntry)->GetItemData();
	else
		return ((CListContainer<CContactListEntry*,CContactListGroup*>*)pEntry)->GetGroupData()->pContactListEntry;
}



//************************************************************************
// returns the contacts group path
//************************************************************************
tstring CContactList::GetContactGroupPath(MCONTACT hContact)
{
	tstring strGroup = _T("");
	if(db_get_b(0, "MetaContacts", "Enabled", 1) && db_mc_isSub(hContact))
	{
		MCONTACT hMetaContact = db_mc_getMeta(hContact);
		if(CConfig::GetBoolSetting(CLIST_USEGROUPS))
			strGroup = CAppletManager::GetContactGroup(hMetaContact);

		tstring strMetaName = CAppletManager::GetContactDisplayname(hMetaContact);
		strGroup += (strGroup.empty()?_T(""):_T("\\"))+ strMetaName;
	}
	else
		strGroup = CAppletManager::GetContactGroup(hContact);
	return strGroup;
}

//************************************************************************
// adds a contact to the list
//************************************************************************
void CContactList::AddContact(MCONTACT hContact)
{
	CListContainer<CContactListEntry*,CContactListGroup*> *pGroup = NULL;

	tstring strName = CAppletManager::GetContactDisplayname(hContact);
	char *szProto = GetContactProto(hContact);
	
	tstring strGroup = GetContactGroupPath(hContact);
	// ignore contacts without a valid protocoll
	if(szProto == NULL)
		return;

	int iStatus = db_get_w(hContact,szProto,"Status",ID_STATUS_OFFLINE);
	char *szStatus = (char *) CallService(MS_CLIST_GETSTATUSMODEDESCRIPTION, iStatus, 0);

	CContactListEntry *psContact = new CContactListEntry();

	psContact->strName = strName;
	psContact->iMessages = 0;
	psContact->hHandle = hContact;
	
	psContact->iStatus = iStatus;
	
	if(szStatus != NULL)
		psContact->strStatus =toTstring(szStatus);
	psContact->strProto = toTstring(szProto);

	// check wether the contact should be listed
	if(!IsVisible(psContact)) {
		delete psContact;
		return;
	}

	// Don't add metacontacts as contacts
	if(!mir_strcmpi(szProto,"MetaContacts"))
	{
		if(!CConfig::GetBoolSetting(CLIST_USEGROUPS))
			strGroup = _T("");
		strGroup += (strGroup.empty()?_T(""):_T("\\"))+psContact->strName;
		pGroup = GetGroupByString(strGroup);
		if(pGroup == NULL)
			pGroup = AddGroupByString(strGroup);
		pGroup->GetGroupData()->hMetaContact = hContact;
		pGroup->GetGroupData()->pContactListEntry = psContact;

		pGroup = (CListContainer<CContactListEntry*,CContactListGroup*>*)pGroup->GetParent();
		if(pGroup->GetType() != ROOT && iStatus != ID_STATUS_OFFLINE)
			ChangeGroupObjectCounters(pGroup->GetGroupData()->strPath,0,1);

		pGroup->sort(CContactList::CompareEntries);

		// check that all subcontacts exist
		int numContacts = db_mc_getSubCount(hContact);
		MCONTACT hSubContact = NULL;
		for(int i=0;i<numContacts;i++) {
			hSubContact = db_mc_getSub(hContact, i);
			RemoveContact(hSubContact);
			AddContact(hSubContact);
		}
		return;
	}
	else if(db_mc_isSub(hContact)) {
		MCONTACT hMetaContact = db_mc_getMeta(hContact);
		// check that the metacontact exists
		if(!FindContact(hMetaContact))
			AddContact(hMetaContact);
	}

	CListItem<CContactListEntry*,CContactListGroup*> *pItem = NULL;
	if((!db_mc_isSub(hContact) && !CConfig::GetBoolSetting(CLIST_USEGROUPS)) || strGroup.empty())
	{
		pItem = AddItem(psContact);
		((CListContainer<CContactListEntry*,CContactListGroup*>*)this)->sort(CContactList::CompareEntries);
	}
	else
	{
		pGroup = GetGroupByString(strGroup);
		if(pGroup == NULL) {
			pGroup = AddGroupByString(strGroup);
		}
		pItem = pGroup->AddItem(psContact);
	
		if(!db_mc_isSub(hContact) && iStatus != ID_STATUS_OFFLINE)
			ChangeGroupObjectCounters(pGroup->GetGroupData()->strPath,0,1);

		pGroup->sort(CContactList::CompareEntries);
	}

	UpdateMessageCounter((CListEntry<CContactListEntry*,CContactListGroup*>*)pItem);
}

//************************************************************************
// returns wether a contact should be listed or not
//************************************************************************
bool CContactList::IsVisible(CContactListEntry *pEntry) {
	if(!pEntry) {
		return false;
	}
	
	if(pEntry->strProto != _T("MetaContacts")) {
		if(pEntry->iStatus == ID_STATUS_OFFLINE && CConfig::GetBoolSetting(CLIST_HIDEOFFLINE)) {
			return false;
		}
	} else {
		if(pEntry->iStatus == ID_STATUS_OFFLINE) {
			int dwNumContacts = db_mc_getSubCount(pEntry->hHandle);
			for(int i = 0; i < dwNumContacts; i++) {
				MCONTACT hSubContact = db_mc_getSub(pEntry->hHandle,i);
				char *szProto = GetContactProto(hSubContact);
				if(db_get_w(hSubContact,szProto,"Status",ID_STATUS_OFFLINE) != ID_STATUS_OFFLINE)
					return true;
			}
		}
	}

	if(pEntry->iMessages > 0)
		return true;

	if(CConfig::GetBoolSetting(CLIST_USEIGNORE)) {	
		if(db_get_b(pEntry->hHandle,"CList","Hidden",0))
			return false;
		
		if(db_mc_isSub(pEntry->hHandle)) {
			MCONTACT hMetaContact = db_mc_getMeta(pEntry->hHandle);
			if(db_get_b(hMetaContact,"CList","Hidden",0))
				return false;
		}
	}


	if(!CConfig::GetProtocolContactlistFilter(pEntry->strProto))
		return false;

	if(CConfig::GetBoolSetting(CLIST_HIDEOFFLINE) && pEntry->iStatus == ID_STATUS_OFFLINE)
		return false;

	return true;
}

//************************************************************************
// removes a contact from the list
//************************************************************************
void CContactList::RemoveContact(MCONTACT hContact) {
	CListContainer<CContactListEntry*,CContactListGroup*> *pGroup = NULL;
	
	///tstring strGroup = GetContactGroupPath(hContact);

	CListEntry<CContactListEntry*,CContactListGroup*> *pContactEntry = FindContact(hContact);
	if(!pContactEntry) {
		return;
	}

	if( !CConfig::GetBoolSetting(CLIST_USEGROUPS)){
		if(pContactEntry->GetType() == ITEM)
			RemoveItem(((CListItem<CContactListEntry*,CContactListGroup*>*)pContactEntry)->GetItemData());
		else
			RemoveGroup(((CListContainer<CContactListEntry*,CContactListGroup*>*)pContactEntry)->GetGroupData());
	} else {
		pGroup = (CListContainer<CContactListEntry*,CContactListGroup*>*)pContactEntry->GetParent();
		ASSERT(pGroup != NULL);
		
		CContactListEntry *pEntry = GetContactData(pContactEntry);
		if(!pEntry) {
			return;
		}
		// Update the contacts group if it has one
		if(pGroup->GetType() != ROOT)
		{
			if(!db_mc_isSub(hContact) && pEntry->iStatus != ID_STATUS_OFFLINE)
				ChangeGroupObjectCounters(pGroup->GetGroupData()->strPath,0,-1);
			
			if(!db_mc_isSub(hContact) && pEntry->iMessages > 0)
				ChangeGroupObjectCounters(pGroup->GetGroupData()->strPath,0,0,-pEntry->iMessages);
		}

		if(pContactEntry->GetType() == ITEM)
			pGroup->RemoveItem(((CListItem<CContactListEntry*,CContactListGroup*>*)pContactEntry)->GetItemData());
		else {
			pGroup->RemoveGroup(((CListContainer<CContactListEntry*,CContactListGroup*>*)pContactEntry)->GetGroupData());
			// Reenumerate all subcontacts (maybe MetaContacts was disabled
			int numContacts = db_mc_getSubCount(hContact);
			for(int i=0;i<numContacts;i++) {
				MCONTACT hSubContact = db_mc_getSub(hContact, i);
				if(!FindContact(hSubContact))
					AddContact(hSubContact);
			}
		}

		CListContainer<CContactListEntry*,CContactListGroup*> *pParent = (CListContainer<CContactListEntry*,CContactListGroup*>*)pGroup->GetParent();
		while(pParent != NULL && pGroup->IsEmpty() && !pGroup->GetGroupData()->hMetaContact)
		{
			pParent->RemoveGroup(pGroup->GetGroupData());
			pGroup = pParent;
			pParent = (CListContainer<CContactListEntry*,CContactListGroup*>*)pGroup->GetParent();
		}
	}
}

//************************************************************************
// get group by string
//************************************************************************
CListContainer<CContactListEntry*,CContactListGroup*> *CContactList::GetGroupByString(tstring strGroup)
{
	tstring strParse = strGroup;
	CListContainer<CContactListEntry*,CContactListGroup*> *pGroup = (CListContainer<CContactListEntry*,CContactListGroup*>*)this;
	tstring::size_type pos;
	while((pos = strParse.find('\\')) !=  tstring::npos )
	{
		strGroup = strParse.substr(0,pos);
		strParse = strParse.substr(pos+1);
		
		pGroup = FindGroupInGroup(strGroup,pGroup);
		if(pGroup == NULL)
			return NULL;
	}
	pGroup = FindGroupInGroup(strParse,pGroup);
	return pGroup;
}

//************************************************************************
// Adds a group
//************************************************************************
CListContainer<CContactListEntry*,CContactListGroup*> *CContactList::AddGroupByString(tstring strGroup)
{
	tstring strParse = strGroup;
	tstring strPath = _T("");

	CListContainer<CContactListEntry*,CContactListGroup*> *pGroup = (CListContainer<CContactListEntry*,CContactListGroup*>*)this;
	CListContainer<CContactListEntry*,CContactListGroup*> *pGroup2 = NULL;
	tstring::size_type pos;
	while((pos = strParse.find('\\')) !=  tstring::npos )
	{
		strGroup = strParse.substr(0,pos);
		strParse = strParse.substr(pos+1);
		strPath += strGroup;

		if(pGroup2 = FindGroupInGroup(strGroup,pGroup))
			pGroup = pGroup2;
		else
		{
			CContactListGroup *pGroupObject = GetGroupObjectByPath(strPath);
			if(!pGroupObject)
				pGroupObject = CreateGroupObjectByPath(strPath);
			pGroup2 = pGroup->InsertGroup(pGroup->begin(),pGroupObject);
			pGroup->sort(CContactList::CompareEntries);
			pGroup = pGroup2;
		}
		ASSERT(pGroup != NULL);
		strPath += _T("\\");
	}
	strPath += strParse;
	if(pGroup2 = FindGroupInGroup(strParse,pGroup))
		return pGroup2;
	else
	{
		CContactListGroup *pGroupObject = GetGroupObjectByPath(strPath);
		if(!pGroupObject)
			pGroupObject = CreateGroupObjectByPath(strPath);
		pGroup2 = pGroup->InsertGroup(pGroup->begin(),pGroupObject);
		pGroup->sort(CContactList::CompareEntries);
		return pGroup2;
	}
}

//************************************************************************
// returns the contact's status
//************************************************************************
int CContactList::GetContactStatus(MCONTACT hContact)
{
	CListEntry<CContactListEntry *,CContactListGroup*> *pContactEntry = FindContact(hContact);
	if(!pContactEntry)
		return ID_STATUS_OFFLINE;

	
	CContactListEntry *pEntry = GetContactData(pContactEntry);
	if(!pEntry) {
		return ID_STATUS_OFFLINE;
	}
	return pEntry->iStatus;
}



//************************************************************************
// Called to delete the specified item
//************************************************************************
void CContactList::DeleteItem(CContactListEntry *pEntry)
{
	delete pEntry;
}

//************************************************************************
// Called to delete the specified group
//************************************************************************
void CContactList::DeleteGroup(CContactListGroup *pGroup)
{
}

//************************************************************************
// Called to draw the specified entry
//************************************************************************
void CContactList::DrawEntry(CLCDGfx *pGfx,CContactListEntry *pEntry,bool bSelected)
{
	if(pEntry == NULL)
		return;

	int iOffset = 0;
	tstring strText = _T("");
	if(pEntry->iMessages > 0) {
		strText = _T("[");
		strText += pEntry->strMessages;
		strText += _T("]");
	}
	strText += pEntry->strName;

	if(CConfig::GetBoolSetting(CLIST_SHOWPROTO) && !CConfig::GetBoolSetting(CLIST_COLUMNS)) {
		int w = pGfx->GetClipWidth();
		pGfx->DrawText(w-w*0.3,0,w*0.3,pEntry->strProto);
		pGfx->DrawText(8,0,w*0.7-8,strText);
	}
	else pGfx->DrawText(8,0,pGfx->GetClipWidth()-8,strText);
	
	pGfx->DrawBitmap(1,ceil((pGfx->GetClipHeight()-5)/2.0f),5,5,CAppletManager::GetInstance()->GetStatusBitmap(pEntry->iStatus));

	if(bSelected && (GetTickCount() - m_dwLastScroll < 1000 || !CConfig::GetBoolSetting(CLIST_SELECTION)))
	{
		RECT invert = { 0,0,GetWidth(),m_iFontHeight};
		InvertRect(pGfx->GetHDC(), &invert);
	}
}

//************************************************************************
// Called to draw the specified group
//************************************************************************
void CContactList::DrawGroup(CLCDGfx *pGfx,CContactListGroup *pGroup,bool bOpen,bool bSelected)
{
	if(pGroup == NULL || ( pGroup->hMetaContact && pGroup->pContactListEntry == NULL)) {
		return;
	}

	char num[10],num2[10];
	itoa(pGroup->iMembers,num,10);
	itoa(pGroup->iOnline,num2,10);

	int iEvents = pGroup->iEvents;
	tstring strText = pGroup->strName;

	if(!pGroup->hMetaContact)
	{
		if(CConfig::GetBoolSetting(CLIST_COUNTERS))
			strText = strText + _T(" (") + toTstring(num2).c_str()+ _T("/") + toTstring(num).c_str() + _T(")");
	}
	else
	{
		pGfx->DrawBitmap(8,ceil((pGfx->GetClipHeight()-5)/2.0f),5,5,CAppletManager::GetInstance()->GetStatusBitmap(pGroup->pContactListEntry->iStatus));
		iEvents += pGroup->pContactListEntry->iMessages;
	}

	if(iEvents != 0)
	{
		itoa(iEvents,num,10);
		strText = _T("[") + toTstring(num) + _T("]") + strText;
	}

	int iOffset = !pGroup->hMetaContact?m_iFontHeight*0.8:m_iFontHeight*0.8+8;
	pGfx->DrawText(iOffset,0,pGfx->GetClipWidth()-iOffset,strText.c_str());

	if(bOpen)
		pGfx->DrawText(1,0,_T("-"));
	else
		pGfx->DrawText(1,0,_T("+"));
	
	if(bSelected && (GetTickCount() - m_dwLastScroll < 1000|| !CConfig::GetBoolSetting(CLIST_SELECTION)))
	{
		RECT invert2 = { 0,0,GetWidth(),m_iFontHeight};
		InvertRect(pGfx->GetHDC(),&invert2);
	}
}


//************************************************************************
// Called to compare two entrys
//************************************************************************
bool CContactList::CompareEntries(CListEntry<CContactListEntry*,CContactListGroup*> *pLeft,CListEntry<CContactListEntry*,CContactListGroup*> *pRight)
{
	CContactListEntry *pLeftEntry = GetContactData(pLeft);
	CContactListEntry *pRightEntry = GetContactData(pRight);

	if(pLeftEntry && pRightEntry)
	{
		int iLeftMessages = pLeftEntry->iMessages;
		int iRightMessages = pRightEntry->iMessages;

		if(pLeft->GetType() == CONTAINER)
			iLeftMessages += ((CListContainer<CContactListEntry*,CContactListGroup*>*)pLeft)->GetGroupData()->iEvents;
		if(pRight->GetType() == CONTAINER)
			iRightMessages += ((CListContainer<CContactListEntry*,CContactListGroup*>*)pRight)->GetGroupData()->iEvents;

		if (!iRightMessages && iLeftMessages)
			return true;
		else if (iRightMessages && !iLeftMessages)
			return false;
		else if (iLeftMessages && iRightMessages)
			return (iLeftMessages > iRightMessages);
		else if(pLeftEntry->iStatus != pRightEntry->iStatus)
			return (aiStatusPriority[pLeftEntry->iStatus - ID_STATUS_OFFLINE] > aiStatusPriority[pRightEntry->iStatus - ID_STATUS_OFFLINE]);
		else
			return mir_tstrcmpi(pLeftEntry->strName.c_str(),pRightEntry->strName.c_str())<0;
	}
	else if(pLeft->GetType() == ITEM && pRight->GetType() == CONTAINER)
		return false;
	else if(pLeft->GetType() == CONTAINER && pRight->GetType() == ITEM)
		return true;
	else if(pLeft->GetType() == CONTAINER && pRight->GetType() == CONTAINER)
	{	
		CContactListGroup *pGroup1 = ((CListContainer<CContactListEntry*,CContactListGroup*>*)pLeft)->GetGroupData();
		CContactListGroup *pGroup2 = ((CListContainer<CContactListEntry*,CContactListGroup*>*)pRight)->GetGroupData();

		if (!pGroup2->iEvents && pGroup1->iEvents)
			return true;
		else if (pGroup2->iEvents && !pGroup1->iEvents)
			return false;
		else if (pGroup1->iEvents && pGroup2->iEvents)
			return (pGroup1->iEvents > pGroup2->iEvents);
		else
			return mir_tstrcmpi(pGroup1->strName.c_str(),pGroup2->strName.c_str())<0;
	}

	return false;
}

//************************************************************************
// refreshes the list
//************************************************************************
void CContactList::RefreshList()
{
	if((db_get_b(NULL,"MetaContacts","Enabled",1) != 0) != m_bUseMetaContacts ||
		CConfig::GetBoolSetting(CLIST_USEGROUPS) != m_bUseGroups)
	{
		InitializeGroupObjects();
		Clear();
	}
	m_bUseGroups = CConfig::GetBoolSetting(CLIST_USEGROUPS);
	m_bUseMetaContacts = db_get_b(NULL,"MetaContacts","Enabled",1) != 0;

	CListEntry<CContactListEntry*,CContactListGroup*> *pContactEntry = NULL;
	MCONTACT hContact = db_find_first();
    while(hContact != NULL)
	{
		pContactEntry = FindContact(hContact);
		if(!pContactEntry)
			AddContact(hContact);
		else if(pContactEntry && !IsVisible(GetContactData(pContactEntry)))
			RemoveContact(hContact);
        hContact = db_find_next(hContact);
    }
}

//************************************************************************
// set the contactlists font
//************************************************************************
bool CContactList::SetFont(LOGFONT &lf)
{
	if(!CLCDList::SetFont(lf))
		return false;

	SetEntryHeight(m_iFontHeight<5?5:m_iFontHeight);

	return true;
}

//************************************************************************
// called when the configuration has changed
//************************************************************************
void CContactList::OnConfigChanged()
{
	RefreshList();
}


//************************************************************************
// returns the entry for the specified group name
//************************************************************************
CListContainer<CContactListEntry*,CContactListGroup*> *CContactList::FindGroup(tstring strGroup)
{
	return FindGroupInGroup(strGroup,(CListContainer<CContactListEntry*,CContactListGroup*>*)this);
}

//************************************************************************
// returns the entry for the specified group name
//************************************************************************
CListContainer<CContactListEntry*,CContactListGroup*> *CContactList::FindGroupInGroup(tstring strGroup,CListContainer<CContactListEntry*,CContactListGroup*> *pGroup)
{
	CListContainer<CContactListEntry*,CContactListGroup*>::iterator iter = pGroup->begin();
	CListContainer<CContactListEntry*,CContactListGroup*> *pItem = NULL;
	while(!pGroup->empty() && iter != pGroup->end())
	{
		if((*iter)->GetType() == CONTAINER)
		{
			pItem = (CListContainer<CContactListEntry*,CContactListGroup*>*)*iter;
			if(pItem->GetGroupData()->strName == strGroup)
				return pItem;

			//pItem = FindGroupInGroup(strGroup,(CListContainer<CContactListEntry*,CContactListGroup*> *)*iter);
			//if(pItem)
			//	return pItem;
		}
		iter++;
	}
	return NULL;
}

//************************************************************************
// returns the entry for the specified handle
//************************************************************************
CListEntry<CContactListEntry*,CContactListGroup*> *CContactList::FindContact(MCONTACT hContact)
{
	if(hContact == NULL)
		return NULL;

	return FindContactInGroup(hContact,(CListContainer<CContactListEntry*,CContactListGroup*>*)this);
}

//************************************************************************
// returns the entry for the specified handle
//************************************************************************
CListEntry<CContactListEntry*,CContactListGroup*> *CContactList::FindContactInGroup(MCONTACT hContact,CListContainer<CContactListEntry*,CContactListGroup*> *pGroup)
{
	if(hContact == NULL)
		return NULL;

	CListItem<CContactListEntry*,CContactListGroup*> *pItemEntry = NULL;
	CListEntry<CContactListEntry*,CContactListGroup*> *pEntry = NULL;
	CListContainer<CContactListEntry*,CContactListGroup*> *pGroupEntry = NULL;
	CListContainer<CContactListEntry*,CContactListGroup*>::iterator iter = pGroup->begin();
	while(iter != pGroup->end())
	{
		if((*iter)->GetType() == ITEM)
		{
			pItemEntry = (CListItem<CContactListEntry*,CContactListGroup*>*)*iter;
			if(pItemEntry->GetItemData()->hHandle == hContact)
				return *iter;
		}
		else
		{
			pGroupEntry = (CListContainer<CContactListEntry*,CContactListGroup*> *)*iter;
			if(pGroupEntry->GetGroupData()->hMetaContact == hContact)
				return *iter;

			pEntry = FindContactInGroup(hContact,pGroupEntry);
			if(pEntry)
				return pEntry;
		}
		iter++;
	}
	return NULL;
}


//************************************************************************
// called when a contacts hidden flag has changed
//************************************************************************
void CContactList::OnContactHiddenChanged(MCONTACT hContact, bool bHidden)
{
	CListEntry<CContactListEntry*,CContactListGroup*> *pContactEntry =  FindContact(hContact);

	if(!pContactEntry && !bHidden)
	{
		AddContact(hContact);
		return;
	}
	else if(!pContactEntry)
		return;

	if(!IsVisible(GetContactData(pContactEntry)))
		RemoveContact(hContact);
}

//************************************************************************
// called when a contacts nickname has changed
//************************************************************************
void CContactList::OnContactNickChanged(MCONTACT hContact, tstring strNick)
{
	CListEntry<CContactListEntry *,CContactListGroup*> *pContactEntry = FindContact(hContact);
	if(!pContactEntry)
		return;
	
	if(pContactEntry->GetType() == CONTAINER)
	{
		CListContainer *pGroup = ((CListContainer<CContactListEntry*,CContactListGroup*>*)pContactEntry);
		pGroup->GetGroupData()->strName = strNick;
		tstring strPath =  GetContactGroupPath(hContact);
		pGroup->GetGroupData()->strPath = strPath + (strPath.empty()?_T(""):_T("\\")) + strNick;
	}

	CContactListEntry* pEntry = GetContactData(pContactEntry);
	if(!pEntry) {
		return;
	}

	pEntry->strName = strNick;
	((CListContainer<CContactListEntry*,CContactListGroup*>*)pContactEntry->GetParent())->sort(CContactList::CompareEntries);
}

//************************************************************************
// called when a contacts status has changed
//************************************************************************
void CContactList::OnStatusChange(MCONTACT hContact,int iStatus)
{
	// find the entry in the list
	CListEntry<CContactListEntry *,CContactListGroup*> *pContactEntry = FindContact(hContact);
	if(!pContactEntry)
	{
		AddContact(hContact);
		return;
	}
	
	
	CContactListEntry *pItemData = GetContactData(pContactEntry);
	if(!pItemData) {
		return;
	}
	// get the old status
	int iOldStatus = pItemData->iStatus;
		
	// Update the list entry
	char *szStatus = (char *) CallService(MS_CLIST_GETSTATUSMODEDESCRIPTION, iStatus, 0);
	if(szStatus != NULL)
		pItemData->strStatus =toTstring(szStatus);
	
	pItemData->iStatus = iStatus;
	
	// update the contacts group
	CListContainer<CContactListEntry*,CContactListGroup*>* pGroup = ((CListContainer<CContactListEntry*,CContactListGroup*>*)pContactEntry->GetParent());
	if(pGroup->GetType() != ROOT)
	{
		if(!db_mc_isSub(hContact) && iStatus == ID_STATUS_OFFLINE && iOldStatus != ID_STATUS_OFFLINE)
			ChangeGroupObjectCounters(pGroup->GetGroupData()->strPath,0,-1);

		else if(!db_mc_isSub(hContact) && iStatus != ID_STATUS_OFFLINE && iOldStatus == ID_STATUS_OFFLINE)
			ChangeGroupObjectCounters(pGroup->GetGroupData()->strPath,0,1);
	}
	
	// check if the entry is still visible
	if(!IsVisible(pItemData))
	{
		RemoveContact(hContact);
		return;
	}

	// sort the list
	pGroup->sort(CContactList::CompareEntries);

}



//************************************************************************
// called when the contacts message count has changed
//************************************************************************
void CContactList::OnMessageCountChanged(MCONTACT hContact)
{
	CListEntry<CContactListEntry *,CContactListGroup*> *pContactEntry = FindContact(hContact);
	if(!pContactEntry)
	{
		AddContact(hContact);
		return;
	}

	UpdateMessageCounter(pContactEntry);

	if(!IsVisible(GetContactData(pContactEntry)))
		RemoveContact(hContact);
	((CListContainer<CContactListEntry*,CContactListGroup*>*)pContactEntry->GetParent())->sort(CContactList::CompareEntries);

}

//************************************************************************
// called when a contact has been added
//************************************************************************
void CContactList::OnContactAdded(MCONTACT hContact)
{
	// Update the list
	AddContact(hContact);

	// increase the membercount of the new group, and check if it needs to be created
	tstring strGroup = GetContactGroupPath(hContact);
	if(!strGroup.empty())
	{
		CContactListGroup *pGroup = GetGroupObjectByPath(strGroup);
		if(!pGroup)
			pGroup = CreateGroupObjectByPath(strGroup);
		
		if(!db_mc_isSub(hContact))
			ChangeGroupObjectCounters(strGroup,1);
	}
}

//************************************************************************
// called when a contact has been deleted
//************************************************************************
void CContactList::OnContactDeleted(MCONTACT hContact)
{
	// Update the list
	RemoveContact(hContact);

	// Decrease the membercount of the old group, and check if it needs to be deleted
	int res = 0;
	tstring strGroup = GetContactGroupPath(hContact);
	if(!strGroup.empty())
	{
		CContactListGroup *pGroup = GetGroupObjectByPath(strGroup);
		
		
		if(!db_mc_isSub(hContact))
			ChangeGroupObjectCounters(strGroup,-1);
		
		if(pGroup->iMembers <= 0)
			DeleteGroupObjectByPath(pGroup->strPath);
	}
}

//************************************************************************
// called when a contacts group has changed
//************************************************************************
void CContactList::OnContactGroupChanged(MCONTACT hContact,tstring strGroup)
{
	bool bMetaContact = false;
	

	strGroup = GetContactGroupPath(hContact);

	// Decrease the membercount of the old group
	CListEntry<CContactListEntry *,CContactListGroup*> *pContactEntry = FindContact(hContact);
	CContactListGroup *pOldGroup = NULL;
	// If the contactentry was not found, try adding the contact (metacontacts fix)
	if(!pContactEntry) {
		return;
	}
	if(pContactEntry->GetType() == CONTAINER)
		bMetaContact = true;


	CListContainer<CContactListEntry*,CContactListGroup*>* pContainer = ((CListContainer<CContactListEntry*,CContactListGroup*>*)pContactEntry->GetParent());
	// Update the contacts group if it has one
	if(pContainer->GetType() != ROOT)
	{
		pOldGroup = pContainer->GetGroupData();
		if(!db_mc_isSub(hContact))
			ChangeGroupObjectCounters(pOldGroup->strPath,-1);
	}
	
	// increase the membercount of the new group, and check if it needs to be created
	if(!strGroup.empty())
	{
		CContactListGroup *pGroup = GetGroupObjectByPath(strGroup);
		if(!pGroup)
			pGroup = CreateGroupObjectByPath(strGroup);
		if(!db_mc_isSub(hContact))
			ChangeGroupObjectCounters(strGroup,1);
	}

	// move subcontacts
	if(pContactEntry->GetType() == CONTAINER)
	{
		CListContainer<CContactListEntry*,CContactListGroup*> *pGroup = (CListContainer<CContactListEntry*,CContactListGroup*>*)pContactEntry;
		CListContainer<CContactListEntry*,CContactListGroup*>::iterator iter = pGroup->begin();
		while(!pGroup->empty())
		{
			iter = pGroup->begin();
			if((*iter)->GetType() == ITEM)
				OnContactGroupChanged(GetContactData(*iter)->hHandle,_T(""));
			Sleep(1);
		}
	}

	// update the list
	RemoveContact(hContact);
	AddContact(hContact);

	if(bMetaContact)
	{
		tstring strName = CAppletManager::GetContactDisplayname(hContact);
		tstring strPath = _T("");
		if(pOldGroup)
			strPath += pOldGroup->strPath;
		strPath += (strPath.empty()?_T(""):_T("\\")) + strName;
		DeleteGroupObjectByPath(strPath);
	}

	// check if the old group ( if it exists ) needs to be deleted
	if(pOldGroup && !pOldGroup->hMetaContact && pOldGroup->iMembers <= 0 && pOldGroup->iGroups <= 0)
		DeleteGroupObjectByPath(pOldGroup->strPath);
}

//************************************************************************
// updates the message count for the specified contact
//************************************************************************
void CContactList::UpdateMessageCounter(CListEntry<CContactListEntry*,CContactListGroup*> *pContactEntry)
{
	CContactListEntry *pEntry = GetContactData(pContactEntry);
	if(!pEntry) {
		return;
	}
	int iOldMessages = pEntry->iMessages;

	bool bSort = false;
	MEVENT hEvent = db_event_firstUnread(pEntry->hHandle);
	if(CAppletManager::IsMessageWindowOpen(pEntry->hHandle) || (hEvent == NULL && pEntry->iMessages > 0))
	{
		pEntry->iMessages = 0;
		bSort = true;
	}
	else
	{
		pEntry->iMessages = 0;
		MEVENT hLastEvent = db_event_last(pEntry->hHandle);
		while(hLastEvent != NULL && hEvent != NULL)
		{
			pEntry->iMessages++;
			if(hLastEvent == hEvent)
				break;
			hLastEvent = db_event_prev(pEntry->hHandle, hLastEvent);
		}
	}
	if(pEntry->iMessages >= 100)
		pEntry->strMessages = _T(">99");
	else
	{
		char buffer[8];
		buffer[0] = 0;
		itoa(pEntry->iMessages,buffer,10);
		pEntry->strMessages = toTstring(buffer);
	}

	CListContainer<CContactListEntry*,CContactListGroup*>* pContainer = ((CListContainer<CContactListEntry*,CContactListGroup*>*)pContactEntry->GetParent());
	// Update the contacts group if it has one
	if(pContainer->GetType() != ROOT)
	{
		// Update the groups event count
		if(iOldMessages != 0 && pEntry->iMessages == 0)
			ChangeGroupObjectCounters(pContainer->GetGroupData()->strPath,0,0,-1);
		else if(iOldMessages == 0 && pEntry->iMessages != 0)
			ChangeGroupObjectCounters(pContainer->GetGroupData()->strPath,0,0,1);
		else
			return;
		
		// sort the groups parent
		((CListContainer<CContactListEntry*,CContactListGroup*>*)pContainer->GetParent())->sort(CContactList::CompareEntries);
	}
}

//************************************************************************
// changes the groups membercount
//************************************************************************
void CContactList::ChangeGroupObjectCounters(tstring strGroup,int iMembers,int iOnline,int iEvents)
{
	CContactListGroup* pGroup = GetGroupObjectByPath(strGroup);
	if(!pGroup)
		return;

	pGroup->iMembers += iMembers;
	pGroup->iOnline += iOnline;
	pGroup->iEvents += iEvents;

	tstring strParse = pGroup->strPath;
	tstring::size_type pos;

	while((pos = strParse.rfind('\\')) !=  tstring::npos )
	{
		strParse = strParse.substr(0,pos);

		pGroup = GetGroupObjectByPath(strParse);
		if(!pGroup)
			break;
		pGroup->iMembers += iMembers;
		pGroup->iOnline += iOnline;
		pGroup->iEvents += iEvents;
	}	
}

//************************************************************************
// uninitializes the group objects
//************************************************************************
void CContactList::UninitializeGroupObjects()
{
	vector<CContactListGroup*>::iterator iter = m_Groups.begin();
	while(iter != m_Groups.end())
	{
		delete (*iter);
		iter++;
	}
	m_Groups.clear();
}

//************************************************************************
// initializes the group objects
//************************************************************************
void CContactList::InitializeGroupObjects()
{
	UninitializeGroupObjects();

	int res = 0;
	CContactListGroup *pGroup = NULL;
	
	MCONTACT hContact =  db_find_first();
	HANDLE hMetaContact = NULL;
	char *szProto = NULL;
	while(hContact != NULL)
	{
		tstring strGroup = GetContactGroupPath(hContact);
		szProto = GetContactProto(hContact);
		if(szProto && db_get_b(NULL,"MetaContacts","Enabled",1) && !mir_strcmpi(szProto,"MetaContacts"))
		{
			tstring strName = CAppletManager::GetContactDisplayname(hContact);
			tstring strPath = _T("");
			if(CConfig::GetBoolSetting(CLIST_USEGROUPS))
				strPath += strGroup;
			strPath += (strPath.empty()?_T(""):_T("\\")) + strName;

			pGroup = CreateGroupObjectByPath(strPath);	
			pGroup->hMetaContact = hContact;

			if(!strGroup.empty())
				ChangeGroupObjectCounters(strGroup,1);
		}
		// If the contact has no group, continue
		else if(!strGroup.empty() && CConfig::GetBoolSetting(CLIST_USEGROUPS))
		{
			pGroup = GetGroupObjectByPath(strGroup);	

			// create the group
			if(!pGroup)
				pGroup = CreateGroupObjectByPath(strGroup);

			// update it's counters
			if(!db_mc_isSub(hContact))
				ChangeGroupObjectCounters(strGroup,1);
		}

        hContact = db_find_next(hContact);
    }
}

//************************************************************************
// get group object by string
//************************************************************************
CContactListGroup *CContactList::GetGroupObjectByPath(tstring strPath)
{
	ASSERT(!strPath.empty());

	CContactListGroup *pGroup = NULL;
	vector<CContactListGroup*>::iterator iter = m_Groups.begin();
	for(;iter != m_Groups.end();iter++)
	{
		if((*iter)->strPath == strPath)
		{
			pGroup = *iter;
			break;
		}
	}
	return pGroup;
}

//************************************************************************
// creates a group object by string
//************************************************************************
CContactListGroup *CContactList::CreateGroupObjectByPath(tstring strPath)
{
	ASSERT(!strPath.empty());

	CContactListGroup *pNewGroup = new CContactListGroup();
	CContactListGroup *pParentGroup = NULL;

	tstring strParsePath = _T("");
	tstring strName = strPath;
	tstring::size_type pos;

	while((pos = strName.find('\\')) !=  tstring::npos )
	{
		strParsePath += strName.substr(0,pos);
		strName = strName.substr(pos+1);
		
		pParentGroup = GetGroupObjectByPath(strParsePath);
		if(!pParentGroup)
			pParentGroup = CreateGroupObjectByPath(strParsePath);
		strParsePath += _T("\\");
	}
	
	if(pParentGroup)
		pParentGroup->iGroups++;

	pNewGroup->strName = strName;
	pNewGroup->strPath = strPath;
	pNewGroup->iMembers = 0;
	pNewGroup->iOnline = 0;
	pNewGroup->iGroups = 0;
	pNewGroup->iEvents = 0;
	pNewGroup->hMetaContact = NULL;
	pNewGroup->pContactListEntry = NULL;

	m_Groups.push_back(pNewGroup);

	return pNewGroup;
}

//************************************************************************
// deletes a group object by string
//************************************************************************
void CContactList::DeleteGroupObjectByPath(tstring strPath)
{
	ASSERT(!strPath.empty());

	CContactListGroup *pParentGroup = NULL;
	vector<CContactListGroup*>::iterator iter = m_Groups.begin();
	for(iter = m_Groups.begin();iter != m_Groups.end();iter++)
	{
		if((*iter)->strPath == strPath)
		{
			CContactListGroup *pGroup = *iter;
			m_Groups.erase(iter);
			if(pGroup->pContactListEntry)
			{
				DeleteEntry(pGroup->pContactListEntry);
			}
			delete pGroup;

			tstring strParse = strPath;
			tstring::size_type pos = strParse.rfind('\\');
			if(pos !=  tstring::npos )
			{
				strParse = strParse.substr(0,pos);
				pParentGroup = GetGroupObjectByPath(strParse);
				pParentGroup->iGroups--;
				if(pParentGroup->iMembers <= 0 && pParentGroup->iGroups <= 0)
					DeleteGroupObjectByPath(strParse);
			}
			return;
		}
	}
}

void CContactList::SetPosition(CListEntry<CContactListEntry*,CContactListGroup*> *pEntry)
{
	CLCDList<CContactListEntry*,CContactListGroup*>::SetPosition(pEntry);
}

bool CContactList::ScrollUp()
{
	m_dwLastScroll = GetTickCount();
	return CLCDList<CContactListEntry*,CContactListGroup*>::ScrollUp();
}

bool CContactList::ScrollDown()
{
	m_dwLastScroll = GetTickCount();
	return CLCDList<CContactListEntry*,CContactListGroup*>::ScrollDown();
}

void CContactList::ShowSelection() {
	m_dwLastScroll = GetTickCount();
}