]> err.no Git - sope/blob - sope-ical/NGiCal/iCalEvent.m
bb60b10b62e1498ebb05bd0a2fe5bd6518c9908f
[sope] / sope-ical / NGiCal / iCalEvent.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21
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>
28 #include "common.h"
29
30 @interface NSString(DurationTimeInterval)
31 - (NSTimeInterval)durationAsTimeInterval;
32 @end
33
34 @implementation iCalEvent
35
36 - (void)dealloc {
37   [self->endDate      release];
38   [self->duration     release];
39   [self->transparency release];
40   [super dealloc];
41 }
42
43 /* accessors */
44
45 - (void)setEndDate:(NSCalendarDate *)_date {
46   id tmp;
47   if (self->endDate == _date) return;
48   tmp = self->endDate;
49   self->endDate = [_date retain];
50   [tmp release];
51 }
52 - (NSCalendarDate *)endDate {
53   if ([self hasEndDate])
54     return self->endDate;
55   
56   if ([self hasDuration] && (self->startDate != nil)) {
57     return [[self startDate] dateByAddingYears:0 months:0 days:0
58                              hours:0 minutes:0 
59                              seconds:[self durationAsTimeInterval]];
60   }
61   return nil;
62 }
63 - (BOOL)hasEndDate {
64   return self->endDate ? YES : NO;
65 }
66
67 - (void)setDuration:(NSString *)_value {
68   ASSIGNCOPY(self->duration, _value);
69 }
70 - (NSString *)duration {
71   // eg: "DURATION:PT1H"
72   if ([self hasDuration])
73     return self->duration;
74   
75   // TODO: calculate
76   return nil;
77 }
78 - (BOOL)hasDuration {
79   return self->duration ? YES : NO;
80 }
81 - (NSTimeInterval)durationAsTimeInterval {
82   /*
83     eg: DURATION:PT1H
84     P      - "period"
85     P2H30M - "2 hours 30 minutes"
86
87      dur-value  = (["+"] / "-") "P" (dur-date / dur-time / dur-week)
88
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"
95      dur-day    = 1*DIGIT "D"
96   */
97   
98   if (self->duration)
99     return [self->duration durationAsTimeInterval];
100   
101   if (self->endDate != nil && self->startDate != nil)
102     /* calculate duration using enddate */
103     return [[self endDate] timeIntervalSinceDate:[self startDate]];
104   
105   return 0.0;
106 }
107
108 - (void)setTransparency:(NSString *)_transparency {
109   ASSIGNCOPY(self->transparency, _transparency);
110 }
111 - (NSString *)transparency {
112   return self->transparency;
113 }
114
115 /* convenience */
116
117 - (BOOL)isOpaque {
118   NSString *s;
119   
120   s = [self transparency];
121   if (s && [[s uppercaseString] isEqualToString:@"TRANSPARENT"])
122     return NO;
123   return YES; /* default is OPAQUE, see RFC2445, Section 4.8.2.7 */
124 }
125
126 /* TODO: FIX THIS!
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
130    timezone.
131 */
132 - (BOOL)isAllDay {
133   NSCalendarDate *ed;
134
135   if (![self hasEndDate])
136     return NO;
137   
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))
144       return YES;
145   return NO;
146 }
147
148 - (BOOL)isWithinCalendarDateRange:(NGCalendarDateRange *)_range {
149   if (![self isRecurrent]) {
150     if (self->startDate && self->endDate) {
151       NGCalendarDateRange *r;
152       
153       r = [NGCalendarDateRange calendarDateRangeWithStartDate:self->startDate
154                                endDate:self->endDate];
155       return [_range containsDateRange:r];
156     }
157     else {
158       return [_range containsDate:self->startDate];
159     }
160   }
161   else {
162     NGCalendarDateRange *fir;
163
164     fir = [NGCalendarDateRange calendarDateRangeWithStartDate:self->startDate
165                                endDate:self->endDate];
166     
167     return [self isWithinCalendarDateRange:_range
168                  firstInstanceCalendarDateRange:fir];
169   }
170   return NO;
171 }
172
173 /* ical typing */
174
175 - (NSString *)entityName {
176   return @"vevent";
177 }
178
179 /* descriptions */
180
181 - (NSString *)description {
182   NSMutableString *ms;
183
184   ms = [NSMutableString stringWithCapacity:128];
185   [ms appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])];
186
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];
191   
192   if (self->organizer)
193     [ms appendFormat:@" organizer=%@", self->organizer];
194   if (self->attendees)
195     [ms appendFormat:@" attendees=%@", self->attendees];
196   
197   if ([self hasAlarms])
198     [ms appendFormat:@" alarms=%@", self->alarms];
199   
200   [ms appendString:@">"];
201   return ms;
202 }
203
204 /* changes */
205
206 - (iCalEventChanges *)getChangesRelativeToEvent:(iCalEvent *)_event {
207   return [iCalEventChanges changesFromEvent:_event
208                            toEvent:self];
209 }
210
211 /* generating iCal content */
212
213 - (NSString *)vEventString {
214   return [[iCalRenderer sharedICalendarRenderer] vEventStringForEvent:self];
215 }
216
217 @end /* iCalEvent */
218
219 @implementation NSString(DurationTimeInterval)
220
221 - (NSTimeInterval)durationAsTimeInterval {
222   /*
223     eg: DURATION:PT1H
224     P      - "period"
225     P2H30M - "2 hours 30 minutes"
226
227      dur-value  = (["+"] / "-") "P" (dur-date / dur-time / dur-week)
228
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"
236   */
237   unsigned       i, len;
238   NSTimeInterval ti;
239   BOOL           isTime;
240   int            val;
241     
242   if (![self hasPrefix:@"P"]) {
243     NSLog(@"Cannot parse iCal duration value: '%@'", self);
244     return 0.0;
245   }
246     
247   ti  = 0.0;
248   val = 0;
249   for (i = 1, len = [self length], isTime = NO; i < len; i++) {
250     unichar c;
251       
252     c = [self characterAtIndex:i];
253     if (c == 't' || c == 'T') {
254       isTime = YES;
255       val = 0;
256       continue;
257     }
258       
259     if (isdigit(c)) {
260       val = (val * 10) + (c - 48);
261       continue;
262     }
263       
264     switch (c) {
265     case 'W': /* week */
266       ti += (val * 7 * 24 * 60 * 60);
267       break;
268     case 'D': /* day  */
269       ti += (val * 24 * 60 * 60);
270       break;
271     case 'H': /* hour */
272       ti += (val * 60 * 60);
273       break;
274     case 'M': /* min  */
275       ti += (val * 60);
276       break;
277     case 'S': /* sec  */
278       ti += val;
279       break;
280     default:
281       [self logWithFormat:@"cannot process duration unit: '%c'", c];
282       break;
283     }
284     val = 0;
285   }
286   return ti;
287 }
288
289 @end /* NSString(DurationTimeInterval) */