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