]> err.no Git - sope/blob - sope-ical/NGiCal/iCalYearlyRecurrenceCalculator.m
moved cluster subclasses to own files
[sope] / sope-ical / NGiCal / iCalYearlyRecurrenceCalculator.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 iCalYearlyRecurrenceCalculator : 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 iCalYearlyRecurrenceCalculator
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 (until) {
51     if ([until compare:rStart] == NSOrderedAscending)
52       return nil;
53     if ([until compare:rEnd] == NSOrderedDescending)
54       rEnd = until;
55   }
56   
57   diff   = [firStart yearsBetweenDate:rStart];
58   if ((diff != 0) && [rStart compare:firStart] == NSOrderedAscending)
59     diff = -diff;
60
61   count  = [rStart yearsBetweenDate:rEnd] + 1;
62   ranges = [NSMutableArray arrayWithCapacity:count];
63   for (i = 0 ; i < count; i++) {
64     int test;
65
66     test = diff + i;
67     if ((test >= 0) && (test % interval) == 0) {
68       NSCalendarDate      *start, *end;
69       NGCalendarDateRange *r;
70       
71       start = [firStart dateByAddingYears:diff + i
72                         months:0
73                         days:0];
74       [start setTimeZone:[firStart timeZone]];
75       end   = [start addTimeInterval:[self->firstRange duration]];
76       r     = [NGCalendarDateRange calendarDateRangeWithStartDate:start
77                                    endDate:end];
78       if ([_r containsDateRange:r])
79         [ranges addObject:r];
80     }
81   }
82   return ranges;
83 }
84
85 - (NSCalendarDate *)lastInstanceStartDate {
86   if ([self->rrule repeatCount] > 0) {
87     NSCalendarDate *until;
88     unsigned       years, interval;
89     
90     interval = [self->rrule repeatInterval];
91     years    = [self->rrule repeatCount] * interval;
92     until    = [[self->firstRange startDate] dateByAddingYears:years
93                                              months:0
94                                              days:0];
95     return until;
96   }
97   return [super lastInstanceStartDate];
98 }
99
100 @end /* iCalYearlyRecurrenceCalculator */