-
Notifications
You must be signed in to change notification settings - Fork 1
/
XSContact.m
126 lines (97 loc) · 3.34 KB
/
XSContact.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
//
// XSContact.m
// SkypeToAddressBook
//
// Created by Xavi Aracil on 16/08/10.
// Copyright 2010 xaracSoft (Xavi Aracil Diaz). All rights reserved.
//
#import "XSContact.h"
#import "XSABContact.h"
#import <AddressBook/AddressBook.h>
#pragma mark Private Methods
@interface XSContact ()
+(NSImage *) defaultPhoto;
-(void) setTransientProperties;
-(void) configureWithSkypeName:(NSString *) skypeName addressBookContact:(NSString *) uniqueId;
@end
@interface XSContact (PrimitiveAccessors)
- (void)setPrimitiveUniqueID:(NSString *)newUniqueID;
@end
#pragma mark -
#pragma mark Public implementation
@implementation XSContact
@dynamic skypeName, uniqueID;
@synthesize photo;
@synthesize name;
- (void) dealloc {
[photo release];
[name release];
[super dealloc];
}
- (void) setUniqueID:(NSString *)newUniqueID {
[self willChangeValueForKey:@"uniqueID"];
// update address book record
ABAddressBook *addressBook = [ABAddressBook sharedAddressBook];
if(self.uniqueID) {
ABPerson *record = (ABPerson *) [addressBook recordForUniqueId:self.uniqueID];
[record removeValueForProperty:kXSSkypeProperty];
}
[self setPrimitiveUniqueID:newUniqueID];
if(newUniqueID) {
ABPerson *record = (ABPerson *) [addressBook recordForUniqueId:self.uniqueID];
[record setValue:self.skypeName forProperty:kXSSkypeProperty];
}
[self didChangeValueForKey:@"uniqueID"];
[self setTransientProperties];
}
+(NSImage *) defaultPhotoForUsersNotInAddressBook {
return [NSImage imageNamed:@"PersonSquare"];
}
#pragma mark -
#pragma mark Non-modeled attributes
-(BOOL) isInAddressBook {
return self.uniqueID != NULL;
}
+ (id) xsContactWithSkypeName:(NSString *) skypeName addressBookUniqueId:(NSString *) uniqueId context:(NSManagedObjectContext *) context {
XSContact *newItem;
newItem = [NSEntityDescription insertNewObjectForEntityForName:@"Contact" inManagedObjectContext:context];
// configure
[newItem configureWithSkypeName:skypeName addressBookContact:uniqueId];
return newItem;
}
#pragma mark -
#pragma mark Private Methods
-(void) awakeFromFetch {
[super awakeFromFetch];
[self setTransientProperties];
}
-(void) setTransientProperties {
NSImage *contactImage = NULL;
NSString *fullName = NULL;
if (self.uniqueID) {
ABAddressBook *addressBook = [ABAddressBook sharedAddressBook];
ABPerson *record = (ABPerson *) [addressBook recordForUniqueId:self.uniqueID];
contactImage = [[[NSImage alloc] initWithData: [record imageData]] autorelease];
fullName = [XSABContact fullNameForPerson:record];
} else {
contactImage = [[self class] defaultPhotoForUsersNotInAddressBook];
fullName = @" ";
}
// image
[self willChangeValueForKey:@"photo"];
self.photo = contactImage ? contactImage : [[self class] defaultPhoto];
[self didChangeValueForKey:@"photo"];
// name
[self willChangeValueForKey:@"name"];
self.name = [fullName isEqualToString:@" "] ? self.skypeName : fullName;
[self didChangeValueForKey:@"name"];
}
-(void) configureWithSkypeName:(NSString *) aSkypeName addressBookContact:(NSString *) uniqueId {
self.skypeName = aSkypeName;
self.uniqueID = uniqueId;
[self setTransientProperties];
}
+(NSImage *) defaultPhoto {
return [NSImage imageNamed:NSImageNameUser];
}
@end