From a22195beec138797d07747cbe2ad51b72c67bb33 Mon Sep 17 00:00:00 2001 From: Francis Lachapelle Date: Wed, 21 Jan 2015 14:18:38 -0500 Subject: [PATCH] Add method [iCalTrigger asDictionary] This method is used to get a JSON representation of an alarm trigger. --- SOPE/NGCards/iCalTrigger.h | 2 ++ SOPE/NGCards/iCalTrigger.m | 70 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/SOPE/NGCards/iCalTrigger.h b/SOPE/NGCards/iCalTrigger.h index 8f2a7c1b7..051fc7c7b 100644 --- a/SOPE/NGCards/iCalTrigger.h +++ b/SOPE/NGCards/iCalTrigger.h @@ -34,6 +34,8 @@ - (NSCalendarDate *) nextAlarmDate; +- (NSDictionary *) asDictionary; + @end #endif /* __NGCards_iCalTrigger_H__ */ diff --git a/SOPE/NGCards/iCalTrigger.m b/SOPE/NGCards/iCalTrigger.m index 715bd1681..1f0bbf995 100644 --- a/SOPE/NGCards/iCalTrigger.m +++ b/SOPE/NGCards/iCalTrigger.m @@ -20,6 +20,7 @@ */ #import +#import #import #import "iCalEvent.h" @@ -52,6 +53,75 @@ return [self value: 0 ofAttribute: @"related"]; } +- (NSDictionary *) asDictionary +{ + NSDictionary *data; + NSString *duration, *relation, *reference, *quantity, *unit; + NSUInteger i; + unichar c; + + data = nil; + if (![[self valueType] length] || + [[self valueType] caseInsensitiveCompare: @"DURATION"] == NSOrderedSame) + { + relation = [[self relationType] uppercaseString]; + duration = [self flattenedValuesForKey: @""]; + i = 0; + c = [duration characterAtIndex: i]; + if (c == '-') + { + reference = @"BEFORE"; + i++; + } + else + { + reference = @"AFTER"; + } + c = [duration characterAtIndex: i]; + if (c == 'P') + { + quantity = @""; + unit = @""; + // Parse duration -- ignore first character (P) + for (i++; i < [duration length]; i++) + { + c = [duration characterAtIndex: i]; + if (c == 't' || c == 'T') + // time -- ignore character + continue; + else if (isdigit (c)) + quantity = [quantity stringByAppendingFormat: @"%c", c]; + else + { + switch (c) + { + case 'D': /* day */ + unit = @"DAYS"; + break; + case 'H': /* hour */ + unit = @"HOURS"; + break; + case 'M': /* min */ + unit = @"MINUTES"; + break; + default: + NSLog(@"Cannot process duration unit: '%c'", c); + break; + } + } + } + } + data = [NSDictionary dictionaryWithObjectsAndKeys: + relation, @"relation", + reference, @"reference", + quantity, @"quantity", + unit, @"unit", + nil]; + } + + return data; +} + - (NSCalendarDate *) nextAlarmDate { NSCalendarDate *relationDate, *nextAlarmDate;