]> err.no Git - scalable-opengroupware.org/blob - SOPE/NGCards/iCalEvent.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1178 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / SOPE / NGCards / 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 #import <NGExtensions/NSCalendarDate+misc.h>
23 #import <NGExtensions/NGCalendarDateRange.h>
24
25 #import "NSCalendarDate+NGCards.h"
26 #import "NSString+NGCards.h"
27
28 #import "iCalEventChanges.h"
29 #import "iCalDateTime.h"
30
31 #import "iCalEvent.h"
32
33 @implementation iCalEvent
34
35 - (Class) classForTag: (NSString *) classTag
36 {
37   Class tagClass;
38
39   if ([classTag isEqualToString: @"DURATION"]
40       || [classTag isEqualToString: @"TRANSP"])
41     tagClass = [CardElement class];
42   else if ([classTag isEqualToString: @"DTEND"])
43     tagClass = [iCalDateTime class];
44   else
45     tagClass = [super classForTag: classTag];
46
47   return tagClass;
48 }
49
50 /* accessors */
51 - (void) setAllDayWithStartDate: (NSCalendarDate *) newStartDate
52                        duration: (unsigned int) days
53 {
54   NSCalendarDate *endDate;
55
56   [(iCalDateTime *) [self uniqueChildWithTag: @"dtstart"]
57                     setDate: newStartDate];
58   endDate = [newStartDate dateByAddingYears: 0 months: 0 days: days];
59   [(iCalDateTime *) [self uniqueChildWithTag: @"dtend"]
60                     setDate: endDate];
61 }
62
63 - (void) setEndDate: (NSCalendarDate *) newEndDate
64 {
65   [(iCalDateTime *) [self uniqueChildWithTag: @"dtend"]
66                     setDateTime: newEndDate];
67 }
68
69 - (NSCalendarDate *) endDate
70 {
71   return [(iCalDateTime *) [self uniqueChildWithTag: @"dtend"]
72                            dateTime];
73 }
74
75 - (BOOL) hasEndDate
76 {
77   return ([[self childrenWithTag: @"dtend"] count] > 0);
78 }
79
80 - (void) setDuration: (NSString *) _value
81 {
82   [[self uniqueChildWithTag: @"duration"] setValue: 0
83                                           to: _value];
84 }
85
86 - (NSString *) duration
87 {
88   return [[self uniqueChildWithTag: @"duration"] value: 0];
89 }
90
91 - (BOOL) hasDuration
92 {
93   return ([[self childrenWithTag: @"duration"] count] > 0);
94 }
95
96 - (NSTimeInterval) durationAsTimeInterval
97 {
98   /*
99     eg: DURATION:PT1H
100     P      - "period"
101     P2H30M - "2 hours 30 minutes"
102
103      dur-value  = (["+"] / "-") "P" (dur-date / dur-time / dur-week)
104
105      dur-date   = dur-day [dur-time]
106      dur-time   = "T" (dur-hour / dur-minute / dur-second)
107      dur-week   = 1*DIGIT "W"
108      dur-hour   = 1*DIGIT "H" [dur-minute]
109      dur-minute = 1*DIGIT "M" [dur-second]
110      dur-second = 1*DIGIT "S"
111      dur-day    = 1*DIGIT "D"
112   */
113   NSTimeInterval interval;
114
115   if ([self hasDuration])
116     interval = [[self duration] durationAsTimeInterval];
117   else if ([self hasEndDate] && [self hasStartDate])
118     /* calculate duration using enddate */
119     interval = [[self endDate] timeIntervalSinceDate: [self startDate]];
120   else
121     interval = 0.0;
122
123   return interval;
124 }
125
126 - (void) setTransparency: (NSString *) _transparency
127 {
128   [[self uniqueChildWithTag: @"transp"] setValue: 0
129                                         to: _transparency];
130 }
131
132 - (NSString *) transparency
133 {
134   return [[self uniqueChildWithTag: @"transp"] value: 0];
135 }
136
137 /* convenience */
138
139 - (BOOL) isOpaque
140 {
141   NSString *s;
142   
143   s = [[self transparency] uppercaseString];
144
145   return (![s isEqualToString: @"TRANSPARENT"]);
146 }
147
148 - (BOOL) isAllDay
149 {
150   return [(iCalDateTime *) [self uniqueChildWithTag: @"dtstart"] isAllDay];
151 }
152
153 - (BOOL) isWithinCalendarDateRange: (NGCalendarDateRange *) _range
154 {
155   NGCalendarDateRange *r;
156   NSCalendarDate *startDate, *endDate;
157   NGCalendarDateRange *fir;
158
159   startDate = [self startDate];
160   endDate = [self endDate];
161
162   if (![self isRecurrent])
163     {
164       if ([self hasStartDate] && [self hasEndDate])
165         {
166           r = [NGCalendarDateRange calendarDateRangeWithStartDate: startDate
167                                    endDate: endDate];
168           return [_range containsDateRange: r];
169         }
170       else
171         return [_range containsDate: startDate];
172     }
173   else
174     {
175       fir = [NGCalendarDateRange calendarDateRangeWithStartDate:startDate
176                                  endDate: endDate];
177     
178       return [self isWithinCalendarDateRange: _range
179                    firstInstanceCalendarDateRange: fir];
180     }
181
182   return NO;
183 }
184
185 - (NSArray *) recurrenceRangesWithinCalendarDateRange: (NGCalendarDateRange *)_r
186 {
187   NGCalendarDateRange *fir;
188   
189   if (![self isRecurrent])
190     return nil;
191
192   fir = [NGCalendarDateRange calendarDateRangeWithStartDate: [self startDate]
193                              endDate: [self endDate]];
194   return [self recurrenceRangesWithinCalendarDateRange:_r
195                firstInstanceCalendarDateRange:fir];
196 }
197
198 - (NSCalendarDate *) lastPossibleRecurrenceStartDate
199 {
200   NGCalendarDateRange *fir;
201
202   if (![self isRecurrent])
203     return nil;
204
205   fir = [NGCalendarDateRange calendarDateRangeWithStartDate: [self startDate]
206                              endDate: [self endDate]];
207
208   return [self lastPossibleRecurrenceStartDateUsingFirstInstanceCalendarDateRange: fir];
209 }
210
211 /* ical typing */
212
213 - (NSString *) entityName
214 {
215   return @"vevent";
216 }
217
218 /* descriptions */
219
220 // - (NSString *) description {
221 //   NSMutableString *ms;
222
223 //   ms = [NSMutableString stringWithCapacity:128];
224 //   [ms appendFormat:@"<0x%p[%@]:", self, NSStringFromClass([self class])];
225
226 //   if (uid)       [ms appendFormat:@" uid=%@", uid];
227 //   if (startDate) [ms appendFormat:@" from=%@", startDate];
228 //   if (endDate)   [ms appendFormat:@" to=%@", endDate];
229 //   if (summary)   [ms appendFormat:@" summary=%@", summary];
230   
231 //   if (organizer)
232 //     [ms appendFormat:@" organizer=%@", organizer];
233 //   if (attendees)
234 //     [ms appendFormat:@" attendees=%@", attendees];
235   
236 //   if ([self hasAlarms])
237 //     [ms appendFormat:@" alarms=%@", alarms];
238   
239 //   [ms appendString:@">"];
240 //   return ms;
241 // }
242
243 /* changes */
244
245 - (iCalEventChanges *) getChangesRelativeToEvent: (iCalEvent *) _event
246 {
247   return [iCalEventChanges changesFromEvent: _event
248                            toEvent: self];
249 }
250
251 @end /* iCalEvent */