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
|
/*
* Jitsi, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
#include "MAPINotification.h"
#include "MAPISession.h"
#include "net_java_sip_communicator_plugin_addrbook_msoutlook_MsOutlookAddrBookContactSourceService.h"
#include "net_java_sip_communicator_plugin_addrbook_msoutlook_MsOutlookAddrBookContactQuery.h"
#include <mapidefs.h>
#include <stdio.h>
#include <unknwn.h>
/**
* Manages notification for the message data base (used to get the list of
* contact).
*
* @author Vincent Lucas
*/
void MAPINotification_registerNotifyAllMsgStores(LPMAPISESSION mapiSession);
void MAPINotification_unregisterNotifyAllMsgStores(void);
/**
* The List of events we want to retrieve.
*/
static ULONG MAPINotification_EVENT_MASK
= fnevObjectCreated
| fnevObjectDeleted
| fnevObjectModified
| fnevObjectMoved;
static LPMDB * MAPINotification_msgStores = NULL;
static ULONG * MAPINotification_msgStoresConnection = NULL;
static LPMAPITABLE MAPINotification_msgStoresTable = NULL;
static ULONG MAPINotification_msgStoresTableConnection = 0;
static ULONG MAPINotification_nbMsgStores = 0;
static jmethodID MAPINotification_notificationsDelegateMethodIdDeleted = NULL;
static jmethodID MAPINotification_notificationsDelegateMethodIdInserted = NULL;
static jmethodID MAPINotification_notificationsDelegateMethodIdUpdated = NULL;
static jobject MAPINotification_notificationsDelegateObject = NULL;
static ULONG MAPINotification_openEntryUlFlags = MAPI_BEST_ACCESS;
static JavaVM * MAPINotification_VM = NULL;
void MAPINotification_callDeletedMethod(LPSTR iUnknown);
void MAPINotification_callInsertedMethod(LPUNKNOWN iUnknown);
void MAPINotification_callUpdatedMethod(LPUNKNOWN iUnknown);
LPUNKNOWN
MAPINotification_openEntry
(ULONG cbEntryID, LPENTRYID lpEntryID, LPVOID lpvContext);
ULONG MAPINotification_registerNotifyTable(LPMAPITABLE iUnknown);
LONG STDAPICALLTYPE MAPINotification_tableChanged
(LPVOID lpvContext, ULONG cNotifications, LPNOTIFICATION lpNotifications);
/**
* Calls back the java side when a contact is deleted.
*
* @param iUnknown The string representation of the entry id of the deleted
* contact.
*/
void MAPINotification_callDeletedMethod(LPSTR iUnknown)
{
JNIEnv *tmpJniEnv = NULL;
if(MAPINotification_VM
->AttachCurrentThreadAsDaemon((void**) &tmpJniEnv, NULL) == 0)
{
jstring value = tmpJniEnv->NewStringUTF(iUnknown);
tmpJniEnv->CallBooleanMethod(
MAPINotification_notificationsDelegateObject,
MAPINotification_notificationsDelegateMethodIdDeleted,
value);
MAPINotification_VM->DetachCurrentThread();
}
}
/**
* Calls back the java side when a contact is inserted.
*
* @param iUnknown A pointer to the newly created contact.
*/
void MAPINotification_callInsertedMethod(LPUNKNOWN iUnknown)
{
JNIEnv *tmpJniEnv = NULL;
if(MAPINotification_VM
->AttachCurrentThreadAsDaemon((void**) &tmpJniEnv, NULL) == 0)
{
tmpJniEnv->CallBooleanMethod(
MAPINotification_notificationsDelegateObject,
MAPINotification_notificationsDelegateMethodIdInserted,
iUnknown);
MAPINotification_VM->DetachCurrentThread();
}
}
/**
* Calls back the java side when a contact is updated.
*
* @param iUnknown A pointer to the updated contact.
*/
void MAPINotification_callUpdatedMethod(LPUNKNOWN iUnknown)
{
JNIEnv *tmpJniEnv = NULL;
if(MAPINotification_VM
->AttachCurrentThreadAsDaemon((void**) &tmpJniEnv, NULL) == 0)
{
tmpJniEnv->CallBooleanMethod(
MAPINotification_notificationsDelegateObject,
MAPINotification_notificationsDelegateMethodIdUpdated,
iUnknown);
MAPINotification_VM->DetachCurrentThread();
}
}
/**
* Functions called when an event is fired from the message data base.
*
* @param lpvContext A pointer to the message data base.
* @param cNotifications The number of event in this call.
* @param lpNotifications The list of notifications.
*/
LONG
STDAPICALLTYPE MAPINotification_onNotify
(LPVOID lpvContext, ULONG cNotifications, LPNOTIFICATION lpNotifications)
{
for(unsigned int i = 0; i < cNotifications; ++i)
{
LPUNKNOWN iUnknown = NULL;
if(lpvContext != NULL)
{
iUnknown = MAPINotification_openEntry(
lpNotifications[i].info.obj.cbEntryID,
lpNotifications[i].info.obj.lpEntryID,
lpvContext);
}
// A contact has been created
if(lpNotifications[i].ulEventType == fnevObjectCreated)
{
if(lpNotifications[i].info.obj.ulObjType == MAPI_MESSAGE)
{
MAPINotification_callInsertedMethod(iUnknown);
}
}
// A contact has been Modified
else if(lpNotifications[i].ulEventType == fnevObjectModified)
{
if(lpNotifications[i].info.obj.ulObjType == MAPI_MESSAGE)
{
MAPINotification_callUpdatedMethod(iUnknown);
}
}
// A contact has been deleted.
else if(lpNotifications[i].ulEventType == fnevObjectDeleted)
{
if(lpvContext != NULL)
{
char entryIdStr[lpNotifications[i].info.obj.cbEntryID * 2 + 1];
HexFromBin(
(LPBYTE) lpNotifications[i].info.obj.lpEntryID,
lpNotifications[i].info.obj.cbEntryID,
entryIdStr);
if(lpNotifications[i].info.obj.ulObjType == MAPI_MESSAGE)
{
MAPINotification_callDeletedMethod(entryIdStr);
}
}
}
// A contact has been deleted (moved to trash).
else if(lpNotifications[i].ulEventType == fnevObjectMoved)
{
if(lpvContext != NULL)
{
char entryIdStr[lpNotifications[i].info.obj.cbEntryID * 2 + 1];
HexFromBin(
(LPBYTE) lpNotifications[i].info.obj.lpEntryID,
lpNotifications[i].info.obj.cbEntryID,
entryIdStr);
char parentEntryIdStr[
lpNotifications[i].info.obj.cbParentID * 2 + 1];
HexFromBin(
(LPBYTE) lpNotifications[i].info.obj.lpParentID,
lpNotifications[i].info.obj.cbParentID,
parentEntryIdStr);
ULONG wasteBasketTags[] = {1, PR_IPM_WASTEBASKET_ENTRYID};
ULONG wasteBasketNbValues = 0;
LPSPropValue wasteBasketProps = NULL;
((LPMDB)lpvContext)->GetProps(
(LPSPropTagArray) wasteBasketTags,
MAPI_UNICODE,
&wasteBasketNbValues,
&wasteBasketProps);
char wasteBasketEntryIdStr[
wasteBasketProps[0].Value.bin.cb * 2 + 1];
HexFromBin(
(LPBYTE) wasteBasketProps[0].Value.bin.lpb,
wasteBasketProps[0].Value.bin.cb,
wasteBasketEntryIdStr);
MAPINotification_openEntry(
lpNotifications[i].info.obj.cbParentID,
lpNotifications[i].info.obj.lpParentID,
lpvContext);
if(lpNotifications[i].info.obj.ulObjType == MAPI_MESSAGE
&& strcmp(parentEntryIdStr, wasteBasketEntryIdStr) == 0)
{
MAPINotification_callDeletedMethod(entryIdStr);
}
}
}
if(iUnknown != NULL)
{
iUnknown->Release();
}
}
// A client must always return a S_OK.
return S_OK;
}
/**
* Opens an object from its entry id.
*/
LPUNKNOWN
MAPINotification_openEntry
(ULONG cbEntryID, LPENTRYID lpEntryID, LPVOID lpvContext)
{
if(lpvContext != NULL)
{
LPUNKNOWN iUnknown;
ULONG objType;
HRESULT hResult =
((LPMDB) lpvContext)->OpenEntry(
cbEntryID,
lpEntryID,
NULL,
MAPINotification_openEntryUlFlags,
&objType,
&iUnknown);
if (HR_SUCCEEDED(hResult))
{
return iUnknown;
}
}
return NULL;
}
/**
* Registers java callback functions when a contact is deleted, inserted or
* updated.
*
* @param jniEnv The Java native interface environment.
* @param mapiSession The current MAPI session.
* @param notificationsDelegate The object called when a notification is fired
* (contact updated, inserted or deleted).
*/
void
MAPINotification_registerNotificationsDelegate
(JNIEnv *jniEnv, LPMAPISESSION mapiSession, jobject notificationsDelegate)
{
if(jniEnv->GetJavaVM(&MAPINotification_VM) < 0)
{
fprintf(stderr, "Failed to get the Java VM\n");
fflush(stderr);
}
// If this function is called once again, then check first to unregister
// previous notification advises.
MAPINotification_unregisterNotificationsDelegate(jniEnv);
if(notificationsDelegate != NULL && mapiSession != NULL)
{
MAPINotification_notificationsDelegateObject
= jniEnv->NewGlobalRef(notificationsDelegate);
if(MAPINotification_notificationsDelegateObject != NULL)
{
jclass callbackClass
= jniEnv->GetObjectClass(notificationsDelegate);
MAPINotification_notificationsDelegateMethodIdInserted
= jniEnv->GetMethodID(
callbackClass,
"inserted",
"(J)V");
MAPINotification_notificationsDelegateMethodIdUpdated
= jniEnv->GetMethodID(
callbackClass,
"updated",
"(J)V");
MAPINotification_notificationsDelegateMethodIdDeleted
= jniEnv->GetMethodID(
callbackClass,
"deleted",
"(Ljava/lang/String;)V");
// Register the notification of contact changed, created and
// deleted.
MAPINotification_registerNotifyAllMsgStores(mapiSession);
}
}
}
/**
* Opens all the message store and register to notifications.
*/
void MAPINotification_registerNotifyAllMsgStores(LPMAPISESSION mapiSession)
{
HRESULT hResult;
hResult = mapiSession->GetMsgStoresTable(
0,
&MAPINotification_msgStoresTable);
if(HR_SUCCEEDED(hResult) && MAPINotification_msgStoresTable)
{
MAPINotification_msgStoresTableConnection
= MAPINotification_registerNotifyTable(
MAPINotification_msgStoresTable);
hResult = MAPINotification_msgStoresTable->SeekRow(
BOOKMARK_BEGINNING,
0,
NULL);
if (HR_SUCCEEDED(hResult))
{
LPSRowSet rows;
hResult = HrQueryAllRows(
MAPINotification_msgStoresTable,
NULL,
NULL,
NULL,
0,
&rows);
if (HR_SUCCEEDED(hResult))
{
MAPINotification_nbMsgStores = rows->cRows;
MAPINotification_msgStores
= (LPMDB*) malloc(rows->cRows * sizeof(LPMDB));
memset(
MAPINotification_msgStores,
0,
rows->cRows * sizeof(LPMDB));
MAPINotification_msgStoresConnection
= (ULONG*) malloc(rows->cRows * sizeof(ULONG));
memset(
MAPINotification_msgStoresConnection,
0,
rows->cRows * sizeof(ULONG));
if(MAPINotification_msgStores != NULL
&& MAPINotification_msgStoresConnection != NULL)
{
for(unsigned int r = 0; r < rows->cRows; ++r)
{
SRow row = rows->aRow[r];
ULONG i;
ULONG objType = 0;
SBinary entryIDBinary = { 0, NULL };
for(i = 0; i < row.cValues; ++i)
{
LPSPropValue prop = (row.lpProps) + i;
switch (prop->ulPropTag)
{
case PR_OBJECT_TYPE:
objType = prop->Value.ul;
break;
case PR_ENTRYID:
entryIDBinary = prop->Value.bin;
break;
}
}
if(objType && entryIDBinary.cb && entryIDBinary.lpb)
{
hResult = mapiSession->OpenMsgStore(
0,
entryIDBinary.cb,
(LPENTRYID) entryIDBinary.lpb,
NULL,
MDB_NO_MAIL
| MAPINotification_openEntryUlFlags,
&MAPINotification_msgStores[r]);
if (HR_SUCCEEDED(hResult))
{
MAPINotification_msgStoresConnection[r]
= MAPINotification_registerNotifyMessageDataBase(
MAPINotification_msgStores[r]);
}
}
}
}
FreeProws(rows);
}
}
}
}
/**
* Registers to notification for the given message data base.
*
* @param iUnknown The data base to register to in order to receive events.
*
* @return A unsigned long which is a token wich must be used to call the
* unadvise function for the same message data base.
*/
ULONG MAPINotification_registerNotifyMessageDataBase(LPMDB iUnknown)
{
LPMAPIADVISESINK adviseSink;
HrAllocAdviseSink(&MAPINotification_onNotify, iUnknown, &adviseSink);
ULONG nbConnection = 0;
iUnknown->Advise(
(ULONG) 0,
(LPENTRYID) NULL,
MAPINotification_EVENT_MASK,
adviseSink,
(ULONG *) &nbConnection);
return nbConnection;
}
/**
* Registers a callback function for when the message store table changes.
*/
ULONG MAPINotification_registerNotifyTable(LPMAPITABLE iUnknown)
{
LPMAPIADVISESINK adviseSink;
HrAllocAdviseSink(
&MAPINotification_tableChanged,
iUnknown,
&adviseSink);
ULONG nbConnection = 0;
iUnknown->Advise(
fnevTableModified,
adviseSink,
(ULONG *) &nbConnection);
return nbConnection;
}
/**
* Function called when a message store table changed.
*/
LONG
STDAPICALLTYPE MAPINotification_tableChanged
(LPVOID lpvContext, ULONG cNotifications, LPNOTIFICATION lpNotifications)
{
if(lpNotifications->ulEventType == fnevTableModified
&& (lpNotifications->info.tab.ulTableEvent == TABLE_CHANGED
|| lpNotifications->info.tab.ulTableEvent == TABLE_ERROR
|| lpNotifications->info.tab.ulTableEvent == TABLE_RELOAD
|| lpNotifications->info.tab.ulTableEvent == TABLE_ROW_ADDED
|| lpNotifications->info.tab.ulTableEvent == TABLE_ROW_DELETED))
{
// Frees and recreates all the notification for the table.
MAPINotification_unregisterNotifyAllMsgStores();
MAPINotification_registerNotifyAllMsgStores(
MAPISession_getMapiSession());
}
// A client must always return a S_OK.
return S_OK;
}
/**
* Unregisters java callback functions when a contact is deleted, inserted or
* updated.
*
* @param jniEnv The Java native interface environment.
*/
void MAPINotification_unregisterNotificationsDelegate(JNIEnv *jniEnv)
{
MAPINotification_unregisterNotifyAllMsgStores();
if(MAPINotification_notificationsDelegateObject != NULL)
{
jniEnv->DeleteGlobalRef( MAPINotification_notificationsDelegateObject);
MAPINotification_notificationsDelegateMethodIdInserted = NULL;
MAPINotification_notificationsDelegateMethodIdUpdated = NULL;
MAPINotification_notificationsDelegateMethodIdDeleted = NULL;
}
}
/**
* Frees all memory used to keep in mind the list of the message store and
* unregister each of them from the notifications.
*/
void MAPINotification_unregisterNotifyAllMsgStores(void)
{
if(MAPINotification_msgStoresConnection != NULL)
{
for(unsigned int i = 0; i < MAPINotification_nbMsgStores; ++i)
{
if(MAPINotification_msgStoresConnection[i] != 0)
{
MAPINotification_msgStores[i]->Unadvise(
MAPINotification_msgStoresConnection[i]);
}
}
free(MAPINotification_msgStoresConnection);
}
if(MAPINotification_msgStores != NULL)
{
for(unsigned int i = 0; i < MAPINotification_nbMsgStores; ++i)
{
if(MAPINotification_msgStores[i] != NULL)
{
MAPINotification_msgStores[i]->Release();
}
}
free(MAPINotification_msgStores);
}
if(MAPINotification_msgStoresTable != NULL)
{
MAPINotification_msgStoresTable->Unadvise(
MAPINotification_msgStoresTableConnection);
MAPINotification_msgStoresTable->Release();
}
}
|