root / Classes / EditPolicyViewController.m @ 9fee07a6
History | View | Annotate | Download (10.6 kB)
1 |
// |
---|---|
2 |
// EditPolicyViewController.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 "EditPolicyViewController.h" |
39 |
#import "UIViewController+Conveniences.h" |
40 |
#import "AccountManager.h" |
41 |
#import "ContainerDetailViewController.h" |
42 |
#import "ContainersViewController.h" |
43 |
#import "APICallback.h" |
44 |
|
45 |
#define kVersioning 0 |
46 |
#define kQuota 1 |
47 |
#define kSavePolicy 2 |
48 |
|
49 |
@implementation EditPolicyViewController |
50 |
|
51 |
@synthesize account, container, containerDetailViewController; |
52 |
@synthesize oldVersioning, oldQuota; |
53 |
|
54 |
- (void)dealloc |
55 |
{ |
56 |
[account release]; |
57 |
[container release]; |
58 |
[oldVersioning release]; |
59 |
[activityIndicatorView release]; |
60 |
[super dealloc]; |
61 |
} |
62 |
|
63 |
- (void)didReceiveMemoryWarning |
64 |
{ |
65 |
[super didReceiveMemoryWarning]; |
66 |
|
67 |
} |
68 |
|
69 |
#pragma mark - Helper Methods |
70 |
|
71 |
- (void) configVersioningVariables |
72 |
{ |
73 |
if ([container.versioning rangeOfString:@"auto"].location != NSNotFound) { |
74 |
autoVersioning = YES; |
75 |
manualVersioning = NO; |
76 |
noVersioning = NO; |
77 |
} |
78 |
else if ([container.versioning rangeOfString:@"manual"].location != NSNotFound) { |
79 |
autoVersioning = NO; |
80 |
manualVersioning = YES; |
81 |
noVersioning = NO; |
82 |
} |
83 |
else if ([container.versioning rangeOfString:@"none"].location != NSNotFound) { |
84 |
autoVersioning = NO; |
85 |
manualVersioning = NO; |
86 |
noVersioning = YES; |
87 |
} |
88 |
} |
89 |
|
90 |
#pragma mark - View lifecycle |
91 |
|
92 |
- (void)viewDidLoad |
93 |
{ |
94 |
[super viewDidLoad]; |
95 |
[self configVersioningVariables]; |
96 |
self.navigationItem.title = @"Edit policy"; |
97 |
} |
98 |
|
99 |
- (void)viewDidUnload |
100 |
{ |
101 |
[super viewDidUnload]; |
102 |
} |
103 |
|
104 |
- (void)viewWillAppear:(BOOL)animated |
105 |
{ |
106 |
[super viewWillAppear:animated]; |
107 |
self.oldQuota = container.quota; |
108 |
} |
109 |
|
110 |
- (void)viewDidAppear:(BOOL)animated |
111 |
{ |
112 |
[super viewDidAppear:animated]; |
113 |
} |
114 |
|
115 |
- (void)viewWillDisappear:(BOOL)animated |
116 |
{ |
117 |
[super viewWillDisappear:animated]; |
118 |
} |
119 |
|
120 |
- (void)viewDidDisappear:(BOOL)animated |
121 |
{ |
122 |
[super viewDidDisappear:animated]; |
123 |
} |
124 |
|
125 |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { |
126 |
return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == UIInterfaceOrientationPortrait); |
127 |
} |
128 |
|
129 |
#pragma mark - Table view data source |
130 |
|
131 |
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView |
132 |
{ |
133 |
return 3; |
134 |
} |
135 |
|
136 |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section |
137 |
{ |
138 |
if (section == kVersioning) |
139 |
return 2; |
140 |
else |
141 |
return 1; |
142 |
} |
143 |
|
144 |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath |
145 |
{ |
146 |
static NSString *CellIdentifier = @"Cell"; |
147 |
|
148 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
149 |
if (cell == nil) { |
150 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
151 |
} |
152 |
|
153 |
|
154 |
if (indexPath.section == kVersioning) { |
155 |
cell.selectionStyle = UITableViewCellSelectionStyleNone; |
156 |
if (indexPath.row == 0) { |
157 |
cell.textLabel.text = @"Auto"; |
158 |
if (autoVersioning) |
159 |
cell.accessoryType = UITableViewCellAccessoryCheckmark; |
160 |
else |
161 |
cell.accessoryType = UITableViewCellAccessoryNone; |
162 |
} |
163 |
else if (indexPath.row == 1) { |
164 |
cell.textLabel.text = @"Manual"; |
165 |
if (manualVersioning) |
166 |
cell.accessoryType = UITableViewCellAccessoryCheckmark; |
167 |
else |
168 |
cell.accessoryType = UITableViewCellAccessoryNone; |
169 |
} |
170 |
else if (indexPath.row == 2) { |
171 |
cell.textLabel.text = @"None"; |
172 |
if (noVersioning) |
173 |
cell.accessoryType = UITableViewCellAccessoryCheckmark; |
174 |
else |
175 |
cell.accessoryType = UITableViewCellAccessoryNone; |
176 |
} |
177 |
} |
178 |
else if (indexPath.section == kQuota) { |
179 |
UITextField *textField = nil; |
180 |
for (id subView in cell.contentView.subviews) { |
181 |
if ([subView isKindOfClass:[UITextField class]]) { |
182 |
textField = (UITextField *)subView; |
183 |
} |
184 |
} |
185 |
|
186 |
if (textField == nil) { |
187 |
CGRect bounds = [cell.contentView bounds]; |
188 |
CGRect rect = CGRectInset(bounds, 10.0, 10.0); |
189 |
textField = [[[UITextField alloc] initWithFrame:rect] autorelease]; |
190 |
[textField setFrame:rect]; |
191 |
} |
192 |
[textField setClearButtonMode:UITextFieldViewModeWhileEditing]; |
193 |
[textField setBackgroundColor:[UIColor clearColor]]; |
194 |
[textField setOpaque:YES]; |
195 |
[textField setAutocorrectionType:UITextAutocorrectionTypeNo]; |
196 |
[textField setAutocapitalizationType:UITextAutocapitalizationTypeNone]; |
197 |
[textField setDelegate:self]; |
198 |
textField.keyboardType = UIKeyboardTypeNumberPad; |
199 |
textField.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; |
200 |
|
201 |
cell.selectionStyle = UITableViewCellSelectionStyleNone; |
202 |
textField.text = [NSString stringWithFormat:@"%u", self.container.quota]; |
203 |
[cell.contentView addSubview:textField]; |
204 |
cell.accessoryType = UITableViewCellAccessoryNone; |
205 |
} |
206 |
else if (indexPath.section == kSavePolicy) { |
207 |
cell.textLabel.text = @"Save"; |
208 |
cell.accessoryType = UITableViewCellAccessoryNone; |
209 |
cell.selectionStyle = UITableViewCellSelectionStyleBlue; |
210 |
} |
211 |
|
212 |
return cell; |
213 |
} |
214 |
|
215 |
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section |
216 |
{ |
217 |
if (section == kVersioning) |
218 |
return @"Versioning"; |
219 |
else if (section == kQuota) |
220 |
return @"Quota"; |
221 |
else |
222 |
return nil; |
223 |
} |
224 |
|
225 |
|
226 |
#pragma mark - Table view delegate |
227 |
|
228 |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath |
229 |
{ |
230 |
|
231 |
if (indexPath.section != kQuota) |
232 |
[self.view endEditing:YES]; |
233 |
|
234 |
if (indexPath.section == kVersioning) { |
235 |
if (indexPath.row == 0) { |
236 |
if (!autoVersioning) { |
237 |
autoVersioning = YES; |
238 |
manualVersioning = NO; |
239 |
noVersioning = NO; |
240 |
} |
241 |
} |
242 |
else if (indexPath.row == 1) { |
243 |
if (!manualVersioning) { |
244 |
autoVersioning = NO; |
245 |
manualVersioning = YES; |
246 |
noVersioning = NO; |
247 |
} |
248 |
} |
249 |
else if (indexPath.row == 2) { |
250 |
if (!noVersioning) { |
251 |
autoVersioning = NO; |
252 |
manualVersioning = NO; |
253 |
noVersioning = NO; |
254 |
} |
255 |
} |
256 |
[self.tableView reloadData]; |
257 |
} |
258 |
else if (indexPath.section == kSavePolicy) { |
259 |
NSIndexPath *quotaCellIndexPath = [NSIndexPath indexPathForRow:0 inSection:kQuota]; |
260 |
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:quotaCellIndexPath]; |
261 |
UITextField *textField = [[cell.contentView subviews] objectAtIndex:0]; |
262 |
self.container.quota = [textField.text integerValue]; |
263 |
|
264 |
self.oldVersioning = container.versioning; |
265 |
|
266 |
if (autoVersioning) |
267 |
container.versioning = @"auto"; |
268 |
else if (manualVersioning) |
269 |
container.versioning = @"manual"; |
270 |
else if (noVersioning) |
271 |
container.versioning = @"none"; |
272 |
|
273 |
NSString *activityMessage = @"Applying policy..."; |
274 |
[activityIndicatorView release]; |
275 |
activityIndicatorView = [[ActivityIndicatorView alloc] initWithFrame:[ActivityIndicatorView frameForText:activityMessage] text:activityMessage]; |
276 |
[activityIndicatorView addToView:self.view]; |
277 |
|
278 |
[[self.account.manager writeContainerPolicy:container] |
279 |
success:^(OpenStackRequest *request) { |
280 |
[self.tableView reloadData]; |
281 |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) |
282 |
[self.containerDetailViewController.containersViewController refreshButtonPressed:nil]; |
283 |
[activityIndicatorView removeFromSuperview]; |
284 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
285 |
} |
286 |
failure:^(OpenStackRequest *request) { |
287 |
container.versioning = self.oldVersioning; |
288 |
container.quota = self.oldQuota; |
289 |
[self configVersioningVariables]; |
290 |
[self.tableView reloadData]; |
291 |
|
292 |
[activityIndicatorView removeFromSuperview]; |
293 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
294 |
[self.tableView reloadData]; |
295 |
[self alert:@"There was a problem applying the policy." request:request]; |
296 |
}]; |
297 |
} |
298 |
} |
299 |
|
300 |
#pragma mark - Textfield delegate |
301 |
|
302 |
- (void)textFieldDidEndEditing:(UITextField *)textField |
303 |
{ |
304 |
self.oldQuota = container.quota; |
305 |
|
306 |
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; |
307 |
[numberFormatter setAllowsFloats:NO]; |
308 |
if (![numberFormatter numberFromString:textField.text]) { |
309 |
[self alert:@"Invalid quota value" message:@"Only integers are accepted as valid quota values"]; |
310 |
textField.text = [NSString stringWithFormat:@"%d", self.oldQuota]; |
311 |
[self.tableView reloadData]; |
312 |
} |
313 |
[numberFormatter release]; |
314 |
} |
315 |
|
316 |
|
317 |
- (BOOL)textFieldShouldReturn:(UITextField *)textField |
318 |
{ |
319 |
[textField resignFirstResponder]; |
320 |
return YES; |
321 |
} |
322 |
|
323 |
|
324 |
@end |