Blame client/iOS/Controllers/BookmarkEditorController.m

Packit 1fb8d4
/*
Packit 1fb8d4
 Bookmark editor controller
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 "BookmarkEditorController.h"
Packit 1fb8d4
#import "Bookmark.h"
Packit 1fb8d4
#import "Utils.h"
Packit 1fb8d4
#import "ScreenSelectionController.h"
Packit 1fb8d4
#import "PerformanceEditorController.h"
Packit 1fb8d4
#import "CredentialsEditorController.h"
Packit 1fb8d4
#import "AdvancedBookmarkEditorController.h"
Packit 1fb8d4
#import "BlockAlertView.h"
Packit 1fb8d4
Packit 1fb8d4
@implementation BookmarkEditorController
Packit 1fb8d4
Packit 1fb8d4
@synthesize delegate;
Packit 1fb8d4
Packit 1fb8d4
#define SECTION_SERVER 0
Packit 1fb8d4
#define SECTION_CREDENTIALS 1
Packit 1fb8d4
#define SECTION_SETTINGS 2
Packit 1fb8d4
#define SECTION_COUNT 3
Packit 1fb8d4
Packit 1fb8d4
#pragma mark -
Packit 1fb8d4
#pragma mark Initialization	
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
        if ([[bookmark uuid] length] == 0)
Packit 1fb8d4
            _bookmark = [bookmark copy];
Packit 1fb8d4
        else
Packit 1fb8d4
            _bookmark = [bookmark copyWithUUID];
Packit 1fb8d4
        _params = [_bookmark params];
Packit 1fb8d4
Packit 1fb8d4
        _display_server_settings = YES;
Packit 1fb8d4
    }
Packit 1fb8d4
    return self;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
Packit 1fb8d4
#pragma mark -
Packit 1fb8d4
#pragma mark View lifecycle
Packit 1fb8d4
Packit 1fb8d4
Packit 1fb8d4
- (void)viewDidLoad {
Packit 1fb8d4
    [super viewDidLoad];
Packit 1fb8d4
Packit 1fb8d4
    // replace back button with a custom handler that checks if the required bookmark settings were specified
Packit 1fb8d4
    UIBarButtonItem* saveButton = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Save", @"Save Button title") style:UIBarButtonItemStyleDone target:self action:@selector(handleSave:)] autorelease];
Packit 1fb8d4
    UIBarButtonItem* cancelButton = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel", @"Cancel Button title") style:UIBarButtonItemStyleBordered target:self action:@selector(handleCancel:)] autorelease];
Packit 1fb8d4
    [[self navigationItem] setLeftBarButtonItem:cancelButton];    
Packit 1fb8d4
    [[self navigationItem] setRightBarButtonItem:saveButton];    
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (void)viewWillAppear:(BOOL)animated {
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
// Override to allow orientations other than the default portrait orientation.
Packit 1fb8d4
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
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_SERVER: // server settings
Packit 1fb8d4
			return (_display_server_settings ? 3 : 0);
Packit 1fb8d4
		case SECTION_CREDENTIALS: // credentials
Packit 1fb8d4
			return 1;
Packit 1fb8d4
		case SECTION_SETTINGS: // session settings
Packit 1fb8d4
			return 3;
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_SERVER:
Packit 1fb8d4
			return (_display_server_settings ? NSLocalizedString(@"Host", @"'Host': host settings header") : nil);
Packit 1fb8d4
		case SECTION_CREDENTIALS:
Packit 1fb8d4
			return NSLocalizedString(@"Credentials", @"'Credentials': credentials settings header");
Packit 1fb8d4
		case SECTION_SETTINGS:
Packit 1fb8d4
			return NSLocalizedString(@"Settings", @"'Session Settings': session 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_SERVER:
Packit 1fb8d4
            cellType = TableCellIdentifierText;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
        case SECTION_CREDENTIALS:
Packit 1fb8d4
            cellType = TableCellIdentifierSelection;
Packit 1fb8d4
            break;
Packit 1fb8d4
            
Packit 1fb8d4
		case SECTION_SETTINGS: // settings
Packit 1fb8d4
			{
Packit 1fb8d4
				switch([indexPath row]) 
Packit 1fb8d4
				{
Packit 1fb8d4
					case 0:	// screen/color depth
Packit 1fb8d4
						cellType = TableCellIdentifierSelection;
Packit 1fb8d4
						break;
Packit 1fb8d4
					case 1:	// performance settings
Packit 1fb8d4
                    case 2: // advanced settings
Packit 1fb8d4
						cellType = TableCellIdentifierSubEditor;
Packit 1fb8d4
						break;		
Packit 1fb8d4
					default:						
Packit 1fb8d4
						break;
Packit 1fb8d4
				}
Packit 1fb8d4
			}
Packit 1fb8d4
			break;			
Packit 1fb8d4
Packit 1fb8d4
        default:
Packit 1fb8d4
            break;
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
		// server settings
Packit 1fb8d4
		case SECTION_SERVER:
Packit 1fb8d4
			[self initServerSettings:indexPath cell:cell];
Packit 1fb8d4
			break;
Packit 1fb8d4
			
Packit 1fb8d4
		// credentials
Packit 1fb8d4
		case SECTION_CREDENTIALS:
Packit 1fb8d4
			[self initCredentialSettings:indexPath cell:cell];
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		// session settings
Packit 1fb8d4
		case SECTION_SETTINGS:
Packit 1fb8d4
			[self initSettings: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 server settings in the UI
Packit 1fb8d4
- (void)initServerSettings:(NSIndexPath*)indexPath cell:(UITableViewCell*)cell
Packit 1fb8d4
{
Packit 1fb8d4
    EditTextTableViewCell* textCell = (EditTextTableViewCell*)cell;
Packit 1fb8d4
	[[textCell textfield] setTag:GET_TAG_FROM_PATH(indexPath)];
Packit 1fb8d4
	switch([indexPath row]) 
Packit 1fb8d4
	{
Packit 1fb8d4
		case 0:
Packit 1fb8d4
			[[textCell label] setText:NSLocalizedString(@"Label", @"'Label': Bookmark label")];
Packit 1fb8d4
			[[textCell textfield] setText:[_bookmark label]];
Packit 1fb8d4
            [[textCell textfield] setPlaceholder:NSLocalizedString(@"not set", @"not set placeholder")];
Packit 1fb8d4
			break;
Packit 1fb8d4
		case 1:
Packit 1fb8d4
			[[textCell label] setText:NSLocalizedString(@"Host", @"'Host': Bookmark hostname")];
Packit 1fb8d4
			[[textCell textfield] setText:[_params StringForKey:@"hostname"]];
Packit 1fb8d4
            [[textCell textfield] setPlaceholder:NSLocalizedString(@"not set", @"not set placeholder")];
Packit 1fb8d4
			break;
Packit 1fb8d4
		case 2:
Packit 1fb8d4
			[[textCell label] setText:NSLocalizedString(@"Port", @"'Port': Bookmark port")];
Packit 1fb8d4
			[[textCell textfield] setText:[NSString stringWithFormat:@"%d", [_params intForKey:@"port"]]];
Packit 1fb8d4
            [[textCell textfield] setKeyboardType:UIKeyboardTypeNumberPad];
Packit 1fb8d4
			break;
Packit 1fb8d4
		default:
Packit 1fb8d4
			NSLog(@"Invalid row index in settings table!");
Packit 1fb8d4
			break;
Packit 1fb8d4
	}	
Packit 1fb8d4
Packit 1fb8d4
    [self adjustEditTextTableViewCell:textCell];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
// updates credentials in the UI
Packit 1fb8d4
- (void)initCredentialSettings:(NSIndexPath*)indexPath cell:(UITableViewCell*)cell
Packit 1fb8d4
{
Packit 1fb8d4
    EditSelectionTableViewCell* selCell = (EditSelectionTableViewCell*)cell;
Packit 1fb8d4
	switch(indexPath.row) 
Packit 1fb8d4
	{
Packit 1fb8d4
		case 0:
Packit 1fb8d4
            [[selCell label] setText:NSLocalizedString(@"Credentials", @"'Credentials': Bookmark credentials")];
Packit 1fb8d4
            [[selCell selection] setText:[_params StringForKey:@"username"]];
Packit 1fb8d4
			break;
Packit 1fb8d4
		default:
Packit 1fb8d4
			NSLog(@"Invalid row index in settings table!");
Packit 1fb8d4
			break;
Packit 1fb8d4
	}
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
// updates session settings in the UI
Packit 1fb8d4
- (void)initSettings:(NSIndexPath*)indexPath cell:(UITableViewCell*)cell
Packit 1fb8d4
{	
Packit 1fb8d4
	switch(indexPath.row) 
Packit 1fb8d4
	{
Packit 1fb8d4
		case 0:
Packit 1fb8d4
        {
Packit 1fb8d4
            EditSelectionTableViewCell* selCell = (EditSelectionTableViewCell*)cell;
Packit 1fb8d4
            [[selCell label] setText:NSLocalizedString(@"Screen", @"'Screen': Bookmark Screen settings")];
Packit 1fb8d4
            NSString* resolution = ScreenResolutionDescription([_params intForKey:@"screen_resolution_type"], [_params intForKey:@"width"], [_params intForKey:@"height"]);
Packit 1fb8d4
            int colorBits = [_params intForKey:@"colors"];
Packit 1fb8d4
            [[selCell selection] setText:[NSString stringWithFormat:@"%@@%d", resolution, colorBits]];
Packit 1fb8d4
            break;
Packit 1fb8d4
        }            
Packit 1fb8d4
		case 1:
Packit 1fb8d4
        {
Packit 1fb8d4
            EditSubEditTableViewCell* editCell = (EditSubEditTableViewCell*)cell;
Packit 1fb8d4
            [[editCell label] setText:NSLocalizedString(@"Performance", @"'Performance': Bookmark Performance Settings")];
Packit 1fb8d4
            break;
Packit 1fb8d4
        }			
Packit 1fb8d4
		case 2:
Packit 1fb8d4
        {
Packit 1fb8d4
            EditSubEditTableViewCell* editCell = (EditSubEditTableViewCell*)cell;
Packit 1fb8d4
            [[editCell label] setText:NSLocalizedString(@"Advanced", @"'Advanced': Bookmark Advanced Settings")];
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
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 section])
Packit 1fb8d4
    {
Packit 1fb8d4
        case SECTION_CREDENTIALS:
Packit 1fb8d4
        {
Packit 1fb8d4
            if ([indexPath row] == 0)
Packit 1fb8d4
                viewCtrl = [[[CredentialsEditorController alloc] initWithBookmark:_bookmark] autorelease];
Packit 1fb8d4
            break;
Packit 1fb8d4
        }
Packit 1fb8d4
            
Packit 1fb8d4
        case SECTION_SETTINGS:
Packit 1fb8d4
        {            
Packit 1fb8d4
            switch ([indexPath row])
Packit 1fb8d4
            {
Packit 1fb8d4
                case 0:
Packit 1fb8d4
                    viewCtrl = [[[ScreenSelectionController alloc] initWithConnectionParams:_params] autorelease];
Packit 1fb8d4
                    break;
Packit 1fb8d4
                case 1:
Packit 1fb8d4
                    viewCtrl = [[[PerformanceEditorController alloc] initWithConnectionParams:_params] autorelease];
Packit 1fb8d4
                    break;    
Packit 1fb8d4
                case 2:
Packit 1fb8d4
                    viewCtrl = [[[AdvancedBookmarkEditorController alloc] initWithBookmark:_bookmark] autorelease];
Packit 1fb8d4
                    break;
Packit 1fb8d4
                default:
Packit 1fb8d4
                    break;
Packit 1fb8d4
            }            
Packit 1fb8d4
            
Packit 1fb8d4
            break;
Packit 1fb8d4
        }            
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 server settings
Packit 1fb8d4
		case GET_TAG(SECTION_SERVER, 0):
Packit 1fb8d4
			[_bookmark setLabel:[textField text]];
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case GET_TAG(SECTION_SERVER, 1):
Packit 1fb8d4
			[_params setValue:[textField text] forKey:@"hostname"];
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case GET_TAG(SECTION_SERVER, 2):
Packit 1fb8d4
			[_params setInt:[[textField text] intValue] forKey:@"port"];
Packit 1fb8d4
			break;
Packit 1fb8d4
		            
Packit 1fb8d4
		default:
Packit 1fb8d4
			break;
Packit 1fb8d4
	}
Packit 1fb8d4
	return YES;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
#pragma mark -
Packit 1fb8d4
#pragma mark Action Handlers
Packit 1fb8d4
Packit 1fb8d4
- (void)handleSave:(id)sender
Packit 1fb8d4
{
Packit 1fb8d4
    // resign any first responder (so that we finish editing any bookmark parameter that might be currently edited)
Packit 1fb8d4
    [[self view] endEditing:NO];
Packit 1fb8d4
    
Packit 1fb8d4
    // verify that bookmark is complete (only for manual bookmarks)
Packit 1fb8d4
    if ([[_bookmark label] length] == 0 || [[_params StringForKey:@"hostname"] length] == 0 || [_params intForKey:@"port"] == 0)
Packit 1fb8d4
    {
Packit 1fb8d4
        BlockAlertView* alertView = [BlockAlertView alertWithTitle:NSLocalizedString(@"Cancel without saving?", @"Incomplete bookmark error title") message:NSLocalizedString(@"Press 'Cancel' to abort!\nPress 'Continue' to specify the required fields!", @"Incomplete bookmark error message")];
Packit 1fb8d4
        [alertView setCancelButtonWithTitle:NSLocalizedString(@"Cancel", @"Cancel Button") block:^{
Packit 1fb8d4
            // cancel bookmark editing and return to previous view controller
Packit 1fb8d4
            [[self navigationController] popViewControllerAnimated:YES];
Packit 1fb8d4
        }];
Packit 1fb8d4
        [alertView addButtonWithTitle:NSLocalizedString(@"Continue", @"Continue Button") block:nil];
Packit 1fb8d4
        [alertView show];
Packit 1fb8d4
        return;
Packit 1fb8d4
    }
Packit 1fb8d4
    
Packit 1fb8d4
    // commit bookmark
Packit 1fb8d4
    if ([[self delegate] respondsToSelector:@selector(commitBookmark:)])
Packit 1fb8d4
        [[self delegate] commitBookmark:_bookmark];
Packit 1fb8d4
    
Packit 1fb8d4
    // return to previous view controller
Packit 1fb8d4
    [[self navigationController] popViewControllerAnimated:YES];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (void)handleCancel:(id)sender
Packit 1fb8d4
{
Packit 1fb8d4
    // return to previous view controller
Packit 1fb8d4
    [[self navigationController] popViewControllerAnimated:YES];    
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
#pragma mark -
Packit 1fb8d4
#pragma mark Memory management
Packit 1fb8d4
Packit 1fb8d4
- (void)didReceiveMemoryWarning {
Packit 1fb8d4
    // Releases the view if it doesn't have a superview.
Packit 1fb8d4
    [super didReceiveMemoryWarning];
Packit 1fb8d4
    
Packit 1fb8d4
    // Relinquish ownership any cached data, images, etc that aren't in use.
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (void)dealloc {
Packit 1fb8d4
    [super dealloc];
Packit 1fb8d4
    [_bookmark autorelease];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
Packit 1fb8d4
@end
Packit 1fb8d4