2 // SettingsViewController.m
5 // Created by Mike Mayo on 10/26/10.
6 // The OpenStack project is provided under the Apache 2.0 license.
9 #import "SettingsViewController.h"
10 #import "UIViewController+Conveniences.h"
11 #import "PasscodeLockViewController.h"
13 #import "AboutViewController.h"
14 #import "OpenStackAppDelegate.h"
15 #import "PithosUtilities.h"
17 #define kPasscodeLock 0
21 @implementation SettingsViewController
23 #pragma mark - View lifecycle
25 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
26 return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == UIInterfaceOrientationPortrait);
31 self.navigationItem.title = @"Settings";
35 #pragma mark - UITableViewDataSource
37 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
41 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
45 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
46 static NSString *CellIdentifier = @"Cell";
48 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
50 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
52 cell.userInteractionEnabled = YES;
53 cell.textLabel.textColor = [UIColor blackColor];
55 // Configure the cell...
56 if (indexPath.section == kPasscodeLock) {
57 cell.textLabel.text = @"Passcode Lock";
58 if ([[Keychain getStringForKey:@"passcode_lock_passcode_on"] isEqualToString:@"YES"]) {
59 cell.detailTextLabel.text = @"On";
61 cell.detailTextLabel.text = @"Off";
63 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
64 } else if (indexPath.section == kCache) {
65 OpenStackAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
66 NSString *cacheSize = [PithosUtilities humanReadableSize:[PithosUtilities sizeOfDirectory:appDelegate.cacheDirectoryPath]];
67 if ([cacheSize hasPrefix:@"0"]) {
68 cell.textLabel.textColor = [UIColor grayColor];
69 cell.userInteractionEnabled = NO;
71 cell.textLabel.text = [NSString stringWithFormat:@"Clear Cache (%@)", cacheSize];
72 cell.detailTextLabel.text = @"";
73 cell.accessoryType = UITableViewCellAccessoryNone;
74 } else if (indexPath.section == kAbout) {
75 NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
76 cell.textLabel.text = [NSString stringWithFormat:@"About This App (v%@ %@)",
77 [infoDictionary objectForKey:@"CFBundleShortVersionString"],
78 [infoDictionary objectForKey:@"CFBundleVersion"]];
79 cell.detailTextLabel.text = @"";
80 cell.accessoryType = UITableViewCellAccessoryNone;
86 #pragma mark - UITableViewDelegate
88 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
89 if (indexPath.section == kPasscodeLock) {
90 PasscodeLockViewController *vc = [[PasscodeLockViewController alloc] initWithNibName:@"PasscodeLockViewController" bundle:nil];
91 vc.settingsViewController = self;
92 [self.navigationController pushViewController:vc animated:YES];
94 } else if (indexPath.section == kCache) {
95 OpenStackAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
96 NSFileManager *fileManager = [NSFileManager defaultManager];
99 NSDirectoryEnumerator *directoryEnumerator = [fileManager enumeratorAtPath:appDelegate.cacheDirectoryPath];
100 @synchronized(appDelegate.cachedObjectsDictionary) {
101 while (file = [directoryEnumerator nextObject]) {
102 [fileManager removeItemAtPath:[NSString stringWithFormat:@"%@/%@",appDelegate.cacheDirectoryPath,file] error:&error];
104 [self alert:@"Error" message:[NSString stringWithFormat:@"Error in removing cached file, %@", error.localizedDescription]];
106 for (NSString *key in [appDelegate.cachedObjectsDictionary allKeys])
107 if ([[appDelegate.cachedObjectsDictionary objectForKey:key] isEqualToString:file])
108 [appDelegate.cachedObjectsDictionary removeObjectForKey:key];
112 [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
113 [self.tableView reloadData];
114 } else if (indexPath.section == kAbout) {
115 // This currently is a link to the pithos docs page. It might change in the future to use the AboutViewController
116 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://pithos.okeanos.grnet.gr"]];
117 [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
118 /*AboutViewController *vc = [[AboutViewController alloc] initWithNibName:@"AboutViewController" bundle:nil];
119 [self.navigationController pushViewController:vc animated:YES];
124 #pragma mark - Button Handlers
126 - (void)doneButtonPressed:(id)sender {
127 [self dismissModalViewControllerAnimated:YES];