Statistics
| Branch: | Tag: | Revision:

root / Classes / SettingsViewController.m @ 3a8071d4

History | View | Annotate | Download (8.3 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
        NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
130
        cell.textLabel.text = [NSString stringWithFormat:@"About This App (v%@ %@)", 
131
                               [infoDictionary objectForKey:@"CFBundleShortVersionString"], 
132
                               [infoDictionary objectForKey:@"CFBundleVersion"]];
133
        cell.detailTextLabel.text = @"";
134
        cell.accessoryType = UITableViewCellAccessoryNone;
135
    } else {
136
        id <SettingsPlugin> plugin = [[SettingsPluginHandler plugins] objectAtIndex:indexPath.section - 1];
137
        [plugin setSettingsViewController:self];
138
        [plugin setNavigationController:self.navigationController];
139
        return [plugin tableView:tableView cellForRowAtIndexPath:indexPath];
140
    }
141
    
142
    return cell;
143
}
144

    
145
#pragma mark -
146
#pragma mark Table view delegate
147

    
148
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
149
    if (indexPath.section == kPasscodeLock) {
150
        PasscodeLockViewController *vc = [[PasscodeLockViewController alloc] initWithNibName:@"PasscodeLockViewController" bundle:nil];
151
        vc.settingsViewController = self;
152
        [self.navigationController pushViewController:vc animated:YES];
153
        [vc release];
154
    } else if (indexPath.section == kAPILogs) {
155
        APILoggingSettingsController *vc = [[APILoggingSettingsController alloc] initWithNibName:@"APILoggingSettingsController" bundle:nil];
156
        vc.settingsViewController = self;
157
        [self.navigationController pushViewController:vc animated:YES];
158
        [vc release];
159
    } else if (indexPath.section == cacheSection) {
160
        OpenStackAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
161
        NSFileManager *fileManager = [NSFileManager defaultManager];
162
        NSError *error = nil;
163
        NSString *file;
164
        NSDirectoryEnumerator *directoryEnumerator = [fileManager enumeratorAtPath:appDelegate.cacheDirectoryPath];
165
        @synchronized(appDelegate.cachedObjectsDictionary) {
166
            while (file = [directoryEnumerator nextObject]) {
167
                [fileManager removeItemAtPath:[NSString stringWithFormat:@"%@/%@",appDelegate.cacheDirectoryPath,file] error:&error];
168
                if (error) {
169
                    [self alert:@"Error" message:[NSString stringWithFormat:@"Error in removing cached file, %@", error.localizedDescription]];
170
                } else {
171
                    for (NSString *key in [appDelegate.cachedObjectsDictionary allKeys])
172
                        if ([[appDelegate.cachedObjectsDictionary objectForKey:key] isEqualToString:file])
173
                            [appDelegate.cachedObjectsDictionary removeObjectForKey:key];
174
                }        
175
            }
176
        }
177
        [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
178
        [self.tableView reloadData];
179
    } else if (indexPath.section == aboutSection) {
180
        //This currently is a link to the pithos docs page. It might change in the future to use the AboutViewController;
181
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://pithos.okeanos.grnet.gr"]];
182
        [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
183
        /*AboutViewController *vc = [[AboutViewController alloc] initWithNibName:@"AboutViewController" bundle:nil];
184
        [self.navigationController pushViewController:vc animated:YES];
185
        [vc release];*/
186
    }  else {
187
        id <SettingsPlugin> plugin = [[SettingsPluginHandler plugins] objectAtIndex:indexPath.section - 1];
188
        return [plugin tableView:tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath];
189
    }
190
}
191

    
192
#pragma mark -
193
#pragma mark Memory management
194

    
195
- (void)dealloc {
196
    [super dealloc];
197
}
198

    
199
@end