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