2 Copyright (C) 2004-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 #import <Foundation/NSArray.h>
23 #import <Foundation/NSCalendarDate.h>
24 #import <Foundation/NSEnumerator.h>
25 #import <Foundation/NSString.h>
27 #import <NGExtensions/NGCalendarDateRange.h>
29 #import "iCalRecurrenceRule.h"
30 #import "iCalRecurrenceCalculator.h"
31 #import "iCalRepeatableEntityObject.h"
33 @implementation iCalRepeatableEntityObject
35 - (Class) classForTag: (NSString *) classTag
39 if ([classTag isEqualToString: @"RRULE"])
40 tagClass = [iCalRecurrenceRule class];
42 tagClass = [super classForTag: classTag];
49 - (void) removeAllRecurrenceRules
51 [children removeObjectsInArray: [self childrenWithTag: @"rrule"]];
54 - (void) addToRecurrenceRules: (id) _rrule
56 [self addChild: _rrule];
59 - (void) setRecurrenceRules: (NSArray *) _rrules
61 [children removeObjectsInArray: [self childrenWithTag: @"rrule"]];
62 [self addChildren: _rrules];
65 - (BOOL) hasRecurrenceRules
67 return ([[self childrenWithTag: @"rrule"] count] > 0);
70 - (NSArray *) recurrenceRules
72 return [self childrenWithTag: @"rrule"];
75 - (void) removeAllExceptionRules
77 [children removeObjectsInArray: [self childrenWithTag: @"exrule"]];
80 - (void) addToExceptionRules: (id) _rrule
82 [self addChild: _rrule];
85 - (void) setExceptionRules: (NSArray *) _rrules
87 [children removeObjectsInArray: [self childrenWithTag: @"exrule"]];
88 [self addChildren: _rrules];
91 - (BOOL) hasExceptionRules
93 return ([[self childrenWithTag: @"exrule"] count] > 0);
96 - (NSArray *) exceptionRules
98 return [self childrenWithTag: @"exrule"];
101 - (void) removeAllExceptionDates
103 [children removeObjectsInArray: [self childrenWithTag: @"exdate"]];
106 - (void) addToExceptionDates: (id) _rdate
108 [self addChild: _rdate];
111 - (void) setExceptionDates: (NSArray *) _rdates
113 [children removeObjectsInArray: [self childrenWithTag: @"exdate"]];
114 [self addChildren: _rdates];
117 - (BOOL) hasExceptionDates
119 return ([[self childrenWithTag: @"exdate"] count] > 0);
122 - (NSArray *) exceptionDates
124 return [self childrenWithTag: @"exdate"];
131 return [self hasRecurrenceRules];
136 - (BOOL) isWithinCalendarDateRange: (NGCalendarDateRange *) _range
137 firstInstanceCalendarDateRange: (NGCalendarDateRange *) _fir
141 ranges = [self recurrenceRangesWithinCalendarDateRange:_range
142 firstInstanceCalendarDateRange:_fir];
143 return [ranges count] > 0;
146 - (NSArray *) recurrenceRangesWithinCalendarDateRange: (NGCalendarDateRange *)_r
147 firstInstanceCalendarDateRange: (NGCalendarDateRange *)_fir
149 return [iCalRecurrenceCalculator recurrenceRangesWithinCalendarDateRange: _r
150 firstInstanceCalendarDateRange: _fir
151 recurrenceRules: [self recurrenceRules]
152 exceptionRules: [self exceptionRules]
153 exceptionDates: [self exceptionDates]];
157 /* this is the outmost bound possible, not necessarily the real last date */
159 lastPossibleRecurrenceStartDateUsingFirstInstanceCalendarDateRange: (NGCalendarDateRange *)_r
161 NSCalendarDate *date;
162 NSEnumerator *rRules;
163 iCalRecurrenceRule *rule;
164 iCalRecurrenceCalculator *calc;
165 NSCalendarDate *rdate;
169 rRules = [[self recurrenceRules] objectEnumerator];
170 rule = [rRules nextObject];
171 while (rule && ![rule isInfinite] & !date)
173 calc = [iCalRecurrenceCalculator
174 recurrenceCalculatorForRecurrenceRule: rule
175 withFirstInstanceCalendarDateRange: _r];
176 rdate = [[calc lastInstanceCalendarDateRange] startDate];
178 || ([date compare: rdate] == NSOrderedAscending))
181 rule = [rRules nextObject];