// // Container.m // OpenStack // // Created by Mike Mayo on 12/7/10. // The OpenStack project is provided under the Apache 2.0 license. // #import "Container.h" #import "Folder.h" #import "StorageObject.h" #import "NSObject+NSCoding.h" #import "NSObject+Conveniences.h" @implementation Container // regular container attributes @synthesize name, count, bytes; //policy @synthesize versioning, quota; //metadata @synthesize metadata; @synthesize rootFolder, objectsBuffer; #pragma mark - #pragma mark Serialization - (void)encodeWithCoder:(NSCoder *)coder { [coder encodeObject:name forKey:@"name"]; [coder encodeInt:count forKey:@"count"]; [coder encodeInt:bytes forKey:@"bytes"]; [coder encodeObject:rootFolder forKey:@"rootFolder"]; } - (id)initWithCoder:(NSCoder *)coder { self = [super init]; if (self) { name = [[coder decodeObjectForKey:@"name"] retain]; @try { count = [coder decodeIntForKey:@"count"]; bytes = [coder decodeIntForKey:@"bytes"]; } @catch (NSException *exception) { // we want to encode as primitives and the auto encode/decode apparently // wasn't working properly. this try/catch block prevents a potential // crash for users who have been encoded/decoded the other way count = 0; bytes = 0; } rootFolder = [[coder decodeObjectForKey:@"rootFolder"] retain]; } return self; } #pragma mark - #pragma mark JSON + (Container *)fromJSON:(NSDictionary *)dict { Container *container = [[[Container alloc] init] autorelease]; // regular attributes container.name = [dict objectForKey:@"name"]; if ([dict objectForKey:@"count"]) { // If "count" doesn't exist then it is a "shared to me" container, where only "name" exists container.count = [[dict objectForKey:@"count"] unsignedIntegerValue]; container.bytes = [[dict objectForKey:@"bytes"] unsignedLongLongValue]; // policy container.versioning = [[dict objectForKey:@"x_container_policy"] objectForKey:@"versioning"]; // we lose precision in the quota for now, this should be turned to unsigned long long container.quota = strtoul([[[dict objectForKey:@"x_container_policy"] objectForKey:@"quota"] UTF8String], NULL, 0); } // metadata are created elsewhere, so are left nil return container; } #pragma mark - #pragma mark Display - (NSString *)humanizedBytes { return [Container humanizedBytes:self.bytes]; } - (NSString *)osxStyleHumanizedBytes { return [Container osxStyleHumanizedBytes:self.bytes]; } - (NSString *)humanizedCount { NSString *noun = NSLocalizedString(@"objects", @"objects"); if (self.count == 1) { noun = NSLocalizedString(@"object", @"object"); } return [NSString stringWithFormat:@"%i %@", self.count, noun]; } - (NSString *)humanizedSize { return [NSString stringWithFormat:@"%@, %@", [self humanizedCount], [self humanizedBytes]]; } - (NSString *)osxStyleHumanizedSize { return [NSString stringWithFormat:@"%@, %@", [self humanizedCount], [self osxStyleHumanizedBytes]]; } #pragma mark - #pragma mark Comparison // flavors should be sorted by RAM instead of name - (NSComparisonResult)compare:(Container *)aContainer { return [self.name compare:aContainer.name]; } #pragma mark - #pragma mark Memory Management -(void) dealloc { [name release]; [rootFolder release]; [super dealloc]; } @end