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 iCalYearlyRecurrenceCalculator : 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
|
|
|
|
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;
|
|
|
|
NSCalendarDate *firStart, *rStart, *rEnd, *until;
|
2008-12-10 19:57:55 +01:00
|
|
|
unsigned i, count, interval;
|
|
|
|
int diff;
|
|
|
|
|
|
|
|
firStart = [firstRange startDate];
|
|
|
|
rStart = [_r startDate];
|
|
|
|
rEnd = [_r endDate];
|
|
|
|
interval = [rrule repeatInterval];
|
|
|
|
until = [self lastInstanceStartDate];
|
|
|
|
|
|
|
|
if (until)
|
|
|
|
{
|
|
|
|
if ([until compare: rStart] == NSOrderedAscending)
|
|
|
|
return nil;
|
|
|
|
if ([until compare: rEnd] == NSOrderedDescending)
|
|
|
|
rEnd = until;
|
|
|
|
}
|
|
|
|
|
|
|
|
diff = [firStart yearsBetweenDate: rStart];
|
|
|
|
if ((diff != 0) && [rStart compare: firStart] == NSOrderedAscending)
|
2007-01-31 18:11:29 +01:00
|
|
|
diff = -diff;
|
|
|
|
|
2008-12-10 19:57:55 +01:00
|
|
|
count = [rStart yearsBetweenDate: rEnd] + 1;
|
|
|
|
ranges = [NSMutableArray arrayWithCapacity: count];
|
|
|
|
for (i = 0 ; i < count; i++)
|
|
|
|
{
|
|
|
|
int test;
|
2007-01-31 18:11:29 +01:00
|
|
|
|
2008-12-10 19:57:55 +01:00
|
|
|
test = diff + i;
|
|
|
|
if ((test >= 0) && (test % interval) == 0)
|
|
|
|
{
|
|
|
|
NSCalendarDate *start, *end;
|
|
|
|
NGCalendarDateRange *r;
|
|
|
|
|
|
|
|
start = [firStart dateByAddingYears: diff + i
|
|
|
|
months: 0
|
|
|
|
days: 0];
|
|
|
|
[start setTimeZone: [firStart timeZone]];
|
|
|
|
end = [start addTimeInterval: [firstRange duration]];
|
|
|
|
r = [NGCalendarDateRange calendarDateRangeWithStartDate: start
|
|
|
|
endDate: end];
|
|
|
|
if ([_r containsDateRange: r])
|
|
|
|
[ranges addObject: r];
|
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
if ([rrule repeatCount] > 0)
|
2008-12-10 19:57:55 +01:00
|
|
|
{
|
2008-12-10 20:02:50 +01:00
|
|
|
firStart = [firstRange startDate];
|
|
|
|
|
|
|
|
lastInstanceStartDate
|
|
|
|
= [firStart dateByAddingYears: ([rrule repeatInterval]
|
2008-12-12 19:43:14 +01:00
|
|
|
* ([rrule repeatCount] - 1))
|
2008-12-10 20:02:50 +01:00
|
|
|
months: 0
|
|
|
|
days: 0];
|
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 /* iCalYearlyRecurrenceCalculator */
|