summaryrefslogtreecommitdiff
path: root/src/modules/database/dbutils.cpp
blob: dede30691b8db2501f336e82f92d31b903d30fd2 (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
/*

Miranda IM: the free IM client for Microsoft* Windows*

Copyright 2000-2009 Miranda ICQ/IM project, 
all portions of this codebase are copyrighted to the people
listed in contributors.txt.

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 "..\..\core\commonheaders.h"
#include "profilemanager.h"

static int CompareEventTypes(const DBEVENTTYPEDESCR* p1, const DBEVENTTYPEDESCR* p2)
{
	int result = strcmp(p1->module, p2->module);
	if (result)
		return result;

	return p1->eventType - p2->eventType;
}

static LIST<DBEVENTTYPEDESCR> eventTypes(10, CompareEventTypes);

static BOOL bModuleInitialized = FALSE;

static INT_PTR DbEventTypeRegister(WPARAM, LPARAM lParam)
{
	DBEVENTTYPEDESCR* et = (DBEVENTTYPEDESCR*)lParam;
	if (eventTypes.getIndex(et) == -1) {
		DBEVENTTYPEDESCR* p = (DBEVENTTYPEDESCR*)mir_alloc(sizeof(DBEVENTTYPEDESCR));
		p->cbSize = DBEVENTTYPEDESCR_SIZE;
		p->module = mir_strdup(et->module);
		p->eventType = et->eventType; 
		p->descr  = mir_strdup(et->descr);
		p->textService = NULL;
		p->iconService = NULL;
		p->eventIcon = NULL;
		p->flags = 0;
		if (et->cbSize == DBEVENTTYPEDESCR_SIZE) {
			if (et->textService)
				p->textService = mir_strdup(et->textService);
			if (et->iconService)
				p->iconService = mir_strdup(et->iconService);
			p->eventIcon = et->eventIcon;
			p->flags = et->flags;
		}
		if ( !p->textService) {
			char szServiceName[100];
			mir_snprintf(szServiceName, sizeof(szServiceName), "%s/GetEventText%d", p->module, p->eventType);
			p->textService = mir_strdup(szServiceName);
		}
		if ( !p->iconService) {
			char szServiceName[100];
			mir_snprintf(szServiceName, sizeof(szServiceName), "%s/GetEventIcon%d", p->module, p->eventType);
			p->iconService = mir_strdup(szServiceName);
		}
		eventTypes.insert(p);
	}

	return 0;
}

static INT_PTR DbEventTypeGet(WPARAM wParam, LPARAM lParam)
{
	DBEVENTTYPEDESCR tmp;
	int idx;

	tmp.module = (char*)wParam;
	tmp.eventType = lParam;
	if ( !List_GetIndex((SortedList*)&eventTypes, &tmp, &idx))
		return 0;

	return (INT_PTR)eventTypes[idx];
}

static INT_PTR DbEventGetText(WPARAM wParam, LPARAM lParam)
{
	DBEVENTGETTEXT* egt = (DBEVENTGETTEXT*)lParam;
	BOOL bIsDenyUnicode = (egt->datatype & DBVTF_DENYUNICODE);

	DBEVENTINFO* dbei = egt->dbei;
	DBEVENTTYPEDESCR* et = (DBEVENTTYPEDESCR*)DbEventTypeGet((WPARAM)dbei->szModule, (LPARAM)dbei->eventType);

	if (et && ServiceExists(et->textService))
		return CallService(et->textService, wParam, lParam);

	if ( !dbei->pBlob) return 0;

	if (dbei->eventType == EVENTTYPE_FILE) {
		char* filename = ((char *)dbei->pBlob) + sizeof(DWORD);
		char* descr = filename + lstrlenA(filename) + 1;
		char* str = (*descr == 0) ? filename : descr;
		switch (egt->datatype) {
		case DBVT_WCHAR:
			return (INT_PTR)((dbei->flags & DBEF_UTF) ? 
					Utf8DecodeT(str) : mir_a2t(str));
		case DBVT_ASCIIZ:
			return (INT_PTR)((dbei->flags & DBEF_UTF) ? Utf8Decode(mir_strdup(str), NULL) : mir_strdup(str));
		}
		return 0;
	}

	// temporary fix for bug with event types conflict between jabber chat states notifications
	// and srmm's status changes, must be commented out in future releases
	if (dbei->eventType == 25368 && dbei->cbBlob == 1 && dbei->pBlob[0] == 1)
		return 0;

	egt->datatype &= ~DBVTF_DENYUNICODE;
	if (egt->datatype == DBVT_WCHAR)
	{
		WCHAR* msg = NULL;
        if (dbei->flags & DBEF_UTF) {
            char* str = (char*)alloca(dbei->cbBlob + 1);
            if (str == NULL) return NULL;
            memcpy(str, dbei->pBlob, dbei->cbBlob);
            str[dbei->cbBlob] = 0;
			Utf8DecodeCP(str, egt->codepage, &msg);
        }
		else {
			size_t msglen = strlen((char*)dbei->pBlob) + 1, msglenW = 0;
			if (msglen !=  dbei->cbBlob) {
				size_t i, count = ((dbei->cbBlob - msglen) / sizeof(WCHAR));
				WCHAR* p = (WCHAR*)&dbei->pBlob[ msglen ];
				for (i=0; i < count; i++) {
					if (p[i] == 0) {
						msglenW = i;
						break;
			}	}	}

			if (msglenW > 0 && msglenW < msglen && !bIsDenyUnicode)
				msg = mir_wstrdup((WCHAR*)&dbei->pBlob[ msglen ]);
			else {
				msg = (WCHAR*)mir_alloc(sizeof(WCHAR) * msglen);
				MultiByteToWideChar(egt->codepage, 0, (char *) dbei->pBlob, -1, msg, (int)msglen);
		}	}
		return (INT_PTR)msg;
	}
	else if (egt->datatype == DBVT_ASCIIZ) {
		char* msg = mir_strdup((char*)dbei->pBlob);
		if (dbei->flags & DBEF_UTF)
			Utf8DecodeCP(msg, egt->codepage, NULL);

      return (INT_PTR)msg;
	}
	return 0;
}

static INT_PTR DbEventGetIcon(WPARAM wParam, LPARAM lParam)
{
	DBEVENTINFO* dbei = (DBEVENTINFO*)lParam;
	HICON icon = NULL;
	DBEVENTTYPEDESCR* et = (DBEVENTTYPEDESCR*)DbEventTypeGet((WPARAM)dbei->szModule, (LPARAM)dbei->eventType);

	if (et && ServiceExists(et->iconService)) {
		icon = (HICON)CallService(et->iconService, wParam, lParam);
		if (icon)
			return (INT_PTR)icon;
	}
	if (et && et->eventIcon)
		icon = (HICON)CallService(MS_SKIN2_GETICONBYHANDLE, 0, (LPARAM)et->eventIcon);
	if ( !icon) {
		char szName[100];
		mir_snprintf(szName, sizeof(szName), "eventicon_%s%d", dbei->szModule, dbei->eventType);
		icon = (HICON)CallService(MS_SKIN2_GETICON, 0, (LPARAM)szName);
	}

	if ( !icon)
	{
		switch(dbei->eventType) {
		case EVENTTYPE_URL:
			icon = LoadSkinIcon(SKINICON_EVENT_URL);
			break;

		case EVENTTYPE_FILE:
			icon = LoadSkinIcon(SKINICON_EVENT_FILE);
			break;

		default: // EVENTTYPE_MESSAGE and unknown types
			icon = LoadSkinIcon(SKINICON_EVENT_MESSAGE);
			break;
		}
	}

  if (wParam & LR_SHARED)
    return (INT_PTR)icon;
  else
    return (INT_PTR)CopyIcon(icon);
}

static INT_PTR DbEventGetStringT(WPARAM wParam, LPARAM lParam)
{
	DBEVENTINFO* dbei = (DBEVENTINFO*)wParam;
	char* string = (char*)lParam;

	if (dbei->flags & DBEF_UTF)
		return (INT_PTR)Utf8DecodeUcs2(string);

	return (INT_PTR)mir_a2t(string);
}

/////////////////////////////////////////////////////////////////////////////////////////

static int sttEnumVars(const char* szVarName, LPARAM lParam)
{
	LIST<char>* vars = (LIST<char>*)lParam;
	vars->insert(mir_strdup(szVarName));
	return 0;
}

static INT_PTR DbDeleteModule(WPARAM, LPARAM lParam)
{
	LIST<char> vars(20);

	DBCONTACTENUMSETTINGS dbces = { 0 };
	dbces.pfnEnumProc = sttEnumVars;
	dbces.lParam = (LPARAM)&vars;
	dbces.szModule = (char*)lParam;
	CallService(MS_DB_CONTACT_ENUMSETTINGS, NULL, (LPARAM)&dbces);

	for (int i=vars.getCount()-1; i >= 0; i--) {
		DBDeleteContactSetting(NULL, (char*)lParam, vars[i]);
		mir_free(vars[i]);
	}
	vars.destroy();
	return 0;
}

static INT_PTR GetProfilePath(WPARAM wParam, LPARAM lParam)
{
	if ( !wParam || !lParam)
		return 1;

	char* dst = (char*)lParam;

	char* tmp = mir_t2a(g_profileDir);
	strncpy(dst, tmp, wParam);
	mir_free(tmp);
	
	if (wParam <= _tcslen(g_profileName))
	{
		dst[wParam - 1] = 0;
		return 1;
	}
	return 0;
}

static INT_PTR GetProfileName(WPARAM wParam, LPARAM lParam)
{
	if ( !wParam || !lParam)
		return 1;

	char* dst = (char*)lParam;

	char* tmp = makeFileName(g_profileName);
	strncpy(dst, tmp, wParam);
	mir_free(tmp);
	
	if (wParam <= _tcslen(g_profileName))
	{
		dst[wParam - 1] = 0;
		return 1;
	}
	return 0;
}

static INT_PTR GetProfilePathW(WPARAM wParam, LPARAM lParam)
{
	if ( !wParam || !lParam)
		return 1;

	wchar_t* dst = (wchar_t*)lParam;
	wcsncpy(dst, g_profileDir, wParam);
	if (wParam <= wcslen(g_profileDir))
	{
		dst[wParam - 1] = 0;
		return 1;
	}
	return 0;
}

static INT_PTR GetProfileNameW(WPARAM wParam, LPARAM lParam)
{
	wchar_t* dst = (wchar_t*)lParam;
	wcsncpy(dst, g_profileName, wParam);
	if (wParam <= wcslen(g_profileName))
	{
		dst[wParam - 1] = 0;
		return 1;
	}
	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////

int InitUtils()
{
	bModuleInitialized = TRUE;

	CreateServiceFunction(MS_DB_EVENT_REGISTERTYPE, DbEventTypeRegister);
	CreateServiceFunction(MS_DB_EVENT_GETTYPE, DbEventTypeGet);
	CreateServiceFunction(MS_DB_EVENT_GETTEXT, DbEventGetText);
	CreateServiceFunction(MS_DB_EVENT_GETICON, DbEventGetIcon);
	CreateServiceFunction(MS_DB_EVENT_GETSTRINGT, DbEventGetStringT);

	CreateServiceFunction(MS_DB_MODULE_DELETE, DbDeleteModule);

	CreateServiceFunction(MS_DB_GETPROFILEPATH, GetProfilePath);
	CreateServiceFunction(MS_DB_GETPROFILENAME, GetProfileName);
	CreateServiceFunction(MS_DB_GETPROFILEPATHW, GetProfilePathW);
	CreateServiceFunction(MS_DB_GETPROFILENAMEW, GetProfileNameW);
	return 0;
}

void UnloadEventsModule()
{
	int i;

	if ( !bModuleInitialized) return;

	for (i=0; i < eventTypes.getCount(); i++) {
		DBEVENTTYPEDESCR* p = eventTypes[i];
		mir_free(p->module);
		mir_free(p->descr);
		mir_free(p->textService);
		mir_free(p->iconService);
		mir_free(p);
	}

	eventTypes.destroy();
}