5 // Copyright 2012 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 "PithosAccount.h"
39 #import "PithosSyncDaemon.h"
41 #import "ASIPithosRequest.h"
42 #import "PithosAccountNode.h"
43 #import "PithosSharingAccountsNode.h"
44 #import "PithosUtilities.h"
45 #import "pithos_macosAppDelegate.h"
47 @interface PithosAccount (Internal)
48 - (BOOL)urlIsValid:(NSString *)urlString;
51 @implementation PithosAccount
52 @synthesize uniqueName, active, name, clientVersion;
53 @synthesize syncActive, syncDirectoryPath, syncAccountsDictionary, syncSkipHidden, syncLastCompleted, syncDaemon;
54 @synthesize serverURL, versionResource, loginResource, publicResource, userCatalogResource;
55 @synthesize authUser, authToken, storageURLPrefix, authURL, loginURLPrefix, publicURLPrefix, userCatalogURL, userCatalog;
56 @synthesize pithos, accountNode, sharingAccountsNode;
59 #pragma mark Object Lifecycle
62 PithosAccount *pithosAccount = [[self alloc] init];
63 pithosAccount.uniqueName = [NSString stringWithFormat:@"pithosAccount-%f", [NSDate timeIntervalSinceReferenceDate]];
64 pithosAccount.clientVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
65 pithosAccount.syncSkipHidden = YES;
70 - (NSString *)description {
71 return [NSString stringWithFormat:@"uniqueName: %@, active: %d, name: %@, clientVersion: %@, syncActive: %d, syncDirectoryPath: %@, syncAccountsDictionary: %@, syncSkipHidden: %d, syncLastCompleted: %@, serverURL: %@, versionResource: %@, loginResource: %@, publicResource: %@, userCatalogResource: %@, authUser: %@, authToken: %@, storageURLPrefix: %@, authURL: %@, loginURLPrefix: %@, publicURLPrefix: %@, userCatalogResource: %@",
72 uniqueName, active, name, clientVersion, syncActive, syncDirectoryPath, syncAccountsDictionary, syncSkipHidden, syncLastCompleted, serverURL, versionResource, loginResource, publicResource, userCatalogResource, authUser, authToken, storageURLPrefix, authURL, loginURLPrefix, publicURLPrefix, userCatalogResource];
78 - (BOOL)urlIsValid:(NSString *)urlString {
80 NSURL *url = [NSURL URLWithString:urlString];
81 if (url && url.scheme && url.host)
88 #pragma mark Properties
92 NSDictionary *pithosAccountsDictionary = [(pithos_macosAppDelegate *)[[NSApplication sharedApplication] delegate] pithosAccountsDictionary];
93 NSString *namePrefix = @"okeanos";
94 NSUInteger nameSuffix = 1;
96 NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
97 NSFileManager *fileManager = [NSFileManager defaultManager];
98 while ([pithosAccountsDictionary objectForKey:name] ||
99 [fileManager fileExistsAtPath:[documentsDirectoryPath stringByAppendingPathComponent:name]]) {
100 name = [NSString stringWithFormat:@"%@%ld", namePrefix, ++nameSuffix];
106 - (void)setName:(NSString *)aName {
107 NSMutableDictionary *pithosAccountsDictionary = [(pithos_macosAppDelegate *)[[NSApplication sharedApplication] delegate] pithosAccountsDictionary];
108 if (![self.name isEqualToString:aName] && [aName length] && ![pithosAccountsDictionary objectForKey:aName]) {
109 [pithosAccountsDictionary setObject:self forKey:aName];
110 [pithosAccountsDictionary removeObjectForKey:name];
122 - (void)setSyncActive:(BOOL)aSyncActive {
123 syncActive = aSyncActive;
124 if (syncDaemon && !self.syncActive)
125 [syncDaemon resetDaemon];
128 - (NSString *)syncDirectoryPath {
129 if (![syncDirectoryPath length]) {
130 syncDirectoryPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
131 stringByAppendingPathComponent:self.name];
133 return syncDirectoryPath;
136 - (void)setSyncDirectoryPath:(NSString *)aSyncDirectoryPath {
137 if (![self.syncDirectoryPath isEqualToString:aSyncDirectoryPath] && [aSyncDirectoryPath length]) {
139 if (![[NSFileManager defaultManager] fileExistsAtPath:aSyncDirectoryPath isDirectory:&isDirectory] || isDirectory) {
140 syncDirectoryPath = aSyncDirectoryPath;
145 @synchronized(self) {
146 resetSyncDaemonLocalState = YES;
147 syncLastCompleted = nil;
152 - (NSMutableDictionary *)syncAccountsDictionary {
153 if (!syncAccountsDictionary) {
154 syncAccountsDictionary = [NSMutableDictionary dictionaryWithObject:[NSMutableDictionary dictionaryWithObject:[NSMutableSet set]
158 return syncAccountsDictionary;
161 - (void)setSyncAccountsDictionary:(NSMutableDictionary *)aSyncAccountsDictionary {
162 if (aSyncAccountsDictionary && ![self.syncAccountsDictionary isEqualToDictionary:aSyncAccountsDictionary]) {
163 syncAccountsDictionary = [[NSMutableDictionary alloc] initWithCapacity:[aSyncAccountsDictionary count]];
164 for (NSString *accountName in aSyncAccountsDictionary) {
165 NSDictionary *aSyncContainersDictionary = [aSyncAccountsDictionary objectForKey:accountName];
166 NSMutableDictionary *syncContainersDictionary = [NSMutableDictionary dictionary];
167 for (NSString *containerName in aSyncContainersDictionary) {
168 if (![accountName isEqualToString:@""] || ![[containerName lowercaseString] isEqualToString:@"shared with me"])
169 [syncContainersDictionary setObject:[NSMutableSet setWithSet:[aSyncContainersDictionary objectForKey:containerName]]
170 forKey:containerName];
172 if ([syncContainersDictionary count])
173 [syncAccountsDictionary setObject:syncContainersDictionary forKey:accountName];
176 @synchronized(self) {
177 resetSyncDaemonLocalState = YES;
178 syncLastCompleted = nil;
183 - (NSDate *)syncLastCompleted {
184 if (self.syncDaemon.lastCompletedSync && ![self.syncDaemon.lastCompletedSync isEqualToDate:syncLastCompleted]) {
185 syncLastCompleted = [self.syncDaemon.lastCompletedSync copy];
187 return syncLastCompleted;
190 - (PithosSyncDaemon *)syncDaemon {
191 @synchronized(self) {
192 if (self.syncActive && !syncDaemon)
193 syncDaemon = [[PithosSyncDaemon alloc] initWithDirectoryPath:self.syncDirectoryPath
195 accountsDictionary:self.syncAccountsDictionary
196 skipHidden:self.syncSkipHidden
197 resetLocalState:resetSyncDaemonLocalState];
198 resetSyncDaemonLocalState = NO;
203 - (NSString *)serverURL {
204 if (![self urlIsValid:serverURL]) {
205 serverURL = @"https://pithos.okeanos.grnet.gr";
210 - (void)setServerURL:(NSString *)aServerURL {
211 if (![self.serverURL isEqualToString:aServerURL] && [self urlIsValid:aServerURL]) {
212 serverURL = aServerURL;
213 storageURLPrefix = nil;
215 publicURLPrefix = nil;
216 loginURLPrefix = nil;
218 @synchronized(self) {
220 resetSyncDaemonLocalState = YES;
221 syncLastCompleted = nil;
226 - (NSString *)versionResource {
227 if (!versionResource) {
228 versionResource = @"v1";
230 return versionResource;
233 - (NSString *)loginResource {
234 if (!loginResource) {
235 loginResource = @"login";
237 return loginResource;
240 - (NSString *)userCatalogResource {
241 if (!userCatalogResource) {
242 userCatalogResource = @"user_catalogs";
244 return userCatalogResource;
247 - (void)setAuthUser:(NSString *)anAuthUser {
248 if ([anAuthUser length] && ![anAuthUser isEqualToString:authUser]) {
249 authUser = anAuthUser;
251 @synchronized(self) {
253 resetSyncDaemonLocalState = YES;
254 syncLastCompleted = nil;
260 - (void)setAuthToken:(NSString *)anAuthToken {
261 if ([anAuthToken length] && ![anAuthToken isEqualToString:authToken]) {
262 authToken = anAuthToken;
264 @synchronized(self) {
270 - (NSString *)storageURLPrefix {
271 if (![self urlIsValid:storageURLPrefix]) {
272 storageURLPrefix = [self.serverURL stringByAppendingFormat:@"/%@", self.versionResource];
274 return storageURLPrefix;
277 - (void)setStorageURLPrefix:(NSString *)aStorageURLPrefix {
278 if (![self.storageURLPrefix isEqualToString:aStorageURLPrefix] && [self urlIsValid:aStorageURLPrefix]) {
279 storageURLPrefix = aStorageURLPrefix;
283 - (NSString *)authURL {
284 if (![self urlIsValid:authURL]) {
285 authURL = [self.serverURL stringByAppendingFormat:@"/%@", self.versionResource];
290 - (void)setAuthURL:(NSString *)anAuthURL {
291 if (![self.authURL isEqualToString:anAuthURL] && [self urlIsValid:anAuthURL]) {
296 - (NSString *)publicURLPrefix {
297 if (![self urlIsValid:publicURLPrefix]) {
299 publicURLPrefix = [self.serverURL stringByAppendingFormat:@"/%@", publicResource];
301 publicURLPrefix = [self.serverURL copy];
303 return publicURLPrefix;
306 - (void)setPublicURLPrefix:(NSString *)aPublicURLPrefix {
307 if (![self.publicURLPrefix isEqualToString:aPublicURLPrefix] && [self urlIsValid:aPublicURLPrefix]) {
308 publicURLPrefix = aPublicURLPrefix;
312 - (NSString *)loginURLPrefix {
313 if (![self urlIsValid:loginURLPrefix]) {
314 loginURLPrefix = [self.serverURL stringByAppendingFormat:@"/%@", self.loginResource];
316 return loginURLPrefix;
319 - (void)setLoginURLPrefix:(NSString *)aLoginURLPrefix {
320 if (![self.loginURLPrefix isEqualToString:aLoginURLPrefix] && [self urlIsValid:aLoginURLPrefix]) {
321 loginURLPrefix = aLoginURLPrefix;
325 - (NSString *)userCatalogURL {
326 if (![self urlIsValid:userCatalogURL]) {
327 userCatalogURL = [self.serverURL stringByAppendingFormat:@"/%@", self.userCatalogResource];
329 return userCatalogURL;
332 - (void)setUserCatalogURL:(NSString *)aUserCatalogURL {
333 if (![self.userCatalogURL isEqualToString:aUserCatalogURL] && [self urlIsValid:aUserCatalogURL]) {
334 userCatalogURL = aUserCatalogURL;
338 - (NSMutableDictionary *)userCatalog {
340 userCatalog = [NSMutableDictionary dictionary];
345 - (ASIPithos *)pithos {
346 @synchronized(self) {
347 if (!pithos || updatePithos) {
348 pithos = [ASIPithos pithos];
349 pithos.authUser = authUser;
350 pithos.authToken = authToken;
351 pithos.storageURLPrefix = self.storageURLPrefix;
352 pithos.authURL = self.authURL;
353 pithos.publicURLPrefix = self.publicURLPrefix;
354 pithos.userCatalogURL = self.userCatalogURL;
361 - (PithosAccountNode *)accountNode {
363 accountNode = [[PithosAccountNode alloc] initWithPithos:self.pithos];
364 accountNode.childrenUpdatedNotificationName = nil;
365 accountNode.inheritChildrenUpdatedNotificationName = YES;
366 accountNode.pithosAccountManager = self;
371 - (PithosSharingAccountsNode *)sharingAccountsNode {
372 if (!sharingAccountsNode) {
373 sharingAccountsNode = [[PithosSharingAccountsNode alloc] initWithPithos:self.pithos];
374 sharingAccountsNode.childrenUpdatedNotificationName = nil;
375 sharingAccountsNode.inheritChildrenUpdatedNotificationName = YES;
376 sharingAccountsNode.pithosAccountManager = self;
378 return sharingAccountsNode;
384 - (void)authenticateWithServerURL:(NSString *)aServerURL authUser:(NSString *)anAuthUser authToken:(NSString *)anAuthToken {
385 self.serverURL = aServerURL;
386 self.authUser = anAuthUser;
387 self.authToken = anAuthToken;
388 DLog(@"Account: %@\nauthentication", self);
389 if (![authUser length] || ![authToken length]) {
391 self.syncActive = NO;
392 // XXX Show preferences with self as the selected account?
396 self.syncDaemon.pithos = self.pithos;
398 [self.syncDaemon startDaemon];
401 self.accountNode.pithos = self.pithos;
402 if (sharingAccountsNode)
403 self.sharingAccountsNode.pithos = self.pithos;
405 if (self.accountNode.children) {
407 [self.accountNode refreshInfo];
409 if (sharingAccountsNode && self.sharingAccountsNode.children) {
412 [self updateUserCatalogForForDisplaynames:nil UUIDs:[NSArray arrayWithObject:authUser]];
416 - (void)loginWithServerURL:(NSString *)aServerURL {
417 self.serverURL = aServerURL;
418 NSProcessInfo *processInfo = [NSProcessInfo processInfo];
419 NSString *loginURL = [NSString stringWithFormat:@"%@?next=pithos://%d/%@&force=",
420 self.loginURLPrefix, [processInfo processIdentifier], [ASIPithosRequest encodeToPercentEscape:self.name]];
421 DLog(@"Account: %@\nloginURL: %@", self, loginURL);
422 [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:loginURL]];
425 - (void)updateSyncWithSyncActive:(BOOL)aSyncActive
426 syncDirectoryPath:(NSString *)aSyncDirectoryPath
427 syncAccountsDictionary:(NSMutableDictionary *)aSyncAccountsDictionary
428 syncSkipHidden:(BOOL)aSyncSkipHidden {
429 self.syncAccountsDictionary = aSyncAccountsDictionary;
430 self.syncDirectoryPath = aSyncDirectoryPath;
431 self.syncSkipHidden = aSyncSkipHidden;
432 self.syncActive = aSyncActive;
434 self.syncDaemon.accountsDictionary = self.syncAccountsDictionary;
435 self.syncDaemon.directoryPath = self.syncDirectoryPath;
436 self.syncDaemon.skipHidden = self.syncSkipHidden;
438 [self.syncDaemon startDaemon];
442 - (ASIPithosRequest *)updateUserCatalogForForDisplaynames:(NSArray *)displaynames UUIDs:(NSArray *)UUIDs {
443 ASIPithosRequest *userCatalogRequest = [ASIPithosRequest userCatalogRequestWithPithos:self.pithos
444 displaynames:displaynames
446 [PithosUtilities startAndWaitForRequest:userCatalogRequest];
447 if (userCatalogRequest.error || ((userCatalogRequest.responseStatusCode != 200) && (userCatalogRequest.responseStatusCode != 404))) {
448 // Don't show alert on 404, since it can be a pre-UUID server.
449 [PithosUtilities httpRequestErrorAlertWithRequest:userCatalogRequest];
450 } else if (userCatalogRequest.responseStatusCode == 200) {
451 NSDictionary *catalogs = [userCatalogRequest catalogs];
452 NSDictionary *displaynameCatalog = [catalogs objectForKey:@"displayname_catalog"];
453 for (NSString *displayname in displaynameCatalog) {
454 [self.userCatalog setObject:displayname forKey:[displaynameCatalog objectForKey:displayname]];
457 NSDictionary *UUIDCatalog = [catalogs objectForKey:@"uuid_catalog"];
458 for (NSString *UUID in UUIDs) {
459 NSString *displayname = [UUIDCatalog objectForKey:UUID];
461 [self.userCatalog setObject:displayname forKey:UUID];
463 [self.userCatalog removeObjectForKey:UUID];
468 return userCatalogRequest;
471 - (NSString *)displaynameForUUID:(NSString *)UUID safe:(BOOL)safe {
472 NSString *displayName = [userCatalog objectForKey:UUID];
473 if (safe && !displayName) {
480 - (NSString *)displaynameForUUID:(NSString *)UUID {
481 return [self displaynameForUUID:UUID safe:NO];
485 #pragma mark NSCoding
487 - (id)initWithCoder:(NSCoder *)decoder {
488 if ((self = [super init])) {
489 self.uniqueName = [decoder decodeObjectForKey:@"uniqueName"];
490 self.active = [decoder decodeBoolForKey:@"active"];
491 name = [decoder decodeObjectForKey:@"name"];
492 self.clientVersion = [decoder decodeObjectForKey:@"clientVersion"];
494 self.syncActive = [decoder decodeBoolForKey:@"syncActive"];
495 self.syncDirectoryPath = [decoder decodeObjectForKey:@"syncDirectoryPath"];
496 self.syncAccountsDictionary = [decoder decodeObjectForKey:@"syncAccountsDictionary"];
497 self.syncSkipHidden = [decoder decodeBoolForKey:@"syncSkipHidden"];
498 self.syncLastCompleted = [decoder decodeObjectForKey:@"syncLastCompleted"];
500 self.serverURL = [decoder decodeObjectForKey:@"serverURL"];
501 self.versionResource = [decoder decodeObjectForKey:@"versionResource"];
502 self.loginResource = [decoder decodeObjectForKey:@"loginResource"];
503 self.publicResource = [decoder decodeObjectForKey:@"publicResource"];
504 self.userCatalogResource = [decoder decodeObjectForKey:@"userCatalogResource"];
506 self.authUser = [decoder decodeObjectForKey:@"authUser"];
507 self.authToken = [decoder decodeObjectForKey:@"authToken"];
508 self.storageURLPrefix = [decoder decodeObjectForKey:@"storageURLPrefix"];
509 self.authURL = [decoder decodeObjectForKey:@"authURL"];
510 self.publicURLPrefix = [decoder decodeObjectForKey:@"publicURLPrefix"];
511 self.loginURLPrefix = [decoder decodeObjectForKey:@"loginURLPrefix"];
512 self.userCatalogURL = [decoder decodeObjectForKey:@"userCatalogURL"];
513 self.userCatalog = [decoder decodeObjectForKey:@"userCatalog"];
515 if (![authUser length] || ![authToken length] || ![self.storageURLPrefix length])
518 NSString *currentVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
519 if (!clientVersion || ![clientVersion isEqualToString:currentVersion]) {
520 resetSyncDaemonLocalState = YES;
521 self.clientVersion = currentVersion;
523 resetSyncDaemonLocalState = NO;
529 - (void)encodeWithCoder:(NSCoder *)encoder {
530 [encoder encodeObject:uniqueName forKey:@"uniqueName"];
531 [encoder encodeBool:active forKey:@"active"];
532 [encoder encodeObject:name forKey:@"name"];
533 [encoder encodeObject:clientVersion forKey:@"clientVersion"];
535 [encoder encodeBool:syncActive forKey:@"syncActive"];
536 [encoder encodeObject:syncDirectoryPath forKey:@"syncDirectoryPath"];
537 [encoder encodeObject:syncAccountsDictionary forKey:@"syncAccountsDictionary"];
538 [encoder encodeBool:syncSkipHidden forKey:@"syncSkipHidden"];
539 [encoder encodeObject:self.syncLastCompleted forKey:@"syncLastCompleted"];
541 [encoder encodeObject:serverURL forKey:@"serverURL"];
542 [encoder encodeObject:versionResource forKey:@"versionResource"];
543 [encoder encodeObject:publicResource forKey:@"publicResource"];
544 [encoder encodeObject:loginResource forKey:@"loginResource"];
545 [encoder encodeObject:loginResource forKey:@"userCatalogResource"];
547 [encoder encodeObject:authUser forKey:@"authUser"];
548 [encoder encodeObject:authToken forKey:@"authToken"];
549 [encoder encodeObject:storageURLPrefix forKey:@"storageURLPrefix"];
550 [encoder encodeObject:authURL forKey:@"authURL"];
551 [encoder encodeObject:publicURLPrefix forKey:@"publicURLPrefix"];
552 [encoder encodeObject:loginURLPrefix forKey:@"loginURLPrefix"];
553 [encoder encodeObject:userCatalogURL forKey:@"userCatalogURL"];
554 [encoder encodeObject:userCatalog forKey:@"userCatalog"];