aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/plugin/addrbook/msoutlook/MsOutlookAddrBookSourceContact.java
blob: e8dfe97bddc373885040af7f7a75689965ccf3fc (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
/*
 * 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 net.java.sip.communicator.service.contactsource.*;
import net.java.sip.communicator.util.*;

/**
 * Implements a custom <tt>SourceContact</tt> for the Address Book of Microsoft
 * Outlook.
 *
 * @author Vincent Lucas
 */
public class MsOutlookAddrBookSourceContact
    extends GenericSourceContact
    implements EditableSourceContact
{
    /**
     * The <tt>Logger</tt> used by the <tt>MsOutlookAddrBookSourceContact</tt>
     * class and its instances for logging output.
     */
    private static final Logger logger
        = Logger.getLogger(MsOutlookAddrBookSourceContact.class);

    /**
     * Boolean used to temporarily lock the access to a single modification
     * source (jitsi or contact).  i.e. it can be useful if Jitsi modifies a
     * batch of details and do not want to receive contact update notification
     * which can produce concurrent changes of the details.
     */
    private Boolean locked = Boolean.FALSE;

    /**
     * The list of Outlook entry IDs we have already seen for this contact.
     */
    private Vector<String> ids = new Vector<String>(1, 1);

    /**
     * Initializes a new MsOutlookAddrBookSourceContact instance.
     *
     * @param contactSource The ContactSourceService which is creating the new
     * instance.
     * @param id The outlook entry identifier for contacts.
     * @param displayName The display name of the new instance.
     * @param organization The organization name of the new instance.
     * @param contactDetails The ContactDetails of the new instance.
     */
    public MsOutlookAddrBookSourceContact(
            ContactSourceService contactSource,
            String id,
            String displayName,
            String organization,
            List<ContactDetail> contactDetails)
    {
        super(contactSource, displayName, contactDetails);

        this.setData(SourceContact.DATA_ID, id);
        this.setDisplayDetails(organization);

        this.ids.add(id);
    }

    /**
     * Returns the string identifier for this source contact.
     *
     * @return The string identifier for this source contact.
     */
    public String getId()
    {
        return (String) this.getData(SourceContact.DATA_ID);
    }

    /**
     * Changes the details list with the supplied one. Called when the outlook
     * database for this source contact has been updated.
     *
     * @param details the details.
     */
    public void setDetails(List<ContactDetail> details)
    {
        synchronized(this)
        {
            contactDetails.clear();
            contactDetails.addAll(details);
        }
    }

    /**
     * Saves all the properties from this source contact into the outlook
     * database.
     */
    public void save()
    {
        synchronized(this)
        {
            setDisplayPostalAddress();

            MsOutlookAddrBookContactDetail outlookContactDetail;

            for(ContactDetail contactDetail: this.contactDetails)
            {
                if(contactDetail instanceof MsOutlookAddrBookContactDetail)
                {
                    outlookContactDetail
                        = (MsOutlookAddrBookContactDetail) contactDetail;
                    for(Long propId: outlookContactDetail.getOutlookPropId())
                    {
                        MsOutlookAddrBookContactQuery.IMAPIProp_SetPropString(
                                propId.longValue(),
                                contactDetail.getDetail(),
                                this.getId());
                    }
                }
            }
        }
    }

    /**
     * Removes the given <tt>ContactDetail</tt> from the list of details for
     * this <tt>SourceContact</tt>.
     *
     * @param detail the <tt>ContactDetail</tt> to remove
     */
    public void removeContactDetail(ContactDetail detail)
    {
        synchronized(this)
        {
            int i = 0;
            while(i < this.contactDetails.size())
            {
                MsOutlookAddrBookContactDetail contactDetail
                    = ((MsOutlookAddrBookContactDetail)
                            this.contactDetails.get(i));
                if(contactDetail.match(detail))
                {
                    this.removeProperty(contactDetail);
                    this.contactDetails.remove(i);
                }
                else
                {
                    ++i;
                }
            }
        }
    }

    /**
     * Removes the contact detail from the outlook database.
     *
     * @param contactDetail The contact detail to remove.
     */
    public void removeProperty(
            final MsOutlookAddrBookContactDetail contactDetail)
    {
        for(Long propId: contactDetail.getOutlookPropId())
        {
            MsOutlookAddrBookContactQuery.IMAPIProp_DeleteProp(
                    propId.longValue(),
                    this.getId());
        }
    }

    /**
     * Adds a contact detail to the list of contact details.
     *
     * @param detail the <tt>ContactDetail</tt> to add
     */
    public void addContactDetail(ContactDetail detail)
    {
        synchronized(this)
        {
            MsOutlookAddrBookContactDetail addDetail;
            if(!(detail instanceof MsOutlookAddrBookContactDetail))
            {
                long property = MsOutlookAddrBookContactQuery.getProperty(
                        detail.getCategory(),
                        detail.getSubCategories());
                Collection<ContactDetail.SubCategory> subCategories
                        = detail.getSubCategories();
                addDetail = new MsOutlookAddrBookContactDetail(
                        detail.getDetail(),
                        detail.getCategory(),
                        subCategories.toArray(
                            new ContactDetail.SubCategory[
                            subCategories.size()]),
                        property);
            }
            else
            {
                addDetail = (MsOutlookAddrBookContactDetail) detail;
            }

            // Checks if this property already exists.
            for(int i = 0; i < this.contactDetails.size(); ++ i)
            {
                MsOutlookAddrBookContactDetail contactDetail
                    = ((MsOutlookAddrBookContactDetail)
                            this.contactDetails.get(i));
                if(contactDetail.match(addDetail))
                {
                    return;
                }
            }
            this.contactDetails.add(addDetail);
        }
    }

    /**
     * Sets the display name for this contact.
     *
     * @param displayName The new display name for this contact.
     */
    @Override
    public void setDisplayName(String displayName)
    {
        if(displayName != null && !displayName.equals(this.getDisplayName()))
        {
            // Be sure that the new determined display named is saved under all
            // the requireed properties.
            long[] displayNamePropIdList =
            {
                0x3001, // PR_DISPLAY_NAME
                0x0037, // PR_SUBJECT
                0x803F, // Do not know, but set by the MFCMAPI application.
                0x0E1D // PR_NORMALIZED_SUBJECT
            };

            for(int i = 0; i < displayNamePropIdList.length; ++i)
            {
                MsOutlookAddrBookContactQuery.IMAPIProp_SetPropString(
                        displayNamePropIdList[i],
                        displayName,
                        this.getId());
            }
        }

        super.setDisplayName(displayName);
    }

    /**
     * Function called by the native part (msoutlook) when this contact has been
     * updated.
     */
    public void updated()
    {
        // Synchronize before the GetProps in order to let other operation (i.e.
        // save) to write/change all desired values (and not override new saved
        // values iwth old ones).
        synchronized(this)
        {
            waitUnlock();

            Object[] props = null;
            try
            {
                props = MsOutlookAddrBookContactQuery.IMAPIProp_GetProps(
                        this.getId(),
                        MsOutlookAddrBookContactQuery.MAPI_MAILUSER_PROP_IDS,
                        MsOutlookAddrBookContactQuery.MAPI_UNICODE);
            }
            catch (MsOutlookMAPIHResultException e)
            {
                if (logger.isDebugEnabled())
                {
                    logger.debug(
                            MsOutlookAddrBookContactQuery.class.getSimpleName()
                            + "#IMAPIProp_GetProps(long, long[], long)",
                            e);
                }
            }

            // let's update the the details
            List<ContactDetail> contactDetails
                = MsOutlookAddrBookContactQuery.getContactDetails(props);
            this.setDetails(contactDetails);

            String displayName
                = MsOutlookAddrBookContactQuery.getDisplayName(props);
            this.setDisplayName(displayName);
            String organization
                = MsOutlookAddrBookContactQuery.getOrganization(props);
            this.setDisplayDetails(organization);
        }
    }

    /**
     * Locks this object before adding or removing several contact details.
     */
    public void lock()
    {
        synchronized(this)
        {
            locked = Boolean.TRUE;
        }
    }

    /**
     * Unlocks this object before after or removing several contact details.
     */
    public void unlock()
    {
        synchronized(this)
        {
            this.save();
            locked = Boolean.FALSE;
            notify();
        }
    }

    /**
     * Waits to be unlocked. This object must be synchronized before calling
     * this function.
     */
    private void waitUnlock()
    {
        boolean continueToWait = this.locked;

        while(continueToWait)
        {
            try
            {
                wait();
                continueToWait = false;
            }
            catch(InterruptedException ie)
            {
                // Nothing to do, we will wait until the notify.
            }
        }
    }

    /**
     * Returns the index of this source contact in its parent.
     *
     * @return the index of this source contact in its parent
     */
    @Override
    public int getIndex()
    {
        return -1;
    }

    /**
     * Tells if the id given in parameters corresponds to this contact.
     *
     * @param id The id to compare with.
     * @param level 0 to only look at cached ids. 1 to only look at outlook
     * database ids.
     *
     * @return True if the id given in parameters corresponds to this contact.
     * False otherwise.
     */
    public boolean match(String id, int level)
    {
        boolean res = false;
        switch(level)
        {
            case 0:
                res = this.ids.contains(id);
                break;
            case 1:
                String localId = this.getId();
                res =
                    MsOutlookAddrBookContactQuery.compareEntryIds(id, localId);
                if(res && !this.ids.contains(id))
                {
                    this.ids.add(id);
                }
                break;
        }
        return res;
    }

    /**
     * Generates and stores the string representation of the home and work
     * postall addresses.
     */
    private void setDisplayPostalAddress()
    {
        synchronized(this)
        {
            MsOutlookAddrBookContactDetail detail;

            // Setting the display work postal address.
            boolean firstLineCR = false;
            boolean secondLineCR = false;
            String workAddress = "";
            detail = findDetail(
                    MsOutlookAddrBookContactQuery.MAPI_MAILUSER_PROP_IDS[
                    MsOutlookAddrBookContactQuery.PR_BUSINESS_ADDRESS_STREET]);
            if(detail != null)
            {
                workAddress += detail.getDetail();
                firstLineCR = true;
            }
            detail = findDetail(
                    MsOutlookAddrBookContactQuery.MAPI_MAILUSER_PROP_IDS[
                    MsOutlookAddrBookContactQuery.PR_BUSINESS_ADDRESS_CITY]);
            if(detail != null)
            {
                if(firstLineCR)
                {
                    workAddress += "\r";
                    firstLineCR = false;
                }
                workAddress += detail.getDetail();
                workAddress += " ";
                secondLineCR = true;
            }
            detail = findDetail(
                    MsOutlookAddrBookContactQuery.MAPI_MAILUSER_PROP_IDS[
                    MsOutlookAddrBookContactQuery.
                        PR_BUSINESS_ADDRESS_STATE_OR_PROVINCE]);
            if(detail != null)
            {
                if(firstLineCR)
                {
                    workAddress += "\r";
                    firstLineCR = false;
                }
                workAddress += detail.getDetail();
                secondLineCR = true;
            }
            detail = findDetail(
                    MsOutlookAddrBookContactQuery.MAPI_MAILUSER_PROP_IDS[
                    MsOutlookAddrBookContactQuery.
                        PR_BUSINESS_ADDRESS_POSTAL_CODE]);
            if(detail != null)
            {
                if(firstLineCR)
                {
                    workAddress += "\r";
                    firstLineCR = false;
                }
                workAddress += detail.getDetail();
                workAddress += " ";
                secondLineCR = true;
            }
            detail = findDetail(
                    MsOutlookAddrBookContactQuery.MAPI_MAILUSER_PROP_IDS[
                    MsOutlookAddrBookContactQuery.PR_BUSINESS_ADDRESS_COUNTRY]);
            if(detail != null)
            {
                if(secondLineCR)
                {
                    workAddress += "\r";
                    secondLineCR = false;
                }
                workAddress += detail.getDetail();
            }

            detail = findDetail(
                    MsOutlookAddrBookContactQuery.MAPI_MAILUSER_PROP_IDS[
                    MsOutlookAddrBookContactQuery.dispidWorkAddress]);
            if(detail != null)
            {
                // set address.
                detail.setDetail(workAddress);
            }
            else if(workAddress.length() > 0)
            {
                detail = new MsOutlookAddrBookContactDetail(
                        workAddress,
                        MsOutlookAddrBookContactQuery.getCategory(
                            MsOutlookAddrBookContactQuery.dispidWorkAddress),
                        MsOutlookAddrBookContactQuery.getSubCategories(
                            MsOutlookAddrBookContactQuery.dispidWorkAddress),
                        MsOutlookAddrBookContactQuery.MAPI_MAILUSER_PROP_IDS[
                            MsOutlookAddrBookContactQuery.dispidWorkAddress]);
                this.contactDetails.add(detail);
            }

            // Setting the display home postal address.
            firstLineCR = false;
            secondLineCR = false;
            String homeAddress = "";
            detail = findDetail(
                    MsOutlookAddrBookContactQuery.MAPI_MAILUSER_PROP_IDS[
                    MsOutlookAddrBookContactQuery.PR_HOME_ADDRESS_STREET]);
            if(detail != null)
            {
                homeAddress += detail.getDetail();
                firstLineCR = true;
            }
            detail = findDetail(
                    MsOutlookAddrBookContactQuery.MAPI_MAILUSER_PROP_IDS[
                    MsOutlookAddrBookContactQuery.PR_HOME_ADDRESS_CITY]);
            if(detail != null)
            {
                if(firstLineCR)
                {
                    homeAddress += "\r";
                    firstLineCR = false;
                }
                homeAddress += detail.getDetail();
                homeAddress += " ";
                secondLineCR = true;
            }
            detail = findDetail(
                    MsOutlookAddrBookContactQuery.MAPI_MAILUSER_PROP_IDS[
                    MsOutlookAddrBookContactQuery.
                        PR_HOME_ADDRESS_STATE_OR_PROVINCE]);
            if(detail != null)
            {
                if(firstLineCR)
                {
                    homeAddress += "\r";
                    firstLineCR = false;
                }
                homeAddress += detail.getDetail();
                secondLineCR = true;
            }
            detail = findDetail(
                    MsOutlookAddrBookContactQuery.MAPI_MAILUSER_PROP_IDS[
                    MsOutlookAddrBookContactQuery.PR_HOME_ADDRESS_POSTAL_CODE]);
            if(detail != null)
            {
                if(firstLineCR)
                {
                    homeAddress += "\r";
                    firstLineCR = false;
                }
                homeAddress += detail.getDetail();
                homeAddress += " ";
                secondLineCR = true;
            }
            detail = findDetail(
                    MsOutlookAddrBookContactQuery.MAPI_MAILUSER_PROP_IDS[
                    MsOutlookAddrBookContactQuery.PR_HOME_ADDRESS_COUNTRY]);
            if(detail != null)
            {
                if(secondLineCR)
                {
                    homeAddress += "\r";
                    secondLineCR = false;
                }
                homeAddress += detail.getDetail();
            }

            detail = findDetail(
                    MsOutlookAddrBookContactQuery.MAPI_MAILUSER_PROP_IDS[
                    MsOutlookAddrBookContactQuery.dispidHomeAddress]);
            if(detail != null)
            {
                // set address.
                detail.setDetail(homeAddress);
            }
            else if(homeAddress.length() > 0)
            {
                detail = new MsOutlookAddrBookContactDetail(
                        homeAddress,
                        MsOutlookAddrBookContactQuery.getCategory(
                            MsOutlookAddrBookContactQuery.dispidHomeAddress),
                        MsOutlookAddrBookContactQuery.getSubCategories(
                            MsOutlookAddrBookContactQuery.dispidHomeAddress),
                        MsOutlookAddrBookContactQuery.MAPI_MAILUSER_PROP_IDS[
                            MsOutlookAddrBookContactQuery.dispidHomeAddress]);
                this.contactDetails.add(detail);
            }
        }
    }


    /**
     * Finds the detail corresponding to the given property id.
     *
     * @param detailPropId The detail identifier.
     *
     * @return The detail corresponding to the given property id. Null if not
     * found.
     */
    private MsOutlookAddrBookContactDetail findDetail(long detailPropId)
    {
        synchronized(this)
        {
            MsOutlookAddrBookContactDetail outlookContactDetail;

            for(ContactDetail contactDetail: this.contactDetails)
            {
                if(contactDetail instanceof MsOutlookAddrBookContactDetail)
                {
                    outlookContactDetail
                        = (MsOutlookAddrBookContactDetail) contactDetail;
                    for(Long propId: outlookContactDetail.getOutlookPropId())
                    {
                        if(propId.longValue() == detailPropId)
                        {
                            return outlookContactDetail;
                        }
                    }
                }
            }
        }
        return null;
    }
}