Refactor use of pithos objects to accommodate
[pithos-macos] / pithos-macos / PithosAccount.m
index 35348f9..7d1b97a 100644 (file)
@@ -2,7 +2,7 @@
 //  PithosAccount.m
 //  pithos-macos
 //
-// Copyright 2012 GRNET S.A. All rights reserved.
+// Copyright 2012-2013 GRNET S.A. All rights reserved.
 //
 // Redistribution and use in source and binary forms, with or
 // without modification, are permitted provided that the following
@@ -59,11 +59,7 @@ static NSString *defaultManualURLString = @"https://pithos.okeanos.grnet.gr";
 }
 
 - (NSString *)stringByRemovingTrailingSlashes {
-    NSString *stringWithRemovedTrailingSlashes = [self copy];
-    while ([stringWithRemovedTrailingSlashes hasSuffix:@"/"]) {
-        stringWithRemovedTrailingSlashes = [stringWithRemovedTrailingSlashes substringToIndex:(stringWithRemovedTrailingSlashes.length - 1)];
-    }
-    return stringWithRemovedTrailingSlashes;
+    return [self stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]];
 }
 @end
 
@@ -312,7 +308,7 @@ static NSString *defaultManualURLString = @"https://pithos.okeanos.grnet.gr";
 
 - (PithosAccountNode *)accountNode {
     if (!accountNode) {
-        accountNode = [[PithosAccountNode alloc] initWithPithosAccountManager:self andPithos:self.pithos];
+        accountNode = [[PithosAccountNode alloc] initWithPithosAccountManager:self];
         accountNode.childrenUpdatedNotificationName = nil;
         accountNode.inheritChildrenUpdatedNotificationName = YES;
     }
@@ -321,7 +317,7 @@ static NSString *defaultManualURLString = @"https://pithos.okeanos.grnet.gr";
 
 - (PithosSharingAccountsNode *)sharingAccountsNode {
     if (!sharingAccountsNode) {
-        sharingAccountsNode = [[PithosSharingAccountsNode alloc] initWithPithosAccountManager:self andPithos:self.pithos];
+        sharingAccountsNode = [[PithosSharingAccountsNode alloc] initWithPithosAccountManager:self];
         sharingAccountsNode.childrenUpdatedNotificationName = nil;
         sharingAccountsNode.inheritChildrenUpdatedNotificationName = YES;
     }
@@ -358,7 +354,13 @@ static NSString *defaultManualURLString = @"https://pithos.okeanos.grnet.gr";
                          manual:(BOOL)aManual
                       authToken:(NSString *)anAuthToken
                       authUser:(NSString *)anAuthUser
-                ignoreSSLErrors:(BOOL)anIgnoreSSLErrors {
+                ignoreSSLErrors:(BOOL)anIgnoreSSLErrors
+                     resetNodes:(BOOL)resetNodes {
+    BOOL pithosUpdated = (![authURLString isEqualToString:[anAuthURLString stringByRemovingTrailingSlashes]] ||
+                          ![pithosObjectStoreURLString isEqualToString:[aPithosObjectStoreURLString stringByRemovingTrailingSlashes]] ||
+                          ![astakosAccountURLString isEqualToString:[anAstakosAccountURLString stringByRemovingTrailingSlashes]] ||
+                          ![authToken isEqualToString:anAuthToken] || ![authUser isEqualToString:anAuthUser] ||
+                          (ignoreSSLErrors != anIgnoreSSLErrors));
     self.authURLString = anAuthURLString;
     self.pithosObjectStoreURLString = aPithosObjectStoreURLString;
     self.astakosAccountURLString = anAstakosAccountURLString;
@@ -375,26 +377,44 @@ static NSString *defaultManualURLString = @"https://pithos.okeanos.grnet.gr";
         [self updateUserCatalogForDisplaynames:nil UUIDs:[NSArray arrayWithObject:authUser]];
 
         self.active = YES;
-        if (syncDaemon) {
-            self.syncDaemon.pithos = self.pithos;
-            if (self.syncActive)
-                [self.syncDaemon startDaemon];
-        }
-        if (accountNode)
-            self.accountNode.pithos = self.pithos;
-        if (sharingAccountsNode)
-            self.sharingAccountsNode.pithos = self.pithos;
-        if (accountNode) {
-            if (self.accountNode.children) {
+        if (pithosUpdated) {
+            if (syncDaemon) {
+                // For the sync daemon we keep using a separate pithos object.
+                self.syncDaemon.pithos = self.pithos;
+                if (self.syncActive)
+                    [self.syncDaemon startDaemon];
+            }
+            if (resetNodes) {
+                [accountNode reset];
+                [accountNode refreshInfo];
+                [sharingAccountsNode reset];
+            
+                [[NSNotificationCenter defaultCenter] postNotificationName:@"PithosAccountPithosChanged" object:self];
             }
-            [self.accountNode refreshInfo];
-        }
-        if (sharingAccountsNode && self.sharingAccountsNode.children) {
         }
     }
 }
 
-- (void)updateSyncWithSyncActive:(BOOL)aSyncActive 
+- (void)updateWithAuthURLString:(NSString *)anAuthURLString
+     pithosObjectStoreURLString:(NSString *)aPithosObjectStoreURLString
+        astakosAccountURLString:(NSString *)anAstakosAccountURLString
+       astakosWebloginURLString:(NSString *)anAstakosWebloginURLString
+                         manual:(BOOL)aManual
+                      authToken:(NSString *)anAuthToken
+                       authUser:(NSString *)anAuthUser
+                ignoreSSLErrors:(BOOL)anIgnoreSSLErrors {
+    [self updateWithAuthURLString:anAuthURLString
+       pithosObjectStoreURLString:aPithosObjectStoreURLString
+          astakosAccountURLString:anAstakosAccountURLString
+         astakosWebloginURLString:anAstakosWebloginURLString
+                           manual:aManual
+                        authToken:anAuthToken
+                         authUser:anAuthUser
+                  ignoreSSLErrors:anIgnoreSSLErrors
+                       resetNodes:YES];
+}
+
+- (void)updateSyncWithSyncActive:(BOOL)aSyncActive
                syncDirectoryPath:(NSString *)aSyncDirectoryPath 
           syncAccountsDictionary:(NSMutableDictionary *)aSyncAccountsDictionary 
                   syncSkipHidden:(BOOL)aSyncSkipHidden {
@@ -443,21 +463,32 @@ static NSString *defaultManualURLString = @"https://pithos.okeanos.grnet.gr";
 - (void)updateServicesFromServiceCatalogRequest:(ASIPithosRequest *)serviceCatalogRequest {
     if (serviceCatalogRequest.responseStatusCode == 200) {
         NSMutableDictionary *services = [self servicesFromServiceCatalogRequest:serviceCatalogRequest];
-        self.pithosObjectStoreURLString = [services objectForKey:@"PithosObjectStoreURLString"];
-        self.astakosAccountURLString = [services objectForKey:@"AstakosAccountURLString"];
-        self.astakosWebloginURLString = [services objectForKey:@"AstakosWebloginURLString"];
-        self.manual = NO;
-        
+        NSString *newAuthToken = nil;
+        NSString *newAuthUser = nil;
         if (authToken.length) {
             NSDictionary *token = [serviceCatalogRequest token];
-            self.authToken = [token objectForKey:@"id"];
-            self.authUser = [[token objectForKey:@"tenant"] objectForKey:@"id"];
+            newAuthToken = [token objectForKey:@"id"];
+            newAuthUser = [[token objectForKey:@"tenant"] objectForKey:@"id"];
         }
+        [self updateWithAuthURLString:authURLString
+           pithosObjectStoreURLString:[services objectForKey:@"PithosObjectStoreURLString"]
+              astakosAccountURLString:[services objectForKey:@"AstakosAccountURLString"]
+             astakosWebloginURLString:[services objectForKey:@"AstakosWebloginURLString"]
+                               manual:NO
+                            authToken:(newAuthToken ? newAuthToken : authToken)
+                             authUser:(newAuthUser ? newAuthUser : authUser)
+                      ignoreSSLErrors:ignoreSSLErrors
+                           resetNodes:NO];
     } else if (serviceCatalogRequest.responseStatusCode == 404) {
-        self.pithosObjectStoreURLString = [[[NSURL URLWithString:authURLString] URLByAppendingPathComponent:@"v1"] description];
-        self.astakosAccountURLString = [self.authURLString copy];
-        self.astakosWebloginURLString = [self.authURLString copy];
-        self.manual = YES;
+        [self updateWithAuthURLString:authURLString
+           pithosObjectStoreURLString:[[[NSURL URLWithString:authURLString] URLByAppendingPathComponent:@"v1"] description]
+              astakosAccountURLString:[authURLString copy]
+             astakosWebloginURLString:[authURLString copy]
+                               manual:YES
+                            authToken:authToken
+                             authUser:authUser
+                      ignoreSSLErrors:ignoreSSLErrors
+                           resetNodes:NO];
     }
 }
 
@@ -525,6 +556,8 @@ static NSString *defaultManualURLString = @"https://pithos.okeanos.grnet.gr";
         self.astakosWebloginURLString = [decoder decodeObjectForKey:@"astakosWebloginURLString"];
         self.manual = [decoder decodeBoolForKey:@"manual"];
         
+        self.ignoreSSLErrors = [decoder decodeBoolForKey:@"ignoreSSLErrors"];
+        
         // Support for older versions.
         if (!authURLString && !pithosObjectStoreURLString && !astakosAccountURLString && !astakosWebloginURLString) {
             NSString *tmpURLString = [decoder decodeObjectForKey:@"serverURL"];
@@ -579,6 +612,8 @@ static NSString *defaultManualURLString = @"https://pithos.okeanos.grnet.gr";
     [encoder encodeObject:authToken forKey:@"authToken"];
     [encoder encodeObject:authUser forKey:@"authUser"];
     [encoder encodeObject:userCatalog forKey:@"userCatalog"];
+    
+    [encoder encodeBool:ignoreSSLErrors forKey:@"ignoreSSLErrors"];
 }
 
 @end