Statistics
| Branch: | Tag: | Revision:

root / Classes / AccountGroupsViewController.m @ e06c24cf

History | View | Annotate | Download (7.6 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 "AccountManager.h"
40
#import "OpenStackRequest.h"
41
#import "EditAccountGroupsViewController.h"
42
#import "UIViewController+Conveniences.h"
43
#import "NSString+Conveniences.h"
44
#import "APICallback.h"
45

    
46

    
47
@implementation AccountGroupsViewController
48

    
49
@synthesize account, groups;
50

    
51

    
52
- (void)dealloc
53
{
54
    [account release];
55
    [groups release];
56
    [metadata release];
57
    [super dealloc];
58
}
59

    
60

    
61
#pragma mark - View lifecycle
62

    
63
- (void)viewDidLoad
64
{
65
    [super viewDidLoad];
66
    self.navigationItem.title = @"Groups";
67
    groups = [[NSMutableDictionary alloc] init];
68
    metadata = [[NSMutableDictionary alloc] init];
69
    
70
    NSString *activityMessage = @"Loading..";
71
    activityIndicatorView = [[ActivityIndicatorView alloc] initWithFrame:[ActivityIndicatorView frameForText:activityMessage] text:activityMessage];
72
    [activityIndicatorView addToView:self.view];
73
    
74
    [[self.account.manager getStorageAccountInfo] 
75
     success:^(OpenStackRequest *request) {
76
         [activityIndicatorView removeFromSuperviewAndRelease];
77
         for (NSString *key in request.responseHeaders) {
78
             if ([key hasPrefix:@"X-Account-Group-"]) {
79
                 NSString *groupUsers = [NSString decodeFromPercentEscape:[request.responseHeaders objectForKey:key]];
80
                 NSString *groupName = [NSString decodeFromPercentEscape:[key substringFromIndex:16]];
81
                 [groups setObject:groupUsers forKey:groupName];
82
             }
83
         }
84
         
85
         for (NSString *header in request.responseHeaders) {
86
             if ([header hasPrefix:@"X-Account-Meta-"])  {
87
                 NSString *metadataKey = [NSString decodeFromPercentEscape:[header substringFromIndex:15]];
88
                 NSString *metadataValue = [NSString decodeFromPercentEscape:[request.responseHeaders objectForKey:header]];
89
                 [metadata setObject:metadataValue forKey:metadataKey];
90
             }
91
         }
92

    
93
         [self.tableView reloadData];
94

    
95
     }
96
     failure:^(OpenStackRequest *request) {
97
         [activityIndicatorView removeFromSuperviewAndRelease];
98
         [self alert:@"Error" message:@"Failed to get account information"];
99
     }];
100
}
101

    
102
- (void)viewDidUnload
103
{
104
    [super viewDidUnload];
105
    // Release any retained subviews of the main view.
106
    // e.g. self.myOutlet = nil;
107
}
108

    
109
- (void)viewWillAppear:(BOOL)animated
110
{
111
    [super viewWillAppear:animated];
112
    [self.tableView reloadData];
113
}
114

    
115
- (void)viewDidAppear:(BOOL)animated
116
{
117
    [super viewDidAppear:animated];
118
}
119

    
120
- (void)viewWillDisappear:(BOOL)animated
121
{
122
    [super viewWillDisappear:animated];
123
}
124

    
125
- (void)viewDidDisappear:(BOOL)animated
126
{
127
    [super viewDidDisappear:animated];
128
}
129

    
130
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
131
    return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == UIInterfaceOrientationPortrait);
132
}
133

    
134
#pragma mark - Table view data source
135

    
136
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
137
{
138
    return 1;
139
}
140

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

    
146
- (CGFloat)findLabelHeight:(NSString*)text font:(UIFont *)font {
147
    CGSize textLabelSize;    
148
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
149
        // 616, 678
150
        textLabelSize = CGSizeMake(596.0, 9000.0f);
151
    } else {
152
        textLabelSize = CGSizeMake(280.0, 9000.0f);
153
    }
154
    CGSize stringSize = [text sizeWithFont:font constrainedToSize:textLabelSize lineBreakMode:UILineBreakModeCharacterWrap];
155
    return stringSize.height;
156
}
157

    
158
- (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
159
    CGFloat result;
160
    
161
    if ([groups count] > 0 && indexPath.row < [groups count]) {
162
        NSString *groupName = [[groups allKeys] objectAtIndex:indexPath.row];
163
        NSString *groupUsers = [groups objectForKey:groupName];
164
        
165
        result = 22.0 + [self findLabelHeight:[NSString stringWithFormat:@"%@", groupUsers] font:[UIFont systemFontOfSize:18.0]];
166
    
167
        return MAX(aTableView.rowHeight, result);
168
    }
169
    return aTableView.rowHeight;
170
}
171

    
172

    
173
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
174
{
175
    static NSString *CellIdentifier = @"Cell";
176
    
177
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
178
    if (cell == nil) {
179
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
180
    }
181
    
182
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
183
    if (indexPath.row < [groups count]) {
184
        NSString *groupName = [[groups allKeys] objectAtIndex:indexPath.row];
185
        cell.textLabel.text = groupName;
186
        cell.detailTextLabel.text = [groups objectForKey:groupName];
187
        cell.detailTextLabel.numberOfLines = 0;
188
        cell.detailTextLabel.lineBreakMode = UILineBreakModeCharacterWrap;
189
    }
190
    else {
191
        cell.textLabel.text = @"Add Group";
192
        cell.detailTextLabel.text = @"";
193
    }
194
    
195
    return cell;
196
}
197

    
198

    
199
#pragma mark - Table view delegate
200

    
201
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
202
{
203
    EditAccountGroupsViewController *vc = [[EditAccountGroupsViewController alloc] initWithNibName:@"EditAccountGroupsViewController" bundle:nil];
204
    
205
    vc.account = self.account;
206
    vc.metadata = metadata;
207
    vc.groups = groups;
208
    if (indexPath.row < [groups count]) {        
209
        NSString *groupName = [[groups allKeys] objectAtIndex:indexPath.row];
210
        NSString *groupUsers = [groups objectForKey:groupName];
211
        
212
        vc.removeGroupEnabled = YES;
213
        vc.groupName = groupName;
214
        vc.groupUsers = groupUsers;
215
        vc.navigationItem.title = @"Edit Group";
216
    }
217
    else {
218
        vc.removeGroupEnabled = NO;
219
        vc.groupName = @"";
220
        vc.groupUsers = @"";
221
        vc.navigationItem.title = @"Add Group";
222
    }
223
    
224
    [self.navigationController pushViewController:vc animated:YES];
225
    [vc release];
226
}
227

    
228

    
229
@end