Fix compile errors
[pithos-ios] / Classes / ChefSettingsViewController.m
1 //
2 //  ChefSettingsViewController.m
3 //  OpenStack
4 //
5 //  Created by Mike Mayo on 10/29/10.
6 //  The OpenStack project is provided under the Apache 2.0 license.
7 //
8
9 #import "ChefSettingsViewController.h"
10 #import "ChefValidationKeyViewController.h"
11 #import "UIColor+MoreColors.h"
12 #import "SettingsViewController.h"
13
14 #define kChefBootstrappingEnabled 0
15 #define kChefEndpointType 1
16 #define kChefEndpointConfiguration 2
17 #define kChefValidationKey 3
18
19 @implementation ChefSettingsViewController
20
21 @synthesize settingsViewController;
22
23 #pragma mark -
24 #pragma mark Switch
25
26 - (void)chefBootstrappingSwitchChanged:(id)sender {
27     
28     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
29     NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 3)];
30     
31     if (chefBootstrappingSwitch.on) {
32         [self.tableView insertSections:sections withRowAnimation:UITableViewRowAnimationBottom];
33         [defaults setValue:@"YES" forKey:@"chef_bootstrapping_on"];
34     } else {
35         [self.tableView deleteSections:sections withRowAnimation:UITableViewRowAnimationTop];
36         [defaults setValue:@"NO" forKey:@"chef_bootstrapping_on"];
37     }
38     [defaults synchronize];
39     
40     [self.settingsViewController.tableView reloadData];
41 }
42
43 #pragma mark -
44 #pragma mark View lifecycle
45
46 - (void)viewDidLoad {
47     [super viewDidLoad];
48     self.navigationItem.title = @"Chef Bootstrapping";
49     
50     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
51     
52     chefBootstrappingSwitch = [[UISwitch alloc] init];
53     chefBootstrappingSwitch.on = [[defaults stringForKey:@"chef_bootstrapping_on"] isEqualToString:@"YES"];    
54     [chefBootstrappingSwitch addTarget:self action:@selector(chefBootstrappingSwitchChanged:) forControlEvents:UIControlEventValueChanged];
55     
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);
59     }
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"];
70
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);
74     }
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"];
85 }
86
87 - (void)viewWillAppear:(BOOL)animated {
88     [super viewWillAppear:animated];
89 }
90
91
92
93 #pragma mark -
94 #pragma mark Table view data source
95
96 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
97     // Return the number of sections.
98     return chefBootstrappingSwitch.on ? 4 : 1;
99 }
100
101
102 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
103     // Return the number of rows in the section.
104     if (section == kChefBootstrappingEnabled) {
105         return 1;
106     } else if (section == kChefEndpointType) {
107         return 2;
108     } else {
109         return 1;
110     }
111 }
112
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";
119         return @"";
120     } else {
121         return @"";
122     }
123 }
124
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.";
128     } else {
129         return @"";
130     }
131 }
132
133 // Customize the appearance of table view cells.
134 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
135     
136     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
137     
138     static NSString *CellIdentifier = @"Cell";
139     
140     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
141     if (cell == nil) {
142         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
143         cell.textLabel.backgroundColor = [UIColor clearColor];
144     }
145     
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;
161             } else {
162                 cell.accessoryType = UITableViewCellAccessoryNone;
163             }
164         } else {
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;
168             } else {
169                 cell.accessoryType = UITableViewCellAccessoryNone;
170             }
171         }
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;
179         } else {
180             cell.textLabel.text = @"Chef URL";
181             cell.accessoryView = chefURLTextField;
182         }
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";
189     }
190     
191     return cell;
192 }
193
194 #pragma mark -
195 #pragma mark Table view delegate
196
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];
201         [vc release];
202     } else if (indexPath.section == kChefEndpointType) {
203         NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
204         NSString *endpointType = [defaults stringForKey:@"chef_bootstrapping_endpoint_type"];
205         
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];
216         }
217 //        [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
218         
219     }
220 }
221
222 #pragma mark -
223 #pragma mark Text Field delegate
224
225 - (BOOL)textFieldShouldReturn:(UITextField *)textField {
226     [textField resignFirstResponder];
227     return NO;
228 }
229
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"];
237     }
238     [defaults synchronize];
239     return YES;
240 }
241
242 #pragma mark -
243 #pragma mark Memory management
244
245 - (void)didReceiveMemoryWarning {
246     // Releases the view if it doesn't have a superview.
247     [super didReceiveMemoryWarning];
248     
249     // Relinquish ownership any cached data, images, etc that aren't in use.
250 }
251
252 - (void)viewDidUnload {
253     // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
254     // For example: self.myOutlet = nil;
255 }
256
257
258 - (void)dealloc {
259     [settingsViewController release];
260     [chefBootstrappingSwitch release];
261     [opscodeOrgTextField release];
262     [chefURLTextField release];
263     [super dealloc];
264 }
265
266
267 @end
268