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