Hide or show keyboard, accordingly, on rotation when folder filter is active
[pithos-ios] / Classes / SettingsViewController.m
1 //
2 //  SettingsViewController.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 "SettingsViewController.h"
10 #import "UIViewController+Conveniences.h"
11 #import "PasscodeLockViewController.h"
12 #import "Keychain.h"
13 #import "AboutViewController.h"
14 #import "OpenStackAppDelegate.h"
15 #import "PithosUtilities.h"
16
17 #define kPasscodeLock 0
18 #define kCache 1
19 #define kAbout 2
20
21 @implementation SettingsViewController
22
23 #pragma mark - View lifecycle
24
25 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
26     return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == UIInterfaceOrientationPortrait);
27 }
28
29 - (void)viewDidLoad {
30     [super viewDidLoad];
31     self.navigationItem.title = @"Settings";
32     [self addDoneButton];
33 }
34
35 #pragma mark - UITableViewDataSource
36
37 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
38     return 3;
39 }
40
41 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
42     return 1;
43 }
44
45 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
46     static NSString *CellIdentifier = @"Cell";
47     
48     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
49     if (cell == nil) {
50         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
51     }
52     cell.userInteractionEnabled = YES;
53     cell.textLabel.textColor = [UIColor blackColor];
54     
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";
60         } else {
61             cell.detailTextLabel.text = @"Off";
62         }
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;
70         }
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;
81     }
82     
83     return cell;
84 }
85
86 #pragma mark - UITableViewDelegate
87
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];
93         [vc release];
94     } else if (indexPath.section == kCache) {
95         OpenStackAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
96         NSFileManager *fileManager = [NSFileManager defaultManager];
97         NSError *error = nil;
98         NSString *file;
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];
103                 if (error) {
104                     [self alert:@"Error" message:[NSString stringWithFormat:@"Error in removing cached file, %@", error.localizedDescription]];
105                 } else {
106                     for (NSString *key in [appDelegate.cachedObjectsDictionary allKeys])
107                         if ([[appDelegate.cachedObjectsDictionary objectForKey:key] isEqualToString:file])
108                             [appDelegate.cachedObjectsDictionary removeObjectForKey:key];
109                 }        
110             }
111         }
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];
120         [vc release];*/
121     }
122 }
123
124 #pragma mark - Button Handlers
125
126 - (void)doneButtonPressed:(id)sender {
127     [self dismissModalViewControllerAnimated:YES];
128 }
129
130 @end