FreeRDP
Loading...
Searching...
No Matches
AppSettingsController.m
1/*
2 Controller to specify application wide settings
3
4 Copyright 2013 Thincast Technologies GmbH, Author: Martin Fleisz
5
6 This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
7 If a copy of the MPL was not distributed with this file, You can obtain one at
8 http://mozilla.org/MPL/2.0/.
9 */
10
11#import "AppSettingsController.h"
12#import "Utils.h"
13#import "Toast+UIView.h"
14
15@implementation AppSettingsController
16
17// keep this up-to-date for correct section display/hiding
18#define SECTION_UI_SETTINGS 0
19#define SECTION_CERTIFICATE_HANDLING_SETTINGS 1
20#define SECTION_NUM_SECTIONS 2
21
22#pragma mark -
23#pragma mark Initialization
24
25- (id)initWithStyle:(UITableViewStyle)style
26{
27 if ((self = [super initWithStyle:style]))
28 {
29 UIImage *tabBarIcon = [UIImage
30 imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"tabbar_icon_settings"
31 ofType:@"png"]];
32 [self
33 setTabBarItem:[[[UITabBarItem alloc]
34 initWithTitle:NSLocalizedString(@"Settings", @"Tabbar item settings")
35 image:tabBarIcon
36 tag:0] autorelease]];
37 }
38 return self;
39}
40
41#pragma mark -
42#pragma mark View lifecycle
43
44- (void)viewDidLoad
45{
46 [super viewDidLoad];
47
48 // set title
49 [self setTitle:NSLocalizedString(@"Settings", @"App Settings title")];
50}
51
52- (void)viewWillDisappear:(BOOL)animated
53{
54 [super viewWillDisappear:animated];
55}
56
57// Override to allow orientations other than the default portrait orientation.
58- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
59{
60 return YES;
61}
62
63- (void)dealloc
64{
65 [[NSNotificationCenter defaultCenter] removeObserver:self];
66 [super dealloc];
67}
68
69#pragma mark -
70#pragma mark Table view data source
71
72- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
73{
74 // Return the number of sections.
75 return SECTION_NUM_SECTIONS;
76}
77
78- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
79{
80 // Return the number of rows in the section.
81 switch (section)
82 {
83 case SECTION_UI_SETTINGS: // UI settings
84 return 4;
85 case SECTION_CERTIFICATE_HANDLING_SETTINGS: // certificate handling settings
86 return 2;
87 default:
88 break;
89 }
90
91 return 0;
92}
93
94// set section headers
95- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
96{
97 switch (section)
98 {
99 case SECTION_UI_SETTINGS:
100 return NSLocalizedString(@"User Interface", @"UI settings section title");
101 case SECTION_CERTIFICATE_HANDLING_SETTINGS:
102 return NSLocalizedString(@"Server Certificate Handling",
103 @"Server Certificate Handling section title");
104 default:
105 return nil;
106 }
107 return @"unknown";
108}
109
110// Customize the appearance of table view cells.
111- (UITableViewCell *)tableView:(UITableView *)tableView
112 cellForRowAtIndexPath:(NSIndexPath *)indexPath
113{
114
115 // determine the required cell type
116 NSString *cellIdentifier = nil;
117 switch ([indexPath section])
118 {
119 case SECTION_UI_SETTINGS:
120 {
121 switch ([indexPath row])
122 {
123 case 0:
124 case 1:
125 case 2:
126 case 3:
127 cellIdentifier = TableCellIdentifierYesNo;
128 break;
129 }
130 break;
131 }
132 case SECTION_CERTIFICATE_HANDLING_SETTINGS:
133 {
134 switch ([indexPath row])
135 {
136 case 0:
137 cellIdentifier = TableCellIdentifierYesNo;
138 break;
139 case 1:
140 cellIdentifier = TableCellIdentifierSubEditor;
141 break;
142 }
143 break;
144 }
145 }
146 NSAssert(cellIdentifier != nil, @"Couldn't determine cell type");
147
148 // get the table view cell
149 UITableViewCell *cell = [self tableViewCellFromIdentifier:cellIdentifier];
150 NSAssert(cell, @"Invalid cell");
151
152 // set cell values
153 switch ([indexPath section])
154 {
155 case SECTION_UI_SETTINGS:
156 [self initUISettings:indexPath cell:cell];
157 break;
158
159 case SECTION_CERTIFICATE_HANDLING_SETTINGS:
160 [self initCertificateHandlingSettings:indexPath cell:cell];
161 break;
162
163 default:
164 break;
165 }
166
167 return cell;
168}
169
170#pragma mark - Initialization helpers
171
172// updates UI settings in the UI
173- (void)initUISettings:(NSIndexPath *)indexPath cell:(UITableViewCell *)cell
174{
175 switch ([indexPath row])
176 {
177 case 0:
178 {
180 [flagCell setTitle:NSLocalizedString(@"Hide Status Bar",
181 "Show/Hide Phone Status Bar setting")];
182 [[flagCell toggle] setTag:GET_TAG_FROM_PATH(indexPath)];
183 [[flagCell toggle]
184 setOn:[[NSUserDefaults standardUserDefaults] boolForKey:@"ui.hide_status_bar"]];
185 [[flagCell toggle] addTarget:self
186 action:@selector(toggleSettingValue:)
187 forControlEvents:UIControlEventValueChanged];
188 break;
189 }
190 case 1:
191 {
193 [flagCell setTitle:NSLocalizedString(@"Hide Tool Bar", "Show/Hide Tool Bar setting")];
194 [[flagCell toggle] setTag:GET_TAG_FROM_PATH(indexPath)];
195 [[flagCell toggle]
196 setOn:[[NSUserDefaults standardUserDefaults] boolForKey:@"ui.hide_tool_bar"]];
197 [[flagCell toggle] addTarget:self
198 action:@selector(toggleSettingValue:)
199 forControlEvents:UIControlEventValueChanged];
200 break;
201 }
202 case 2:
203 {
205 [flagCell
206 setTitle:NSLocalizedString(@"Swap Mouse Buttons", "Swap Mouse Button UI setting")];
207 [[flagCell toggle] setTag:GET_TAG_FROM_PATH(indexPath)];
208 [[flagCell toggle]
209 setOn:[[NSUserDefaults standardUserDefaults] boolForKey:@"ui.swap_mouse_buttons"]];
210 [[flagCell toggle] addTarget:self
211 action:@selector(toggleSettingValue:)
212 forControlEvents:UIControlEventValueChanged];
213 break;
214 }
215 case 3:
216 {
218 [flagCell
219 setTitle:NSLocalizedString(@"Invert Scrolling", "Invert Scrolling UI setting")];
220 [[flagCell toggle] setTag:GET_TAG_FROM_PATH(indexPath)];
221 [[flagCell toggle]
222 setOn:[[NSUserDefaults standardUserDefaults] boolForKey:@"ui.invert_scrolling"]];
223 [[flagCell toggle] addTarget:self
224 action:@selector(toggleSettingValue:)
225 forControlEvents:UIControlEventValueChanged];
226 break;
227 }
228 default:
229 NSLog(@"Invalid row index in settings table!");
230 break;
231 }
232}
233
234// updates certificate handling settings in the UI
235- (void)initCertificateHandlingSettings:(NSIndexPath *)indexPath cell:(UITableViewCell *)cell
236{
237 switch ([indexPath row])
238 {
239 case 0:
240 {
242 [flagCell setTitle:NSLocalizedString(@"Accept all Certificates",
243 "Accept All Certificates setting")];
244 [[flagCell toggle] setTag:GET_TAG_FROM_PATH(indexPath)];
245 [[flagCell toggle] setOn:[[NSUserDefaults standardUserDefaults]
246 boolForKey:@"security.accept_certificates"]];
247 [[flagCell toggle] addTarget:self
248 action:@selector(toggleSettingValue:)
249 forControlEvents:UIControlEventValueChanged];
250 break;
251 }
252 case 1:
253 {
255 [subCell setTitle:NSLocalizedString(@"Erase Certificate Cache",
256 @"Erase certificate cache button")];
257 break;
258 }
259 default:
260 NSLog(@"Invalid row index in settings table!");
261 break;
262 }
263}
264
265#pragma mark -
266#pragma mark Table view delegate
267
268- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
269{
270
271 // deselect any row to fake a button-pressed like effect
272 [tableView deselectRowAtIndexPath:indexPath animated:YES];
273
274 // ensure everything is stored in our settings before we proceed
275 [[self view] endEditing:NO];
276
277 // clear certificate cache
278 if ([indexPath section] == SECTION_CERTIFICATE_HANDLING_SETTINGS && [indexPath row] == 1)
279 {
280 // delete certificates cache
281 NSError *err;
282 if ([[NSFileManager defaultManager]
283 removeItemAtPath:[[NSSearchPathForDirectoriesInDomains(
284 NSDocumentDirectory, NSUserDomainMask, YES) lastObject]
285 stringByAppendingPathComponent:@"/.freerdp"]
286 error:&err])
287 [[self view] makeToast:NSLocalizedString(@"Certificate Cache cleared!",
288 @"Clear Certificate cache success message")
289 duration:ToastDurationNormal
290 position:@"center"];
291 else
292 [[self view] makeToast:NSLocalizedString(@"Error clearing the Certificate Cache!",
293 @"Clear Certificate cache failed message")
294 duration:ToastDurationNormal
295 position:@"center"];
296 }
297}
298
299#pragma mark -
300#pragma mark Action Handlers
301
302- (void)toggleSettingValue:(id)sender
303{
304 UISwitch *valueSwitch = (UISwitch *)sender;
305 switch ([valueSwitch tag])
306 {
307 case GET_TAG(SECTION_UI_SETTINGS, 0):
308 [[NSUserDefaults standardUserDefaults] setBool:[valueSwitch isOn]
309 forKey:@"ui.hide_status_bar"];
310 break;
311
312 case GET_TAG(SECTION_UI_SETTINGS, 1):
313 [[NSUserDefaults standardUserDefaults] setBool:[valueSwitch isOn]
314 forKey:@"ui.hide_tool_bar"];
315 break;
316
317 case GET_TAG(SECTION_UI_SETTINGS, 2):
318 [[NSUserDefaults standardUserDefaults] setBool:[valueSwitch isOn]
319 forKey:@"ui.swap_mouse_buttons"];
320 SetSwapMouseButtonsFlag([valueSwitch isOn]);
321 break;
322
323 case GET_TAG(SECTION_UI_SETTINGS, 3):
324 [[NSUserDefaults standardUserDefaults] setBool:[valueSwitch isOn]
325 forKey:@"ui.invert_scrolling"];
326 SetInvertScrollingFlag([valueSwitch isOn]);
327 break;
328
329 case GET_TAG(SECTION_CERTIFICATE_HANDLING_SETTINGS, 0):
330 [[NSUserDefaults standardUserDefaults] setBool:[valueSwitch isOn]
331 forKey:@"security.accept_certificates"];
332 break;
333
334 default:
335 break;
336 }
337}
338
339@end