2 // EditPolicyViewController.m
5 // Copyright 2011 GRNET S.A. All rights reserved.
7 // Redistribution and use in source and binary forms, with or
8 // without modification, are permitted provided that the following
11 // 1. Redistributions of source code must retain the above
12 // copyright notice, this list of conditions and the following
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.
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.
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.
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"
49 @implementation EditPolicyViewController
51 @synthesize account, container, containerDetailViewController;
52 @synthesize oldVersioning, oldQuota;
58 [oldVersioning release];
59 [activityIndicatorView release];
63 - (void)didReceiveMemoryWarning
65 [super didReceiveMemoryWarning];
69 #pragma mark - Helper Methods
71 - (void) configVersioningVariables
73 if ([container.versioning rangeOfString:@"auto"].location != NSNotFound) {
75 manualVersioning = NO;
78 else if ([container.versioning rangeOfString:@"manual"].location != NSNotFound) {
80 manualVersioning = YES;
83 else if ([container.versioning rangeOfString:@"none"].location != NSNotFound) {
85 manualVersioning = NO;
90 #pragma mark - View lifecycle
95 [self configVersioningVariables];
96 self.navigationItem.title = @"Edit policy";
101 [super viewDidUnload];
104 - (void)viewWillAppear:(BOOL)animated
106 [super viewWillAppear:animated];
107 self.oldQuota = container.quota;
110 - (void)viewDidAppear:(BOOL)animated
112 [super viewDidAppear:animated];
115 - (void)viewWillDisappear:(BOOL)animated
117 [super viewWillDisappear:animated];
120 - (void)viewDidDisappear:(BOOL)animated
122 [super viewDidDisappear:animated];
125 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
126 return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == UIInterfaceOrientationPortrait);
129 #pragma mark - Table view data source
131 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
136 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
138 if (section == kVersioning)
144 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
146 static NSString *CellIdentifier = @"Cell";
148 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
150 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
154 if (indexPath.section == kVersioning) {
155 cell.selectionStyle = UITableViewCellSelectionStyleNone;
156 if (indexPath.row == 0) {
157 cell.textLabel.text = @"Auto";
159 cell.accessoryType = UITableViewCellAccessoryCheckmark;
161 cell.accessoryType = UITableViewCellAccessoryNone;
163 else if (indexPath.row == 1) {
164 cell.textLabel.text = @"Manual";
165 if (manualVersioning)
166 cell.accessoryType = UITableViewCellAccessoryCheckmark;
168 cell.accessoryType = UITableViewCellAccessoryNone;
170 else if (indexPath.row == 2) {
171 cell.textLabel.text = @"None";
173 cell.accessoryType = UITableViewCellAccessoryCheckmark;
175 cell.accessoryType = UITableViewCellAccessoryNone;
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;
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];
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;
201 cell.selectionStyle = UITableViewCellSelectionStyleNone;
202 textField.text = [NSString stringWithFormat:@"%u", self.container.quota];
203 [cell.contentView addSubview:textField];
204 cell.accessoryType = UITableViewCellAccessoryNone;
206 else if (indexPath.section == kSavePolicy) {
207 cell.textLabel.text = @"Save";
208 cell.accessoryType = UITableViewCellAccessoryNone;
209 cell.selectionStyle = UITableViewCellSelectionStyleBlue;
215 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
217 if (section == kVersioning)
218 return @"Versioning";
219 else if (section == kQuota)
226 #pragma mark - Table view delegate
228 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
231 if (indexPath.section != kQuota)
232 [self.view endEditing:YES];
234 if (indexPath.section == kVersioning) {
235 if (indexPath.row == 0) {
236 if (!autoVersioning) {
237 autoVersioning = YES;
238 manualVersioning = NO;
242 else if (indexPath.row == 1) {
243 if (!manualVersioning) {
245 manualVersioning = YES;
249 else if (indexPath.row == 2) {
252 manualVersioning = NO;
256 [self.tableView reloadData];
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];
264 self.oldVersioning = container.versioning;
267 container.versioning = @"auto";
268 else if (manualVersioning)
269 container.versioning = @"manual";
270 else if (noVersioning)
271 container.versioning = @"none";
273 NSString *activityMessage = @"Applying policy...";
274 [activityIndicatorView release];
275 activityIndicatorView = [[ActivityIndicatorView alloc] initWithFrame:[ActivityIndicatorView frameForText:activityMessage] text:activityMessage];
276 [activityIndicatorView addToView:self.view];
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];
286 failure:^(OpenStackRequest *request) {
287 container.versioning = self.oldVersioning;
288 container.quota = self.oldQuota;
289 [self configVersioningVariables];
290 [self.tableView reloadData];
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];
300 #pragma mark - Textfield delegate
302 - (void)textFieldDidEndEditing:(UITextField *)textField
304 self.oldQuota = container.quota;
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];
313 [numberFormatter release];
317 - (BOOL)textFieldShouldReturn:(UITextField *)textField
319 [textField resignFirstResponder];