Statistics
| Branch: | Tag: | Revision:

root / Classes / OpenStackAccount.m @ 62ea6d49

History | View | Annotate | Download (7.2 kB)

1
//
2
//  OpenStackAccount.m
3
//  OpenStack
4
//
5
//  Created by Mike Mayo on 10/1/10.
6
//  The OpenStack project is provided under the Apache 2.0 license.
7
//
8

    
9
#import "OpenStackAccount.h"
10
#import "Keychain.h"
11
#import "Provider.h"
12
#import "Archiver.h"
13
#import "OpenStackRequest.h"
14
#import "NSObject+Conveniences.h"
15
#import "AccountManager.h"
16
#import "APICallback.h"
17

    
18
static NSArray *accounts = nil;
19

    
20
@implementation OpenStackAccount
21

    
22
@synthesize uuid, provider, username, manager,
23
            bytesUsed, policyQuota, containers, hasBeenRefreshed, flaggedForDelete,
24
            shared, sharingAccount, userCatalog, ignoreSSLErrors;
25

    
26
+ (void)initialize {
27
    accounts = [[Archiver retrieve:@"accounts"] retain];
28
    if (accounts == nil) {
29
        accounts = [[NSArray alloc] init];
30
        [Archiver persist:accounts key:@"accounts"];
31
    }
32
}
33

    
34
#pragma mark - Constructors
35

    
36
- (id)init {
37
    if ((self = [super init])) {
38
        uuid = [[NSString alloc] initWithString:[OpenStackAccount stringWithUUID]];
39
        
40
        self.userCatalog = [NSMutableDictionary dictionary];
41
        
42
        manager = [[AccountManager alloc] init];
43
        manager.account = self;
44
    }
45
    return self;
46
}
47

    
48
- (id)initWithCoder:(NSCoder *)coder {
49
    if ((self = [super init])) {
50
        self.uuid = [self decode:coder key:@"uuid"];
51
        self.provider = [self decode:coder key:@"provider"];
52
        self.username = [self decode:coder key:@"username"];
53
        
54
        self.bytesUsed = [self decode:coder key:@"bytesUsed"];
55
        self.policyQuota = [self decode:coder key:@"policyQuota"];
56
        
57
//        containers = [self decode:coder key:@"containers"];
58
        
59
        self.userCatalog = [self decode:coder key:@"userCatalog"];
60
        if (!userCatalog) {
61
            self.userCatalog = [NSMutableDictionary dictionary];
62
        }
63
        
64
        self.ignoreSSLErrors = [coder decodeBoolForKey:@"ignoreSSLErrors"];
65
        
66
        manager = [[AccountManager alloc] init];
67
        manager.account = self;
68
    }
69
    return self;
70
}
71

    
72
#pragma mark - Serialization
73

    
74
- (id)copyWithZone:(NSZone *)zone {
75
    OpenStackAccount *copy = [[[self class] allocWithZone:zone] init];
76
    copy.uuid = [[self.uuid copy] autorelease];
77
    copy.provider = [[self.provider copy] autorelease];
78
    copy.username = [[self.username copy] autorelease];
79
    copy.apiKey = [[self.apiKey copy] autorelease];
80
    copy.authToken = [[self.authToken copy] autorelease];
81
    
82
    copy.bytesUsed = [[self.bytesUsed copy] autorelease];
83
    copy.policyQuota = [[self.policyQuota copy] autorelease];
84
    
85
    copy.userCatalog = [[self.userCatalog copy] autorelease];
86
    
87
    copy.ignoreSSLErrors = self.ignoreSSLErrors;
88
    
89
    copy.manager = [[[AccountManager alloc] init] autorelease];
90
    copy.manager.account = copy;
91
    return copy;
92
}
93

    
94
- (void)encodeWithCoder: (NSCoder *)coder {
95
    [coder encodeObject:uuid forKey:@"uuid"];
96
    [coder encodeObject:provider forKey:@"provider"];
97
    [coder encodeObject:username forKey:@"username"];
98
    
99
    [coder encodeObject:bytesUsed forKey:@"bytesUsed"];
100
    [coder encodeObject:policyQuota forKey:@"policyQuota"];
101
    
102
//    [coder encodeObject:containers forKey:@"containers"];
103
    
104
    [coder encodeObject:userCatalog forKey:@"userCatalog"];
105
    
106
    [coder encodeBool:ignoreSSLErrors forKey:@"ignoreSSLErrors"];
107
}
108

    
109
- (id)decode:(NSCoder *)coder key:(NSString *)key {
110
    @try {
111
        return [coder decodeObjectForKey:key];
112
    }
113
    @catch (NSException *exception) {
114
        return nil;
115
    }
116
}
117

    
118
#pragma mark - Properties
119

    
120
- (void)setShared:(BOOL)aShared {
121
    if (shared != aShared) {
122
        self.containers = nil;
123
    }
124
    shared = aShared;
125
}
126

    
127
- (void)setSharingAccount:(NSString *)aSharingAccount {
128
    if ((aSharingAccount && ![aSharingAccount isEqualToString:sharingAccount]) || (!aSharingAccount && sharingAccount)) {
129
        self.containers = nil;
130
    }
131
    [sharingAccount release];
132
    sharingAccount = [aSharingAccount retain];
133
}
134

    
135
// the API key and auth token are stored in the Keychain, so overriding the
136
// getter and setter to abstract the encryption away and make it easy to use
137

    
138
- (NSString *)apiKeyKeychainKey {
139
    return [NSString stringWithFormat:@"%@-apiKey", self.uuid];
140
}
141

    
142
- (NSString *)apiKey {
143
    return [Keychain getStringForKey:[self apiKeyKeychainKey]];
144
}
145

    
146
- (void)setApiKey:(NSString *)newAPIKey {
147
    [Keychain setString:newAPIKey forKey:[self apiKeyKeychainKey]];
148
}
149

    
150
- (NSString *)authTokenKeychainKey {
151
    return [NSString stringWithFormat:@"%@-authToken", self.uuid];
152
}
153

    
154
- (NSString *)authToken {
155
    NSString *authToken = [Keychain getStringForKey:[self authTokenKeychainKey]];
156
    if (!authToken) {
157
        authToken = @"";
158
    }
159
    return authToken;
160
}
161

    
162
- (void)setAuthToken:(NSString *)newAuthToken {
163
    [Keychain setString:newAuthToken forKey:[self authTokenKeychainKey]];
164
}
165

    
166
#pragma mark - Class Actions
167

    
168
+ (NSArray *)accounts {
169
    if (accounts == nil) {
170
        accounts = [[Archiver retrieve:@"accounts"] retain];
171
    }
172
    return accounts;
173
}
174

    
175
+ (void)persist:(NSArray *)accountArray {
176
    accounts = [[NSArray arrayWithArray:accountArray] retain];
177
    [Archiver persist:accounts key:@"accounts"];
178
    [accounts release];
179
    accounts = nil;
180
}
181

    
182
#pragma mark - Actions
183

    
184
- (void)persist {
185
    if (!flaggedForDelete) {
186
        NSMutableArray *accountArr = [NSMutableArray arrayWithArray:[OpenStackAccount accounts]];
187
        
188
        BOOL accountPresent = NO;
189
        for (int i = 0; i < [accountArr count]; i++) {
190
            OpenStackAccount *account = [accountArr objectAtIndex:i];
191
            
192
            if ([account.uuid isEqualToString:self.uuid]) {
193
                accountPresent = YES;
194
                [accountArr replaceObjectAtIndex:i withObject:self];
195
                break;
196
            }
197
        }
198
        
199
        if (!accountPresent) {
200
            [accountArr insertObject:self atIndex:0];
201
        }
202
        
203
        [Archiver persist:[NSArray arrayWithArray:accountArr] key:@"accounts"];
204
        [accounts release];
205
        accounts = nil;
206
    }
207
}
208

    
209
- (NSArray *)pithosSortedContainers {
210
    NSMutableArray *pithosSortedContainers = [NSMutableArray array];
211
    if ([self.containers objectForKey:@"pithos"])
212
        [pithosSortedContainers addObject:[self.containers objectForKey:@"pithos"]];
213
    if ([self.containers objectForKey:@"trash"])
214
        [pithosSortedContainers addObject:[self.containers objectForKey:@"trash"]];
215
    
216
    NSMutableDictionary *otherContainers = [NSMutableDictionary dictionaryWithDictionary:self.containers];
217
    [otherContainers removeObjectForKey:@"pithos"];
218
    [otherContainers removeObjectForKey:@"trash"];
219
    [pithosSortedContainers addObjectsFromArray:[[otherContainers allValues] sortedArrayUsingSelector:@selector(compare:)]];
220
    return pithosSortedContainers;
221
}
222

    
223
- (NSString *)displaynameForUUID:(NSString *)UUID safe:(BOOL)safe {
224
    NSString *displayName = [userCatalog objectForKey:UUID];
225
    if (safe && !displayName) {
226
        return UUID;
227
    } else {
228
        return displayName;
229
    }
230
}
231

    
232
- (NSString *)displaynameForUUID:(NSString *)UUID {
233
    return [self displaynameForUUID:UUID safe:NO];
234
}
235

    
236
#pragma mark - Memory Management
237

    
238
- (void)dealloc {
239
    [uuid release];
240
    [manager release];
241
    [provider release];
242
    [username release];
243
    [containers release];
244
    [sharingAccount release];
245
    [bytesUsed release];
246
    [policyQuota release];
247
    [userCatalog release];
248
    
249
    [super dealloc];
250
}
251

    
252
@end