Monotone-Parent: 4e8caa9222644e6820515bbf312f74dbd372c2fc

Monotone-Revision: 25c172f93ef4ab6ada0aeb6ff4267e98aef8b719

Monotone-Author: wsourdeau@inverse.ca
Monotone-Date: 2008-12-10T18:57:55
Monotone-Branch: ca.inverse.sogo
maint-2.0.2
Wolfgang Sourdeau 2008-12-10 18:57:55 +00:00
parent b8b4c6b5d3
commit ff33d95a42
4 changed files with 526 additions and 469 deletions

View File

@ -130,23 +130,20 @@
- (NSCalendarDate *) lastInstanceStartDate
{
NSCalendarDate *firStart, *lastInstanceStartDate;
if ([rrule repeatCount] > 0)
{
long jnFirst, jnRuleLast;
NSCalendarDate *firStart, *until;
firStart = [firstRange startDate];
jnFirst = [firStart julianNumber];
jnRuleLast = ([rrule repeatInterval] *
[rrule repeatCount]) +
jnFirst;
until = [NSCalendarDate dateForJulianNumber:jnRuleLast];
until = [until hour: [firStart hourOfDay]
minute: [firStart minuteOfHour]
second: [firStart secondOfMinute]];
return until;
lastInstanceStartDate = [firStart dateByAddingYears: 0 months: 0
days: ([rrule repeatInterval]
* [rrule repeatCount])];
}
return [super lastInstanceStartDate];
else
lastInstanceStartDate = [super lastInstanceStartDate];
return lastInstanceStartDate;
}
@end /* iCalDailyRecurrenceCalculator */

View File

