-
Notifications
You must be signed in to change notification settings - Fork 2
/
ABKeyEmailSearchResultsController.m
232 lines (182 loc) · 6.37 KB
/
ABKeyEmailSearchResultsController.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
//
// ABKeyEmailSearchResultsController.m
// ABKeyManager
//
// Created by Robert Goldsmith on 20/03/2005.
// Copyright 2005 Far-Blue. All rights reserved.
//
#import <AddressBook/AddressBook.h>
#import "GPGMEController.h"
#import "ABKeyEmailSearchResultsController.h"
#import "ABKeyEmailSearchSelectButtonCell.h"
#import "GPGKey extensions.h"
@implementation ABKeyEmailSearchResultsController
+(void)showEmailSearchResults:(GPGContext *)results forPerson:(ABPerson *)thePerson
{
[[self alloc] initWithReturnedContext:results forPerson:thePerson];
}
-(id)initWithReturnedContext:(GPGContext *)theContext forPerson:(ABPerson *)thePerson
{
if([super init])
{
unsigned int i;
person=[thePerson retain];
context=[theContext retain];
NSArray *keyArray=[[context operationResults] objectForKey:@"keys"];
NSArray *localKeys=[GPGMEController keysForRecord:thePerson gpgContext:theContext];
// NSLog(@"local keys: %@. remote keys: %@",[[localKeys lastObject] shortKeyID],[[keyArray lastObject] shortKeyID]);
selectedKeys=[[NSMutableSet alloc] initWithCapacity:[keyArray count]];
keys=[[NSMutableArray alloc] initWithCapacity:[keyArray count]];
NSEnumerator *keyEnumerator=[keyArray objectEnumerator];
GPGRemoteKey *currentGPGKey;
while((currentGPGKey=[keyEnumerator nextObject]))
{
if(![keys containsObject:currentGPGKey])
{
BOOL existsLocally=NO;
[keys addObject:currentGPGKey];
for(i=0;i<[localKeys count];i++)
if([[[localKeys objectAtIndex:i] shortKeyID] isEqualToString:[currentGPGKey shortKeyID]])
{
existsLocally=YES;
break;
}
if(!existsLocally && ![currentGPGKey isKeyRevoked])
[self addKeyToSelected:currentGPGKey];
}
}
[NSBundle loadNibNamed:@"EmailSearchResults" owner:self];
[self setCurrentKey:nil];
[window setTitle:[NSString stringWithFormat:@"Key Search for %@ %@",[person valueForProperty:kABFirstNameProperty],[person valueForProperty:kABLastNameProperty]]];
[window makeKeyAndOrderFront:self];
[[GPGMEController sharedController] addEmailSearchDialogue:window forPerson:person];
}
return self;
}
-(void)addKeyToSelected:(GPGRemoteKey *)newKey
{
if([selectedKeys count]==0)
[importKeys setEnabled:YES];
[selectedKeys addObject:newKey];
}
-(void)removeKeyFromSelected:(GPGRemoteKey *)newKey
{
[selectedKeys removeObject:newKey];
if([selectedKeys count]==0)
[importKeys setEnabled:NO];
}
//bindings
-(GPGRemoteKey *)currentKey
{
return currentKey;
}
-(void)setCurrentKey:(GPGRemoteKey *)newKey
{
[self willChangeValueForKey:@"currentKey"];
currentKey=newKey;
[self didChangeValueForKey:@"currentKey"];
}
//window delegate methods
-(void)windowWillClose:(NSNotification *)aNotification
{
[[GPGMEController sharedController] removeEmailSearchDialogueForPerson:person];
[self release];
}
//outline view delegate methods
- (void)outlineViewSelectionDidChange:(NSNotification *)notification
{
id selectedItem=[outlineView itemAtRow:[outlineView selectedRow]];
if([selectedItem isKindOfClass:[GPGRemoteKey class]])
[self setCurrentKey:selectedItem];
else if([selectedItem isKindOfClass:[GPGRemoteUserID class]])
[self setCurrentKey:(GPGRemoteKey *)[selectedItem key]];
else
[self setCurrentKey:nil];
}
//Outline view datasource methods
- (id)outlineView:(NSOutlineView *)outlineView child:(int)childIndex ofItem:(id)item
{
if(item)
return [[item userIDs] objectAtIndex:childIndex];
return [keys objectAtIndex:childIndex]; //nil -> root items
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
{ return [item isKindOfClass:[GPGRemoteKey class]];}
- (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
{
if(item)
return [[item userIDs] count];
return [keys count]; //nil -> root items
}
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
{
if([[tableColumn identifier] isEqualToString:@"select"])
{
if([item isKindOfClass:[GPGRemoteKey class]])
return [NSNumber numberWithInt:[selectedKeys containsObject:item]];
return [NSNumber numberWithInt:-1];
}
else if([[tableColumn identifier] isEqualToString:@"info"])
{
if([item isKindOfClass:[GPGRemoteKey class]])
{
if([item isKeyRevoked])
return [[[NSAttributedString alloc] initWithString:[item formattedShortKeyID] attributes:[NSDictionary dictionaryWithObject:[NSColor colorWithCalibratedRed:0.7 green:0.0 blue:0.0 alpha:1] forKey:NSForegroundColorAttributeName]] autorelease];
return [item formattedShortKeyID];
}
else if([item isKindOfClass:[GPGRemoteUserID class]])
return [item userID];
}
return @"";
}
-(void)outlineView:(NSOutlineView *)outlineView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
{
if(![[tableColumn identifier] isEqualToString:@"select"])
return;
if([object intValue]==1)
[self addKeyToSelected:item];
else
[self removeKeyFromSelected:item];
}
//button targets
-(IBAction)close:(id)sender
{[window close];}
-(IBAction)import:(id)sender
{
if([selectedKeys count]==0)
return;
//change the view and animate the window resize
NSRect importViewFrame=[window frameRectForContentRect:[importingView frame]];
NSRect windowFrame=[window frame];
importViewFrame.origin.y=windowFrame.origin.y+windowFrame.size.height-importViewFrame.size.height;
importViewFrame.origin.x=windowFrame.origin.x+(windowFrame.size.width/2)-(importViewFrame.size.width/2);
[window setContentView:importingView];
[window setShowsResizeIndicator:NO];
[window setTitle:@"Importing..."];
[window setFrame:importViewFrame display:YES animate:YES];
//start the progress indicator
[importingProgress startAnimation:self];
//register for notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(emailKeyImportStatus:) name:@"GPGAsynchronousOperationDidTerminateNotification" object:context];
//download the selected keys
[context asyncDownloadKeys:[selectedKeys allObjects] serverOptions:nil];
}
-(IBAction)cancelImport:(id)sender
{
[context interruptAsyncOperation];
//this will automatically result in a notification which will close the window and clean up
}
-(void)emailKeyImportStatus:(NSNotification *)notification
{
[window close];
}
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[keys release];
[selectedKeys release];
[person release];
[context release];
[super dealloc];
}
@end