2 // pithos_macosAppDelegate.m
5 // Copyright 2011 GRNET S.A. All rights reserved.
7 // Redistribution and use in source and binary forms, with or
8 // without modification, are permitted provided that the following
11 // 1. Redistributions of source code must retain the above
12 // copyright notice, this list of conditions and the following
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.
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.
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.
38 #import "pithos_macosAppDelegate.h"
39 #import "PithosBrowserController.h"
40 #import "PithosPreferencesController.h"
41 #import "PithosSyncDaemon.h"
42 #import "ASIPithosRequest.h"
43 #import "ASIDownloadCache.h"
45 @implementation pithos_macosAppDelegate
46 @synthesize pithosBrowserController, pithosSyncDaemon, alwaysNo;
48 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
49 userDefaults = [[NSUserDefaults standardUserDefaults] retain];
53 stringURL = [userDefaults stringForKey:@"storageURLPrefix"];
54 testURL = (stringURL) ? [NSURL URLWithString:stringURL] : nil;
55 if (!testURL || !testURL.scheme || !testURL.host)
56 [userDefaults setObject:@"https://plus.pithos.grnet.gr/v1" forKey:@"storageURLPrefix"];
58 stringURL = [userDefaults stringForKey:@"publicURLPrefix"];
59 testURL = (stringURL) ? [NSURL URLWithString:stringURL] : nil;
60 if (!testURL || !testURL.scheme || !testURL.host)
61 [userDefaults setObject:@"https://plus.pithos.grnet.gr" forKey:@"publicURLPrefix"];
63 stringURL = [userDefaults stringForKey:@"loginURLPrefix"];
64 testURL = (stringURL) ? [NSURL URLWithString:stringURL] : nil;
65 if (!testURL || !testURL.scheme || !testURL.host)
66 [userDefaults setObject:@"https://plus.pithos.grnet.gr/login" forKey:@"loginURLPrefix"];
68 stringURL = [userDefaults stringForKey:@"aboutURL"];
69 testURL = (stringURL) ? [NSURL URLWithString:stringURL] : nil;
70 if (!testURL || !testURL.scheme || !testURL.host)
71 [userDefaults setObject:@"https://pithos.dev.grnet.gr/docs" forKey:@"aboutURL"];
73 NSString *syncDirectoryPath = [userDefaults stringForKey:@"syncDirectoryPath"];
74 if (!syncDirectoryPath || ![syncDirectoryPath length]) {
75 syncDirectoryPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"Pithos"];
77 NSFileManager *fileManager = [NSFileManager defaultManager];
79 BOOL fileExists = [fileManager fileExistsAtPath:syncDirectoryPath isDirectory:&isDirectory];
81 if ((fileExists && !isDirectory) ||
82 (!fileExists && (![fileManager createDirectoryAtPath:syncDirectoryPath withIntermediateDirectories:YES attributes:nil error:&error] || error))) {
83 syncDirectoryPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"Pithos"];
86 [userDefaults setObject:syncDirectoryPath forKey:@"syncDirectoryPath"];
88 NSString *syncContainerName = [userDefaults stringForKey:@"syncContainerName"];
89 if (!syncContainerName || ![syncContainerName length] || [syncContainerName isEqualToString:@"trash"])
90 [userDefaults setObject:@"pithos" forKey:@"syncContainerName"];
92 double syncTimeInterval = [userDefaults doubleForKey:@"syncTimeInteral"];
93 if (syncTimeInterval <= 0)
94 [userDefaults setDouble:180.0 forKey:@"syncTimeInteral"];
96 [userDefaults synchronize];
98 [[NSAppleEventManager sharedAppleEventManager] setEventHandler:self
99 andSelector:@selector(handleAppleEvent:withReplyEvent:)
100 forEventClass:kInternetEventClass
101 andEventID:kAEGetURL];
102 [self showPithosBrowser:self];
107 // Based on: http://cocoatutorial.grapewave.com/2010/01/creating-a-status-bar-application/
108 // and: http://www.cocoadev.com/index.pl?ThumbnailImages
109 - (void)awakeFromNib {
110 NSImage *sourceImage = [NSImage imageNamed:@"pithos-large.png"];
112 NSImage *smallImage = [[[NSImage alloc] initWithSize:NSMakeSize(18, 18)] autorelease];
113 [smallImage lockFocus];
114 [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
115 [sourceImage setSize:NSMakeSize(18, 18)];
116 [sourceImage compositeToPoint:NSZeroPoint operation:NSCompositeCopy];
117 [smallImage unlockFocus];
119 statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
120 [statusItem setMenu:statusMenu];
121 [statusItem setImage:sourceImage];
122 [statusItem setHighlightMode:YES];
127 - (void)handleAppleEvent:(NSAppleEventDescriptor *)event withReplyEvent: (NSAppleEventDescriptor *)replyEvent {
128 NSURL *url = [NSURL URLWithString:[[event paramDescriptorForKeyword:keyDirectObject] stringValue]];
129 NSString *host = [url host];
130 NSString *query = [url query];
131 NSLog(@"host : '%@', query: '%@'", host, query);
132 NSProcessInfo *processInfo = [NSProcessInfo processInfo];
133 if ([host isEqualToString:[NSString stringWithFormat:@"%@_%d", [processInfo processName], [processInfo processIdentifier]]] && query) {
136 NSRange userRange = [query rangeOfString:@"user=" options:NSCaseInsensitiveSearch];
137 if (userRange.length == 0)
138 // XXX maybe show an error message?
140 NSUInteger authUserStartLocation = userRange.location + userRange.length;
141 NSRange userEndRange = [query rangeOfString:@"&" options:NSCaseInsensitiveSearch
142 range:NSMakeRange(authUserStartLocation, [query length] - authUserStartLocation)];
143 if (userEndRange.length) {
144 authUser = [[query substringWithRange:NSMakeRange(authUserStartLocation, userEndRange.location - authUserStartLocation)]
145 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
147 authUser = [[query substringFromIndex:authUserStartLocation]
148 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
152 NSRange tokenRange = [query rangeOfString:@"token=" options:NSCaseInsensitiveSearch];
153 if (tokenRange.length == 0)
154 // XXX maybe show an error message?
156 NSUInteger authTokenStartLocation = tokenRange.location + tokenRange.length;
157 NSRange tokenEndRange = [query rangeOfString:@"&" options:NSCaseInsensitiveSearch
158 range:NSMakeRange(authTokenStartLocation, [query length] - authTokenStartLocation)];
159 if (tokenEndRange.length) {
160 authToken = [[query substringWithRange:NSMakeRange(authTokenStartLocation, tokenEndRange.location - authTokenStartLocation)]
161 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
163 authToken = [[query substringFromIndex:authTokenStartLocation]
164 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
167 NSLog(@"query authUser: '%@', authToken: '%@'", authUser, authToken);
168 if ([authUser length] && [authToken length]) {
169 [userDefaults setObject:authUser forKey:@"authUser"];
170 [userDefaults setObject:authToken forKey:@"authToken"];
171 [userDefaults synchronize];
175 // XXX else maybe show an error message?
177 // XXX else maybe show an error message?
183 - (IBAction)showPithosBrowser:(id)sender {
184 [pithosBrowserController showWindow:sender];
185 [[pithosBrowserController window] makeKeyAndOrderFront:sender];
186 [NSApp activateIgnoringOtherApps:YES];
189 - (IBAction)showPithosPreferences:(id)sender {
190 [pithosPreferencesController showWindow:sender];
191 [[pithosPreferencesController window] makeKeyAndOrderFront:sender];
192 [NSApp activateIgnoringOtherApps:YES];
195 - (IBAction)aboutPithos:(id)sender {
196 [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[userDefaults stringForKey:@"aboutURL"]]];
199 - (IBAction)syncNow:(id)sender {
200 [pithosSyncDaemon sync];
204 #pragma Authentication
207 NSProcessInfo *processInfo = [NSProcessInfo processInfo];
208 NSString *loginURL = [NSString stringWithFormat:@"%@?next=pithos://%@_%d",
209 [userDefaults stringForKey:@"loginURLPrefix"], [processInfo processName], [processInfo processIdentifier]];
210 NSLog(@"loginURL: %@", loginURL);
211 [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:loginURL]];
214 - (void)authenticate {
215 NSString *authUser = [userDefaults stringForKey:@"authUser"];
216 NSString *authToken = [userDefaults stringForKey:@"authToken"];
217 NSString *storageURLPrefix = [userDefaults stringForKey:@"storageURLPrefix"];
218 NSString *publicURLPrefix = [userDefaults stringForKey:@"publicURLPrefix"];
219 NSString *syncDirectoryPath = [userDefaults stringForKey:@"syncDirectoryPath"];
220 NSString *syncContainerName = [userDefaults stringForKey:@"syncContainerName"];
221 double syncTimeInterval = [userDefaults doubleForKey:@"syncTimeInterval"];
222 NSLog(@"Authentication - storageURLPrefix:%@, authUser:%@, authToken:%@", storageURLPrefix, authUser, authToken);
223 if (([authUser length] == 0) || ([authToken length] == 0)) {
224 [self showPithosPreferences:self];
225 } else if ([authUser length] && [authToken length] &&
226 (![[ASIPithosRequest authUser] isEqualToString:authUser] || ![[ASIPithosRequest authToken] isEqualToString:authToken])) {
227 [userDefaults setObject:authUser forKey:@"previousAuthUser"];
228 [userDefaults setObject:authToken forKey:@"previousAuthToken"];
230 [[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
231 [[ASIPithosRequest sharedQueue] cancelAllOperations];
233 [ASIPithosRequest setAuthURL:storageURLPrefix];
234 [ASIPithosRequest setStorageURLPrefix:storageURLPrefix];
235 [ASIPithosRequest setAuthUser:authUser];
236 [ASIPithosRequest setAuthToken:authToken];
237 [ASIPithosRequest setPublicURLPrefix:publicURLPrefix];
239 self.pithosSyncDaemon = [[[PithosSyncDaemon alloc] initWithDirectoryPath:syncDirectoryPath
240 containerName:syncContainerName
241 timeInterval:syncTimeInterval
242 resetLocalState:NO] autorelease];
244 [[NSNotificationCenter defaultCenter] postNotificationName:@"PithosAuthenticationCredentialsUpdated" object:self];
245 } else if (![pithosSyncDaemon.directoryPath isEqualToString:syncDirectoryPath]) {
246 self.pithosSyncDaemon = [[[PithosSyncDaemon alloc] initWithDirectoryPath:syncDirectoryPath
247 containerName:syncContainerName
248 timeInterval:syncTimeInterval
249 resetLocalState:YES] autorelease];