FreeRDP
BookmarkGatewaySettingsController.m
1 //
2 // BookmarkGatewaySettingsController.m
3 // FreeRDP
4 //
5 // Created by a Thincast Developer on 4/30/13.
6 //
7 //
8 
9 #import "BookmarkGatewaySettingsController.h"
10 #import "Bookmark.h"
11 #import "Utils.h"
12 #import "EditorSelectionController.h"
13 
14 #define SECTION_TSGATEWAY_SETTINGS 0
15 #define SECTION_COUNT 1
16 
18 
19 @end
20 
22 
23 - (id)initWithBookmark:(ComputerBookmark *)bookmark
24 {
25  if ((self = [super initWithStyle:UITableViewStyleGrouped]))
26  {
27  // set additional settings state according to bookmark data
28  _bookmark = [bookmark retain];
29  _params = [bookmark params];
30  }
31  return self;
32 }
33 
34 - (void)viewDidLoad
35 {
36  [super viewDidLoad];
37  [self setTitle:NSLocalizedString(@"TS Gateway Settings", @"TS Gateway Settings title")];
38 }
39 
40 - (void)viewWillAppear:(BOOL)animated
41 {
42  [super viewWillAppear:animated];
43 
44  // we need to reload the table view data here to have up-to-date data for the
45  // advanced settings accessory items (like for resolution/color mode settings)
46  [[self tableView] reloadData];
47 }
48 
49 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
50 {
51  return YES;
52 }
53 
54 - (void)dealloc
55 {
56  [super dealloc];
57  [_bookmark release];
58 }
59 
60 #pragma mark - Table view data source
61 
62 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
63 {
64  // Return the number of sections.
65  return SECTION_COUNT;
66 }
67 
68 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
69 {
70  // Return the number of rows in the section.
71  switch (section)
72  {
73  case SECTION_TSGATEWAY_SETTINGS: // ts gateway settings
74  return 5;
75  default:
76  break;
77  }
78 
79  return 0;
80 }
81 
82 // set section headers
83 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
84 {
85  switch (section)
86  {
87  case SECTION_TSGATEWAY_SETTINGS:
88  return NSLocalizedString(@"TS Gateway", @"'TS Gateway': ts gateway settings header");
89  }
90  return @"unknown";
91 }
92 
93 - (UITableViewCell *)tableView:(UITableView *)tableView
94  cellForRowAtIndexPath:(NSIndexPath *)indexPath
95 {
96  // determine the required cell type
97  NSString *cellType = nil;
98  switch ([indexPath section])
99  {
100  case SECTION_TSGATEWAY_SETTINGS: // advanced settings
101  {
102  switch ([indexPath row])
103  {
104  case 0: // hostname
105  case 1: // port
106  case 2: // username
107  case 4: // domain
108  cellType = TableCellIdentifierText;
109  break;
110  case 3: // password
111  cellType = TableCellIdentifierSecretText;
112  break;
113  default:
114  break;
115  }
116  break;
117  }
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  // advanced settings
129  case SECTION_TSGATEWAY_SETTINGS:
130  [self initGatewaySettings:indexPath cell:cell];
131  break;
132 
133  default:
134  break;
135  }
136 
137  return cell;
138 }
139 
140 // updates server settings in the UI
141 - (void)initGatewaySettings:(NSIndexPath *)indexPath cell:(UITableViewCell *)cell
142 {
143  EditTextTableViewCell *textCell = (EditTextTableViewCell *)cell;
144  [[textCell textfield] setTag:GET_TAG_FROM_PATH(indexPath)];
145  switch ([indexPath row])
146  {
147  case 0:
148  {
149  [[textCell label] setText:NSLocalizedString(@"Host", @"'Host': Bookmark hostname")];
150  [[textCell textfield] setText:[_params StringForKey:@"tsg_hostname"]];
151  [[textCell textfield]
152  setPlaceholder:NSLocalizedString(@"not set", @"not set placeholder")];
153  break;
154  }
155  case 1:
156  {
157  int port = [_params intForKey:@"tsg_port"];
158  if (port == 0)
159  port = 443;
160  [[textCell label] setText:NSLocalizedString(@"Port", @"'Port': Bookmark port")];
161  [[textCell textfield] setText:[NSString stringWithFormat:@"%d", port]];
162  [[textCell textfield] setKeyboardType:UIKeyboardTypeNumberPad];
163  break;
164  }
165  case 2:
166  {
167  [[textCell textfield] setTag:GET_TAG_FROM_PATH(indexPath)];
168  [[textCell label]
169  setText:NSLocalizedString(@"Username", @"'Username': Bookmark username")];
170  [[textCell textfield] setText:[_params StringForKey:@"tsg_username"]];
171  [[textCell textfield]
172  setPlaceholder:NSLocalizedString(@"not set", @"not set placeholder")];
173  break;
174  }
175  case 3:
176  {
177  [[textCell textfield] setTag:GET_TAG_FROM_PATH(indexPath)];
178  [[textCell label]
179  setText:NSLocalizedString(@"Password", @"'Password': Bookmark password")];
180  [[textCell textfield] setText:[_params StringForKey:@"tsg_password"]];
181  [[textCell textfield]
182  setPlaceholder:NSLocalizedString(@"not set", @"not set placeholder")];
183  break;
184  }
185  case 4:
186  {
187  [[textCell textfield] setTag:GET_TAG_FROM_PATH(indexPath)];
188  [[textCell label] setText:NSLocalizedString(@"Domain", @"'Domain': Bookmark domain")];
189  [[textCell textfield] setText:[_params StringForKey:@"tsg_domain"]];
190  [[textCell textfield]
191  setPlaceholder:NSLocalizedString(@"not set", @"not set placeholder")];
192  break;
193  }
194  default:
195  NSLog(@"Invalid row index in settings table!");
196  break;
197  }
198 
199  [self adjustEditTextTableViewCell:textCell];
200 }
201 
202 #pragma mark -
203 #pragma mark Text Field delegate
204 
205 - (BOOL)textFieldShouldReturn:(UITextField *)textField
206 {
207  [textField resignFirstResponder];
208  return NO;
209 }
210 
211 - (BOOL)textFieldShouldEndEditing:(UITextField *)textField
212 {
213  switch (textField.tag)
214  {
215  // update server settings
216  case GET_TAG(SECTION_TSGATEWAY_SETTINGS, 0):
217  [_params setValue:[textField text] forKey:@"tsg_hostname"];
218  break;
219 
220  case GET_TAG(SECTION_TSGATEWAY_SETTINGS, 1):
221  [_params setInt:[[textField text] intValue] forKey:@"tsg_port"];
222  break;
223 
224  case GET_TAG(SECTION_TSGATEWAY_SETTINGS, 2):
225  [_params setValue:[textField text] forKey:@"tsg_username"];
226  break;
227 
228  case GET_TAG(SECTION_TSGATEWAY_SETTINGS, 3):
229  [_params setValue:[textField text] forKey:@"tsg_password"];
230  break;
231 
232  case GET_TAG(SECTION_TSGATEWAY_SETTINGS, 4):
233  [_params setValue:[textField text] forKey:@"tsg_domain"];
234  break;
235 
236  default:
237  break;
238  }
239  return YES;
240 }
241 
242 @end