Statistics
| Branch: | Tag: | Revision:

root / Classes / AccountSettingsViewController.m @ 3a8071d4

History | View | Annotate | Download (10 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 "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
        account.serversURL = [NSURL URLWithString:[[request responseHeaders] objectForKey:@"X-Server-Management-Url"]];
39
        
40
        NSString *filesStorageURL;
41
        if (![[request responseHeaders] objectForKey:@"X-Storage-URL"]) {
42
            filesStorageURL = [NSString stringWithFormat:@"%@/v1/%@",
43
                               [account.hostURL absoluteString],
44
                               [account username]];
45
            account.filesURL = [NSURL URLWithString:filesStorageURL];
46
        }
47
        else {
48
            account.filesURL = [NSURL URLWithString:[[request responseHeaders] objectForKey:@"X-Storage-Url"]];
49
        }
50
        
51
        account.pithosPublicLinkURLPrefix = account.hostURL;
52
        account.pithosLoginURLPrefix = [NSURL URLWithString:
53
                                        [[account.hostURL absoluteString]
54
                                         stringByAppendingString:@"/login"]];
55

    
56
        NSString *cdnStr = [[request responseHeaders] objectForKey:@"X-Cdn-Management-Url"];
57
        if (!cdnStr) {
58
            cdnStr = [[request responseHeaders] objectForKey:@"X-CDN-Management-Url"];
59
        }
60
        if (cdnStr) {
61
            account.cdnURL = [NSURL URLWithString:cdnStr];
62
        }
63
        [account persist];
64
        [account refreshCollections];
65
        [self.navigationController dismissModalViewControllerAnimated:YES];
66
    } else {
67
        self.navigationItem.rightBarButtonItem.enabled = NO;
68
        [self alert:@"Authentication Failure" message:@"Please check your User Name and Token."];
69
    }
70
}
71

    
72
- (void)authenticationFailed:(OpenStackRequest *)request {
73
    [self.activityIndicatorView removeFromSuperview];
74
    self.navigationItem.rightBarButtonItem.enabled = YES;
75
    self.account.containers = nil;
76
    if ([request responseStatusCode] == 401) {
77
        [self alert:@"Authentication Failure" message:@"Please check your User Name and Token."];
78
    } else {
79
        [self failOnBadConnection];
80
    }
81
}
82

    
83

    
84
- (void)authenticate {
85
    
86
    BOOL valid = YES;
87
    
88
        valid = valid && usernameTextField.text && ![@"" isEqualToString:usernameTextField.text];
89
        if (!valid) {
90
            [self alert:nil message:@"Please enter your username."];
91
            [usernameTextField becomeFirstResponder];
92
        } else {
93
            valid = valid && apiKeyTextField.text && ![@"" isEqualToString:apiKeyTextField.text];
94
            if (!valid) {
95
                [self alert:nil message:@"Please enter your token."];
96
                [apiKeyTextField becomeFirstResponder];
97
            } else {
98
                account.username = usernameTextField.text;
99
                account.apiKey = apiKeyTextField.text;                        
100
                
101
                self.activityIndicatorView = [[[ActivityIndicatorView alloc] initWithFrame:[ActivityIndicatorView frameForText:@"Authenticating..."] text:@"Authenticating..."] autorelease];
102
                [self.activityIndicatorView addToView:self.view];
103
                
104
                OpenStackRequest *request = [OpenStackRequest authenticationRequest:account];
105
                request.delegate = self;
106
                request.didFinishSelector = @selector(authenticationSucceded:);
107
                request.didFailSelector = @selector(authenticationFailed:);
108
                [request startAsynchronous];
109
            }
110
        }
111
}
112

    
113

    
114

    
115
#pragma mark -
116
#pragma mark Button Handlers
117

    
118
- (void)saveButtonPressed:(id)sender {
119
    [self authenticate];
120
}
121

    
122

    
123
#pragma mark - View lifecycle
124

    
125
- (void)viewDidLoad {
126
    [super viewDidLoad];
127
    self.navigationItem.title = @"API Account Info";
128
    userDetailsSection = 0;
129
    getTokenSection = 1;
130
    
131
    /*if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
132
        UIView *backgroundContainer = [[UIView alloc] init];
133
        backgroundContainer.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
134
        backgroundContainer.backgroundColor = [UIColor iPadTableBackgroundColor];
135
        NSString *logoFilename = @"account-settings-icon-large.png";
136
        UIImageView *osLogo = [[UIImageView alloc] initWithImage:[UIImage imageNamed:logoFilename]];
137
        osLogo.contentMode = UIViewContentModeScaleAspectFit;
138
        osLogo.frame = CGRectMake(100.0, 100.0, 1000.0, 1000.0);
139
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
140
            osLogo.alpha = 0.3;
141
        }
142
        [backgroundContainer addSubview:osLogo];
143
        [osLogo release];
144
        self.tableView.backgroundView = backgroundContainer;
145
        [backgroundContainer release];
146
    }   */ 
147
    
148
    [self addSaveButton];
149
}
150

    
151

    
152
- (void)viewDidAppear:(BOOL)animated {
153
    [super viewDidAppear:animated];    
154
    //[usernameTextField becomeFirstResponder];
155
}
156

    
157
#pragma mark - Table view data source
158

    
159
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
160
    return 2;
