/
NGVCard+SOGo.m
904 lines (786 loc) · 27.9 KB
/
NGVCard+SOGo.m
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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
/* NGVCard+SOGo.m - this file is part of SOGo
*
* Copyright (C) 2009-2015 Inverse inc.
*
* This file 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, or (at your option)
* any later version.
*
* This file 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; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#import <Foundation/NSTimeZone.h>
#import <NGExtensions/NSNull+misc.h>
#import <NGCards/NSArray+NGCards.h>
#import <NGCards/NSString+NGCards.h>
#import <SOGo/NSArray+Utilities.h>
#import <SOGo/NSCalendarDate+SOGo.h>
#import "NSDictionary+LDIF.h"
#import "NGVCard+SOGo.h"
/*
objectclass ( 2.5.6.6 NAME 'person'
DESC 'RFC2256: a person'
SUP top STRUCTURAL
MUST ( sn $ cn )
MAY ( userPassword $ telephoneNumber $ seeAlso $ description ) )
objectclass ( 2.5.6.7 NAME 'organizationalPerson'
DESC 'RFC2256: an organizational person'
SUP person STRUCTURAL
MAY ( title $ x121Address $ registeredAddress $ destinationIndicator $
preferredDeliveryMethod $ telexNumber $ teletexTerminalIdentifier $
telephoneNumber $ internationaliSDNNumber $
facsimileTelephoneNumber $ street $ postOfficeBox $ postalCode $
postalAddress $ physicalDeliveryOfficeName $ ou $ st $ l ) )
objectclass ( 2.16.840.1.113730.3.2.2
NAME 'inetOrgPerson'
DESC 'RFC2798: Internet Organizational Person'
SUP organizationalPerson
STRUCTURAL
MAY (
audio $ businessCategory $ carLicense $ departmentNumber $
displayName $ employeeNumber $ employeeType $ givenName $
homePhone $ homePostalAddress $ initials $ jpegPhoto $
labeledURI $ mail $ manager $ mobile $ o $ pager $
photo $ roomNumber $ secretary $ uid $ userCertificate $
x500uniqueIdentifier $ preferredLanguage $
userSMIMECertificate $ userPKCS12 )
)
objectclass ( 1.3.6.1.4.1.13769.9.1 NAME 'mozillaAbPersonAlpha'
SUP top AUXILIARY
MUST ( cn )
MAY( c $
description $
displayName $
facsimileTelephoneNumber $
givenName $
homePhone $
l $
mail $
mobile $
mozillaCustom1 $
mozillaCustom2 $
mozillaCustom3 $
mozillaCustom4 $
mozillaHomeCountryName $
mozillaHomeLocalityName $
mozillaHomePostalCode $
mozillaHomeState $
mozillaHomeStreet $
mozillaHomeStreet2 $
mozillaHomeUrl $
mozillaNickname $
mozillaSecondEmail $
mozillaUseHtmlMail $
mozillaWorkStreet2 $
mozillaWorkUrl $
nsAIMid $
o $
ou $
pager $
postalCode $
postOfficeBox $
sn $
st $
street $
telephoneNumber $
title ) )
additional vcard fields:
"vcardCategories"
test contact (export from tb):
dn:: Y249UHLDqW5vbSBOb20sbWFpbD1hZHIxQGVsZS5jb20=
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
objectclass: mozillaAbPersonAlpha
givenName:: UHLDqW5vbQ==
sn: Nom
cn:: UHLDqW5vbSBOb20=
mozillaNickname: Surnom
mail: adr1@ele.com
mozillaSecondEmail: adralt@ele.com
nsAIMid: pseudo aim
modifytimestamp: 1324509379
telephoneNumber: travail
homePhone: dom
facsimiletelephonenumber: fax
pager: pager
mobile: port
mozillaHomeStreet:: YWRyMSBwcml2w6ll
mozillaHomeStreet2:: YWRyMiBwcml2w6ll
mozillaHomeLocalityName: ville/locallite
mozillaHomeState:: w6l0YXQvcHJvdg==
mozillaHomePostalCode: codepos
mozillaHomeCountryName: pays
street: adr1 pro
mozillaWorkStreet2: adr2 pro
l: ville pro
st: etat pro
postalCode: codepro
c: payspro
title: fonction pro
ou: service pro
o: soc pro
mozillaWorkUrl: webpro
mozillaHomeUrl: web
birthyear: 1946
birthmonth: 12
birthday: 04
mozillaCustom1: d1
mozillaCustom2: d2
mozillaCustom3: d3
mozillaCustom4: d4
description: notes
convention:
- our "LDIF records" are inetOrgPerson + mozillaAbPersonAlpha + a few custom
fields (categories)
- all keys are lowercase
*/
@implementation NGVCard (SOGoExtensions)
/* LDIF -> VCARD */
- (CardElement *) elementWithTag: (NSString *) elementTag
ofType: (NSString *) type
{
NSArray *elements;
CardElement *element;
elements = [self childrenWithTag: elementTag
andAttribute: @"type" havingValue: type];
if ([elements count] > 0)
element = [elements objectAtIndex: 0];
else
{
element = [CardElement elementWithTag: elementTag];
[element addType: type];
[self addChild: element];
}
return element;
}
- (void) addElementWithTag: (NSString *) elementTag
ofType: (NSString *) type
withValue: (id) value
{
NSArray *allValues;
NSEnumerator *list;
CardElement *element;
// value is either an array or a string
if ([value isKindOfClass: [NSString class]])
allValues = [NSArray arrayWithObject: value];
else
allValues = value;
// Add all values as separate elements
list = [allValues objectEnumerator];
while ((value = [list nextObject]))
{
element = [CardElement simpleElementWithTag: elementTag
singleType: type
value: value];
[self addChild: element];
}
}
- (void) _setPhoneValues: (NSDictionary *) ldifRecord
{
[self addElementWithTag: @"tel"
ofType: @"work"
withValue: [ldifRecord objectForKey: @"telephonenumber"]];
[self addElementWithTag: @"tel"
ofType: @"home"
withValue: [ldifRecord objectForKey: @"homephone"]];
[self addElementWithTag: @"tel"
ofType: @"cell"
withValue: [ldifRecord objectForKey: @"mobile"]];
[self addElementWithTag: @"tel"
ofType: @"fax"
withValue: [ldifRecord objectForKey: @"facsimiletelephonenumber"]];
[self addElementWithTag: @"tel"
ofType: @"pager"
withValue: [ldifRecord objectForKey: @"pager"]];
}
- (void) _setEmails: (NSDictionary *) ldifRecord
{
CardElement *homeMail;
// Emails from the configured mail fields of the source have already been extracted in
// [LDAPSource _fillEmailsOfEntry:intoLDIFRecord:]
[self addElementWithTag: @"email"
ofType: @"work"
withValue: [ldifRecord objectForKey: @"c_emails"]];
homeMail = [self elementWithTag: @"email" ofType: @"home"];
[homeMail setSingleValue: [ldifRecord objectForKey: @"mozillasecondemail"] forKey: @""];
[[self uniqueChildWithTag: @"x-mozilla-html"]
setSingleValue: [ldifRecord objectForKey: @"mozillausehtmlmail"]
forKey: @""];
}
- (void) updateFromLDIFRecord: (NSDictionary *) ldifRecord
{
NSInteger year, yearOfToday, month, day;
CardElement *element;
NSCalendarDate *now;
NSArray *units;
NSString *fn, *ou;
id o;
[self setNWithFamily: [ldifRecord objectForKey: @"sn"]
given: [ldifRecord objectForKey: @"givenname"]
additional: nil prefixes: nil suffixes: nil];
[self setNickname: [ldifRecord objectForKey: @"mozillanickname"]];
[self setTitle: [ldifRecord objectForKey: @"title"]];
fn = [ldifRecord objectForKey: @"displayname"];
if (!fn)
fn = [ldifRecord objectForKey: @"cn"];
[self setFn: fn];
element = [self elementWithTag: @"adr" ofType: @"home"];
[element setSingleValue: [ldifRecord objectForKey: @"mozillahomestreet2"]
atIndex: 1 forKey: @""];
[element setSingleValue: [ldifRecord objectForKey: @"mozillahomestreet"]
atIndex: 2 forKey: @""];
[element setSingleValue: [ldifRecord objectForKey: @"mozillahomelocalityname"]
atIndex: 3 forKey: @""];
[element setSingleValue: [ldifRecord objectForKey: @"mozillahomestate"]
atIndex: 4 forKey: @""];
[element setSingleValue: [ldifRecord objectForKey: @"mozillahomepostalcode"]
atIndex: 5 forKey: @""];
[element setSingleValue: [ldifRecord objectForKey: @"mozillahomecountryname"]
atIndex: 6 forKey: @""];
element = [self elementWithTag: @"adr" ofType: @"work"];
[element setSingleValue: [ldifRecord objectForKey: @"mozillaworkstreet2"]
atIndex: 1 forKey: @""];
[element setSingleValue: [ldifRecord objectForKey: @"street"]
atIndex: 2 forKey: @""];
[element setSingleValue: [ldifRecord objectForKey: @"l"]
atIndex: 3 forKey: @""];
[element setSingleValue: [ldifRecord objectForKey: @"st"]
atIndex: 4 forKey: @""];
[element setSingleValue: [ldifRecord objectForKey: @"postalcode"]
atIndex: 5 forKey: @""];
[element setSingleValue: [ldifRecord objectForKey: @"c"]
atIndex: 6 forKey: @""];
ou = [ldifRecord objectForKey: @"ou"];
if ([ou isKindOfClass: [NSArray class]])
units = [NSArray arrayWithArray: (NSArray *)ou];
else if (ou)
units = [NSArray arrayWithObject: ou];
else
units = nil;
[self setOrg: [ldifRecord objectForKey: @"o"]
units: units];
[self _setPhoneValues: ldifRecord];
[self _setEmails: ldifRecord];
[[self elementWithTag: @"url" ofType: @"home"]
setSingleValue: [ldifRecord objectForKey: @"mozillahomeurl"] forKey: @""];
[[self elementWithTag: @"url" ofType: @"work"]
setSingleValue: [ldifRecord objectForKey: @"mozillaworkurl"] forKey: @""];
[[self uniqueChildWithTag: @"x-aim"]
setSingleValue: [ldifRecord objectForKey: @"nsaimid"]
forKey: @""];
now = [NSCalendarDate date];
year = [[ldifRecord objectForKey: @"birthyear"] intValue];
if (year < 100)
{
yearOfToday = [now yearOfCommonEra];
if (year == 0)
year = yearOfToday;
else if (yearOfToday < (year + 2000))
year += 1900;
else
year += 2000;
}
month = [[ldifRecord objectForKey: @"birthmonth"] intValue];
day = [[ldifRecord objectForKey: @"birthday"] intValue];
if (year && month && day)
[self setBday: [NSString stringWithFormat: @"%.4d-%.2d-%.2d",
(int)year, (int)month, (int)day]];
else
[self setBday: @""];
/* hack to carry SOGoLDAPContactInfo to vcards */
[[self uniqueChildWithTag: @"x-sogo-contactinfo"]
setSingleValue: [ldifRecord objectForKey: @"c_info"]
forKey: @""];
[self setNote: [ldifRecord objectForKey: @"description"]];
o = [ldifRecord objectForKey: @"vcardcategories"];
// We can either have an array (from SOGo's web gui) or a
// string (from a LDIF import) as the value here.
if ([o isKindOfClass: [NSArray class]])
[self setCategories: o];
else
[self setCategories: [o componentsSeparatedByString: @","]];
[self cleanupEmptyChildren];
}
/* VCARD -> LDIF */
- (NSString *) _simpleValueForType: (NSString *) aType
inArray: (NSArray *) anArray
excluding: (NSString *) aTypeToExclude
{
NSArray *elements;
NSString *value;
elements = [anArray cardElementsWithAttribute: @"type"
havingValue: aType];
value = nil;
if ([elements count] > 0)
{
CardElement *ce;
int i;
for (i = 0; i < [elements count]; i++)
{
ce = [elements objectAtIndex: i];
value = [ce flattenedValuesForKey: @""];
if (!aTypeToExclude)
break;
if (![ce hasAttribute: @"type" havingValue: aTypeToExclude])
break;
value = nil;
}
}
return value;
}
- (void) _setValue: (NSString *) key
to: (NSString *) aValue
inLDIFRecord: (NSMutableDictionary *) ldifRecord
{
if (!aValue)
aValue = @"";
[ldifRecord setObject: aValue forKey: key];
}
- (void) _setupEmailFieldsInLDIFRecord: (NSMutableDictionary *) ldifRecord
{
NSArray *elements;
NSString *workMail, *homeMail, *mail, *secondEmail;
NSUInteger max;
elements = [self childrenWithTag: @"email"];
max = [elements count];
if (max > 0)
{
workMail = [self _simpleValueForType: @"work"
inArray: elements excluding: nil];
homeMail = [self _simpleValueForType: @"home"
inArray: elements excluding: nil];
mail = workMail;
if (mail)
secondEmail = homeMail;
else
{
secondEmail = nil;
mail = homeMail;
}
if (!mail)
{
mail = [[elements objectAtIndex: 0] flattenedValuesForKey: @""];
if (max > 1) /* we know secondEmail is not set here either... */
secondEmail = [[elements objectAtIndex: 1] flattenedValuesForKey: @""];
}
[self _setValue: @"mail" to: mail inLDIFRecord: ldifRecord];
[self _setValue: @"mozillasecondemail" to: secondEmail inLDIFRecord: ldifRecord];
}
[self _setValue: @"mozillausehtmlmail"
to: [[self uniqueChildWithTag: @"x-mozilla-html"]
flattenedValuesForKey: @""]
inLDIFRecord: ldifRecord];
}
- (void) _setupOrgFieldsInLDIFRecord: (NSMutableDictionary *) ldifRecord
{
NSMutableArray *orgServices;
CardElement *org;
NSString *service;
NSUInteger count, max;
org = [self org];
[self _setValue: @"o"
to: [org flattenedValueAtIndex: 0 forKey: @""]
inLDIFRecord: ldifRecord];
max = [[org valuesForKey: @""] count];
if (max > 1)
{
orgServices = [NSMutableArray arrayWithCapacity: max];
for (count = 1; count < max; count++)
{
service = [org flattenedValueAtIndex: count forKey: @""];
if ([service length] > 0)
[orgServices addObject: service];
}
[self _setValue: @"ou"
to: [orgServices componentsJoinedByString: @", "]
inLDIFRecord: ldifRecord];
}
}
- (NSMutableDictionary *) asLDIFRecord
{
NSArray *elements, *categories;
CardElement *element;
NSMutableDictionary *ldifRecord;
NSCalendarDate *birthDay;
NSString *dn, *stringValue, *stringValue2;
ldifRecord = [NSMutableDictionary dictionaryWithCapacity: 32];
[ldifRecord setObject: [NSArray arrayWithObjects: @"top", @"inetOrgPerson",
@"mozillaAbPersonAlpha", nil]
forKey: @"objectClass"];
element = [self n];
[self _setValue: @"sn"
to: [element flattenedValueAtIndex: 0 forKey: @""]
inLDIFRecord: ldifRecord];
[self _setValue: @"givenname"
to: [element flattenedValueAtIndex: 1 forKey: @""]
inLDIFRecord: ldifRecord];
[self _setValue: @"displayname" to: [self fn]
inLDIFRecord: ldifRecord];
[self _setValue: @"mozillanickname" to: [self nickname]
inLDIFRecord: ldifRecord];
elements = [self childrenWithTag: @"tel"];
// We do this (exclude FAX) in order to avoid setting the WORK number as the FAX
// one if we do see the FAX field BEFORE the WORK number.
[self _setValue: @"telephonenumber"
to: [self _simpleValueForType: @"work" inArray: elements
excluding: @"fax"]
inLDIFRecord: ldifRecord];
[self _setValue: @"homephone"
to: [self _simpleValueForType: @"home" inArray: elements
excluding: @"fax"]
inLDIFRecord: ldifRecord];
[self _setValue: @"mobile"
to: [self _simpleValueForType: @"cell" inArray: elements
excluding: nil]
inLDIFRecord: ldifRecord];
[self _setValue: @"facsimiletelephonenumber"
to: [self _simpleValueForType: @"fax" inArray: elements
excluding: nil]
inLDIFRecord: ldifRecord];
[self _setValue: @"pager"
to: [self _simpleValueForType: @"pager" inArray: elements
excluding: nil]
inLDIFRecord: ldifRecord];
// If we don't have a "home" and "work" phone number but
// we have a "voice" one defined, we set it to the "work" value
// This can happen when we have :
// VERSION:2.1
// N:name;surname;;;;
// TEL;VOICE;HOME:
// TEL;VOICE;WORK:
// TEL;PAGER:
// TEL;FAX;WORK:
// TEL;CELL:514 123 1234
// TEL;VOICE:450 456 6789
// ADR;HOME:;;;;;;
// ADR;WORK:;;;;;;
// ADR:;;;;;;
if ([[ldifRecord objectForKey: @"telephonenumber"] length] == 0 &&
[[ldifRecord objectForKey: @"homephone"] length] == 0 &&
[elements count] > 0)
[self _setValue: @"telephonenumber"
to: [self _simpleValueForType: @"voice" inArray: elements
excluding: nil]
inLDIFRecord: ldifRecord];
[self _setupEmailFieldsInLDIFRecord: ldifRecord];
[self _setValue: @"nsaimid"
to: [[self uniqueChildWithTag: @"x-aim"]
flattenedValuesForKey: @""]
inLDIFRecord: ldifRecord];
elements = [self childrenWithTag: @"adr"
andAttribute: @"type" havingValue: @"home"];
if (elements && [elements count] > 0)
{
element = [elements objectAtIndex: 0];
[self _setValue: @"mozillahomestreet2"
to: [element flattenedValueAtIndex: 1 forKey: @""]
inLDIFRecord: ldifRecord];
[self _setValue: @"mozillahomestreet"
to: [element flattenedValueAtIndex: 2 forKey: @""]
inLDIFRecord: ldifRecord];
[self _setValue: @"mozillahomelocalityname"
to: [element flattenedValueAtIndex: 3 forKey: @""]
inLDIFRecord: ldifRecord];
[self _setValue: @"mozillahomestate"
to: [element flattenedValueAtIndex: 4 forKey: @""]
inLDIFRecord: ldifRecord];
[self _setValue: @"mozillahomepostalcode"
to: [element flattenedValueAtIndex: 5 forKey: @""]
inLDIFRecord: ldifRecord];
[self _setValue: @"mozillahomecountryname"
to: [element flattenedValueAtIndex: 6 forKey: @""]
inLDIFRecord: ldifRecord];
}
elements = [self childrenWithTag: @"adr"
andAttribute: @"type" havingValue: @"work"];
if (!elements || [elements count] == 0)
elements = [self childrenWithTag: @"adr"];
if (elements && [elements count] > 0)
{
element = [elements objectAtIndex: 0];
[self _setValue: @"mozillaworkstreet2"
to: [element flattenedValueAtIndex: 1 forKey: @""]
inLDIFRecord: ldifRecord];
[self _setValue: @"street"
to: [element flattenedValueAtIndex: 2 forKey: @""]
inLDIFRecord: ldifRecord];
[self _setValue: @"l"
to: [element flattenedValueAtIndex: 3 forKey: @""]
inLDIFRecord: ldifRecord];
[self _setValue: @"st"
to: [element flattenedValueAtIndex: 4 forKey: @""]
inLDIFRecord: ldifRecord];
[self _setValue: @"postalcode"
to: [element flattenedValueAtIndex: 5 forKey: @""]
inLDIFRecord: ldifRecord];
[self _setValue: @"c"
to: [element flattenedValueAtIndex: 6 forKey: @""]
inLDIFRecord: ldifRecord];
}
elements = [self childrenWithTag: @"url"];
[self _setValue: @"mozillaworkurl"
to: [self _simpleValueForType: @"work" inArray: elements
excluding: nil]
inLDIFRecord: ldifRecord];
[self _setValue: @"mozillahomeurl"
to: [self _simpleValueForType: @"home" inArray: elements
excluding: nil]
inLDIFRecord: ldifRecord];
// If we don't have a "work" or "home" URL but we still have
// an URL field present, let's add it to the "home" value
if ([[ldifRecord objectForKey: @"mozillaworkurl"] length] == 0 &&
[[ldifRecord objectForKey: @"mozillahomeurl"] length] == 0 &&
[elements count] > 0)
[self _setValue: @"mozillahomeurl"
to: [[elements objectAtIndex: 0]
flattenedValuesForKey: @""]
inLDIFRecord: ldifRecord];
// If we do have a "work" URL but no "home" URL but two
// values URLs present, let's add the second one as the home URL
else if ([[ldifRecord objectForKey: @"mozillaworkurl"] length] > 0 &&
[[ldifRecord objectForKey: @"mozillahomeurl"] length] == 0 &&
[elements count] > 1)
{
int i;
for (i = 0; i < [elements count]; i++)
{
if ([[[elements objectAtIndex: i] flattenedValuesForKey: @""]
caseInsensitiveCompare: [ldifRecord objectForKey: @"mozillaworkurl"]] != NSOrderedSame)
{
[self _setValue: @"mozillahomeurl"
to: [[elements objectAtIndex: i]
flattenedValuesForKey: @""]
inLDIFRecord: ldifRecord];
break;
}
}
}
[self _setValue: @"title" to: [self title] inLDIFRecord: ldifRecord];
[self _setupOrgFieldsInLDIFRecord: ldifRecord];
categories = [self categories];
if ([categories count] > 0)
[ldifRecord setValue: categories forKey: @"vcardcategories"];
birthDay = [[self bday] asCalendarDate];
if (birthDay)
{
stringValue = [NSString stringWithFormat: @"%.4d", (int)[birthDay yearOfCommonEra]];
[self _setValue: @"birthyear" to: stringValue inLDIFRecord: ldifRecord];
stringValue = [NSString stringWithFormat: @"%.2d", (int)[birthDay monthOfYear]];
[self _setValue: @"birthmonth" to: stringValue inLDIFRecord: ldifRecord];
stringValue = [NSString stringWithFormat: @"%.2d", (int)[birthDay dayOfMonth]];
[self _setValue: @"birthday" to: stringValue inLDIFRecord: ldifRecord];
}
[self _setValue: @"description" to: [self note] inLDIFRecord: ldifRecord];
stringValue = [ldifRecord objectForKey: @"displayname"];
stringValue2 = [ldifRecord objectForKey: @"mail"];
if ([stringValue length] > 0)
{
if ([stringValue2 length] > 0)
dn = [NSString stringWithFormat: @"cn=%@,mail=%@",
stringValue, stringValue2];
else
dn = [NSString stringWithFormat: @"cn=%@", stringValue];
}
else if ([stringValue2 length] > 0)
dn = [NSString stringWithFormat: @"mail=%@", stringValue2];
else
dn = @"";
[ldifRecord setObject: dn forKey: @"dn"];
return ldifRecord;
}
- (NSString *) workCompany
{
CardElement *org;
NSString *company;
org = [self org];
company = [org flattenedValueAtIndex: 0 forKey: @""];
if ([company length] == 0)
company = nil;
return company;
}
- (NSString *) fullName
{
CardElement *n;
NSString *fn, *firstName, *lastName, *org;
fn = [self fn];
if ([fn length] == 0)
{
n = [self n];
lastName = [n flattenedValueAtIndex: 0 forKey: @""];
firstName = [n flattenedValueAtIndex: 1 forKey: @""];
if ([firstName length] > 0)
{
if ([lastName length] > 0)
fn = [NSString stringWithFormat: @"%@ %@", firstName, lastName];
else
fn = firstName;
}
else if ([lastName length] > 0)
fn = lastName;
else
{
n = [self org];
org = [n flattenedValueAtIndex: 0 forKey: @""];
fn = org;
}
}
return fn;
}
- (NSArray *) emails
{
NSArray *elements;
NSMutableArray *emails;
NSString *email;
int i;
emails = [NSMutableArray array];
elements = [self childrenWithTag: @"email"];
for (i = [elements count]-1; i >= 0; i--)
{
email = [[elements objectAtIndex: i] flattenedValuesForKey: @""];
if ([email caseInsensitiveCompare: [self preferredEMail]] == NSOrderedSame)
// First element of array is the preferred email address
[emails insertObject: email atIndex: 0];
else
[emails addObject: email];
}
return emails;
}
- (NSArray *) secondaryEmails
{
NSMutableArray *emails;
NSString *email;
int i;
emails = [NSMutableArray array];
[emails addObjectsFromArray: [self childrenWithTag: @"email"]];
[emails removeObjectsInArray: [self childrenWithTag: @"email"
andAttribute: @"type"
havingValue: @"pref"]];
for (i = [emails count]-1; i >= 0; i--)
{
email = [[emails objectAtIndex: i] flattenedValuesForKey: @""];
if ([email caseInsensitiveCompare: [self preferredEMail]] == NSOrderedSame)
[emails removeObjectAtIndex: i];
}
return emails;
}
- (NSString *) _phoneOfType: (NSString *) aType
excluding: (NSString *) aTypeToExclude
{
NSArray *elements, *phones;
NSString *phone;
phones = [self childrenWithTag: @"tel"];
elements = [phones cardElementsWithAttribute: @"type"
havingValue: aType];
phone = nil;
if ([elements count] > 0)
{
CardElement *ce;
int i;
for (i = 0; i < [elements count]; i++)
{
ce = [elements objectAtIndex: i];
phone = [ce flattenedValuesForKey: @""];
if (!aTypeToExclude)
break;
if (![ce hasAttribute: @"type" havingValue: aTypeToExclude])
break;
phone = nil;
}
}
return phone;
}
- (NSString *) workPhone
{
// We do this (exclude FAX) in order to avoid setting the WORK number as the FAX
// one if we do see the FAX field BEFORE the WORK number.
return [self _phoneOfType: @"work" excluding: @"fax"];
}
- (NSString *) homePhone
{
return [self _phoneOfType: @"home" excluding: @"fax"];
}
- (NSString *) fax
{
return [self _phoneOfType: @"fax" excluding: nil];
}
- (NSString *) mobile
{
return [self _phoneOfType: @"cell" excluding: nil];
}
- (NSString *) pager
{
return [self _phoneOfType: @"pager" excluding: nil];
}
- (NSCalendarDate *) birthday
{
NSString *bday, *value;
NSCalendarDate *date;
bday = [self bday];
date = nil;
if ([bday length] > 0)
{
// Expected format of BDAY is YYYY[-]MM[-]DD
value = [bday stringByReplacingString: @"-" withString: @""];
date = [NSCalendarDate dateFromShortDateString: value
andShortTimeString: nil
inTimeZone: [NSTimeZone timeZoneWithName: @"GMT"]];
}
return date;
}
- (NSMutableDictionary *) quickRecordFromContent: (NSString *) theContent
container: (id) theContainer
{
NSMutableDictionary *fields;
CardElement *element;
NSString *value;
NSArray *v;
fields = [NSMutableDictionary dictionaryWithCapacity: 16];
value = [self fn];
if (value)
[fields setObject: value forKey: @"c_cn"];
element = [self n];
[fields setObject: [element flattenedValueAtIndex: 0 forKey: @""]
forKey: @"c_sn"];
[fields setObject: [element flattenedValueAtIndex: 1 forKey: @""]
forKey: @"c_givenName"];
value = [self preferredTel];
if (value)
[fields setObject: value forKey: @"c_telephonenumber"];
v = [self emails];
if ([v count] > 0)
[fields setObject: [v componentsJoinedByString: @","]
forKey: @"c_mail"];
else
[fields setObject: [NSNull null] forKey: @"c_mail"];
element = [self org];
[fields setObject: [element flattenedValueAtIndex: 0 forKey: @""]
forKey: @"c_o"];
[fields setObject: [element flattenedValueAtIndex: 1 forKey: @""]
forKey: @"c_ou"];
element = [self preferredAdr];
if (element && ![element isVoid])
[fields setObject: [element flattenedValueAtIndex: 3
forKey: @""]
forKey: @"c_l"];
value = [[self uniqueChildWithTag: @"X-AIM"] flattenedValuesForKey: @""];
[fields setObject: value forKey: @"c_screenname"];
v = [[self categories] trimmedComponents];
if ([v count] > 0)
[fields setObject: [v componentsJoinedByString: @","]
forKey: @"c_categories"];
else
[fields setObject: [NSNull null] forKey: @"c_categories"];
[fields setObject: @"vcard" forKey: @"c_component"];
return fields;
}
@end /* NGVCard */