root / Classes / AccountSettingsViewController.m @ dd072154
History | View | Annotate | Download (9.3 kB)
1 |
// |
---|---|
2 |
// AccountSettingsViewController.m |
3 |
// OpenStack |
4 |
// |
5 |
// Created by Mike Mayo on 12/14/10. |
6 |
// The OpenStack project is provided under the Apache 2.0 license. |
7 |
// |
8 |
|
9 |
#import "AccountSettingsViewController.h" |
10 |
#import "OpenStackAccount.h" |
11 |
#import "ActivityIndicatorView.h" |
12 |
#import "Provider.h" |
13 |
#import "RSTextFieldCell.h" |
14 |
#import "UIColor+MoreColors.h" |
15 |
#import "OpenStackAppDelegate.h" |
16 |
#import "UIViewController+Conveniences.h" |
17 |
#import "OpenStackRequest.h" |
18 |
|
19 |
#define kUsername 0 |
20 |
#define kAuthToken 1 |
21 |
|
22 |
@implementation AccountSettingsViewController |
23 |
|
24 |
@synthesize account, username, authToken; |
25 |
|
26 |
#pragma mark - View lifecycle |
27 |
|
28 |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { |
29 |
return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == UIInterfaceOrientationPortrait); |
30 |
} |
31 |
|
32 |
- (void)viewDidLoad { |
33 |
[super viewDidLoad]; |
34 |
self.navigationItem.title = @"API Account Info"; |
35 |
userDetailsSection = 0; |
36 |
getTokenSection = 1; |
37 |
[self addSaveButton]; |
38 |
self.navigationItem.rightBarButtonItem.enabled = NO; |
39 |
} |
40 |
|
41 |
#pragma mark - Memory management |
42 |
|
43 |
- (void)dealloc { |
44 |
[username release]; |
45 |
[authToken release]; |
46 |
[account release]; |
47 |
[super dealloc]; |
48 |
} |
49 |
|
50 |
#pragma mark - Internal |
51 |
|
52 |
- (void) updateSaveButtonForUsername:(NSString *)checkUsername andAuthToken:(NSString *)checkAuthToken { |
53 |
self.navigationItem.rightBarButtonItem.enabled = (checkUsername && checkUsername.length && |
54 |
checkAuthToken &&checkAuthToken.length && |
55 |
(![checkUsername isEqualToString:account.username] || |
56 |
![checkAuthToken isEqualToString:account.authToken])); |
57 |
} |
58 |
|
59 |
#pragma mark - Properties |
60 |
|
61 |
- (void)setUsername:(NSString *)aUsername { |
62 |
[username release]; |
63 |
username = [aUsername retain]; |
64 |
[self updateSaveButtonForUsername:username andAuthToken:authTokenTextField.text]; |
65 |
} |
66 |
|
67 |
- (void)setAuthToken:(NSString *)anAuthToken { |
68 |
[authToken release]; |
69 |
authToken = [anAuthToken retain]; |
70 |
[self updateSaveButtonForUsername:usernameTextField.text andAuthToken:authToken]; |
71 |
} |
72 |
|
73 |
#pragma mark - Actions |
74 |
|
75 |
- (void)setUsername:(NSString *)aUsername andAuthToken:(NSString *)anAuthToken { |
76 |
if (aUsername) { |
77 |
[username release]; |
78 |
username = [aUsername retain]; |
79 |
} |
80 |
if (anAuthToken) { |
81 |
[authToken release]; |
82 |
authToken = [anAuthToken retain]; |
83 |
} |
84 |
[self updateSaveButtonForUsername:username andAuthToken:authToken]; |
85 |
} |
86 |
|
87 |
#pragma mark - UITableViewDataSource |
88 |
|
89 |
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { |
90 |
return 2; |
91 |
} |
92 |
|
93 |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
94 |
if (section == userDetailsSection) |
95 |
return 2; |
96 |
else |
97 |
return 1; |
98 |
} |
99 |
|
100 |
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { |
101 |
if (section == userDetailsSection) |
102 |
return [NSString stringWithFormat:@"%@ Login", self.account.provider.name]; |
103 |
else |
104 |
return nil; |
105 |
} |
106 |
|
107 |
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { |
108 |
if (section == userDetailsSection) |
109 |
return @"API Version 1.0"; |
110 |
else |
111 |
return nil; |
112 |
} |
113 |
|
114 |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
115 |
static NSString *CellIdentifier = @"Cell"; |
116 |
RSTextFieldCell *cell = nil; |
117 |
if (indexPath.section == userDetailsSection) { |
118 |
cell = (RSTextFieldCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
119 |
if (cell == nil) { |
120 |
cell = [[[RSTextFieldCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; |
121 |
cell.selectionStyle = UITableViewCellSelectionStyleNone; |
122 |
} |
123 |
|
124 |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { |
125 |
cell.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.8]; |
126 |
} |
127 |
|
128 |
[cell.textLabel setBackgroundColor:[UIColor clearColor]]; |
129 |
|
130 |
if (indexPath.row == kUsername) { |
131 |
cell.textLabel.text = @"Username"; |
132 |
usernameTextField = cell.textField; |
133 |
usernameTextField.delegate = self; |
134 |
usernameTextField.secureTextEntry = NO; |
135 |
usernameTextField.returnKeyType = UIReturnKeyNext; |
136 |
if (username) { |
137 |
usernameTextField.text = username; |
138 |
} else { |
139 |
usernameTextField.text = self.account.username; |
140 |
} |
141 |
} else if (indexPath.row == kAuthToken) { |
142 |
cell.textLabel.text = @"Token"; |
143 |
authTokenTextField = cell.textField; |
144 |
authTokenTextField.secureTextEntry = NO; |
145 |
authTokenTextField.delegate = self; |
146 |
authTokenTextField.returnKeyType = UIReturnKeyDone; |
147 |
if (authToken) { |
148 |
authTokenTextField.text = authToken; |
149 |
} else { |
150 |
authTokenTextField.text = self.account.authToken; |
151 |
} |
152 |
} |
153 |
} else { |
154 |
cell = (RSTextFieldCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
155 |
if (cell == nil) { |
156 |
cell = [[[RSTextFieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
157 |
} |
158 |
|
159 |
cell.textLabel.text = @"Get Token"; |
160 |
} |
161 |
return cell; |
162 |
} |
163 |
|
164 |
#pragma mark - UITableViewDelegate |
165 |
|
166 |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
167 |
if (indexPath.section == getTokenSection) { |
168 |
NSString *protocol = [[[[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleURLTypes"] objectAtIndex:0] objectForKey:@"CFBundleURLSchemes"] objectAtIndex:0]; |
169 |
NSString *loginURL = [NSString stringWithFormat:@"%@?next=%@://login&force=", account.pithosLoginURLPrefix, protocol]; |
170 |
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:loginURL]]; |
171 |
[tableView deselectRowAtIndexPath:indexPath animated:NO]; |
172 |
} |
173 |
} |
174 |
|
175 |
#pragma mark - UITextFieldDelegate |
176 |
|
177 |
- (BOOL)textFieldShouldReturn:(UITextField *)textField { |
178 |
[textField resignFirstResponder]; |
179 |
if ([textField isEqual:usernameTextField]) { |
180 |
[authTokenTextField becomeFirstResponder]; |
181 |
} |
182 |
return NO; |
183 |
} |
184 |
|
185 |
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { |
186 |
NSString *result = [textField.text stringByReplacingCharactersInRange:range withString:string]; |
187 |
if ([textField isEqual:usernameTextField]) { |
188 |
self.username = result; |
189 |
} else if ([textField isEqual:authTokenTextField]) { |
190 |
self.authToken = result; |
191 |
} |
192 |
return YES; |
193 |
} |
194 |
|
195 |
#pragma mark - Button Handlers |
196 |
|
197 |
- (void)saveButtonPressed:(id)sender { |
198 |
self.navigationItem.rightBarButtonItem.enabled = NO; |
199 |
[self authenticate]; |
200 |
} |
201 |
|
202 |
#pragma mark - Authentication |
203 |
|
204 |
- (void)authenticate { |
205 |
if (!usernameTextField.text || [usernameTextField.text isEqualToString:@""]) { |
206 |
[self alert:nil message:@"Please enter your Username."]; |
207 |
self.navigationItem.rightBarButtonItem.enabled = YES; |
208 |
[usernameTextField becomeFirstResponder]; |
209 |
} else if (!authTokenTextField.text || [authTokenTextField.text isEqualToString:@""]) { |
210 |
[self alert:nil message:@"Please enter your Token."]; |
211 |
self.navigationItem.rightBarButtonItem.enabled = YES; |
212 |
[authTokenTextField becomeFirstResponder]; |
213 |
} else { |
214 |
OpenStackAccount *temporaryAccount = [[[OpenStackAccount alloc] init] autorelease]; |
215 |
temporaryAccount.provider = account.provider; |
216 |
temporaryAccount.username = usernameTextField.text; |
217 |
temporaryAccount.authToken = authTokenTextField.text; |
218 |
|
219 |
__block ActivityIndicatorView *activityIndicatorView = [ActivityIndicatorView activityIndicatorViewWithText:@"Authenticating..." |
220 |
andAddToView:self.view]; |
221 |
OpenStackRequest *request = [OpenStackRequest authenticationRequest:temporaryAccount]; |
222 |
request.completionBlock = ^{ |
223 |
[activityIndicatorView removeFromSuperview]; |
224 |
if ([request isSuccess]) { |
225 |
account.username = request.account.username; |
226 |
account.authToken = request.account.authToken; |
227 |
NSString *storageURLString = [[request responseHeaders] objectForKey:@"X-Storage-Url"]; |
228 |
if (storageURLString) { |
229 |
account.filesURL = [NSURL URLWithString:storageURLString]; |
230 |
} else { |
231 |
account.filesURL = [[account.hostURL URLByAppendingPathComponent:@"v1"] URLByAppendingPathComponent:account.username]; |
232 |
} |
233 |
[account persist]; |
234 |
} else { |
235 |
self.navigationItem.rightBarButtonItem.enabled = YES; |
236 |
[self alert:@"Authentication Failure" message:@"Please check your Username and Token."]; |
237 |
} |
238 |
}; |
239 |
request.failedBlock = ^{ |
240 |
[activityIndicatorView removeFromSuperview]; |
241 |
self.navigationItem.rightBarButtonItem.enabled = YES; |
242 |
if ([request responseStatusCode] == 401) { |
243 |
[self alert:@"Authentication Failure" message:@"Please check your Username and Token."]; |
244 |
} else { |
245 |
[self failOnBadConnection]; |
246 |
} |
247 |
}; |
248 |
[request startAsynchronous]; |
249 |
} |
250 |
} |
251 |
|
252 |
@end |
253 |
|