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 "iCalRecurrenceCalculator.h"
24 @interface iCalMonthlyRecurrenceCalculator : iCalRecurrenceCalculator
27 #include <NGExtensions/NGCalendarDateRange.h>
28 #include "iCalRecurrenceRule.h"
29 #include "NSCalendarDate+ICal.h"
32 @interface iCalRecurrenceCalculator(PrivateAPI)
33 - (NSCalendarDate *)lastInstanceStartDate;
36 // #define HEAVY_DEBUG 1
38 @implementation iCalMonthlyRecurrenceCalculator
40 typedef BOOL NGMonthSet[12];
41 typedef BOOL NGMonthDaySet[32]; // 0 is unused
43 static void NGMonthDaySet_clear(NGMonthDaySet *daySet) {
46 for (i = 1; i <= 31; i++)
50 static void NGMonthDaySet_copyOrUnion(NGMonthDaySet *base, NGMonthDaySet *new,
56 memcpy(base, new, sizeof(NGMonthDaySet));
58 for (i = 1; i <= 31; i++) {
65 static BOOL NGMonthDaySet_fillWithByMonthDay(NGMonthDaySet *daySet,
68 /* list of days in the month */
72 NGMonthDaySet_clear(daySet);
74 for (i = 0, count = [byMonthDay count], ok = YES; i < count; i++) {
75 int dayInMonth; /* -31..-1 and 1..31 */
77 if ((dayInMonth = [[byMonthDay objectAtIndex:i] intValue]) == 0) {
79 continue; /* invalid value */
81 if (dayInMonth > 31) {
83 continue; /* error, value to large */
85 if (dayInMonth < -31) {
87 continue; /* error, value to large */
90 /* adjust negative days */
93 /* eg: -1 == last day in month, 30 days => 30 */
94 dayInMonth = 32 - dayInMonth /* because we count from 1 */;
97 (*daySet)[dayInMonth] = YES;
102 static inline unsigned iCalDoWForNSDoW(int dow) {
104 case 0: return iCalWeekDaySunday;
105 case 1: return iCalWeekDayMonday;
106 case 2: return iCalWeekDayTuesday;
107 case 3: return iCalWeekDayWednesday;
108 case 4: return iCalWeekDayThursday;
109 case 5: return iCalWeekDayFriday;
110 case 6: return iCalWeekDaySaturday;
111 case 7: return iCalWeekDaySunday;
117 static NSString *dowEN[8] = {
118 @"SU", @"MO", @"TU", @"WE", @"TH", @"FR", @"SA", @"SU-"
122 static void NGMonthDaySet_fillWithByDayX(NGMonthDaySet *daySet,
124 unsigned firstDoWInMonth,
125 unsigned numberOfDaysInMonth,
128 // TODO: this is called 'X' because the API doesn't allow for full iCalendar
129 // functionality. The daymask must be a list of occurence+dow
130 register unsigned dayInMonth;
131 register int dow; /* current day of the week */
132 int occurrences[7] = { 0, 0, 0, 0, 0, 0, 0 } ;
134 NGMonthDaySet_clear(daySet);
136 if (occurrence1 >= 0) {
137 for (dayInMonth = 1, dow = firstDoWInMonth; dayInMonth<=31; dayInMonth++) {
140 if (dayMask & iCalDoWForNSDoW(dow)) {
141 if (occurrence1 == 0)
142 (*daySet)[dayInMonth] = YES;
143 else { /* occurrence1 > 0 */
144 occurrences[dow] = occurrences[dow] + 1;
146 if (occurrences[dow] == occurrence1)
147 (*daySet)[dayInMonth] = YES;
151 dow = (dow == 6 /* Sat */) ? 0 /* Sun */ : (dow + 1);
155 int lastDoWInMonthSet;
157 /* get the last dow in the set (not necessarily the month!) */
158 for (dayInMonth = 1, dow = firstDoWInMonth;
159 dayInMonth < numberOfDaysInMonth;dayInMonth++)
160 dow = (dow == 6 /* Sat */) ? 0 /* Sun */ : (dow + 1);
161 lastDoWInMonthSet = dow;
164 NSLog(@"LAST DOW IN SET: %i / %@",
165 lastDoWInMonthSet, dowEN[lastDoWInMonthSet]);
167 /* start at the end of the set */
168 for (dayInMonth = numberOfDaysInMonth, dow = lastDoWInMonthSet;
169 dayInMonth >= 1; dayInMonth--) {
173 NSLog(@" CHECK day-of-month %02i, "
174 @" dow=%i/%@ (first=%i/%@, last=%i/%@)",
177 firstDoWInMonth, dowEN[firstDoWInMonth],
178 lastDoWInMonthSet, dowEN[lastDoWInMonthSet]
182 if (dayMask & iCalDoWForNSDoW(dow)) {
183 occurrences[dow] = occurrences[dow] + 1;
185 NSLog(@" MATCH %i/%@ count: %i occurences=%i",
186 dow, dowEN[dow], occurrences[dow], occurrence1);
189 if (occurrences[dow] == -occurrence1) {
191 NSLog(@" COUNT MATCH");
193 (*daySet)[dayInMonth] = YES;
197 dow = (dow == 0 /* Sun */) ? 6 /* Sat */ : (dow - 1);
202 - (BOOL)_addInstanceWithStartDate:(NSCalendarDate *)_startDate
203 limitDate:(NSCalendarDate *)_until
204 limitRange:(NGCalendarDateRange *)_r
205 toArray:(NSMutableArray *)_ranges
207 NGCalendarDateRange *r;
210 /* check whether we are still in the limits */
212 // TODO: I think we should check in here whether we succeeded the
213 // repeatCount. Currently we precalculate that info in the
214 // -lastInstanceStartDate method.
216 /* Note: the 'until' in the rrule is inclusive as per spec */
217 if ([_until compare:_startDate] == NSOrderedAscending)
218 /* start after until */
219 return NO; /* Note: we assume that the algorithm is sequential */
222 /* create end date */
224 end = [_startDate addTimeInterval:[self->firstRange duration]];
225 [end setTimeZone:[_startDate timeZone]];
227 /* create range and check whether its in the requested range */
229 r = [[NGCalendarDateRange alloc] initWithStartDate:_startDate endDate:end];
230 if ([_r containsDateRange:r])
231 [_ranges addObject:r];
232 [r release]; r = nil;
237 - (NSArray *)recurrenceRangesWithinCalendarDateRange:(NGCalendarDateRange *)_r{
239 // TODO: check whether this is OK for multiday-events!
240 NSMutableArray *ranges;
241 NSTimeZone *timeZone;
242 NSCalendarDate *eventStartDate, *rStart, *rEnd, *until;
244 unsigned monthIdxInRange, numberOfMonthsInRange, interval;
246 NGMonthSet byMonthList = { // TODO: fill from rrule, this is the default
247 YES, YES, YES, YES, YES, YES,
248 YES, YES, YES, YES, YES, YES
250 NSArray *byMonthDay = nil; // array of ints (-31..-1 and 1..31)
251 NGMonthDaySet byMonthDaySet;
253 eventStartDate = [self->firstRange startDate];
254 eventDayOfMonth = [eventStartDate dayOfMonth];
255 timeZone = [eventStartDate timeZone];
256 rStart = [_r startDate];
258 interval = [self->rrule repeatInterval];
259 until = [self lastInstanceStartDate]; // TODO: maybe replace
260 byMonthDay = [self->rrule byMonthDay];
263 /* check whether the range to be processed is beyond the 'until' date */
266 if ([until compare:rStart] == NSOrderedAscending) /* until before start */
268 if ([until compare:rEnd] == NSOrderedDescending) /* end before until */
269 rEnd = until; // TODO: why is that? end is _before_ until?
273 /* precalculate month days (same for all instances) */
275 if (byMonthDay != nil)
276 NGMonthDaySet_fillWithByMonthDay(&byMonthDaySet, byMonthDay);
279 // TODO: I think the 'diff' is to skip recurrence which are before the
280 // requested range. Not sure whether this is actually possible, eg
281 // the repeatCount must be processed from the start.
282 diff = [eventStartDate monthsBetweenDate:rStart];
283 if ((diff != 0) && [rStart compare:eventStartDate] == NSOrderedAscending)
286 numberOfMonthsInRange = [rStart monthsBetweenDate:rEnd] + 1;
287 ranges = [NSMutableArray arrayWithCapacity:numberOfMonthsInRange];
290 Note: we do not add 'eventStartDate', this is intentional, the event date
291 itself is _not_ necessarily part of the sequence, eg with monthly
295 for (monthIdxInRange = 0; monthIdxInRange < numberOfMonthsInRange;
297 NSCalendarDate *cursor;
298 unsigned numDaysInMonth;
299 int monthIdxInRecurrence, dom;
300 NGMonthDaySet monthDays;
301 BOOL didByFill, doCont;
303 monthIdxInRecurrence = diff + monthIdxInRange;
305 if (monthIdxInRecurrence < 0)
308 /* first check whether we are in the interval */
310 if ((monthIdxInRecurrence % interval) != 0)
314 Then the sequence is:
315 - check whether the month is in the BYMONTH list
318 cursor = [eventStartDate dateByAddingYears:0
319 months:(diff + monthIdxInRange)
321 [cursor setTimeZone:timeZone];
322 numDaysInMonth = [cursor numberOfDaysInMonth];
325 /* check whether we match the bymonth specification */
327 if (!byMonthList[[cursor monthOfYear] - 1])
331 /* check 'day level' byXYZ rules */
335 if (byMonthDay != nil) { /* list of days in the month */
336 NGMonthDaySet_copyOrUnion(&monthDays, &byMonthDaySet, !didByFill);
340 if ([self->rrule byDayMask] != 0) { // TODO: replace the mask with an array
341 NGMonthDaySet ruleset;
342 unsigned firstDoWInMonth;
344 firstDoWInMonth = [[cursor firstDayOfMonth] dayOfWeek];
346 NGMonthDaySet_fillWithByDayX(&ruleset,
347 [self->rrule byDayMask],
349 [cursor numberOfDaysInMonth],
350 [self->rrule byDayOccurence1]);
351 NGMonthDaySet_copyOrUnion(&monthDays, &ruleset, !didByFill);
356 /* no rules applied, take the dayOfMonth of the startDate */
357 NGMonthDaySet_clear(&monthDays);
358 monthDays[eventDayOfMonth] = YES;
361 // TODO: add processing of byhour/byminute/bysecond etc
363 for (dom = 1, doCont = YES; dom <= numDaysInMonth && doCont; dom++) {
364 NSCalendarDate *start;
369 if (eventDayOfMonth == dom)
372 start = [cursor dateByAddingYears:0 months:0
373 days:(dom - eventDayOfMonth)];
376 doCont = [self _addInstanceWithStartDate:start
381 if (!doCont) break; /* reached some limit */
386 - (NSCalendarDate *)lastInstanceStartDate {
387 if ([self->rrule repeatCount] > 0) {
388 NSCalendarDate *until;
389 unsigned months, interval;
391 interval = [self->rrule repeatInterval];
392 months = [self->rrule repeatCount] - 1 /* the first counts as one! */;
397 until = [[self->firstRange startDate] dateByAddingYears:0
402 return [super lastInstanceStartDate];
405 @end /* iCalMonthlyRecurrenceCalculator */