Create application/directory for subdir, if metadata or permissions are applied.
[pithos-ios] / Classes / PasscodeLockViewController.m
1 //
2 //  PasscodeLockViewController.m
3 //  OpenStack
4 //
5 //  Created by Mike Mayo on 10/26/10.
6 //  The OpenStack project is provided under the Apache 2.0 license.
7 //
8
9 #import "PasscodeLockViewController.h"
10 #import "Keychain.h"
11 #import "UIViewController+Conveniences.h"
12 #import "PasscodeViewController.h"
13 #import "SettingsViewController.h"
14
15
16 @implementation PasscodeLockViewController
17
18 @synthesize settingsViewController;
19
20 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
21     return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == UIInterfaceOrientationPortrait);
22 }
23
24 - (void)simplePasscodeSwitchChanged:(id)sender {
25     // no need to authenticate for this one
26     simplePasscodeOn = simplePasscodeSwitch.on;
27     if (simplePasscodeOn) {
28         [Keychain setString:@"YES" forKey:@"passcode_lock_simple_passcode_on"];
29     } else {
30         [Keychain setString:@"NO" forKey:@"passcode_lock_simple_passcode_on"];
31     }
32 }
33
34 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
35     if (buttonIndex == 0) {
36         eraseDataOn = YES;
37         [Keychain setString:@"YES" forKey:@"passcode_lock_erase_data_on"];
38     } else {
39         eraseDataOn = NO;
40         [Keychain setString:@"NO" forKey:@"passcode_lock_erase_data_on"];
41     }
42     [eraseDataSwitch setOn:eraseDataOn animated:YES];
43 }
44
45 - (void)eraseDataSwitchChanged:(id)sender {
46     if (eraseDataSwitch.on) {
47         UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"All data in this app will be erased after 10 failed passcode attempts." delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Enable" otherButtonTitles:nil];
48         [sheet showInView:self.view];
49         [sheet release];
50     } else {
51         eraseDataOn = NO;
52         [Keychain setString:@"NO" forKey:@"passcode_lock_erase_data_on"];
53     }    
54 }
55
56 #pragma mark -
57 #pragma mark View lifecycle
58
59 - (void)viewDidLoad {
60     [super viewDidLoad];
61     self.navigationItem.title = @"Passcode Lock";
62     
63     simplePasscodeSwitch = [[UISwitch alloc] init];
64     [simplePasscodeSwitch addTarget:self action:@selector(simplePasscodeSwitchChanged:) forControlEvents:UIControlEventValueChanged];
65     
66     eraseDataSwitch = [[UISwitch alloc] init];
67     [eraseDataSwitch addTarget:self action:@selector(eraseDataSwitchChanged:) forControlEvents:UIControlEventValueChanged];
68 }
69
70
71 - (void)viewWillAppear:(BOOL)animated {
72     [super viewWillAppear:animated];
73     passcodeLockOn = [[Keychain getStringForKey:@"passcode_lock_passcode_on"] isEqualToString:@"YES"];
74     simplePasscodeOn = [[Keychain getStringForKey:@"passcode_lock_simple_passcode_on"] isEqualToString:@"YES"];
75     eraseDataOn = [[Keychain getStringForKey:@"passcode_lock_erase_data_on"] isEqualToString:@"YES"];
76     simplePasscodeSwitch.on = simplePasscodeOn;
77     eraseDataSwitch.on = eraseDataOn;
78 }
79
80 /*
81 - (void)viewDidAppear:(BOOL)animated {
82     [super viewDidAppear:animated];
83 }
84 */
85 /*
86 - (void)viewWillDisappear:(BOOL)animated {
87     [super viewWillDisappear:animated];
88 }
89 */
90 /*
91 - (void)viewDidDisappear:(BOOL)animated {
92     [super viewDidDisappear:animated];
93 }
94 */
95
96 #pragma mark -
97 #pragma mark Table view data source
98
99 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
100     // Return the number of sections.
101     return 4;
102 }
103
104
105 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
106     // Return the number of rows in the section.
107     return 1;
108 }
109
110 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
111     if (section == 2) {
112         return @"A simple passcode is a 4 digit number.";
113     } else if (section == 3) {
114         return @"Erase all data in this app after 10 failed passcode attempts.";
115     } else {
116         return @"";
117     }
118 }
119
120 // Customize the appearance of table view cells.
121 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
122     
123     static NSString *CellIdentifier = @"Cell";
124     
125     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
126     if (cell == nil) {
127         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
128     }
129     
130     // Configure the cell...
131     if (indexPath.section == 0) {
132         if (passcodeLockOn) {
133             cell.textLabel.text = @"Turn Passcode Off";
134         } else {
135             cell.textLabel.text = @"Turn Passcode On";
136         }
137         cell.textLabel.textColor = [UIColor blackColor];
138         cell.textLabel.textAlignment = UITextAlignmentCenter;
139         cell.accessoryView = nil;
140         cell.selectionStyle = UITableViewCellSelectionStyleBlue;
141     } else if (indexPath.section == 1) {
142         cell.textLabel.text = @"Change Passcode";
143         if (passcodeLockOn) {
144             cell.textLabel.textColor = [UIColor blackColor];
145             cell.selectionStyle = UITableViewCellSelectionStyleBlue;
146         } else {
147             cell.textLabel.textColor = [UIColor grayColor];
148             cell.selectionStyle = UITableViewCellSelectionStyleNone;
149         }
150         cell.textLabel.textAlignment = UITextAlignmentCenter;
151         cell.accessoryView = nil;
152     } else if (indexPath.section == 2) {
153         cell.textLabel.text = @"Simple Passcode";
154         cell.textLabel.textColor = [UIColor blackColor];
155         cell.textLabel.textAlignment = UITextAlignmentLeft;
156         cell.accessoryView = simplePasscodeSwitch;
157         cell.selectionStyle = UITableViewCellSelectionStyleNone;
158         if (passcodeLockOn) {
159             cell.textLabel.textColor = [UIColor grayColor];
160             simplePasscodeSwitch.enabled = NO;
161         } else {
162             cell.textLabel.textColor = [UIColor blackColor];
163             simplePasscodeSwitch.enabled = YES;
164         }
165     } else if (indexPath.section == 3) {
166         cell.textLabel.text = @"Erase Data";
167         cell.textLabel.textAlignment = UITextAlignmentLeft;
168         cell.accessoryView = eraseDataSwitch;
169         cell.selectionStyle = UITableViewCellSelectionStyleNone;
170         if (passcodeLockOn) {
171             cell.textLabel.textColor = [UIColor blackColor];
172             eraseDataSwitch.enabled = YES;
173         } else {
174             cell.textLabel.textColor = [UIColor grayColor];
175             eraseDataSwitch.enabled = NO;
176         }
177     }
178     
179     return cell;
180 }
181
182 #pragma mark -
183 #pragma mark Table view delegate
184
185 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
186     // Navigation logic may go here. Create and push another view controller.
187     if (indexPath.section == 0) {
188         PasscodeViewController *vc = [[PasscodeViewController alloc] initWithNibName:@"PasscodeViewController" bundle:nil];
189         if (passcodeLockOn) {
190             vc.mode = kModeDisablePasscode;
191         } else {
192             vc.mode = kModeSetPasscode;
193         }
194         vc.settingsViewController = self.settingsViewController;
195         vc.passcodeLockViewController = self;
196         if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
197             vc.modalPresentationStyle = UIModalPresentationFormSheet;
198         }                
199         [self presentModalViewControllerWithNavigation:vc];
200         [vc release];
201     } else if (indexPath.section == 1 && passcodeLockOn) {
202         PasscodeViewController *vc = [[PasscodeViewController alloc] initWithNibName:@"PasscodeViewController" bundle:nil];
203         vc.mode = kModeChangePasscode;
204         vc.settingsViewController = self.settingsViewController;
205         vc.passcodeLockViewController = self;
206         if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
207             vc.modalPresentationStyle = UIModalPresentationFormSheet;
208         }                
209         [self presentModalViewControllerWithNavigation:vc];
210         [vc release];
211     }
212 }
213
214
215 #pragma mark -
216 #pragma mark Memory management
217
218 - (void)didReceiveMemoryWarning {
219     // Releases the view if it doesn't have a superview.
220     [super didReceiveMemoryWarning];
221     
222     // Relinquish ownership any cached data, images, etc that aren't in use.
223 }
224
225 - (void)viewDidUnload {
226     // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
227     // For example: self.myOutlet = nil;
228 }
229
230
231 - (void)dealloc {
232     [simplePasscodeSwitch release];
233     [eraseDataSwitch release];
234     [settingsViewController release];
235     [super dealloc];
236 }
237
238
239 @end
240