Statistics
| Branch: | Tag: | Revision:

root / Classes / AccountGroupsViewController.m @ c2940e36

History | View | Annotate | Download (8.7 kB)

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, metadata;
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
}
62

    
63
- (void)viewDidAppear:(BOOL)animated {
64
    [super viewDidAppear:animated];
65
    __block ActivityIndicatorView *activityIndicatorView = [ActivityIndicatorView activityIndicatorViewWithText:@"Loading groups..."
66
                                                                                                   andAddToView:self.view];
67
    [[self.account.manager getStorageAccountInfo]
68
     success:^(OpenStackRequest *request) {
69
         self.groups = [NSMutableDictionary dictionary];
70
         self.metadata = [NSMutableDictionary dictionary];
71
     
72
         for (NSString *headerName in request.responseHeaders) {
73
             if ([headerName hasPrefix:@"X-Account-Group-"]) {
74
                 [groups setObject:[[NSString decodeFromPercentEscape:[request.responseHeaders objectForKey:headerName]] componentsSeparatedByString:@","]
75
                            forKey:[NSString decodeFromPercentEscape:[headerName substringFromIndex:16]]];
76
              } else if ([headerName hasPrefix:@"X-Account-Meta-"]) {
77
                 [metadata setObject:[NSString decodeFromPercentEscape:[request.responseHeaders objectForKey:headerName]]
78
                              forKey:[NSString decodeFromPercentEscape:[headerName substringFromIndex:15]]];
79
             }
80
         }
81
         
82
         NSMutableArray *UUIDs = [NSMutableArray array];
83
         for (NSString *groupName in groups) {
84
             [UUIDs addObjectsFromArray:[groups objectForKey:groupName]];
85
         }
86
         if (UUIDs.count) {
87
             [[self.account.manager userCatalogForDisplaynames:nil UUIDs:UUIDs]
88
              success:^(OpenStackRequest *request) {
89
                  [activityIndicatorView stopAnimatingAndRemoveFromSuperview];
90
                  [self.tableView reloadData];
91
              }
92
              failure:^(OpenStackRequest *request) {
93
                  [activityIndicatorView stopAnimatingAndRemoveFromSuperview];
94
                  [self.tableView reloadData];
95
                  [self alert:@"Failed to translate group UUIDs." request:request];
96
              }];
97
         } else {
98
             [activityIndicatorView stopAnimatingAndRemoveFromSuperview];
99
             [self.tableView reloadData];
100
         }
101
     }
102
     failure:^(OpenStackRequest *request) {
103
         [activityIndicatorView stopAnimatingAndRemoveFromSuperview];
104
         [self alert:@"Failed to get account information." request:request];
105
     }];
106
}
107

    
108
#pragma mark - Internal
109

    
110
- (NSMutableArray *)groupUsersForGroupName:(NSString *)groupName {
111
    NSArray *groupUUIDs = [groups objectForKey:groupName];
112
    NSMutableArray *groupUsers = [NSMutableArray arrayWithCapacity:groupUUIDs.count];
113
    for (NSString *UUID in groupUUIDs) {
114
        [groupUsers addObject:[self.account displaynameForUUID:UUID safe:YES]];
115
    }
116
    return groupUsers;
117
}
118

    
119
- (NSMutableString *)groupUsersStringForGroupName:(NSString *)groupName {
120
    NSMutableArray *groupUsers = [self groupUsersForGroupName:groupName];
121
    NSMutableString *groupUsersString = [NSMutableString string];
122
    for (NSUInteger index = 0; index < groupUsers.count; index++) {
123
        if (index) {
124
            [groupUsersString appendString:@","];
125
        }
126
        [groupUsersString appendString:[groupUsers objectAtIndex:index]];
127
    }
128
    return groupUsersString;
129
}
130

    
131
#pragma mark - Memory management
132

    
133
- (void)dealloc {
134
    [account release];
135
    [groups release];
136
    [metadata release];
137
    [super dealloc];
138
}
139

    
140
#pragma mark - UITableViewDataSource
141

    
142
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
143
    return (groups.count + 1);
144
}
145

    
146
- (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
147
    CGFloat result;
148
    if (indexPath.row < groups.count) {
149
        NSString *groupName = [[[groups allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)] objectAtIndex:indexPath.row];
150
        result = 22.0 + [[self groupUsersStringForGroupName:groupName] sizeWithFont:[UIFont systemFontOfSize:18.0]
151
                                                                  constrainedToSize:(([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) ?
152
                                                                                     CGSizeMake(596.0, 9000.0) :
153
                                                                                     CGSizeMake(280.0, 9000.0))
154
                                                                      lineBreakMode:UILineBreakModeCharacterWrap].height;
155
        return MAX(aTableView.rowHeight, result);
156
    }
157
    return aTableView.rowHeight;
158
}
159

    
160

    
161
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
162
    static NSString *CellIdentifier = @"Cell";
163
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
164
    if (cell == nil) {
165
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
166
    }
167
    
168
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
169
    if (indexPath.row < groups.count) {
170
        NSString *groupName = [[[groups allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)] objectAtIndex:indexPath.row];
171
        cell.textLabel.text = groupName;
172
        cell.detailTextLabel.text = [self groupUsersStringForGroupName:groupName];
173
        cell.detailTextLabel.numberOfLines = 0;
174
        cell.detailTextLabel.lineBreakMode = UILineBreakModeCharacterWrap;
175
    } else {
176
        cell.textLabel.text = @"Add Group";
177
        cell.detailTextLabel.text = @"";
178
    }
179
    
180
    return cell;
181
}
182

    
183

    
184
#pragma mark - UITableViewDelegate
185

    
186
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
187
    EditAccountGroupsViewController *vc = [[EditAccountGroupsViewController alloc] initWithNibName:@"EditAccountGroupsViewController" bundle:nil];
188
    vc.account = self.account;
189
    vc.metadata = metadata;
190
    vc.groups = groups;
191
    if (indexPath.row < groups.count) {
192
        NSString *groupName = [[[groups allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)] objectAtIndex:indexPath.row];
193
        vc.removeGroupEnabled = YES;
194
        vc.groupName = groupName;
195
        vc.navigationItem.title = @"Edit Group";
196
    } else {
197
        vc.removeGroupEnabled = NO;
198
        vc.groupName = @"";
199
        vc.navigationItem.title = @"Add Group";
200
    }
201
    
202
    [self.navigationController pushViewController:vc animated:YES];
203
    [vc release];
204
}
205

    
206
@end