]> err.no Git - scalable-opengroupware.org/blob - SOPE/NGCards/iCalYearlyRecurrenceCalculator.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1178 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / SOPE / NGCards / 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 #import <NGExtensions/NSCalendarDate+misc.h>
23
24 #import "iCalRecurrenceCalculator.h"
25
26 @interface iCalYearlyRecurrenceCalculator : iCalRecurrenceCalculator
27 @end
28
29 #import <NGExtensions/NGCalendarDateRange.h>
30 #import "iCalRecurrenceRule.h"
31 #import "NSCalendarDate+ICal.h"
32
33 @interface iCalRecurrenceCalculator(PrivateAPI)
34 - (NSCalendarDate *)lastInstanceStartDate;
35 @end
36
37 @implementation iCalYearlyRecurrenceCalculator
38
39 - (NSArray *)recurrenceRangesWithinCalendarDateRange:(NGCalendarDateRange *)_r{
40   NSMutableArray *ranges;
41   NSCalendarDate *firStart, *rStart, *rEnd, *until;
42   unsigned       i, count, interval;
43   int            diff;
44   
45   firStart = [self->firstRange startDate];
46   rStart   = [_r startDate];
47   rEnd     = [_r endDate];
48   interval = [self->rrule repeatInterval];
49   until    = [self lastInstanceStartDate];
50   
51   if (until) {
52     if ([until compare:rStart] == NSOrderedAscending)
53       return nil;
54     if ([until compare:rEnd] == NSOrderedDescending)
55       rEnd = until;
56   }
57   
58   diff   = [firStart yearsBetweenDate:rStart];
59   if ((diff != 0) && [rStart compare:firStart] == NSOrderedAscending)
60     diff = -diff;
61
62   count  = [rStart yearsBetweenDate:rEnd] + 1;
63   ranges = [NSMutableArray arrayWithCapacity:count];
64   for (i = 0 ; i < count; i++) {
65     int test;
66
67     test = diff + i;
68     if ((test >= 0) && (test % interval) == 0) {
69       NSCalendarDate      *start, *end;
70       NGCalendarDateRange *r;
71       
72       start = [firStart dateByAddingYears:diff + i
73                         months:0
74                         days:0];
75       [start setTimeZone:[firStart timeZone]];
76       end   = [start addTimeInterval:[self->firstRange duration]];
77       r     = [NGCalendarDateRange calendarDateRangeWithStartDate:start
78                                    endDate:end];
79       if ([_r containsDateRange:r])
80         [ranges addObject:r];
81     }
82   }
83   return ranges;
84 }
85
86 - (NSCalendarDate *)lastInstanceStartDate {
87   if ([self->rrule repeatCount] > 0) {
88     NSCalendarDate *until;
89     unsigned       years, interval;
90     
91     interval = [self->rrule repeatInterval];
92     years    = [self->rrule repeatCount] * interval;
93     until    = [[self->firstRange startDate] dateByAddingYears:years
94                                              months:0
95                                              days:0];
96     return until;
97   }
98   return [super lastInstanceStartDate];
99 }
100
101 @end /* iCalYearlyRecurrenceCalculator */