FreeRDP
Loading...
Searching...
No Matches
BookmarkEditorController.m
1/*
2 Bookmark editor controller
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 "BookmarkEditorController.h"
12#import "Bookmark.h"
13#import "Utils.h"
14#import "ScreenSelectionController.h"
15#import "PerformanceEditorController.h"
16#import "CredentialsEditorController.h"
17#import "AdvancedBookmarkEditorController.h"
18
19@implementation BookmarkEditorController
20
21@synthesize delegate;
22
23#define SECTION_SERVER 0
24#define SECTION_CREDENTIALS 1
25#define SECTION_SETTINGS 2
26#define SECTION_COUNT 3
27
28#pragma mark -
29#pragma mark Initialization
30
31- (id)initWithBookmark:(ComputerBookmark *)bookmark
32{
33 if ((self = [super initWithStyle:UITableViewStyleInsetGrouped]))
34 {
35 // set additional settings state according to bookmark data
36 if ([[bookmark uuid] length] == 0)
37 _bookmark = [bookmark copy];
38 else
39 _bookmark = [bookmark copyWithUUID];
40 _params = [_bookmark params];
41
42 _display_server_settings = YES;
43 }
44 return self;
45}
46
47#pragma mark -
48#pragma mark View lifecycle
49
50- (void)viewDidLoad
51{
52 [super viewDidLoad];
53
54 // replace back button with a custom handler that checks if the required bookmark settings were
55 // specified
56 UIBarButtonItem *saveButton =
57 [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Save", @"Save Button title")
58 style:UIBarButtonItemStyleDone
59 target:self
60 action:@selector(handleSave:)] autorelease];
61 UIBarButtonItem *cancelButton =
62 [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel", @"Cancel Button title")
63 style:UIBarButtonItemStylePlain
64 target:self
65 action:@selector(handleCancel:)] autorelease];
66 [[self navigationItem] setLeftBarButtonItem:cancelButton];
67 [[self navigationItem] setRightBarButtonItem:saveButton];
68}
69
70- (void)viewWillAppear:(BOOL)animated
71{
72 [super viewWillAppear:animated];
73
74 // we need to reload the table view data here to have up-to-date data for the
75 // advanced settings accessory items (like for resolution/color mode settings)
76 [[self tableView] reloadData];
77}
78
79// Override to allow orientations other than the default portrait orientation.
80- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
81{
82 return YES;
83}
84
85#pragma mark -
86#pragma mark Table view data source
87
88- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
89{
90 // Return the number of sections.
91 return SECTION_COUNT;
92}
93
94- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
95{
96 // Return the number of rows in the section.
97 switch (section)
98 {
99 case SECTION_SERVER: // server settings
100 return (_display_server_settings ? 3 : 0);
101 case SECTION_CREDENTIALS: // credentials
102 return 1;
103 case SECTION_SETTINGS: // session settings
104 return 3;
105 default:
106 break;
107 }
108
109 return 0;
110}
111
112// set section headers
113- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
114{
115 switch (section)
116 {
117 case SECTION_SERVER:
118 return (_display_server_settings
119 ? NSLocalizedString(@"Host", @"'Host': host settings header")
120 : nil);
121 case SECTION_CREDENTIALS:
122 return NSLocalizedString(@"Credentials", @"'Credentials': credentials settings header");
123 case SECTION_SETTINGS:
124 return NSLocalizedString(@"Settings", @"'Session Settings': session settings header");
125 }
126 return @"unknown";
127}
128
129// Customize the appearance of table view cells.
130- (UITableViewCell *)tableView:(UITableView *)tableView
131 cellForRowAtIndexPath:(NSIndexPath *)indexPath
132{
133
134 // determine the required cell type
135 NSString *cellType = nil;
136 switch ([indexPath section])
137 {
138 case SECTION_SERVER:
139 cellType = TableCellIdentifierText;
140 break;
141
142 case SECTION_CREDENTIALS:
143 cellType = TableCellIdentifierSelection;
144 break;
145
146 case SECTION_SETTINGS: // settings
147 {
148 switch ([indexPath row])
149 {
150 case 0: // screen/color depth
151 cellType = TableCellIdentifierSelection;
152 break;
153 case 1: // performance settings
154 case 2: // advanced settings
155 cellType = TableCellIdentifierSubEditor;
156 break;
157 default:
158 break;
159 }
160 }
161 break;
162
163 default:
164 break;
165 }
166 NSAssert(cellType != nil, @"Couldn't determine cell type");
167
168 // get the table view cell
169 UITableViewCell *cell = [self tableViewCellFromIdentifier:cellType];
170 NSAssert(cell, @"Invalid cell");
171
172 // set cell values
173 switch ([indexPath section])
174 {
175 // server settings
176 case SECTION_SERVER:
177 [self initServerSettings:indexPath cell:cell];
178 break;
179
180 // credentials
181 case SECTION_CREDENTIALS:
182 [self initCredentialSettings:indexPath cell:cell];
183 break;
184
185 // session settings
186 case SECTION_SETTINGS:
187 [self initSettings:indexPath cell:cell];
188 break;
189
190 default:
191 break;
192 }
193
194 return cell;
195}
196
197// updates server settings in the UI
198- (void)initServerSettings:(NSIndexPath *)indexPath cell:(UITableViewCell *)cell
199{
201 [[textCell textfield] setTag:GET_TAG_FROM_PATH(indexPath)];
202 switch ([indexPath row])
203 {
204 case 0:
205 [[textCell label] setText:NSLocalizedString(@"Label", @"'Label': Bookmark label")];
206 [[textCell textfield] setText:[_bookmark label]];
207 [[textCell textfield]
208 setPlaceholder:NSLocalizedString(@"not set", @"not set placeholder")];
209 break;
210 case 1:
211 [[textCell label] setText:NSLocalizedString(@"Host", @"'Host': Bookmark hostname")];
212 [[textCell textfield] setText:[_params StringForKey:@"hostname"]];
213 [[textCell textfield]
214 setPlaceholder:NSLocalizedString(@"not set", @"not set placeholder")];
215 break;
216 case 2:
217 [[textCell label] setText:NSLocalizedString(@"Port", @"'Port': Bookmark port")];
218 [[textCell textfield]
219 setText:[NSString stringWithFormat:@"%d", [_params intForKey:@"port"]]];
220 [[textCell textfield] setKeyboardType:UIKeyboardTypeNumberPad];
221 break;
222 default:
223 NSLog(@"Invalid row index in settings table!");
224 break;
225 }
226}
227
228// updates credentials in the UI
229- (void)initCredentialSettings:(NSIndexPath *)indexPath cell:(UITableViewCell *)cell
230{
232 switch (indexPath.row)
233 {
234 case 0:
235 [selCell
236 setTitle:NSLocalizedString(@"Credentials", @"'Credentials': Bookmark credentials")];
237 [selCell setValue:[_params StringForKey:@"username"]];
238 break;
239 default:
240 NSLog(@"Invalid row index in settings table!");
241 break;
242 }
243}
244
245// updates session settings in the UI
246- (void)initSettings:(NSIndexPath *)indexPath cell:(UITableViewCell *)cell
247{
248 switch (indexPath.row)
249 {
250 case 0:
251 {
253 [selCell setTitle:NSLocalizedString(@"Screen", @"'Screen': Bookmark Screen settings")];
254 NSString *resolution = ScreenResolutionDescription(
255 [_params intForKey:@"screen_resolution_type"], [_params intForKey:@"width"],
256 [_params intForKey:@"height"]);
257 int colorBits = [_params intForKey:@"colors"];
258 [selCell setValue:[NSString stringWithFormat:@"%@@%d", resolution, colorBits]];
259 break;
260 }
261 case 1:
262 {
264 [editCell setTitle:NSLocalizedString(@"Performance",
265 @"'Performance': Bookmark Performance Settings")];
266 break;
267 }
268 case 2:
269 {
271 [editCell
272 setTitle:NSLocalizedString(@"Advanced", @"'Advanced': Bookmark Advanced Settings")];
273 break;
274 }
275 default:
276 NSLog(@"Invalid row index in settings table!");
277 break;
278 }
279}
280
281#pragma mark -
282#pragma mark Table view delegate
283
284- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
285{
286 UIViewController *viewCtrl = nil;
287
288 // determine view
289 switch ([indexPath section])
290 {
291 case SECTION_CREDENTIALS:
292 {
293 if ([indexPath row] == 0)
294 viewCtrl =
295 [[[CredentialsEditorController alloc] initWithBookmark:_bookmark] autorelease];
296 break;
297 }
298
299 case SECTION_SETTINGS:
300 {
301 switch ([indexPath row])
302 {
303 case 0:
304 viewCtrl = [[[ScreenSelectionController alloc] initWithConnectionParams:_params]
305 autorelease];
306 break;
307 case 1:
308 viewCtrl = [[[PerformanceEditorController alloc]
309 initWithConnectionParams:_params] autorelease];
310 break;
311 case 2:
312 viewCtrl = [[[AdvancedBookmarkEditorController alloc]
313 initWithBookmark:_bookmark] autorelease];
314 break;
315 default:
316 break;
317 }
318
319 break;
320 }
321 }
322
323 // display view
324 if (viewCtrl)
325 [[self navigationController] pushViewController:viewCtrl animated:YES];
326}
327
328#pragma mark -
329#pragma mark Text Field delegate
330
331- (BOOL)textFieldShouldReturn:(UITextField *)textField
332{
333 [textField resignFirstResponder];
334 return NO;
335}
336
337- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
338{
339 switch (textField.tag)
340 {
341 // update server settings
342 case GET_TAG(SECTION_SERVER, 0):
343 [_bookmark setLabel:[textField text]];
344 break;
345
346 case GET_TAG(SECTION_SERVER, 1):
347 [_params setValue:[textField text] forKey:@"hostname"];
348 break;
349
350 case GET_TAG(SECTION_SERVER, 2):
351 [_params setInt:[[textField text] intValue] forKey:@"port"];
352 break;
353
354 default:
355 break;
356 }
357 return YES;
358}
359
360#pragma mark -
361#pragma mark Action Handlers
362
363- (void)handleSave:(id)sender
364{
365 // resign any first responder (so that we finish editing any bookmark parameter that might be
366 // currently edited)
367 [[self view] endEditing:NO];
368
369 // verify that bookmark is complete (only for manual bookmarks)
370 if ([[_bookmark label] length] == 0 || [[_params StringForKey:@"hostname"] length] == 0 ||
371 [_params intForKey:@"port"] == 0)
372 {
373 UIAlertController *alertView = [UIAlertController
374 alertControllerWithTitle:NSLocalizedString(@"Cancel without saving?",
375 @"Incomplete bookmark error title")
376 message:NSLocalizedString(
377 @"Press 'Cancel' to abort!\nPress 'Continue' to "
378 @"specify the required fields!",
379 @"Incomplete bookmark error message")
380 preferredStyle:UIAlertControllerStyleAlert];
381 [alertView
382 addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel Button")
383 style:UIAlertActionStyleCancel
384 handler:^(UIAlertAction *action) {
385 [[self navigationController]
386 popViewControllerAnimated:YES];
387 }]];
388 [alertView addAction:[UIAlertAction
389 actionWithTitle:NSLocalizedString(@"Continue", @"Continue Button")
390 style:UIAlertActionStyleDefault
391 handler:nil]];
392 [self presentViewController:alertView animated:YES completion:nil];
393 return;
394 }
395
396 // commit bookmark
397 if ([[self delegate] respondsToSelector:@selector(commitBookmark:)])
398 [[self delegate] commitBookmark:_bookmark];
399
400 // return to previous view controller
401 [[self navigationController] popViewControllerAnimated:YES];
402}
403
404- (void)handleCancel:(id)sender
405{
406 // return to previous view controller
407 [[self navigationController] popViewControllerAnimated:YES];
408}
409
410#pragma mark -
411#pragma mark Memory management
412
413- (void)didReceiveMemoryWarning
414{
415 // Releases the view if it doesn't have a superview.
416 [super didReceiveMemoryWarning];
417
418 // Relinquish ownership any cached data, images, etc that aren't in use.
419}
420
421- (void)dealloc
422{
423 [_bookmark autorelease];
424 [super dealloc];
425}
426
427@end