aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/plugin/addrbook/msoutlook/MsOutlookAddrBookContactSourceService.java
blob: 26bafd17aed351a78d2004587ea8fc542842a55b (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
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Copyright @ 2015 Atlassian Pty Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.java.sip.communicator.plugin.addrbook.msoutlook;

import java.util.*;
import java.util.regex.*;

import net.java.sip.communicator.plugin.addrbook.*;
import net.java.sip.communicator.service.contactsource.*;
import net.java.sip.communicator.util.*;

/**
 * Implements <tt>ContactSourceService</tt> for the Address Book of Microsoft
 * Outlook.
 *
 * @author Lyubomir Marinov
 * @author Vincent Lucas
 */
public class MsOutlookAddrBookContactSourceService
    extends AsyncContactSourceService
    implements EditableContactSourceService, PrefixedContactSourceService
{
    /**
     * The <tt>Logger</tt> used by the
     * <tt>MsOutlookAddrBookContactSourceService</tt> class and its instances
     * for logging output.
     */
    private static final Logger logger
        = Logger.getLogger(MsOutlookAddrBookContactSourceService.class);

    /**
     * The outlook address book prefix.
     */
    public static final String OUTLOOK_ADDR_BOOK_PREFIX
        = "net.java.sip.communicator.plugin.addrbook.OUTLOOK_ADDR_BOOK_PREFIX";

    /**
     * Boolean property that defines whether using this contact source service
     * as result for the search field is authorized.  This is useful when an
     * external plugin looks for result of this contact source service, but want
     * to display the search field result from its own (avoid duplicate
     * results).
     */
    public static final String PNAME_OUTLOOK_ADDR_BOOK_SEARCH_FIELD_DISABLED =
        "net.java.sip.communicator.plugin.addrbook.OUTLOOK_ADDR_BOOK_SEARCH_FIELD_DISABLED";

    /**
     * Boolean property that defines whether the warning for the default mail
     * client should be shown or not.
     */
    public static final String PNAME_OUTLOOK_ADDR_BOOK_SHOW_DEFAULTMAILCLIENT_WARNING =
        "net.java.sip.communicator.plugin.addrbook.SHOW_DEFAULTMAILCLIENT_WARNING";

    private static final long MAPI_INIT_VERSION = 0;

    private static final long MAPI_MULTITHREAD_NOTIFICATIONS = 0x00000001;
    
    private static final int NATIVE_LOGGER_LEVEL_INFO = 0;

    private static final int NATIVE_LOGGER_LEVEL_TRACE = 1;

    /**
     * The thread used to collect the notifications.
     */
    private NotificationThread notificationThread = null;

    /**
     * The mutex used to synchronized the notification thread.
     */
    private Object notificationThreadMutex = new Object();

    /**
     * The latest query created.
     */
    private MsOutlookAddrBookContactQuery latestQuery = null;

    /**
     * Indicates whether MAPI is initialized or not.
     */
    private static boolean isMAPIInitialized = false;

    static
    {
        String lib = "jmsoutlookaddrbook";

        try
        {
            System.loadLibrary(lib);
        }
        catch (Throwable t)
        {
            logger.error(
                    "Failed to load native library " + lib + ": "
                        + t.getMessage());
            throw new RuntimeException(t);
        }

        /*
         * We have multiple reports of an "UnsatisfiedLinkError: no
         * jmsoutlookaddrbook in java.library.path" at
         * MsOutlookAddrBookContactSourceService#queryContactSource() which
         * seems strange since getting there means that we have already
         * successfully gone through the System.loadLibrary() above. Try to load
         * MsOutlookAddrBookContactQuery here and see how it goes.
         */
        try
        {
            Class.forName(MsOutlookAddrBookContactQuery.class.getName());
        }
        catch (ClassNotFoundException cnfe)
        {
            throw new RuntimeException(cnfe);
        }

        int bitness = getOutlookBitnessVersion();
        int version = getOutlookVersion();
        if(bitness != -1 && version != -1)
        {
            logger.info(
                    "Outlook " + version + "-x" + bitness + " is installed.");
        }
    }

    /**
     * Initializes a new <tt>MsOutlookAddrBookContactSourceService</tt>
     * instance.
     * @param notificationDelegate the object to be notified for addressbook 
     * changes
     * @throws MsOutlookMAPIHResultException if anything goes wrong while
     * initializing the new <tt>MsOutlookAddrBookContactSourceService</tt>
     * instance
     */
    public static void initMAPI(NotificationsDelegate notificationDelegate)
        throws MsOutlookMAPIHResultException
    {
        if(!isMAPIInitialized)
        {
            boolean isOutlookDefaultMailClient = isOutlookDefaultMailClient();
            boolean showWarning 
                = AddrBookActivator.getConfigService().getBoolean(
                    PNAME_OUTLOOK_ADDR_BOOK_SHOW_DEFAULTMAILCLIENT_WARNING, 
                    true);
            if(!isOutlookDefaultMailClient && showWarning)
            {
                DefaultMailClientMessageDialog dialog 
                    = new DefaultMailClientMessageDialog();
                int result = dialog.showDialog();
                if((result & DefaultMailClientMessageDialog
                    .DONT_ASK_SELECTED_MASK) != 0)
                {
                    AddrBookActivator.getConfigService().setProperty(
                        PNAME_OUTLOOK_ADDR_BOOK_SHOW_DEFAULTMAILCLIENT_WARNING, 
                        false);
                }
                
                if((result & DefaultMailClientMessageDialog
                        .DEFAULT_MAIL_CLIENT_SELECTED_MASK) != 0)
                {
                    RegistryHandler.setOutlookAsDefaultMailClient();
                }
            }
            
            if(isOutlookDefaultMailClient && !showWarning)
            {
                AddrBookActivator.getConfigService().setProperty(
                    PNAME_OUTLOOK_ADDR_BOOK_SHOW_DEFAULTMAILCLIENT_WARNING, 
                    true);
            }
            
            String logFileName = "";
            String homeLocation = System.getProperty(
                "net.java.sip.communicator.SC_LOG_DIR_LOCATION");
            String dirName = System.getProperty(
                "net.java.sip.communicator.SC_HOME_DIR_NAME");

            if(homeLocation != null && dirName != null)
            {
                logFileName = homeLocation + "\\" + dirName + "\\log\\";
            }

            int logLevel = NATIVE_LOGGER_LEVEL_INFO;
            if(logger.isTraceEnabled())
            {
                logLevel = NATIVE_LOGGER_LEVEL_TRACE;
            }

            logger.info("Init mapi with log level " + logLevel + " and log file"
                + " path " + logFileName);

            MAPIInitialize(
                    MAPI_INIT_VERSION,
                    MAPI_MULTITHREAD_NOTIFICATIONS,
                    notificationDelegate,
                    logFileName,
                    logLevel);
            isMAPIInitialized = true;
        }
    }

    /**
     * Creates new <tt>NotificationsDelegate</tt> instance.
     * @return the <tt>NotificationsDelegate</tt> instance
     */
    public NotificationsDelegate createNotificationDelegate()
    {
        return new NotificationsDelegate();
    }

    /**
     * Gets a human-readable <tt>String</tt> which names this
     * <tt>ContactSourceService</tt> implementation.
     *
     * @return a human-readable <tt>String</tt> which names this
     * <tt>ContactSourceService</tt> implementation
     * @see ContactSourceService#getDisplayName()
     */
    public String getDisplayName()
    {
        return "Microsoft Outlook";
    }

    /**
     * Gets a <tt>String</tt> which uniquely identifies the instances of the
     * <tt>MsOutlookAddrBookContactSourceService</tt> implementation.
     *
     * @return a <tt>String</tt> which uniquely identifies the instances of the
     * <tt>MsOutlookAddrBookContactSourceService</tt> implementation
     * @see ContactSourceService#getType()
     */
    public int getType()
    {
        return SEARCH_TYPE;
    }

    private static native void MAPIInitialize(
            long version,
            long flags,
            NotificationsDelegate callback,
            String logFileName,
            int logLevel)
        throws MsOutlookMAPIHResultException;
    
    /**
     * Uninitializes MAPI.
     */
    public static void UninitializeMAPI()
    {
        if(isMAPIInitialized)
        {
            MAPIUninitialize();
            isMAPIInitialized = false;
        }
    }

    private static native void MAPIUninitialize();

    public static native int getOutlookBitnessVersion();

    public static native int getOutlookVersion();
    
    private static native boolean isOutlookDefaultMailClient();

    /**
     *  Creates query that searches for <tt>SourceContact</tt>s
     * which match a specific <tt>query</tt> <tt>Pattern</tt>.
     *
     * @param query the <tt>Pattern</tt> which this
     * <tt>ContactSourceService</tt> is being queried for
     * @return a <tt>ContactQuery</tt> which represents the query of this
     * <tt>ContactSourceService</tt> implementation for the specified
     * <tt>Pattern</tt> and via which the matching <tt>SourceContact</tt>s (if
     * any) will be returned
     * @see ExtendedContactSourceService#createContactQuery(Pattern)
     */
    public ContactQuery createContactQuery(Pattern query)
    {
        if(latestQuery != null)
            latestQuery.clear();

        latestQuery = new MsOutlookAddrBookContactQuery(this, query);

        return latestQuery;
    }

    /**
     * Stops this <tt>ContactSourceService</tt> implementation and prepares it
     * for garbage collection.
     *
     * @see AsyncContactSourceService#stop()
     */
    @Override
    public void stop()
    {
        if(latestQuery != null)
        {
            latestQuery.clear();
            latestQuery = null;
        }
        UninitializeMAPI();
    }

    /**
     * Returns the global phone number prefix to be used when calling contacts
     * from this contact source.
     *
     * @return the global phone number prefix
     */
    @Override
    public String getPhoneNumberPrefix()
    {
        return AddrBookActivator.getConfigService()
                .getString(OUTLOOK_ADDR_BOOK_PREFIX);
    }

    /**
     * Returns the index of the contact source in the result list.
     *
     * @return the index of the contact source in the result list
     */
    public int getIndex()
    {
        return -1;
    }

    /**
     * Delegate class to be notified for addressbook changes.
     */
    public class NotificationsDelegate
    {
        /**
         * Callback method when receiving notifications for inserted items.
         */
        public void inserted(String id)
        {
            if(latestQuery != null)
                addNotification(id, 'i');
        }

        /**
         * Callback method when receiving notifications for updated items.
         */
        public void updated(String id)
        {
            if(latestQuery != null)
                addNotification(id, 'u');
        }

        /**
         * Callback method when receiving notifications for deleted items.
         */
        public void deleted(String id)
        {
            if(latestQuery != null)
                addNotification(id, 'd');
        }
    }

    /**
     * Creates a new contact from the database (i.e "contacts" or
     * "msoutlook", etc.).
     *
     * @return The ID of the contact to remove. NULL if failed to create a new
     * contact.
     */
    public String createContact()
    {
        return MsOutlookAddrBookContactQuery.createContact();
    }

    /**
     * Adds a new empty contact, which will be filled in later.
     *
     * @param id The ID of the contact to add.
     */
    public void addEmptyContact(String id)
    {
        if(id != null && latestQuery != null)
        {
            latestQuery.addEmptyContact(id);
        }
    }

    /**
     * Removes the given contact from the database (i.e "contacts" or
     * "msoutlook", etc.).
     *
     * @param id The ID of the contact to remove.
     */
    public void deleteContact(String id)
    {
        if(id != null && MsOutlookAddrBookContactQuery.deleteContact(id))
        {
            if(latestQuery != null)
            {
                latestQuery.deleted(id);
            }
        }
    }

    /**
     * Defines whether using this contact source service can be used as result
     * for the search field. This is useful when an external plugin looks for
     * result of this contact source service, but want to display the search
     * field result from its own (avoid duplicate results).
     *
     * @return True if this contact source service can be used to perform search
     * for contacts. False otherwise.
     */
    @Override
    public boolean canBeUsedToSearchContacts()
    {
        return !AddrBookActivator.getConfigService().getBoolean(
                    PNAME_OUTLOOK_ADDR_BOOK_SEARCH_FIELD_DISABLED, false);
    }

    /**
     * Collects a new notification and adds it to the notification thread.
     *
     * @param id The contact id.
     * @param function The kind of notification: 'd' for deleted, 'u' for
     * updated and 'i' for inserted.
     */
    public void addNotification(String id, char function)
    {
        synchronized(notificationThreadMutex)
        {
            if(notificationThread == null
                    || !notificationThread.isAlive())
            {
                notificationThread = new NotificationThread();
                notificationThread.start();
            }
            notificationThread.add(id, function);
        }
    }

    /**
     * Thread used to collect the notification.
     */
    private class NotificationThread
        extends Thread
    {
        /**
         * The list of notification collected.
         */
        private Vector<NotificationIdFunction> contactIds
            = new Vector<NotificationIdFunction>();

        /**
         * Initializes a new notification thread.
         */
        public NotificationThread()
        {
            super("MsOutlookAddrbookContactSourceService notification thread");
        }

        /**
         * Dispatchs the collected notifications.
         */
        public void run()
        {
            boolean hasMore;
            NotificationIdFunction idFunction = null;
            String id;
            char function;

            synchronized(notificationThreadMutex)
            {
                hasMore = (contactIds.size() > 0);
                if(hasMore)
                {
                    idFunction = contactIds.get(0);
                }
            }
            while(hasMore)
            {
                if(latestQuery != null)
                {
                    id = idFunction.getId();
                    function = idFunction.getFunction();
                    if(function == 'd')
                    {
                        latestQuery.deleted(id);
                    }
                    else if(function == 'u')
                    {
                        latestQuery.updated(id);
                    }
                    else if(function == 'i')
                    {
                        latestQuery.inserted(id);
                    }
                }
                synchronized(notificationThreadMutex)
                {
                    contactIds.remove(0);
                    hasMore = (contactIds.size() > 0);
                    if(hasMore)
                    {
                        idFunction = contactIds.get(0);
                    }
                }
            }
        }

        /**
         * Adds a new notification. Avoids previous notification for the given
         * contact.
         *
         * @param id The contact id.
         * @param function The kind of notification: 'd' for deleted, 'u' for
         * updated and 'i' for inserted.
         */
        public void add(String id, char function)
        {
            NotificationIdFunction idFunction
                = new NotificationIdFunction(id, function);

            synchronized(notificationThreadMutex)
            {
                contactIds.remove(idFunction);
                contactIds.add(idFunction);
            }
        }

        /**
         * Returns the number of contact notifications to deal with.
         *
         * @return The number of contact notifications to deal with.
         */
        public int getNbRemainingNotifications()
        {
            return contactIds.size();
        }

        /**
         * Clear the current results.
         */
        public void clear()
        {
            synchronized(notificationThreadMutex)
            {
                contactIds.clear();
            }
        }
    }

    /**
     * Defines a notification: a combination of a contact identifier and a
     * function.
     */
    private class NotificationIdFunction
    {
        /**
         * The contact identifier.
         */
        private String id;

        /**
         * The kind of notification: 'd' for deleted, 'u' for updated and 'i'
         * for inserted.
         */
        private char function;

        /**
         * Creates a new notification.
         *
         * @param id The contact id.
         * @param function The kind of notification: 'd' for deleted, 'u' for
         * updated and 'i' for inserted.
         */
        public NotificationIdFunction(String id, char function)
        {
            this.id = id;
            this.function = function;
        }

        /**
         * Returns the contact identifier.
         *
         * @return The contact identifier.
         */
        public String getId()
        {
            return this.id;
        }

        /**
         * Returns the kind of notification.
         *
         * @return 'd' for deleted, 'u' for updated and 'i' for inserted.
         */
        public char getFunction()
        {
            return this.function;
        }

        /**
         * Returns if this notification is about the same contact has the one
         * given in parameter.
         *
         * @param obj An NotificationIdFunction to compare with.
         *
         * @return True if this notification is about the same contact has the
         * one given in parameter. False otherwise.
         */
        public boolean equals(Object obj)
        {
            return (this.id == null && obj == null
                || obj instanceof String && this.id.equals((String) obj));
        }

        /**
         * Returns the hash code corresponding to the contact identifier.
         *
         * @return The hash code corresponding to the contact identifier.
         */
        public int hashCode()
        {
            return this.id.hashCode();
        }
    }

    /**
     * Returns the bitness of this contact source service.
     *
     * @return The bitness of this contact source service.
     */
    public int getBitness()
    {
        return getOutlookBitnessVersion();
    }

    /**
     * Returns the version of this contact source service.
     *
     * @return The version of this contact source service.
     */
    public int getVersion()
    {
        return getOutlookVersion();
    }

    /**
     * Returns the number of contact notifications to deal with.
     *
     * @return The number of contact notifications to deal with.
     */
    public int getNbRemainingNotifications()
    {
        int nbNotifications = 0;

        synchronized(notificationThreadMutex)
        {
            if(notificationThread != null)
            {
                nbNotifications
                    = notificationThread.getNbRemainingNotifications();
            }
        }

        return nbNotifications;
    }

    /**
     * Cancels the contact notifications.
     */
    public void clearRemainingNotifications()
    {
        if(notificationThread != null)
        {
            notificationThread.clear();
        }
    }
}