FreeRDP
CredentialsEditorController.m
1 /*
2  Controller to edit bookmark credentials
3 
4  Copyright 2013 Thincast Technologies GmbH, Author: Martin Fleisz
5 
6  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
7  If a copy of the MPL was not distributed with this file, You can obtain one at
8  http://mozilla.org/MPL/2.0/.
9  */
10 
11 #import "CredentialsEditorController.h"
12 #import "Bookmark.h"
13 #import "Utils.h"
14 
15 @interface CredentialsEditorController ()
16 
17 @end
18 
19 #define SECTION_CREDENTIALS 0
20 #define SECTION_COUNT 1
21 
22 @implementation CredentialsEditorController
23 
24 - (id)initWithBookmark:(ComputerBookmark *)bookmark
25 {
26  if ((self = [super initWithStyle:UITableViewStyleGrouped]))
27  {
28  // set additional settings state according to bookmark data
29  _bookmark = [bookmark retain];
30  _params = [bookmark params];
31  }
32  return self;
33 }
34 
35 - (void)viewDidLoad
36 {
37  [super viewDidLoad];
38  [self setTitle:NSLocalizedString(@"Credentials", @"Credentials title")];
39 }
40 
41 - (void)viewDidUnload
42 {
43  [super viewDidUnload];
44  // Release any retained subviews of the main view.
45 }
46 
47 - (void)viewWillDisappear:(BOOL)animated
48 {
49  [super viewWillDisappear:animated];
50 
51  // foce any active editing to stop
52  [[self view] endEditing:NO];
53 }
54 
55 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
56 {
57  return YES;
58 }
59 
60 - (void)dealloc
61 {
62  [super dealloc];
63  [_bookmark release];
64 }
65 
66 #pragma mark -
67 #pragma mark Table view data source
68 
69 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
70 {
71  // Return the number of sections.
72  return SECTION_COUNT;
73 }
74 
75 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
76 {
77  // Return the number of rows in the section.
78  switch (section)
79  {
80  case SECTION_CREDENTIALS: // credentials
81  return 3;
82  default:
83  break;
84  }
85 
86  return 0;
87 }
88 
89 // set section headers
90 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
91 {
92  switch (section)
93  {
94  case SECTION_CREDENTIALS:
95  return NSLocalizedString(@"Credentials", @"'Credentials': credentials settings header");
96  }
97  return @"unknown";
98 }
99 
100 // Customize the appearance of table view cells.
101 - (UITableViewCell *)tableView:(UITableView *)tableView
102  cellForRowAtIndexPath:(NSIndexPath *)indexPath
103 {
104 
105  // determine the required cell type
106  NSString *cellType = nil;
107  switch ([indexPath section])
108  {
109  case SECTION_CREDENTIALS: // credentials
110  if ([indexPath row] == 1)
111  cellType = TableCellIdentifierSecretText; // password field
112  else
113  cellType = TableCellIdentifierText;
114  break;
115 
116  default:
117  break;
118  }
119  NSAssert(cellType != nil, @"Couldn't determine cell type");
120 
121  // get the table view cell
122  UITableViewCell *cell = [self tableViewCellFromIdentifier:cellType];
123  NSAssert(cell, @"Invalid cell");
124 
125  // set cell values
126  switch ([indexPath section])
127  {
128  // credentials
129  case SECTION_CREDENTIALS:
130  [self initCredentialSettings:indexPath cell:cell];
131  break;
132 
133  default:
134  break;
135  }
136 
137  return cell;
138 }
139 
140 // updates credentials in the UI
141 - (void)initCredentialSettings:(NSIndexPath *)indexPath cell:(UITableViewCell *)cell
142 {
143  switch (indexPath.row)
144  {
145  case 0:
146  {
147  EditTextTableViewCell *textCell = (EditTextTableViewCell *)cell;
148  [[textCell textfield] setTag:GET_TAG_FROM_PATH(indexPath)];
149  [[textCell label]
150  setText:NSLocalizedString(@"Username", @"'Username': Bookmark username")];
151  [[textCell textfield] setText:[_params StringForKey:@"username"]];
152  [[textCell textfield]
153  setPlaceholder:NSLocalizedString(@"not set", @"not set placeholder")];
154  break;
155  }
156  case 1:
157  {
159  [[textCell textfield] setTag:GET_TAG_FROM_PATH(indexPath)];
160  [[textCell label]
161  setText:NSLocalizedString(@"Password", @"'Password': Bookmark password")];
162  [[textCell textfield] setText:[_params StringForKey:@"password"]];
163  [[textCell textfield]
164  setPlaceholder:NSLocalizedString(@"not set", @"not set placeholder")];
165  break;
166  }
167  case 2:
168  {
169  EditTextTableViewCell *textCell = (EditTextTableViewCell *)cell;
170  [[textCell textfield] setTag:GET_TAG_FROM_PATH(indexPath)];
171  [[textCell label] setText:NSLocalizedString(@"Domain", @"'Domain': Bookmark domain")];
172  [[textCell textfield] setText:[_params StringForKey:@"domain"]];
173  [[textCell textfield]
174  setPlaceholder:NSLocalizedString(@"not set", @"not set placeholder")];
175  break;
176  }
177  default:
178  NSLog(@"Invalid row index in settings table!");
179  break;
180  }
181 }
182 
183 #pragma mark -
184 #pragma mark Text Field delegate
185 
186 - (BOOL)textFieldShouldReturn:(UITextField *)textField
187 {
188  [textField resignFirstResponder];
189  return NO;
190 }
191 
192 - (BOOL)textFieldShouldEndEditing:(UITextField *)textField
193 {
194  switch (textField.tag)
195  {
196  // update credentials settings
197  case GET_TAG(SECTION_CREDENTIALS, 0):
198  [_params setValue:[textField text] forKey:@"username"];
199  break;
200 
201  case GET_TAG(SECTION_CREDENTIALS, 1):
202  [_params setValue:[textField text] forKey:@"password"];
203  break;
204 
205  case GET_TAG(SECTION_CREDENTIALS, 2):
206  [_params setValue:[textField text] forKey:@"domain"];
207  break;
208 
209  default:
210  break;
211  }
212  return YES;
213 }
214 
215 @end