Expanded open file functionality to use available apps.
[pithos-ios] / Classes / ComputeModel.m
1 //
2 //  ComputeModel.m
3 //  OpenStack
4 //
5 //  Created by Mike Mayo on 10/4/10.
6 //  The OpenStack project is provided under the Apache 2.0 license.
7 //
8
9 #import "ComputeModel.h"
10 #import "NSObject+NSCoding.h"
11
12
13 @implementation ComputeModel
14
15 @synthesize identifier, name;
16
17 #pragma mark -
18 #pragma mark Serialization
19
20 - (void)encodeWithCoder: (NSCoder *)coder {
21     [self autoEncodeWithCoder:coder];
22 }
23
24 - (id)initWithCoder:(NSCoder *)coder {
25     self = [super init];
26     if (self) {
27         [self autoDecode:coder];
28     }
29     return self;
30 }
31
32 #pragma mark -
33 #pragma mark JSON Parsing
34
35 - (id)initWithJSONDict:(NSDictionary *)dict {
36     if (self = [super init]) {
37         self.identifier = [[dict objectForKey:@"id"] description];
38         self.name = [dict objectForKey:@"name"];
39     }
40     return self;
41 }
42
43 - (NSInteger)intForKey:(NSString *)key inDict:(NSDictionary *)dict {
44     NSInteger result = 0;
45     if ([dict objectForKey:key] != [NSNull null]) {
46         result = [((NSNumber *)[dict objectForKey:key]) intValue];
47     }
48     return result;
49 }
50
51 - (NSDate *)dateForKey:(NSString *)key inDict:(NSDictionary *)dict {
52     NSDate *date = nil;
53     if ([dict objectForKey:key] != [NSNull null]) {
54         date = [self dateFromString:[dict objectForKey:key]];
55     }
56     return date;
57 }
58
59 #pragma mark -
60 #pragma mark Date Parser
61
62 + (NSDate *)dateFromString:(NSString *)dateString {
63     return nil; // temporarily removing date parsing for performance
64     
65     /*
66         NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
67         [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
68         // example: 2009-11-04T19:46:20.192723
69     // 2010-01-26T12:07:32-06:00
70
71     // this is nasty, but -06:00 is not a valid timezone format.  converting to -0600 style
72     dateString = [NSString stringWithFormat:@"%@%@", [dateString substringToIndex:22], [dateString substringFromIndex:23]];
73     [dateFormatter setDateFormat:@"yyyy-MM-dd'T'H:mm:ssZ"];
74
75         NSDate *date = [dateFormatter dateFromString:dateString];
76         [dateFormatter release];
77         
78         return date;
79      */
80 }
81
82 - (NSDate *)dateFromString:(NSString *)dateString {
83         return [[self class] dateFromString:dateString];
84 }
85
86 #pragma mark Comparison
87
88 - (NSComparisonResult)compare:(ComputeModel *)aComputeModel {
89     return [self.name caseInsensitiveCompare:aComputeModel.name];
90 }
91
92 - (NSString *)description {
93     if ([[self class] respondsToSelector:@selector(toJSON:)]) {
94         return [[self class] performSelector:@selector(toJSON:) withObject:self];
95     } else {
96         return [super description];
97     }
98 }
99
100 #pragma mark -
101 #pragma mark Memory Management
102
103 - (void)dealloc {
104     [identifier release];
105     [name release];
106     [super dealloc];
107 }
108
109 @end