root / pithos-macos / pithos_macosAppDelegate.m @ 32ae06f7
History | View | Annotate | Download (18.9 kB)
1 |
// |
---|---|
2 |
// pithos_macosAppDelegate.m |
3 |
// pithos-macos |
4 |
// |
5 |
// Copyright 2011-2012 GRNET S.A. All rights reserved. |
6 |
// |
7 |
// Redistribution and use in source and binary forms, with or |
8 |
// without modification, are permitted provided that the following |
9 |
// conditions are met: |
10 |
// |
11 |
// 1. Redistributions of source code must retain the above |
12 |
// copyright notice, this list of conditions and the following |
13 |
// disclaimer. |
14 |
// |
15 |
// 2. Redistributions in binary form must reproduce the above |
16 |
// copyright notice, this list of conditions and the following |
17 |
// disclaimer in the documentation and/or other materials |
18 |
// provided with the distribution. |
19 |
// |
20 |
// THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS |
21 |
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
22 |
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
23 |
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR |
24 |
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
25 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
26 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
27 |
// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
28 |
// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
29 |
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
30 |
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
31 |
// POSSIBILITY OF SUCH DAMAGE. |
32 |
// |
33 |
// The views and conclusions contained in the software and |
34 |
// documentation are those of the authors and should not be |
35 |
// interpreted as representing official policies, either expressed |
36 |
// or implied, of GRNET S.A. |
37 |
|
38 |
#import "pithos_macosAppDelegate.h" |
39 |
#import "PithosAccount.h" |
40 |
#import "PithosBrowserController.h" |
41 |
#import "PithosPreferencesController.h" |
42 |
#import "PithosSyncDaemon.h" |
43 |
#import "ASIPithosRequest.h" |
44 |
#import "ASIPithos.h" |
45 |
#import "ASIDownloadCache.h" |
46 |
#import "LastCompletedSyncTransformer.h" |
47 |
|
48 |
@implementation pithos_macosAppDelegate |
49 |
@synthesize pithosBrowserController, pithosPreferencesController, alwaysNo, activated, |
50 |
currentPithosAccount, pithosAccounts, pithosAccountsDictionary, syncPithosAccount, activityFacilityTimeInterval; |
51 |
|
52 |
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { |
53 |
[[NSAppleEventManager sharedAppleEventManager] setEventHandler:self |
54 |
andSelector:@selector(handleAppleEvent:withReplyEvent:) |
55 |
forEventClass:kInternetEventClass |
56 |
andEventID:kAEGetURL]; |
57 |
|
58 |
userDefaults = [[NSUserDefaults standardUserDefaults] retain]; |
59 |
|
60 |
syncTimeInterval = [userDefaults doubleForKey:@"syncTimeInterval"]; |
61 |
if (syncTimeInterval <= 0.0) { |
62 |
syncTimeInterval = 180.0; |
63 |
[userDefaults setDouble:syncTimeInterval forKey:@"syncTimeInterval"]; |
64 |
[userDefaults synchronize]; |
65 |
} |
66 |
|
67 |
activityFacilityTimeInterval = [userDefaults doubleForKey:@"activityFacilityTimeInterval"]; |
68 |
if (activityFacilityTimeInterval <= 0.0) { |
69 |
activityFacilityTimeInterval = 0.05; |
70 |
[userDefaults setDouble:activityFacilityTimeInterval forKey:@"activityFacilityTimeInterval"]; |
71 |
[userDefaults synchronize]; |
72 |
} |
73 |
|
74 |
NSData *tmpData = [userDefaults objectForKey:@"pithosAccounts"]; |
75 |
NSArray *tmpArray; |
76 |
if (tmpData && (tmpArray = [NSKeyedUnarchiver unarchiveObjectWithData:tmpData])) |
77 |
self.pithosAccounts = [NSMutableArray arrayWithArray:tmpArray]; |
78 |
else |
79 |
self.pithosAccounts = [NSMutableArray array]; |
80 |
|
81 |
if (![pithosAccounts count]) { |
82 |
[pithosAccounts addObject:[PithosAccount pithosAccount]]; |
83 |
self.pithosAccounts = self.pithosAccounts; |
84 |
} else { |
85 |
self.activated = YES; |
86 |
} |
87 |
|
88 |
pithosAccountsDictionary = [[NSMutableDictionary alloc] initWithCapacity:[pithosAccounts count]]; |
89 |
for (PithosAccount *pithosAccount in pithosAccounts) { |
90 |
[pithosAccountsDictionary setObject:pithosAccount forKey:pithosAccount.name]; |
91 |
if (!currentPithosAccount && pithosAccount.active) |
92 |
currentPithosAccount = [pithosAccount retain]; |
93 |
} |
94 |
if (!currentPithosAccount) |
95 |
self.currentPithosAccount = [pithosAccounts objectAtIndex:0]; |
96 |
|
97 |
if (currentPithosAccount.active) { |
98 |
[self savePithosAccounts:self]; |
99 |
[self showPithosBrowser:self]; |
100 |
self.pithosBrowserController.pithos = currentPithosAccount.pithos; |
101 |
} else { |
102 |
// XXX maybe call specifically to go to new account tab |
103 |
[self showPithosPreferences:self]; |
104 |
} |
105 |
|
106 |
syncTimer = [[NSTimer scheduledTimerWithTimeInterval:syncTimeInterval |
107 |
target:self |
108 |
selector:@selector(sync) |
109 |
userInfo:nil |
110 |
repeats:YES] retain]; |
111 |
[syncTimer fire]; |
112 |
} |
113 |
|
114 |
// Based on: http://cocoatutorial.grapewave.com/2010/01/creating-a-status-bar-application/ |
115 |
// and: http://www.cocoadev.com/index.pl?ThumbnailImages |
116 |
- (void)awakeFromNib { |
117 |
NSImage *sourceImage = [NSImage imageNamed:@"pithos-large.png"]; |
118 |
|
119 |
NSImage *smallImage = [[[NSImage alloc] initWithSize:NSMakeSize(18, 18)] autorelease]; |
120 |
[smallImage lockFocus]; |
121 |
[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh]; |
122 |
[sourceImage setSize:NSMakeSize(18, 18)]; |
123 |
[sourceImage compositeToPoint:NSZeroPoint operation:NSCompositeCopy]; |
124 |
[smallImage unlockFocus]; |
125 |
|
126 |
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain]; |
127 |
[statusItem setMenu:statusMenu]; |
128 |
[statusItem setImage:sourceImage]; |
129 |
[statusItem setHighlightMode:YES]; |
130 |
|
131 |
self.alwaysNo = NO; |
132 |
} |
133 |
|
134 |
- (void)handleAppleEvent:(NSAppleEventDescriptor *)event withReplyEvent: (NSAppleEventDescriptor *)replyEvent { |
135 |
NSURL *url = [NSURL URLWithString:[[event paramDescriptorForKeyword:keyDirectObject] stringValue]]; |
136 |
NSString *host = [url host]; |
137 |
NSString *query = [url query]; |
138 |
PithosAccount *pithosAccount = [pithosAccountsDictionary objectForKey:[url lastPathComponent]]; |
139 |
NSProcessInfo *processInfo = [NSProcessInfo processInfo]; |
140 |
if ([host isEqualToString:[NSString stringWithFormat:@"%d", [processInfo processIdentifier]]] && pithosAccount && query) { |
141 |
// user= |
142 |
NSString *authUser; |
143 |
NSRange userRange = [query rangeOfString:@"user=" options:NSCaseInsensitiveSearch]; |
144 |
if (userRange.length == 0) |
145 |
// XXX maybe show an error message? |
146 |
return; |
147 |
NSUInteger authUserStartLocation = userRange.location + userRange.length; |
148 |
NSRange userEndRange = [query rangeOfString:@"&" options:NSCaseInsensitiveSearch |
149 |
range:NSMakeRange(authUserStartLocation, [query length] - authUserStartLocation)]; |
150 |
if (userEndRange.length) { |
151 |
authUser = [[query substringWithRange:NSMakeRange(authUserStartLocation, userEndRange.location - authUserStartLocation)] |
152 |
stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; |
153 |
} else { |
154 |
authUser = [[query substringFromIndex:authUserStartLocation] |
155 |
stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; |
156 |
} |
157 |
// token= |
158 |
NSString *authToken; |
159 |
NSRange tokenRange = [query rangeOfString:@"token=" options:NSCaseInsensitiveSearch]; |
160 |
if (tokenRange.length == 0) |
161 |
// XXX maybe show an error message? |
162 |
return; |
163 |
NSUInteger authTokenStartLocation = tokenRange.location + tokenRange.length; |
164 |
NSRange tokenEndRange = [query rangeOfString:@"&" options:NSCaseInsensitiveSearch |
165 |
range:NSMakeRange(authTokenStartLocation, [query length] - authTokenStartLocation)]; |
166 |
if (tokenEndRange.length) { |
167 |
authToken = [[query substringWithRange:NSMakeRange(authTokenStartLocation, tokenEndRange.location - authTokenStartLocation)] |
168 |
stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; |
169 |
} else { |
170 |
authToken = [[query substringFromIndex:authTokenStartLocation] |
171 |
stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; |
172 |
} |
173 |
|
174 |
NSLog(@"query authUser: '%@', authToken: '%@'", authUser, authToken); |
175 |
if ([authUser length] && [authToken length]) { |
176 |
[pithosAccount authenticateWithServerURL:nil authUser:authUser authToken:authToken]; |
177 |
[self savePithosAccounts:self]; |
178 |
if (self.pithosPreferencesController && [self.pithosPreferencesController.selectedPithosAccount isEqualTo:pithosAccount]) { |
179 |
self.pithosPreferencesController.authUser = pithosAccount.authUser; |
180 |
self.pithosPreferencesController.authToken = pithosAccount.authToken; |
181 |
} |
182 |
self.activated = YES; |
183 |
if ([pithosAccount isEqualTo:currentPithosAccount]) { |
184 |
[self showPithosBrowser:self]; |
185 |
self.pithosBrowserController.pithos = pithosAccount.pithos; |
186 |
} |
187 |
} |
188 |
// XXX else maybe show an error message? |
189 |
} |
190 |
// XXX else maybe show an error message? |
191 |
} |
192 |
|
193 |
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender { |
194 |
[self savePithosAccounts:self]; |
195 |
if ([self.pithosBrowserController operationsPending]) { |
196 |
NSAlert *alert = [[[NSAlert alloc] init] autorelease]; |
197 |
[alert setMessageText:@"Pending Operations"]; |
198 |
[alert setInformativeText:@"There are pending operations in the browser, do you want to quit and cancel them?"]; |
199 |
[alert addButtonWithTitle:@"OK"]; |
200 |
[alert addButtonWithTitle:@"Cancel"]; |
201 |
NSInteger choice = [alert runModal]; |
202 |
if (choice == NSAlertSecondButtonReturn) |
203 |
return NSTerminateCancel; |
204 |
} |
205 |
return NSTerminateNow; |
206 |
} |
207 |
|
208 |
#pragma mark - |
209 |
#pragma mark Properties |
210 |
|
211 |
- (PithosBrowserController *)pithosBrowserController { |
212 |
if (!pithosBrowserController) { |
213 |
pithosBrowserController = [[PithosBrowserController alloc] init]; |
214 |
} |
215 |
return pithosBrowserController; |
216 |
} |
217 |
|
218 |
- (PithosPreferencesController *)pithosPreferencesController { |
219 |
if (!pithosPreferencesController) { |
220 |
pithosPreferencesController = [[PithosPreferencesController alloc] init]; |
221 |
} |
222 |
return pithosPreferencesController; |
223 |
} |
224 |
|
225 |
#pragma mark - |
226 |
#pragma mark NSMenuDelegate |
227 |
|
228 |
- (void)menuNeedsUpdate:(NSMenu *)menu { |
229 |
NSMenuItem *menuItem; |
230 |
[menu removeAllItems]; |
231 |
if ([menu isEqualTo:accountsMenu]) { |
232 |
[menu setAutoenablesItems:NO]; |
233 |
for (PithosAccount *pithosAccount in pithosAccounts) { |
234 |
menuItem = [[[NSMenuItem alloc] initWithTitle:pithosAccount.name |
235 |
action:@selector(menuChangePithosAccount:) |
236 |
keyEquivalent:@""] autorelease]; |
237 |
[menuItem setRepresentedObject:pithosAccount]; |
238 |
[menuItem setEnabled:pithosAccount.active]; |
239 |
[menuItem setState:((pithosAccount.active && [currentPithosAccount isEqualTo:pithosAccount]) ? NSOnState : NSOffState)]; |
240 |
[menu addItem:menuItem]; |
241 |
} |
242 |
} else if ([menu isEqualTo:lastSyncMenu]) { |
243 |
NSString *menuItemTitle; |
244 |
[menu setAutoenablesItems:NO]; |
245 |
for (PithosAccount *pithosAccount in pithosAccounts) { |
246 |
menuItemTitle = [NSString stringWithFormat:@"%@: %@", |
247 |
pithosAccount.name, |
248 |
[[[[LastCompletedSyncTransformer alloc] init] autorelease] transformedValue:pithosAccount.syncLastCompleted]]; |
249 |
if ([pithosAccount isEqualTo:syncPithosAccount] && [pithosAccount.syncDaemon isSyncing]) |
250 |
menuItemTitle = [menuItemTitle stringByAppendingString:@" (syncing)"]; |
251 |
// menuItem = [[[NSMenuItem alloc] initWithTitle:menuItemTitle |
252 |
// action:@selector(menuChangeSyncActive:) |
253 |
// keyEquivalent:@""] autorelease]; |
254 |
// [menuItem setRepresentedObject:pithosAccount]; |
255 |
// [menuItem setEnabled:pithosAccount.active]; |
256 |
// [menuItem setState:((pithosAccount.active && pithosAccount.syncActive) ? NSOnState : NSOffState)]; |
257 |
menuItem = [[[NSMenuItem alloc] initWithTitle:menuItemTitle action:nil keyEquivalent:@""] autorelease]; |
258 |
[menuItem setEnabled:NO]; |
259 |
[menuItem setState:NO]; |
260 |
[menu addItem:menuItem]; |
261 |
} |
262 |
[menu addItem:[NSMenuItem separatorItem]]; |
263 |
[menu addItem:[[[NSMenuItem alloc] initWithTitle:@"Next Sync" |
264 |
action:@selector(sync) |
265 |
keyEquivalent:@""] autorelease]]; |
266 |
} |
267 |
} |
268 |
|
269 |
#pragma mark - |
270 |
#pragma mark Actions |
271 |
|
272 |
- (IBAction)showPithosBrowser:(id)sender { |
273 |
if (!activated) |
274 |
return; |
275 |
[self.pithosBrowserController showWindow:sender]; |
276 |
[[self.pithosBrowserController window] makeKeyAndOrderFront:sender]; |
277 |
[NSApp activateIgnoringOtherApps:YES]; |
278 |
} |
279 |
|
280 |
- (IBAction)showPithosPreferences:(id)sender { |
281 |
[self.pithosPreferencesController showWindow:sender]; |
282 |
[[self.pithosPreferencesController window] makeKeyAndOrderFront:sender]; |
283 |
[NSApp activateIgnoringOtherApps:YES]; |
284 |
} |
285 |
|
286 |
- (IBAction)showPithosAbout:(id)sender { |
287 |
[NSApp orderFrontStandardAboutPanel:sender]; |
288 |
[NSApp activateIgnoringOtherApps:YES]; |
289 |
} |
290 |
|
291 |
- (void)sync { |
292 |
if (!activated || ![pithosAccounts count]) |
293 |
return; |
294 |
NSUInteger syncIndex; |
295 |
BOOL syncPithosAccountFound = [pithosAccounts containsObject:syncPithosAccount]; |
296 |
if (syncPithosAccountFound) |
297 |
syncIndex = [pithosAccounts indexOfObject:syncPithosAccount]; |
298 |
|
299 |
PithosAccount *singleSyncPithosAccount = nil; |
300 |
for (PithosAccount *pithosAccount in pithosAccounts) { |
301 |
if (!singleSyncPithosAccount && pithosAccount.active && pithosAccount.syncActive && pithosAccount.syncDaemon) { |
302 |
singleSyncPithosAccount = pithosAccount; |
303 |
} else if (singleSyncPithosAccount && pithosAccount.active && pithosAccount.syncActive && pithosAccount.syncDaemon) { |
304 |
singleSyncPithosAccount = nil; |
305 |
break; |
306 |
} |
307 |
} |
308 |
|
309 |
if (syncPithosAccount && syncPithosAccount.active && syncPithosAccount.syncActive && syncPithosAccount.syncDaemon) { |
310 |
// An active syncDaemon was previously syncing |
311 |
if (singleSyncPithosAccount && [singleSyncPithosAccount isEqualTo:syncPithosAccount]) { |
312 |
// It's the only one, sync again |
313 |
[syncPithosAccount.syncDaemon startDaemon]; |
314 |
[syncPithosAccount.syncDaemon sync]; |
315 |
return; |
316 |
} else if ([syncPithosAccount.syncDaemon isSyncing]) { |
317 |
// It's still syncing, mark it as late and return |
318 |
[syncPithosAccount.syncDaemon syncLate]; |
319 |
return; |
320 |
} |
321 |
} |
322 |
PithosAccount *newSyncPithosAccount = nil; |
323 |
if (syncPithosAccountFound) { |
324 |
for (PithosAccount *pithosAccount in [pithosAccounts objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(syncIndex + 1, [pithosAccounts count] - syncIndex - 1)]]) { |
325 |
if (pithosAccount.active && pithosAccount.syncActive && pithosAccount.syncDaemon) { |
326 |
newSyncPithosAccount = pithosAccount; |
327 |
break; |
328 |
} |
329 |
} |
330 |
if (!newSyncPithosAccount) { |
331 |
for (PithosAccount *pithosAccount in [pithosAccounts objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, syncIndex)]]) { |
332 |
if (pithosAccount.active && pithosAccount.syncActive && pithosAccount.syncDaemon) { |
333 |
newSyncPithosAccount = pithosAccount; |
334 |
break; |
335 |
} |
336 |
} |
337 |
} |
338 |
} else { |
339 |
for (PithosAccount *pithosAccount in pithosAccounts) { |
340 |
if (pithosAccount.active && pithosAccount.syncActive && pithosAccount.syncDaemon) { |
341 |
newSyncPithosAccount = pithosAccount; |
342 |
break; |
343 |
} |
344 |
} |
345 |
} |
346 |
if (newSyncPithosAccount) { |
347 |
// A different syncDaemon is found, sync it |
348 |
self.syncPithosAccount = newSyncPithosAccount; |
349 |
[syncPithosAccount.syncDaemon startDaemon]; |
350 |
[syncPithosAccount.syncDaemon sync]; |
351 |
} else if (syncPithosAccountFound && syncPithosAccount && syncPithosAccount.active && syncPithosAccount.syncActive && syncPithosAccount.syncDaemon) { |
352 |
[syncPithosAccount.syncDaemon startDaemon]; |
353 |
[syncPithosAccount.syncDaemon sync]; |
354 |
} else { |
355 |
self.syncPithosAccount = nil; |
356 |
} |
357 |
} |
358 |
|
359 |
- (void)savePithosAccounts:(id)sender { |
360 |
[userDefaults setObject:[NSKeyedArchiver archivedDataWithRootObject:pithosAccounts] forKey:@"pithosAccounts"]; |
361 |
[userDefaults synchronize]; |
362 |
} |
363 |
|
364 |
- (void)removedPithosAccount:(PithosAccount *)removedPithosAccount { |
365 |
if ([self.currentPithosAccount isEqualTo:removedPithosAccount]) { |
366 |
for (PithosAccount *pithosAccount in pithosAccounts) { |
367 |
if (pithosAccount.active) { |
368 |
self.currentPithosAccount = pithosAccount; |
369 |
self.pithosBrowserController.pithos = currentPithosAccount.pithos; |
370 |
break; |
371 |
} |
372 |
} |
373 |
if ([self.currentPithosAccount isEqualTo:removedPithosAccount]) { |
374 |
self.activated = NO; |
375 |
[self.pithosBrowserController.window close]; |
376 |
[self.pithosBrowserController resetBrowser]; |
377 |
self.currentPithosAccount = [pithosAccounts objectAtIndex:0]; |
378 |
} |
379 |
} |
380 |
if ([self.syncPithosAccount isEqualTo:removedPithosAccount]) |
381 |
self.syncPithosAccount = nil; |
382 |
} |
383 |
|
384 |
#pragma mark - |
385 |
#pragma mark Menu Actions |
386 |
|
387 |
- (void)menuChangePithosAccount:(NSMenuItem *)sender { |
388 |
PithosAccount *pithosAccount = (PithosAccount *)[sender representedObject]; |
389 |
if (!pithosAccount.active) |
390 |
return; |
391 |
if (![currentPithosAccount isEqualTo:pithosAccount] && [pithosAccounts containsObject:pithosAccount]) { |
392 |
if ([self.pithosBrowserController operationsPending]) { |
393 |
NSAlert *alert = [[[NSAlert alloc] init] autorelease]; |
394 |
[alert setMessageText:@"Pending Operations"]; |
395 |
[alert setInformativeText:@"There are pending operations in the browser, do you want to change accounts and cancel them?"]; |
396 |
[alert addButtonWithTitle:@"OK"]; |
397 |
[alert addButtonWithTitle:@"Cancel"]; |
398 |
NSInteger choice = [alert runModal]; |
399 |
if (choice == NSAlertSecondButtonReturn) |
400 |
return; |
401 |
} |
402 |
self.currentPithosAccount = pithosAccount; |
403 |
[self showPithosBrowser:self]; |
404 |
self.pithosBrowserController.pithos = currentPithosAccount.pithos; |
405 |
} |
406 |
} |
407 |
|
408 |
//- (void)menuChangeSyncActive:(NSMenuItem *)sender { |
409 |
// PithosAccount *pithosAccount = (PithosAccount *)[sender representedObject]; |
410 |
// if (!pithosAccount.active) |
411 |
// return; |
412 |
// pithosAccount.syncActive = !pithosAccount.syncActive; |
413 |
// if (self.pithosPreferencesController && [self.pithosPreferencesController.selectedPithosAccount isEqualTo:pithosAccount]) |
414 |
// self.pithosPreferencesController.syncActive = pithosAccount.syncActive; |
415 |
// [self savePithosAccounts:self]; |
416 |
//} |
417 |
|
418 |
@end |