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 - (id)copyWithZone:(NSZone *)_zone {
40 iCalRepeatableEntityObject *new;
42 new = [super copyWithZone:_zone];
44 ASSIGNCOPY(new->rRules, self->rRules);
45 ASSIGNCOPY(new->exRules, self->exRules);
46 ASSIGNCOPY(new->exDates, self->exDates);
53 - (void)removeAllRecurrenceRules {
54 [self->rRules removeAllObjects];
56 - (void)addToRecurrenceRules:(id)_rrule {
57 if (_rrule == nil) return;
58 if (self->rRules == nil)
59 self->rRules = [[NSMutableArray alloc] initWithCapacity:1];
60 [self->rRules addObject:_rrule];
62 - (void)setRecurrenceRules:(NSArray *)_rrules {
63 if (_rrules == self->rRules)
65 [self->rRules release];
66 self->rRules = [_rrules mutableCopy];
68 - (BOOL)hasRecurrenceRules {
69 return [self->rRules count] > 0 ? YES : NO;
71 - (NSArray *)recurrenceRules {
75 - (void)removeAllExceptionRules {
76 [self->exRules removeAllObjects];
78 - (void)addToExceptionRules:(id)_rrule {
79 if (_rrule == nil) return;
80 if (self->exRules == nil)
81 self->exRules = [[NSMutableArray alloc] initWithCapacity:1];
82 [self->exRules addObject:_rrule];
84 - (void)setExceptionRules:(NSArray *)_rrules {
85 if (_rrules == self->exRules)
87 [self->exRules release];
88 self->exRules = [_rrules mutableCopy];
90 - (BOOL)hasExceptionRules {
91 return [self->exRules count] > 0 ? YES : NO;
93 - (NSArray *)exceptionRules {
97 - (void)removeAllExceptionDates {
98 [self->exDates removeAllObjects];
100 - (void)setExceptionDates:(NSArray *)_exDates {
101 if (_exDates == self->exDates)
103 [self->exDates release];
104 self->exDates = [_exDates mutableCopy];
106 - (void)addToExceptionDates:(id)_date {
107 if (_date == nil) return;
108 if (self->exDates == nil)
109 self->exDates = [[NSMutableArray alloc] initWithCapacity:4];
110 [self->exDates addObject:_date];
112 - (BOOL)hasExceptionDates {
113 return [self->exDates count] > 0 ? YES : NO;
115 - (NSArray *)exceptionDates {
116 return self->exDates;
121 - (BOOL)isRecurrent {
122 return [self hasRecurrenceRules] ? YES : NO;
127 - (BOOL)isWithinCalendarDateRange:(NGCalendarDateRange *)_range
128 firstInstanceCalendarDateRange:(NGCalendarDateRange *)_fir
132 ranges = [self recurrenceRangesWithinCalendarDateRange:_range
133 firstInstanceCalendarDateRange:_fir];
134 return [ranges count] > 0;
137 - (NSArray *)recurrenceRangesWithinCalendarDateRange:(NGCalendarDateRange *)_r
138 firstInstanceCalendarDateRange:(NGCalendarDateRange *)_fir
140 return [iCalRecurrenceCalculator recurrenceRangesWithinCalendarDateRange:_r
141 firstInstanceCalendarDateRange:_fir
142 recurrenceRules:self->rRules
143 exceptionRules:self->exRules
144 exceptionDates:self->exDates];
148 /* this is the outmost bound possible, not necessarily the real last date */
149 - (NSCalendarDate *)lastPossibleRecurrenceStartDateUsingFirstInstanceCalendarDateRange:(NGCalendarDateRange *)_r
151 NSCalendarDate *date;
154 count = [self->rRules count];
159 for (i = 0; i < count; i++) {
160 iCalRecurrenceRule *rule;
161 iCalRecurrenceCalculator *calc;
162 NSCalendarDate *rdate;
164 rule = [self->rRules objectAtIndex:i];
165 if ([rule isInfinite])
166 return nil; /* rule is not bound, hence no limit */
167 calc = [iCalRecurrenceCalculator recurrenceCalculatorForRecurrenceRule:rule
168 withFirstInstanceCalendarDateRange:_r];
169 rdate = [[calc lastInstanceCalendarDateRange] startDate];
170 if (date == nil || [date compare:rdate] == NSOrderedAscending)