summaryrefslogtreecommitdiff
path: root/plugins/FavContacts/src/favlist.h
blob: 6201126b44e789e909c1c2a2b10564012eaee6e4 (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
#ifndef favlist_h__
#define favlist_h__

struct TContactInfo
{
private:
	HANDLE hContact;
	DWORD status;
	TCHAR *name;
	TCHAR *group;
	bool bManual;
	float fRate;

public:
	TContactInfo(HANDLE hContact, bool bManual, float fRate = 0)
	{
		DBVARIANT dbv = {0};

		this->hContact = hContact;
		this->bManual = bManual;
		this->fRate = fRate;
		name = mir_tstrdup((TCHAR *)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR));
		if (g_Options.bUseGroups && !DBGetContactSettingTString(hContact, "CList", "Group", &dbv))
		{
			group = mir_tstrdup(dbv.ptszVal);
			DBFreeVariant(&dbv);
		} else
		if (g_Options.bUseGroups)
		{
			group = mir_tstrdup(TranslateT("<no group>"));
		} else
		{
			group = mir_tstrdup(TranslateT("Favourite Contacts"));
		}
		status = DBGetContactSettingWord(hContact, (char *)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)hContact, 0), "Status", ID_STATUS_OFFLINE);
	}

	~TContactInfo()
	{
		mir_free(name);
		mir_free(group);
	}

	HANDLE getHandle() const
	{
		return hContact;
	}

	TCHAR *getGroup() const
	{
		return group;
	}

	static int cmp(const TContactInfo *p1, const TContactInfo *p2)
	{
		if (p1->bManual && !p2->bManual) return -1;
		if (!p1->bManual && p2->bManual) return 1;

		if (!p1->bManual)
		{
			if (p1->fRate > p2->fRate) return -1;
			if (p1->fRate < p2->fRate) return 1;
		}

		int res = 0;
		if (res = lstrcmp(p1->group, p2->group)) return res;
		//if (p1->status < p2->status) return -1;
		//if (p1->status < p2->status) return +1;
		if (res = lstrcmp(p1->name, p2->name)) return res;
		return 0;
	}
};

class TFavContacts: public LIST<TContactInfo>
{
private:
	int nGroups;

public:
	TFavContacts(): LIST<TContactInfo>(5, TContactInfo::cmp) {}
	~TFavContacts()
	{
		for (int i = 0; i < this->getCount(); ++i)
			delete (*this)[i];
	}

	int groupCount()
	{
		return nGroups;
	}

	TContactInfo *addContact(HANDLE hContact, bool bManual)
	{
		TContactInfo *info = new TContactInfo(hContact, bManual);
		this->insert(info);
		return info;
	}

	void build()
	{
		TCHAR *prevGroup = NULL;
		int i;
		
		nGroups = 1;

		HANDLE hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDFIRST, 0, 0);
		for ( ; hContact; hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDNEXT, (WPARAM)hContact, 0))
			if (DBGetContactSettingByte(hContact, "FavContacts", "IsFavourite", 0))
			{
				TCHAR *group = addContact(hContact, true)->getGroup();
				if (prevGroup && lstrcmp(prevGroup, group))
					++nGroups;
				prevGroup = group;
			}

		int nRecent = 0;
		for (i = 0; nRecent < g_Options.wMaxRecent; ++i)
		{
			hContact = g_contactCache->get(i);
			if (!hContact) break;
			if (!DBGetContactSettingByte(hContact, "FavContacts", "IsFavourite", 0))
			{
				TCHAR *group = addContact(hContact, false)->getGroup();
				if (prevGroup && lstrcmp(prevGroup, group))
					++nGroups;
				prevGroup = group;

				++nRecent;
			}
		}
	}
};

#endif // favlist_h__