Expanded open file functionality to use available apps.
[pithos-ios] / Classes / StorageObject.m
1 //
2 //  Object.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 "StorageObject.h"
10 #import "ComputeModel.h"
11 #import "NSObject+NSCoding.h"
12
13
14 @implementation StorageObject
15
16 @synthesize name, fullPath, hash, bytes, contentType, lastModified, data, publicURI, sharing, metadata;
17
18 #pragma mark -
19 #pragma mark Serialization
20
21 - (void)encodeWithCoder:(NSCoder *)coder {
22     [self autoEncodeWithCoder:coder];
23     /*
24     [coder encodeObject:name forKey:@"name"];
25     [coder encodeObject:fullPath forKey:@"fullPath"];
26     [coder encodeObject:hash forKey:@"hash"];
27     [coder encodeInt:bytes forKey:@"bytes"];
28     [coder encodeObject:contentType forKey:@"contentType"];
29     [coder encodeObject:lastModified forKey:@"lastModified"];
30     // not persisting data to save space
31     [coder encodeObject:metadata forKey:@"metadata"];
32      */
33 }
34
35 - (id)initWithCoder:(NSCoder *)coder {
36     self = [super init];
37     if (self) {
38         [self autoDecode:coder];
39         /*
40         name = [[coder decodeObjectForKey:@"name"] retain];
41         fullPath = [[coder decodeObjectForKey:@"fullPath"] retain];
42         hash = [[coder decodeObjectForKey:@"hash"] retain];
43         bytes = [coder decodeIntForKey:@"bytes"];
44         contentType = [[coder decodeObjectForKey:@"contentType"] retain];
45         lastModified = [[coder decodeObjectForKey:@"lastModified"] retain];
46         data = [[coder decodeObjectForKey:@"data"] retain];
47         metadata = [[coder decodeObjectForKey:@"metadata"] retain];
48          */
49     }
50     return self;
51 }
52
53 #pragma mark -
54 #pragma mark JSON
55
56 + (StorageObject *)fromJSON:(NSDictionary *)dict {
57     
58     StorageObject *object = [[[StorageObject alloc] init] autorelease];
59     
60     object.name = [dict objectForKey:@"name"]; // this may be shortened by folder parsing later
61     object.fullPath = [dict objectForKey:@"name"];
62     object.hash = [dict objectForKey:@"hash"];
63     object.bytes = [[dict objectForKey:@"bytes"] intValue];
64     object.contentType = [dict objectForKey:@"content_type"];
65     object.lastModified = [ComputeModel dateFromString:[dict objectForKey:@"last_modified"]];
66     object.publicURI = [dict objectForKey:@"x_object_public"];
67     object.sharing = [dict objectForKey:@"x_object_sharing"];
68         
69     object.metadata = [NSMutableDictionary dictionary];
70     for (NSString *key in dict) {
71         if ([key hasPrefix:@"x_object_meta_"]) {
72             NSString *metadataKey = [key substringFromIndex:14];
73             [object.metadata setObject:[dict objectForKey:key] forKey:metadataKey];
74         }
75     }
76         
77     return object;
78 }
79
80 #pragma mark -
81 #pragma mark Presentation
82
83 -(NSString *)humanizedBytes {
84         NSString *result;       
85         if (self.bytes >= 1024000000) {
86                 result = [NSString stringWithFormat:@"%.2f GiB", self.bytes / 1024000000.0];
87         } else if (self.bytes >= 1024000) {
88                 result = [NSString stringWithFormat:@"%.2f MiB", self.bytes / 1024000.0];
89         } else if (self.bytes >= 1024) {
90                 result = [NSString stringWithFormat:@"%.2f KiB", self.bytes / 1024.0];
91         } else {
92                 result = [NSString stringWithFormat:@"%i bytes", self.bytes];
93         }
94         return result;
95 }
96
97 - (BOOL)isPlayableMedia {
98     
99     // check file extension
100     NSString *extensionPattern = @"^.+\\.((mov)|(m4a)|(mp3)|(wav)|(aiff)|(aac)|(aif)|(aifc)|(amr)|(caf)|(m2a)|(m4p)|(mp4)|(mpv)|(3gp))$";
101     NSRegularExpression *extensionRegex = [NSRegularExpression regularExpressionWithPattern:extensionPattern options:NSRegularExpressionCaseInsensitive error:nil];
102     NSInteger matches = [extensionRegex numberOfMatchesInString:self.name options:0 range:NSMakeRange(0, [self.name length])];
103
104     // check content type
105     NSString *contentTypePattern = @"^(video|audio)/";
106     NSRegularExpression *contentTypeRegex = [NSRegularExpression regularExpressionWithPattern:contentTypePattern options:NSRegularExpressionCaseInsensitive error:nil];
107     matches += [contentTypeRegex numberOfMatchesInString:self.contentType options:0 range:NSMakeRange(0, [self.contentType length])];
108     
109     return matches > 0;
110 }
111
112 - (NSComparisonResult)compare:(StorageObject *)anObject {
113     return [self.name caseInsensitiveCompare:anObject.name];
114 }
115
116 #pragma mark -
117 #pragma mark Memory Management
118
119 -(void)dealloc {
120         [name release];
121     [fullPath release];
122         [hash release];
123         [contentType release];
124         [lastModified release];
125         [data release];
126         [metadata release];
127         [super dealloc];
128 }
129
130 @end