11#import "BookmarkEditorController.h" 
   14#import "ScreenSelectionController.h" 
   15#import "PerformanceEditorController.h" 
   16#import "CredentialsEditorController.h" 
   17#import "AdvancedBookmarkEditorController.h" 
   18#import "BlockAlertView.h" 
   24#define SECTION_SERVER 0 
   25#define SECTION_CREDENTIALS 1 
   26#define SECTION_SETTINGS 2 
   27#define SECTION_COUNT 3 
   30#pragma mark Initialization 
   34  if ((
self = [super initWithStyle:UITableViewStyleGrouped]))
 
   37    if ([[bookmark uuid] length] == 0)
 
   38      _bookmark = [bookmark copy];
 
   40      _bookmark = [bookmark copyWithUUID];
 
   41    _params = [_bookmark params];
 
   43    _display_server_settings = YES;
 
   49#pragma mark View lifecycle 
   57  UIBarButtonItem *saveButton =
 
   58      [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Save", @"Save Button title")
 
   59                                        style:UIBarButtonItemStyleDone
 
   61                                       action:@selector(handleSave:)] autorelease];
 
   62  UIBarButtonItem *cancelButton =
 
   63      [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel", @"Cancel Button title")
 
   64                                        style:UIBarButtonItemStyleBordered
 
   66                                       action:@selector(handleCancel:)] autorelease];
 
   67  [[
self navigationItem] setLeftBarButtonItem:cancelButton];
 
   68  [[
self navigationItem] setRightBarButtonItem:saveButton];
 
   71- (void)viewWillAppear:(BOOL)animated
 
   73  [
super viewWillAppear:animated];
 
   77  [[
self tableView] reloadData];
 
   81- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
 
   87#pragma mark Table view data source 
   89- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 
   95- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 
  101      return (_display_server_settings ? 3 : 0);
 
  102    case SECTION_CREDENTIALS: 
 
  104    case SECTION_SETTINGS: 
 
  114- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
 
  119      return (_display_server_settings
 
  120                  ? NSLocalizedString(
@"Host", 
@"'Host': host settings header")
 
  122    case SECTION_CREDENTIALS:
 
  123      return NSLocalizedString(
@"Credentials", 
@"'Credentials': credentials settings header");
 
  124    case SECTION_SETTINGS:
 
  125      return NSLocalizedString(
@"Settings", 
@"'Session Settings': session settings header");
 
  131- (UITableViewCell *)tableView:(UITableView *)tableView
 
  132         cellForRowAtIndexPath:(NSIndexPath *)indexPath
 
  136  NSString *cellType = nil;
 
  137  switch ([indexPath section])
 
  140      cellType = TableCellIdentifierText;
 
  143    case SECTION_CREDENTIALS:
 
  144      cellType = TableCellIdentifierSelection;
 
  147    case SECTION_SETTINGS: 
 
  149      switch ([indexPath row])
 
  152          cellType = TableCellIdentifierSelection;
 
  156          cellType = TableCellIdentifierSubEditor;
 
  167  NSAssert(cellType != nil, 
@"Couldn't determine cell type");
 
  170  UITableViewCell *cell = [
self tableViewCellFromIdentifier:cellType];
 
  171  NSAssert(cell, 
@"Invalid cell");
 
  174  switch ([indexPath section])
 
  178      [
self initServerSettings:indexPath cell:cell];
 
  182    case SECTION_CREDENTIALS:
 
  183      [
self initCredentialSettings:indexPath cell:cell];
 
  187    case SECTION_SETTINGS:
 
  188      [
self initSettings:indexPath cell:cell];
 
  199- (void)initServerSettings:(NSIndexPath *)indexPath cell:(UITableViewCell *)cell
 
  202  [[textCell textfield] setTag:GET_TAG_FROM_PATH(indexPath)];
 
  203  switch ([indexPath row])
 
  206      [[textCell label] setText:NSLocalizedString(@"Label", @"'Label': Bookmark label")];
 
  207      [[textCell textfield] setText:[_bookmark label]];
 
  208      [[textCell textfield]
 
  209          setPlaceholder:NSLocalizedString(@"not set", @"not set placeholder")];
 
  212      [[textCell label] setText:NSLocalizedString(@"Host", @"'Host': Bookmark hostname")];
 
  213      [[textCell textfield] setText:[_params StringForKey:@"hostname"]];
 
  214      [[textCell textfield]
 
  215          setPlaceholder:NSLocalizedString(@"not set", @"not set placeholder")];
 
  218      [[textCell label] setText:NSLocalizedString(@"Port", @"'Port': Bookmark port")];
 
  219      [[textCell textfield]
 
  220          setText:[NSString stringWithFormat:@"%d", [_params intForKey:@"port"]]];
 
  221      [[textCell textfield] setKeyboardType:UIKeyboardTypeNumberPad];
 
  224      NSLog(
@"Invalid row index in settings table!");
 
  228  [
self adjustEditTextTableViewCell:textCell];
 
  232- (void)initCredentialSettings:(NSIndexPath *)indexPath cell:(UITableViewCell *)cell
 
  235  switch (indexPath.row)
 
  239          setText:NSLocalizedString(@"Credentials", @"'Credentials': Bookmark credentials")];
 
  240      [[selCell selection] setText:[_params StringForKey:@"username"]];
 
  243      NSLog(
@"Invalid row index in settings table!");
 
  249- (void)initSettings:(NSIndexPath *)indexPath cell:(UITableViewCell *)cell
 
  251  switch (indexPath.row)
 
  257          setText:NSLocalizedString(@"Screen", @"'Screen': Bookmark Screen settings")];
 
  258      NSString *resolution = ScreenResolutionDescription(
 
  259          [_params intForKey:
@"screen_resolution_type"], [_params intForKey:
@"width"],
 
  260          [_params intForKey:
@"height"]);
 
  261      int colorBits = [_params intForKey:@"colors"];
 
  263          setText:[NSString stringWithFormat:@"%@@%d", resolution, colorBits]];
 
  270          setText:NSLocalizedString(@"Performance",
 
  271                                    @"'Performance': Bookmark Performance Settings")];
 
  278          setText:NSLocalizedString(@"Advanced", @"'Advanced': Bookmark Advanced Settings")];
 
  282      NSLog(
@"Invalid row index in settings table!");
 
  288#pragma mark Table view delegate 
  290- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 
  292  UIViewController *viewCtrl = nil;
 
  295  switch ([indexPath section])
 
  297    case SECTION_CREDENTIALS:
 
  299      if ([indexPath row] == 0)
 
  305    case SECTION_SETTINGS:
 
  307      switch ([indexPath row])
 
  315              initWithConnectionParams:_params] autorelease];
 
  319              initWithBookmark:_bookmark] autorelease];
 
  331    [[
self navigationController] pushViewController:viewCtrl animated:YES];
 
  335#pragma mark Text Field delegate 
  337- (BOOL)textFieldShouldReturn:(UITextField *)textField
 
  339  [textField resignFirstResponder];
 
  343- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
 
  345  switch (textField.tag)
 
  348    case GET_TAG(SECTION_SERVER, 0):
 
  349      [_bookmark setLabel:[textField text]];
 
  352    case GET_TAG(SECTION_SERVER, 1):
 
  353      [_params setValue:[textField text] forKey:
@"hostname"];
 
  356    case GET_TAG(SECTION_SERVER, 2):
 
  357      [_params setInt:[[textField text] intValue] forKey:
@"port"];
 
  367#pragma mark Action Handlers 
  369- (void)handleSave:(
id)sender
 
  373  [[
self view] endEditing:NO];
 
  376  if ([[_bookmark label] length] == 0 || [[_params StringForKey:
@"hostname"] length] == 0 ||
 
  377      [_params intForKey:
@"port"] == 0)
 
  380        alertWithTitle:NSLocalizedString(@"Cancel without saving?",
 
  381                                         @"Incomplete bookmark error title")
 
  382               message:NSLocalizedString(@"Press 'Cancel' to abort!\nPress 'Continue' to "
 
  383                                         @"specify the required fields!",
 
  384                                         @"Incomplete bookmark error message")];
 
  386        setCancelButtonWithTitle:NSLocalizedString(@"Cancel", @"Cancel Button")
 
  389                             [[
self navigationController] popViewControllerAnimated:YES];
 
  391    [alertView addButtonWithTitle:NSLocalizedString(@"Continue", @"Continue Button") block: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];
 
  424  [_bookmark autorelease];