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"
33 @interface iCalRecurrenceCalculator(PrivateAPI)
34 - (NSCalendarDate *)lastInstanceStartDate;
37 // #define HEAVY_DEBUG 1
39 @implementation iCalMonthlyRecurrenceCalculator
41 typedef BOOL NGMonthSet[12];
42 typedef BOOL NGMonthDaySet[32]; // 0 is unused
44 static void NGMonthDaySet_clear(NGMonthDaySet *daySet) {
47 for (i = 1; i <= 31; i++)
51 static void NGMonthDaySet_copyOrUnion(NGMonthDaySet *base, NGMonthDaySet *new,
57 memcpy(base, new, sizeof(NGMonthDaySet));
59 for (i = 1; i <= 31; i++) {
66 static BOOL NGMonthDaySet_fillWithByMonthDay(NGMonthDaySet *daySet,
69 /* list of days in the month */
73 NGMonthDaySet_clear(daySet);
75 for (i = 0, count = [byMonthDay count], ok = YES; i < count; i++) {
76 int dayInMonth; /* -31..-1 and 1..31 */
78 if ((dayInMonth = [[byMonthDay objectAtIndex:i] intValue]) == 0) {
80 continue; /* invalid value */
82 if (dayInMonth > 31) {
84 continue; /* error, value to large */
86 if (dayInMonth < -31) {
88 continue; /* error, value to large */
91 /* adjust negative days */
94 /* eg: -1 == last day in month, 30 days => 30 */
95 dayInMonth = 32 - dayInMonth /* because we count from 1 */;
98 (*daySet)[dayInMonth] = YES;
103 static inline unsigned iCalDoWForNSDoW(int dow) {
105 case 0: return iCalWeekDaySunday;
106 case 1: return iCalWeekDayMonday;
107 case 2: return iCalWeekDayTuesday;
108 case 3: return iCalWeekDayWednesday;
109 case 4: return iCalWeekDayThursday;
110 case 5: return iCalWeekDayFriday;
111 case 6: return iCalWeekDaySaturday;
112 case 7: return iCalWeekDaySunday;
118 static NSString *dowEN[8] = {
119 @"SU", @"MO", @"TU", @"WE", @"TH", @"FR", @"SA", @"SU-"
123 static void NGMonthDaySet_fillWithByDayX(NGMonthDaySet *daySet,
125 unsigned firstDoWInMonth,
126 unsigned numberOfDaysInMonth,
129 // TODO: this is called 'X' because the API doesn't allow for full iCalendar
130 // functionality. The daymask must be a list of occurence+dow
131 register unsigned dayInMonth;
132 register int dow; /* current day of the week */
133 int occurrences[7] = { 0, 0, 0, 0, 0, 0, 0 } ;
135 NGMonthDaySet_clear(daySet);
137 if (occurrence1 >= 0) {
138 for (dayInMonth = 1, dow = firstDoWInMonth; dayInMonth<=31; dayInMonth++) {
141 if (dayMask & iCalDoWForNSDoW(dow)) {
142 if (occurrence1 == 0)
143 (*daySet)[dayInMonth] = YES;
144 else { /* occurrence1 > 0 */
145 occurrences[dow] = occurrences[dow] + 1;
147 if (occurrences[dow] == occurrence1)
148 (*daySet)[dayInMonth] = YES;
152 dow = (dow == 6 /* Sat */) ? 0 /* Sun */ : (dow + 1);
156 int lastDoWInMonthSet;
158 /* get the last dow in the set (not necessarily the month!) */
159 for (dayInMonth = 1, dow = firstDoWInMonth;
160 dayInMonth < numberOfDaysInMonth;dayInMonth++)
161 dow = (dow == 6 /* Sat */) ? 0 /* Sun */ : (dow + 1);
162 lastDoWInMonthSet = dow;
165 NSLog(@"LAST DOW IN SET: %i / %@",
166 lastDoWInMonthSet, dowEN[lastDoWInMonthSet]);
168 /* start at the end of the set */
169 for (dayInMonth = numberOfDaysInMonth, dow = lastDoWInMonthSet;
170 dayInMonth >= 1; dayInMonth--) {
174 NSLog(@" CHECK day-of-month %02i, "
175 @" dow=%i/%@ (first=%i/%@, last=%i/%@)",
178 firstDoWInMonth, dowEN[firstDoWInMonth],
179 lastDoWInMonthSet, dowEN[lastDoWInMonthSet]
183 if (dayMask & iCalDoWForNSDoW(dow)) {
184 occurrences[dow] = occurrences[dow] + 1;
186 NSLog(@" MATCH %i/%@ count: %i occurences=%i",
187 dow, dowEN[dow], occurrences[dow], occurrence1);
190 if (occurrences[dow] == -occurrence1) {
192 NSLog(@" COUNT MATCH");
194 (*daySet)[dayInMonth] = YES;
198 dow = (dow == 0 /* Sun */) ? 6 /* Sat */ : (dow - 1);
203 - (BOOL)_addInstanceWithStartDate:(NSCalendarDate *)_startDate
204 limitDate:(NSCalendarDate *)_until
205 limitRange:(NGCalendarDateRange *)_r
206 toArray:(NSMutableArray *)_ranges
208 NGCalendarDateRange *r;
211 /* check whether we are still in the limits */
213 // TODO: I think we should check in here whether we succeeded the
214 // repeatCount. Currently we precalculate that info in the
215 // -lastInstanceStartDate method.
217 /* Note: the 'until' in the rrule is inclusive as per spec */
218 if ([_until compare:_startDate] == NSOrderedAscending)
219 /* start after until */
220 return NO; /* Note: we assume that the algorithm is sequential */
223 /* create end date */
225 end = [_startDate addTimeInterval:[self->firstRange duration]];
226 [end setTimeZone:[_startDate timeZone]];
228 /* create range and check whether its in the requested range */
230 r = [[NGCalendarDateRange alloc] initWithStartDate:_startDate endDate:end];
231 if ([_r containsDateRange:r])
232 [_ranges addObject:r];
233 [r release]; r = nil;
238 - (NSArray *)recurrenceRangesWithinCalendarDateRange:(NGCalendarDateRange *)_r{
240 // TODO: check whether this is OK for multiday-events!
241 NSMutableArray *ranges;
242 NSTimeZone *timeZone;
243 NSCalendarDate *eventStartDate, *rStart, *rEnd, *until;
245 unsigned monthIdxInRange, numberOfMonthsInRange, interval;
247 NGMonthSet byMonthList = { // TODO: fill from rrule, this is the default
248 YES, YES, YES, YES, YES, YES,
249 YES, YES, YES, YES, YES, YES
251 NSArray *byMonthDay = nil; // array of ints (-31..-1 and 1..31)
252 NGMonthDaySet byMonthDaySet;
254 eventStartDate = [self->firstRange startDate];
255 eventDayOfMonth = [eventStartDate dayOfMonth];
256 timeZone = [eventStartDate timeZone];
257 rStart = [_r startDate];
259 interval = [self->rrule repeatInterval];
260 until = [self lastInstanceStartDate]; // TODO: maybe replace
261 byMonthDay = [self->rrule byMonthDay];
264 /* check whether the range to be processed is beyond the 'until' date */
267 if ([until compare:rStart] == NSOrderedAscending) /* until before start */
269 if ([until compare:rEnd] == NSOrderedDescending) /* end before until */
270 rEnd = until; // TODO: why is that? end is _before_ until?
274 /* precalculate month days (same for all instances) */
276 if (byMonthDay != nil)
277 NGMonthDaySet_fillWithByMonthDay(&byMonthDaySet, byMonthDay);
280 // TODO: I think the 'diff' is to skip recurrence which are before the
281 // requested range. Not sure whether this is actually possible, eg
282 // the repeatCount must be processed from the start.
283 diff = [eventStartDate monthsBetweenDate:rStart];
284 if ((diff != 0) && [rStart compare:eventStartDate] == NSOrderedAscending)
287 numberOfMonthsInRange = [rStart monthsBetweenDate:rEnd] + 1;
288 ranges = [NSMutableArray arrayWithCapacity:numberOfMonthsInRange];
291 Note: we do not add 'eventStartDate', this is intentional, the event date
292 itself is _not_ necessarily part of the sequence, eg with monthly
296 for (monthIdxInRange = 0; monthIdxInRange < numberOfMonthsInRange;
298 NSCalendarDate *cursor;
299 unsigned numDaysInMonth;
300 int monthIdxInRecurrence, dom;
301 NGMonthDaySet monthDays;
302 BOOL didByFill, doCont;
304 monthIdxInRecurrence = diff + monthIdxInRange;
306 if (monthIdxInRecurrence < 0)
309 /* first check whether we are in the interval */
311 if ((monthIdxInRecurrence % interval) != 0)
315 Then the sequence is:
316 - check whether the month is in the BYMONTH list
319 cursor = [eventStartDate dateByAddingYears:0
320 months:(diff + monthIdxInRange)
322 [cursor setTimeZone:timeZone];
323 numDaysInMonth = [cursor numberOfDaysInMonth];
326 /* check whether we match the bymonth specification */
328 if (!byMonthList[[cursor monthOfYear] - 1])
332 /* check 'day level' byXYZ rules */
336 if (byMonthDay != nil) { /* list of days in the month */
337 NGMonthDaySet_copyOrUnion(&monthDays, &byMonthDaySet, !didByFill);
341 if ([self->rrule byDayMask] != 0) { // TODO: replace the mask with an array
342 NGMonthDaySet ruleset;
343 unsigned firstDoWInMonth;
345 firstDoWInMonth = [[cursor firstDayOfMonth] dayOfWeek];
347 NGMonthDaySet_fillWithByDayX(&ruleset,
348 [self->rrule byDayMask],
350 [cursor numberOfDaysInMonth],
351 [self->rrule byDayOccurence1]);
352 NGMonthDaySet_copyOrUnion(&monthDays, &ruleset, !didByFill);
357 /* no rules applied, take the dayOfMonth of the startDate */
358 NGMonthDaySet_clear(&monthDays);
359 monthDays[eventDayOfMonth] = YES;
362 // TODO: add processing of byhour/byminute/bysecond etc
364 for (dom = 1, doCont = YES; dom <= numDaysInMonth && doCont; dom++) {
365 NSCalendarDate *start;
370 if (eventDayOfMonth == dom)
373 start = [cursor dateByAddingYears:0 months:0
374 days:(dom - eventDayOfMonth)];
377 doCont = [self _addInstanceWithStartDate:start
382 if (!doCont) break; /* reached some limit */
387 - (NSCalendarDate *)lastInstanceStartDate {
388 if ([self->rrule repeatCount] > 0) {
389 NSCalendarDate *until;
390 unsigned months, interval;
392 interval = [self->rrule repeatInterval];
393 months = [self->rrule repeatCount] - 1 /* the first counts as one! */;
398 until = [[self->firstRange startDate] dateByAddingYears:0
403 return [super lastInstanceStartDate];
406 @end /* iCalMonthlyRecurrenceCalculator */