Fix bug in container model
[pithos-ios] / Classes / Container.m
1 //
2 //  Container.m
3 //  OpenStack
4 //
5 //  Created by Mike Mayo on 12/7/10.
6 //  The OpenStack project is provided under the Apache 2.0 license.
7 //
8
9 #import "Container.h"
10 #import "Folder.h"
11 #import "StorageObject.h"
12 #import "NSObject+NSCoding.h"
13 #import "NSObject+Conveniences.h"
14
15 @implementation Container
16
17 // regular container attributes
18 @synthesize name, count, bytes;
19
20 //policy
21 @synthesize versioning, quota;
22
23 //metadata
24 @synthesize metadata;
25
26 @synthesize rootFolder, objectsBuffer;
27
28 #pragma mark -
29 #pragma mark Serialization
30
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"];
36 }
37
38 - (id)initWithCoder:(NSCoder *)coder {
39     self = [super init];
40     if (self) {
41         name = [[coder decodeObjectForKey:@"name"] retain];
42         @try {
43             count = [coder decodeIntForKey:@"count"];
44             bytes = [coder decodeIntForKey:@"bytes"];
45         }
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
50             count = 0;
51             bytes = 0;
52         }
53         rootFolder = [[coder decodeObjectForKey:@"rootFolder"] retain];
54     }
55     return self;
56 }
57
58 #pragma mark -
59 #pragma mark JSON
60
61 + (Container *)fromJSON:(NSDictionary *)dict {
62     Container *container = [[[Container alloc] init] autorelease];
63     // regular attributes
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];
69         
70         // policy
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);
74     }
75     // metadata are created elsewhere, so are left nil
76     
77     return container;
78 }
79
80 #pragma mark -
81 #pragma mark Display
82
83 - (NSString *)humanizedBytes {
84     return [Container humanizedBytes:self.bytes];
85 }
86
87 - (NSString *)osxStyleHumanizedBytes {
88     return [Container osxStyleHumanizedBytes:self.bytes];
89 }
90
91 - (NSString *)humanizedCount {
92         NSString *noun = NSLocalizedString(@"objects", @"objects");
93         if (self.count == 1) {
94                 noun = NSLocalizedString(@"object", @"object");
95         }
96         return [NSString stringWithFormat:@"%i %@", self.count, noun];
97 }
98
99 - (NSString *)humanizedSize {
100         return [NSString stringWithFormat:@"%@, %@", [self humanizedCount], [self humanizedBytes]];
101 }
102
103 - (NSString *)osxStyleHumanizedSize {
104     return [NSString stringWithFormat:@"%@, %@", [self humanizedCount], [self osxStyleHumanizedBytes]];
105 }
106
107 #pragma mark -
108 #pragma mark Comparison
109
110 // flavors should be sorted by RAM instead of name
111 - (NSComparisonResult)compare:(Container *)aContainer {
112     return [self.name compare:aContainer.name];
113 }
114
115 #pragma mark -
116 #pragma mark Memory Management
117
118 -(void) dealloc {
119         [name release];
120     [rootFolder release];
121         [super dealloc];
122 }
123
124 @end