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 "ASIPithosRequest.h"
42 #import "ASIDownloadCache.h"
44 @implementation pithos_macosAppDelegate
45 @synthesize storageURLPrefix, publicURLPrefix;
47 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
48 // XXX hardcoded for now
49 storageURLPrefix = @"https://pithos.dev.grnet.gr/v1";
50 publicURLPrefix = @"https://pithos.dev.grnet.gr";
52 [[NSAppleEventManager sharedAppleEventManager] setEventHandler:self
53 andSelector:@selector(handleAppleEvent:withReplyEvent:)
54 forEventClass:kInternetEventClass
55 andEventID:kAEGetURL];
56 [self showPithosBrowser:self];
58 [self authenticateWithAuthUser:[[userDefaultsController values] valueForKey:@"authUser"]
59 authToken:[[userDefaultsController values] valueForKey:@"authToken"]];
62 // Based on: http://cocoatutorial.grapewave.com/2010/01/creating-a-status-bar-application/
63 // and: http://www.cocoadev.com/index.pl?ThumbnailImages
64 - (void)awakeFromNib {
65 NSImage *sourceImage = [NSImage imageNamed:NSImageNameFolder];
67 NSImage *smallImage = [[[NSImage alloc] initWithSize:NSMakeSize(18, 18)] autorelease];
68 [smallImage lockFocus];
69 [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
70 [sourceImage setSize:NSMakeSize(18, 18)];
71 [sourceImage compositeToPoint:NSZeroPoint operation:NSCompositeCopy];
72 [smallImage unlockFocus];
74 statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
75 [statusItem setMenu:statusMenu];
76 [statusItem setImage:sourceImage];
77 [statusItem setHighlightMode:YES];
80 - (void)handleAppleEvent:(NSAppleEventDescriptor *)event withReplyEvent: (NSAppleEventDescriptor *)replyEvent {
81 NSURL *url = [NSURL URLWithString:[[event paramDescriptorForKeyword:keyDirectObject] stringValue]];
82 NSString *host = [url host];
83 NSString *query = [url query];
84 NSLog(@"host : '%@', query: '%@'", host, query);
85 NSProcessInfo *processInfo = [NSProcessInfo processInfo];
86 if ([host isEqualToString:[NSString stringWithFormat:@"%@_%d", [processInfo processName], [processInfo processIdentifier]]] && query) {
89 NSRange userRange = [query rangeOfString:@"user=" options:NSCaseInsensitiveSearch];
90 if (userRange.length == 0)
91 // XXX maybe show an error message?
93 NSUInteger authUserStartLocation = userRange.location + userRange.length;
94 NSRange userEndRange = [query rangeOfString:@"&" options:NSCaseInsensitiveSearch
95 range:NSMakeRange(authUserStartLocation, [query length] - authUserStartLocation)];
96 if (userEndRange.length) {
97 authUser = [[query substringWithRange:NSMakeRange(authUserStartLocation, userEndRange.location - authUserStartLocation)]
98 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
100 authUser = [[query substringFromIndex:authUserStartLocation]
101 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
105 NSRange tokenRange = [query rangeOfString:@"token=" options:NSCaseInsensitiveSearch];
106 if (tokenRange.length == 0)
107 // XXX maybe show an error message?
109 NSUInteger authTokenStartLocation = tokenRange.location + tokenRange.length;
110 NSRange tokenEndRange = [query rangeOfString:@"&" options:NSCaseInsensitiveSearch
111 range:NSMakeRange(authTokenStartLocation, [query length] - authTokenStartLocation)];
112 if (tokenEndRange.length) {
113 authToken = [[query substringWithRange:NSMakeRange(authTokenStartLocation, tokenEndRange.location - authTokenStartLocation)]
114 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
116 authToken = [[query substringFromIndex:authTokenStartLocation]
117 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
120 NSLog(@"query authUser: '%@', authToken: '%@'", authUser, authToken);
121 if ([authUser length] && [authToken length]) {
122 [[userDefaultsController values] setValue:authUser forKey:@"authUser"];
123 [[userDefaultsController values] setValue:authToken forKey:@"authToken"];
125 [self authenticateWithAuthUser:authUser authToken:authToken];
127 // XXX else maybe show an error message?
129 // XXX else maybe show an error message?
135 - (IBAction)showPithosBrowser:(id)sender {
136 [pithosBrowserController showWindow:sender];
137 [[pithosBrowserController window] makeKeyAndOrderFront:sender];
138 [NSApp activateIgnoringOtherApps:YES];
141 - (IBAction)showPithosPreferences:(id)sender {
142 [pithosPreferencesController showWindow:sender];
143 [[pithosPreferencesController window] makeKeyAndOrderFront:sender];
144 [NSApp activateIgnoringOtherApps:YES];
147 - (IBAction)aboutPithos:(id)sender {
148 [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://pithos.dev.grnet.gr/docs"]];
152 #pragma Authentication
154 - (void)authenticateWithAuthUser:(NSString *)authUser authToken:(NSString *)authToken {
155 NSLog(@"Authentication - storageURLPrefix:%@, authUser:%@, authToken:%@", storageURLPrefix, authUser, authToken);
156 if ([authUser length] && [authToken length]) {
157 [[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
158 [ASIPithosRequest setAuthURL:storageURLPrefix];
159 [ASIPithosRequest setStorageURLPrefix:storageURLPrefix];
160 [ASIPithosRequest setAuthUser:authUser];
161 [ASIPithosRequest setAuthToken:authToken];
162 [ASIPithosRequest setPublicURLPrefix:publicURLPrefix];
164 [[NSNotificationCenter defaultCenter] postNotificationName:@"PithosAuthenticationCredentialsUpdated" object:self];