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:UITableViewStyleGrouped]))
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 [self adjustEditTextTableViewCell:textCell];
228}
229
230// updates credentials in the UI
231- (void)initCredentialSettings:(NSIndexPath *)indexPath cell:(UITableViewCell *)cell
232{
234 switch (indexPath.row)
235 {
236 case 0:
237 [[selCell label]
238 setText:NSLocalizedString(@"Credentials", @"'Credentials': Bookmark credentials")];
239 [[selCell selection] setText:[_params StringForKey:@"username"]];
240 break;
241 default:
242 NSLog(@"Invalid row index in settings table!");
243 break;
244 }
245}
246
247// updates session settings in the UI
248- (void)initSettings:(NSIndexPath *)indexPath cell:(UITableViewCell *)cell
249{
250 switch (indexPath.row)
251 {
252 case 0:
253 {
255 [[selCell label]
256 setText:NSLocalizedString(@"Screen", @"'Screen': Bookmark Screen settings")];
257 NSString *resolution = ScreenResolutionDescription(
258 [_params intForKey:@"screen_resolution_type"], [_params intForKey:@"width"],
259 [_params intForKey:@"height"]);
260 int colorBits = [_params intForKey:@"colors"];
261 [[selCell selection]
262 setText:[NSString stringWithFormat:@"%@@%d", resolution, colorBits]];
263 break;
264 }
265 case 1:
266 {
268 [[editCell label]
269 setText:NSLocalizedString(@"Performance",
270 @"'Performance': Bookmark Performance Settings")];
271 break;
272 }
273 case 2:
274 {
276 [[editCell label]
277 setText:NSLocalizedString(@"Advanced", @"'Advanced': Bookmark Advanced Settings")];
278 break;
279 }
280 default:
281 NSLog(@"Invalid row index in settings table!");
282 break;
283 }
284}
285
286#pragma mark -
287#pragma mark Table view delegate
288
289- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
290{
291 UIViewController *viewCtrl = nil;
292
293 // determine view
294 switch ([indexPath section])
295 {
296 case SECTION_CREDENTIALS:
297 {
298 if ([indexPath row] == 0)
299 viewCtrl =
300 [[[CredentialsEditorController alloc] initWithBookmark:_bookmark] autorelease];
301 break;
302 }
303
304 case SECTION_SETTINGS:
305 {
306 switch ([indexPath row])
307 {
308 case 0:
309 viewCtrl = [[[ScreenSelectionController alloc] initWithConnectionParams:_params]
310 autorelease];
311 break;
312 case 1:
313 viewCtrl = [[[PerformanceEditorController alloc]
314 initWithConnectionParams:_params] autorelease];
315 break;
316 case 2:
317 viewCtrl = [[[AdvancedBookmarkEditorController alloc]
318 initWithBookmark:_bookmark] autorelease];
319 break;
320 default:
321 break;
322 }
323
324 break;
325 }
326 }
327
328 // display view
329 if (viewCtrl)
330 [[self navigationController] pushViewController:viewCtrl animated:YES];
331}
332
333#pragma mark -
334#pragma mark Text Field delegate
335
336- (BOOL)textFieldShouldReturn:(UITextField *)textField
337{
338 [textField resignFirstResponder];
339 return NO;
340}
341
342- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
343{
344 switch (textField.tag)
345 {
346 // update server settings
347 case GET_TAG(SECTION_SERVER, 0):
348 [_bookmark setLabel:[textField text]];
349 break;
350
351 case GET_TAG(SECTION_SERVER, 1):
352 [_params setValue:[textField text] forKey:@"hostname"];
353 break;
354
355 case GET_TAG(SECTION_SERVER, 2):
356 [_params setInt:[[textField text] intValue] forKey:@"port"];
357 break;
358
359 default:
360 break;
361 }
362 return YES;
363}
364
365#pragma mark -
366#pragma mark Action Handlers
367
368- (void)handleSave:(id)sender
369{
370 // resign any first responder (so that we finish editing any bookmark parameter that might be
371 // currently edited)
372 [[self view] endEditing:NO];
373
374 // verify that bookmark is complete (only for manual bookmarks)
375 if ([[_bookmark label] length] == 0 || [[_params StringForKey:@"hostname"] length] == 0 ||
376 [_params intForKey:@"port"] == 0)
377 {
378 UIAlertController *alertView = [UIAlertController
379 alertControllerWithTitle:NSLocalizedString(@"Cancel without saving?",
380 @"Incomplete bookmark error title")
381 message:NSLocalizedString(
382 @"Press 'Cancel' to abort!\nPress 'Continue' to "
383 @"specify the required fields!",
384 @"Incomplete bookmark error message")
385 preferredStyle:UIAlertControllerStyleAlert];
386 [alertView
387 addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel Button")
388 style:UIAlertActionStyleCancel
389 handler:^(UIAlertAction *action) {
390 [[self navigationController]
391 popViewControllerAnimated:YES];
392 }]];
393 [alertView addAction:[UIAlertAction
394 actionWithTitle:NSLocalizedString(@"Continue", @"Continue Button")
395 style:UIAlertActionStyleDefault
396 handler:nil]];
397 [self presentViewController:alertView animated:YES completion:nil];
398 return;
399 }
400
401 // commit bookmark
402 if ([[self delegate] respondsToSelector:@selector(commitBookmark:)])
403 [[self delegate] commitBookmark:_bookmark];
404
405 // return to previous view controller
406 [[self navigationController] popViewControllerAnimated:YES];
407}
408
409- (void)handleCancel:(id)sender
410{
411 // return to previous view controller
412 [[self navigationController] popViewControllerAnimated:YES];
413}
414
415#pragma mark -
416#pragma mark Memory management
417
418- (void)didReceiveMemoryWarning
419{
420 // Releases the view if it doesn't have a superview.
421 [super didReceiveMemoryWarning];
422
423 // Relinquish ownership any cached data, images, etc that aren't in use.
424}
425
426- (void)dealloc
427{
428 [_bookmark autorelease];
429 [super dealloc];
430}
431
432@end