Blame client/iOS/Controllers/CredentialsEditorController.m

Packit 1fb8d4
/*
Packit 1fb8d4
 Controller to edit bookmark credentials
Packit Service 5a9772
Packit 1fb8d4
 Copyright 2013 Thincast Technologies GmbH, Author: Martin Fleisz
Packit Service 5a9772
Packit Service 5a9772
 This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
Packit Service 5a9772
 If a copy of the MPL was not distributed with this file, You can obtain one at
Packit Service 5a9772
 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 Service 5a9772
- (id)initWithBookmark:(ComputerBookmark *)bookmark
Packit 1fb8d4
{
Packit Service 5a9772
	if ((self = [super initWithStyle:UITableViewStyleGrouped]))
Packit 1fb8d4
	{
Packit 1fb8d4
		// set additional settings state according to bookmark data
Packit 1fb8d4
		_bookmark = [bookmark retain];
Packit Service 5a9772
		_params = [bookmark params];
Packit Service 5a9772
	}
Packit Service 5a9772
	return self;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (void)viewDidLoad
Packit 1fb8d4
{
Packit Service 5a9772
	[super viewDidLoad];
Packit Service 5a9772
	[self setTitle:NSLocalizedString(@"Credentials", @"Credentials title")];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (void)viewDidUnload
Packit 1fb8d4
{
Packit Service 5a9772
	[super viewDidUnload];
Packit Service 5a9772
	// Release any retained subviews of the main view.
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (void)viewWillDisappear:(BOOL)animated
Packit 1fb8d4
{
Packit Service 5a9772
	[super viewWillDisappear:animated];
Packit Service 5a9772
Packit Service 5a9772
	// foce any active editing to stop
Packit Service 5a9772
	[[self view] endEditing:NO];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
Packit 1fb8d4
{
Packit Service 5a9772
	return YES;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (void)dealloc
Packit 1fb8d4
{
Packit Service 5a9772
	[super dealloc];
Packit Service 5a9772
	[_bookmark release];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
#pragma mark -
Packit 1fb8d4
#pragma mark Table view data source
Packit 1fb8d4
Packit Service 5a9772
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
Packit Service 5a9772
{
Packit Service 5a9772
	// Return the number of sections.
Packit Service 5a9772
	return SECTION_COUNT;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
Packit Service 5a9772
{
Packit Service 5a9772
	// 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 Service 5a9772
	return 0;
Packit Service 5a9772
}
Packit 1fb8d4
Packit 1fb8d4
// set section headers
Packit 1fb8d4
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
Packit 1fb8d4
{
Packit Service 5a9772
	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
// Customize the appearance of table view cells.
Packit Service 5a9772
- (UITableViewCell *)tableView:(UITableView *)tableView
Packit Service 5a9772
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
Packit Service 5a9772
{
Packit Service 5a9772
Packit 1fb8d4
	// determine the required cell type
Packit Service 5a9772
	NSString *cellType = nil;
Packit Service 5a9772
	switch ([indexPath section])
Packit 1fb8d4
	{
Packit 1fb8d4
		case SECTION_CREDENTIALS: // credentials
Packit Service 5a9772
			if ([indexPath row] == 1)
Packit Service 5a9772
				cellType = TableCellIdentifierSecretText; // password field
Packit Service 5a9772
			else
Packit Service 5a9772
				cellType = TableCellIdentifierText;
Packit Service 5a9772
			break;
Packit Service 5a9772
Packit Service 5a9772
		default:
Packit 1fb8d4
			break;
Packit Service 5a9772
	}
Packit Service 5a9772
	NSAssert(cellType != nil, @"Couldn't determine cell type");
Packit Service 5a9772
Packit 1fb8d4
	// get the table view cell
Packit 1fb8d4
	UITableViewCell *cell = [self tableViewCellFromIdentifier:cellType];
Packit Service 5a9772
	NSAssert(cell, @"Invalid cell");
Packit Service 5a9772
Packit 1fb8d4
	// set cell values
Packit Service 5a9772
	switch ([indexPath section])
Packit Service 5a9772
	{
Packit Service 5a9772
			// credentials
Packit 1fb8d4
		case SECTION_CREDENTIALS:
Packit 1fb8d4
			[self initCredentialSettings:indexPath cell:cell];
Packit Service 5a9772
			break;
Packit Service 5a9772
Packit 1fb8d4
		default:
Packit 1fb8d4
			break;
Packit Service 5a9772
	}
Packit 1fb8d4
Packit Service 5a9772
	return cell;
Packit Service 5a9772
}
Packit 1fb8d4
Packit 1fb8d4
// updates credentials in the UI
Packit Service 5a9772
- (void)initCredentialSettings:(NSIndexPath *)indexPath cell:(UITableViewCell *)cell
Packit 1fb8d4
{
Packit Service 5a9772
	switch (indexPath.row)
Packit 1fb8d4
	{
Packit 1fb8d4
		case 0:
Packit Service 5a9772
		{
Packit Service 5a9772
			EditTextTableViewCell *textCell = (EditTextTableViewCell *)cell;
Packit Service 5a9772
			[[textCell textfield] setTag:GET_TAG_FROM_PATH(indexPath)];
Packit Service 5a9772
			[[textCell label]
Packit Service 5a9772
			    setText:NSLocalizedString(@"Username", @"'Username': Bookmark username")];
Packit 1fb8d4
			[[textCell textfield] setText:[_params StringForKey:@"username"]];
Packit Service 5a9772
			[[textCell textfield]
Packit Service 5a9772
			    setPlaceholder:NSLocalizedString(@"not set", @"not set placeholder")];
Packit Service 5a9772
			break;
Packit Service 5a9772
		}
Packit 1fb8d4
		case 1:
Packit Service 5a9772
		{
Packit Service 5a9772
			EditSecretTextTableViewCell *textCell = (EditSecretTextTableViewCell *)cell;
Packit Service 5a9772
			[[textCell textfield] setTag:GET_TAG_FROM_PATH(indexPath)];
Packit Service 5a9772
			[[textCell label]
Packit Service 5a9772
			    setText:NSLocalizedString(@"Password", @"'Password': Bookmark password")];
Packit 1fb8d4
			[[textCell textfield] setText:[_params StringForKey:@"password"]];
Packit Service 5a9772
			[[textCell textfield]
Packit Service 5a9772
			    setPlaceholder:NSLocalizedString(@"not set", @"not set placeholder")];
Packit 1fb8d4
			break;
Packit Service 5a9772
		}
Packit 1fb8d4
		case 2:
Packit Service 5a9772
		{
Packit Service 5a9772
			EditTextTableViewCell *textCell = (EditTextTableViewCell *)cell;
Packit Service 5a9772
			[[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 Service 5a9772
			[[textCell textfield]
Packit Service 5a9772
			    setPlaceholder:NSLocalizedString(@"not set", @"not set placeholder")];
Packit Service 5a9772
			break;
Packit Service 5a9772
		}
Packit 1fb8d4
		default:
Packit 1fb8d4
			NSLog(@"Invalid row index in settings table!");
Packit 1fb8d4
			break;
Packit Service 5a9772
	}
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
#pragma mark -
Packit 1fb8d4
#pragma mark Text Field delegate
Packit 1fb8d4
Packit Service 5a9772
- (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 Service 5a9772
	switch (textField.tag)
Packit Service 5a9772
	{
Packit Service 5a9772
			// update credentials settings
Packit 1fb8d4
		case GET_TAG(SECTION_CREDENTIALS, 0):
Packit 1fb8d4
			[_params setValue:[textField text] forKey:@"username"];
Packit 1fb8d4
			break;
Packit Service 5a9772
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 Service 5a9772
Packit 1fb8d4
		default:
Packit 1fb8d4
			break;
Packit 1fb8d4
	}
Packit 1fb8d4
	return YES;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
@end