Statistics
| Branch: | Tag: | Revision:

root / Classes / AccountGroupsViewController.m @ 3a8071d4

History | View | Annotate | Download (7.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 "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
    [activityIndicatorView release];
58
    [super dealloc];
59
}
60

    
61

    
62
#pragma mark - View lifecycle
63

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

    
95
         [self.tableView reloadData];
96

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

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

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

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

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

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

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

    
136
#pragma mark - Table view data source
137

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

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

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

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

    
174

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

    
200

    
201
#pragma mark - Table view delegate
202

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

    
230

    
231
@end