]> err.no Git - sope/blob - sope-ical/NGiCal/iCalMonthlyRecurrenceCalculator.m
improved byday (failure) handling
[sope] / sope-ical / NGiCal / iCalMonthlyRecurrenceCalculator.m
1 /*
2   Copyright (C) 2004-2005 SKYRIX Software AG
3   
4   This file is part of SOPE.
5   
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
9   later version.
10   
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.
15   
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
19   02111-1307, USA.
20 */
21
22 #include "iCalRecurrenceCalculator.h"
23
24 @interface iCalMonthlyRecurrenceCalculator : iCalRecurrenceCalculator
25 @end
26
27 #include <NGExtensions/NGCalendarDateRange.h>
28 #include "iCalRecurrenceRule.h"
29 #include "NSCalendarDate+ICal.h"
30 #include "common.h"
31
32 @interface iCalRecurrenceCalculator(PrivateAPI)
33 - (NSCalendarDate *)lastInstanceStartDate;
34 @end
35
36 @implementation iCalMonthlyRecurrenceCalculator
37
38 - (NSArray *)recurrenceRangesWithinCalendarDateRange:(NGCalendarDateRange *)_r{
39   NSMutableArray *ranges;
40   NSCalendarDate *firStart, *rStart, *rEnd, *until;
41   unsigned       i, count, interval;
42   int            diff;
43
44   firStart = [self->firstRange startDate];
45   rStart   = [_r startDate];
46   rEnd     = [_r endDate];
47   interval = [self->rrule repeatInterval];
48   until    = [self lastInstanceStartDate];
49   
50   if ([self->rrule byDayMask] != 0) {
51     [self errorWithFormat:@"cannot process byday part of rrule: %@", 
52             self->rrule];
53     return nil;
54   }
55   
56   if (until != nil) {
57     if ([until compare:rStart] == NSOrderedAscending)
58       return nil;
59     if ([until compare:rEnd] == NSOrderedDescending)
60       rEnd = until;
61   }
62
63   diff   = [firStart monthsBetweenDate:rStart];
64   if ((diff != 0) && [rStart compare:firStart] == NSOrderedAscending)
65     diff = -diff;
66
67   count  = [rStart monthsBetweenDate:rEnd] + 1;
68   ranges = [NSMutableArray arrayWithCapacity:count];
69   for (i = 0 ; i < count; i++) {
70     int test;
71     
72     test = diff + i;
73     if ((test >= 0) && (test % interval) == 0) {
74       NSCalendarDate      *start, *end;
75       NGCalendarDateRange *r;
76       
77       start = [firStart dateByAddingYears:0
78                         months:diff + i
79                         days:0];
80       [start setTimeZone:[firStart timeZone]];
81       end   = [start addTimeInterval:[self->firstRange duration]];
82       r     = [NGCalendarDateRange calendarDateRangeWithStartDate:start
83                                    endDate:end];
84       if ([_r containsDateRange:r])
85         [ranges addObject:r];
86     }
87   }
88   return ranges;
89 }
90
91 - (NSCalendarDate *)lastInstanceStartDate {
92   if ([self->rrule repeatCount] > 0) {
93     NSCalendarDate *until;
94     unsigned       months, interval;
95
96     interval = [self->rrule repeatInterval];
97     months   = [self->rrule repeatCount] * interval;
98     until    = [[self->firstRange startDate] dateByAddingYears:0
99                                              months:months
100                                              days:0];
101     return until;
102   }
103   return [super lastInstanceStartDate];
104 }
105
106 @end /* iCalMonthlyRecurrenceCalculator */