2007-01-31 18:11:29 +01:00
|
|
|
/*
|
|
|
|
Copyright (C) 2004-2005 SKYRIX Software AG
|
2010-04-19 23:05:35 +02:00
|
|
|
Copyright (C) 2006-2010 Inverse inc.
|
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
|
|
|
|
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
|
|
|
|
2010-04-21 16:44:42 +02:00
|
|
|
@interface iCalYearlyRecurrenceCalculator : iCalRecurrenceCalculator
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface iCalMonthlyRecurrenceCalculator : iCalRecurrenceCalculator
|
|
|
|
@end
|
|
|
|
|
2008-12-10 19:57:55 +01:00
|
|
|
@interface iCalRecurrenceCalculator (PrivateAPI)
|
|
|
|
- (NSCalendarDate *) lastInstanceStartDate;
|
2007-01-31 18:11:29 +01:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation iCalYearlyRecurrenceCalculator
|
|
|
|
|
2008-12-10 19:57:55 +01:00
|
|
|
- (NSArray *)
|
|
|
|
recurrenceRangesWithinCalendarDateRange: (NGCalendarDateRange *) _r
|
|
|
|
{
|
2007-01-31 18:11:29 +01:00
|
|
|
NSMutableArray *ranges;
|
2010-04-19 23:05:35 +02:00
|
|
|
NSArray *byMonth;
|
|
|
|
NSCalendarDate *firStart, *lastDate, *rStart, *rEnd, *until, *referenceDate;
|
|
|
|
iCalMonthlyRecurrenceCalculator *monthlyCalc;
|
|
|
|
unsigned j, yearIdxInRange, numberOfYearsInRange, count, interval, monthDiff;
|
|
|
|
int diff, repeatCount, currentMonth;
|
2008-12-10 19:57:55 +01:00
|
|
|
|
|
|
|
firStart = [firstRange startDate];
|
|
|
|
rStart = [_r startDate];
|
|
|
|
rEnd = [_r endDate];
|
|
|
|
interval = [rrule repeatInterval];
|
2010-04-19 23:05:35 +02:00
|
|
|
byMonth = [rrule byMonth];
|
|
|
|
diff = 0;
|
|
|
|
repeatCount = 0;
|
|
|
|
count = 0;
|
|
|
|
referenceDate = nil;
|
2008-12-10 19:57:55 +01:00
|
|
|
|
2010-04-19 23:05:35 +02:00
|
|
|
if ([rEnd compare: firStart] == NSOrderedAscending)
|
|
|
|
// Range ends before first occurrence
|
|
|
|
return nil;
|
|
|
|
|
|
|
|
// If rule is bound, check the bounds
|
|
|
|
if (![rrule isInfinite])
|
|
|
|
{
|
|
|
|
lastDate = nil;
|
|
|
|
until = [rrule untilDate];
|
|
|
|
repeatCount = [rrule repeatCount];
|
|
|
|
|
|
|
|
if (until)
|
|
|
|
{
|
|
|
|
lastDate = until;
|
|
|
|
}
|
|
|
|
if (repeatCount > 0)
|
|
|
|
{
|
|
|
|
if (lastDate == nil && ![rrule hasByMask])
|
|
|
|
// When there's no BYxxx mask, we can find the date of the last
|
|
|
|
// occurrence.
|
|
|
|
lastDate = [firStart dateByAddingYears: (interval * (repeatCount - 1))
|
|
|
|
months: 0
|
|
|
|
days: 0];
|
|
|
|
referenceDate = firStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lastDate != nil)
|
|
|
|
{
|
|
|
|
if ([lastDate compare: rStart] == NSOrderedAscending)
|
|
|
|
// Range starts after last occurrence
|
|
|
|
return nil;
|
2011-11-03 12:54:03 +01:00
|
|
|
if ([lastDate compare: rEnd] == NSOrderedDescending)
|
2010-04-19 23:05:35 +02:00
|
|
|
// Range ends after last occurence; adjust end date
|
|
|
|
rEnd = lastDate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (referenceDate == nil)
|
2008-12-10 19:57:55 +01:00
|
|
|
{
|
2010-04-19 23:05:35 +02:00
|
|
|
diff = [firStart yearsBetweenDate: rStart];
|
|
|
|
if ((diff != 0) && [rStart compare: firStart] == NSOrderedAscending)
|
|
|
|
diff = -diff;
|
|
|
|
referenceDate = rStart;
|
2008-12-10 19:57:55 +01:00
|
|
|
}
|
2007-01-31 18:11:29 +01:00
|
|
|
|
2010-04-19 23:05:35 +02:00
|
|
|
// Initialize array to return with an approximation of the number total
|
|
|
|
// number of possible matches, ie the number of years spawned by the period.
|
|
|
|
numberOfYearsInRange = [referenceDate yearsBetweenDate: rEnd] + 1;
|
|
|
|
ranges = [NSMutableArray arrayWithCapacity: numberOfYearsInRange];
|
|
|
|
|
|
|
|
if (byMonth)
|
2008-12-10 19:57:55 +01:00
|
|
|
{
|
2010-04-19 23:05:35 +02:00
|
|
|
/*
|
|
|
|
* WARNING/TODO : if there's no BYMONTH rule but there's a BYMONTHDAY
|
|
|
|
* rule we should implicitely define a BYMONTH rule by extracting the
|
|
|
|
* month from the DTSTART field. However, this kind of definition is
|
|
|
|
* uncommon.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Instantiate a MONTHLY calculator
|
2010-04-22 15:15:04 +02:00
|
|
|
// Fool the monthly calculator, otherwise it will verify the COUNT
|
|
|
|
// constraint and perform the calculation from the first occurence of
|
|
|
|
// the recurrence. This calculation is performed by the current method.
|
|
|
|
// The interval must be ignored as well since it refers to the years.
|
|
|
|
[rrule setRepeatCount: 0];
|
|
|
|
[rrule setInterval: @"1"];
|
2010-04-19 23:05:35 +02:00
|
|
|
|
|
|
|
// There's a bug in GNUstep in [NSCalendarDate dateByAddingYears:months:days:]
|
|
|
|
// that causes errors when adding subsequently a month. For this reason,
|
|
|
|
// we set the day of the reference date to 1.
|
|
|
|
referenceDate = [NSCalendarDate dateWithYear: [referenceDate yearOfCommonEra]
|
|
|
|
month: [referenceDate monthOfYear]
|
|
|
|
day: 1
|
|
|
|
hour: [referenceDate hourOfDay]
|
|
|
|
minute: [referenceDate minuteOfHour]
|
|
|
|
second: 0
|
|
|
|
timeZone: [referenceDate timeZone]];
|
|
|
|
|
|
|
|
// If the BYMONTH constraints exclude the month of the event DTSTART, we
|
|
|
|
// add the corresponding range manually if it is included in the period.
|
|
|
|
// Otherwise, it will be included by the monthly calculator in the loop
|
|
|
|
// bellow.
|
|
|
|
int month = [firStart monthOfYear];
|
|
|
|
if (![byMonth containsObject: [NSString stringWithFormat: @"%i", month]])
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
if ([_r containsDateRange: firstRange])
|
|
|
|
{
|
|
|
|
[ranges addObject: firstRange];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
monthDiff = 0;
|
|
|
|
currentMonth = [referenceDate monthOfYear];
|
|
|
|
for (yearIdxInRange = 0 ; yearIdxInRange < numberOfYearsInRange; yearIdxInRange++)
|
|
|
|
{
|
2011-04-05 15:33:48 +02:00
|
|
|
int k, test;
|
2010-04-19 23:05:35 +02:00
|
|
|
|
|
|
|
test = diff + yearIdxInRange;
|
2008-12-10 19:57:55 +01:00
|
|
|
if ((test >= 0) && (test % interval) == 0)
|
|
|
|
{
|
2010-04-19 23:05:35 +02:00
|
|
|
if (byMonth)
|
|
|
|
{
|
2011-04-05 15:33:48 +02:00
|
|
|
monthlyCalc = [[iCalMonthlyRecurrenceCalculator alloc]
|
|
|
|
initWithRecurrenceRule: rrule
|
|
|
|
firstInstanceCalendarDateRange: firstRange];
|
|
|
|
[monthlyCalc autorelease];
|
|
|
|
|
2010-04-19 23:05:35 +02:00
|
|
|
// When there's a BYMONTH constraint, evaluate each month of the constraint using
|
|
|
|
// the monthly calculator.
|
|
|
|
for (j = 0; currentMonth < 13 && j <= 12; j++, currentMonth++, monthDiff++)
|
|
|
|
{
|
|
|
|
if ([byMonth containsObject: [NSString stringWithFormat: @"%i", currentMonth]])
|
|
|
|
{
|
|
|
|
NGCalendarDateRange *rangeForMonth;
|
|
|
|
NSArray *rangesInMonth;
|
|
|
|
|
|
|
|
rStart = [referenceDate dateByAddingYears: 0
|
|
|
|
months: monthDiff
|
|
|
|
days: 0];
|
|
|
|
rEnd = [rStart dateByAddingYears: 0
|
|
|
|
months: 0
|
2010-04-22 15:15:04 +02:00
|
|
|
days: [rStart numberOfDaysInMonth]];
|
2010-04-19 23:05:35 +02:00
|
|
|
rangeForMonth = [NGCalendarDateRange calendarDateRangeWithStartDate: rStart
|
|
|
|
endDate: rEnd];
|
|
|
|
rangesInMonth = [monthlyCalc recurrenceRangesWithinCalendarDateRange: rangeForMonth];
|
|
|
|
|
|
|
|
for (k = 0; k < [rangesInMonth count] && (repeatCount == 0 || count < repeatCount); k++) {
|
2011-11-03 12:54:03 +01:00
|
|
|
//NSLog(@"*** YEARLY found %@ (count = %i)", [[rangesInMonth objectAtIndex: k] startDate], count);
|
2010-04-19 23:05:35 +02:00
|
|
|
count++;
|
|
|
|
if ([_r containsDateRange: [rangesInMonth objectAtIndex: k]])
|
|
|
|
{
|
|
|
|
[ranges addObject: [rangesInMonth objectAtIndex: k]];
|
2011-11-03 12:54:03 +01:00
|
|
|
//NSLog(@"*** YEARLY adding %@ (count = %i)", [[rangesInMonth objectAtIndex: k] startDate], count);
|
2010-04-19 23:05:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Done with the current year; start the next iteration from January
|
|
|
|
currentMonth = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// No BYxxx mask
|
|
|
|
NSCalendarDate *start, *end;
|
|
|
|
NGCalendarDateRange *r;
|
|
|
|
|
|
|
|
start = [firStart dateByAddingYears: diff + yearIdxInRange
|
|
|
|
months: 0
|
|
|
|
days: 0];
|
|
|
|
[start setTimeZone: [firStart timeZone]];
|
|
|
|
end = [start addTimeInterval: [firstRange duration]];
|
|
|
|
r = [NGCalendarDateRange calendarDateRangeWithStartDate: start
|
|
|
|
endDate: end];
|
|
|
|
if ([_r containsDateRange: r] && (repeatCount == 0 || count < repeatCount))
|
|
|
|
{
|
|
|
|
[ranges addObject: r];
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
2008-12-10 19:57:55 +01:00
|
|
|
}
|
2010-04-22 15:15:04 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Year was skipped, added 12 months to the counter
|
|
|
|
monthDiff += 12;
|
|
|
|
}
|
2007-01-31 18:11:29 +01:00
|
|
|
}
|
2010-04-19 23:05:35 +02:00
|
|
|
|
2010-04-22 15:15:04 +02:00
|
|
|
if (byMonth)
|
|
|
|
{
|
|
|
|
// Restore the repeat count and interval
|
2010-04-22 16:49:46 +02:00
|
|
|
if (repeatCount > 0)
|
|
|
|
[rrule setRepeatCount: repeatCount];
|
2010-04-22 15:15:04 +02:00
|
|
|
[rrule setRepeatInterval: interval];
|
|
|
|
}
|
2007-01-31 18:11:29 +01:00
|
|
|
return ranges;
|
|
|
|
}
|
|
|
|
|
2008-12-10 19:57:55 +01:00
|
|
|
- (NSCalendarDate *) lastInstanceStartDate
|
|
|
|
{
|
2008-12-10 20:02:50 +01:00
|
|
|
NSCalendarDate *firStart, *lastInstanceStartDate;
|
2010-04-19 23:05:35 +02:00
|
|
|
NGCalendarDateRange *r;
|
|
|
|
NSArray *instances;
|
2008-12-10 20:02:50 +01:00
|
|
|
|
2010-04-19 23:05:35 +02:00
|
|
|
lastInstanceStartDate = nil;
|
|
|
|
if ([rrule repeatCount] > 0)
|
2008-12-10 19:57:55 +01:00
|
|
|
{
|
2010-04-20 22:29:59 +02:00
|
|
|
firStart = [firstRange startDate];
|
2010-04-19 23:05:35 +02:00
|
|
|
if ([rrule hasByMask])
|
|
|
|
{
|
|
|
|
// Must perform the complete calculation
|
|
|
|
r = [NGCalendarDateRange calendarDateRangeWithStartDate: firStart
|
|
|
|
endDate: [NSCalendarDate distantFuture]];
|
|
|
|
instances = [self recurrenceRangesWithinCalendarDateRange: r];
|
|
|
|
if ([instances count])
|
|
|
|
lastInstanceStartDate = [(NGCalendarDateRange *)[instances lastObject] startDate];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// No BYxxx mask
|
|
|
|
lastInstanceStartDate = [firStart dateByAddingYears: ([rrule repeatInterval]
|
|
|
|
* ([rrule repeatCount] - 1))
|
|
|
|
months: 0
|
|
|
|
days: 0];
|
|
|
|
}
|
2008-12-10 19:57:55 +01:00
|
|
|
}
|
2008-12-10 20:02:50 +01:00
|
|
|
else
|
|
|
|
lastInstanceStartDate = [super lastInstanceStartDate];
|
2010-04-19 23:05:35 +02:00
|
|
|
|
2008-12-10 20:02:50 +01:00
|
|
|
return lastInstanceStartDate;
|
2007-01-31 18:11:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@end /* iCalYearlyRecurrenceCalculator */
|