Revision c91b5b28 Classes/OpenStackAccount.m
b/Classes/OpenStackAccount.m | ||
---|---|---|
20 | 20 |
@implementation OpenStackAccount |
21 | 21 |
|
22 | 22 |
@synthesize uuid, provider, username, filesURL, manager, |
23 |
containerCount, bytesUsed, policyQuota, containers, hasBeenRefreshed, flaggedForDelete,
|
|
24 |
shared, sharingAccount, pithosLoginURLPrefix, pithosPublicLinkURLPrefix, hostURL;
|
|
23 |
bytesUsed, policyQuota, containers, hasBeenRefreshed, flaggedForDelete, |
|
24 |
shared, sharingAccount, userCatalog;
|
|
25 | 25 |
|
26 | 26 |
+ (void)initialize { |
27 | 27 |
accounts = [[Archiver retrieve:@"accounts"] retain]; |
... | ... | |
31 | 31 |
} |
32 | 32 |
} |
33 | 33 |
|
34 |
- (NSArray *)pithosSortedContainers { |
|
35 |
NSMutableArray *pithosSortedContainers = [NSMutableArray array]; |
|
36 |
if ([self.containers objectForKey:@"pithos"]) |
|
37 |
[pithosSortedContainers addObject:[self.containers objectForKey:@"pithos"]]; |
|
38 |
if ([self.containers objectForKey:@"trash"]) |
|
39 |
[pithosSortedContainers addObject:[self.containers objectForKey:@"trash"]]; |
|
40 |
|
|
41 |
NSMutableDictionary *otherContainers = [NSMutableDictionary dictionaryWithDictionary:self.containers]; |
|
42 |
[otherContainers removeObjectForKey:@"pithos"]; |
|
43 |
[otherContainers removeObjectForKey:@"trash"]; |
|
44 |
[pithosSortedContainers addObjectsFromArray:[[otherContainers allValues] sortedArrayUsingSelector:@selector(compare:)]]; |
|
45 |
return pithosSortedContainers; |
|
46 |
} |
|
47 |
|
|
48 |
#pragma mark - Properties |
|
49 |
|
|
50 |
- (NSURL *)pithosPublicLinkURLPrefix { |
|
51 |
return self.hostURL; |
|
52 |
} |
|
53 |
|
|
54 |
- (NSURL *)pithosLoginURLPrefix { |
|
55 |
return [self.hostURL URLByAppendingPathComponent:@"login"]; |
|
56 |
} |
|
34 |
#pragma mark - Constructors |
|
57 | 35 |
|
58 |
- (void)setShared:(BOOL)aShared { |
|
59 |
if (shared != aShared) { |
|
60 |
[self.containers removeAllObjects]; |
|
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; |
|
61 | 44 |
} |
62 |
shared = aShared;
|
|
45 |
return self;
|
|
63 | 46 |
} |
64 |
- (void)setSharingAccount:(NSString *)aSharingAccount { |
|
65 |
if ((aSharingAccount && ![aSharingAccount isEqualToString:sharingAccount]) || (!aSharingAccount && sharingAccount)) { |
|
66 |
[self.containers removeAllObjects]; |
|
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.filesURL = [self decode:coder key:@"filesURL"]; |
|
55 |
self.bytesUsed = [self decode:coder key:@"bytesUsed"]; |
|
56 |
self.policyQuota = [self decode:coder key:@"policyQuota"]; |
|
57 |
|
|
58 |
// containers = [self decode:coder key:@"containers"]; |
|
59 |
|
|
60 |
self.userCatalog = [self decode:coder key:@"userCatalog"]; |
|
61 |
if (!userCatalog) { |
|
62 |
self.userCatalog = [NSMutableDictionary dictionary]; |
|
63 |
} |
|
64 |
|
|
65 |
manager = [[AccountManager alloc] init]; |
|
66 |
manager.account = self; |
|
67 | 67 |
} |
68 |
[sharingAccount release]; |
|
69 |
sharingAccount = [aSharingAccount retain]; |
|
68 |
return self; |
|
70 | 69 |
} |
71 | 70 |
|
72 | 71 |
#pragma mark - Serialization |
... | ... | |
80 | 79 |
copy.authToken = self.authToken; |
81 | 80 |
|
82 | 81 |
copy.filesURL = self.filesURL; |
83 |
copy.hostURL = self.hostURL; |
|
84 |
copy.containerCount = self.containerCount; |
|
85 | 82 |
copy.bytesUsed = self.bytesUsed; |
86 | 83 |
copy.policyQuota = self.policyQuota; |
84 |
|
|
85 |
copy.userCatalog = [self.userCatalog copy]; |
|
86 |
|
|
87 | 87 |
manager = [[AccountManager alloc] init]; |
88 | 88 |
manager.account = copy; |
89 | 89 |
return copy; |
... | ... | |
95 | 95 |
[coder encodeObject:username forKey:@"username"]; |
96 | 96 |
|
97 | 97 |
[coder encodeObject:filesURL forKey:@"filesURL"]; |
98 |
[coder encodeObject:hostURL forKey:@"hostURL"]; |
|
99 |
[coder encodeInt:containerCount forKey:@"containerCount"]; |
|
100 | 98 |
[coder encodeObject:bytesUsed forKey:@"bytesUsed"]; |
101 | 99 |
[coder encodeObject:policyQuota forKey:@"policyQuota"]; |
100 |
|
|
101 |
// [coder encodeObject:containers forKey:@"containers"]; |
|
102 |
|
|
103 |
[coder encodeObject:userCatalog forKey:@"userCatalog"]; |
|
102 | 104 |
} |
103 | 105 |
|
104 |
- (id)decode:(NSCoder *)coder key:(NSString *)key {
|
|
106 |
- (id)decode:(NSCoder *)coder key:(NSString *)key { |
|
105 | 107 |
@try { |
106 |
return [[coder decodeObjectForKey:key] retain];
|
|
108 |
return [coder decodeObjectForKey:key];
|
|
107 | 109 |
} |
108 | 110 |
@catch (NSException *exception) { |
109 | 111 |
return nil; |
110 | 112 |
} |
111 | 113 |
} |
112 | 114 |
|
113 |
- (id)initWithCoder:(NSCoder *)coder { |
|
114 |
if ((self = [super init])) { |
|
115 |
uuid = [self decode:coder key:@"uuid"]; |
|
116 |
provider = [self decode:coder key:@"provider"]; |
|
117 |
username = [self decode:coder key:@"username"]; |
|
118 |
|
|
119 |
filesURL = [self decode:coder key:@"filesURL"]; |
|
120 |
hostURL = [self decode:coder key:@"hostURL"]; |
|
121 |
|
|
122 |
containerCount = [coder decodeIntForKey:@"containerCount"]; |
|
115 |
#pragma mark - Properties |
|
123 | 116 |
|
124 |
bytesUsed = [self decode:coder key:@"bytesUsed"]; |
|
125 |
policyQuota = [self decode:coder key:@"policyQuota"]; |
|
126 |
|
|
127 |
containers = [self decode:coder key:@"containers"]; |
|
128 |
|
|
129 |
manager = [[AccountManager alloc] init]; |
|
130 |
manager.account = self; |
|
117 |
- (void)setShared:(BOOL)aShared { |
|
118 |
if (shared != aShared) { |
|
119 |
[self.containers removeAllObjects]; |
|
131 | 120 |
} |
132 |
return self;
|
|
121 |
shared = aShared;
|
|
133 | 122 |
} |
134 | 123 |
|
135 |
- (id)init { |
|
136 |
if ((self = [super init])) { |
|
137 |
uuid = [[NSString alloc] initWithString:[OpenStackAccount stringWithUUID]]; |
|
138 |
|
|
139 |
manager = [[AccountManager alloc] init]; |
|
140 |
manager.account = self; |
|
124 |
- (void)setSharingAccount:(NSString *)aSharingAccount { |
|
125 |
if ((aSharingAccount && ![aSharingAccount isEqualToString:sharingAccount]) || (!aSharingAccount && sharingAccount)) { |
|
126 |
[self.containers removeAllObjects]; |
|
141 | 127 |
} |
142 |
return self; |
|
128 |
[sharingAccount release]; |
|
129 |
sharingAccount = [aSharingAccount retain]; |
|
130 |
} |
|
131 |
|
|
132 |
// the API key and auth token are stored in the Keychain, so overriding the |
|
133 |
// getter and setter to abstract the encryption away and make it easy to use |
|
134 |
|
|
135 |
- (NSString *)apiKeyKeychainKey { |
|
136 |
return [NSString stringWithFormat:@"%@-apiKey", self.uuid]; |
|
137 |
} |
|
138 |
|
|
139 |
- (NSString *)apiKey { |
|
140 |
return [Keychain getStringForKey:[self apiKeyKeychainKey]]; |
|
141 |
} |
|
142 |
|
|
143 |
- (void)setApiKey:(NSString *)newAPIKey { |
|
144 |
[Keychain setString:newAPIKey forKey:[self apiKeyKeychainKey]]; |
|
143 | 145 |
} |
144 | 146 |
|
147 |
- (NSString *)authTokenKeychainKey { |
|
148 |
return [NSString stringWithFormat:@"%@-authToken", self.uuid]; |
|
149 |
} |
|
150 |
|
|
151 |
- (NSString *)authToken { |
|
152 |
NSString *authToken = [Keychain getStringForKey:[self authTokenKeychainKey]]; |
|
153 |
if (!authToken) { |
|
154 |
authToken = @""; |
|
155 |
} |
|
156 |
return authToken; |
|
157 |
} |
|
158 |
|
|
159 |
- (void)setAuthToken:(NSString *)newAuthToken { |
|
160 |
[Keychain setString:newAuthToken forKey:[self authTokenKeychainKey]]; |
|
161 |
} |
|
162 |
|
|
163 |
#pragma mark - Class Actions |
|
164 |
|
|
145 | 165 |
+ (NSArray *)accounts { |
146 | 166 |
if (accounts == nil) { |
147 | 167 |
accounts = [[Archiver retrieve:@"accounts"] retain]; |
... | ... | |
156 | 176 |
accounts = nil; |
157 | 177 |
} |
158 | 178 |
|
179 |
#pragma mark - Actions |
|
180 |
|
|
159 | 181 |
- (void)persist { |
160 | 182 |
if (!flaggedForDelete) { |
161 | 183 |
NSMutableArray *accountArr = [NSMutableArray arrayWithArray:[OpenStackAccount accounts]]; |
... | ... | |
170 | 192 |
break; |
171 | 193 |
} |
172 | 194 |
} |
173 |
|
|
195 |
|
|
174 | 196 |
if (!accountPresent) { |
175 | 197 |
[accountArr insertObject:self atIndex:0]; |
176 | 198 |
} |
177 | 199 |
|
178 |
[Archiver persist:[NSArray arrayWithArray:accountArr] key:@"accounts"];
|
|
200 |
[Archiver persist:[NSArray arrayWithArray:accountArr] key:@"accounts"]; |
|
179 | 201 |
[accounts release]; |
180 | 202 |
accounts = nil; |
181 | 203 |
} |
182 | 204 |
} |
183 | 205 |
|
184 |
// the API key and auth token are stored in the Keychain, so overriding the |
|
185 |
// getter and setter to abstract the encryption away and make it easy to use |
|
186 |
|
|
187 |
- (NSString *)apiKeyKeychainKey { |
|
188 |
return [NSString stringWithFormat:@"%@-apiKey", self.uuid]; |
|
189 |
} |
|
190 |
|
|
191 |
- (NSString *)apiKey { |
|
192 |
return [Keychain getStringForKey:[self apiKeyKeychainKey]]; |
|
193 |
} |
|
194 |
|
|
195 |
- (void)setApiKey:(NSString *)newAPIKey { |
|
196 |
[Keychain setString:newAPIKey forKey:[self apiKeyKeychainKey]]; |
|
197 |
} |
|
198 |
|
|
199 |
- (NSString *)authTokenKeychainKey { |
|
200 |
return [NSString stringWithFormat:@"%@-authToken", self.uuid]; |
|
206 |
- (NSArray *)pithosSortedContainers { |
|
207 |
NSMutableArray *pithosSortedContainers = [NSMutableArray array]; |
|
208 |
if ([self.containers objectForKey:@"pithos"]) |
|
209 |
[pithosSortedContainers addObject:[self.containers objectForKey:@"pithos"]]; |
|
210 |
if ([self.containers objectForKey:@"trash"]) |
|
211 |
[pithosSortedContainers addObject:[self.containers objectForKey:@"trash"]]; |
|
212 |
|
|
213 |
NSMutableDictionary *otherContainers = [NSMutableDictionary dictionaryWithDictionary:self.containers]; |
|
214 |
[otherContainers removeObjectForKey:@"pithos"]; |
|
215 |
[otherContainers removeObjectForKey:@"trash"]; |
|
216 |
[pithosSortedContainers addObjectsFromArray:[[otherContainers allValues] sortedArrayUsingSelector:@selector(compare:)]]; |
|
217 |
return pithosSortedContainers; |
|
201 | 218 |
} |
202 | 219 |
|
203 |
- (NSString *)authToken { |
|
204 |
NSString *authToken = [Keychain getStringForKey:[self authTokenKeychainKey]]; |
|
205 |
if (!authToken) { |
|
206 |
authToken = @""; |
|
220 |
- (NSString *)displaynameForUUID:(NSString *)UUID safe:(BOOL)safe { |
|
221 |
NSString *displayName = [userCatalog objectForKey:UUID]; |
|
222 |
if (safe && !displayName) { |
|
223 |
return UUID; |
|
224 |
} else { |
|
225 |
return displayName; |
|
207 | 226 |
} |
208 |
return authToken; |
|
209 | 227 |
} |
210 | 228 |
|
211 |
- (void)setAuthToken:(NSString *)newAuthToken {
|
|
212 |
[Keychain setString:newAuthToken forKey:[self authTokenKeychainKey]];
|
|
229 |
- (NSString *)displaynameForUUID:(NSString *)UUID {
|
|
230 |
return [self displaynameForUUID:UUID safe:NO];
|
|
213 | 231 |
} |
214 | 232 |
|
215 | 233 |
#pragma mark - Memory Management |
... | ... | |
219 | 237 |
[manager release]; |
220 | 238 |
[provider release]; |
221 | 239 |
[username release]; |
222 |
[hostURL release]; |
|
223 | 240 |
[filesURL release]; |
224 | 241 |
[containers release]; |
225 | 242 |
[sharingAccount release]; |
226 | 243 |
[bytesUsed release]; |
227 | 244 |
[policyQuota release]; |
245 |
[userCatalog release]; |
|
228 | 246 |
|
229 | 247 |
[super dealloc]; |
230 | 248 |
} |
Also available in: Unified diff