2007-01-31 18:11:29 +01:00
|
|
|
/*
|
|
|
|
Copyright (C) 2004-2005 SKYRIX Software AG
|
2008-12-10 19:57:55 +01:00
|
|
|
|
2007-01-31 18:11:29 +01:00
|
|
|
This file is part of SOPE.
|
2008-12-10 19:57:55 +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:57:55 +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:57:55 +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:57:55 +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:57:55 +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 <NGExtensions/NSCalendarDate+misc.h>
|
|
|
|
|
|
|
|
#import "iCalRecurrenceCalculator.h"
|
2007-01-31 18:11:29 +01:00
|
|
|
|
|
|
|
@interface iCalWeeklyRecurrenceCalculator : iCalRecurrenceCalculator
|
|
|
|
@end
|
|
|
|
|
2007-06-07 18:17:51 +02:00
|
|
|
#import <NGExtensions/NGCalendarDateRange.h>
|
|
|
|
#import "iCalRecurrenceRule.h"
|
|
|
|
#import "NSCalendarDate+ICal.h"
|
2007-01-31 18:11:29 +01:00
|
|
|
|
|
|
|
@interface iCalRecurrenceCalculator (PrivateAPI)
|
|
|
|
|
2008-03-26 21:30:53 +01:00
|
|
|
- (NSCalendarDate *) lastInstanceStartDate;
|
2007-01-31 18:11:29 +01:00
|
|
|
|
2008-12-10 19:57:55 +01:00
|
|
|
- (unsigned) offsetFromSundayForJulianNumber: (long) _jn;
|
|
|
|
- (unsigned) offsetFromSundayForWeekDay: (iCalWeekDay) _weekDay;
|
2008-03-26 21:30:53 +01:00
|
|
|
- (unsigned) offsetFromSundayForCurrentWeekStart;
|
2008-12-10 19:57:55 +01:00
|
|
|
|
|
|
|
- (iCalWeekDay) weekDayForJulianNumber: (long) _jn;
|
2007-01-31 18:11:29 +01:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
/*
|
2008-12-10 19:57:55 +01:00
|
|
|
TODO: If BYDAY is specified, lastInstanceStartDate and recurrences will
|
|
|
|
differ significantly!
|
2007-01-31 18:11:29 +01:00
|
|
|
*/
|
|
|
|
@implementation iCalWeeklyRecurrenceCalculator
|
|
|
|
|
2008-12-19 23:14:02 +01:00
|
|
|
- (NSArray *) recurrenceRangesWithinCalendarDateRange: (NGCalendarDateRange *) _r
|
2008-03-26 21:30:53 +01:00
|
|
|
{
|
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:57:55 +01:00
|
|
|
unsigned interval, byDayMask;
|
2007-01-31 18:11:29 +01:00
|
|
|
|
2008-12-10 19:57:55 +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-19 23:14:02 +01:00
|
|
|
|
2008-12-10 19:57:55 +01:00
|
|
|
interval = [rrule repeatInterval];
|
|
|
|
|
2008-12-19 23:14:02 +01:00
|
|
|
// If rule is bound, check the bounds
|
2008-12-10 19:57:55 +01:00
|
|
|
if (![rrule isInfinite])
|
|
|
|
{
|
2008-12-19 23:14:02 +01:00
|
|
|
NSCalendarDate *until, *lastDate;
|
2008-12-10 19:57:55 +01:00
|
|
|
|
|
|
|
until = [rrule untilDate];
|
2008-12-19 23:14:02 +01:00
|
|
|
if (until)
|
|
|
|
lastDate = until;
|
2008-12-10 19:57:55 +01:00
|
|
|
else
|
2008-12-19 23:14:02 +01:00
|
|
|
lastDate = [firStart dateByAddingYears: 0 months: 0
|
|
|
|
days: (interval
|
|
|
|
* ([rrule repeatCount] - 1) * 7)];
|
|
|
|
|
|
|
|
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-10 19:57:55 +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];
|
2008-12-10 19:57:55 +01:00
|
|
|
byDayMask = [rrule byDayMask];
|
2008-12-19 23:14:02 +01:00
|
|
|
i = 1;
|
2008-12-10 19:57:55 +01:00
|
|
|
if (!byDayMask)
|
|
|
|
{
|
2008-12-19 23:14:02 +01:00
|
|
|
while ([currentStartDate compare: endDate] == NSOrderedAscending ||
|
|
|
|
[currentStartDate compare: endDate] == NSOrderedSame)
|
2008-12-10 19:57:55 +01:00
|
|
|
{
|
2008-12-19 23:14:02 +01:00
|
|
|
if ([startDate compare: currentStartDate] == NSOrderedAscending ||
|
|
|
|
[startDate compare: currentStartDate] == NSOrderedSame)
|
2008-12-10 19:57:55 +01:00
|
|
|
{
|
2008-12-19 23:14:02 +01:00
|
|
|
NGCalendarDateRange *r;
|
|
|
|
|
|
|
|
currentEndDate = [currentStartDate addTimeInterval: [firstRange duration]];
|
|
|
|
r = [NGCalendarDateRange calendarDateRangeWithStartDate: currentStartDate
|
|
|
|
endDate: currentEndDate];
|
|
|
|
if ([_r containsDateRange: r])
|
|
|
|
[ranges addObject: r];
|
|
|
|
}
|
|
|
|
currentStartDate = [firStart dateByAddingYears: 0
|
|
|
|
months: 0
|
|
|
|
days: (interval * i * 7)];
|
|
|
|
i++;
|
2008-12-10 19:57:55 +01:00
|
|
|
}
|
2007-01-31 18:11:29 +01:00
|
|
|
}
|
2008-12-19 23:14:02 +01:00
|
|
|
else
|
2008-12-10 19:57:55 +01:00
|
|
|
{
|
2008-12-19 23:14:02 +01:00
|
|
|
unsigned dayOfWeek;
|
|
|
|
NGCalendarDateRange *r;
|
|
|
|
|
|
|
|
while ([currentStartDate compare: endDate] == NSOrderedAscending ||
|
|
|
|
[currentStartDate compare: endDate] == NSOrderedSame)
|
2008-12-10 19:57:55 +01:00
|
|
|
{
|
2008-12-19 23:14:02 +01:00
|
|
|
if ([startDate compare: currentStartDate] == NSOrderedAscending ||
|
|
|
|
[startDate compare: currentStartDate] == NSOrderedSame)
|
2008-12-10 19:57:55 +01:00
|
|
|
{
|
2009-04-01 15:49:24 +02:00
|
|
|
int days, week;
|
2008-12-19 23:14:02 +01:00
|
|
|
|
2009-04-01 15:49:24 +02:00
|
|
|
[currentStartDate years:NULL months:NULL days:(int *)&days hours:NULL
|
2008-12-19 23:14:02 +01:00
|
|
|
minutes:NULL seconds:NULL sinceDate:firStart];
|
|
|
|
week = days / 7;
|
|
|
|
|
|
|
|
if ((week % interval) == 0)
|
2008-12-10 19:57:55 +01:00
|
|
|
{
|
2008-12-19 23:14:02 +01:00
|
|
|
// Date is in the proper week with respect to the
|
|
|
|
// week interval
|
2008-12-10 19:57:55 +01:00
|
|
|
BOOL isRecurrence = NO;
|
2008-12-19 23:14:02 +01:00
|
|
|
|
|
|
|
if ([currentStartDate compare: firStart] == NSOrderedSame)
|
|
|
|
// Always add the event of the start date of
|
|
|
|
// the recurring event.
|
|
|
|
isRecurrence = YES;
|
|
|
|
else
|
2008-12-10 19:57:55 +01:00
|
|
|
{
|
2008-12-19 23:14:02 +01:00
|
|
|
// Only consider events that matches the day mask.
|
|
|
|
dayOfWeek = ([currentStartDate dayOfWeek]
|
|
|
|
? (unsigned int) 1 << [currentStartDate dayOfWeek]
|
|
|
|
: iCalWeekDaySunday);
|
|
|
|
if (dayOfWeek & [rrule byDayMask])
|
|
|
|
isRecurrence = YES;
|
2008-12-10 19:57:55 +01:00
|
|
|
}
|
2008-12-19 23:14:02 +01:00
|
|
|
if (isRecurrence)
|
2008-12-10 19:57:55 +01:00
|
|
|
{
|
2008-12-19 23:14:02 +01:00
|
|
|
currentEndDate = [currentStartDate addTimeInterval: [firstRange duration]];
|
|
|
|
r = [NGCalendarDateRange calendarDateRangeWithStartDate: currentStartDate
|
|
|
|
endDate: currentEndDate];
|
2008-12-10 19:57:55 +01:00
|
|
|
if ([_r containsDateRange: r])
|
|
|
|
[ranges addObject: r];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-12-19 23:14:02 +01:00
|
|
|
currentStartDate = [currentStartDate dateByAddingYears: 0
|
|
|
|
months: 0
|
|
|
|
days: 1];
|
2008-12-10 19:57:55 +01:00
|
|
|
}
|
2007-01-31 18:11:29 +01:00
|
|
|
}
|
2008-12-19 23:14:02 +01:00
|
|
|
|
2007-01-31 18:11:29 +01:00
|
|
|
return ranges;
|
|
|
|
}
|
|
|
|
|
2008-12-10 20:02:50 +01:00
|
|
|
- (NSCalendarDate *) lastInstanceStartDate
|
2008-12-10 19:57:55 +01:00
|
|
|
{
|
2008-12-10 20:02:50 +01:00
|
|
|
NSCalendarDate *firStart, *lastInstanceStartDate;
|
|
|
|
|
2008-12-10 19:57:55 +01:00
|
|
|
if ([rrule repeatCount] > 0)
|
|
|
|
{
|
|
|
|
firStart = [firstRange startDate];
|
2008-12-10 20:02:50 +01:00
|
|
|
|
|
|
|
lastInstanceStartDate = [firStart dateByAddingYears: 0 months: 0
|
|
|
|
days: (7 * [rrule repeatInterval]
|
2008-12-12 19:43:14 +01:00
|
|
|
* ([rrule repeatCount] - 1))];
|
2008-12-10 19:57:55 +01:00
|
|
|
}
|
2008-12-10 20:02:50 +01:00
|
|
|
else
|
|
|
|
lastInstanceStartDate = [super lastInstanceStartDate];
|
|
|
|
|
|
|
|
return lastInstanceStartDate;
|
2007-01-31 18:11:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@end /* iCalWeeklyRecurrenceCalculator */
|