summaryrefslogtreecommitdiff
path: root/src/modules/extraicons/DefaultExtraIcons.cpp
blob: 9abf143e466681b403046f53d21e5a9d484623f9 (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
J/*

Copyright (C) 2009 Ricardo Pescuma Domenecci
Copyright (C) 2012-13 Miranda NG Project

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

This 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
Library General Public License for more details.

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

#include "..\..\core\commonheaders.h"

#include "m_cluiframes.h"

#include "ExtraIcon.h"
#include "extraicons.h"

ExtraIcon* GetExtraIcon(HANDLE id);

// DB extra icons ///////////////////////////////////////////////////////////////////////

struct Info;

HANDLE hExtraVisibility = NULL;
HANDLE hExtraChat = NULL;
HANDLE hExtraGender = NULL;

static void SetVisibility(HANDLE hContact, int apparentMode, bool clear)
{
	if (hContact == NULL)
		return;

	char *proto = GetContactProto(hContact);
	if ( IsEmpty(proto))
		return;

	if (apparentMode <= 0)
		apparentMode = db_get_w(hContact, proto, "ApparentMode", 0);

	HANDLE hExtraIcon, hIcolib = NULL;

	if ( db_get_b(hContact, proto, "ChatRoom", 0)) {
		// Is chat
		hExtraIcon = hExtraChat;
		if (apparentMode == ID_STATUS_OFFLINE)
			hIcolib = Skin_GetIconHandle("ChatActivity");
	}
	else {
		// Not chat
		hExtraIcon = hExtraVisibility;
		if (apparentMode == ID_STATUS_OFFLINE)
			hIcolib = LoadSkinnedIconHandle(SKINICON_OTHER_INVISIBLE_ALL);
		else if (apparentMode == ID_STATUS_ONLINE)
			hIcolib = LoadSkinnedIconHandle(SKINICON_OTHER_VISIBLE_ALL);
	}

	if (hIcolib != NULL || clear) {
		ExtraIcon *extra = GetExtraIcon(hExtraIcon);
		if (extra)
			extra->setIcon((int)hExtraIcon, hContact, hIcolib);
	}
}

static void SetGender(HANDLE hContact, int gender, bool clear)
{
	if (hContact == NULL)
		return;

	char *proto = GetContactProto(hContact);
	if ( IsEmpty(proto))
		return;

	if (gender <= 0)
		gender = db_get_b(hContact, proto, "Gender", 0);
	if (gender <= 0)
		gender = db_get_b(hContact, "UserInfo", "Gender", 0);

	const char *ico;
	if (gender == 'M')
		ico = "gender_male";
	else if (gender == 'F')
		ico = "gender_female";
	else
		ico = NULL;

	if (ico != NULL || clear) {
		ExtraIcon *extra = GetExtraIcon(hExtraGender);
		if (extra)
			extra->setIconByName((int)hExtraGender, hContact, ico);
	}
}

static void EmailOnClick(Info *info, const char *text);
static void HomepageOnClick(Info *info, const char *text);
static void DefaultSetIcon(HANDLE hContact, Info *info, const char *text);

struct Info
{
	const char *name;
	const char *desc;
	int         iSkinIcon;
	const char *db[8];
	void (*SetIcon)(HANDLE hContact, Info *info, const char *text);
	void (*OnClick)(Info *info, const char *text);

	HANDLE hIcolib, hExtraIcon;
}
static infos[] =
{
	{ "homepage", "Homepage", SKINICON_EVENT_URL,
		{ NULL, "Homepage", "UserInfo", "Homepage" },
		DefaultSetIcon, &HomepageOnClick },
	{ "sms", "Phone/SMS", SKINICON_OTHER_SMS,
		{ NULL, "Cellular", "UserInfo", "Cellular", "UserInfo", "Phone", "UserInfo", "MyPhone0" },
		DefaultSetIcon, NULL },
	{ "email", "E-mail", SKINICON_OTHER_SENDEMAIL,
		{ NULL, "e-mail", "UserInfo", "e-mail", "UserInfo", "Mye-mail0" },
		DefaultSetIcon, &EmailOnClick },
};

static void EmailOnClick(Info *info, const char *text)
{
	char cmd[1024];
	mir_snprintf(cmd, SIZEOF(cmd), "mailto:%s", text);
	ShellExecuteA(NULL, "open", cmd, NULL, NULL, SW_SHOW);
}

static void HomepageOnClick(Info *info, const char *text)
{
	ShellExecuteA(NULL, "open", text, NULL, NULL, SW_SHOW);
}

static void DefaultSetIcon(HANDLE hContact, Info *info, const char *text)
{
	ExtraIcon_SetIcon(info->hExtraIcon, hContact, text ? info->hIcolib : NULL);
}

static void SetExtraIcons(HANDLE hContact)
{
	if (hContact == NULL)
		return;

	char *proto = GetContactProto(hContact);
	if ( IsEmpty(proto))
		return;

	for (unsigned int i = 0; i < SIZEOF(infos); i++) {
		Info &p = infos[i];

		bool show = false;
		for (unsigned int j = 0; !show && j < SIZEOF(p.db); j += 2) {
			if (p.db[j + 1] == NULL)
				break;

			DBVARIANT dbv;
			if (!DBGetContactSettingString(hContact, p.db[j] == NULL ? proto : p.db[j], p.db[j+1], &dbv)) {
				if (!IsEmpty(dbv.pszVal)) {
					p.SetIcon(hContact, &p, dbv.pszVal);
					show = true;
				}
				db_free(&dbv);
			}
		}
	}
}

static int SettingChanged(WPARAM wParam, LPARAM lParam)
{
	HANDLE hContact = (HANDLE)wParam;
	DBCONTACTWRITESETTING *cws = (DBCONTACTWRITESETTING*) lParam;

	if (hContact == NULL)
		return 0;

	char *proto = GetContactProto(hContact);
	if ( IsEmpty(proto))
		return 0;

	bool isProto = (strcmp(cws->szModule, proto) == 0);
	if (isProto && strcmp(cws->szSetting, "ApparentMode") == 0) {
		SetVisibility(hContact, cws->value.type == DBVT_DELETED ? 0 : cws->value.wVal, true);
		return 0;
	}

	if (strcmp(cws->szSetting, "Gender") == 0 && (isProto || strcmp(cws->szModule, "UserInfo") == 0)) {
		SetGender(hContact, cws->value.type == DBVT_DELETED ? 0 : cws->value.bVal, true);
		return 0;
	}

	for (int i = 0; i < SIZEOF(infos); i++) {
		Info &p = infos[i];

		for (int j = 0; j < SIZEOF(p.db); j += 2) {
			if (p.db[j + 1] == NULL)
				break;
			if (p.db[j] == NULL && !isProto)
				continue;
			if (p.db[j] != NULL && strcmp(cws->szModule, p.db[j]))
				continue;
			if (strcmp(cws->szSetting, p.db[j + 1]))
				continue;

			bool show = (cws->value.type != DBVT_DELETED && !IsEmpty(cws->value.pszVal));
			p.SetIcon(hContact, &p, show ? cws->value.pszVal : NULL);

			break;
		}
	}

	return 0;
}

static int DefaultOnClick(WPARAM wParam, LPARAM lParam, LPARAM param)
{
	Info *p = (Info*)param;
	if (p == NULL)
		return 0;

	HANDLE hContact = (HANDLE)wParam;
	if (hContact == NULL)
		return 0;

	char *proto = GetContactProto(hContact);
	if ( IsEmpty(proto))
		return 0;

	bool found = false;
	for (int j = 0; !found && j < SIZEOF(p->db); j += 2) {
		if (p->db[j + 1] == NULL)
			break;

		DBVARIANT dbv;
		if ( !DBGetContactSettingString(hContact, p->db[j] == NULL ? proto : p->db[j], p->db[j+1], &dbv)) {
			if (!IsEmpty(dbv.pszVal)) {
				p->OnClick(p, dbv.pszVal);
				found = true;
			}

			db_free(&dbv);
		}
	}

	return 0;
}

// Protocol /////////////////////////////////////////////////////////////////////////////

struct ProtoInfo
{
	string proto;
	HANDLE hImage;
};

vector<ProtoInfo> protos;

static HANDLE hExtraProto;

static int ProtocolRebuildIcons(WPARAM wParam, LPARAM lParam)
{
	protos.clear();
	return 0;
}

static ProtoInfo *FindProto(const char * proto)
{
	for (unsigned int i = 0; i < protos.size(); i++) {
		ProtoInfo *pi = &protos[i];
		if (strcmp(pi->proto.c_str(), proto) == 0)
			return pi;
	}

	HICON hIcon = LoadSkinnedProtoIcon(proto, ID_STATUS_ONLINE);
	if (hIcon == NULL)
		return NULL;

	HANDLE hImage = ExtraIcon_Add(hIcon);
	if (hImage == INVALID_HANDLE_VALUE)
		return NULL;

	ProtoInfo tmp;
	tmp.proto = proto;
	tmp.hImage = hImage;
	protos.push_back(tmp);

	return &protos[protos.size() - 1];
}

static int ProtocolApplyIcon(WPARAM wParam, LPARAM lParam)
{
	HANDLE hContact = (HANDLE)wParam;

	char *proto = GetContactProto(hContact);
	if ( IsEmpty(proto))
		return 0;

	ProtoInfo *pi = FindProto(proto);

	HANDLE hImage = INVALID_HANDLE_VALUE;
	if (pi != NULL)
		hImage = pi->hImage;

	ExtraIcon_SetIcon(hExtraProto, hContact, hImage);

	return 0;
}

static int ProtocolOnClick(WPARAM wParam, LPARAM lParam, LPARAM param)
{
	HANDLE hContact = (HANDLE)wParam;
	if (hContact == NULL)
		return 0;

	CallService(MS_USERINFO_SHOWDIALOG, (WPARAM) hContact, 0);
	return 0;
}

void DefaultExtraIcons_Load()
{
	hExtraChat = ExtraIcon_Register("chat_activity", LPGEN("Chat activity"), "ChatActivity");
	hExtraVisibility = ExtraIcon_Register("visibility", "Visibility", LoadSkinnedIconName(SKINICON_OTHER_VISIBLE_ALL));
	hExtraGender = ExtraIcon_Register("gender", "Gender", "gender_male");
	hExtraProto = ExtraIcon_Register("protocol", "Account", LoadSkinnedIconName(SKINICON_OTHER_ACCMGR),
		&ProtocolRebuildIcons, &ProtocolApplyIcon, &ProtocolOnClick);

	for (unsigned int i = 0; i < SIZEOF(infos); i++) {
		Info &p = infos[i];
		p.hIcolib = LoadSkinnedIconHandle(p.iSkinIcon);
		if (p.OnClick)
			p.hExtraIcon = ExtraIcon_Register(p.name, p.desc, LoadSkinnedIconName(p.iSkinIcon), DefaultOnClick, (LPARAM) &p);
		else
			p.hExtraIcon = ExtraIcon_Register(p.name, p.desc, LoadSkinnedIconName(p.iSkinIcon));
	}

	HANDLE hContact = db_find_first();
	while (hContact != NULL) {
		SetExtraIcons(hContact);
		SetVisibility(hContact, -1, false);
		SetGender(hContact, -1, false);

		hContact = db_find_next(hContact);
	}

	HookEvent(ME_DB_CONTACT_SETTINGCHANGED, SettingChanged);
}