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