2 // ChefSettingsViewController.m
5 // Created by Mike Mayo on 10/29/10.
6 // The OpenStack project is provided under the Apache 2.0 license.
9 #import "ChefSettingsViewController.h"
10 #import "ChefValidationKeyViewController.h"
11 #import "UIColor+MoreColors.h"
12 #import "SettingsViewController.h"
14 #define kChefBootstrappingEnabled 0
15 #define kChefEndpointType 1
16 #define kChefEndpointConfiguration 2
17 #define kChefValidationKey 3
19 @implementation ChefSettingsViewController
21 @synthesize settingsViewController;
26 - (void)chefBootstrappingSwitchChanged:(id)sender {
28 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
29 NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 3)];
31 if (chefBootstrappingSwitch.on) {
32 [self.tableView insertSections:sections withRowAnimation:UITableViewRowAnimationBottom];
33 [defaults setValue:@"YES" forKey:@"chef_bootstrapping_on"];
35 [self.tableView deleteSections:sections withRowAnimation:UITableViewRowAnimationTop];
36 [defaults setValue:@"NO" forKey:@"chef_bootstrapping_on"];
38 [defaults synchronize];
40 [self.settingsViewController.tableView reloadData];
44 #pragma mark View lifecycle
48 self.navigationItem.title = @"Chef Bootstrapping";
50 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
52 chefBootstrappingSwitch = [[UISwitch alloc] init];
53 chefBootstrappingSwitch.on = [[defaults stringForKey:@"chef_bootstrapping_on"] isEqualToString:@"YES"];
54 [chefBootstrappingSwitch addTarget:self action:@selector(chefBootstrappingSwitchChanged:) forControlEvents:UIControlEventValueChanged];
56 chefURLTextField = [[UITextField alloc] initWithFrame:CGRectMake(30.0, 13.0, 186.0, 24.0)];
57 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
58 chefURLTextField.frame = CGRectMake(30.0, 13.0, 368.0, 24.0);
60 chefURLTextField.delegate = self;
61 chefURLTextField.placeholder = @"Ex: http://mynode/chef";
62 chefURLTextField.font = [UIFont systemFontOfSize:17.0];
63 chefURLTextField.textColor = [UIColor value1DetailTextLabelColor];
64 chefURLTextField.backgroundColor = [UIColor clearColor];
65 chefURLTextField.textAlignment = UITextAlignmentRight;
66 chefURLTextField.returnKeyType = UIReturnKeyDone;
67 chefURLTextField.autocorrectionType = UITextAutocorrectionTypeNo;
68 chefURLTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
69 chefURLTextField.text = [defaults valueForKey:@"chef_bootstrapping_chef_server_url"];
71 opscodeOrgTextField = [[UITextField alloc] initWithFrame:CGRectMake(60.0, 13.0, 156.0, 24.0)];
72 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
73 opscodeOrgTextField.frame = CGRectMake(60.0, 13.0, 338.0, 24.0);
75 opscodeOrgTextField.delegate = self;
76 opscodeOrgTextField.placeholder = @"Ex: greenisus";
77 opscodeOrgTextField.font = [UIFont systemFontOfSize:17.0];
78 opscodeOrgTextField.textColor = [UIColor value1DetailTextLabelColor];
79 opscodeOrgTextField.backgroundColor = [UIColor clearColor];
80 opscodeOrgTextField.textAlignment = UITextAlignmentRight;
81 opscodeOrgTextField.returnKeyType = UIReturnKeyDone;
82 opscodeOrgTextField.autocorrectionType = UITextAutocorrectionTypeNo;
83 opscodeOrgTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
84 opscodeOrgTextField.text = [defaults valueForKey:@"chef_bootstrapping_opscode_organization"];
87 - (void)viewWillAppear:(BOOL)animated {
88 [super viewWillAppear:animated];
94 #pragma mark Table view data source
96 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
97 // Return the number of sections.
98 return chefBootstrappingSwitch.on ? 4 : 1;
102 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
103 // Return the number of rows in the section.
104 if (section == kChefBootstrappingEnabled) {
106 } else if (section == kChefEndpointType) {
113 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
114 if (section == kChefEndpointType) {
115 return @"How are you using Chef?";
116 } else if (section == kChefEndpointConfiguration) {
117 //return @"Chef Server URL";
118 //return @"Opscode Organization";
125 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
126 if (section == kChefValidationKey) {
127 return @"Enter a validator key or choose a .pem file synced from iTunes.";
133 // Customize the appearance of table view cells.
134 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
136 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
138 static NSString *CellIdentifier = @"Cell";
140 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
142 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
143 cell.textLabel.backgroundColor = [UIColor clearColor];
146 // Configure the cell...
147 if (indexPath.section == kChefBootstrappingEnabled) {
148 cell.textLabel.text = @"Chef Bootstrapping";
149 cell.detailTextLabel.text = @"";
150 cell.accessoryView = chefBootstrappingSwitch;
151 cell.selectionStyle = UITableViewCellSelectionStyleNone;
152 } else if (indexPath.section == kChefEndpointType) {
153 cell.detailTextLabel.text = @"";
154 cell.accessoryView = nil;
155 cell.selectionStyle = UITableViewCellSelectionStyleBlue;
156 cell.accessoryType = UITableViewCellAccessoryNone;
157 if (indexPath.row == 0) {
158 cell.textLabel.text = @"I use the Opscode Platform.";
159 if ([[defaults stringForKey:@"chef_bootstrapping_endpoint_type"] isEqualToString:@"opscode"]) {
160 cell.accessoryType = UITableViewCellAccessoryCheckmark;
162 cell.accessoryType = UITableViewCellAccessoryNone;
165 cell.textLabel.text = @"I run my own Chef server.";
166 if ([[defaults stringForKey:@"chef_bootstrapping_endpoint_type"] isEqualToString:@"chef_server"]) {
167 cell.accessoryType = UITableViewCellAccessoryCheckmark;
169 cell.accessoryType = UITableViewCellAccessoryNone;
172 } else if (indexPath.section == kChefEndpointConfiguration) {
173 cell.detailTextLabel.text = @"";
174 cell.accessoryType = UITableViewCellAccessoryNone;
175 cell.selectionStyle = UITableViewCellSelectionStyleNone;
176 if ([[defaults stringForKey:@"chef_bootstrapping_endpoint_type"] isEqualToString:@"opscode"]) {
177 cell.textLabel.text = @"Organization";
178 cell.accessoryView = opscodeOrgTextField;
180 cell.textLabel.text = @"Chef URL";
181 cell.accessoryView = chefURLTextField;
183 } else if (indexPath.section == kChefValidationKey) {
184 cell.detailTextLabel.text = @"";
185 cell.accessoryView = nil;
186 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
187 cell.selectionStyle = UITableViewCellSelectionStyleBlue;
188 cell.textLabel.text = @"Chef Validator Key";
195 #pragma mark Table view delegate
197 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
198 if (indexPath.section == kChefValidationKey) {
199 ChefValidationKeyViewController *vc = [[ChefValidationKeyViewController alloc] initWithNibName:@"ChefValidationKeyViewController" bundle:nil];
200 [self.navigationController pushViewController:vc animated:YES];
202 } else if (indexPath.section == kChefEndpointType) {
203 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
204 NSString *endpointType = [defaults stringForKey:@"chef_bootstrapping_endpoint_type"];
206 if (indexPath.row == 0 && ![endpointType isEqualToString:@"opscode"]) {
207 [defaults setValue:@"opscode" forKey:@"chef_bootstrapping_endpoint_type"];
208 [defaults synchronize];
209 [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:kChefEndpointConfiguration]] withRowAnimation:UITableViewRowAnimationLeft];
210 [NSTimer scheduledTimerWithTimeInterval:0.3 target:self.tableView selector:@selector(reloadData) userInfo:nil repeats:NO];
211 } else if (indexPath.row == 1 && ![endpointType isEqualToString:@"chef_server"]) {
212 [defaults setValue:@"chef_server" forKey:@"chef_bootstrapping_endpoint_type"];
213 [defaults synchronize];
214 [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:kChefEndpointConfiguration]] withRowAnimation:UITableViewRowAnimationLeft];
215 [NSTimer scheduledTimerWithTimeInterval:0.3 target:self.tableView selector:@selector(reloadData) userInfo:nil repeats:NO];
217 // [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
223 #pragma mark Text Field delegate
225 - (BOOL)textFieldShouldReturn:(UITextField *)textField {
226 [textField resignFirstResponder];
230 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
231 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
232 NSString *result = [textField.text stringByReplacingCharactersInRange:range withString:string];
233 if ([textField isEqual:opscodeOrgTextField]) {
234 [defaults setValue:result forKey:@"chef_bootstrapping_opscode_organization"];
235 } else if ([textField isEqual:chefURLTextField]) {
236 [defaults setValue:result forKey:@"chef_bootstrapping_chef_server_url"];
238 [defaults synchronize];
243 #pragma mark Memory management
245 - (void)didReceiveMemoryWarning {
246 // Releases the view if it doesn't have a superview.
247 [super didReceiveMemoryWarning];
249 // Relinquish ownership any cached data, images, etc that aren't in use.
252 - (void)viewDidUnload {
253 // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
254 // For example: self.myOutlet = nil;
259 [settingsViewController release];
260 [chefBootstrappingSwitch release];
261 [opscodeOrgTextField release];
262 [chefURLTextField release];