Change default authURL
[pithos-ios] / Classes / AccountGroupsViewController.m
index f5d22f5..f9fd70d 100644 (file)
@@ -2,7 +2,7 @@
 //  AccountGroupsViewController.m
 //  pithos-ios
 //
-// Copyright 2011 GRNET S.A. All rights reserved.
+// Copyright 2011-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
@@ -47,7 +47,7 @@
 
 @implementation AccountGroupsViewController
 
-@synthesize account, groups;
+@synthesize account, groups, metadata;
 
 #pragma mark - View lifecycle
 
 - (void)viewWillAppear:(BOOL)animated {
     [super viewWillAppear:animated];
     self.navigationItem.title = @"Groups";
-    groups = [[NSMutableDictionary alloc] init];
-    metadata = [[NSMutableDictionary alloc] init];
+}
 
-    __block ActivityIndicatorView *activityIndicatorView = [ActivityIndicatorView activityIndicatorViewWithText:@"Loading..."
+- (void)viewDidAppear:(BOOL)animated {
+    [super viewDidAppear:animated];
+    __block ActivityIndicatorView *activityIndicatorView = [ActivityIndicatorView activityIndicatorViewWithText:@"Loading groups..."
                                                                                                    andAddToView:self.view];
     [[self.account.manager getStorageAccountInfo]
      success:^(OpenStackRequest *request) {
-         [activityIndicatorView removeFromSuperview];
-         for (NSString *key in request.responseHeaders) {
-             if ([key hasPrefix:@"X-Account-Group-"]) {
-                 NSString *groupUsers = [NSString decodeFromPercentEscape:[request.responseHeaders objectForKey:key]];
-                 NSString *groupName = [NSString decodeFromPercentEscape:[key substringFromIndex:16]];
-                 [groups setObject:groupUsers forKey:groupName];
+         self.groups = [NSMutableDictionary dictionary];
+         self.metadata = [NSMutableDictionary dictionary];
+     
+         for (NSString *headerName in request.responseHeaders) {
+             if ([headerName hasPrefix:@"X-Account-Group-"]) {
+                 [groups setObject:[[NSString decodeFromPercentEscape:[request.responseHeaders objectForKey:headerName]] componentsSeparatedByString:@","]
+                            forKey:[NSString decodeFromPercentEscape:[headerName substringFromIndex:16]]];
+              } else if ([headerName hasPrefix:@"X-Account-Meta-"]) {
+                 [metadata setObject:[NSString decodeFromPercentEscape:[request.responseHeaders objectForKey:headerName]]
+                              forKey:[NSString decodeFromPercentEscape:[headerName substringFromIndex:15]]];
              }
          }
          
-         for (NSString *header in request.responseHeaders) {
-             if ([header hasPrefix:@"X-Account-Meta-"])  {
-                 NSString *metadataKey = [NSString decodeFromPercentEscape:[header substringFromIndex:15]];
-                 NSString *metadataValue = [NSString decodeFromPercentEscape:[request.responseHeaders objectForKey:header]];
-                 [metadata setObject:metadataValue forKey:metadataKey];
-             }
+         NSMutableArray *UUIDs = [NSMutableArray array];
+         for (NSString *groupName in groups) {
+             [UUIDs addObjectsFromArray:[groups objectForKey:groupName]];
+         }
+         if (UUIDs.count) {
+             [[self.account.manager userCatalogForDisplaynames:nil UUIDs:UUIDs]
+              success:^(OpenStackRequest *request) {
+                  [activityIndicatorView stopAnimatingAndRemoveFromSuperview];
+                  [self.tableView reloadData];
+              }
+              failure:^(OpenStackRequest *request) {
+                  [activityIndicatorView stopAnimatingAndRemoveFromSuperview];
+                  [self.tableView reloadData];
+                  if (request.responseStatusCode != 404) {
+                      // Don't show alert on 404, since it can be a pre-UUID server.
+                      [self alert:@"Failed to translate group UUIDs." request:request];
+                  }
+              }];
+         } else {
+             [activityIndicatorView stopAnimatingAndRemoveFromSuperview];
+             [self.tableView reloadData];
          }
-         
-         [self.tableView reloadData];
      }
      failure:^(OpenStackRequest *request) {
-         [activityIndicatorView removeFromSuperview];
-         [self alert:@"Error" message:@"Failed to get account information"];
+         [activityIndicatorView stopAnimatingAndRemoveFromSuperview];
+         [self alert:@"Failed to get account information." request:request];
      }];
 }
 
+#pragma mark - Internal
+
+- (NSMutableArray *)groupUsersForGroupName:(NSString *)groupName {
+    NSArray *groupUUIDs = [groups objectForKey:groupName];
+    NSMutableArray *groupUsers = [NSMutableArray arrayWithCapacity:groupUUIDs.count];
+    for (NSString *UUID in groupUUIDs) {
+        [groupUsers addObject:[self.account displaynameForUUID:UUID safe:YES]];
+    }
+    return groupUsers;
+}
+
+- (NSMutableString *)groupUsersStringForGroupName:(NSString *)groupName {
+    NSMutableArray *groupUsers = [self groupUsersForGroupName:groupName];
+    NSMutableString *groupUsersString = [NSMutableString string];
+    for (NSUInteger index = 0; index < groupUsers.count; index++) {
+        if (index) {
+            [groupUsersString appendString:@","];
+        }
+        [groupUsersString appendString:[groupUsers objectAtIndex:index]];
+    }
+    return groupUsersString;
+}
+
 #pragma mark - Memory management
 
 - (void)dealloc {
     [super dealloc];
 }
 
-#pragma mark - Internal
-
-- (CGFloat)findLabelHeight:(NSString*)text font:(UIFont *)font {
-    CGSize textLabelSize;
-    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
-        // 616, 678
-        textLabelSize = CGSizeMake(596.0, 9000.0f);
-    } else {
-        textLabelSize = CGSizeMake(280.0, 9000.0f);
-    }
-    CGSize stringSize = [text sizeWithFont:font constrainedToSize:textLabelSize lineBreakMode:UILineBreakModeCharacterWrap];
-    return stringSize.height;
-}
-
 #pragma mark - UITableViewDataSource
 
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
-    return [groups count] + 1;
+    return (groups.count + 1);
 }
 
 - (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
     CGFloat result;
-    
-    if ([groups count] > 0 && indexPath.row < [groups count]) {
-        NSString *groupName = [[groups allKeys] objectAtIndex:indexPath.row];
-        NSString *groupUsers = [groups objectForKey:groupName];
-        
-        result = 22.0 + [self findLabelHeight:[NSString stringWithFormat:@"%@", groupUsers] font:[UIFont systemFontOfSize:18.0]];
-    
+    if (indexPath.row < groups.count) {
+        NSString *groupName = [[[groups allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)] objectAtIndex:indexPath.row];
+        result = 22.0 + [[self groupUsersStringForGroupName:groupName] sizeWithFont:[UIFont systemFontOfSize:18.0]
+                                                                  constrainedToSize:(([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) ?
+                                                                                     CGSizeMake(596.0, 9000.0) :
+                                                                                     CGSizeMake(280.0, 9000.0))
+                                                                      lineBreakMode:UILineBreakModeCharacterWrap].height;
         return MAX(aTableView.rowHeight, result);
     }
     return aTableView.rowHeight;
 
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     static NSString *CellIdentifier = @"Cell";
-    
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     if (cell == nil) {
         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
     }
     
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
-    if (indexPath.row < [groups count]) {
-        NSString *groupName = [[groups allKeys] objectAtIndex:indexPath.row];
+    if (indexPath.row < groups.count) {
+        NSString *groupName = [[[groups allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)] objectAtIndex:indexPath.row];
         cell.textLabel.text = groupName;
-        cell.detailTextLabel.text = [groups objectForKey:groupName];
+        cell.detailTextLabel.text = [self groupUsersStringForGroupName:groupName];
         cell.detailTextLabel.numberOfLines = 0;
         cell.detailTextLabel.lineBreakMode = UILineBreakModeCharacterWrap;
     } else {
     vc.account = self.account;
     vc.metadata = metadata;
     vc.groups = groups;
-    if (indexPath.row < [groups count]) {
-        NSString *groupName = [[groups allKeys] objectAtIndex:indexPath.row];
-        NSString *groupUsers = [groups objectForKey:groupName];
-        
+    if (indexPath.row < groups.count) {
+        NSString *groupName = [[[groups allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)] objectAtIndex:indexPath.row];
         vc.removeGroupEnabled = YES;
         vc.groupName = groupName;
-        vc.groupUsers = groupUsers;
         vc.navigationItem.title = @"Edit Group";
     } else {
         vc.removeGroupEnabled = NO;
         vc.groupName = @"";
-        vc.groupUsers = @"";
         vc.navigationItem.title = @"Add Group";
     }