aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/plugin/addrbook/AddrBookActivator.java
blob: 8fbfa812db334917b0f5a9b3baba44737aaf330f (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
/*
 * 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;

import java.util.*;

import net.java.sip.communicator.plugin.addrbook.msoutlook.*;
import net.java.sip.communicator.plugin.addrbook.msoutlook.calendar.*;
import net.java.sip.communicator.service.calendar.*;
import net.java.sip.communicator.service.contactsource.*;
import net.java.sip.communicator.service.gui.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.protocol.event.*;
import net.java.sip.communicator.util.*;
import net.java.sip.communicator.util.Logger;

import org.jitsi.service.configuration.*;
import org.jitsi.service.resources.*;
import org.jitsi.util.*;
import org.osgi.framework.*;

/**
 * Implements <tt>BundleActivator</tt> for the addrbook plug-in which provides
 * support for OS-specific Address Book.
 *
 * @author Lyubomir Marinov
 * @author Hristo Terezov
 */
public class AddrBookActivator
    implements BundleActivator
{
    /**
     * Boolean property that defines whether the integration of the Outlook
     * address book is enabled.
     */
    public static final String PNAME_ENABLE_MICROSOFT_OUTLOOK_SEARCH =
        "plugin.addrbook.ENABLE_MICROSOFT_OUTLOOK_SEARCH";

    /**
     * Boolean property that defines whether the integration of the OS X
     * address book is enabled.
     */
    public static final String PNAME_ENABLE_MACOSX_ADDRESS_BOOK_SEARCH =
        "plugin.addrbook.ENABLE_MACOSX_ADDRESS_BOOK_SEARCH";

    /**
     * Boolean property that defines whether changing the default IM application
     * is enabled or not.
     */
    public static final String PNAME_ENABLE_DEFAULT_IM_APPLICATION_CHANGE =
        "plugin.addrbook.ENABLE_DEFAULT_IM_APPLICATION_CHANGE";

    /**
     * Boolean property that defines whether Jitsi should be the default IM
     * Application or not.
     */
    public static final String PNAME_MAKE_JITSI_DEFAULT_IM_APPLICATION =
        "plugin.addrbook.REGISTER_AS_DEFAULT_IM_PROVIDER";

    /**
     * The <tt>Logger</tt> used by the <tt>AddrBookActivator</tt> class and its
     * instances for logging output.
     */
    private static final Logger logger
        = Logger.getLogger(AddrBookActivator.class);

    /**
     * The <tt>BundleContext</tt> in which the addrbook plug-in is started.
     */
    private static BundleContext bundleContext;

    /**
     * The <tt>ContactSourceService</tt> implementation for the OS-specific
     * Address Book.
     */
    private static ContactSourceService css;

    /**
     * The <tt>ServiceRegistration</tt> of {@link #css} in the
     * <tt>BundleContext</tt> in which this <tt>AddrBookActivator</tt> has been
     * started.
     */
    private static ServiceRegistration cssServiceRegistration;

    /**
     * The <tt>ResourceManagementService</tt> through which we access resources.
     */
    private static ResourceManagementService resourceService;

    /**
     * The <tt>ConfigurationService</tt> through which we access configuration
     * properties.
     */
    private static ConfigurationService configService;

    /**
     * The calendar service
     */
    private static CalendarServiceImpl calendarService = null;

    /**
     * List of the providers with registration listener.
     */
    private static List<ProtocolProviderService> providers
        = new ArrayList<ProtocolProviderService>();

    /**
     * The registered PhoneNumberI18nService.
     */
    private static PhoneNumberI18nService phoneNumberI18nService;

    /**
     * The registration change listener.
     */
    private static RegistrationStateChangeListener providerListener
        = new RegistrationStateChangeListener()
        {
            @Override
            public void registrationStateChanged(
                    RegistrationStateChangeEvent ev)
            {
                if(ev.getNewState().equals(RegistrationState.REGISTERED)
                        && (calendarService != null))
                {
                    calendarService.handleProviderAdded(ev.getProvider());
                }
            }
        };

    /**
     * A listener for addition of <tt>ProtocolProviderService</tt>
     */
    private static ServiceListener serviceListener
        = new ServiceListener()
        {
            @Override
            public void serviceChanged(ServiceEvent ev)
            {
                Object service
                    = bundleContext.getService(ev.getServiceReference());

                if (! (service instanceof ProtocolProviderService))
                    return;

                ProtocolProviderService pps = (ProtocolProviderService) service;

                switch (ev.getType())
                {
                case ServiceEvent.REGISTERED:
                    synchronized(providers)
                    {
                        providers.add(pps);
                    }
                    if(!pps.isRegistered())
                        pps.addRegistrationStateChangeListener(providerListener);
                    break;

                case ServiceEvent.UNREGISTERING:
                    synchronized(providers)
                    {
                        providers.remove(pps);
                    }
                    pps.removeRegistrationStateChangeListener(providerListener);
                    break;
                }
            }
        };

    /**
     * Gets the <tt>ResourceManagementService</tt> to be used by the
     * functionality of the addrbook plug-in.
     *
     * @return the <tt>ResourceManagementService</tt> to be used by the
     * functionality of the addrbook plug-in
     */
    public static ResourceManagementService getResources()
    {
        if (resourceService == null)
        {
            resourceService
                = ServiceUtils.getService(
                        bundleContext,
                        ResourceManagementService.class);
        }
        return resourceService;
    }

    public static CalendarServiceImpl getCalendarService()
    {
        return calendarService;
    }

    /**
     * Gets the <tt>ConfigurationService</tt> to be used by the
     * functionality of the addrbook plug-in.
     *
     * @return the <tt>ConfigurationService</tt> to be used by the
     * functionality of the addrbook plug-in
     */
    public static ConfigurationService getConfigService()
    {
        if (configService == null)
        {
            configService
                = ServiceUtils.getService(
                        bundleContext,
                        ConfigurationService.class);
        }
        return configService;
    }

    /**
     * Starts the addrbook plug-in.
     *
     * @param bundleContext the <tt>BundleContext</tt> in which the addrbook
     * plug-in is to be started
     * @throws Exception if anything goes wrong while starting the addrbook
     * plug-in
     * @see BundleActivator#start(BundleContext)
     */
    public void start(BundleContext bundleContext)
        throws Exception
    {
        if (logger.isInfoEnabled())
        {
            logger.info(
                    "Address book \"plugin.addrbook.ADDRESS_BOOKS\" ..."
                        + " [STARTED]");
        }

        AddrBookActivator.bundleContext = bundleContext;

        Dictionary<String, String> properties = new Hashtable<String, String>();

        // Registers the sip config panel as advanced configuration form.
        properties.put( ConfigurationForm.FORM_TYPE,
                        ConfigurationForm.CONTACT_SOURCE_TYPE);

        bundleContext.registerService(
                ConfigurationForm.class.getName(),
                new LazyConfigurationForm(
                        AdvancedConfigForm.class.getName(),
                        getClass().getClassLoader(),
                        null,
                        "plugin.addrbook.ADDRESS_BOOKS",
                        101,
                        false),
                properties);

        startService();
        startCalendarService();
    }

    /**
     * Stops the addrbook plug-in.
     *
     * @param bundleContext the <tt>BundleContext</tt> in which the addrbook
     * plug-in is to be stopped
     * @throws Exception if anything goes wrong while stopping the addrbook
     * plug-in
     * @see BundleActivator#stop(BundleContext)
     */
    public void stop(BundleContext bundleContext)
        throws Exception
    {
        if (logger.isInfoEnabled())
        {
            logger.info(
                    "Address book \"plugin.addrbook.ADDRESS_BOOKS\" ..."
                        + " [STOPPED]");
        }

        stopService();
        stopCalendarService();
    }

    /**
     * Starts the address book service.
     */
    static void startService()
    {
        /* Register the ContactSourceService implementation (if any). */
        String cssClassName;
        ConfigurationService configService = getConfigService();

        if (OSUtils.IS_WINDOWS
                && configService.getBoolean(
                        PNAME_ENABLE_MICROSOFT_OUTLOOK_SEARCH,
                        true))

        {
            cssClassName
                = "net.java.sip.communicator.plugin.addrbook"
                    + ".msoutlook.MsOutlookAddrBookContactSourceService";
        }
        else if (OSUtils.IS_MAC
                && configService.getBoolean(
                        PNAME_ENABLE_MACOSX_ADDRESS_BOOK_SEARCH,
                        true))
        {
            cssClassName
                = "net.java.sip.communicator.plugin.addrbook"
                    + ".macosx.MacOSXAddrBookContactSourceService";
        }
        else
            return;

        if (OSUtils.IS_WINDOWS
                && configService.getBoolean(
                        PNAME_ENABLE_DEFAULT_IM_APPLICATION_CHANGE,
                        true))
        {
            String isDefaultIMAppString
                = configService.getString(
                        PNAME_MAKE_JITSI_DEFAULT_IM_APPLICATION);

            if(isDefaultIMAppString == null)
            {
                configService.setProperty(
                        PNAME_MAKE_JITSI_DEFAULT_IM_APPLICATION,
                        RegistryHandler.isJitsiDefaultIMApp());
            }
            else
            {
                boolean isDefaultIMApp
                    = Boolean.parseBoolean(isDefaultIMAppString);

                if(RegistryHandler.isJitsiDefaultIMApp() != isDefaultIMApp)
                {
                    if(isDefaultIMApp)
                        setAsDefaultIMApplication();
                    else
                        unsetDefaultIMApplication();
                }
            }
        }

        try
        {
            css
                = (ContactSourceService)
                    Class.forName(cssClassName).newInstance();
            if(cssClassName.equals(
                    "net.java.sip.communicator.plugin.addrbook"
                        + ".msoutlook.MsOutlookAddrBookContactSourceService"))
            {
                MsOutlookAddrBookContactSourceService contactSource
                    = (MsOutlookAddrBookContactSourceService) css;

                MsOutlookAddrBookContactSourceService.initMAPI(
                        contactSource.createNotificationDelegate());
            }
        }
        catch (Exception ex)
        {
            String msg
                = "Failed to instantiate " + cssClassName + ": "
                    + ex.getMessage();

            logger.error(msg);
            if (logger.isDebugEnabled())
                logger.debug(msg, ex);
            return;
        }

        try
        {
            cssServiceRegistration
                = bundleContext.registerService(
                        ContactSourceService.class.getName(),
                        css,
                        null);
        }
        finally
        {
            if (cssServiceRegistration == null)
            {
                if (css instanceof AsyncContactSourceService)
                    ((AsyncContactSourceService) css).stop();
                css = null;
            }
            else if (logger.isInfoEnabled())
            {
                logger.info(
                        "Address book \"" + css.getDisplayName()
                            + "\" ... [REGISTERED]");
            }
        }
    }

    /**
     * Tries to start the calendar service.
     */
    static void startCalendarService()
    {
        if(OSUtils.IS_WINDOWS
                && !getConfigService().getBoolean(
                        CalendarService.PNAME_FREE_BUSY_STATUS_DISABLED,
                        false))
        {
            calendarService = new CalendarServiceImpl();

            try
            {
                MsOutlookAddrBookContactSourceService.initMAPI(null);
            }
            catch (MsOutlookMAPIHResultException ex)
            {
                String msg = "Failed to initialize MAPI: " + ex.getMessage();

                logger.error(msg);
                if (logger.isDebugEnabled())
                    logger.debug(msg, ex);
                return;
            }

            bundleContext.addServiceListener(serviceListener);
            for(ProtocolProviderService pps : getProtocolProviders())
            {
                if(!pps.isRegistered())
                    pps.addRegistrationStateChangeListener(providerListener);
            }
            bundleContext.registerService(CalendarService.class.getName(),
                calendarService, null);
            calendarService.start();
        }
    }

    /**
     * Stops the calendar service.
     */
    static void stopCalendarService()
    {
        if(OSUtils.IS_WINDOWS
                && !getConfigService().getBoolean(
                        CalendarService.PNAME_FREE_BUSY_STATUS_DISABLED,
                        false))
        {
            bundleContext.removeServiceListener(serviceListener);
            synchronized(providers)
            {
                for(ProtocolProviderService pps : getProtocolProviders())
                   pps.removeRegistrationStateChangeListener(providerListener);
            }
            calendarService = null;
            MsOutlookAddrBookContactSourceService.UninitializeMAPI();
        }
    }

    /**
     * Stop the previously registered service.
     */
    static void stopService()
    {
        try
        {
            if (cssServiceRegistration != null)
            {
                cssServiceRegistration.unregister();
                cssServiceRegistration = null;
            }
        }
        finally
        {
            if (css != null)
            {
                if (css instanceof AsyncContactSourceService)
                    ((AsyncContactSourceService) css).stop();

                if (logger.isInfoEnabled())
                {
                    logger.info(
                            "Address book \"" + css.getDisplayName()
                                + "\" ... [UNREGISTERED]");
                }

                css = null;
            }
        }
    }

    /**
     * Sets Jitsi as Default IM application.
     */
    public static void setAsDefaultIMApplication()
    {
        if (OSUtils.IS_WINDOWS)
            RegistryHandler.setJitsiAsDefaultApp();
    }

    /**
     * Unsets Jitsi as Default IM application.
     */
    public static void unsetDefaultIMApplication()
    {
        if (OSUtils.IS_WINDOWS)
            RegistryHandler.unsetDefaultApp();
    }

    public static List<ProtocolProviderService> getProtocolProviders()
    {
        List<ProtocolProviderService> result;
        synchronized(providers)
        {
            ServiceReference[] ppsRefs;
            try
            {
                ppsRefs
                    = bundleContext.getServiceReferences(
                            ProtocolProviderService.class.getName(),
                            null);
            }
            catch (InvalidSyntaxException ise)
            {
                ppsRefs = null;
            }

            if ((ppsRefs != null) && (ppsRefs.length != 0))
            {
                for (ServiceReference ppsRef : ppsRefs)
                {
                    ProtocolProviderService pps
                        = (ProtocolProviderService)
                            bundleContext.getService(ppsRef);
                    providers.add(pps);
                }
            }
        }

        synchronized(providers)
        {
            result = new ArrayList<ProtocolProviderService>(providers);
        }

        return result;
    }

    /**
     * Returns the PhoneNumberI18nService.
     * @return returns the PhoneNumberI18nService.
     */
    public static PhoneNumberI18nService getPhoneNumberI18nService()
    {
        if(phoneNumberI18nService == null)
        {
            phoneNumberI18nService
                = ServiceUtils.getService(
                        bundleContext,
                        PhoneNumberI18nService.class);
        }
        return phoneNumberI18nService;
    }
}