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 label] setText: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 label]
194 setText:NSLocalizedString(@"Hide Tool Bar", "Show/Hide Tool Bar setting")];
195 [[flagCell toggle] setTag:GET_TAG_FROM_PATH(indexPath)];
196 [[flagCell toggle]
197 setOn:[[NSUserDefaults standardUserDefaults] boolForKey:@"ui.hide_tool_bar"]];
198 [[flagCell toggle] addTarget:self
199 action:@selector(toggleSettingValue:)
200 forControlEvents:UIControlEventValueChanged];
201 break;
202 }
203 case 2:
204 {
206 [[flagCell label]
207 setText:NSLocalizedString(@"Swap Mouse Buttons", "Swap Mouse Button UI setting")];
208 [[flagCell toggle] setTag:GET_TAG_FROM_PATH(indexPath)];
209 [[flagCell toggle]
210 setOn:[[NSUserDefaults standardUserDefaults] boolForKey:@"ui.swap_mouse_buttons"]];
211 [[flagCell toggle] addTarget:self
212 action:@selector(toggleSettingValue:)
213 forControlEvents:UIControlEventValueChanged];
214 break;
215 }
216 case 3:
217 {
219 [[flagCell label]
220 setText:NSLocalizedString(@"Invert Scrolling", "Invert Scrolling UI setting")];
221 [[flagCell toggle] setTag:GET_TAG_FROM_PATH(indexPath)];
222 [[flagCell toggle]
223 setOn:[[NSUserDefaults standardUserDefaults] boolForKey:@"ui.invert_scrolling"]];
224 [[flagCell toggle] addTarget:self
225 action:@selector(toggleSettingValue:)
226 forControlEvents:UIControlEventValueChanged];
227 break;
228 }
229 default:
230 NSLog(@"Invalid row index in settings table!");
231 break;
232 }
233}
234
235// updates certificate handling settings in the UI
236- (void)initCertificateHandlingSettings:(NSIndexPath *)indexPath cell:(UITableViewCell *)cell
237{
238 switch ([indexPath row])
239 {
240 case 0:
241 {
243 [[flagCell label] setText:NSLocalizedString(@"Accept all Certificates",
244 "Accept All Certificates setting")];
245 [[flagCell toggle] setTag:GET_TAG_FROM_PATH(indexPath)];
246 [[flagCell toggle] setOn:[[NSUserDefaults standardUserDefaults]
247 boolForKey:@"security.accept_certificates"]];
248 [[flagCell toggle] addTarget:self
249 action:@selector(toggleSettingValue:)
250 forControlEvents:UIControlEventValueChanged];
251 break;
252 }
253 case 1:
254 {
256 [[subCell label] setText:NSLocalizedString(@"Erase Certificate Cache",
257 @"Erase certificate cache button")];
258 break;
259 }
260 default:
261 NSLog(@"Invalid row index in settings table!");
262 break;
263 }
264}
265
266#pragma mark -
267#pragma mark Table view delegate
268
269- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
270{
271
272 // deselect any row to fake a button-pressed like effect
273 [tableView deselectRowAtIndexPath:indexPath animated:YES];
274
275 // ensure everything is stored in our settings before we proceed
276 [[self view] endEditing:NO];
277
278 // clear certificate cache
279 if ([indexPath section] == SECTION_CERTIFICATE_HANDLING_SETTINGS && [indexPath row] == 1)
280 {
281 // delete certificates cache
282 NSError *err;
283 if ([[NSFileManager defaultManager]
284 removeItemAtPath:[[NSSearchPathForDirectoriesInDomains(
285 NSDocumentDirectory, NSUserDomainMask, YES) lastObject]
286 stringByAppendingPathComponent:@"/.freerdp"]
287 error:&err])
288 [[self view] makeToast:NSLocalizedString(@"Certificate Cache cleared!",
289 @"Clear Certificate cache success message")
290 duration:ToastDurationNormal
291 position:@"center"];
292 else
293 [[self view] makeToast:NSLocalizedString(@"Error clearing the Certificate Cache!",
294 @"Clear Certificate cache failed message")
295 duration:ToastDurationNormal
296 position:@"center"];
297 }
298}
299
300#pragma mark -
301#pragma mark Action Handlers
302
303- (void)toggleSettingValue:(id)sender
304{
305 UISwitch *valueSwitch = (UISwitch *)sender;
306 switch ([valueSwitch tag])
307 {
308 case GET_TAG(SECTION_UI_SETTINGS, 0):
309 [[NSUserDefaults standardUserDefaults] setBool:[valueSwitch isOn]
310 forKey:@"ui.hide_status_bar"];
311 break;
312
313 case GET_TAG(SECTION_UI_SETTINGS, 1):
314 [[NSUserDefaults standardUserDefaults] setBool:[valueSwitch isOn]
315 forKey:@"ui.hide_tool_bar"];
316 break;
317
318 case GET_TAG(SECTION_UI_SETTINGS, 2):
319 [[NSUserDefaults standardUserDefaults] setBool:[valueSwitch isOn]
320 forKey:@"ui.swap_mouse_buttons"];
321 SetSwapMouseButtonsFlag([valueSwitch isOn]);
322 break;
323
324 case GET_TAG(SECTION_UI_SETTINGS, 3):
325 [[NSUserDefaults standardUserDefaults] setBool:[valueSwitch isOn]
326 forKey:@"ui.invert_scrolling"];
327 SetInvertScrollingFlag([valueSwitch isOn]);
328 break;
329
330 case GET_TAG(SECTION_CERTIFICATE_HANDLING_SETTINGS, 0):
331 [[NSUserDefaults standardUserDefaults] setBool:[valueSwitch isOn]
332 forKey:@"security.accept_certificates"];
333 break;
334
335 default:
336 break;
337 }
338}
339
340@end