root / Classes / SettingsViewController.m @ 54fd5c36
History | View | Annotate | Download (8 kB)
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 "APILoggingSettingsController.h" |
14 |
#import "SettingsPluginHandler.h" |
15 |
#import "SettingsPlugin.h" |
16 |
#import "AboutViewController.h" |
17 |
#import "OpenStackAppDelegate.h" |
18 |
#import "PithosUtilities.h" |
19 |
|
20 |
#define kPasscodeLock 0 |
21 |
|
22 |
#define kAPILogs -1 |
23 |
|
24 |
@implementation SettingsViewController |
25 |
|
26 |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { |
27 |
return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == UIInterfaceOrientationPortrait); |
28 |
} |
29 |
|
30 |
- (void)doneButtonPressed:(id)sender { |
31 |
[self dismissModalViewControllerAnimated:YES]; |
32 |
} |
33 |
|
34 |
#pragma mark - |
35 |
#pragma mark View lifecycle |
36 |
|
37 |
- (void)viewDidLoad { |
38 |
[super viewDidLoad]; |
39 |
self.navigationItem.title = @"Settings"; |
40 |
|
41 |
//aboutSection = 1 + [[SettingsPluginHandler plugins] count]; |
42 |
cacheSection = 1; |
43 |
aboutSection = 2; |
44 |
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)]; |
45 |
self.navigationItem.rightBarButtonItem = doneButton; |
46 |
[doneButton release]; |
47 |
} |
48 |
|
49 |
#pragma mark - |
50 |
#pragma mark Table view data source |
51 |
|
52 |
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { |
53 |
if (section > kPasscodeLock && section < cacheSection) { |
54 |
id <SettingsPlugin> plugin = [[SettingsPluginHandler plugins] objectAtIndex:section - 1]; |
55 |
return [plugin tableView:tableView titleForFooterInSection:section]; |
56 |
} else { |
57 |
return @""; |
58 |
} |
59 |
} |
60 |
|
61 |
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { |
62 |
//return 2 + [[SettingsPluginHandler plugins] count]; |
63 |
return 3; |
64 |
} |
65 |
|
66 |
|
67 |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
68 |
// Return the number of rows in the section. |
69 |
if (section == kPasscodeLock) { |
70 |
return 1; |
71 |
} else if (section == kAPILogs) { |
72 |
return 1; |
73 |
} else { |
74 |
return 1; |
75 |
} |
76 |
/*} else { |
77 |
id <SettingsPlugin> plugin = [[SettingsPluginHandler plugins] objectAtIndex:section - 1]; |
78 |
return [plugin tableView:tableView numberOfRowsInSection:section]; |
79 |
}*/ |
80 |
} |
81 |
|
82 |
|
83 |
// Customize the appearance of table view cells. |
84 |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
85 |
|
86 |
static NSString *CellIdentifier = @"Cell"; |
87 |
|
88 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
89 |
if (cell == nil) { |
90 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; |
91 |
} |
92 |
cell.userInteractionEnabled = YES; |
93 |
cell.textLabel.textColor = [UIColor blackColor]; |
94 |
|
95 |
// Configure the cell... |
96 |
if (indexPath.section == kPasscodeLock) { |
97 |
cell.textLabel.text = @"Passcode Lock"; |
98 |
if ([[Keychain getStringForKey:@"passcode_lock_passcode_on"] isEqualToString:@"YES"]) { |
99 |
cell.detailTextLabel.text = @"On"; |
100 |
} else { |
101 |
cell.detailTextLabel.text = @"Off"; |
102 |
} |
103 |
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
104 |
} else if (indexPath.section == kAPILogs) { |
105 |
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; |
106 |
NSString *loggingLevel = [defaults valueForKey:@"api_logging_level"]; |
107 |
|
108 |
cell.textLabel.text = @"API Logging"; |
109 |
if ([loggingLevel isEqualToString:@"all"]) { |
110 |
cell.detailTextLabel.text = @"All"; |
111 |
} else if ([loggingLevel isEqualToString:@"errors"]) { |
112 |
cell.detailTextLabel.text = @"Only Errors"; |
113 |
} else if ([loggingLevel isEqualToString:@"none"]) { |
114 |
cell.detailTextLabel.text = @"None"; |
115 |
} |
116 |
|
117 |
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
118 |
} else if (indexPath.section == cacheSection) { |
119 |
OpenStackAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; |
120 |
NSString *cacheSize = [PithosUtilities humanReadableSize:[PithosUtilities sizeOfDirectory:appDelegate.cacheDirectoryPath]]; |
121 |
if ([cacheSize hasPrefix:@"0"]) { |
122 |
cell.textLabel.textColor = [UIColor grayColor]; |
123 |
cell.userInteractionEnabled = NO; |
124 |
} |
125 |
cell.textLabel.text = [NSString stringWithFormat:@"Clear Cache (%@)", cacheSize]; |
126 |
cell.detailTextLabel.text = @""; |
127 |
cell.accessoryType = UITableViewCellAccessoryNone; |
128 |
} else if (indexPath.section == aboutSection) { |
129 |
cell.textLabel.text = @"About This App"; |
130 |
cell.detailTextLabel.text = @""; |
131 |
cell.accessoryType = UITableViewCellAccessoryNone; |
132 |
} else { |
133 |
id <SettingsPlugin> plugin = [[SettingsPluginHandler plugins] objectAtIndex:indexPath.section - 1]; |
134 |
[plugin setSettingsViewController:self]; |
135 |
[plugin setNavigationController:self.navigationController]; |
136 |
return [plugin tableView:tableView cellForRowAtIndexPath:indexPath]; |
137 |
} |
138 |
|
139 |
return cell; |
140 |
} |
141 |
|
142 |
#pragma mark - |
143 |
#pragma mark Table view delegate |
144 |
|
145 |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
146 |
if (indexPath.section == kPasscodeLock) { |
147 |
PasscodeLockViewController *vc = [[PasscodeLockViewController alloc] initWithNibName:@"PasscodeLockViewController" bundle:nil]; |
148 |
vc.settingsViewController = self; |
149 |
[self.navigationController pushViewController:vc animated:YES]; |
150 |
[vc release]; |
151 |
} else if (indexPath.section == kAPILogs) { |
152 |
APILoggingSettingsController *vc = [[APILoggingSettingsController alloc] initWithNibName:@"APILoggingSettingsController" bundle:nil]; |
153 |
vc.settingsViewController = self; |
154 |
[self.navigationController pushViewController:vc animated:YES]; |
155 |
[vc release]; |
156 |
} else if (indexPath.section == cacheSection) { |
157 |
OpenStackAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; |
158 |
NSFileManager *fileManager = [NSFileManager defaultManager]; |
159 |
NSError *error = nil; |
160 |
NSString *file; |
161 |
NSDirectoryEnumerator *directoryEnumerator = [fileManager enumeratorAtPath:appDelegate.cacheDirectoryPath]; |
162 |
@synchronized(appDelegate.cachedObjectsDictionary) { |
163 |
while (file = [directoryEnumerator nextObject]) { |
164 |
[fileManager removeItemAtPath:[NSString stringWithFormat:@"%@/%@",appDelegate.cacheDirectoryPath,file] error:&error]; |
165 |
if (error) { |
166 |
[self alert:@"Error" message:[NSString stringWithFormat:@"Error in removing cached file, %@", error.localizedDescription]]; |
167 |
} else { |
168 |
for (NSString *key in [appDelegate.cachedObjectsDictionary allKeys]) |
169 |
if ([[appDelegate.cachedObjectsDictionary objectForKey:key] isEqualToString:file]) |
170 |
[appDelegate.cachedObjectsDictionary removeObjectForKey:key]; |
171 |
} |
172 |
} |
173 |
} |
174 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
175 |
[self.tableView reloadData]; |
176 |
} else if (indexPath.section == aboutSection) { |
177 |
//This currently is a link to the pithos docs page. It might change in the future to use the AboutViewController; |
178 |
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://pithos.okeanos.grnet.gr"]]; |
179 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
180 |
/*AboutViewController *vc = [[AboutViewController alloc] initWithNibName:@"AboutViewController" bundle:nil]; |
181 |
[self.navigationController pushViewController:vc animated:YES]; |
182 |
[vc release];*/ |
183 |
} else { |
184 |
id <SettingsPlugin> plugin = [[SettingsPluginHandler plugins] objectAtIndex:indexPath.section - 1]; |
185 |
return [plugin tableView:tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath]; |
186 |
} |
187 |
} |
188 |
|
189 |
#pragma mark - |
190 |
#pragma mark Memory management |
191 |
|
192 |
- (void)dealloc { |
193 |
[super dealloc]; |
194 |
} |
195 |
|
196 |
@end |