-
Notifications
You must be signed in to change notification settings - Fork 2
/
ABPerson+merging.m
146 lines (139 loc) · 3.96 KB
/
ABPerson+merging.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
//
// ABPerson+merging.m
// Jabber
//
// Created by David Chisnall on 19/11/2007.
// Copyright 2007 __MyCompanyName__. All rights reserved.
//
#import <EtoileFoundation/EtoileFoundation.h>
// FIXME: Ugly hack
#ifdef GNUSTEP
#define ABPerson ADPerson
#endif
#import "ABPerson+merging.h"
#define MATCH(x) [ABPerson searchElementForProperty:x\
label:nil\
key:nil\
value:[self valueForProperty:x]\
comparison:kABEqual];
#define MATCH_IM_ADDRESS(type) if((addresses = [self valueForProperty:type]) != nil)\
{\
for(unsigned int i=0 ; i<[addresses count] ; i++)\
{\
id address = [addresses valueAtIndex:i];\
ABSearchElement * search = [ABPerson searchElementForProperty:type\
label:nil\
key:nil\
value:address\
comparison:kABEqual];\
NSArray * people = [ab recordsMatchingSearchElement:search];\
if([people count] == 1)\
{\
return [people objectAtIndex:0];\
}\
}\
}
@implementation ABPerson (merging)
- (ABPerson*) findExistingPerson
{
ABAddressBook * ab = [ABAddressBook sharedAddressBook];
NSString * firstName = [self valueForProperty:kABFirstNameProperty];
if([self valueForProperty:kABLastNameProperty] && firstName)
{
ABSearchElement * search = MATCH(kABLastNameProperty);
NSArray * people = [ab recordsMatchingSearchElement:search];
FOREACH(people, person, ABPerson*)
{
if([firstName isEqualToString:[person valueForProperty:kABFirstNameProperty]])
{
return person;
}
}
}
else
{
#ifndef GNUSTEP
#define kABJabberInstantProperty kABInstantMessageServiceJabber
#define kABMSNInstantProperty kABInstantMessageServiceMSN
#define kABAIMInstantProperty kABInstantMessageServiceAIM
#define kABICQInstantProperty kABInstantMessageServiceICQ
#define kABYahooInstantProperty kABInstantMessageServiceYahoo
#endif
ABMultiValue * addresses;
MATCH_IM_ADDRESS(kABJabberInstantProperty)
MATCH_IM_ADDRESS(kABMSNInstantProperty)
MATCH_IM_ADDRESS(kABAIMInstantProperty)
MATCH_IM_ADDRESS(kABICQInstantProperty)
MATCH_IM_ADDRESS(kABYahooInstantProperty)
}
return nil;
}
- (NSArray*) mergePerson:(ABPerson*)aPerson
{
NSArray * properties = [ABPerson properties];
NSMutableArray * failedProperties = [NSMutableArray array];
FOREACH(properties, property, NSString*)
{
//Don't update implicit properties.
if(!([property isEqualToString:kABUIDProperty]
||
[property isEqualToString:kABCreationDateProperty]
||
[property isEqualToString:kABModificationDateProperty]))
{
id otherValue = [aPerson valueForProperty:property];
if(otherValue != nil)
{
id value = [self valueForProperty:property];
//If we have no copy of this, just add it
if(value == nil)
{
[self setValue:otherValue forProperty:property];
}
else
{
//If it's a multivalue with compatible types, merge it:
if([value isKindOfClass:[ABMultiValue class]] &&
([value propertyType] == [otherValue propertyType]))
{
ABMutableMultiValue * old = [value mutableCopy];
for(unsigned int i=0 ; i<[otherValue count] ; i++)
{
id v = [(ABMultiValue*)otherValue valueAtIndex:i];
BOOL duplicate = NO;
for(unsigned int j=0 ; i<[old count] ; i++)
{
if([v isEqual:[old valueAtIndex:j]])
{
duplicate = YES;
break;
}
}
if(!duplicate)
{
[old addValue:v withLabel:[otherValue labelAtIndex:i]];
}
}
[self setValue:old forProperty:property];
}
else
{
if(![otherValue isEqual:value])
{
#ifndef DNDEBUG
ETLog(@"Not merging %@. %@ => %@", property, value, otherValue);
#endif
[failedProperties addObject:property];
}
}
}
}
}
}
if([self imageData] == nil)
{
[self setImageData:[aPerson imageData]];
}
return failedProperties;
}
@end