/* * karecurrence.cpp - recurrence with special yearly February 29th handling * Program: kalarm * Copyright © 2005,2006,2008 by David Jarvie * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "kalarm.h" #include #include #include #include "datetime.h" #include "functions.h" #include "karecurrence.h" using namespace KCal; /*============================================================================= = Class KARecurrence = The purpose of this class is to represent the restricted range of recurrence = types which are handled by KAlarm, and to translate between these and the = libkcal Recurrence class. In particular, it handles yearly recurrences on = 29th February specially: = = KARecurrence allows annual 29th February recurrences to fall on 28th = February or 1st March, or not at all, in non-leap years. It allows such = 29th February recurrences to be combined with the 29th of other months in = a simple way, represented simply as the 29th of multiple months including = February. For storage in the libkcal calendar, the 29th day of the month = recurrence for other months is combined with a last-day-of-February or a = 60th-day-of-the-year recurrence rule, thereby conforming to RFC2445. =============================================================================*/ KARecurrence::Feb29Type KARecurrence::mDefaultFeb29 = KARecurrence::FEB29_FEB29; /****************************************************************************** * Set up a KARecurrence from recurrence parameters, using the start date to * determine the recurrence day/month as appropriate. * Only a restricted subset of recurrence types is allowed. * Reply = true if successful. */ bool KARecurrence::set(Type recurType, int freq, int count, int f29, const DateTime& start, const TQDateTime& end) { mCachedType = -1; RecurrenceRule::PeriodType rrtype; switch (recurType) { case MINUTELY: rrtype = RecurrenceRule::rMinutely; break; case DAILY: rrtype = RecurrenceRule::rDaily; break; case WEEKLY: rrtype = RecurrenceRule::rWeekly; break; case MONTHLY_DAY: rrtype = RecurrenceRule::rMonthly; break; case ANNUAL_DATE: rrtype = RecurrenceRule::rYearly; break; case NO_RECUR: rrtype = RecurrenceRule::rNone; break; default: return false; } if (!init(rrtype, freq, count, f29, start, end)) return false; switch (recurType) { case WEEKLY: { TQBitArray days(7); days.setBit(start.date().dayOfWeek() - 1); addWeeklyDays(days); break; } case MONTHLY_DAY: addMonthlyDate(start.date().day()); break; case ANNUAL_DATE: addYearlyDate(start.date().day()); addYearlyMonth(start.date().month()); break; default: break; } return true; } /****************************************************************************** * Initialise a KARecurrence from recurrence parameters. * Reply = true if successful. */ bool KARecurrence::init(RecurrenceRule::PeriodType recurType, int freq, int count, int f29, const DateTime& start, const TQDateTime& end) { mCachedType = -1; Feb29Type feb29Type = (f29 == -1) ? mDefaultFeb29 : static_cast(f29); mFeb29Type = FEB29_FEB29; clear(); if (count < -1) return false; bool dateOnly = start.isDateOnly(); if (!count && ((!dateOnly && !end.isValid()) || (dateOnly && !end.date().isValid()))) return false; switch (recurType) { case RecurrenceRule::rMinutely: case RecurrenceRule::rDaily: case RecurrenceRule::rWeekly: case RecurrenceRule::rMonthly: case RecurrenceRule::rYearly: break; case rNone: return true; default: return false; } setNewRecurrenceType(recurType, freq); if (count) setDuration(count); else if (dateOnly) setEndDate(end.date()); else setEndDateTime(end); TQDateTime startdt = start.dateTime(); if ((recurType == RecurrenceRule::rYearly && feb29Type == FEB29_FEB28) || feb29Type == FEB29_MAR1) { int year = startdt.date().year(); if (!TQDate::leapYear(year) && startdt.date().dayOfYear() == (feb29Type == FEB29_MAR1 ? 60 : 59)) { /* The event start date is February 28th or March 1st, but it * is a recurrence on February 29th (recurring on February 28th * or March 1st in non-leap years). Adjust the start date to * be on February 29th in the last previous leap year. * This is necessary because KARecurrence represents all types * of 29th February recurrences by a simple 29th February. */ while (!TQDate::leapYear(--year)) ; startdt.setDate(TQDate(year, 2, 29)); } mFeb29Type = feb29Type; } if (dateOnly) setStartDate(startdt.date()); else setStartDateTime(startdt); return true; } /****************************************************************************** * Initialise the recurrence from an iCalendar RRULE string. */ bool KARecurrence::set(const TQString& icalRRULE) { static TQString RRULE = TQString::fromLatin1("RRULE:"); mCachedType = -1; clear(); if (icalRRULE.isEmpty()) return true; ICalFormat format; if (!format.fromString(defaultRRule(true), (icalRRULE.startsWith(RRULE) ? icalRRULE.mid(RRULE.length()) : icalRRULE))) return false; fix(); return true; } /****************************************************************************** * Must be called after presetting with a KCal::Recurrence, to convert the * recurrence to KARecurrence types: * - Convert hourly recurrences to minutely. * - Remove all but the first day in yearly date recurrences. * - Check for yearly recurrences falling on February 29th and adjust them as * necessary. A 29th of the month rule can be combined with either a 60th day * of the year rule or a last day of February rule. */ void KARecurrence::fix() { mCachedType = -1; mFeb29Type = FEB29_FEB29; int convert = 0; int days[2] = { 0, 0 }; RecurrenceRule* rrules[2]; RecurrenceRule::List rrulelist = rRules(); RecurrenceRule::List::ConstIterator rr = rrulelist.begin(); for (int i = 0; i < 2 && rr != rrulelist.end(); ++i, ++rr) { RecurrenceRule* rrule = *rr; rrules[i] = rrule; bool stop = true; int rtype = recurrenceType(rrule); switch (rtype) { case rHourly: // Convert an hourly recurrence to a minutely one rrule->setRecurrenceType(RecurrenceRule::rMinutely); rrule->setFrequency(rrule->frequency() * 60); // fall through to rMinutely case rMinutely: case rDaily: case rWeekly: case rMonthlyDay: case rMonthlyPos: case rYearlyPos: if (!convert) ++rr; // remove all rules except the first break; case rOther: if (dailyType(rrule)) { // it's a daily rule with BYDAYS if (!convert) ++rr; // remove all rules except the first } break; case rYearlyDay: { // Ensure that the yearly day number is 60 (i.e. Feb 29th/Mar 1st) if (convert) { // This is the second rule. // Ensure that it can be combined with the first one. if (days[0] != 29 || rrule->frequency() != rrules[0]->frequency() || rrule->startDt() != rrules[0]->startDt()) break; } TQValueList ds = rrule->byYearDays(); if (!ds.isEmpty() && ds.first() == 60) { ++convert; // this rule needs to be converted days[i] = 60; stop = false; break; } break; // not day 60, so remove this rule } case rYearlyMonth: { TQValueList ds = rrule->byMonthDays(); if (!ds.isEmpty()) { int day = ds.first(); if (convert) { // This is the second rule. // Ensure that it can be combined with the first one. if (day == days[0] || (day == -1 && days[0] == 60) || rrule->frequency() != rrules[0]->frequency() || rrule->startDt() != rrules[0]->startDt()) break; } if (ds.count() > 1) { ds.clear(); // remove all but the first day ds.append(day); rrule->setByMonthDays(ds); } if (day == -1) { // Last day of the month - only combine if it's February TQValueList months = rrule->byMonths(); if (months.count() != 1 || months.first() != 2) day = 0; } if (day == 29 || day == -1) { ++convert; // this rule may need to be converted days[i] = day; stop = false; break; } } if (!convert) ++rr; break; } default: break; } if (stop) break; } // Remove surplus rules for ( ; rr != rrulelist.end(); ++rr) { removeRRule(*rr); delete *rr; } TQDate end; int count; TQValueList months; if (convert == 2) { // There are two yearly recurrence rules to combine into a February 29th recurrence. // Combine the two recurrence rules into a single rYearlyMonth rule falling on Feb 29th. // Find the duration of the two RRULEs combined, using the shorter of the two if they differ. if (days[0] != 29) { // Swap the two rules so that the 29th rule is the first RecurrenceRule* rr = rrules[0]; rrules[0] = rrules[1]; // the 29th rule rrules[1] = rr; int d = days[0]; days[0] = days[1]; days[1] = d; // the non-29th day } // If February is included in the 29th rule, remove it to avoid duplication months = rrules[0]->byMonths(); if (months.remove(2)) rrules[0]->setByMonths(months); count = combineDurations(rrules[0], rrules[1], end); mFeb29Type = (days[1] == 60) ? FEB29_MAR1 : FEB29_FEB28; } else if (convert == 1 && days[0] == 60) { // There is a single 60th day of the year rule. // Convert it to a February 29th recurrence. count = duration(); if (!count) end = endDate(); mFeb29Type = FEB29_MAR1; } else return; // Create the new February 29th recurrence setNewRecurrenceType(RecurrenceRule::rYearly, frequency()); RecurrenceRule* rrule = defaultRRule(); months.append(2); rrule->setByMonths(months); TQValueList ds; ds.append(29); rrule->setByMonthDays(ds); if (count) setDuration(count); else setEndDate(end); } /****************************************************************************** * Get the next time the recurrence occurs, strictly after a specified time. */ TQDateTime KARecurrence::getNextDateTime(const TQDateTime& preDateTime) const { switch (type()) { case ANNUAL_DATE: case ANNUAL_POS: { Recurrence recur; writeRecurrence(recur); return recur.getNextDateTime(preDateTime); } default: return Recurrence::getNextDateTime(preDateTime); } } /****************************************************************************** * Get the previous time the recurrence occurred, strictly before a specified time. */ TQDateTime KARecurrence::getPreviousDateTime(const TQDateTime& afterDateTime) const { switch (type()) { case ANNUAL_DATE: case ANNUAL_POS: { Recurrence recur; writeRecurrence(recur); return recur.getPreviousDateTime(afterDateTime); } default: return Recurrence::getPreviousDateTime(afterDateTime); } } /****************************************************************************** * Initialise a KCal::Recurrence to be the same as this instance. * Additional recurrence rules are created as necessary if it recurs on Feb 29th. */ void KARecurrence::writeRecurrence(KCal::Recurrence& recur) const { recur.clear(); recur.setStartDateTime(startDateTime()); recur.setExDates(exDates()); recur.setExDateTimes(exDateTimes()); const RecurrenceRule* rrule = defaultRRuleConst(); if (!rrule) return; int freq = frequency(); int count = duration(); static_cast(&recur)->setNewRecurrenceType(rrule->recurrenceType(), freq); if (count) recur.setDuration(count); else recur.setEndDateTime(endDateTime()); switch (type()) { case DAILY: if (rrule->byDays().isEmpty()) break; // fall through to rWeekly case WEEKLY: case MONTHLY_POS: recur.defaultRRule(true)->setByDays(rrule->byDays()); break; case MONTHLY_DAY: recur.defaultRRule(true)->setByMonthDays(rrule->byMonthDays()); break; case ANNUAL_POS: recur.defaultRRule(true)->setByMonths(rrule->byMonths()); recur.defaultRRule()->setByDays(rrule->byDays()); break; case ANNUAL_DATE: { TQValueList months = rrule->byMonths(); TQValueList days = monthDays(); bool special = (mFeb29Type != FEB29_FEB29 && !days.isEmpty() && days.first() == 29 && months.remove(2)); RecurrenceRule* rrule1 = recur.defaultRRule(); rrule1->setByMonths(months); rrule1->setByMonthDays(days); if (!special) break; // It recurs on the 29th February. // Create an additional 60th day of the year, or last day of February, rule. RecurrenceRule* rrule2 = new RecurrenceRule(); rrule2->setRecurrenceType(RecurrenceRule::rYearly); rrule2->setFrequency(freq); rrule2->setStartDt(startDateTime()); rrule2->setFloats(doesFloat()); if (!count) rrule2->setEndDt(endDateTime()); if (mFeb29Type == FEB29_MAR1) { TQValueList ds; ds.append(60); rrule2->setByYearDays(ds); } else { TQValueList ds; ds.append(-1); rrule2->setByMonthDays(ds); TQValueList ms; ms.append(2); rrule2->setByMonths(ms); } if (months.isEmpty()) { // Only February recurs. // Replace the RRULE and keep the recurrence count the same. if (count) rrule2->setDuration(count); recur.unsetRecurs(); } else { // Months other than February also recur on the 29th. // Remove February from the list and add a separate RRULE for February. if (count) { rrule1->setDuration(-1); rrule2->setDuration(-1); if (count > 0) { /* Adjust counts in the two rules to keep the correct occurrence total. * Note that durationTo() always includes the start date. Since for an * individual RRULE the start date may not actually be included, we need * to decrement the count if the start date doesn't actually recur in * this RRULE. * Note that if the count is small, one of the rules may not recur at * all. In that case, retain it so that the February 29th characteristic * is not lost should the user later change the recurrence count. */ TQDateTime end = endDateTime(); kdDebug()<<"29th recurrence: count="<