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:UITableViewStyleGrouped]))
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!");
227 [
self adjustEditTextTableViewCell:textCell];
231- (void)initCredentialSettings:(NSIndexPath *)indexPath cell:(UITableViewCell *)cell
234 switch (indexPath.row)
238 setText:NSLocalizedString(@"Credentials", @"'Credentials': Bookmark credentials")];
239 [[selCell selection] setText:[_params StringForKey:@"username"]];
242 NSLog(
@"Invalid row index in settings table!");
248- (void)initSettings:(NSIndexPath *)indexPath cell:(UITableViewCell *)cell
250 switch (indexPath.row)
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"];
262 setText:[NSString stringWithFormat:@"%@@%d", resolution, colorBits]];
269 setText:NSLocalizedString(@"Performance",
270 @"'Performance': Bookmark Performance Settings")];
277 setText:NSLocalizedString(@"Advanced", @"'Advanced': Bookmark Advanced Settings")];
281 NSLog(
@"Invalid row index in settings table!");
287#pragma mark Table view delegate
289- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
291 UIViewController *viewCtrl = nil;
294 switch ([indexPath section])
296 case SECTION_CREDENTIALS:
298 if ([indexPath row] == 0)
304 case SECTION_SETTINGS:
306 switch ([indexPath row])
314 initWithConnectionParams:_params] autorelease];
318 initWithBookmark:_bookmark] autorelease];
330 [[
self navigationController] pushViewController:viewCtrl animated:YES];
334#pragma mark Text Field delegate
336- (BOOL)textFieldShouldReturn:(UITextField *)textField
338 [textField resignFirstResponder];
342- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
344 switch (textField.tag)
347 case GET_TAG(SECTION_SERVER, 0):
348 [_bookmark setLabel:[textField text]];
351 case GET_TAG(SECTION_SERVER, 1):
352 [_params setValue:[textField text] forKey:
@"hostname"];
355 case GET_TAG(SECTION_SERVER, 2):
356 [_params setInt:[[textField text] intValue] forKey:
@"port"];
366#pragma mark Action Handlers
368- (void)handleSave:(
id)sender
372 [[
self view] endEditing:NO];
375 if ([[_bookmark label] length] == 0 || [[_params StringForKey:
@"hostname"] length] == 0 ||
376 [_params intForKey:
@"port"] == 0)
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];
387 addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel Button")
388 style:UIAlertActionStyleCancel
389 handler:^(UIAlertAction *action) {
390 [[
self navigationController]
391 popViewControllerAnimated:YES];
393 [alertView addAction:[UIAlertAction
394 actionWithTitle:NSLocalizedString(@"Continue", @"Continue Button")
395 style:UIAlertActionStyleDefault
397 [
self presentViewController:alertView animated:YES completion:nil];
402 if ([[
self delegate] respondsToSelector:@selector(commitBookmark:)])
403 [[self delegate] commitBookmark:_bookmark];
406 [[
self navigationController] popViewControllerAnimated:YES];
409- (void)handleCancel:(
id)sender
412 [[
self navigationController] popViewControllerAnimated:YES];
416#pragma mark Memory management
418- (void)didReceiveMemoryWarning
421 [
super didReceiveMemoryWarning];
428 [_bookmark autorelease];