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];
175 - (NSString *)entityName {
181 - (NSString *)description {
184 ms = [NSMutableString stringWithCapacity:128];
185 [ms appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])];
187 if (self->uid) [ms appendFormat:@" uid=%@", self->uid];
188 if (self->startDate) [ms appendFormat:@" from=%@", self->startDate];
189 if (self->endDate) [ms appendFormat:@" to=%@", self->endDate];
190 if (self->summary) [ms appendFormat:@" summary=%@", self->summary];
193 [ms appendFormat:@" organizer=%@", self->organizer];
195 [ms appendFormat:@" attendees=%@", self->attendees];
197 if ([self hasAlarms])
198 [ms appendFormat:@" alarms=%@", self->alarms];
200 [ms appendString:@">"];
206 - (iCalEventChanges *)getChangesRelativeToEvent:(iCalEvent *)_event {
207 return [iCalEventChanges changesFromEvent:_event
211 /* generating iCal content */
213 - (NSString *)vEventString {
214 return [[iCalRenderer sharedICalendarRenderer] vEventStringForEvent:self];
219 @implementation NSString(DurationTimeInterval)
221 - (NSTimeInterval)durationAsTimeInterval {
225 P2H30M - "2 hours 30 minutes"
227 dur-value = (["+"] / "-") "P" (dur-date / dur-time / dur-week)
229 dur-date = dur-day [dur-time]
230 dur-time = "T" (dur-hour / dur-minute / dur-second)
231 dur-week = 1*DIGIT "W"
232 dur-hour = 1*DIGIT "H" [dur-minute]
233 dur-minute = 1*DIGIT "M" [dur-second]
234 dur-second = 1*DIGIT "S"
235 dur-day = 1*DIGIT "D"
242 if (![self hasPrefix:@"P"]) {
243 NSLog(@"Cannot parse iCal duration value: '%@'", self);
249 for (i = 1, len = [self length], isTime = NO; i < len; i++) {
252 c = [self characterAtIndex:i];
253 if (c == 't' || c == 'T') {
260 val = (val * 10) + (c - 48);
266 ti += (val * 7 * 24 * 60 * 60);
269 ti += (val * 24 * 60 * 60);
272 ti += (val * 60 * 60);
281 [self logWithFormat:@"cannot process duration unit: '%c'", c];
289 @end /* NSString(DurationTimeInterval) */