Blame client/iOS/Controllers/AdvancedBookmarkEditorController.m

Packit 1fb8d4
/*
Packit 1fb8d4
 Controller to edit advanced bookmark settings
Packit 1fb8d4
 
Packit 1fb8d4
 Copyright 2013 Thincast Technologies GmbH, Author: Martin Fleisz
Packit 1fb8d4
 
Packit 1fb8d4
 This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. 
Packit 1fb8d4
 If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
#import "AdvancedBookmarkEditorController.h"
Packit 1fb8d4
#import "Bookmark.h"
Packit 1fb8d4
#import "Utils.h"
Packit 1fb8d4
#import "EditorSelectionController.h"
Packit 1fb8d4
#import "ScreenSelectionController.h"
Packit 1fb8d4
#import "PerformanceEditorController.h"
Packit 1fb8d4
#import "BookmarkGatewaySettingsController.h"
Packit 1fb8d4
Packit 1fb8d4
@interface AdvancedBookmarkEditorController ()
Packit 1fb8d4
Packit 1fb8d4
@end
Packit 1fb8d4
Packit 1fb8d4
#define SECTION_ADVANCED_SETTINGS 0
Packit 1fb8d4
#define SECTION_COUNT 1
Packit 1fb8d4
Packit 1fb8d4
@implementation AdvancedBookmarkEditorController
Packit 1fb8d4
Packit 1fb8d4
- (id)initWithBookmark:(ComputerBookmark*)bookmark
Packit 1fb8d4
{
Packit 1fb8d4
    if ((self = [super initWithStyle:UITableViewStyleGrouped])) 
Packit 1fb8d4
	{
Packit 1fb8d4
		// set additional settings state according to bookmark data
Packit 1fb8d4
		_bookmark = [bookmark retain];
Packit 1fb8d4
        _params = [bookmark params];                
Packit 1fb8d4
    }
Packit 1fb8d4
    return self;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (void)viewDidLoad
Packit 1fb8d4
{
Packit 1fb8d4
    [super viewDidLoad];
Packit 1fb8d4
    [self setTitle:NSLocalizedString(@"Advanced Settings", @"Advanced Settings title")];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (void)viewWillAppear:(BOOL)animated
Packit 1fb8d4
{
Packit 1fb8d4
    [super viewWillAppear:animated];
Packit 1fb8d4
    
Packit 1fb8d4
    // we need to reload the table view data here to have up-to-date data for the 
Packit 1fb8d4
    // advanced settings accessory items (like for resolution/color mode settings)
Packit 1fb8d4
    [[self tableView] reloadData];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
Packit 1fb8d4
{
Packit 1fb8d4
    return YES;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
#pragma mark -
Packit 1fb8d4
#pragma mark Table view data source
Packit 1fb8d4
Packit 1fb8d4
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
Packit 1fb8d4
    // Return the number of sections.
Packit 1fb8d4
    return SECTION_COUNT;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
Packit 1fb8d4
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
Packit 1fb8d4
    // Return the number of rows in the section.
Packit 1fb8d4
	switch (section)
Packit 1fb8d4
	{
Packit 1fb8d4
		case SECTION_ADVANCED_SETTINGS: // advanced settings
Packit 1fb8d4
			return 9;
Packit 1fb8d4
		default:
Packit 1fb8d4
			break;
Packit 1fb8d4
	}
Packit 1fb8d4
	
Packit 1fb8d4
    return 0;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
Packit 1fb8d4
// set section headers
Packit 1fb8d4
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
Packit 1fb8d4
{
Packit 1fb8d4
	switch(section)
Packit 1fb8d4
	{
Packit 1fb8d4
		case SECTION_ADVANCED_SETTINGS:
Packit 1fb8d4
			return NSLocalizedString(@"Advanced", @"'Advanced': advanced settings header");
Packit 1fb8d4
	}
Packit 1fb8d4
	return @"unknown";
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
Packit 1fb8d4
// Customize the appearance of table view cells.
Packit 1fb8d4
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Packit 1fb8d4
    
Packit 1fb8d4
	// determine the required cell type
Packit 1fb8d4
	NSString* cellType = nil;
Packit 1fb8d4
	switch([indexPath section])
Packit 1fb8d4
	{
Packit 1fb8d4
        case SECTION_ADVANCED_SETTINGS: // advanced settings
Packit 1fb8d4
        {
Packit 1fb8d4
            switch([indexPath row]) 
Packit 1fb8d4
            {
Packit 1fb8d4
                case 0: // Enable/Disable TSG Settings
Packit 1fb8d4
                    cellType = TableCellIdentifierYesNo;
Packit 1fb8d4
                    break;
Packit 1fb8d4
                case 1:	// TS Gateway Settings
Packit 1fb8d4
                    cellType = TableCellIdentifierSubEditor;
Packit 1fb8d4
                    break;
Packit 1fb8d4
                case 2: // 3G Settings
Packit 1fb8d4
                    cellType = TableCellIdentifierYesNo;
Packit 1fb8d4
                    break;
Packit 1fb8d4
                case 3:	// 3G screen/color depth
Packit 1fb8d4
                    cellType = TableCellIdentifierSelection;
Packit 1fb8d4
                    break;
Packit 1fb8d4
                case 4:	// 3G performance settings
Packit 1fb8d4
                    cellType = TableCellIdentifierSubEditor;
Packit 1fb8d4
                    break;
Packit 1fb8d4
                case 5: // security mode
Packit 1fb8d4
                    cellType = TableCellIdentifierSelection;
Packit 1fb8d4
                    break;
Packit 1fb8d4
                case 6:	// remote program
Packit 1fb8d4
                case 7:	// work dir
Packit 1fb8d4
                    cellType = TableCellIdentifierText;
Packit 1fb8d4
                    break;
Packit 1fb8d4
                case 8:	// console mode
Packit 1fb8d4
                    cellType = TableCellIdentifierYesNo;
Packit 1fb8d4
                    break;
Packit 1fb8d4
                default:						
Packit 1fb8d4
                    break;
Packit 1fb8d4
            }
Packit 1fb8d4
			break;
Packit 1fb8d4
        }
Packit 1fb8d4
	}	
Packit 1fb8d4
	NSAssert(cellType != nil, @"Couldn't determine cell type");	
Packit 1fb8d4
	
Packit 1fb8d4
	// get the table view cell
Packit 1fb8d4
	UITableViewCell *cell = [self tableViewCellFromIdentifier:cellType];
Packit 1fb8d4
	NSAssert(cell, @"Invalid cell");	
Packit 1fb8d4
    
Packit 1fb8d4
	// set cell values
Packit 1fb8d4
	switch([indexPath section]) 
Packit 1fb8d4
	{
Packit 1fb8d4
            // advanced settings
Packit 1fb8d4
		case SECTION_ADVANCED_SETTINGS:
Packit 1fb8d4
			[self initAdvancedSettings:indexPath cell:cell];
Packit 1fb8d4
			break;
Packit 1fb8d4
            
Packit 1fb8d4
		default:
Packit 1fb8d4
			break;
Packit 1fb8d4
	}	
Packit 1fb8d4
    
Packit 1fb8d4
    return cell;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
// updates advanced settings in the UI
Packit 1fb8d4
- (void)initAdvancedSettings:(NSIndexPath*)indexPath cell:(UITableViewCell*)cell
Packit 1fb8d4
{
Packit 1fb8d4
    BOOL enable_3G_settings = [_params boolForKey:@"enable_3g_settings"];
Packit 1fb8d4
	switch(indexPath.row) 
Packit 1fb8d4
	{
Packit 1fb8d4
		case 0:
Packit 1fb8d4
        {
Packit 1fb8d4
            EditFlagTableViewCell* flagCell = (EditFlagTableViewCell*)cell;
Packit 1fb8d4
            [[flagCell label] setText:NSLocalizedString(@"Enable TS Gateway", @"'Enable TS Gateway': Bookmark enable TSG settings")];
Packit 1fb8d4
            [[flagCell toggle] setTag:GET_TAG_FROM_PATH(indexPath)];
Packit 1fb8d4
            [[flagCell toggle] setOn:[_params boolForKey:@"enable_tsg_settings"]];
Packit 1fb8d4
            [[flagCell toggle] addTarget:self action:@selector(toggleSettingValue:) forControlEvents:UIControlEventValueChanged];
Packit 1fb8d4
            break;
Packit 1fb8d4
        }
Packit 1fb8d4
        case 1:
Packit 1fb8d4
        {
Packit 1fb8d4
            BOOL enable_tsg_settings = [_params boolForKey:@"enable_tsg_settings"];
Packit 1fb8d4
            EditSubEditTableViewCell* editCell = (EditSubEditTableViewCell*)cell;
Packit 1fb8d4
            [[editCell label] setText:NSLocalizedString(@"TS Gateway Settings", @"'TS Gateway Settings': Bookmark TS Gateway Settings")];
Packit 1fb8d4
            [[editCell label] setEnabled:enable_tsg_settings];
Packit 1fb8d4
            [editCell setSelectionStyle:enable_tsg_settings ? UITableViewCellSelectionStyleBlue : UITableViewCellSelectionStyleNone];
Packit 1fb8d4
            break;
Packit 1fb8d4
        }
Packit 1fb8d4
		case 2:
Packit 1fb8d4
        {
Packit 1fb8d4
            EditFlagTableViewCell* flagCell = (EditFlagTableViewCell*)cell;
Packit 1fb8d4
            [[flagCell label] setText:NSLocalizedString(@"3G Settings", @"'3G Settings': Bookmark enable 3G settings")];
Packit 1fb8d4
            [[flagCell toggle] setTag:GET_TAG_FROM_PATH(indexPath)];
Packit 1fb8d4
            [[flagCell toggle] setOn:[_params boolForKey:@"enable_3g_settings"]];
Packit 1fb8d4
            [[flagCell toggle] addTarget:self action:@selector(toggleSettingValue:) forControlEvents:UIControlEventValueChanged];
Packit 1fb8d4
            break;
Packit 1fb8d4
        }
Packit 1fb8d4
		case 3:
Packit 1fb8d4
        {
Packit 1fb8d4
            EditSelectionTableViewCell* selCell = (EditSelectionTableViewCell*)cell;
Packit 1fb8d4
            [[selCell label] setText:NSLocalizedString(@"3G Screen", @"'3G Screen': Bookmark 3G Screen settings")];
Packit 1fb8d4
            NSString* resolution = ScreenResolutionDescription([_params intForKeyPath:@"settings_3g.screen_resolution_type"], [_params intForKeyPath:@"settings_3g.width"], [_params intForKeyPath:@"settings_3g.height"]);
Packit 1fb8d4
            int colorBits = [_params intForKeyPath:@"settings_3g.colors"];
Packit 1fb8d4
            [[selCell selection] setText:[NSString stringWithFormat:@"%@@%d", resolution, colorBits]];
Packit 1fb8d4
            [[selCell label] setEnabled:enable_3G_settings];
Packit 1fb8d4
            [[selCell selection] setEnabled:enable_3G_settings];
Packit 1fb8d4
            [selCell setSelectionStyle:enable_3G_settings ? UITableViewCellSelectionStyleBlue : UITableViewCellSelectionStyleNone];
Packit 1fb8d4
            break;
Packit 1fb8d4
        }
Packit 1fb8d4
		case 4:
Packit 1fb8d4
        {
Packit 1fb8d4
            EditSubEditTableViewCell* editCell = (EditSubEditTableViewCell*)cell;
Packit 1fb8d4
            [[editCell label] setText:NSLocalizedString(@"3G Performance", @"'3G Performance': Bookmark 3G Performance Settings")];
Packit 1fb8d4
            [[editCell label] setEnabled:enable_3G_settings];
Packit 1fb8d4
            [editCell setSelectionStyle:enable_3G_settings ? UITableViewCellSelectionStyleBlue : UITableViewCellSelectionStyleNone];
Packit 1fb8d4
            break;
Packit 1fb8d4
        }			
Packit 1fb8d4
		case 5:
Packit 1fb8d4
        {
Packit 1fb8d4
            EditSelectionTableViewCell* selCell = (EditSelectionTableViewCell*)cell;
Packit 1fb8d4
            [[selCell label] setText:NSLocalizedString(@"Security", @"'Security': Bookmark protocl security settings")];
Packit 1fb8d4
            [[selCell selection] setText:ProtocolSecurityDescription([_params intForKey:@"security"])];
Packit 1fb8d4
            break;
Packit 1fb8d4
        }                                    
Packit 1fb8d4
		case 6:
Packit 1fb8d4
        {
Packit 1fb8d4
            EditTextTableViewCell* textCell = (EditTextTableViewCell*)cell;
Packit 1fb8d4
            [[textCell label] setText:NSLocalizedString(@"Remote Program", @"'Remote Program': Bookmark remote program settings")];
Packit 1fb8d4
            [[textCell textfield] setText:[_params StringForKey:@"remote_program"]];
Packit 1fb8d4
            [[textCell textfield] setTag:GET_TAG_FROM_PATH(indexPath)];
Packit 1fb8d4
            [[textCell textfield] setPlaceholder:NSLocalizedString(@"not set", @"not set placeholder")];
Packit 1fb8d4
            [self adjustEditTextTableViewCell:textCell];
Packit 1fb8d4
            break;
Packit 1fb8d4
        }            
Packit 1fb8d4
		case 7:
Packit 1fb8d4
        {
Packit 1fb8d4
            EditTextTableViewCell* textCell = (EditTextTableViewCell*)cell;
Packit 1fb8d4
            [[textCell label] setText:NSLocalizedString(@"Working Directory", @"'Working Directory': Bookmark working directory settings")];
Packit 1fb8d4
            [[textCell textfield] setText:[_params StringForKey:@"working_dir"]];
Packit 1fb8d4
            [[textCell textfield] setTag:GET_TAG_FROM_PATH(indexPath)];
Packit 1fb8d4
            [[textCell textfield] setPlaceholder:NSLocalizedString(@"not set", @"not set placeholder")];
Packit 1fb8d4
            [self adjustEditTextTableViewCell:textCell];
Packit 1fb8d4
            break;
Packit 1fb8d4
        }            
Packit 1fb8d4
		case 8:
Packit 1fb8d4
        {
Packit 1fb8d4
            EditFlagTableViewCell* flagCell = (EditFlagTableViewCell*)cell;
Packit 1fb8d4
            [[flagCell label] setText:NSLocalizedString(@"Console Mode", @"'Console Mode': Bookmark console mode settings")];
Packit 1fb8d4
            [[flagCell toggle] setTag:GET_TAG_FROM_PATH(indexPath)];
Packit 1fb8d4
            [[flagCell toggle] setOn:[_params boolForKey:@"console"]];
Packit 1fb8d4
            [[flagCell toggle] addTarget:self action:@selector(toggleSettingValue:) forControlEvents:UIControlEventValueChanged];
Packit 1fb8d4
            break;
Packit 1fb8d4
        }
Packit 1fb8d4
		default:
Packit 1fb8d4
			NSLog(@"Invalid row index in settings table!");
Packit 1fb8d4
			break;
Packit 1fb8d4
	}			
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
#pragma mark -
Packit 1fb8d4
#pragma mark Table view delegate
Packit 1fb8d4
Packit 1fb8d4
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Packit 1fb8d4
    UIViewController* viewCtrl = nil;
Packit 1fb8d4
    
Packit 1fb8d4
    // determine view
Packit 1fb8d4
    switch ([indexPath row])
Packit 1fb8d4
    {
Packit 1fb8d4
        case 1:
Packit 1fb8d4
            if ([_params boolForKey:@"enable_tsg_settings"])
Packit 1fb8d4
                viewCtrl = [[[BookmarkGatewaySettingsController alloc] initWithBookmark:_bookmark] autorelease];
Packit 1fb8d4
            break;
Packit 1fb8d4
        case 3:
Packit 1fb8d4
            if ([_params boolForKey:@"enable_3g_settings"])
Packit 1fb8d4
                viewCtrl = [[[ScreenSelectionController alloc] initWithConnectionParams:_params keyPath:@"settings_3g"] autorelease];
Packit 1fb8d4
            break;
Packit 1fb8d4
        case 4:
Packit 1fb8d4
            if ([_params boolForKey:@"enable_3g_settings"])
Packit 1fb8d4
                viewCtrl = [[[PerformanceEditorController alloc] initWithConnectionParams:_params keyPath:@"settings_3g"] autorelease];
Packit 1fb8d4
            break;                    
Packit 1fb8d4
        case 5:
Packit 1fb8d4
            viewCtrl = [[[EditorSelectionController alloc] initWithConnectionParams:_params entries:[NSArray arrayWithObject:@"security"] selections:[NSArray arrayWithObject:SelectionForSecuritySetting()]] autorelease];
Packit 1fb8d4
            break;                                    
Packit 1fb8d4
        default:
Packit 1fb8d4
            break;
Packit 1fb8d4
    }
Packit 1fb8d4
    
Packit 1fb8d4
    // display view
Packit 1fb8d4
    if(viewCtrl)
Packit 1fb8d4
        [[self navigationController] pushViewController:viewCtrl animated:YES];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
Packit 1fb8d4
#pragma mark -
Packit 1fb8d4
#pragma mark Text Field delegate
Packit 1fb8d4
Packit 1fb8d4
- (BOOL)textFieldShouldReturn:(UITextField*)textField
Packit 1fb8d4
{
Packit 1fb8d4
	[textField resignFirstResponder];
Packit 1fb8d4
	return NO;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
Packit 1fb8d4
{
Packit 1fb8d4
	switch(textField.tag)
Packit 1fb8d4
	{
Packit 1fb8d4
        // update remote program/work dir settings
Packit 1fb8d4
        case GET_TAG(SECTION_ADVANCED_SETTINGS, 6):
Packit 1fb8d4
        {
Packit 1fb8d4
            [_params setValue:[textField text] forKey:@"remote_program"];
Packit 1fb8d4
            break;
Packit 1fb8d4
        }
Packit 1fb8d4
            
Packit 1fb8d4
        case GET_TAG(SECTION_ADVANCED_SETTINGS, 7):
Packit 1fb8d4
        {
Packit 1fb8d4
            [_params setValue:[textField text] forKey:@"working_dir"];
Packit 1fb8d4
            break;
Packit 1fb8d4
        }
Packit 1fb8d4
			
Packit 1fb8d4
		default:
Packit 1fb8d4
			break;
Packit 1fb8d4
	}
Packit 1fb8d4
	return YES;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
#pragma mark - Action handlers
Packit 1fb8d4
Packit 1fb8d4
- (void)toggleSettingValue:(id)sender
Packit 1fb8d4
{
Packit 1fb8d4
    UISwitch* valueSwitch = (UISwitch*)sender;
Packit 1fb8d4
    switch(valueSwitch.tag)
Packit 1fb8d4
    {
Packit 1fb8d4
        case GET_TAG(SECTION_ADVANCED_SETTINGS, 0):
Packit 1fb8d4
        {
Packit 1fb8d4
            [_params setBool:[valueSwitch isOn] forKey:@"enable_tsg_settings"];
Packit 1fb8d4
            NSArray* indexPaths = [NSArray arrayWithObjects:[NSIndexPath indexPathForRow:1 inSection:SECTION_ADVANCED_SETTINGS], [NSIndexPath indexPathForRow:2 inSection:SECTION_ADVANCED_SETTINGS], nil];
Packit 1fb8d4
            [[self tableView] reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
Packit 1fb8d4
            break;
Packit 1fb8d4
        }
Packit 1fb8d4
Packit 1fb8d4
        case GET_TAG(SECTION_ADVANCED_SETTINGS, 2):
Packit 1fb8d4
        {
Packit 1fb8d4
            [_params setBool:[valueSwitch isOn] forKey:@"enable_3g_settings"];
Packit 1fb8d4
            NSArray* indexPaths = [NSArray arrayWithObjects:[NSIndexPath indexPathForRow:3 inSection:SECTION_ADVANCED_SETTINGS], [NSIndexPath indexPathForRow:2 inSection:SECTION_ADVANCED_SETTINGS], nil];
Packit 1fb8d4
            [[self tableView] reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
Packit 1fb8d4
            break;
Packit 1fb8d4
        }
Packit 1fb8d4
            
Packit 1fb8d4
        case GET_TAG(SECTION_ADVANCED_SETTINGS, 8):
Packit 1fb8d4
            [_params setBool:[valueSwitch isOn] forKey:@"console"];
Packit 1fb8d4
            break;
Packit 1fb8d4
            
Packit 1fb8d4
        default:
Packit 1fb8d4
            break;
Packit 1fb8d4
    }    
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
@end