Merge branch 'master' of https://code.grnet.gr/git/pithos-ios
[pithos-ios] / Classes / AccountSettingsViewController.m
index 844d813..848f626 100755 (executable)
 
 @implementation AccountSettingsViewController
 
-@synthesize account, username, authToken, activityIndicatorView;
+@synthesize account, username, authToken;
 
 #pragma mark - View lifecycle
 
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
-    return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == UIInterfaceOrientationPortrait);
+    return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == UIInterfaceOrientationPortrait);
 }
 
 - (void)viewDidLoad {
     [super viewDidLoad];
-    self.navigationItem.title = @"API Account Info";
+    self.navigationItem.title = @"Authentication";
     userDetailsSection = 0;
     getTokenSection = 1;
     [self addSaveButton];
@@ -43,7 +43,6 @@
 - (void)dealloc {
     [username release];
     [authToken release];
-    [activityIndicatorView release];
     [account release];
     [super dealloc];
 }
@@ -51,7 +50,8 @@
 #pragma mark - Internal
 
 - (void) updateSaveButtonForUsername:(NSString *)checkUsername andAuthToken:(NSString *)checkAuthToken {
-    self.navigationItem.rightBarButtonItem.enabled = (checkUsername.length && checkAuthToken.length &&
+    self.navigationItem.rightBarButtonItem.enabled = (checkUsername && checkUsername.length &&
+                                                      checkAuthToken &&checkAuthToken.length &&
                                                       (![checkUsername isEqualToString:account.username] ||
                                                        ![checkAuthToken isEqualToString:account.authToken]));
 }
 - (void)setUsername:(NSString *)aUsername {
     [username release];
     username = [aUsername retain];
-    if (username)
-        [self updateSaveButtonForUsername:username andAuthToken:authTokenTextField.text];
+    [self updateSaveButtonForUsername:username andAuthToken:authTokenTextField.text];
 }
 
 - (void)setAuthToken:(NSString *)anAuthToken {
     [authToken release];
     authToken = [anAuthToken retain];
-    if (authToken)
-        [self updateSaveButtonForUsername:usernameTextField.text andAuthToken:authToken];
+    [self updateSaveButtonForUsername:usernameTextField.text andAuthToken:authToken];
+}
+
+#pragma mark - Actions
+
+- (void)setUsername:(NSString *)aUsername andAuthToken:(NSString *)anAuthToken {
+    if (aUsername) {
+        [username release];
+        username = [aUsername retain];
+    }
+    if (anAuthToken) {
+        [authToken release];
+        authToken = [anAuthToken retain];
+    }
+    [self updateSaveButtonForUsername:username andAuthToken:authToken];
 }
 
 #pragma mark - UITableViewDataSource
             cell.selectionStyle = UITableViewCellSelectionStyleNone;
         }
         
-        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
+        if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
             cell.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.8];
         }
         
     [self authenticate];
 }
 
-#pragma mark - HTTP Response Handlers
-
-- (void)authenticationSucceded:(OpenStackRequest *)request {
-    [self.activityIndicatorView removeFromSuperview];
-    if ([request isSuccess]) {
-        account.username = request.account.username;
-        account.authToken = request.account.authToken;
-        NSString *storageURLString = [[request responseHeaders] objectForKey:@"X-Storage-Url"];
-        if (storageURLString) {
-            account.filesURL = [NSURL URLWithString:storageURLString];
-        } else {
-            account.filesURL = [[account.hostURL URLByAppendingPathComponent:@"v1"] URLByAppendingPathComponent:account.username];
-        }
-        [account persist];
-    } else {
-        self.navigationItem.rightBarButtonItem.enabled = YES;
-        [self alert:@"Authentication Failure" message:@"Please check your Username and Token."];
-    }
-}
-
-- (void)authenticationFailed:(OpenStackRequest *)request {
-    [self.activityIndicatorView removeFromSuperview];
-    self.navigationItem.rightBarButtonItem.enabled = YES;
-    if ([request responseStatusCode] == 401) {
-        [self alert:@"Authentication Failure" message:@"Please check your Username and Token."];
-    } else {
-        [self failOnBadConnection];
-    }
-}
+#pragma mark - Authentication
 
 - (void)authenticate {
     if (!usernameTextField.text || [usernameTextField.text isEqualToString:@""]) {
         temporaryAccount.username = usernameTextField.text;
         temporaryAccount.authToken = authTokenTextField.text;
         
-        self.activityIndicatorView = [[[ActivityIndicatorView alloc] initWithFrame:[ActivityIndicatorView frameForText:@"Authenticating..."] text:@"Authenticating..."] autorelease];
-        [self.activityIndicatorView addToView:self.view];
-        
+        __block ActivityIndicatorView *activityIndicatorView = [ActivityIndicatorView activityIndicatorViewWithText:@"Authenticating..."
+                                                                                                       andAddToView:self.view];
         OpenStackRequest *request = [OpenStackRequest authenticationRequest:temporaryAccount];
-        request.delegate = self;
-        request.didFinishSelector = @selector(authenticationSucceded:);
-        request.didFailSelector = @selector(authenticationFailed:);
+        request.completionBlock = ^{
+            [activityIndicatorView removeFromSuperview];
+            if ([request isSuccess]) {
+                account.username = request.account.username;
+                account.authToken = request.account.authToken;
+                NSString *storageURLString = [[request responseHeaders] objectForKey:@"X-Storage-Url"];
+                if (storageURLString) {
+                    account.filesURL = [NSURL URLWithString:storageURLString];
+                } else {
+                    account.filesURL = [[account.hostURL URLByAppendingPathComponent:@"v1"] URLByAppendingPathComponent:account.username];
+                }
+                [account persist];
+            } else {
+                self.navigationItem.rightBarButtonItem.enabled = YES;
+                [self alert:@"Authentication Failure" message:@"Please check your Username and Token."];
+            }
+        };
+        request.failedBlock = ^{
+            [activityIndicatorView removeFromSuperview];
+            self.navigationItem.rightBarButtonItem.enabled = YES;
+            if ([request responseStatusCode] == 401) {
+                [self alert:@"Authentication Failure" message:@"Please check your Username and Token."];
+            } else {
+                [self failOnBadConnection];
+            }
+        };
         [request startAsynchronous];
     }
 }