Show last modified date in folder cells
[pithos-ios] / Classes / AccountGroupsViewController.m
1 //
2 //  AccountGroupsViewController.m
3 //  pithos-ios
4 //
5 // Copyright 2011 GRNET S.A. All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or
8 // without modification, are permitted provided that the following
9 // conditions are met:
10 // 
11 //   1. Redistributions of source code must retain the above
12 //      copyright notice, this list of conditions and the following
13 //      disclaimer.
14 // 
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.
19 // 
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.
32 // 
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.
37
38 #import "AccountGroupsViewController.h"
39 #import "OpenStackAccount.h"
40 #import "ActivityIndicatorView.h"
41 #import "AccountManager.h"
42 #import "OpenStackRequest.h"
43 #import "EditAccountGroupsViewController.h"
44 #import "UIViewController+Conveniences.h"
45 #import "NSString+Conveniences.h"
46 #import "APICallback.h"
47
48 @implementation AccountGroupsViewController
49
50 @synthesize account, groups;
51
52 #pragma mark - View lifecycle
53
54 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
55     return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == UIInterfaceOrientationPortrait);
56 }
57
58 - (void)viewWillAppear:(BOOL)animated {
59     [super viewWillAppear:animated];
60     self.navigationItem.title = @"Groups";
61     groups = [[NSMutableDictionary alloc] init];
62     metadata = [[NSMutableDictionary alloc] init];
63
64     __block ActivityIndicatorView *activityIndicatorView = [ActivityIndicatorView activityIndicatorViewWithText:@"Loading..."
65                                                                                                    andAddToView:self.view];
66     [[self.account.manager getStorageAccountInfo]
67      success:^(OpenStackRequest *request) {
68          [activityIndicatorView removeFromSuperview];
69          for (NSString *key in request.responseHeaders) {
70              if ([key hasPrefix:@"X-Account-Group-"]) {
71                  NSString *groupUsers = [NSString decodeFromPercentEscape:[request.responseHeaders objectForKey:key]];
72                  NSString *groupName = [NSString decodeFromPercentEscape:[key substringFromIndex:16]];
73                  [groups setObject:groupUsers forKey:groupName];
74              }
75          }
76          
77          for (NSString *header in request.responseHeaders) {
78              if ([header hasPrefix:@"X-Account-Meta-"])  {
79                  NSString *metadataKey = [NSString decodeFromPercentEscape:[header substringFromIndex:15]];
80                  NSString *metadataValue = [NSString decodeFromPercentEscape:[request.responseHeaders objectForKey:header]];
81                  [metadata setObject:metadataValue forKey:metadataKey];
82              }
83          }
84          
85          [self.tableView reloadData];
86      }
87      failure:^(OpenStackRequest *request) {
88          [activityIndicatorView removeFromSuperview];
89          [self alert:@"Error" message:@"Failed to get account information"];
90      }];
91 }
92
93 #pragma mark - Memory management
94
95 - (void)dealloc {
96     [account release];
97     [groups release];
98     [metadata release];
99     [super dealloc];
100 }
101
102 #pragma mark - Internal
103
104 - (CGFloat)findLabelHeight:(NSString*)text font:(UIFont *)font {
105     CGSize textLabelSize;
106     if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
107         // 616, 678
108         textLabelSize = CGSizeMake(596.0, 9000.0f);
109     } else {
110         textLabelSize = CGSizeMake(280.0, 9000.0f);
111     }
112     CGSize stringSize = [text sizeWithFont:font constrainedToSize:textLabelSize lineBreakMode:UILineBreakModeCharacterWrap];
113     return stringSize.height;
114 }
115
116 #pragma mark - UITableViewDataSource
117
118 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
119     return [groups count] + 1;
120 }
121
122 - (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
123     CGFloat result;
124     
125     if ([groups count] > 0 && indexPath.row < [groups count]) {
126         NSString *groupName = [[groups allKeys] objectAtIndex:indexPath.row];
127         NSString *groupUsers = [groups objectForKey:groupName];
128         
129         result = 22.0 + [self findLabelHeight:[NSString stringWithFormat:@"%@", groupUsers] font:[UIFont systemFontOfSize:18.0]];
130     
131         return MAX(aTableView.rowHeight, result);
132     }
133     return aTableView.rowHeight;
134 }
135
136
137 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
138     static NSString *CellIdentifier = @"Cell";
139     
140     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
141     if (cell == nil) {
142         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
143     }
144     
145     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
146     if (indexPath.row < [groups count]) {
147         NSString *groupName = [[groups allKeys] objectAtIndex:indexPath.row];
148         cell.textLabel.text = groupName;
149         cell.detailTextLabel.text = [groups objectForKey:groupName];
150         cell.detailTextLabel.numberOfLines = 0;
151         cell.detailTextLabel.lineBreakMode = UILineBreakModeCharacterWrap;
152     } else {
153         cell.textLabel.text = @"Add Group";
154         cell.detailTextLabel.text = @"";
155     }
156     
157     return cell;
158 }
159
160
161 #pragma mark - UITableViewDelegate
162
163 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
164     EditAccountGroupsViewController *vc = [[EditAccountGroupsViewController alloc] initWithNibName:@"EditAccountGroupsViewController" bundle:nil];
165     vc.account = self.account;
166     vc.metadata = metadata;
167     vc.groups = groups;
168     if (indexPath.row < [groups count]) {
169         NSString *groupName = [[groups allKeys] objectAtIndex:indexPath.row];
170         NSString *groupUsers = [groups objectForKey:groupName];
171         
172         vc.removeGroupEnabled = YES;
173         vc.groupName = groupName;
174         vc.groupUsers = groupUsers;
175         vc.navigationItem.title = @"Edit Group";
176     } else {
177         vc.removeGroupEnabled = NO;
178         vc.groupName = @"";
179         vc.groupUsers = @"";
180         vc.navigationItem.title = @"Add Group";
181     }
182     
183     [self.navigationController pushViewController:vc animated:YES];
184     [vc release];
185 }
186
187 @end