Expanded open file functionality to use available apps.
[pithos-ios] / Classes / BackupSchedule.m
1 //
2 //  BackupSchedule.m
3 //  OpenStack
4 //
5 //  Created by Mike Mayo on 10/1/10.
6 //  The OpenStack project is provided under the Apache 2.0 license.
7 //
8
9 #import "BackupSchedule.h"
10 #import "NSObject+NSCoding.h"
11
12
13 @implementation BackupSchedule
14
15 @synthesize enabled, weekly, daily;
16
17 #pragma mark -
18 #pragma mark Serialization
19
20 - (void)encodeWithCoder: (NSCoder *)coder {
21     [self autoEncodeWithCoder:coder];
22     /*
23     [coder encodeBool:enabled forKey:@"enabled"];
24     [coder encodeObject:weekly forKey:@"weekly"];
25     [coder encodeObject:daily forKey:@"daily"];
26      */
27 }
28
29 - (id)initWithCoder:(NSCoder *)coder {
30     if (self = [super init]) {
31         [self autoDecode:coder];
32         /*
33         enabled = [coder decodeBoolForKey:@"enabled"];
34         weekly = [[coder decodeObjectForKey:@"weekly"] retain];
35         daily = [[coder decodeObjectForKey:@"daily"] retain];
36          */
37     }
38     return self;
39 }
40
41 #pragma mark -
42 #pragma mark JSON
43
44 + (BackupSchedule *)fromJSON:(NSDictionary *)dict {
45     BackupSchedule *backupSchedule = [[[BackupSchedule alloc] init] autorelease];
46     backupSchedule.enabled = [[dict objectForKey:@"enabled"] boolValue];
47     backupSchedule.weekly = [dict objectForKey:@"weekly"];
48     backupSchedule.daily = [dict objectForKey:@"daily"];
49     return backupSchedule;
50 }
51
52 #pragma mark -
53 #pragma mark Display
54
55 + (NSDictionary *)weeklyOptionsDict {
56     NSArray *weeklyOptions = [BackupSchedule weeklyOptions];
57     NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:8];
58     for (NSString *option in weeklyOptions) {
59         [dict setObject:[BackupSchedule humanizedWeeklyForString:option] forKey:option];
60     }
61     NSDictionary *result = [NSDictionary dictionaryWithDictionary:dict];
62     [dict release];     
63     return result;
64 }
65
66 + (NSDictionary *)dailyOptionsDict {
67     NSArray *dailyOptions = [BackupSchedule dailyOptions];
68     NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:13];
69     for (NSString *option in dailyOptions) {
70         [dict setObject:[BackupSchedule humanizedDailyForString:option] forKey:option];
71     }
72     NSDictionary *result = [NSDictionary dictionaryWithDictionary:dict];
73     [dict release];     
74     return result;
75 }
76
77 + (NSArray *)weeklyOptions {
78     return [NSArray arrayWithObjects:@"DISABLED", @"MONDAY", @"TUESDAY", @"WEDNESDAY", @"THURSDAY", @"FRIDAY", @"SATURDAY", @"SUNDAY", nil];
79 }
80
81 + (NSArray *)dailyOptions {
82     NSArray *timeRanges = [NSArray arrayWithObjects:@"H_0000_0200", @"H_0200_0400", @"H_0400_0600", @"H_0600_0800", @"H_0800_1000", @"H_1000_1200", @"H_1200_1400", @"H_1400_1600", @"H_1600_1800", @"H_1800_2000", @"H_2000_2200", @"H_2200_0000", nil];
83     NSTimeZone *tz = [NSTimeZone systemTimeZone];
84     NSInteger gmtOffset = (([tz secondsFromGMT] / 3600) / 2);    
85     if (gmtOffset < 0) {
86         gmtOffset = [timeRanges count] + gmtOffset;
87     }
88     
89     NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:12];
90     for (int i = gmtOffset; i < [timeRanges count]; i++) {
91         [arr addObject:[timeRanges objectAtIndex:i]];
92     }
93     for (int i = 0; i < gmtOffset; i++) {
94         [arr addObject:[timeRanges objectAtIndex:i]];
95     }
96          
97     [arr insertObject:@"DISABLED" atIndex:0];
98     
99     NSArray *result = [NSArray arrayWithArray:arr];
100     [arr release];
101     return result;
102 }
103
104 + (NSString *)humanizedWeeklyForString:(NSString *)weeklyString {
105     
106     if ([weeklyString isEqualToString:@"DISABLED"]) {
107         return @"Disabled";
108     } else if ([weeklyString isEqualToString:@"MONDAY"]) {
109         return @"Every Monday";
110     } else if ([weeklyString isEqualToString:@"TUESDAY"]) {
111         return @"Every Tuesday";
112     } else if ([weeklyString isEqualToString:@"WEDNESDAY"]) {
113         return @"Every Wednesday";
114     } else if ([weeklyString isEqualToString:@"THURSDAY"]) {
115         return @"Every Thursday";
116     } else if ([weeklyString isEqualToString:@"FRIDAY"]) {
117         return @"Every Friday";
118     } else if ([weeklyString isEqualToString:@"SATURDAY"]) {
119         return @"Every Saturday";
120     } else if ([weeklyString isEqualToString:@"SUNDAY"]) {
121         return @"Every Sunday";
122     } else {
123         return weeklyString;
124     } 
125 }
126
127 + (NSString *)humanizedDailyForString:(NSString *)timeRange {
128
129     if ([timeRange isEqualToString:@"DISABLED"]) {
130         return @"Disabled";
131     } else {      
132         NSTimeZone *tz = [NSTimeZone systemTimeZone];
133         NSArray *components = [timeRange componentsSeparatedByString:@"_"];
134
135         NSInteger gmtOffset = ([tz secondsFromGMT] / 3600) * 100;
136         NSInteger fromInt = [[components objectAtIndex:1] intValue] + gmtOffset;
137         if (fromInt < 0) {
138             fromInt += 2400;
139         }
140         
141         NSString *from = @"";
142         if (fromInt >= 1200) {
143             from = [NSString stringWithFormat:@"%i:00 PM", (fromInt - 1200) / 100];
144         } else if (fromInt == 0) {
145             from = @"12:00 AM";
146         } else {
147             from = [NSString stringWithFormat:@"%i:00 AM", fromInt / 100];
148         }
149         
150         if ([from isEqualToString:@"0:00 PM"]) {
151             from = @"12:00 PM";
152         } else if ([from isEqualToString:@"0:00 AM"]) {
153             from = @"12:00 AM";
154         }
155         
156         NSInteger toInt = [[components objectAtIndex:2] intValue] + gmtOffset;
157         if (toInt < 0) {
158             toInt += 2400;
159         }
160         NSString *to = @"";
161         if (toInt >= 1200) {
162             to = [NSString stringWithFormat:@"%i:00 PM", (toInt - 1200) / 100];
163         } else if (toInt == 0) {
164             to = @"12:00 AM";
165         } else {
166             to = [NSString stringWithFormat:@"%i:00 AM", toInt / 100];
167         }
168
169         if ([to isEqualToString:@"0:00 PM"]) {
170             to = @"12:00 PM";
171         } else if ([to isEqualToString:@"0:00 AM"]) {
172             to = @"12:00 AM";
173         }
174         
175         return [NSString stringWithFormat:@"%@ - %@ %@", from, to, [tz abbreviation]];
176     }
177 }
178
179 #pragma mark -
180 #pragma mark Memory Management
181
182 - (void)dealloc {
183     [weekly release];
184     [daily release];
185     [super dealloc];
186 }
187
188 @end