5 // Created by Mike Mayo on 12/7/10.
6 // The OpenStack project is provided under the Apache 2.0 license.
11 #import "StorageObject.h"
12 #import "NSObject+NSCoding.h"
13 #import "NSObject+Conveniences.h"
15 @implementation Container
17 // regular container attributes
18 @synthesize name, count, bytes;
21 @synthesize versioning, quota;
26 @synthesize rootFolder, objectsBuffer;
29 #pragma mark Serialization
31 - (void)encodeWithCoder:(NSCoder *)coder {
32 [coder encodeObject:name forKey:@"name"];
33 [coder encodeInt:count forKey:@"count"];
34 [coder encodeInt:bytes forKey:@"bytes"];
35 [coder encodeObject:rootFolder forKey:@"rootFolder"];
38 - (id)initWithCoder:(NSCoder *)coder {
41 name = [[coder decodeObjectForKey:@"name"] retain];
43 count = [coder decodeIntForKey:@"count"];
44 bytes = [coder decodeIntForKey:@"bytes"];
46 @catch (NSException *exception) {
47 // we want to encode as primitives and the auto encode/decode apparently
48 // wasn't working properly. this try/catch block prevents a potential
49 // crash for users who have been encoded/decoded the other way
53 rootFolder = [[coder decodeObjectForKey:@"rootFolder"] retain];
61 + (Container *)fromJSON:(NSDictionary *)dict {
62 Container *container = [[[Container alloc] init] autorelease];
64 container.name = [dict objectForKey:@"name"];
65 if ([dict objectForKey:@"count"]) {
66 // If "count" doesn't exist then it is a "shared to me" container, where only "name" exists
67 container.count = [[dict objectForKey:@"count"] unsignedIntegerValue];
68 container.bytes = [[dict objectForKey:@"bytes"] unsignedLongLongValue];
71 container.versioning = [[dict objectForKey:@"x_container_policy"] objectForKey:@"versioning"];
72 // we lose precision in the quota for now, this should be turned to unsigned long long
73 container.quota = strtoul([[[dict objectForKey:@"x_container_policy"] objectForKey:@"quota"] UTF8String], NULL, 0);
75 // metadata are created elsewhere, so are left nil
83 - (NSString *)humanizedBytes {
84 return [Container humanizedBytes:self.bytes];
87 - (NSString *)osxStyleHumanizedBytes {
88 return [Container osxStyleHumanizedBytes:self.bytes];
91 - (NSString *)humanizedCount {
92 NSString *noun = NSLocalizedString(@"objects", @"objects");
93 if (self.count == 1) {
94 noun = NSLocalizedString(@"object", @"object");
96 return [NSString stringWithFormat:@"%i %@", self.count, noun];
99 - (NSString *)humanizedSize {
100 return [NSString stringWithFormat:@"%@, %@", [self humanizedCount], [self humanizedBytes]];
103 - (NSString *)osxStyleHumanizedSize {
104 return [NSString stringWithFormat:@"%@, %@", [self humanizedCount], [self osxStyleHumanizedBytes]];
108 #pragma mark Comparison
110 // flavors should be sorted by RAM instead of name
111 - (NSComparisonResult)compare:(Container *)aContainer {
112 return [self.name compare:aContainer.name];
116 #pragma mark Memory Management
120 [rootFolder release];