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
|
/*
Miranda IM: the free IM client for Microsoft* Windows*
Copyright 2000-12 Miranda IM, 2012-13 Miranda NG 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 "clc.h"
struct CListEvent
{
int imlIconIndex;
int flashesDone;
CLISTEVENT cle;
};
struct CListImlIcon
{
int index;
HICON hIcon;
};
static struct CListImlIcon *imlIcon;
static int imlIconCount;
extern HIMAGELIST hCListImages;
static UINT_PTR flashTimerId;
static int iconsOn;
static int disableTrayFlash;
static int disableIconFlash;
int fnGetImlIconIndex(HICON hIcon)
{
int i;
for (i=0; i < imlIconCount; i++) {
if (imlIcon[i].hIcon == hIcon)
return imlIcon[i].index;
}
imlIcon = (struct CListImlIcon *) mir_realloc(imlIcon, sizeof(struct CListImlIcon) * (imlIconCount + 1));
imlIconCount++;
imlIcon[i].hIcon = hIcon;
imlIcon[i].index = ImageList_AddIcon(hCListImages, hIcon);
return imlIcon[i].index;
}
static char * GetEventProtocol(int idx)
{
if (cli.events.count && idx>=0 && idx<cli.events.count)
{
char *szProto;
if (cli.events.items[idx]->cle.hContact == NULL)
{
if (cli.events.items[idx]->cle.flags&CLEF_PROTOCOLGLOBAL)
szProto = cli.events.items[idx]->cle.lpszProtocol;
else
szProto = NULL;
}
else
szProto = GetContactProto(cli.events.items[idx]->cle.hContact);
return szProto;
}
return NULL;
}
static void ShowOneEventInTray(int idx)
{
cli.pfnTrayIconUpdateWithImageList((iconsOn || disableTrayFlash) ? cli.events.items[idx]->imlIconIndex : 0, cli.events.items[idx]->cle.ptszTooltip, GetEventProtocol(idx));
}
static void ShowEventsInTray()
{
int i;
char ** pTrayProtos;
char nTrayProtoCnt;
int nTrayCnt = cli.trayIconCount;
if ( !cli.events.count || !nTrayCnt) return;
if (cli.events.count == 1 || nTrayCnt == 1)
{
ShowOneEventInTray(0); //for only one icon in tray show topmost event
return;
}
// in case if we have several icons in tray and several events with different protocols
// lets use several icon to show events from protocols in different icons
cli.pfnLockTray();
pTrayProtos = (char**)_alloca(sizeof(char*)*cli.trayIconCount);
nTrayProtoCnt = 0;
for (i=0; i<cli.trayIconCount; i++)
{
if (cli.trayIcon[i].id == 0 || !cli.trayIcon[i].szProto) continue;
pTrayProtos[nTrayProtoCnt++] = cli.trayIcon[i].szProto;
}
for (i=0; i<cli.events.count; i++)
{
char * iEventProto = GetEventProtocol(i);
{
int j;
for (j = 0; j<nTrayProtoCnt; j++)
if (iEventProto && pTrayProtos[j] && !lstrcmpA(pTrayProtos[j], iEventProto))
break;
if (j>=nTrayProtoCnt) j = 0; //event was not found so assume first icon
if (pTrayProtos[j]) //if not already set
ShowOneEventInTray(i); //show it
pTrayProtos[j] = NULL; //and clear slot
}
}
cli.pfnUnlockTray();
}
static VOID CALLBACK IconFlashTimer(HWND, UINT, UINT_PTR idEvent, DWORD)
{
int i, j;
ShowEventsInTray();
for (i=0; i < cli.events.count; i++) {
for (j = 0; j < i; j++)
if (cli.events.items[j]->cle.hContact == cli.events.items[i]->cle.hContact)
break;
if (j >= i)
cli.pfnChangeContactIcon(cli.events.items[i]->cle.hContact, iconsOn || disableIconFlash ? cli.events.items[i]->imlIconIndex : 0, 0);
//decrease eflashes in any case - no need to collect all events
if (cli.events.items[i]->cle.flags & CLEF_ONLYAFEW) {
if (0 >= --cli.events.items[i]->flashesDone)
cli.pfnRemoveEvent(cli.events.items[i]->cle.hContact, cli.events.items[i]->cle.hDbEvent);
}
}
if (cli.events.count == 0) {
KillTimer(NULL, idEvent);
cli.pfnTrayIconSetToBase(NULL);
}
iconsOn = !iconsOn;
}
struct CListEvent* fnAddEvent(CLISTEVENT *cle)
{
int i;
struct CListEvent* p;
if (cle == NULL || cle->cbSize != sizeof(CLISTEVENT))
return NULL;
if (cle->flags & CLEF_URGENT) {
for (i=0; i < cli.events.count; i++)
if ( !(cli.events.items[i]->cle.flags & CLEF_URGENT))
break;
}
else i = cli.events.count;
if ((p = (struct CListEvent*)cli.pfnCreateEvent()) == NULL)
return NULL;
List_Insert((SortedList*)&cli.events, p, i);
p->cle = *cle;
p->imlIconIndex = fnGetImlIconIndex(cli.events.items[i]->cle.hIcon);
p->flashesDone = 12;
p->cle.pszService = mir_strdup(cli.events.items[i]->cle.pszService);
if (p->cle.flags & CLEF_UNICODE)
p->cle.ptszTooltip = mir_tstrdup((TCHAR*)p->cle.ptszTooltip);
else
p->cle.ptszTooltip = mir_a2u((char*)p->cle.pszTooltip); //if no flag defined it handled as unicode
if (cli.events.count == 1) {
char *szProto;
if (cle->hContact == NULL)
{
if (cle->flags&CLEF_PROTOCOLGLOBAL)
szProto = cle->lpszProtocol;
else
szProto = NULL;
}
else szProto = GetContactProto(cle->hContact);
iconsOn = 1;
flashTimerId = SetTimer(NULL, 0, db_get_w(NULL, "CList", "IconFlashTime", 550), IconFlashTimer);
cli.pfnTrayIconUpdateWithImageList(p->imlIconIndex, p->cle.ptszTooltip, szProto);
}
cli.pfnChangeContactIcon(cle->hContact, p->imlIconIndex, 1);
cli.pfnSortContacts();
return p;
}
// Removes an event from the contact list's queue
// Returns 0 if the event was successfully removed, or nonzero if the event was not found
int fnRemoveEvent(HANDLE hContact, HANDLE dbEvent)
{
int i;
char *szProto;
int nSameProto = 0;
// Find the event that should be removed
for (i=0; i < cli.events.count; i++)
if ((cli.events.items[i]->cle.hContact == hContact) && (cli.events.items[i]->cle.hDbEvent == dbEvent))
break;
// Event was not found
if (i == cli.events.count)
return 1;
// Update contact's icon
szProto = GetContactProto(hContact);
cli.pfnChangeContactIcon(cli.events.items[i]->cle.hContact,
CallService(MS_CLIST_GETCONTACTICON, (WPARAM)cli.events.items[i]->cle.hContact, 1),
0);
// Free any memory allocated to the event
cli.pfnFreeEvent(cli.events.items[i]);
List_Remove((SortedList*)&cli.events, i);
{
//count same protocoled events
char * szEventProto;
for (i=0; i < cli.events.count; i++)
{
if (cli.events.items[i]->cle.hContact)
szEventProto = GetContactProto((cli.events.items[i]->cle.hContact));
else if (cli.events.items[i]->cle.flags&CLEF_PROTOCOLGLOBAL)
szEventProto = (char *) cli.events.items[i]->cle.lpszProtocol;
else
szEventProto = NULL;
if (szEventProto && szProto && !lstrcmpA(szEventProto, szProto))
nSameProto++;
}
}
if (cli.events.count == 0 || nSameProto == 0) {
if (cli.events.count == 0)
KillTimer(NULL, flashTimerId);
cli.pfnTrayIconSetToBase(hContact == NULL ? NULL : szProto);
}
else {
if (cli.events.items[0]->cle.hContact == NULL)
szProto = NULL;
else
szProto = GetContactProto(cli.events.items[0]->cle.hContact);
cli.pfnTrayIconUpdateWithImageList(iconsOn ? cli.events.items[0]->imlIconIndex : 0, cli.events.items[0]->cle.ptszTooltip, szProto);
}
return 0;
}
CLISTEVENT* fnGetEvent(HANDLE hContact, int idx)
{
if (hContact == INVALID_HANDLE_VALUE) {
if (idx >= cli.events.count)
return NULL;
return &cli.events.items[idx]->cle;
}
for (int i=0; i < cli.events.count; i++)
if (cli.events.items[i]->cle.hContact == hContact)
if (idx-- == 0)
return &cli.events.items[i]->cle;
return NULL;
}
int fnEventsProcessContactDoubleClick(HANDLE hContact)
{
for (int i=0; i < cli.events.count; i++) {
if (cli.events.items[i]->cle.hContact == hContact) {
HANDLE hDbEvent = cli.events.items[i]->cle.hDbEvent;
CallService(cli.events.items[i]->cle.pszService, (WPARAM) (HWND) NULL, (LPARAM) & cli.events.items[i]->cle);
cli.pfnRemoveEvent(hContact, hDbEvent);
return 0;
}
}
return 1;
}
int fnEventsProcessTrayDoubleClick(int index)
{
BOOL click_in_first_icon = FALSE;
if (cli.events.count) {
HANDLE hContact, hDbEvent;
int eventIndex = 0;
cli.pfnLockTray();
if (cli.trayIconCount>1 && index>0) {
int i;
char * szProto = NULL;
for (i=0; i<cli.trayIconCount; i++)
if (cli.trayIcon[i].id == index) {
szProto = cli.trayIcon[i].szProto;
if (i == 0) click_in_first_icon = TRUE;
break;
}
if (szProto) {
for (i=0; i<cli.events.count; i++) {
char * eventProto = NULL;
if (cli.events.items[i]->cle.hContact)
eventProto = GetContactProto(cli.events.items[i]->cle.hContact);
if ( !eventProto)
eventProto = cli.events.items[i]->cle.lpszProtocol;
if ( !eventProto || !_strcmpi(eventProto, szProto)) {
eventIndex = i;
break;
}
}
if (i == cli.events.count) { //EventNotFound
//lets process backward try to find first event without desired proto in tray
int j;
if (click_in_first_icon)
for (i=0; i<cli.events.count; i++) {
char * eventProto = NULL;
if (cli.events.items[i]->cle.hContact)
eventProto = GetContactProto(cli.events.items[i]->cle.hContact);
if ( !eventProto)
eventProto = cli.events.items[i]->cle.lpszProtocol;
if (eventProto) {
for (j = 0; j<cli.trayIconCount; j++)
if (cli.trayIcon[j].szProto && !_strcmpi(eventProto, cli.trayIcon[j].szProto))
break;
if (j == cli.trayIconCount) {
eventIndex = i;
break;
}
}
}
if (i == cli.events.count) { //not found
cli.pfnUnlockTray();
return 1; //continue processing to show contact list
}
}
}
}
cli.pfnUnlockTray();
hContact = cli.events.items[eventIndex]->cle.hContact;
hDbEvent = cli.events.items[eventIndex]->cle.hDbEvent;
// ; may be better to show send msg?
CallService(cli.events.items[eventIndex]->cle.pszService, (WPARAM) NULL, (LPARAM) & cli.events.items[eventIndex]->cle);
cli.pfnRemoveEvent(hContact, hDbEvent);
return 0;
}
return 1;
}
static int RemoveEventsForContact(WPARAM wParam, LPARAM)
{
int j, hit;
/*
the for (;;) loop is used here since the cli.events.count can not be relied upon to take us
thru the cli.events.items[] array without suffering from shortsightedness about how many unseen
events remain, e.g. three events, we remove the first, we're left with 2, the event
loop exits at 2 and we never see the real new 2.
*/
for (; cli.events.count > 0;) {
for (hit = 0, j = 0; j < cli.events.count; j++) {
if (cli.events.items[j]->cle.hContact == (HANDLE)wParam) {
cli.pfnRemoveEvent((HANDLE)wParam, cli.events.items[j]->cle.hDbEvent);
hit = 1;
}
}
if (j == cli.events.count && hit == 0)
return 0; /* got to the end of the array and didnt remove anything */
}
return 0;
}
static int CListEventSettingsChanged(WPARAM wParam, LPARAM lParam)
{
HANDLE hContact = (HANDLE)wParam;
DBCONTACTWRITESETTING *cws = (DBCONTACTWRITESETTING *) lParam;
if (hContact == NULL && cws && cws->szModule && cws->szSetting && strcmp(cws->szModule, "CList") == 0) {
if (strcmp(cws->szSetting, "DisableTrayFlash") == 0)
disableTrayFlash = (int)cws->value.bVal;
else if (strcmp(cws->szSetting, "NoIconBlink") == 0)
disableIconFlash = (int)cws->value.bVal;
}
return 0;
}
/***************************************************************************************/
INT_PTR AddEventSyncStub(WPARAM wParam, LPARAM lParam) { return CallServiceSync(MS_CLIST_ADDEVENT"_SYNC", wParam, lParam); }
INT_PTR AddEventStub(WPARAM, LPARAM lParam) { return cli.pfnAddEvent((CLISTEVENT*)lParam) == NULL; }
INT_PTR RemoveEventStub(WPARAM wParam, LPARAM lParam) { return cli.pfnRemoveEvent((HANDLE)wParam, (HANDLE)lParam); }
INT_PTR GetEventStub(WPARAM wParam, LPARAM lParam) { return (INT_PTR)cli.pfnGetEvent((HANDLE)wParam, (int)lParam); }
int InitCListEvents(void)
{
memset(&cli.events, 0, sizeof(cli.events));
cli.events.increment = 10;
disableTrayFlash = db_get_b(NULL, "CList", "DisableTrayFlash", 0);
disableIconFlash = db_get_b(NULL, "CList", "NoIconBlink", 0);
CreateServiceFunction(MS_CLIST_ADDEVENT, AddEventSyncStub); //need to be called through sync to keep flash timer workable
CreateServiceFunction(MS_CLIST_ADDEVENT"_SYNC", AddEventStub);
CreateServiceFunction(MS_CLIST_REMOVEEVENT, RemoveEventStub);
CreateServiceFunction(MS_CLIST_GETEVENT, GetEventStub);
HookEvent(ME_DB_CONTACT_DELETED, RemoveEventsForContact);
HookEvent(ME_DB_CONTACT_SETTINGCHANGED, CListEventSettingsChanged);
return 0;
}
struct CListEvent* fnCreateEvent(void)
{
return (struct CListEvent*)mir_calloc(sizeof(struct CListEvent));
}
void fnFreeEvent(struct CListEvent* p)
{
mir_free(p->cle.pszService);
mir_free(p->cle.pszTooltip);
mir_free(p);
}
void UninitCListEvents(void)
{
int i;
if (cli.events.count) KillTimer(NULL, flashTimerId);
for (i=0; i < cli.events.count; i++)
cli.pfnFreeEvent((struct CListEvent*)cli.events.items[i]);
List_Destroy((SortedList*)&cli.events);
if (imlIcon != NULL)
mir_free(imlIcon);
}
|