-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathQSObject_ContactHandling.m
More file actions
391 lines (330 loc) · 14.6 KB
/
QSObject_ContactHandling.m
File metadata and controls
391 lines (330 loc) · 14.6 KB
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
#import <AddressBook/AddressBook.h>
#import "QSObject_ContactHandling.h"
#import "ABPerson_Display.h"
@implementation QSContactObjectHandler
// Object Handler Methods
- (BOOL)objectHasChildren:(id <QSObject > )object {
return YES;
}
- (void)setQuickIconForObject:(QSObject *)object {
[object setIcon:[QSResourceManager imageNamed:@"VCard"]];
}
- (BOOL)loadIconForObject:(QSObject *)object {
ABPerson *person = [object ABPerson];
NSImage *personImage = [[NSImage alloc] initWithData:[person imageData]];
if (!personImage && [NSApplication isMountainLion]) {
// ML and above: if no icon exists, attempt to get one from a linked person (i.e. another source - Fb, Skype etc.)
for (ABPerson *eachLinkedPerson in [person linkedPeople]) {
if (eachLinkedPerson != person) {
personImage = [[NSImage alloc] initWithData:[eachLinkedPerson imageData]];
if (personImage) {
break;
}
}
}
}
if (personImage) {
[object setIcon:personImage];
[personImage release];
}
return YES;
}
//- (NSString *)identifierForObject:(id <QSObject > )object {
// return [[object objectForType:QSABPersonType] objectAtIndex:0];
//}
- (NSString *)identifierForObject:(id <QSObject > )object {
return [[(QSObject *)object objectForType:@"ABPeopleUIDsPboardType"] objectAtIndex:0];
}
+ (NSString *)contactlingNameForPerson:(ABPerson *)person label:(NSString *)label type:(NSString *)type asChild:(BOOL)child {
if (![label length]) {
// label has no length, so the details have most likely come from an IM service, attenpt to use the IMService name as the label
ABMultiValue *imDeets = [person valueForProperty:kABInstantMessageProperty];
if (imDeets) {
label = [[imDeets valueAtIndex:[imDeets indexForIdentifier:[imDeets primaryIdentifier]]] objectForKey:kABInstantMessageServiceKey];
}
}
label = ABLocalizedPropertyOrLabel(label);
if ([type length]) {
type = ABLocalizedPropertyOrLabel(type);
}
if (child)
return [[NSString stringWithFormat:@"%@%@", label, [type length] ? [NSString stringWithFormat:@" %@",type] : @""] capitalizedString];
else
return [NSString stringWithFormat:@"%@'s %@%@", [person displayName], label, [type length] ? [NSString stringWithFormat:@" %@",type] : @""];
}
+ (NSArray *)URLObjectsForPerson:(ABPerson *)person asChild:(BOOL)asChild {
NSMutableArray *contactlings = [NSMutableArray arrayWithCapacity:1];
NSArray *peeps = nil;
if ([NSApplication isMountainLion]) {
peeps = [person linkedPeople];
} else {
peeps = [NSArray arrayWithObject:person];
}
NSMutableArray *usedURLs = [NSMutableArray array];
for (ABPerson *thePerson in peeps) {
ABMultiValue *urls = [thePerson valueForProperty:kABURLsProperty];
NSUInteger i;
for (i = 0; i < [urls count]; i++) {
NSString *url = [urls valueAtIndex:i];
if ([usedURLs containsObject:url]) {
continue;
}
[usedURLs addObject:url];
NSString *name = [self contactlingNameForPerson:thePerson label:[urls labelAtIndex:i] type:kABURLsProperty asChild:asChild];
QSObject *obj = [QSObject URLObjectWithURL:url title:name];
if (obj) {
[obj setParentID:[person uniqueId]];
[contactlings addObject:obj];
}
}
}
return contactlings;
}
+ (NSArray *)emailObjectsForPerson:(ABPerson *)person asChild:(BOOL)asChild {
NSMutableArray *contactlings = [NSMutableArray arrayWithCapacity:1];
NSUInteger i;
NSArray *peeps = nil;
if ([NSApplication isMountainLion]) {
peeps = [person linkedPeople];
} else {
peeps = [NSArray arrayWithObject:person];
}
NSMutableArray *usedEmails = [NSMutableArray array];
for (ABPerson *thePerson in peeps) {
ABMultiValue *emailAddresses = [thePerson valueForProperty:kABEmailProperty];
for (i = 0; i < [emailAddresses count]; i++) {
NSString *address = [emailAddresses valueAtIndex:i];
if ([usedEmails containsObject:address]) {
continue;
}
[usedEmails addObject:address];
NSString *name = [self contactlingNameForPerson:thePerson label:[emailAddresses labelAtIndex:i] type:kABEmailProperty asChild:asChild];
QSObject *obj = [QSObject URLObjectWithURL:[NSString stringWithFormat:@"mailto:%@", address]
title:name];
[obj setLabel:address];
[obj setDetails:name];
if (obj) {
[obj setParentID:[person uniqueId]];
[contactlings addObject:obj];
}
}
}
return contactlings;
}
+ (NSArray *)phoneObjectsForPerson:(ABPerson *)person asChild:(BOOL)asChild {
NSMutableArray *contactlings = [NSMutableArray arrayWithCapacity:1];
NSMutableArray *usedPhoneNos = [NSMutableArray array];
NSArray *peeps = nil;
if ([NSApplication isMountainLion]) {
peeps = [person linkedPeople];
} else {
peeps = [NSArray arrayWithObject:person];
}
for (ABPerson *thePerson in peeps) {
ABMultiValue *phoneNumbers = [thePerson valueForProperty:kABPhoneProperty];
NSUInteger i;
for (i = 0; i < [phoneNumbers count]; i++) {
NSString *phoneNo = [phoneNumbers valueAtIndex:i];
if ([usedPhoneNos containsObject:phoneNo]) {
continue;
}
[usedPhoneNos addObject:phoneNo];
NSString *name = [self contactlingNameForPerson:thePerson label:[phoneNumbers labelAtIndex:i] type:kABPhoneProperty asChild:asChild];
QSObject *obj = [QSObject objectWithContactDetail:phoneNo name:name type:QSContactPhoneType];
if (obj) {
[obj setParentID:[person uniqueId]];
[contactlings addObject:obj];
}
}
}
return contactlings;
}
+ (NSArray *)addressObjectsForPerson:(ABPerson *)person asChild:(BOOL)asChild {
NSMutableArray *contactlings = [NSMutableArray arrayWithCapacity:1];
NSUInteger i;
NSMutableArray *usedAddresses = [NSMutableArray array];
NSArray *peeps = nil;
if ([NSApplication isMountainLion]) {
peeps = [person linkedPeople];
} else {
peeps = [NSArray arrayWithObject:person];
}
for (ABPerson *thePerson in peeps) {
ABMultiValue *addresses = [thePerson valueForProperty:kABAddressProperty];
for (i = 0; i < [addresses count]; i++) {
ABMultiValue *address = [addresses valueAtIndex:i];
NSString *string = [[[ABAddressBook sharedAddressBook] formattedAddressFromDictionary:(NSDictionary *)address] string];
if ([usedAddresses containsObject:string]) {
continue;
}
[usedAddresses addObject:string];
NSString *name = [self contactlingNameForPerson:thePerson label:[addresses labelAtIndex:i] type:kABAddressProperty asChild:asChild];
QSObject * obj = [QSObject objectWithContactDetail:string name:name type:QSContactAddressType];
if (obj) {
[obj setParentID:[person uniqueId]];
[contactlings addObject:obj];
}
}
}
return contactlings;
}
+ (NSArray *)imObjectsForPerson:(ABPerson *)person asChild:(BOOL)asChild {
NSMutableSet *contactlings = [NSMutableArray arrayWithCapacity:1];
NSUInteger i;
NSMutableArray *usernames = [NSMutableArray array];
NSArray *peeps = nil;
if ([NSApplication isMountainLion]) {
peeps = [person linkedPeople];
} else {
peeps = [NSArray arrayWithObject:person];
}
for (ABPerson *thePerson in peeps) {
ABMultiValue *ims = [person valueForProperty:kABInstantMessageProperty];
for (i = 0; i < [ims count]; i++) {
NSString *username = [[ims valueAtIndex:i] objectForKey:kABInstantMessageUsernameKey];
if ([usernames containsObject:username]) {
continue;
}
[usernames addObject:username];
NSString *name = [self contactlingNameForPerson:person label:[ims labelAtIndex:i] type:@"" asChild:asChild];
QSObject *obj = [QSObject objectWithContactDetail:username name:name type:QSIMAccountType];
if (obj) {
[obj setParentID:[person uniqueId]];
[contactlings addObject:obj];
}
}
}
return [contactlings allObjects];
}
- (BOOL)loadChildrenForObject:(QSObject *)object {
ABPerson *person = [object ABPerson];
NSMutableArray *contactlings = [NSMutableArray arrayWithCapacity:1];
[contactlings addObjectsFromArray:[QSContactObjectHandler phoneObjectsForPerson:person asChild:YES]];
[contactlings addObjectsFromArray:[QSContactObjectHandler emailObjectsForPerson:person asChild:YES]];
[contactlings addObjectsFromArray:[QSContactObjectHandler imObjectsForPerson:person asChild:YES]];
[contactlings addObjectsFromArray:[QSContactObjectHandler addressObjectsForPerson:person asChild:YES]];
[contactlings addObjectsFromArray:[QSContactObjectHandler URLObjectsForPerson:person asChild:YES]];
NSString *note = [person valueForProperty:kABNoteProperty];
if (note && [note length]) {
QSObject *obj = [QSObject objectWithString:note];
if (obj) {
[obj setParentID:[object identifier]];
[contactlings addObject:obj];
}
}
[contactlings makeObjectsPerformSelector:@selector(setParentID:) withObject:[object identifier]];
if (contactlings) {
[object setChildren:contactlings];
return YES;
}
return NO;
}
@end
@implementation QSObject (ContactHandling)
#pragma mark QSObject creation methods
- (QSObject *)initWithPerson:(ABPerson *)person {
//id object = [QSObject objectWithIdentifier:[person uniqueId]];
if ((self = [self init])) {
[data setObject:[person uniqueId] forKey:QSABPersonType];
//[QSObject registerObject:self withIdentifier:[self identifier]];
[self setIdentifier:[person uniqueId]];
[self loadContactInfo:person];
}
return self;
}
+ (QSObject *)objectWithPerson:(ABPerson *)person {
return [[[QSObject alloc] initWithPerson:person] autorelease];
}
+ (QSObject *)objectWithContactDetail:(NSString *)detail name:(NSString *)name type:(NSString *)type {
QSObject * obj = [QSObject objectWithString:detail name:name type:type];
[obj setDetails:name];
[obj setLabel:detail];
return obj;
}
// - -NSString *formalName(NSString *title, NSString *firstName, NSString *middleName, NSString *lastName, NSString *suffix) {
//NSMutableString *formalName=
- (ABPerson *)ABPerson
{
return (ABPerson *)[[ABAddressBook sharedAddressBook] recordForUniqueId:[self identifier]];
}
- (void)loadContactInfo {
[self loadContactInfo:nil];
}
- (void)loadContactInfo:(ABPerson *)person {
if (person == nil) {
person = [self ABPerson];
}
NSString *newName = nil;
NSString *newLabel = nil;
//NSString *firstName = [person valueForProperty:kABFirstNameProperty];
NSString *lastName = [person valueForProperty:kABLastNameProperty];
//NSString *middleName = [person valueForProperty:kABMiddleNameProperty];
NSString *nickName = [person valueForProperty:kABNicknameProperty];
//NSString *title = [person valueForProperty:kABTitleProperty];
//NSString *suffix = [person valueForProperty:kABSuffixProperty];
NSString *jobTitle = [person valueForProperty:kABJobTitleProperty];
NSString *companyName = [person valueForProperty:kABOrganizationProperty];
newName = [NSString stringWithFormat:@"%@ %@", nickName, lastName];
[self setName:newName];
[self setObject:lastName forMeta:@"surname"];
newLabel = [person displayName];
if (newLabel) {
[self setLabel:newLabel];
}
[self setPrimaryType:QSABPersonType];
ABMultiValue *emailAddresses = [person valueForProperty:kABEmailProperty];
NSString *address = [emailAddresses valueAtIndex:0];
if (address) {
[self setObject:address forType:QSEmailAddressType];
}
NSMutableArray *detailsParts = [[NSMutableArray alloc] init];
if (jobTitle) {
[detailsParts addObject:jobTitle];
}
if (companyName) {
[detailsParts addObject:companyName];
}
if ([detailsParts count]) {
// show title, company, or "title, company" if both are present
[self setDetails:[detailsParts componentsJoinedByString:@", "]];
} else {
// prevent details from showing at all by setting them equal to name
[self setDetails:[self displayName]];
}
[detailsParts release];
/* NSArray *aimAccounts = [person valueForProperty:kABAIMInstantProperty];
if ([aimAccounts count])
[self setObject:[NSString stringWithFormat:@"AIM:%@", [aimAccounts valueAtIndex:0]] forType:QSIMAccountType]; */
[self useDefaultIMFromPerson:person];
}
- (BOOL)useDefaultEmailFromPerson:(ABPerson *)person {
return NO;
}
/*!
* @abstract If possible, makes this object respond to IM actions by associating it with the first IM account it finds.
* @param person The person to search for IM accounts.
* @result YES if a suitable IM account was found, NO otherwise.
*/
- (BOOL)useDefaultIMFromPerson:(ABPerson *)person {
ABMultiValue *im = [person valueForProperty:kABInstantMessageProperty];
// [ABMultiValue propertyType] == kABMultiDictionaryProperty (= kABMultiValueMask | kABDictionaryProperty)
if (!im) {
return NO;
}
NSDictionary *primaryIM = [im valueAtIndex:[im indexForIdentifier:[im primaryIdentifier]]];
NSString *primaryIMType = [primaryIM objectForKey:kABInstantMessageServiceKey];
if (!primaryIMType) {
return NO;
}
static NSDictionary *imProperties = nil;
if (imProperties == nil) {
imProperties = [[NSDictionary alloc] initWithObjectsAndKeys:@"AIM:", kABInstantMessageServiceAIM, @"MSN:", kABInstantMessageServiceMSN, @"Yahoo!:", kABInstantMessageServiceYahoo, @"ICQ:", kABInstantMessageServiceICQ, @"Jabber:", kABInstantMessageServiceJabber, @"Facebook:", kABInstantMessageServiceFacebook, @"Google Talk:", kABInstantMessageServiceGoogleTalk, @"Gadu Gadu:", kABInstantMessageServiceGaduGadu, @"QQ:", kABInstantMessageServiceQQ, @"Skype:", kABInstantMessageServiceSkype, nil];
}
NSString *prefix = [imProperties objectForKey:primaryIMType];
if (!prefix) {
return NO;
}
[self setObject:[prefix stringByAppendingString:[primaryIM objectForKey:kABInstantMessageUsernameKey]] forType:QSIMAccountType];
return YES;
}
@end