Blame client/iOS/Controllers/CredentialsEditorController.m

Packit 1fb8d4
/*
Packit 1fb8d4
 Controller to edit bookmark credentials
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 "CredentialsEditorController.h"
Packit 1fb8d4
#import "Bookmark.h"
Packit 1fb8d4
#import "Utils.h"
Packit 1fb8d4
Packit 1fb8d4
@interface CredentialsEditorController ()
Packit 1fb8d4
Packit 1fb8d4
@end
Packit 1fb8d4
Packit 1fb8d4
#define SECTION_CREDENTIALS 0
Packit 1fb8d4
#define SECTION_COUNT 1
Packit 1fb8d4
Packit 1fb8d4
@implementation CredentialsEditorController
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(@"Credentials", @"Credentials title")];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (void)viewDidUnload
Packit 1fb8d4
{
Packit 1fb8d4
    [super viewDidUnload];
Packit 1fb8d4
    // Release any retained subviews of the main view.
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (void)viewWillDisappear:(BOOL)animated
Packit 1fb8d4
{
Packit 1fb8d4
    [super viewWillDisappear:animated];
Packit 1fb8d4
    
Packit 1fb8d4
    // foce any active editing to stop
Packit 1fb8d4
    [[self view] endEditing:NO];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
Packit 1fb8d4
{
Packit 1fb8d4
    return YES;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (void)dealloc
Packit 1fb8d4
{
Packit 1fb8d4
    [super dealloc];
Packit 1fb8d4
    [_bookmark release];
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_CREDENTIALS: // credentials
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_CREDENTIALS:
Packit 1fb8d4
			return NSLocalizedString(@"Credentials", @"'Credentials': credentials 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_CREDENTIALS: // credentials
Packit 1fb8d4
			if([indexPath row] == 1)
Packit 1fb8d4
                cellType = TableCellIdentifierSecretText; // password field
Packit 1fb8d4
            else
Packit 1fb8d4
                cellType = TableCellIdentifierText;
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
            // credentials
Packit 1fb8d4
		case SECTION_CREDENTIALS:
Packit 1fb8d4
			[self initCredentialSettings: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
Packit 1fb8d4
// updates credentials in the UI
Packit 1fb8d4
- (void)initCredentialSettings:(NSIndexPath*)indexPath cell:(UITableViewCell*)cell
Packit 1fb8d4
{
Packit 1fb8d4
	switch(indexPath.row) 
Packit 1fb8d4
	{
Packit 1fb8d4
		case 0:
Packit 1fb8d4
        {
Packit 1fb8d4
            EditTextTableViewCell* textCell = (EditTextTableViewCell*)cell;
Packit 1fb8d4
            [[textCell textfield] setTag:GET_TAG_FROM_PATH(indexPath)];
Packit 1fb8d4
            [[textCell label] setText:NSLocalizedString(@"Username", @"'Username': Bookmark username")];
Packit 1fb8d4
			[[textCell textfield] setText:[_params StringForKey:@"username"]];
Packit 1fb8d4
            [[textCell textfield] setPlaceholder:NSLocalizedString(@"not set", @"not set placeholder")];
Packit 1fb8d4
			break;            
Packit 1fb8d4
        }
Packit 1fb8d4
		case 1:
Packit 1fb8d4
        {
Packit 1fb8d4
            EditSecretTextTableViewCell* textCell = (EditSecretTextTableViewCell*)cell;
Packit 1fb8d4
            [[textCell textfield] setTag:GET_TAG_FROM_PATH(indexPath)];            
Packit 1fb8d4
			[[textCell label] setText:NSLocalizedString(@"Password", @"'Password': Bookmark password")];
Packit 1fb8d4
			[[textCell textfield] setText:[_params StringForKey:@"password"]];
Packit 1fb8d4
            [[textCell textfield] setPlaceholder:NSLocalizedString(@"not set", @"not set placeholder")];
Packit 1fb8d4
			break;
Packit 1fb8d4
        }
Packit 1fb8d4
		case 2:
Packit 1fb8d4
        {
Packit 1fb8d4
            EditTextTableViewCell* textCell = (EditTextTableViewCell*)cell;
Packit 1fb8d4
            [[textCell textfield] setTag:GET_TAG_FROM_PATH(indexPath)];
Packit 1fb8d4
			[[textCell label] setText:NSLocalizedString(@"Domain", @"'Domain': Bookmark domain")];
Packit 1fb8d4
			[[textCell textfield] setText:[_params StringForKey:@"domain"]];
Packit 1fb8d4
            [[textCell textfield] setPlaceholder:NSLocalizedString(@"not set", @"not set placeholder")];
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 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 credentials settings
Packit 1fb8d4
		case GET_TAG(SECTION_CREDENTIALS, 0):
Packit 1fb8d4
			[_params setValue:[textField text] forKey:@"username"];
Packit 1fb8d4
			break;
Packit 1fb8d4
            			
Packit 1fb8d4
		case GET_TAG(SECTION_CREDENTIALS, 1):
Packit 1fb8d4
			[_params setValue:[textField text] forKey:@"password"];
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case GET_TAG(SECTION_CREDENTIALS, 2):
Packit 1fb8d4
			[_params setValue:[textField text] forKey:@"domain"];
Packit 1fb8d4
			break;
Packit 1fb8d4
            
Packit 1fb8d4
		default:
Packit 1fb8d4
			break;
Packit 1fb8d4
	}
Packit 1fb8d4
	return YES;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
@end