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 // avoiding screen edges only applies to iPhone
86 return IsPad() ? 2 : 3;
87 case SECTION_CERTIFICATE_HANDLING_SETTINGS: // certificate handling settings
88 return 2;
89 default:
90 break;
91 }
92
93 return 0;
94}
95
96// set section headers
97- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
98{
99 switch (section)
100 {
101 case SECTION_UI_SETTINGS:
102 return NSLocalizedString(@"User Interface", @"UI settings section title");
103 case SECTION_CERTIFICATE_HANDLING_SETTINGS:
104 return NSLocalizedString(@"Server Certificate Handling",
105 @"Server Certificate Handling section title");
106 default:
107 return nil;
108 }
109 return @"unknown";
110}
111
112// Customize the appearance of table view cells.
113- (UITableViewCell *)tableView:(UITableView *)tableView
114 cellForRowAtIndexPath:(NSIndexPath *)indexPath
115{
116
117 // determine the required cell type
118 NSString *cellIdentifier = nil;
119 switch ([indexPath section])
120 {
121 case SECTION_UI_SETTINGS:
122 {
123 switch ([indexPath row])
124 {
125 case 0:
126 case 1:
127 case 2:
128 cellIdentifier = TableCellIdentifierYesNo;
129 break;
130 }
131 break;
132 }
133 case SECTION_CERTIFICATE_HANDLING_SETTINGS:
134 {
135 switch ([indexPath row])
136 {
137 case 0:
138 cellIdentifier = TableCellIdentifierYesNo;
139 break;
140 case 1:
141 cellIdentifier = TableCellIdentifierSubEditor;
142 break;
143 }
144 break;
145 }
146 }
147 NSAssert(cellIdentifier != nil, @"Couldn't determine cell type");
148
149 // get the table view cell
150 UITableViewCell *cell = [self tableViewCellFromIdentifier:cellIdentifier];
151 NSAssert(cell, @"Invalid cell");
152
153 // set cell values
154 switch ([indexPath section])
155 {
156 case SECTION_UI_SETTINGS:
157 [self initUISettings:indexPath cell:cell];
158 break;
159
160 case SECTION_CERTIFICATE_HANDLING_SETTINGS:
161 [self initCertificateHandlingSettings:indexPath cell:cell];
162 break;
163
164 default:
165 break;
166 }
167
168 return cell;
169}
170
171#pragma mark - Initialization helpers
172
173// updates UI settings in the UI
174- (void)initUISettings:(NSIndexPath *)indexPath cell:(UITableViewCell *)cell
175{
176 switch ([indexPath row])
177 {
178 case 0:
179 {
181 [flagCell
182 setTitle:NSLocalizedString(@"Swap Mouse Buttons", "Swap Mouse Button UI setting")];
183 [[flagCell toggle] setTag:GET_TAG_FROM_PATH(indexPath)];
184 [[flagCell toggle]
185 setOn:[[NSUserDefaults standardUserDefaults] boolForKey:@"ui.swap_mouse_buttons"]];
186 [[flagCell toggle] addTarget:self
187 action:@selector(toggleSettingValue:)
188 forControlEvents:UIControlEventValueChanged];
189 break;
190 }
191 case 1:
192 {
194 [flagCell
195 setTitle:NSLocalizedString(@"Invert Scrolling", "Invert Scrolling UI setting")];
196 [[flagCell toggle] setTag:GET_TAG_FROM_PATH(indexPath)];
197 [[flagCell toggle]
198 setOn:[[NSUserDefaults standardUserDefaults] boolForKey:@"ui.invert_scrolling"]];
199 [[flagCell toggle] addTarget:self
200 action:@selector(toggleSettingValue:)
201 forControlEvents:UIControlEventValueChanged];
202 break;
203 }
204 case 2:
205 {
207 [flagCell
208 setTitle:NSLocalizedString(@"Avoid Screen Edges", "Avoid Screen Edges UI setting")];
209 [[flagCell toggle] setTag:GET_TAG_FROM_PATH(indexPath)];
210 [[flagCell toggle]
211 setOn:[[NSUserDefaults standardUserDefaults] boolForKey:@"ui.avoid_screen_edges"]];
212 [[flagCell toggle] addTarget:self
213 action:@selector(toggleSettingValue:)
214 forControlEvents:UIControlEventValueChanged];
215 break;
216 }
217 default:
218 NSLog(@"Invalid row index in settings table!");
219 break;
220 }
221}
222
223// updates certificate handling settings in the UI
224- (void)initCertificateHandlingSettings:(NSIndexPath *)indexPath cell:(UITableViewCell *)cell
225{
226 switch ([indexPath row])
227 {
228 case 0:
229 {
231 [flagCell setTitle:NSLocalizedString(@"Accept all Certificates",
232 "Accept All Certificates setting")];
233 [[flagCell toggle] setTag:GET_TAG_FROM_PATH(indexPath)];
234 [[flagCell toggle] setOn:[[NSUserDefaults standardUserDefaults]
235 boolForKey:@"security.accept_certificates"]];
236 [[flagCell toggle] addTarget:self
237 action:@selector(toggleSettingValue:)
238 forControlEvents:UIControlEventValueChanged];
239 break;
240 }
241 case 1:
242 {
244 [subCell setTitle:NSLocalizedString(@"Erase Certificate Cache",
245 @"Erase certificate cache button")];
246 break;
247 }
248 default:
249 NSLog(@"Invalid row index in settings table!");
250 break;
251 }
252}
253
254#pragma mark -
255#pragma mark Table view delegate
256
257- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
258{
259
260 // deselect any row to fake a button-pressed like effect
261 [tableView deselectRowAtIndexPath:indexPath animated:YES];
262
263 // ensure everything is stored in our settings before we proceed
264 [[self view] endEditing:NO];
265
266 // clear certificate cache
267 if ([indexPath section] == SECTION_CERTIFICATE_HANDLING_SETTINGS && [indexPath row] == 1)
268 {
269 BOOL cleared = YES;
270 // get config path using freerdp api
271 char *config = freerdp_settings_get_config_path();
272 if (config)
273 {
274 NSString *configPath = [NSString stringWithUTF8String:config];
275 NSFileManager *fileManager = [NSFileManager defaultManager];
276 NSString *path = [configPath stringByAppendingPathComponent:@"server"];
277 if ([fileManager fileExistsAtPath:path] && ![fileManager removeItemAtPath:path
278 error:nil])
279 cleared = NO;
280
281 free(config);
282 }
283 else
284 {
285 cleared = NO;
286 }
287
288 if (cleared)
289 [[self view] makeToast:NSLocalizedString(@"Certificate Cache cleared!",
290 @"Clear Certificate cache success message")
291 duration:ToastDurationNormal
292 position:@"center"];
293 else
294 [[self view] makeToast:NSLocalizedString(@"Error clearing the Certificate Cache!",
295 @"Clear Certificate cache failed message")
296 duration:ToastDurationNormal
297 position:@"center"];
298 }
299}
300
301#pragma mark -
302#pragma mark Action Handlers
303
304- (void)toggleSettingValue:(id)sender
305{
306 UISwitch *valueSwitch = (UISwitch *)sender;
307 switch ([valueSwitch tag])
308 {
309 case GET_TAG(SECTION_UI_SETTINGS, 0):
310 [[NSUserDefaults standardUserDefaults] setBool:[valueSwitch isOn]
311 forKey:@"ui.swap_mouse_buttons"];
312 SetSwapMouseButtonsFlag([valueSwitch isOn]);
313 break;
314
315 case GET_TAG(SECTION_UI_SETTINGS, 1):
316 [[NSUserDefaults standardUserDefaults] setBool:[valueSwitch isOn]
317 forKey:@"ui.invert_scrolling"];
318 SetInvertScrollingFlag([valueSwitch isOn]);
319 break;
320
321 case GET_TAG(SECTION_UI_SETTINGS, 2):
322 [[NSUserDefaults standardUserDefaults] setBool:[valueSwitch isOn]
323 forKey:@"ui.avoid_screen_edges"];
324 break;
325
326 case GET_TAG(SECTION_CERTIFICATE_HANDLING_SETTINGS, 0):
327 [[NSUserDefaults standardUserDefaults] setBool:[valueSwitch isOn]
328 forKey:@"security.accept_certificates"];
329 break;
330
331 default:
332 break;
333 }
334}
335
336@end
WINPR_ATTR_NODISCARD FREERDP_API char * freerdp_settings_get_config_path(void)
return the configuration directory for the library