@ -33,7 +33,9 @@
#import <string.h>
@interface iCalRecurrenceCalculator (PrivateAPI)
- (NSCalendarDate *) lastInstanceStartDate;
@end
// #define HEAVY_DEBUG 1
@ -45,6 +47,7 @@ typedef BOOL NGMonthDaySet[32]; // 0 is unused
static void
NGMonthDaySet_clear (NGMonthDaySet *daySet)
{
register unsigned i;
@ -55,13 +58,16 @@ NGMonthDaySet_clear(NGMonthDaySet *daySet)
static void
NGMonthDaySet_copyOrUnion (NGMonthDaySet *base, NGMonthDaySet *new,
BOOL doCopy)
{
register unsigned i;
if (doCopy)
memcpy (base, new, sizeof (NGMonthDaySet));
else {
for (i = 1; i <= 31; i++) {
else
{
for (i = 1; i <= 31; i++)
{
if (! (*new)[i])
(*base)[i] = NO;
}
@ -70,6 +76,7 @@ NGMonthDaySet_copyOrUnion(NGMonthDaySet *base, NGMonthDaySet *new,
static BOOL NGMonthDaySet_fillWithByMonthDay (NGMonthDaySet *daySet,
NSArray *byMonthDay)
{
/* list of days in the month */
unsigned i, count;
@ -77,25 +84,30 @@ static BOOL NGMonthDaySet_fillWithByMonthDay(NGMonthDaySet *daySet,
NGMonthDaySet_clear (daySet);
for (i = 0, count = [byMonthDay count], ok = YES; i < count; i++) {
for (i = 0, count = [byMonthDay count], ok = YES; i < count; i++)
{
int dayInMonth; /* -31..-1 and 1..31 */
if ((dayInMonth = [[byMonthDay objectAtIndex:i] intValue]) == 0) {
if ((dayInMonth = [[byMonthDay objectAtIndex: i] intValue]) == 0)
{
ok = NO;
continue; /* invalid value */
}
if (dayInMonth > 31) {
if (dayInMonth > 31)
{
ok = NO;
continue; /* error, value to large */
}
if (dayInMonth < -31) {
if (dayInMonth < -31)
{
ok = NO;
continue; /* error, value to large */
}
/* adjust negative days */
if (dayInMonth < 0) {
if (dayInMonth < 0)
{
/* eg: -1 == last day in month, 30 days => 30 */
dayInMonth = 32 - dayInMonth /* because we count from 1 */;
}
@ -105,8 +117,10 @@ static BOOL NGMonthDaySet_fillWithByMonthDay(NGMonthDaySet *daySet,
return ok;
}
static inline unsigned iCalDoWForNSDoW(int dow) {
switch (dow) {
static inline unsigned iCalDoWForNSDoW (int dow)
{
switch (dow)
{
case 0: return iCalWeekDaySunday;
case 1: return iCalWeekDayMonday;
case 2: return iCalWeekDayTuesday;
@ -130,6 +144,7 @@ static void NGMonthDaySet_fillWithByDayX(NGMonthDaySet *daySet,
unsigned firstDoWInMonth,
unsigned numberOfDaysInMonth,
int occurrence1)
{
// TODO: this is called 'X' because the API doesn't allow for full iCalendar
// functionality. The daymask must be a list of occurence+dow
@ -139,11 +154,14 @@ static void NGMonthDaySet_fillWithByDayX(NGMonthDaySet *daySet,
NGMonthDaySet_clear (daySet);
if (occurrence1 >= 0) {
for (dayInMonth = 1, dow = firstDoWInMonth; dayInMonth<=31; dayInMonth++) {
if (occurrence1 >= 0)
{
for (dayInMonth = 1, dow = firstDoWInMonth; dayInMonth<=31; dayInMonth++)
{
// TODO: complete me
if (dayMask & iCalDoWForNSDoW(dow)) {
if (dayMask & iCalDoWForNSDoW (dow))
{
if (occurrence1 == 0)
(*daySet)[dayInMonth] = YES;
else { /* occurrence1 > 0 */
@ -157,7 +175,8 @@ static void NGMonthDaySet_fillWithByDayX(NGMonthDaySet *daySet,
dow = (dow == 6 /* Sat */) ? 0 /* Sun */ : (dow + 1);
}
}
else {
else
{
int lastDoWInMonthSet;
/* get the last dow in the set (not necessarily the month!) */
@ -172,7 +191,8 @@ static void NGMonthDaySet_fillWithByDayX(NGMonthDaySet *daySet,
#endif
/* start at the end of the set */
for (dayInMonth = numberOfDaysInMonth, dow = lastDoWInMonthSet;
dayInMonth >= 1; dayInMonth--) {
dayInMonth >= 1; dayInMonth--)
{
// TODO: complete me
#if HEAVY_DEBUG
@ -185,14 +205,16 @@ static void NGMonthDaySet_fillWithByDayX(NGMonthDaySet *daySet,
);
#endif
if (dayMask & iCalDoWForNSDoW(dow)) {
if (dayMask & iCalDoWForNSDoW (dow))
{
occurrences[dow] = occurrences[dow] + 1;
#if HEAVY_DEBUG
NSLog (@" MATCH %i/%@ count: %i occurences=%i",
dow, dowEN[dow], occurrences[dow], occurrence1);
#endif
if (occurrences[dow] == -occurrence1) {
if (occurrences[dow] == -occurrence1)
{
#if HEAVY_DEBUG
NSLog (@" COUNT MATCH");
#endif
@ -209,6 +231,7 @@ static void NGMonthDaySet_fillWithByDayX(NGMonthDaySet *daySet,
limitDate: (NSCalendarDate *)_until
limitRange: (NGCalendarDateRange *)_r
toArray: (NSMutableArray *)_ranges
{
NGCalendarDateRange *r;
NSCalendarDate *end;
@ -218,7 +241,8 @@ static void NGMonthDaySet_fillWithByDayX(NGMonthDaySet *daySet,
// TODO: I think we should check in here whether we succeeded the
// repeatCount. Currently we precalculate that info in the
// -lastInstanceStartDate method.
if (_until != nil) {
if (_until != nil)
{
/* Note: the 'until' in the rrule is inclusive as per spec */
if ([_until compare: _startDate] == NSOrderedAscending)
/* start after until */
@ -227,7 +251,7 @@ static void NGMonthDaySet_fillWithByDayX(NGMonthDaySet *daySet,
/* create end date */
end = [_startDate addTimeInterval:[self->firstRange duration]];
end = [_startDate addTimeInterval: [firstRange duration]];
[end setTimeZone: [_startDate timeZone]];
/* create range and check whether its in the requested range */
@ -240,7 +264,9 @@ static void NGMonthDaySet_fillWithByDayX(NGMonthDaySet *daySet,
return YES;
}
- (NSArray *)recurrenceRangesWithinCalendarDateRange:(NGCalendarDateRange *)_r{
- (NSArray *)
recurrenceRangesWithinCalendarDateRange: (NGCalendarDateRange *) _r
{
/* main entry */
// TODO: check whether this is OK for multiday-events!
NSMutableArray *ranges;
@ -257,18 +283,19 @@ static void NGMonthDaySet_fillWithByDayX(NGMonthDaySet *daySet,
NSArray *byMonthDay; // array of ints (-31..-1 and 1..31)
NGMonthDaySet byMonthDaySet;
eventStartDate = [self->firstRange startDate];
eventStartDate = [firstRange startDate];
eventDayOfMonth = [eventStartDate dayOfMonth];
timeZone = [eventStartDate timeZone];
rStart = [_r startDate];
rEnd = [_r endDate];
interval = [self->rrule repeatInterval];
interval = [rrule repeatInterval];
until = [self lastInstanceStartDate]; // TODO: maybe replace
byMonthDay = [self->rrule byMonthDay];
byMonthDay = [rrule byMonthDay];
/* check whether the range to be processed is beyond the 'until' date */
if (until) {
if (until)
{
if ([until compare: rStart] == NSOrderedAscending) /* until before start */
return nil;
if ([until compare: rEnd] == NSOrderedDescending) /* end before until */
@ -278,7 +305,8 @@ static void NGMonthDaySet_fillWithByDayX(NGMonthDaySet *daySet,
/* precalculate month days (same for all instances) */
if (byMonthDay) {
if (byMonthDay)
{
#if HEAVY_DEBUG
NSLog (@"byMonthDay: %@", byMonthDay);
#endif
@ -303,7 +331,8 @@ static void NGMonthDaySet_fillWithByDayX(NGMonthDaySet *daySet,
*/
for (monthIdxInRange = 0; monthIdxInRange < numberOfMonthsInRange;
monthIdxInRange++) {
monthIdxInRange++)
{
NSCalendarDate *cursor;
unsigned numDaysInMonth;
int monthIdxInRecurrence, dom;
@ -346,27 +375,30 @@ static void NGMonthDaySet_fillWithByDayX(NGMonthDaySet *daySet,
didByFill = NO;
if (byMonthDay) { /* list of days in the month */
if (byMonthDay)
{ /* list of days in the month */
NGMonthDaySet_copyOrUnion (&monthDays, &byMonthDaySet, !didByFill);
didByFill = YES;
}
if ([self->rrule byDayMask] != 0) { // TODO: replace the mask with an array
if ([rrule byDayMask] != 0)
{ // TODO: replace the mask with an array
NGMonthDaySet ruleset;
unsigned firstDoWInMonth;
firstDoWInMonth = [[cursor firstDayOfMonth] dayOfWeek];
NGMonthDaySet_fillWithByDayX (&ruleset,
[self->rrule byDayMask],
[rrule byDayMask],
firstDoWInMonth,
[cursor numberOfDaysInMonth],
[self->rrule byDayOccurence1]);
[rrule byDayOccurence1]);
NGMonthDaySet_copyOrUnion (&monthDays, &ruleset, !didByFill);
didByFill = YES;
}
if (!didByFill) {
if (!didByFill)
{
/* no rules applied, take the dayOfMonth of the startDate */
NGMonthDaySet_clear (&monthDays);
monthDays[eventDayOfMonth] = YES;
@ -388,7 +420,8 @@ static void NGMonthDaySet_fillWithByDayX(NGMonthDaySet *daySet,
cursor[2]: 2007-02-28 <== Note: we have February!
*/
for (dom = 1, doCont = YES; dom <= numDaysInMonth && doCont; dom++) {
for (dom = 1, doCont = YES; dom <= numDaysInMonth && doCont; dom++)
{
NSCalendarDate *start;
if (!monthDays[dom])
@ -416,10 +449,12 @@ static void NGMonthDaySet_fillWithByDayX(NGMonthDaySet *daySet,
start was: 2007-01-30
cursor will be: 2007-02-28
*/
if (eventDayOfMonth == dom) {
if (eventDayOfMonth == dom)
{
start = cursor;
}
else {
else
{
int maxDay =
eventDayOfMonth > numDaysInMonth ? numDaysInMonth : eventDayOfMonth;
@ -449,18 +484,20 @@ static void NGMonthDaySet_fillWithByDayX(NGMonthDaySet *daySet,
return ranges;
}
- (NSCalendarDate *)lastInstanceStartDate {
if ([self->rrule repeatCount] > 0) {
- (NSCalendarDate *) lastInstanceStartDate
{
if ([rrule repeatCount] > 0)
{
NSCalendarDate *until;
unsigned months, interval;
interval = [self->rrule repeatInterval];
months = [self->rrule repeatCount] - 1 /* the first counts as one! */;
interval = [rrule repeatInterval];
months = [rrule repeatCount] - 1 /* the first counts as one! */;
if (interval > 0)
months *= interval;
until = [[self->firstRange startDate] dateByAddingYears:0
until = [[firstRange startDate] dateByAddingYears: 0
months: months
days: 0];
return until;

View File

@ -56,7 +56,7 @@
long i, jnFirst, jnStart, jnEnd, startEndCount;
unsigned interval, byDayMask;
firStart = [self->firstRange startDate];
firStart = [firstRange startDate];
jnFirst = [firStart julianNumber];
jnEnd = [[_r endDate] julianNumber];
@ -64,21 +64,24 @@
return nil;
jnStart = [[_r startDate] julianNumber];
interval = [self->rrule repeatInterval];
interval = [rrule repeatInterval];
/* if rule is bound, check the bounds */
if (![self->rrule isInfinite]) {
if (![rrule isInfinite])
{
NSCalendarDate *until;
long jnRuleLast;
until = [self->rrule untilDate];
if (until) {
until = [rrule untilDate];
if (until)
{
if ([until compare: [_r startDate]] == NSOrderedAscending)
return nil;
jnRuleLast = [until julianNumber];
}
else {
jnRuleLast = (interval * [self->rrule repeatCount] * 7)
else
{
jnRuleLast = (interval * [rrule repeatCount] * 7)
+ jnFirst;
if (jnRuleLast < jnStart)
return nil;
@ -90,17 +93,21 @@
startEndCount = (jnEnd - jnStart) + 1;
ranges = [NSMutableArray arrayWithCapacity: startEndCount];
byDayMask = [self->rrule byDayMask];
if (!byDayMask) {
for (i = 0 ; i < startEndCount; i++) {
byDayMask = [rrule byDayMask];
if (!byDayMask)
{
for (i = 0 ; i < startEndCount; i++)
{
long jnCurrent;
jnCurrent = jnStart + i;
if (jnCurrent >= jnFirst) {
if (jnCurrent >= jnFirst)
{
long jnDiff;
jnDiff = jnCurrent - jnFirst; /* difference in days */
if ((jnDiff % (interval * 7)) == 0) {
if ((jnDiff % (interval * 7)) == 0)
{
NSCalendarDate *start, *end;
NGCalendarDateRange *r;
@ -109,7 +116,7 @@
start = [start hour: [firStart hourOfDay]
minute: [firStart minuteOfHour]
second: [firStart secondOfMinute]];
end = [start addTimeInterval:[self->firstRange duration]];
end = [start addTimeInterval: [firstRange duration]];
r = [NGCalendarDateRange calendarDateRangeWithStartDate: start
endDate: end];
if ([_r containsDateRange: r])
@ -118,7 +125,8 @@
}
}
}
else {
else
{
long jnFirstWeekStart, weekStartOffset;
/* calculate jnFirst's week start - this depends on our setting of week
@ -128,28 +136,34 @@
jnFirstWeekStart = jnFirst - weekStartOffset;
for (i = 0 ; i < startEndCount; i++) {
for (i = 0 ; i < startEndCount; i++)
{
long jnCurrent;
jnCurrent = jnStart + i;
if (jnCurrent >= jnFirst) {
if (jnCurrent >= jnFirst)
{
long jnDiff;
/* we need to calculate a difference in weeks */
jnDiff = (jnCurrent - jnFirstWeekStart) % 7;
if ((jnDiff % interval) == 0) {
if ((jnDiff % interval) == 0)
{
BOOL isRecurrence = NO;
if (jnCurrent == jnFirst) {
if (jnCurrent == jnFirst)
{
isRecurrence = YES;
}
else {
else
{
iCalWeekDay weekDay;
weekDay = [self weekDayForJulianNumber: jnCurrent];
isRecurrence = (weekDay & [self->rrule byDayMask]) ? YES : NO;
isRecurrence = (weekDay & [rrule byDayMask]) ? YES : NO;
}
if (isRecurrence) {
if (isRecurrence)
{
NSCalendarDate *start, *end;
NGCalendarDateRange *r;
@ -158,7 +172,7 @@
start = [start hour: [firStart hourOfDay]
minute: [firStart minuteOfHour]
second: [firStart secondOfMinute]];
end = [start addTimeInterval:[self->firstRange duration]];
end = [start addTimeInterval: [firstRange duration]];
r = [NGCalendarDateRange calendarDateRangeWithStartDate: start
endDate: end];
if ([_r containsDateRange: r])
@ -171,15 +185,17 @@
return ranges;
}
- (NSCalendarDate *)lastInstanceStartDate {
if ([self->rrule repeatCount] > 0) {
- (NSCalendarDate *) lastInstanceStartDate
{
if ([rrule repeatCount] > 0)
{
long jnFirst, jnRuleLast;
NSCalendarDate *firStart, *until;
firStart = [self->firstRange startDate];
firStart = [firstRange startDate];
jnFirst = [firStart julianNumber];
jnRuleLast = ([self->rrule repeatInterval] *
[self->rrule repeatCount] * 7) +
jnRuleLast = ([rrule repeatInterval] *
[rrule repeatCount] * 7) +
jnFirst;
until = [NSCalendarDate dateForJulianNumber: jnRuleLast];
until = [until hour: [firStart hourOfDay]

View File

@ -36,19 +36,22 @@
@implementation iCalYearlyRecurrenceCalculator
- (NSArray *)recurrenceRangesWithinCalendarDateRange:(NGCalendarDateRange *)_r{
- (NSArray *)
recurrenceRangesWithinCalendarDateRange: (NGCalendarDateRange *) _r
{
NSMutableArray *ranges;
NSCalendarDate *firStart, *rStart, *rEnd, *until;
unsigned i, count, interval;
int diff;
firStart = [self->firstRange startDate];
firStart = [firstRange startDate];
rStart = [_r startDate];
rEnd = [_r endDate];
interval = [self->rrule repeatInterval];
interval = [rrule repeatInterval];
until = [self lastInstanceStartDate];
if (until) {
if (until)
{
if ([until compare: rStart] == NSOrderedAscending)
return nil;
if ([until compare: rEnd] == NSOrderedDescending)
@ -61,11 +64,13 @@
count = [rStart yearsBetweenDate: rEnd] + 1;
ranges = [NSMutableArray arrayWithCapacity: count];
for (i = 0 ; i < count; i++) {
for (i = 0 ; i < count; i++)
{
int test;
test = diff + i;
if ((test >= 0) && (test % interval) == 0) {
if ((test >= 0) && (test % interval) == 0)
{
NSCalendarDate *start, *end;
NGCalendarDateRange *r;
@ -73,7 +78,7 @@
months: 0
days: 0];
[start setTimeZone: [firStart timeZone]];
end = [start addTimeInterval:[self->firstRange duration]];
end = [start addTimeInterval: [firstRange duration]];
r = [NGCalendarDateRange calendarDateRangeWithStartDate: start
endDate: end];
if ([_r containsDateRange: r])
@ -83,14 +88,16 @@
return ranges;
}
- (NSCalendarDate *)lastInstanceStartDate {
if ([self->rrule repeatCount] > 0) {
- (NSCalendarDate *) lastInstanceStartDate
{
if ([rrule repeatCount] > 0)
{
NSCalendarDate *until;
unsigned years, interval;
interval = [self->rrule repeatInterval];
years = [self->rrule repeatCount] * interval;
until = [[self->firstRange startDate] dateByAddingYears:years
interval = [rrule repeatInterval];
years = [rrule repeatCount] * interval;
until = [[firstRange startDate] dateByAddingYears: years
months: 0
days: 0];
return until;