2 // AccountGroupsViewController.m
5 // Copyright 2011 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 "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"
48 @implementation AccountGroupsViewController
50 @synthesize account, groups;
52 #pragma mark - View lifecycle
54 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
55 return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == UIInterfaceOrientationPortrait);
58 - (void)viewWillAppear:(BOOL)animated {
59 [super viewWillAppear:animated];
60 self.navigationItem.title = @"Groups";
61 groups = [[NSMutableDictionary alloc] init];
62 metadata = [[NSMutableDictionary alloc] init];
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];
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];
85 [self.tableView reloadData];
87 failure:^(OpenStackRequest *request) {
88 [activityIndicatorView removeFromSuperview];
89 [self alert:@"Error" message:@"Failed to get account information"];
93 #pragma mark - Memory management
102 #pragma mark - Internal
104 - (CGFloat)findLabelHeight:(NSString*)text font:(UIFont *)font {
105 CGSize textLabelSize;
106 if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
108 textLabelSize = CGSizeMake(596.0, 9000.0f);
110 textLabelSize = CGSizeMake(280.0, 9000.0f);
112 CGSize stringSize = [text sizeWithFont:font constrainedToSize:textLabelSize lineBreakMode:UILineBreakModeCharacterWrap];
113 return stringSize.height;
116 #pragma mark - UITableViewDataSource
118 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
119 return [groups count] + 1;
122 - (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
125 if ([groups count] > 0 && indexPath.row < [groups count]) {
126 NSString *groupName = [[groups allKeys] objectAtIndex:indexPath.row];
127 NSString *groupUsers = [groups objectForKey:groupName];
129 result = 22.0 + [self findLabelHeight:[NSString stringWithFormat:@"%@", groupUsers] font:[UIFont systemFontOfSize:18.0]];
131 return MAX(aTableView.rowHeight, result);
133 return aTableView.rowHeight;
137 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
138 static NSString *CellIdentifier = @"Cell";
140 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
142 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
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;
153 cell.textLabel.text = @"Add Group";
154 cell.detailTextLabel.text = @"";
161 #pragma mark - UITableViewDelegate
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;
168 if (indexPath.row < [groups count]) {
169 NSString *groupName = [[groups allKeys] objectAtIndex:indexPath.row];
170 NSString *groupUsers = [groups objectForKey:groupName];
172 vc.removeGroupEnabled = YES;
173 vc.groupName = groupName;
174 vc.groupUsers = groupUsers;
175 vc.navigationItem.title = @"Edit Group";
177 vc.removeGroupEnabled = NO;
180 vc.navigationItem.title = @"Add Group";
183 [self.navigationController pushViewController:vc animated:YES];