11#import "BookmarkEditorController.h"
14#import "ScreenSelectionController.h"
15#import "PerformanceEditorController.h"
16#import "CredentialsEditorController.h"
17#import "AdvancedBookmarkEditorController.h"
23#define SECTION_SERVER 0
24#define SECTION_CREDENTIALS 1
25#define SECTION_SETTINGS 2
26#define SECTION_COUNT 3
29#pragma mark Initialization
33 if ((
self = [super initWithStyle:UITableViewStyleInsetGrouped]))
36 if ([[bookmark uuid] length] == 0)
37 _bookmark = [bookmark copy];
39 _bookmark = [bookmark copyWithUUID];
40 _params = [_bookmark params];
42 _display_server_settings = YES;
48#pragma mark View lifecycle
56 UIBarButtonItem *saveButton =
57 [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Save", @"Save Button title")
58 style:UIBarButtonItemStyleDone
60 action:@selector(handleSave:)] autorelease];
61 UIBarButtonItem *cancelButton =
62 [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel", @"Cancel Button title")
63 style:UIBarButtonItemStylePlain
65 action:@selector(handleCancel:)] autorelease];
66 [[
self navigationItem] setLeftBarButtonItem:cancelButton];
67 [[
self navigationItem] setRightBarButtonItem:saveButton];
70- (void)viewWillAppear:(BOOL)animated
72 [
super viewWillAppear:animated];
76 [[
self tableView] reloadData];
80- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
86#pragma mark Table view data source
88- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
94- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
100 return (_display_server_settings ? 3 : 0);
101 case SECTION_CREDENTIALS:
103 case SECTION_SETTINGS:
113- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
118 return (_display_server_settings
119 ? NSLocalizedString(
@"Host",
@"'Host': host settings header")
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");
130- (UITableViewCell *)tableView:(UITableView *)tableView
131 cellForRowAtIndexPath:(NSIndexPath *)indexPath
135 NSString *cellType = nil;
136 switch ([indexPath section])
139 cellType = TableCellIdentifierText;
142 case SECTION_CREDENTIALS:
143 cellType = TableCellIdentifierSelection;
146 case SECTION_SETTINGS:
148 switch ([indexPath row])
151 cellType = TableCellIdentifierSelection;
155 cellType = TableCellIdentifierSubEditor;
166 NSAssert(cellType != nil,
@"Couldn't determine cell type");
169 UITableViewCell *cell = [
self tableViewCellFromIdentifier:cellType];
170 NSAssert(cell,
@"Invalid cell");
173 switch ([indexPath section])
177 [
self initServerSettings:indexPath cell:cell];
181 case SECTION_CREDENTIALS:
182 [
self initCredentialSettings:indexPath cell:cell];
186 case SECTION_SETTINGS:
187 [
self initSettings:indexPath cell:cell];
198- (void)initServerSettings:(NSIndexPath *)indexPath cell:(UITableViewCell *)cell
201 [[textCell textfield] setTag:GET_TAG_FROM_PATH(indexPath)];
202 switch ([indexPath row])
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")];
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")];
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];
223 NSLog(
@"Invalid row index in settings table!");
229- (void)initCredentialSettings:(NSIndexPath *)indexPath cell:(UITableViewCell *)cell
232 switch (indexPath.row)
236 setTitle:NSLocalizedString(@"Credentials", @"'Credentials': Bookmark credentials")];
237 [selCell setValue:[_params StringForKey:@"username"]];
240 NSLog(
@"Invalid row index in settings table!");
246- (void)initSettings:(NSIndexPath *)indexPath cell:(UITableViewCell *)cell
248 switch (indexPath.row)
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]];
264 [editCell setTitle:NSLocalizedString(@"Performance",
265 @"'Performance': Bookmark Performance Settings")];
272 setTitle:NSLocalizedString(@"Advanced", @"'Advanced': Bookmark Advanced Settings")];
276 NSLog(
@"Invalid row index in settings table!");
282#pragma mark Table view delegate
284- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
286 UIViewController *viewCtrl = nil;
289 switch ([indexPath section])
291 case SECTION_CREDENTIALS:
293 if ([indexPath row] == 0)
299 case SECTION_SETTINGS:
301 switch ([indexPath row])
309 initWithConnectionParams:_params] autorelease];
313 initWithBookmark:_bookmark] autorelease];
325 [[
self navigationController] pushViewController:viewCtrl animated:YES];
329#pragma mark Text Field delegate
331- (BOOL)textFieldShouldReturn:(UITextField *)textField
333 [textField resignFirstResponder];
337- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
339 switch (textField.tag)
342 case GET_TAG(SECTION_SERVER, 0):
343 [_bookmark setLabel:[textField text]];
346 case GET_TAG(SECTION_SERVER, 1):
347 [_params setValue:[textField text] forKey:
@"hostname"];
350 case GET_TAG(SECTION_SERVER, 2):
351 [_params setInt:[[textField text] intValue] forKey:
@"port"];
361#pragma mark Action Handlers
363- (void)handleSave:(
id)sender
367 [[
self view] endEditing:NO];
370 if ([[_bookmark label] length] == 0 || [[_params StringForKey:
@"hostname"] length] == 0 ||
371 [_params intForKey:
@"port"] == 0)
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];
382 addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel Button")
383 style:UIAlertActionStyleCancel
384 handler:^(UIAlertAction *action) {
385 [[
self navigationController]
386 popViewControllerAnimated:YES];
388 [alertView addAction:[UIAlertAction
389 actionWithTitle:NSLocalizedString(@"Continue", @"Continue Button")
390 style:UIAlertActionStyleDefault
392 [
self presentViewController:alertView animated:YES completion:nil];
397 if ([[
self delegate] respondsToSelector:@selector(commitBookmark:)])
398 [[self delegate] commitBookmark:_bookmark];
401 [[
self navigationController] popViewControllerAnimated:YES];
404- (void)handleCancel:(
id)sender
407 [[
self navigationController] popViewControllerAnimated:YES];
411#pragma mark Memory management
413- (void)didReceiveMemoryWarning
416 [
super didReceiveMemoryWarning];
423 [_bookmark autorelease];