2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include "iCalEvent.h"
23 #include "iCalPerson.h"
24 #include "iCalEventChanges.h"
25 #include "iCalRecurrenceRule.h"
26 #include "iCalRenderer.h"
27 #include <NGExtensions/NGCalendarDateRange.h>
30 @interface NSString(DurationTimeInterval)
31 - (NSTimeInterval)durationAsTimeInterval;
34 @implementation iCalEvent
37 [self->endDate release];
38 [self->duration release];
39 [self->transparency release];
45 - (void)setEndDate:(NSCalendarDate *)_date {
47 if (self->endDate == _date) return;
49 self->endDate = [_date retain];
52 - (NSCalendarDate *)endDate {
53 if ([self hasEndDate])
56 if ([self hasDuration] && (self->startDate != nil)) {
57 return [[self startDate] dateByAddingYears:0 months:0 days:0
59 seconds:[self durationAsTimeInterval]];
64 return self->endDate ? YES : NO;
67 - (void)setDuration:(NSString *)_value {
68 ASSIGNCOPY(self->duration, _value);
70 - (NSString *)duration {
71 // eg: "DURATION:PT1H"
72 if ([self hasDuration])
73 return self->duration;
79 return self->duration ? YES : NO;
81 - (NSTimeInterval)durationAsTimeInterval {
85 P2H30M - "2 hours 30 minutes"
87 dur-value = (["+"] / "-") "P" (dur-date / dur-time / dur-week)
89 dur-date = dur-day [dur-time]
90 dur-time = "T" (dur-hour / dur-minute / dur-second)
91 dur-week = 1*DIGIT "W"
92 dur-hour = 1*DIGIT "H" [dur-minute]
93 dur-minute = 1*DIGIT "M" [dur-second]
94 dur-second = 1*DIGIT "S"
99 return [self->duration durationAsTimeInterval];
101 if (self->endDate != nil && self->startDate != nil)
102 /* calculate duration using enddate */
103 return [[self endDate] timeIntervalSinceDate:[self startDate]];
108 - (void)setTransparency:(NSString *)_transparency {
109 ASSIGNCOPY(self->transparency, _transparency);
111 - (NSString *)transparency {
112 return self->transparency;
120 s = [self transparency];
121 if (s && [[s uppercaseString] isEqualToString:@"TRANSPARENT"])
123 return YES; /* default is OPAQUE, see RFC2445, Section 4.8.2.7 */
127 The problem is, that startDate/endDate are inappropriately modelled here.
128 We'd need to have a special iCalDate in order to fix all the mess.
129 For the time being, we chose allday to mean 00:00 - 23:59 in startDate's
135 if (![self hasEndDate])
138 ed = [[[self endDate] copy] autorelease];
139 [ed setTimeZone:[self->startDate timeZone]];
140 if (([self->startDate hourOfDay] == 0) &&
141 ([self->startDate minuteOfHour] == 0) &&
142 ([ed hourOfDay] == 23) &&
143 ([ed minuteOfHour] == 59))
148 - (BOOL)isWithinCalendarDateRange:(NGCalendarDateRange *)_range {
149 if (![self isRecurrent]) {
150 if (self->startDate && self->endDate) {
151 NGCalendarDateRange *r;
153 r = [NGCalendarDateRange calendarDateRangeWithStartDate:self->startDate
154 endDate:self->endDate];
155 return [_range containsDateRange:r];
158 return [_range containsDate:self->startDate];
162 NGCalendarDateRange *fir;
164 fir = [NGCalendarDateRange calendarDateRangeWithStartDate:self->startDate
165 endDate:self->endDate];
167 return [self isWithinCalendarDateRange:_range
168 firstInstanceCalendarDateRange:fir];
173 - (NSArray *)recurrenceRangesWithinCalendarDateRange:(NGCalendarDateRange *)_r {
174 NGCalendarDateRange *fir;
176 if (![self isRecurrent])
179 fir = [NGCalendarDateRange calendarDateRangeWithStartDate:self->startDate
180 endDate:self->endDate];
181 return [self recurrenceRangesWithinCalendarDateRange:_r
182 firstInstanceCalendarDateRange:fir];
185 - (NSCalendarDate *)lastPossibleRecurrenceStartDate {
186 NGCalendarDateRange *fir;
188 if (![self isRecurrent])
191 fir = [NGCalendarDateRange calendarDateRangeWithStartDate:self->startDate
192 endDate:self->endDate];
193 return [self lastPossibleRecurrenceStartDateUsingFirstInstanceCalendarDateRange:fir];
198 - (NSString *)entityName {
204 - (NSString *)description {
207 ms = [NSMutableString stringWithCapacity:128];
208 [ms appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])];
210 if (self->uid) [ms appendFormat:@" uid=%@", self->uid];
211 if (self->startDate) [ms appendFormat:@" from=%@", self->startDate];
212 if (self->endDate) [ms appendFormat:@" to=%@", self->endDate];
213 if (self->summary) [ms appendFormat:@" summary=%@", self->summary];
216 [ms appendFormat:@" organizer=%@", self->organizer];
218 [ms appendFormat:@" attendees=%@", self->attendees];
220 if ([self hasAlarms])
221 [ms appendFormat:@" alarms=%@", self->alarms];
223 [ms appendString:@">"];
229 - (iCalEventChanges *)getChangesRelativeToEvent:(iCalEvent *)_event {
230 return [iCalEventChanges changesFromEvent:_event
234 /* generating iCal content */
236 - (NSString *)vEventString {
237 return [[iCalRenderer sharedICalendarRenderer] vEventStringForEvent:self];
242 @implementation NSString(DurationTimeInterval)
244 - (NSTimeInterval)durationAsTimeInterval {
248 P2H30M - "2 hours 30 minutes"
250 dur-value = (["+"] / "-") "P" (dur-date / dur-time / dur-week)
252 dur-date = dur-day [dur-time]
253 dur-time = "T" (dur-hour / dur-minute / dur-second)
254 dur-week = 1*DIGIT "W"
255 dur-hour = 1*DIGIT "H" [dur-minute]
256 dur-minute = 1*DIGIT "M" [dur-second]
257 dur-second = 1*DIGIT "S"
258 dur-day = 1*DIGIT "D"
265 if (![self hasPrefix:@"P"]) {
266 NSLog(@"Cannot parse iCal duration value: '%@'", self);
272 for (i = 1, len = [self length], isTime = NO; i < len; i++) {
275 c = [self characterAtIndex:i];
276 if (c == 't' || c == 'T') {
283 val = (val * 10) + (c - 48);
289 ti += (val * 7 * 24 * 60 * 60);
292 ti += (val * 24 * 60 * 60);
295 ti += (val * 60 * 60);
304 [self logWithFormat:@"cannot process duration unit: '%c'", c];
312 @end /* NSString(DurationTimeInterval) */