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 #include "iCalRepeatableEntityObject.h"
23 #include <NGExtensions/NGCalendarDateRange.h>
24 #include "iCalRecurrenceRule.h"
25 #include "iCalRecurrenceCalculator.h"
28 @implementation iCalRepeatableEntityObject
31 [self->rRules release];
32 [self->exRules release];
33 [self->exDates release];
39 - (void)removeAllRecurrenceRules {
40 [self->rRules removeAllObjects];
42 - (void)addToRecurrenceRules:(id)_rrule {
43 if (_rrule == nil) return;
44 if (self->rRules == nil)
45 self->rRules = [[NSMutableArray alloc] initWithCapacity:1];
46 [self->rRules addObject:_rrule];
48 - (void)setRecurrenceRules:(NSArray *)_rrules {
49 if (_rrules == self->rRules)
51 [self->rRules release];
52 self->rRules = [_rrules mutableCopy];
54 - (BOOL)hasRecurrenceRules {
55 return [self->rRules count] > 0 ? YES : NO;
57 - (NSArray *)recurrenceRules {
61 - (void)removeAllExceptionRules {
62 [self->exRules removeAllObjects];
64 - (void)addToExceptionRules:(id)_rrule {
65 if (_rrule == nil) return;
66 if (self->exRules == nil)
67 self->exRules = [[NSMutableArray alloc] initWithCapacity:1];
68 [self->exRules addObject:_rrule];
70 - (void)setExceptionRules:(NSArray *)_rrules {
71 if (_rrules == self->exRules)
73 [self->exRules release];
74 self->exRules = [_rrules mutableCopy];
76 - (BOOL)hasExceptionRules {
77 return [self->exRules count] > 0 ? YES : NO;
79 - (NSArray *)exceptionRules {
83 - (void)removeAllExceptionDates {
84 [self->exDates removeAllObjects];
86 - (void)setExceptionDates:(NSArray *)_exDates {
87 if (_exDates == self->exDates)
89 [self->exDates release];
90 self->exDates = [_exDates mutableCopy];
92 - (void)addToExceptionDates:(id)_date {
93 if (_date == nil) return;
94 if (self->exDates == nil)
95 self->exDates = [[NSMutableArray alloc] initWithCapacity:4];
96 [self->exDates addObject:_date];
98 - (NSArray *)exceptionDates {
104 - (BOOL)isRecurrent {
105 return [self hasRecurrenceRules] ? YES : NO;
110 - (BOOL)isWithinCalendarDateRange:(NGCalendarDateRange *)_range
111 firstInstanceCalendarDateRange:(NGCalendarDateRange *)_fir
115 ranges = [self recurrenceRangesWithinCalendarDateRange:_range
116 firstInstanceCalendarDateRange:_fir];
117 return [ranges count] > 0;
120 - (NSArray *)recurrenceRangesWithinCalendarDateRange:(NGCalendarDateRange *)_r
121 firstInstanceCalendarDateRange:(NGCalendarDateRange *)_fir
123 iCalRecurrenceRule *rule;
124 iCalRecurrenceCalculator *calc;
125 NSMutableArray *ranges;
126 unsigned i, count, rCount;
128 ranges = [NSMutableArray array];
129 count = [self->rRules count];
130 for (i = 0; i < count; i++) {
133 rule = [self->rRules objectAtIndex:i];
134 calc = [iCalRecurrenceCalculator recurrenceCalculatorForRecurrenceRule:rule
135 withFirstInstanceCalendarDateRange:_fir];
136 rs = [calc recurrenceRangesWithinCalendarDateRange:_r];
137 [ranges addObjectsFromArray:rs];
143 /* test if any exceptions do match */
144 count = [self->exRules count];
145 for (i = 0; i < count; i++) {
148 rule = [self->exRules objectAtIndex:i];
149 calc = [iCalRecurrenceCalculator recurrenceCalculatorForRecurrenceRule:rule
150 withFirstInstanceCalendarDateRange:_fir];
151 rs = [calc recurrenceRangesWithinCalendarDateRange:_r];
152 [ranges removeObjectsInArray:rs];
158 /* exception dates are also possible */
159 rCount = [ranges count];
160 count = [self->exDates count];
161 for (i = 0; i < count; i++) {
162 NSCalendarDate *exDate;
163 NGCalendarDateRange *r;
166 exDate = [self->exDates objectAtIndex:i];
167 for (k = 0; k < rCount; k++) {
168 r = [ranges objectAtIndex:(rCount - k) - 1];
169 if ([r containsDate:exDate]) {
170 [ranges removeObjectAtIndex:k];
178 /* this is the outmost bound possible, not necessarily the real last date */
179 - (NSCalendarDate *)lastPossibleRecurrenceStartDateUsingFirstInstanceCalendarDateRange:(NGCalendarDateRange *)_r
181 NSCalendarDate *date;
184 count = [self->rRules count];
189 for (i = 0; i < count; i++) {
190 iCalRecurrenceRule *rule;
191 iCalRecurrenceCalculator *calc;
192 NSCalendarDate *rdate;
194 rule = [self->rRules objectAtIndex:i];
195 if ([rule isInfinite])
196 return nil; /* rule is not bound, hence no limit */
197 calc = [iCalRecurrenceCalculator recurrenceCalculatorForRecurrenceRule:rule
198 withFirstInstanceCalendarDateRange:_r];
199 rdate = [[calc lastInstanceCalendarDateRange] startDate];
200 if (date == nil || [date compare:rdate] == NSOrderedAscending)