Statistics
| Branch: | Tag: | Revision:

root / Classes / OpenStackAccount.m @ ef74c42f

History | View | Annotate | Download (7.4 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, filesURL, 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
- (NSURL *)filesURL {
121
    return [self.provider.authEndpointURL URLByAppendingPathComponent:self.username];
122
}
123

    
124
- (void)setShared:(BOOL)aShared {
125
    if (shared != aShared) {
126
        self.containers = nil;
127
    }
128
    shared = aShared;
129
}
130

    
131
- (void)setSharingAccount:(NSString *)aSharingAccount {
132
    if ((aSharingAccount && ![aSharingAccount isEqualToString:sharingAccount]) || (!aSharingAccount && sharingAccount)) {
133
        self.containers = nil;
134
    }
135
    [sharingAccount release];
136
    sharingAccount = [aSharingAccount retain];
137
}
138

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

    
142
- (NSString *)apiKeyKeychainKey {
143
    return [NSString stringWithFormat:@"%@-apiKey", self.uuid];
144
}
145

    
146
- (NSString *)apiKey {
147
    return [Keychain getStringForKey:[self apiKeyKeychainKey]];
148
}
149

    
150
- (void)setApiKey:(NSString *)newAPIKey {
151
    [Keychain setString:newAPIKey forKey:[self apiKeyKeychainKey]];
152
}
153

    
154
- (NSString *)authTokenKeychainKey {
155
    return [NSString stringWithFormat:@"%@-authToken", self.uuid];
156
}
157

    
158
- (NSString *)authToken {
159
    NSString *authToken = [Keychain getStringForKey:[self authTokenKeychainKey]];
160
    if (!authToken) {
161
        authToken = @"";
162
    }
163
    return authToken;
164
}
165

    
166
- (void)setAuthToken:(NSString *)newAuthToken {
167
    [Keychain setString:newAuthToken forKey:[self authTokenKeychainKey]];
168
}
169

    
170
#pragma mark - Class Actions
171

    
172
+ (NSArray *)accounts {
173
    if (accounts == nil) {
174
        accounts = [[Archiver retrieve:@"accounts"] retain];
175
    }
176
    return accounts;
177
}
178

    
179
+ (void)persist:(NSArray *)accountArray {
180
    accounts = [[NSArray arrayWithArray:accountArray] retain];
181
    [Archiver persist:accounts key:@"accounts"];
182
    [accounts release];
183
    accounts = nil;
184
}
185

    
186
#pragma mark - Actions
187

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

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

    
227
- (NSString *)displaynameForUUID:(NSString *)UUID safe:(BOOL)safe {
228
    NSString *displayName = [userCatalog objectForKey:UUID];
229
    if (safe && !displayName) {
230
        return UUID;
231
    } else {
232
        return displayName;
233
    }
234
}
235

    
236
- (NSString *)displaynameForUUID:(NSString *)UUID {
237
    return [self displaynameForUUID:UUID safe:NO];
238
}
239

    
240
#pragma mark - Memory Management
241

    
242
- (void)dealloc {
243
    [uuid release];
244
    [manager release];
245
    [provider release];
246
    [username release];
247
    [containers release];
248
    [sharingAccount release];
249
    [bytesUsed release];
250
    [policyQuota release];
251
    [userCatalog release];
252
    
253
    [super dealloc];
254
}
255

    
256
@end