161
}
162

    
163
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
164
    if (section == userDetailsSection)
165
        return 2;
166
    else
167
        return 1;
168
}
169

    
170
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
171
    if (section == userDetailsSection)
172
        return [NSString stringWithFormat:@"%@ Login", self.account.provider.name];
173
    else 
174
        return nil;
175
}
176

    
177
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
178
    if (section == userDetailsSection)
179
        return [NSString stringWithFormat:@"API Version %@", self.account.apiVersion];
180
    else
181
        return nil;
182
}
183

    
184
// Customize the appearance of table view cells.
185
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
186
    
187
    static NSString *CellIdentifier = @"Cell";
188
    RSTextFieldCell *cell = nil;
189
    if (indexPath.section == userDetailsSection) {
190
        cell = (RSTextFieldCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
191
        if (cell == nil) {
192
            cell = [[[RSTextFieldCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
193
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
194
        }
195
    
196
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
197
            cell.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.8];
198
        }
199

    
200
        [cell.textLabel setBackgroundColor:[UIColor clearColor]];
201

    
202
        if (indexPath.row == kUsername) {
203
            cell.textLabel.text = @"Username";
204
            usernameTextField = cell.textField;
205
            usernameTextField.delegate = self;
206
            usernameTextField.secureTextEntry = NO;
207
            usernameTextField.returnKeyType = UIReturnKeyNext;
208
            usernameTextField.text = self.account.username;
209
            usernameTextField.placeholder = @"username";
210
            if (userName)
211
                usernameTextField.text = userName;
212
        } else if (indexPath.row == kAPIKey) {
213
            cell.textLabel.text = @"Token";
214
            apiKeyTextField = cell.textField;
215
            apiKeyTextField.secureTextEntry = YES;
216
            apiKeyTextField.delegate = self;
217
            apiKeyTextField.returnKeyType = UIReturnKeyDone;
218
            apiKeyTextField.text = self.account.apiKey;
219
            if (apiKey)
220
                apiKeyTextField.text = apiKey;
221
        }
222
    } else {
223
        cell = (RSTextFieldCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
224
        if (cell == nil) {
225
            cell = [[[RSTextFieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
226
        }
227
        
228
        cell.textLabel.text = @"Get Token";
229
    }
230
    return cell;
231
}
232

    
233
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
234
    if (indexPath.section == getTokenSection) {
235
        NSString *loginURL = [NSString stringWithFormat:@"%@?next=pithos://login&force=", account.pithosLoginURLPrefix];         
236
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:loginURL]];
237
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
238
    }
239
}
240

    
241
#pragma mark - Text Field Delegate
242

    
243
- (BOOL)textFieldShouldReturn:(UITextField *)textField {    
244
    [textField resignFirstResponder];    
245
    if ([textField isEqual:usernameTextField]) {
246
        [apiKeyTextField becomeFirstResponder];
247
    }
248
    return NO;
249
}
250

    
251
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
252
    NSString *result = [textField.text stringByReplacingCharactersInRange:range withString:string];
253
    if ([textField isEqual:usernameTextField]) {
254
        self.account.username = result;
255
    } else if ([textField isEqual:apiKeyTextField]) {
256
        self.account.apiKey = result;
257
    }
258
    self.account.authToken = @"";
259
    self.account.hasBeenRefreshed = NO;
260
    [self.account persist];
261
    
262
    return YES;
263
}
264

    
265
#pragma mark - Memory management
266

    
267
- (void)dealloc {
268
    [userName release];
269
    [apiKey release];
270
    [activityIndicatorView release];
271
    [account release];
272
    [super dealloc];
273
}
274

    
275

    
276
@end
277