5 // Created by Mike Mayo on 10/1/10.
6 // The OpenStack project is provided under the Apache 2.0 license.
9 #import "BackupSchedule.h"
10 #import "NSObject+NSCoding.h"
13 @implementation BackupSchedule
15 @synthesize enabled, weekly, daily;
18 #pragma mark Serialization
20 - (void)encodeWithCoder: (NSCoder *)coder {
21 [self autoEncodeWithCoder:coder];
23 [coder encodeBool:enabled forKey:@"enabled"];
24 [coder encodeObject:weekly forKey:@"weekly"];
25 [coder encodeObject:daily forKey:@"daily"];
29 - (id)initWithCoder:(NSCoder *)coder {
30 if (self = [super init]) {
31 [self autoDecode:coder];
33 enabled = [coder decodeBoolForKey:@"enabled"];
34 weekly = [[coder decodeObjectForKey:@"weekly"] retain];
35 daily = [[coder decodeObjectForKey:@"daily"] retain];
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;
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];
61 NSDictionary *result = [NSDictionary dictionaryWithDictionary:dict];
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];
72 NSDictionary *result = [NSDictionary dictionaryWithDictionary:dict];
77 + (NSArray *)weeklyOptions {
78 return [NSArray arrayWithObjects:@"DISABLED", @"MONDAY", @"TUESDAY", @"WEDNESDAY", @"THURSDAY", @"FRIDAY", @"SATURDAY", @"SUNDAY", nil];
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);
86 gmtOffset = [timeRanges count] + gmtOffset;
89 NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:12];
90 for (int i = gmtOffset; i < [timeRanges count]; i++) {
91 [arr addObject:[timeRanges objectAtIndex:i]];
93 for (int i = 0; i < gmtOffset; i++) {
94 [arr addObject:[timeRanges objectAtIndex:i]];
97 [arr insertObject:@"DISABLED" atIndex:0];
99 NSArray *result = [NSArray arrayWithArray:arr];
104 + (NSString *)humanizedWeeklyForString:(NSString *)weeklyString {
106 if ([weeklyString isEqualToString:@"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";
127 + (NSString *)humanizedDailyForString:(NSString *)timeRange {
129 if ([timeRange isEqualToString:@"DISABLED"]) {
132 NSTimeZone *tz = [NSTimeZone systemTimeZone];
133 NSArray *components = [timeRange componentsSeparatedByString:@"_"];
135 NSInteger gmtOffset = ([tz secondsFromGMT] / 3600) * 100;
136 NSInteger fromInt = [[components objectAtIndex:1] intValue] + gmtOffset;
141 NSString *from = @"";
142 if (fromInt >= 1200) {
143 from = [NSString stringWithFormat:@"%i:00 PM", (fromInt - 1200) / 100];
144 } else if (fromInt == 0) {
147 from = [NSString stringWithFormat:@"%i:00 AM", fromInt / 100];
150 if ([from isEqualToString:@"0:00 PM"]) {
152 } else if ([from isEqualToString:@"0:00 AM"]) {
156 NSInteger toInt = [[components objectAtIndex:2] intValue] + gmtOffset;
162 to = [NSString stringWithFormat:@"%i:00 PM", (toInt - 1200) / 100];
163 } else if (toInt == 0) {
166 to = [NSString stringWithFormat:@"%i:00 AM", toInt / 100];
169 if ([to isEqualToString:@"0:00 PM"]) {
171 } else if ([to isEqualToString:@"0:00 AM"]) {
175 return [NSString stringWithFormat:@"%@ - %@ %@", from, to, [tz abbreviation]];
180 #pragma mark Memory Management