]> err.no Git - scalable-opengroupware.org/blob - SOPE/NGCards/iCalDailyRecurrenceCalculator.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1178 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / SOPE / NGCards / iCalDailyRecurrenceCalculator.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 <Foundation/NSDate.h>
23 #import <Foundation/NSArray.h>
24
25 #import <NGExtensions/NSCalendarDate+misc.h>
26
27 #import "iCalRecurrenceCalculator.h"
28
29 @interface iCalDailyRecurrenceCalculator : iCalRecurrenceCalculator
30 @end
31
32 #import <NGExtensions/NGCalendarDateRange.h>
33 #import "iCalRecurrenceRule.h"
34 #import "NSCalendarDate+ICal.h"
35
36 @interface iCalRecurrenceCalculator(PrivateAPI)
37 - (NSCalendarDate *)lastInstanceStartDate;
38 @end
39
40 @implementation iCalDailyRecurrenceCalculator
41
42 - (NSArray *)recurrenceRangesWithinCalendarDateRange:(NGCalendarDateRange *)_r{
43   NSMutableArray *ranges;
44   NSCalendarDate *firStart;
45   long           i, jnFirst, jnStart, jnEnd, startEndCount;
46   unsigned       interval;
47
48   firStart = [self->firstRange startDate];
49   jnFirst  = [firStart julianNumber];
50   jnEnd    = [[_r endDate] julianNumber];
51   
52   if (jnFirst > jnEnd)
53     return nil;
54   
55   jnStart  = [[_r startDate] julianNumber];
56   interval = [self->rrule repeatInterval];
57   
58   /* if rule is bound, check the bounds */
59   if (![self->rrule isInfinite]) {
60     NSCalendarDate *until;
61     long           jnRuleLast;
62     
63     until = [self->rrule untilDate];
64     if (until) {
65       if ([until compare:[_r startDate]] == NSOrderedAscending)
66         return nil;
67       jnRuleLast = [until julianNumber];
68     }
69     else {
70       jnRuleLast = (interval * [self->rrule repeatCount])
71       + jnFirst;
72       if (jnRuleLast < jnStart)
73         return nil;
74     }
75     /* jnStart < jnRuleLast < jnEnd ? */
76     if (jnEnd > jnRuleLast)
77       jnEnd = jnRuleLast;
78   }
79
80   startEndCount = (jnEnd - jnStart) + 1;
81   ranges        = [NSMutableArray arrayWithCapacity:startEndCount];
82   for (i = 0 ; i < startEndCount; i++) {
83     long jnCurrent;
84     
85     jnCurrent = jnStart + i;
86     if (jnCurrent >= jnFirst) {
87       long jnTest;
88       
89       jnTest = jnCurrent - jnFirst;
90       if ((jnTest % interval) == 0) {
91         NSCalendarDate      *start, *end;
92         NGCalendarDateRange *r;
93       
94         start = [NSCalendarDate dateForJulianNumber:jnCurrent];
95         [start setTimeZone:[firStart timeZone]];
96         start = [start hour:  [firStart hourOfDay]
97                        minute:[firStart minuteOfHour]
98                        second:[firStart secondOfMinute]];
99         end   = [start addTimeInterval:[self->firstRange duration]];
100         r     = [NGCalendarDateRange calendarDateRangeWithStartDate:start
101                                      endDate:end];
102         if ([_r containsDateRange:r])
103           [ranges addObject:r];
104       }
105     }
106   }
107   return ranges;
108 }
109
110 - (NSCalendarDate *)lastInstanceStartDate {
111   if ([self->rrule repeatCount] > 0) {
112     long           jnFirst, jnRuleLast;
113     NSCalendarDate *firStart, *until;
114
115     firStart   = [self->firstRange startDate];
116     jnFirst    = [firStart julianNumber];
117     jnRuleLast = ([self->rrule repeatInterval] *
118                   [self->rrule repeatCount]) +
119                   jnFirst;
120     until      = [NSCalendarDate dateForJulianNumber:jnRuleLast];
121     until      = [until hour:  [firStart hourOfDay]
122                         minute:[firStart minuteOfHour]
123                         second:[firStart secondOfMinute]];
124     return until;
125   }
126   return [super lastInstanceStartDate];
127 }
128
129 @end /* iCalDailyRecurrenceCalculator */