-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathQSABSource.m
321 lines (268 loc) · 12.2 KB
/
QSABSource.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
#import "QSObject_ContactHandling.h"
#import "ABPerson_Display.h"
#import "QSABSource.h"
static NSTimer *delayScan = nil;
@implementation QSAddressBookObjectSource
- (id)init {
if ((self = [super init])) {
contactDictionary = [[NSMutableDictionary alloc]init];
addressBookModDate = [NSDate timeIntervalSinceReferenceDate];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addressBookChanged:) name:kABDatabaseChangedExternallyNotification object:nil];
}
return self;
}
- (BOOL)usesGlobalSettings {return YES;}
- (NSArray *)contactGroups {
NSMutableArray *array = [NSMutableArray array];
[array addObject:@"All Contacts"];
ABAddressBook *book = [ABAddressBook addressBook];
NSMutableArray *groups = [[[[book groups] valueForKey:kABGroupNameProperty] mutableCopy] autorelease];
[groups removeObject:@"Me"];
[groups sortUsingSelector:@selector(caseInsensitiveCompare:)];
[array addObjectsFromArray:groups];
return array;
}
- (NSArray *)contactDistributions {
NSMutableArray *array = [NSMutableArray array];
[array addObject:@"None"];
ABAddressBook *book = [ABAddressBook addressBook];
NSMutableArray *groups = [[[[book groups] valueForKey:kABGroupNameProperty] mutableCopy] autorelease];
[groups removeObject:@"Me"];
[groups sortUsingSelector:@selector(caseInsensitiveCompare:)];
[array addObjectsFromArray:groups];
return array;
}
// - (void)refreshGroupList {
// [groupList removeAllItems];
// [distributionList removeAllItems];
//
//
// NSLog(@"group %@", groups);
// [groupList addItemWithTitle:@"All Contacts"];
// [[groupList menu]addItem:[NSMenuItem separatorItem]];
// [groupList addItemsWithTitles:groups];
//
// [distributionList addItemWithTitle:@"Default Emails"];
// [[distributionList menu]addItem:[NSMenuItem separatorItem]];
// [distributionList addItemsWithTitles:groups];
//}
- (NSImage *)iconForEntry:(NSDictionary *)theEntry {return [[NSWorkspace sharedWorkspace]iconForFile:@"/Applications/Contacts.app"];}
- (NSArray *)contactWebPages {
NSMutableArray *array = [NSMutableArray array];
ABAddressBook *book = [ABAddressBook addressBook];
NSArray *people = [book people];
NSEnumerator *personEnumerator = [people objectEnumerator];
id thePerson;
while ((thePerson = [personEnumerator nextObject])) {
NSString *homePage = [thePerson valueForProperty:kABHomePageProperty];
if (!homePage)continue;
NSString *name = NSLocalizedStringFromTableInBundle(@"(no name)", nil, [NSBundle bundleForClass:[self class]], nil);
NSString *namePiece;
BOOL showAsCompany = [[thePerson valueForProperty:kABPersonFlags] integerValue] & kABShowAsMask & kABShowAsCompany;
if (showAsCompany) {
if ((namePiece = [thePerson valueForProperty:kABOrganizationProperty]))
name = namePiece;
}else {
NSMutableArray *nameArray = [NSMutableArray arrayWithCapacity:3];
if ((namePiece = [thePerson valueForProperty:kABFirstNameProperty]))
[nameArray addObject:namePiece];
if ((namePiece = [thePerson valueForProperty:kABMiddleNameProperty]))
[nameArray addObject:namePiece];
if ((namePiece = [thePerson valueForProperty:kABLastNameProperty]))
[nameArray addObject:namePiece];
if ([nameArray count])name = [nameArray componentsJoinedByString:@" "];
}
QSObject *object = [QSObject URLObjectWithURL:homePage
title:name];
if (object) {
[array addObject:object];
}
}
NSSortDescriptor *nameDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"displayName" ascending:YES] autorelease];
[array sortUsingDescriptors:[NSArray arrayWithObject:nameDescriptor]];
return array;
}
- (void) addressBookChanged:(NSNotification *)notif {
[self invalidateSelf];
}
- (BOOL)indexIsValidFromDate:(NSDate *)indexDate forEntry:(NSDictionary *)theEntry {
return ([indexDate timeIntervalSinceReferenceDate] > addressBookModDate);
}
- (void)invalidateSelf {
addressBookModDate = [NSDate timeIntervalSinceReferenceDate];
// cancel previously scheduled scan
[delayScan invalidate];
// wait 10 seconds (for repeated notifications) before scanning
delayScan = [NSTimer scheduledTimerWithTimeInterval:10 target:[super self] selector:@selector(invalidateSelf) userInfo:nil repeats:NO];
}
- (BOOL)objectHasValidChildren:(QSObject *)object
{
return YES;
}
- (BOOL)loadChildrenForObject:(QSObject *)object {
NSArray *abchildren = [QSLib scoredArrayForType:QSABPersonType];
if (![abchildren count]) {
abchildren = [self objectsForEntry:nil];
}
// sort contacts by last name
abchildren = [abchildren sortedArrayUsingComparator:^NSComparisonResult(QSObject *person1, QSObject *person2) {
return [[person1 objectForMeta:@"surname"] caseInsensitiveCompare:[person2 objectForMeta:@"surname"]];
}];
[object setChildren:abchildren];
return YES;
}
- (NSArray *)objectsForEntry:(NSDictionary *)theEntry {
NSMutableArray *array = [NSMutableArray array];
ABAddressBook *book = [ABAddressBook addressBook];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL includePhone = [defaults boolForKey:@"QSABIncludePhone"];
BOOL includeURL = [defaults boolForKey:@"QSABIncludeURL"];
BOOL includeIM = [defaults boolForKey:@"QSABIncludeIM"];
BOOL includeEmail = [defaults boolForKey:@"QSABIncludeEmail"];
BOOL includeContacts = [defaults boolForKey:@"QSABIncludeContacts"];
NSArray *people = [book people];
if (!includeContacts) {
return array;
}
if ([NSApplication isMountainLion]) {
NSMutableArray *addedPeople = [NSMutableArray array];
for (id thePerson in people) {
if ([addedPeople containsObject:thePerson]) {
continue;
}
NSArray *linkedPeople = [thePerson linkedPeople];
ABPerson *localPerson = nil;
if ([linkedPeople count] > 1) {
[addedPeople addObjectsFromArray:linkedPeople];
// linked people exist for this contact, use the 'local' contact (if it exists) as the basis
NSIndexSet *ind = [linkedPeople indexesOfObjectsWithOptions:NSEnumerationConcurrent passingTest:^BOOL(ABPerson *p, NSUInteger idx, BOOL *stop) {
return [p valueForProperty:kABSocialProfileProperty] == nil;
}];
if ([ind count]) {
localPerson = [linkedPeople objectAtIndex:[ind lastIndex]];
} else {
localPerson = [linkedPeople objectAtIndex:0];
}
} else {
localPerson = thePerson;
}
if (includeContacts) [array addObject:[QSObject objectWithPerson:localPerson]];
}
} else {
for(id thePerson in people) {
if (includeContacts) [array addObject:[QSObject objectWithPerson:thePerson]];
if (includePhone) [array addObjectsFromArray:[QSContactObjectHandler phoneObjectsForPerson:thePerson asChild:NO]];
if (includeURL) [array addObjectsFromArray:[QSContactObjectHandler URLObjectsForPerson:thePerson asChild:NO]];
if (includeIM) [array addObjectsFromArray:[QSContactObjectHandler imObjectsForPerson:thePerson asChild:NO]];
if (includeEmail) [array addObjectsFromArray:[QSContactObjectHandler emailObjectsForPerson:thePerson asChild:NO]];
}
}
return array;
}
@end
# define kContactShowAction @"QSABContactShowAction"
# define kContactEditAction @"QSABContactEditAction"
@implementation QSABContactActions
// - (NSArray *)actions {
//
// NSMutableArray *actionArray = [NSMutableArray arrayWithCapacity:5];
// //NSString *chatApp = [[NSWorkspace sharedWorkspace]absolutePathForAppBundleWithIdentifier:[QSReg chatMediatorID]];
//
// //NSImage *chatIcon = [[NSWorkspace sharedWorkspace]iconForFile:chatApp];
//
// // NSImage *finderProxyIcon = [[(QSController *)[NSApp delegate]finderProxy]icon];
//
// QSAction *action;
//
// action = [QSAction actionWithIdentifier:kContactShowAction];
// [action setIcon: [QSResourceManager imageNamed:@"com.apple.AddressBook"]];
// [action setProvider: self];
// [action setArgumentCount:1];
// [actionArray addObject:action];
//
// action = [QSAction actionWithIdentifier:kContactEditAction];
// [action setIcon: [QSResourceManager imageNamed:@"com.apple.AddressBook"]];
// [action setProvider: self];
// [action setArgumentCount:1];
// [actionArray addObject:action];
//
//
//
// return actionArray;
//}
/*
- (NSArray *)validActionsForDirectObject:(QSObject *)dObject indirectObject:(QSObject *)iObject {
NSMutableArray *newActions = [NSMutableArray arrayWithCapacity:1];
if ([[dObject primaryType] isEqualToString:@"ABPeopleUIDsPboardType"]) {
ABPerson *person = (ABPerson *)[[ABAddressBook sharedAddressBook] recordForUniqueId:[dObject identifier]];
[newActions addObject:kContactShowAction];
[newActions addObject:kContactEditAction];
if (0 && [(NSArray *)[person valueForProperty:kABAIMInstantProperty]count]) {
[newActions addObject:kContactIMAction];
// ***warning * learn to check if they are online
[newActions addObject:kContactSendItemIMAction];
// Person *thisPerson = [[[AddressCard alloc]initWithABPerson:person]autorelease];
// [IMService connectToDaemonWithLaunch:NO];
}
// [AddressBookPeople loadBuddyList];
// People *people = [[[People alloc]init]autorelease];
//[people addPerson:thisPerson];
//NSLog(@"%@", );
// [People sendMessageToPeople:[NSArray arrayWithObject:thisPerson]];
// [self defaultEmailAddress];
}else if ([dObject objectForType:QSTextType]) {
[newActions addObject:kItemSendToContactIMAction];
}
return newActions;
}
- (NSArray *)validIndirectObjectsForAction:(NSString *)action directObject:(QSObject *)dObject {
// NSLog(@"request");
if ([action isEqualToString:kContactSendItemEmailAction]) {
return nil; //[QSLibarrayForType:NSFilenamesPboardType];
}
if ([action isEqualToString:kContactSendItemIMAction]) {
QSObject *textObject = [QSObject textProxyObjectWithDefaultValue:@""];
return [NSArray arrayWithObject:textObject]; //[QSLibarrayForType:NSFilenamesPboardType];
// return [NSArray arrayWithObject:QSTextProxy]; //[QSLibarrayForType:NSFilenamesPboardType];
}
if ([action isEqualToString:kItemSendToContactEmailAction]) {
QSLibrarian *librarian = QSLib;
return [librarian scoredArrayForString:nil inSet:[librarian arrayForType:@"ABPeopleUIDsPboardType"]];
return [[librarian arrayForType:@"ABPeopleUIDsPboardType"] sortedArrayUsingSelector:@selector(nameCompare:)];
}
return nil;
}
- (QSObject *)performAction:(QSAction *)action directObject:(QSBasicObject *)dObject indirectObject:(QSBasicObject *)iObject {
//NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
if ([[action identifier] isEqualToString:kContactShowAction]) {
} else if ([[action identifier] isEqualToString:kContactEditAction]) {
}
else if ([[action identifier] isEqualToString:kContactEmailAction]) {
ABPerson *person = (ABPerson *)[[ABAddressBook sharedAddressBook] recordForUniqueId:[dObject identifier]];
NSString *address = [[person valueForProperty:kABEmailProperty]valueAtIndex:0];
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"mailto:%@", address]]];
}
return nil;
}
*/
- (QSObject *)showContact:(QSObject *)dObject {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"addressbook://%@", [dObject identifier]]]];
return nil;
}
- (QSObject *)editContact:(QSObject *)dObject {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"addressbook://%@?edit", [dObject identifier]]]];
return nil;
}
- (QSObject *)sendItemViaIM:(QSObject *)dObject toPerson:(QSObject *)iObject {
if ([dObject validPaths]) {
[[QSReg preferredChatMediator] sendFile:[dObject stringValue] toPerson:[iObject identifier]];
}else {
[[QSReg preferredChatMediator] sendText:[dObject stringValue] toPerson:[iObject identifier]];
}
return nil;
}
- (QSObject *)composeIMToPerson:(QSObject *)dObject {
[[QSReg preferredChatMediator] chatWithPerson:[dObject identifier]];
return nil;
}
@end