mirror of
https://github.com/inverse-inc/sogo.git
synced 2026-04-14 01:38:51 +00:00
Monotone-Parent: 4e8caa9222644e6820515bbf312f74dbd372c2fc
Monotone-Revision: 25c172f93ef4ab6ada0aeb6ff4267e98aef8b719 Monotone-Author: wsourdeau@inverse.ca Monotone-Date: 2008-12-10T18:57:55 Monotone-Branch: ca.inverse.sogo
This commit is contained in:
@@ -1,20 +1,20 @@
|
||||
/*
|
||||
Copyright (C) 2004-2005 SKYRIX Software AG
|
||||
|
||||
|
||||
This file is part of SOPE.
|
||||
|
||||
|
||||
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.
|
||||
|
||||
|
||||
SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
||||
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
||||
License for more details.
|
||||
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with SOPE; see the file COPYING. If not, write to the
|
||||
License along with SOPE; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
|
||||
02111-1307, USA.
|
||||
*/
|
||||
@@ -34,17 +34,17 @@
|
||||
|
||||
- (NSCalendarDate *) lastInstanceStartDate;
|
||||
|
||||
- (unsigned) offsetFromSundayForJulianNumber:(long)_jn;
|
||||
- (unsigned) offsetFromSundayForWeekDay:(iCalWeekDay)_weekDay;
|
||||
- (unsigned) offsetFromSundayForJulianNumber: (long) _jn;
|
||||
- (unsigned) offsetFromSundayForWeekDay: (iCalWeekDay) _weekDay;
|
||||
- (unsigned) offsetFromSundayForCurrentWeekStart;
|
||||
|
||||
- (iCalWeekDay) weekDayForJulianNumber:(long)_jn;
|
||||
|
||||
- (iCalWeekDay) weekDayForJulianNumber: (long) _jn;
|
||||
|
||||
@end
|
||||
|
||||
/*
|
||||
TODO: If BYDAY is specified, lastInstanceStartDate and recurrences will
|
||||
differ significantly!
|
||||
TODO: If BYDAY is specified, lastInstanceStartDate and recurrences will
|
||||
differ significantly!
|
||||
*/
|
||||
@implementation iCalWeeklyRecurrenceCalculator
|
||||
|
||||
@@ -53,140 +53,156 @@
|
||||
{
|
||||
NSMutableArray *ranges;
|
||||
NSCalendarDate *firStart;
|
||||
long i, jnFirst, jnStart, jnEnd, startEndCount;
|
||||
unsigned interval, byDayMask;
|
||||
long i, jnFirst, jnStart, jnEnd, startEndCount;
|
||||
unsigned interval, byDayMask;
|
||||
|
||||
firStart = [self->firstRange startDate];
|
||||
jnFirst = [firStart julianNumber];
|
||||
jnEnd = [[_r endDate] julianNumber];
|
||||
|
||||
firStart = [firstRange startDate];
|
||||
jnFirst = [firStart julianNumber];
|
||||
jnEnd = [[_r endDate] julianNumber];
|
||||
|
||||
if (jnFirst > jnEnd)
|
||||
return nil;
|
||||
|
||||
jnStart = [[_r startDate] julianNumber];
|
||||
interval = [self->rrule repeatInterval];
|
||||
|
||||
|
||||
jnStart = [[_r startDate] julianNumber];
|
||||
interval = [rrule repeatInterval];
|
||||
|
||||
/* if rule is bound, check the bounds */
|
||||
if (![self->rrule isInfinite]) {
|
||||
NSCalendarDate *until;
|
||||
long jnRuleLast;
|
||||
|
||||
until = [self->rrule untilDate];
|
||||
if (until) {
|
||||
if ([until compare:[_r startDate]] == NSOrderedAscending)
|
||||
return nil;
|
||||
jnRuleLast = [until julianNumber];
|
||||
if (![rrule isInfinite])
|
||||
{
|
||||
NSCalendarDate *until;
|
||||
long jnRuleLast;
|
||||
|
||||
until = [rrule untilDate];
|
||||
if (until)
|
||||
{
|
||||
if ([until compare: [_r startDate]] == NSOrderedAscending)
|
||||
return nil;
|
||||
jnRuleLast = [until julianNumber];
|
||||
}
|
||||
else
|
||||
{
|
||||
jnRuleLast = (interval * [rrule repeatCount] * 7)
|
||||
+ jnFirst;
|
||||
if (jnRuleLast < jnStart)
|
||||
return nil;
|
||||
}
|
||||
/* jnStart < jnRuleLast < jnEnd ? */
|
||||
if (jnEnd > jnRuleLast)
|
||||
jnEnd = jnRuleLast;
|
||||
}
|
||||
else {
|
||||
jnRuleLast = (interval * [self->rrule repeatCount] * 7)
|
||||
+ jnFirst;
|
||||
if (jnRuleLast < jnStart)
|
||||
return nil;
|
||||
}
|
||||
/* jnStart < jnRuleLast < jnEnd ? */
|
||||
if (jnEnd > jnRuleLast)
|
||||
jnEnd = jnRuleLast;
|
||||
}
|
||||
|
||||
|
||||
startEndCount = (jnEnd - jnStart) + 1;
|
||||
ranges = [NSMutableArray arrayWithCapacity:startEndCount];
|
||||
byDayMask = [self->rrule byDayMask];
|
||||
if (!byDayMask) {
|
||||
for (i = 0 ; i < startEndCount; i++) {
|
||||
long jnCurrent;
|
||||
|
||||
jnCurrent = jnStart + i;
|
||||
if (jnCurrent >= jnFirst) {
|
||||
long jnDiff;
|
||||
|
||||
jnDiff = jnCurrent - jnFirst; /* difference in days */
|
||||
if ((jnDiff % (interval * 7)) == 0) {
|
||||
NSCalendarDate *start, *end;
|
||||
NGCalendarDateRange *r;
|
||||
|
||||
start = [NSCalendarDate dateForJulianNumber:jnCurrent];
|
||||
[start setTimeZone:[firStart timeZone]];
|
||||
start = [start hour: [firStart hourOfDay]
|
||||
minute:[firStart minuteOfHour]
|
||||
second:[firStart secondOfMinute]];
|
||||
end = [start addTimeInterval:[self->firstRange duration]];
|
||||
r = [NGCalendarDateRange calendarDateRangeWithStartDate:start
|
||||
endDate:end];
|
||||
if ([_r containsDateRange:r])
|
||||
[ranges addObject:r];
|
||||
}
|
||||
}
|
||||
ranges = [NSMutableArray arrayWithCapacity: startEndCount];
|
||||
byDayMask = [rrule byDayMask];
|
||||
if (!byDayMask)
|
||||
{
|
||||
for (i = 0 ; i < startEndCount; i++)
|
||||
{
|
||||
long jnCurrent;
|
||||
|
||||
jnCurrent = jnStart + i;
|
||||
if (jnCurrent >= jnFirst)
|
||||
{
|
||||
long jnDiff;
|
||||
|
||||
jnDiff = jnCurrent - jnFirst; /* difference in days */
|
||||
if ((jnDiff % (interval * 7)) == 0)
|
||||
{
|
||||
NSCalendarDate *start, *end;
|
||||
NGCalendarDateRange *r;
|
||||
|
||||
start = [NSCalendarDate dateForJulianNumber: jnCurrent];
|
||||
[start setTimeZone: [firStart timeZone]];
|
||||
start = [start hour: [firStart hourOfDay]
|
||||
minute: [firStart minuteOfHour]
|
||||
second: [firStart secondOfMinute]];
|
||||
end = [start addTimeInterval: [firstRange duration]];
|
||||
r = [NGCalendarDateRange calendarDateRangeWithStartDate: start
|
||||
endDate: end];
|
||||
if ([_r containsDateRange: r])
|
||||
[ranges addObject: r];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
long jnFirstWeekStart, weekStartOffset;
|
||||
else
|
||||
{
|
||||
long jnFirstWeekStart, weekStartOffset;
|
||||
|
||||
/* calculate jnFirst's week start - this depends on our setting of week
|
||||
start */
|
||||
weekStartOffset = [self offsetFromSundayForJulianNumber:jnFirst] -
|
||||
[self offsetFromSundayForCurrentWeekStart];
|
||||
/* calculate jnFirst's week start - this depends on our setting of week
|
||||
start */
|
||||
weekStartOffset = [self offsetFromSundayForJulianNumber: jnFirst] -
|
||||
[self offsetFromSundayForCurrentWeekStart];
|
||||
|
||||
jnFirstWeekStart = jnFirst - weekStartOffset;
|
||||
jnFirstWeekStart = jnFirst - weekStartOffset;
|
||||
|
||||
for (i = 0 ; i < startEndCount; i++) {
|
||||
long jnCurrent;
|
||||
for (i = 0 ; i < startEndCount; i++)
|
||||
{
|
||||
long jnCurrent;
|
||||
|
||||
jnCurrent = jnStart + i;
|
||||
if (jnCurrent >= jnFirst) {
|
||||
long jnDiff;
|
||||
|
||||
/* we need to calculate a difference in weeks */
|
||||
jnDiff = (jnCurrent - jnFirstWeekStart) % 7;
|
||||
if ((jnDiff % interval) == 0) {
|
||||
BOOL isRecurrence = NO;
|
||||
|
||||
if (jnCurrent == jnFirst) {
|
||||
isRecurrence = YES;
|
||||
}
|
||||
else {
|
||||
iCalWeekDay weekDay;
|
||||
jnCurrent = jnStart + i;
|
||||
if (jnCurrent >= jnFirst)
|
||||
{
|
||||
long jnDiff;
|
||||
|
||||
/* we need to calculate a difference in weeks */
|
||||
jnDiff = (jnCurrent - jnFirstWeekStart) % 7;
|
||||
if ((jnDiff % interval) == 0)
|
||||
{
|
||||
BOOL isRecurrence = NO;
|
||||
|
||||
if (jnCurrent == jnFirst)
|
||||
{
|
||||
isRecurrence = YES;
|
||||
}
|
||||
else
|
||||
{
|
||||
iCalWeekDay weekDay;
|
||||
|
||||
weekDay = [self weekDayForJulianNumber:jnCurrent];
|
||||
isRecurrence = (weekDay & [self->rrule byDayMask]) ? YES : NO;
|
||||
}
|
||||
if (isRecurrence) {
|
||||
NSCalendarDate *start, *end;
|
||||
NGCalendarDateRange *r;
|
||||
|
||||
start = [NSCalendarDate dateForJulianNumber:jnCurrent];
|
||||
[start setTimeZone:[firStart timeZone]];
|
||||
start = [start hour: [firStart hourOfDay]
|
||||
minute:[firStart minuteOfHour]
|
||||
second:[firStart secondOfMinute]];
|
||||
end = [start addTimeInterval:[self->firstRange duration]];
|
||||
r = [NGCalendarDateRange calendarDateRangeWithStartDate:start
|
||||
endDate:end];
|
||||
if ([_r containsDateRange:r])
|
||||
[ranges addObject:r];
|
||||
}
|
||||
}
|
||||
}
|
||||
weekDay = [self weekDayForJulianNumber: jnCurrent];
|
||||
isRecurrence = (weekDay & [rrule byDayMask]) ? YES : NO;
|
||||
}
|
||||
if (isRecurrence)
|
||||
{
|
||||
NSCalendarDate *start, *end;
|
||||
NGCalendarDateRange *r;
|
||||
|
||||
start = [NSCalendarDate dateForJulianNumber: jnCurrent];
|
||||
[start setTimeZone: [firStart timeZone]];
|
||||
start = [start hour: [firStart hourOfDay]
|
||||
minute: [firStart minuteOfHour]
|
||||
second: [firStart secondOfMinute]];
|
||||
end = [start addTimeInterval: [firstRange duration]];
|
||||
r = [NGCalendarDateRange calendarDateRangeWithStartDate: start
|
||||
endDate: end];
|
||||
if ([_r containsDateRange: r])
|
||||
[ranges addObject: r];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return ranges;
|
||||
}
|
||||
|
||||
- (NSCalendarDate *)lastInstanceStartDate {
|
||||
if ([self->rrule repeatCount] > 0) {
|
||||
long jnFirst, jnRuleLast;
|
||||
NSCalendarDate *firStart, *until;
|
||||
|
||||
firStart = [self->firstRange startDate];
|
||||
jnFirst = [firStart julianNumber];
|
||||
jnRuleLast = ([self->rrule repeatInterval] *
|
||||
[self->rrule repeatCount] * 7) +
|
||||
jnFirst;
|
||||
until = [NSCalendarDate dateForJulianNumber:jnRuleLast];
|
||||
until = [until hour: [firStart hourOfDay]
|
||||
minute:[firStart minuteOfHour]
|
||||
second:[firStart secondOfMinute]];
|
||||
return until;
|
||||
}
|
||||
- (NSCalendarDate *) lastInstanceStartDate
|
||||
{
|
||||
if ([rrule repeatCount] > 0)
|
||||
{
|
||||
long jnFirst, jnRuleLast;
|
||||
NSCalendarDate *firStart, *until;
|
||||
|
||||
firStart = [firstRange startDate];
|
||||
jnFirst = [firStart julianNumber];
|
||||
jnRuleLast = ([rrule repeatInterval] *
|
||||
[rrule repeatCount] * 7) +
|
||||
jnFirst;
|
||||
until = [NSCalendarDate dateForJulianNumber: jnRuleLast];
|
||||
until = [until hour: [firStart hourOfDay]
|
||||
minute: [firStart minuteOfHour]
|
||||
second: [firStart secondOfMinute]];
|
||||
return until;
|
||||
}
|
||||
return [super lastInstanceStartDate];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user