forked from dhess/contacts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormatHelper.m
618 lines (563 loc) · 18 KB
/
FormatHelper.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
//
// FormatHelper.m
// contacts
//
// Created by Shane Celis on Fri Jan 10 2003.
//
#import "FormatHelper.h"
BOOL loose = NO;
BOOL strict = NO;
BOOL firstname_first = YES;
/*
Returns true if the object is nil or the string is empty (e.g. "").
*/
BOOL isNullString(NSString* string) {
if (string == nil)
return true;
else if ([string length] == 0)
return true;
// could trim it and check too.
else
return false;
}
/*
Returns the value for a given property and label (purely convenience).
*/
id getValueForLabel(ABPerson *person,
NSString *property,
NSString *label) {
id value;
int count;
int index;
ABMultiValue* multi;
value = [person valueForProperty:property];
if (label == nil) {
return value; // may be nil
}
multi = value;
count = [multi count];
for(index = 0; index < count; index++) {
if ([label isEqualToString: [multi labelAtIndex: index]]) {
return [multi valueAtIndex: index];
}
}
return nil;
}
/*
Returns a string value for a given property and label (purely
convenience); if the value is nil, it'll return a zero-length
string.
*/
NSString *getStringForLabel(ABPerson *person,
NSString *property,
NSString *label) {
NSString *value;
value = (NSString *) getValueForLabel(person, property, label);
if (value == nil)
return @"";
return value;
}
/*
* Returns a string value for a date-property
* NSCalendarDate seems to be obsolete now ...
*/
NSString *getDateForLabel(ABPerson *person,
NSString *property,
NSString *label) {
NSCalendarDate *value;
value = (NSCalendarDate *) getValueForLabel(person, property, label);
if (value == nil)
return @"99/99";
return [value descriptionWithCalendarFormat:@"%m/%d"];
}
/*
Parses the format string for FormatHelper tokens.
*/
NSArray *getFormatHelpers(NSString *format) {
NSMutableArray *array = [NSMutableArray array];
char *str;
for(str = (char *) [format UTF8String]; *str != NULL; str++) {
if (str[0] == '%') {
if (str[1] != NULL) {
// skip double percents
if (str[1] == '%') {
str++;
[array addObject: [[FormatHelper alloc]
initWithStringLiteral: @"%"]];
continue;
}
NSString *token = nil;
if (str[2] != NULL && (str[2] == 'e' || str[2] == 'p'
|| str[2] == 'n' || str[2] == 'i'
|| str[2] == 'a' || str[2] == 'b')) {
token = [NSString stringWithFormat: @"%%%c%c",
str[1], str[2]];
str++;
str++;
} else {
token = [NSString stringWithFormat: @"%%%c", str[1]];
str++;
}
FormatHelper *helper = [[FormatHelper alloc]
initWithFormatToken: token];
if ([helper headerName] == nil)
fprintf(stderr, "warning: invalid format token given \"%s\"\n",
[token UTF8String]);
else
[array addObject: helper];
}
} else {
FormatHelper *helper = [[FormatHelper alloc] initWithStringLiteral:
[NSString stringWithFormat: @"%c", str[0]]];
[array addObject: helper];
//printf("got literal \"%c\"\n", str[0]);
}
}
return array;
}
@implementation FormatHelper
/*
Initialize with a token (e.g. "%fn").
*/
- (id) initWithFormatToken: (NSString *) atoken {
token = [atoken retain];
literalValue = nil;
return self;
}
- (id) initWithStringLiteral: (NSString *) literal {
token = nil;
literalValue = [literal retain];
return self;
}
/*
Returns a printf token (e.g. "%-12.11s").
*/
- (NSString *) printfToken {
if (literalValue != nil)
return @"%s";
int size = [self fieldSize];
if (strict)
return @"%s";
else if (loose)
return [NSString stringWithFormat: @"%%-%ds", size];
else
return [NSString stringWithFormat: @"%%-%d.%ds", size, size];
}
/*
Returns the field size for a particular token.
*/
- (int) fieldSize {
if (literalValue != nil)
return 1;
// these values should be in the preferences...
switch ([self type]) {
case 'u':
return 45;
case 'n':
switch ([self subtype]) {
case 'f':
return 12;
case 'l':
return 12;
case 'n':
return 12;
default:
return 21;
}
case 'w':
return 25;
case 'i':
return 19;
case 'e':
return 23;
case 'a':
return 45;
case 'p':
return 14;
// case 'N':
// return @"\nNOTE: %s";
case 'N':
return 76;
default:
return 12;
}
}
/*
Returns the type of the token. For example, the token "%fn" has a
'n' type.
*/
- (char) type {
return [token characterAtIndex: ([token length] - 1)];
}
/*
Returns the subtype of the token. For example, the token "%fn" has
a 'f' type.
*/
- (char) subtype {
return [token characterAtIndex: ([token length] - 2)];
}
/*
Returns the header for the token. For example, the token "%n" has a
header of "NAME".
*/
- (NSString *) headerName {
if (literalValue != nil)
return literalValue;
int subtype = [self subtype];
switch ([self type]) {
case 'u':
return @"UID";
case 'n':
switch (subtype) {
case 'f':
return @"FIRST";
case 'l':
return @"LAST";
case 'n':
return @"NICK";
default:
return @"NAME";
}
case 'p':
switch (subtype) {
case 'h':
return @"HOME";
case 'w':
return @"WORK";
case 'm':
return @"MOBILE";
case 'p':
return @"PAGER";
case 'f':
return @"FAX";
case 'M':
return @"MAIN";
case 'o':
return @"OTHER";
default:
return @"PHONE";
}
case 'a':
switch (subtype) {
case 'h':
return @"HOME";
case 'w':
return @"WORK";
default:
return @"ADDRESS";
}
case 'e':
switch (subtype) {
case 'h':
return @"HOME";
case 'w':
return @"WORK";
case 'o':
return @"OTHER";
default:
return @"EMAIL";
}
case 't':
return @"TITLE";
case 'c':
return @"COMPANY";
case 'g':
return @"GROUP";
case 'w':
return @"HOMEPAGE";
case 'b':
switch (subtype) {
case 'b':
return @"BIRTHDAY";
default:
return @"BIRTHDAY";
}
case 'i':
switch (subtype) {
case 'a':
return @"AIM";
case 'y':
return @"YAHOO";
case 'j':
return @"JABBER";
case 'i':
return @"ICQ";
case 'm':
return @"MSN";
default:
return @"IM";
}
case 'N':
return @""; // Note doesn't have a header (should
// come at the end)
}
return nil;
}
/*
Returns the value for a person with this instance's token.
*/
- (NSString *) valueForPerson: (ABPerson *) person {
return [self valueForPerson: person withToken: token];
}
/*
Returns the value for a person with a particular token (e.g. "%fn")
*/
- (NSString *) valueForPerson: (ABPerson *) person
withToken: (NSString *) aToken {
NSString *scratch = nil; // just a temp value
//int subtype = [self subtype];
int length = [aToken length];
char type = [aToken characterAtIndex: (length - 1)];
char subtype = [aToken characterAtIndex: (length - 2)];
if (literalValue != nil)
return literalValue;
switch (type) {
case 'u':
return [person uniqueId];
case 'n':
switch (subtype) {
case 'f':
return getStringForLabel(person, kABFirstNameProperty, nil);
case 'l':
return getStringForLabel(person, kABLastNameProperty, nil);
case 'n':
return getStringForLabel(person, kABNicknameProperty, nil);
default:
return [self getNameString: person];
// Show company name if no first and last name is given.
// if (isNullString([self valueForPerson: person withToken: @"%fn"])
// && isNullString([self valueForPerson: person withToken: @"%ln"]))
// return [self valueForPerson: person withToken: @"%c"];
// return [NSString stringWithFormat: @"%@ %@",
// [self valueForPerson: person withToken: @"%fn"],
// [self valueForPerson: person withToken: @"%ln"]];
}
case 'p':
switch (subtype) {
case 'h':
return getStringForLabel(person,
kABPhoneProperty,
kABPhoneHomeLabel);
case 'w':
return getStringForLabel(person,
kABPhoneProperty,
kABPhoneWorkLabel);
case 'm':
return getStringForLabel(person,
kABPhoneProperty,
kABPhoneMobileLabel);
case 'M':
return getStringForLabel(person,
kABPhoneProperty,
kABPhoneMainLabel);
case 'f':
// We don't differentiate here... I don't think it's worth the effort
scratch = getStringForLabel(person,
kABPhoneProperty,
kABPhoneHomeFAXLabel);
if (!isNullString(scratch))
return scratch;
return getStringForLabel(person,
kABPhoneProperty,
kABPhoneWorkFAXLabel);
case 'p':
return getStringForLabel(person,
kABPhoneProperty,
kABPhonePagerLabel);
case 'o':
return getStringForLabel(person,
kABPhoneProperty,
kABOtherLabel);
default:
return [self valueForPerson: person withTokenPref:
[NSArray arrayWithObjects: @"%hp", @"%wp", @"%mp",
@"%Mp", @"%pp", @"%fp", @"%op", nil]];
}
case 'a':
switch (subtype) {
case 'h':
return [self addressForPerson: person
withLabel: kABAddressHomeLabel];
case 'w':
return [self addressForPerson: person
withLabel: kABAddressWorkLabel];
case 'o':
return [self addressForPerson: person
withLabel: kABOtherLabel];
default:
return [self valueForPerson: person withTokenPref:
[NSArray arrayWithObjects: @"%ha", @"wa",
@"oa", nil]];
}
case 'e':
switch (subtype) {
case 'h':
return getStringForLabel(person,
kABEmailProperty,
kABEmailHomeLabel);
case 'w':
return getStringForLabel(person,
kABEmailProperty,
kABEmailWorkLabel);
case 'o':
return getStringForLabel(person,
kABEmailProperty,
kABOtherLabel);
default:
return [self valueForPerson: person withTokenPref:
[NSArray arrayWithObjects: @"%he", @"%we",
@"%oe", nil]];
}
case 't':
return getStringForLabel(person,
kABJobTitleProperty,
nil);
case 'c':
return getStringForLabel(person,
kABOrganizationProperty,
nil);
case 'w':
return getStringForLabel(person,
kABHomePageProperty,
nil);
case 'b':
switch (subtype) {
case 'b':
return getDateForLabel(person,
kABBirthdayProperty,
nil);
default:
return [getStringForLabel(person,
kABBirthdayProperty,
nil) description];
}
case 'i':
switch (subtype) {
case 'a':
return getStringForLabel(person,
kABAIMInstantProperty,
kABAIMHomeLabel);
case 'y':
return getStringForLabel(person,
kABYahooInstantProperty,
kABYahooHomeLabel);
case 'j':
return getStringForLabel(person,
kABJabberInstantProperty,
kABJabberHomeLabel);
case 'i':
return getStringForLabel(person,
kABICQInstantProperty,
kABICQHomeLabel);
case 'm':
return getStringForLabel(person,
kABMSNInstantProperty,
kABMSNHomeLabel);
default:
return [self valueForPerson: person withTokenPref:
[NSArray arrayWithObjects: @"%ai", @"%yi", @"%ji",
@"%ii", @"%mi", nil]];
}
case 'g':
return getStringForLabel(person, kABGroupNameProperty, nil);
case 'N':
// trim the note string
scratch = getStringForLabel(person, kABNoteProperty, nil);
scratch = [scratch stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
// make it all fit on one line, if we're not loosely formatting
if (!loose) {
int len = [scratch length];
NSMutableString *temp = [NSMutableString stringWithCapacity: len];
[temp setString: scratch];
[temp replaceOccurrencesOfString: @"\n"
withString: @" "
options: NSLiteralSearch
range: NSMakeRange(0, len)];
scratch = temp;
}
return [NSString stringWithFormat: @"\nNOTE: %@", scratch];
}
return nil;
}
/*
Returns value for a person with a set of tokens in order of preference.
*/
- (NSString *) valueForPerson: (ABPerson *) person
withTokenPref: (NSArray *) tokens
{
NSEnumerator* tokenEnum = [tokens objectEnumerator];
NSString* atoken;
NSString* scratch;
while((atoken = [tokenEnum nextObject]) != nil) {
scratch = [self valueForPerson: person withToken: atoken];
if (!isNullString(scratch))
return scratch;
}
return @"";
}
/*
Returns a string version of the address.
*/
- (NSString *) addressForPerson: (ABPerson *) person
withLabel: (NSString *) label
{
NSDictionary *dict;
NSMutableString *address;
NSString *value;
dict = (NSDictionary *) getValueForLabel(person,
kABAddressProperty,
label);
if (dict == nil)
return @"";
address = [NSMutableString string];
if (value = [dict objectForKey: kABAddressStreetKey]) {
[address appendFormat: @"%@, ", value];
}
if (value = [dict objectForKey: kABAddressCityKey]) {
[address appendFormat: @"%@, ", value];
}
if (value = [dict objectForKey: kABAddressStateKey]) {
[address appendFormat: @"%@ ", value];
}
if (value = [dict objectForKey: kABAddressZIPKey]) {
[address appendFormat: @"%@", value];
}
return [NSString stringWithString: address];
}
/*
Returns the name of the person, or company, taking into account
whether it should be firstname first or lastname first.
*/
- (NSString *) getNameString: (ABPerson *) person
{
NSString * firstname;
NSString * lastname;
NSString * companyname;
int flags;
firstname = getStringForLabel(person, kABFirstNameProperty, nil);
lastname = getStringForLabel(person, kABLastNameProperty, nil);
companyname = getStringForLabel(person, kABOrganizationProperty, nil);
if (isNullString(firstname) && isNullString(lastname)) {
// Show company name if no first and last name is given.
return companyname;
}
flags = [[person valueForProperty: kABPersonFlags] intValue];
if (flags & kABShowAsCompany) {
return companyname;
}
if (flags & kABLastNameFirst || firstname_first == NO) {
if (isNullString(lastname))
return firstname;
else
return [NSString stringWithFormat: @"%@, %@",
lastname,
firstname];
}
// by default use firstname first
//printf("flags %d\n", flags);
return [NSString stringWithFormat: @"%@ %@",
firstname,
lastname];
}
@end