b7bc7415c75924cc52753996f3f013c6ee9a00fe
[pithos-ios] / Classes / AccountSettingsViewController.m
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 "Provider.h"
12 #import "RSTextFieldCell.h"
13 #import "UIColor+MoreColors.h"
14 #import "OpenStackAppDelegate.h"
15 #import "UIViewController+Conveniences.h"
16 #import "OpenStackRequest.h"
17
18 #define kUsername 0
19 #define kAPIKey 1
20
21 @implementation AccountSettingsViewController
22
23 @synthesize account, userName, apiKey, activityIndicatorView;
24
25 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
26     return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == UIInterfaceOrientationPortrait);
27 }
28
29 #pragma mark -
30 #pragma mark HTTP Response Handlers
31
32 - (void)authenticationSucceded:(OpenStackRequest *)request {
33     
34     [self.activityIndicatorView removeFromSuperview];
35
36     if ([request isSuccess]) {        
37         account.authToken = [[request responseHeaders] objectForKey:@"X-Auth-Token"];
38         
39         NSString *filesStorageURL;
40         if (![[request responseHeaders] objectForKey:@"X-Storage-URL"]) {
41             filesStorageURL = [NSString stringWithFormat:@"%@/v1/%@",
42                                [account.hostURL absoluteString],
43                                [account username]];
44             account.filesURL = [NSURL URLWithString:filesStorageURL];
45         }
46         else {
47             account.filesURL = [NSURL URLWithString:[[request responseHeaders] objectForKey:@"X-Storage-Url"]];
48         }
49         
50         account.pithosPublicLinkURLPrefix = account.hostURL;
51         account.pithosLoginURLPrefix = [NSURL URLWithString:
52                                         [[account.hostURL absoluteString]
53                                          stringByAppendingString:@"/login"]];
54
55         [account persist];
56         [account refreshCollections];
57         [self.navigationController dismissModalViewControllerAnimated:YES];
58     } else {
59         self.navigationItem.rightBarButtonItem.enabled = NO;
60         [self alert:@"Authentication Failure" message:@"Please check your User Name and Token."];
61     }
62 }
63
64 - (void)authenticationFailed:(OpenStackRequest *)request {
65     [self.activityIndicatorView removeFromSuperview];
66     self.navigationItem.rightBarButtonItem.enabled = YES;
67     self.account.containers = nil;
68     if ([request responseStatusCode] == 401) {
69         [self alert:@"Authentication Failure" message:@"Please check your User Name and Token."];
70     } else {
71         [self failOnBadConnection];
72     }
73 }
74
75
76 - (void)authenticate {
77     
78     BOOL valid = YES;
79     
80         valid = valid && usernameTextField.text && ![@"" isEqualToString:usernameTextField.text];
81         if (!valid) {
82             [self alert:nil message:@"Please enter your username."];
83             [usernameTextField becomeFirstResponder];
84         } else {
85             valid = valid && apiKeyTextField.text && ![@"" isEqualToString:apiKeyTextField.text];
86             if (!valid) {
87                 [self alert:nil message:@"Please enter your token."];
88                 [apiKeyTextField becomeFirstResponder];
89             } else {
90                 account.username = usernameTextField.text;
91                 account.apiKey = apiKeyTextField.text;                        
92                 
93                 self.activityIndicatorView = [[[ActivityIndicatorView alloc] initWithFrame:[ActivityIndicatorView frameForText:@"Authenticating..."] text:@"Authenticating..."] autorelease];
94                 [self.activityIndicatorView addToView:self.view];
95                 
96                 OpenStackRequest *request = [OpenStackRequest authenticationRequest:account];
97                 request.delegate = self;
98                 request.didFinishSelector = @selector(authenticationSucceded:);
99                 request.didFailSelector = @selector(authenticationFailed:);
100                 [request startAsynchronous];
101             }
102         }
103 }
104
105
106
107 #pragma mark -
108 #pragma mark Button Handlers
109
110 - (void)saveButtonPressed:(id)sender {
111     [self authenticate];
112 }
113
114
115 #pragma mark - View lifecycle
116
117 - (void)viewDidLoad {
118     [super viewDidLoad];
119     self.navigationItem.title = @"API Account Info";
120     userDetailsSection = 0;
121     getTokenSection = 1;
122     
123     /*if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
124         UIView *backgroundContainer = [[UIView alloc] init];
125         backgroundContainer.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
126         backgroundContainer.backgroundColor = [UIColor iPadTableBackgroundColor];
127         NSString *logoFilename = @"account-settings-icon-large.png";
128         UIImageView *osLogo = [[UIImageView alloc] initWithImage:[UIImage imageNamed:logoFilename]];
129         osLogo.contentMode = UIViewContentModeScaleAspectFit;
130         osLogo.frame = CGRectMake(100.0, 100.0, 1000.0, 1000.0);
131         if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
132             osLogo.alpha = 0.3;
133         }
134         [backgroundContainer addSubview:osLogo];
135         [osLogo release];
136         self.tableView.backgroundView = backgroundContainer;
137         [backgroundContainer release];
138     }   */ 
139     
140     [self addSaveButton];
141 }
142
143
144 - (void)viewDidAppear:(BOOL)animated {
145     [super viewDidAppear:animated];    
146     //[usernameTextField becomeFirstResponder];
147 }
148
149 #pragma mark - Table view data source
150
151 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
152     return 2;
153 }
154
155 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
156     if (section == userDetailsSection)
157         return 2;
158     else
159         return 1;
160 }
161
162 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
163     if (section == userDetailsSection)
164         return [NSString stringWithFormat:@"%@ Login", self.account.provider.name];
165     else 
166         return nil;
167 }
168
169 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
170     if (section == userDetailsSection)
171         return @"API Version 1.0";
172     else
173         return nil;
174 }
175
176 // Customize the appearance of table view cells.
177 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
178     
179     static NSString *CellIdentifier = @"Cell";
180     RSTextFieldCell *cell = nil;
181     if (indexPath.section == userDetailsSection) {
182         cell = (RSTextFieldCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
183         if (cell == nil) {
184             cell = [[[RSTextFieldCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
185             cell.selectionStyle = UITableViewCellSelectionStyleNone;
186         }
187     
188         if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
189             cell.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.8];
190         }
191
192         [cell.textLabel setBackgroundColor:[UIColor clearColor]];
193
194         if (indexPath.row == kUsername) {
195             cell.textLabel.text = @"Username";
196             usernameTextField = cell.textField;
197             usernameTextField.delegate = self;
198             usernameTextField.secureTextEntry = NO;
199             usernameTextField.returnKeyType = UIReturnKeyNext;
200             usernameTextField.text = self.account.username;
201             usernameTextField.placeholder = @"username";
202             if (userName)
203                 usernameTextField.text = userName;
204         } else if (indexPath.row == kAPIKey) {
205             cell.textLabel.text = @"Token";
206             apiKeyTextField = cell.textField;
207             apiKeyTextField.secureTextEntry = YES;
208             apiKeyTextField.delegate = self;
209             apiKeyTextField.returnKeyType = UIReturnKeyDone;
210             apiKeyTextField.text = self.account.apiKey;
211             if (apiKey)
212                 apiKeyTextField.text = apiKey;
213         }
214     } else {
215         cell = (RSTextFieldCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
216         if (cell == nil) {
217             cell = [[[RSTextFieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
218         }
219         
220         cell.textLabel.text = @"Get Token";
221     }
222     return cell;
223 }
224
225 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
226     if (indexPath.section == getTokenSection) {
227         NSString *loginURL = [NSString stringWithFormat:@"%@?next=pithos://login&force=", account.pithosLoginURLPrefix];         
228         [[UIApplication sharedApplication] openURL:[NSURL URLWithString:loginURL]];
229         [tableView deselectRowAtIndexPath:indexPath animated:NO];
230     }
231 }
232
233 #pragma mark - Text Field Delegate
234
235 - (BOOL)textFieldShouldReturn:(UITextField *)textField {    
236     [textField resignFirstResponder];    
237     if ([textField isEqual:usernameTextField]) {
238         [apiKeyTextField becomeFirstResponder];
239     }
240     return NO;
241 }
242
243 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
244     NSString *result = [textField.text stringByReplacingCharactersInRange:range withString:string];
245     if ([textField isEqual:usernameTextField]) {
246         self.account.username = result;
247     } else if ([textField isEqual:apiKeyTextField]) {
248         self.account.apiKey = result;
249     }
250     self.account.authToken = @"";
251     self.account.hasBeenRefreshed = NO;
252     [self.account persist];
253     
254     return YES;
255 }
256
257 #pragma mark - Memory management
258
259 - (void)dealloc {
260     [userName release];
261     [apiKey release];
262     [activityIndicatorView release];
263     [account release];
264     [super dealloc];
265 }
266
267
268 @end
269