]> err.no Git - sope/blob - sope-ical/NGiCal/iCalEvent.m
new Xcode projects
[sope] / sope-ical / NGiCal / iCalEvent.m
1 /*
2   Copyright (C) 2000-2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
6   OGo 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   OGo 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 OGo; 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 // $Id$
22
23 #include "iCalEvent.h"
24 #include "iCalPerson.h"
25 #include "iCalEventChanges.h"
26 #include "common.h"
27
28 @interface NSString(DurationTimeInterval)
29 - (NSTimeInterval)durationAsTimeInterval;
30 @end
31
32 @implementation iCalEvent
33
34 - (void)dealloc {
35   [self->duration release];
36   [self->endDate  release];
37   [super dealloc];
38 }
39
40 /* accessors */
41
42 - (void)setEndDate:(NSCalendarDate *)_date {
43   id tmp;
44   if (self->endDate == _date) return;
45   tmp = self->endDate;
46   self->endDate = [_date retain];
47   [tmp release];
48 }
49 - (NSCalendarDate *)endDate {
50   if ([self hasEndDate])
51     return self->endDate;
52   
53   if ([self hasDuration] && (self->startDate != nil)) {
54     return [[self startDate] dateByAddingYears:0 months:0 days:0
55                              hours:0 minutes:0 
56                              seconds:[self durationAsTimeInterval]];
57   }
58   return nil;
59 }
60 - (BOOL)hasEndDate {
61   return self->endDate ? YES : NO;
62 }
63
64 - (void)setTransp:(NSString *)_transp {
65   /* ignore transp ... (used by Evo 'TRANSP:OPAQUE') */
66 }
67
68 - (void)setDuration:(NSString *)_value {
69   ASSIGNCOPY(self->duration, _value);
70 }
71 - (NSString *)duration {
72   // eg: "DURATION:PT1H"
73   if ([self hasDuration])
74     return self->duration;
75   
76   // TODO: calculate
77   return nil;
78 }
79 - (BOOL)hasDuration {
80   return self->duration ? YES : NO;
81 }
82 - (NSTimeInterval)durationAsTimeInterval {
83   /*
84     eg: DURATION:PT1H
85     P      - "period"
86     P2H30M - "2 hours 30 minutes"
87
88      dur-value  = (["+"] / "-") "P" (dur-date / dur-time / dur-week)
89
90      dur-date   = dur-day [dur-time]
91      dur-time   = "T" (dur-hour / dur-minute / dur-second)
92      dur-week   = 1*DIGIT "W"
93      dur-hour   = 1*DIGIT "H" [dur-minute]
94      dur-minute = 1*DIGIT "M" [dur-second]
95      dur-second = 1*DIGIT "S"
96      dur-day    = 1*DIGIT "D"
97   */
98   
99   if (self->duration)
100     return [self->duration durationAsTimeInterval];
101   
102   if (self->endDate != nil && self->startDate != nil)
103     /* calculate duration using enddate */
104     return [[self endDate] timeIntervalSinceDate:[self startDate]];
105   
106   return 0.0;
107 }
108
109 /* ical typing */
110
111 - (NSString *)entityName {
112   return @"vevent";
113 }
114
115 /* descriptions */
116
117 - (NSString *)description {
118   NSMutableString *ms;
119
120   ms = [NSMutableString stringWithCapacity:128];
121   [ms appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])];
122
123   if (self->uid)       [ms appendFormat:@" uid=%@", self->uid];
124   if (self->startDate) [ms appendFormat:@" from=%@", self->startDate];
125   if (self->endDate)   [ms appendFormat:@" to=%@", self->endDate];
126   if (self->summary)   [ms appendFormat:@" summary=%@", self->summary];
127   
128   if (self->organizer)
129     [ms appendFormat:@" organizer=%@", self->organizer];
130   if (self->attendees)
131     [ms appendFormat:@" attendees=%@", self->attendees];
132   
133   if ([self hasAlarms])
134     [ms appendFormat:@" alarms=%@", self->alarms];
135   
136   [ms appendString:@">"];
137   return ms;
138 }
139
140 /* changes */
141
142 - (iCalEventChanges *)getChangesRelativeToEvent:(iCalEvent *)_event {
143   return [iCalEventChanges changesFromEvent:_event
144                            toEvent:self];
145 }
146
147 @end /* iCalEvent */
148
149 @implementation NSString(DurationTimeInterval)
150
151 - (NSTimeInterval)durationAsTimeInterval {
152   /*
153     eg: DURATION:PT1H
154     P      - "period"
155     P2H30M - "2 hours 30 minutes"
156
157      dur-value  = (["+"] / "-") "P" (dur-date / dur-time / dur-week)
158
159      dur-date   = dur-day [dur-time]
160      dur-time   = "T" (dur-hour / dur-minute / dur-second)
161      dur-week   = 1*DIGIT "W"
162      dur-hour   = 1*DIGIT "H" [dur-minute]
163      dur-minute = 1*DIGIT "M" [dur-second]
164      dur-second = 1*DIGIT "S"
165      dur-day    = 1*DIGIT "D"
166   */
167   unsigned       i, len;
168   NSTimeInterval ti;
169   BOOL           isTime;
170   int            val;
171     
172   if (![self hasPrefix:@"P"]) {
173     NSLog(@"Cannot parse iCal duration value: '%@'", self);
174     return 0.0;
175   }
176     
177   ti  = 0.0;
178   val = 0;
179   for (i = 1, len = [self length], isTime = NO; i < len; i++) {
180     unichar c;
181       
182     c = [self characterAtIndex:i];
183     if (c == 't' || c == 'T') {
184       isTime = YES;
185       val = 0;
186       continue;
187     }
188       
189     if (isdigit(c)) {
190       val = (val * 10) + (c - 48);
191       continue;
192     }
193       
194     switch (c) {
195     case 'W': /* week */
196       ti += (val * 7 * 24 * 60 * 60);
197       break;
198     case 'D': /* day  */
199       ti += (val * 24 * 60 * 60);
200       break;
201     case 'H': /* hour */
202       ti += (val * 60 * 60);
203       break;
204     case 'M': /* min  */
205       ti += (val * 60);
206       break;
207     case 'S': /* sec  */
208       ti += val;
209       break;
210     default:
211       [self logWithFormat:@"cannot process duration unit: '%c'", c];
212       break;
213     }
214     val = 0;
215   }
216   return ti;
217 }
218
219 @end /* NSString(DurationTimeInterval) */