2007-01-31 18:11:29 +01:00
|
|
|
/*
|
|
|
|
Copyright (C) 2004-2005 SKYRIX Software AG
|
2008-12-10 19:40:20 +01:00
|
|
|
|
2007-01-31 18:11:29 +01:00
|
|
|
This file is part of SOPE.
|
2008-12-10 19:40:20 +01:00
|
|
|
|
2007-01-31 18:11:29 +01:00
|
|
|
SOPE is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU Lesser General Public License as published by the
|
|
|
|
Free Software Foundation; either version 2, or (at your option) any
|
|
|
|
later version.
|
2008-12-10 19:40:20 +01:00
|
|
|
|
2007-01-31 18:11:29 +01:00
|
|
|
SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
2008-12-10 19:40:20 +01:00
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
2007-01-31 18:11:29 +01:00
|
|
|
License for more details.
|
2008-12-10 19:40:20 +01:00
|
|
|
|
2007-01-31 18:11:29 +01:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2008-12-10 19:40:20 +01:00
|
|
|
License along with SOPE; see the file COPYING. If not, write to the
|
2007-01-31 18:11:29 +01:00
|
|
|
Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
|
|
|
|
02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2007-06-07 18:17:51 +02:00
|
|
|
#import <Foundation/NSDate.h>
|
|
|
|
#import <Foundation/NSArray.h>
|
|
|
|
|
|
|
|
#import <NGExtensions/NSCalendarDate+misc.h>
|
2008-01-28 16:32:14 +01:00
|
|
|
#import <NGExtensions/NGCalendarDateRange.h>
|
|
|
|
|
|
|
|
#import "NSCalendarDate+ICal.h"
|
2007-06-07 18:17:51 +02:00
|
|
|
|
|
|
|
#import "iCalRecurrenceCalculator.h"
|
2008-01-28 16:32:14 +01:00
|
|
|
#import "iCalRecurrenceRule.h"
|
2007-01-31 18:11:29 +01:00
|
|
|
|
|
|
|
@interface iCalDailyRecurrenceCalculator : iCalRecurrenceCalculator
|
|
|
|
@end
|
|
|
|
|
2008-12-10 19:40:20 +01:00
|
|
|
@interface iCalRecurrenceCalculator (PrivateAPI)
|
|
|
|
- (NSCalendarDate *) lastInstanceStartDate;
|
2007-01-31 18:11:29 +01:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation iCalDailyRecurrenceCalculator
|
|
|
|
|
2008-12-10 19:40:20 +01:00
|
|
|
- (NSArray *)
|
|
|
|
recurrenceRangesWithinCalendarDateRange: (NGCalendarDateRange *) _r
|
|
|
|
{
|
2007-01-31 18:11:29 +01:00
|
|
|
NSMutableArray *ranges;
|
2008-12-19 23:14:02 +01:00
|
|
|
NSCalendarDate *firStart, *startDate, *endDate, *currentStartDate, *currentEndDate;
|
|
|
|
long i;
|
2008-12-10 19:40:20 +01:00
|
|
|
unsigned interval;
|
2007-01-31 18:11:29 +01:00
|
|
|
|
2008-12-10 19:40:20 +01:00
|
|
|
firStart = [firstRange startDate];
|
2008-12-19 23:14:02 +01:00
|
|
|
startDate = [_r startDate];
|
|
|
|
endDate = [_r endDate];
|
|
|
|
|
|
|
|
if ([endDate compare: firStart] == NSOrderedAscending)
|
|
|
|
// Range ends before first occurrence
|
2007-01-31 18:11:29 +01:00
|
|
|
return nil;
|
2008-12-10 19:40:20 +01:00
|
|
|
|
|
|
|
interval = [rrule repeatInterval];
|
|
|
|
|
2008-12-19 23:14:02 +01:00
|
|
|
// If rule is bound, check the bounds
|
2008-12-10 19:40:20 +01:00
|
|
|
if (![rrule isInfinite])
|
|
|
|
{
|
2008-12-19 23:14:02 +01:00
|
|
|
NSCalendarDate *until, *lastDate;
|
2008-12-10 19:40:20 +01:00
|
|
|
|
|
|
|
until = [rrule untilDate];
|
|
|
|
if (until)
|
2008-12-19 23:14:02 +01:00
|
|
|
lastDate = until;
|
|
|
|
else
|
|
|
|
lastDate = [firStart dateByAddingYears: 0 months: 0
|
|
|
|
days: (interval
|
|
|
|
* ([rrule repeatCount] - 1))];
|
|
|
|
|
|
|
|
if ([lastDate compare: startDate] == NSOrderedAscending)
|
|
|
|
// Range starts after last occurrence
|
|
|
|
return nil;
|
|
|
|
|
|
|
|
if ([lastDate compare: endDate] == NSOrderedAscending)
|
|
|
|
// Range ends after last occurence; adjust end date
|
|
|
|
endDate = lastDate;
|
2007-01-31 18:11:29 +01:00
|
|
|
}
|
|
|
|
|
2008-12-19 23:14:02 +01:00
|
|
|
currentStartDate = [firStart copy];
|
2009-03-24 14:45:46 +01:00
|
|
|
[currentStartDate autorelease];
|
2008-12-19 23:14:02 +01:00
|
|
|
ranges = [NSMutableArray array];
|
|
|
|
i = 1;
|
|
|
|
|
|
|
|
while ([currentStartDate compare: endDate] == NSOrderedAscending ||
|
|
|
|
[currentStartDate compare: endDate] == NSOrderedSame)
|
2008-12-10 19:40:20 +01:00
|
|
|
{
|
2008-12-19 23:14:02 +01:00
|
|
|
if ([startDate compare: currentStartDate] == NSOrderedAscending ||
|
|
|
|
[startDate compare: currentStartDate] == NSOrderedSame)
|
2008-12-10 19:40:20 +01:00
|
|
|
{
|
2008-12-19 23:14:02 +01:00
|
|
|
BOOL wrongDay = NO;
|
|
|
|
unsigned int mask;
|
|
|
|
NGCalendarDateRange *r;
|
|
|
|
|
|
|
|
if ([rrule byDayMask])
|
|
|
|
{
|
|
|
|
mask = ([currentStartDate dayOfWeek]
|
|
|
|
? (unsigned int) 1 << ([currentStartDate dayOfWeek])
|
|
|
|
: iCalWeekDaySunday);
|
|
|
|
if (([rrule byDayMask] & mask) != mask)
|
|
|
|
wrongDay = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wrongDay == NO)
|
2008-12-10 19:40:20 +01:00
|
|
|
{
|
2008-12-19 23:14:02 +01:00
|
|
|
currentEndDate = [currentStartDate addTimeInterval: [firstRange duration]];
|
|
|
|
r = [NGCalendarDateRange calendarDateRangeWithStartDate: currentStartDate
|
|
|
|
endDate: currentEndDate];
|
|
|
|
if ([_r containsDateRange: r])
|
|
|
|
[ranges addObject: r];
|
2008-12-10 19:40:20 +01:00
|
|
|
}
|
2008-01-14 14:57:28 +01:00
|
|
|
}
|
2008-12-19 23:14:02 +01:00
|
|
|
|
|
|
|
currentStartDate = [firStart dateByAddingYears: 0 months: 0
|
|
|
|
days: (interval * i)];
|
|
|
|
i++;
|
2007-01-31 18:11:29 +01:00
|
|
|
}
|
|
|
|
return ranges;
|
|
|
|
}
|
|
|
|
|
2008-12-10 19:57:55 +01:00
|
|
|
- (NSCalendarDate *) lastInstanceStartDate
|
2008-12-10 19:40:20 +01:00
|
|
|
{
|
2008-12-10 19:57:55 +01:00
|
|
|
NSCalendarDate *firStart, *lastInstanceStartDate;
|
|
|
|
|
2008-12-10 19:40:20 +01:00
|
|
|
if ([rrule repeatCount] > 0)
|
|
|
|
{
|
|
|
|
firStart = [firstRange startDate];
|
2008-12-10 19:57:55 +01:00
|
|
|
|
|
|
|
lastInstanceStartDate = [firStart dateByAddingYears: 0 months: 0
|
|
|
|
days: ([rrule repeatInterval]
|
2008-12-12 19:43:14 +01:00
|
|
|
* ([rrule repeatCount] - 1))];
|
2008-12-10 19:40:20 +01:00
|
|
|
}
|
2008-12-10 19:57:55 +01:00
|
|
|
else
|
|
|
|
lastInstanceStartDate = [super lastInstanceStartDate];
|
|
|
|
|
|
|
|
return lastInstanceStartDate;
|
2007-01-31 18:11:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@end /* iCalDailyRecurrenceCalculator */
|