diff --git a/CMakeLists.txt b/CMakeLists.txt index 6cc491223..dae97b806 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,123 +1,125 @@ cmake_minimum_required(VERSION 3.5) set(KF5_VERSION "5.64.0") # handled by release scripts project(KCalendarCore VERSION ${KF5_VERSION}) # ECM setup include(FeatureSummary) find_package(ECM 5.63.0 NO_MODULE) set_package_properties(ECM PROPERTIES TYPE REQUIRED DESCRIPTION "Extra CMake Modules." URL "https://projects.kde.org/projects/kdesupport/extra-cmake-modules") feature_summary(WHAT REQUIRED_PACKAGES_NOT_FOUND FATAL_ON_MISSING_REQUIRED_PACKAGES) set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake) set(REQUIRED_QT_VERSION 5.11.0) include(KDEInstallDirs) include(KDECMakeSettings) include(KDEFrameworkCompilerSettings NO_POLICY_SCOPE) -include(GenerateExportHeader) +include(ECMGenerateExportHeader) include(ECMGenerateHeaders) include(ECMGeneratePriFile) include(ECMSetupVersion) include(ECMQtDeclareLoggingCategory) include(ECMAddQch) +set(EXCLUDE_DEPRECATED_BEFORE_AND_AT 0 CACHE STRING "Control the range of deprecated API excluded from the build [default=0].") + option(BUILD_QCH "Build API documentation in QCH format (for e.g. Qt Assistant, Qt Creator & KDevelop)" OFF) add_feature_info(QCH ${BUILD_QCH} "API documentation in QCH format (for e.g. Qt Assistant, Qt Creator & KDevelop)") # TODO: remove for KF6 option(KCALENDARCORE_NO_DEPRECATED_NAMESPACE "Disable deprecated KCalCore namespace" OFF) if(POLICY CMP0053) cmake_policy(SET CMP0053 NEW) endif() set(KCALENDARCORE_LIB_VERSION ${KF5_VERSION}) ecm_setup_version(PROJECT VARIABLE_PREFIX KCALENDARCORE VERSION_HEADER "${KCalendarCore_BINARY_DIR}/kcalendarcore_version.h" PACKAGE_VERSION_FILE "${KCalendarCore_BINARY_DIR}/KF5CalendarCoreConfigVersion.cmake" SOVERSION 5 ) ########### Find packages ########### find_package(Qt5 ${REQUIRED_QT_VERSION} CONFIG REQUIRED Core Gui) set(LibIcal_MIN_VERSION "2.0") find_package(LibIcal ${LibIcal_MIN_VERSION}) set_package_properties(LibIcal PROPERTIES TYPE REQUIRED) if(LibIcal_VERSION VERSION_GREATER "2.98") set(HAVE_ICAL_3 TRUE) add_definitions(-DUSE_ICAL_3) endif() ########### CMake Config Files ########### set(CMAKECONFIG_INSTALL_DIR "${KDE_INSTALL_CMAKEPACKAGEDIR}/KF5CalendarCore") if (BUILD_QCH) set(PACKAGE_INCLUDE_QCHTARGETS "include(\"\${CMAKE_CURRENT_LIST_DIR}/KF5CalendarCoreQchTargets.cmake\")") endif() configure_package_config_file( "${CMAKE_CURRENT_SOURCE_DIR}/KF5CalendarCoreConfig.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/KF5CalendarCoreConfig.cmake" INSTALL_DESTINATION ${CMAKECONFIG_INSTALL_DIR} ) add_definitions(-DQT_DISABLE_DEPRECATED_BEFORE=0x050d00) add_definitions(-DQT_NO_FOREACH) ########### Targets ########### add_subdirectory(src) if(BUILD_TESTING) find_package(Qt5 ${QT_REQUIRED_VERSION} CONFIG REQUIRED Test) add_subdirectory(autotests) endif() add_subdirectory(cmake) ########### Install Files ########### install(FILES "${CMAKE_CURRENT_BINARY_DIR}/KF5CalendarCoreConfig.cmake" "${CMAKE_CURRENT_BINARY_DIR}/KF5CalendarCoreConfigVersion.cmake" DESTINATION "${CMAKECONFIG_INSTALL_DIR}" COMPONENT Devel ) install(EXPORT KF5CalendarCoreTargets DESTINATION "${CMAKECONFIG_INSTALL_DIR}" FILE KF5CalendarCoreTargets.cmake NAMESPACE KF5:: ) if (BUILD_QCH) ecm_install_qch_export( TARGETS KF5CalendarCore_QCH FILE KF5CalendarCoreQchTargets.cmake DESTINATION "${CMAKECONFIG_INSTALL_DIR}" COMPONENT Devel ) endif() install(FILES ${CMAKE_CURRENT_BINARY_DIR}/kcalendarcore_version.h DESTINATION ${KDE_INSTALL_INCLUDEDIR_KF5} COMPONENT Devel ) if (NOT KCALENDARCORE_NO_DEPRECATED_NAMESPACE) ecm_setup_version(PROJECT VARIABLE_PREFIX KCALCORE VERSION_HEADER "${KCalendarCore_BINARY_DIR}/kcalcore_version.h" ) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/kcalcore_version.h DESTINATION ${KDE_INSTALL_INCLUDEDIR_KF5} COMPONENT Devel ) endif() install(FILES kcalendarcore.renamecategories kcalendarcore.categories DESTINATION ${KDE_INSTALL_LOGGINGCATEGORIESDIR}) feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3521a3910..f29051b66 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,175 +1,182 @@ ########### next target ############### set(kcalcore_LIB_SRCS alarm.cpp attachment.cpp attendee.cpp calendar.cpp calfilter.cpp calformat.cpp calstorage.cpp compat.cpp customproperties.cpp duration.cpp event.cpp exceptions.cpp filestorage.cpp freebusy.cpp freebusycache.cpp freebusyperiod.cpp icalformat.cpp icalformat_p.cpp icaltimezones.cpp incidence.cpp incidencebase.cpp journal.cpp memorycalendar.cpp occurrenceiterator.cpp period.cpp person.cpp recurrence.cpp recurrencerule.cpp schedulemessage.cpp sorting.cpp todo.cpp utils.cpp vcalformat.cpp visitor.cpp ) ecm_qt_declare_logging_category(kcalcore_LIB_SRCS HEADER kcalendarcore_debug.h IDENTIFIER KCALCORE_LOG CATEGORY_NAME org.kde.pim.kcalcore) add_library(KF5CalendarCore ${kcalcore_LIB_SRCS}) +add_library(KF5::CalendarCore ALIAS KF5CalendarCore) -generate_export_header(KF5CalendarCore BASE_NAME kcalendarcore) +ecm_generate_export_header(KF5CalendarCore + BASE_NAME kcalendarcore + # GROUP_BASE_NAME KF <- enable once all of KF modules use ecm_generate_export_header + VERSION ${KF5_VERSION} + DEPRECATED_BASE_VERSION 0 + DEPRECATION_VERSIONS 5.64 + EXCLUDE_DEPRECATED_BEFORE_AND_AT ${EXCLUDE_DEPRECATED_BEFORE_AND_AT} +) -add_library(KF5::CalendarCore ALIAS KF5CalendarCore) # backward compatibility with the old name if (NOT KCALENDARCORE_NO_DEPRECATED_NAMESPACE) target_compile_definitions(KF5CalendarCore INTERFACE "-DKCalCore=KCalendarCore") endif() target_include_directories(KF5CalendarCore INTERFACE "$") target_include_directories(KF5CalendarCore PUBLIC "$") target_link_libraries(KF5CalendarCore PUBLIC Qt5::Core PRIVATE Qt5::Gui LibIcal ) set_target_properties(KF5CalendarCore PROPERTIES VERSION ${KCALENDARCORE_VERSION} SOVERSION ${KCALENDARCORE_SOVERSION} EXPORT_NAME CalendarCore ) install(TARGETS KF5CalendarCore EXPORT KF5CalendarCoreTargets ${KF5_INSTALL_TARGETS_DEFAULT_ARGS}) ########### Generate Headers ############### set(kcalendarcore_headers Alarm Attachment Attendee CalFilter CalFormat CalStorage Calendar CustomProperties Duration Event Exceptions FileStorage FreeBusy FreeBusyCache FreeBusyPeriod ICalFormat Incidence IncidenceBase Journal MemoryCalendar OccurrenceIterator Period Person Recurrence RecurrenceRule ScheduleMessage Sorting Todo VCalFormat Visitor ) ecm_generate_headers(KCalendarCore_CamelCase_HEADERS HEADER_NAMES ${kcalendarcore_headers} PREFIX KCalendarCore REQUIRED_HEADERS KCalendarCore_HEADERS ) if (NOT KCALENDARCORE_NO_DEPRECATED_NAMESPACE) ecm_generate_headers(KCalCore_CamelCase_HEADERS HEADER_NAMES ${kcalendarcore_headers} PREFIX KCalCore REQUIRED_HEADERS KCalCore_HEADERS ) endif() ########### install files ############### install(FILES ${CMAKE_CURRENT_BINARY_DIR}/kcalendarcore_export.h ${KCalendarCore_HEADERS} DESTINATION ${KDE_INSTALL_INCLUDEDIR_KF5}/KCalendarCore/kcalendarcore COMPONENT Devel ) install(FILES ${KCalendarCore_CamelCase_HEADERS} DESTINATION ${KDE_INSTALL_INCLUDEDIR_KF5}/KCalendarCore/KCalendarCore COMPONENT Devel ) if (NOT KCALENDARCORE_NO_DEPRECATED_NAMESPACE) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/kcalendarcore_export.h ${KCalCore_HEADERS} DESTINATION ${KDE_INSTALL_INCLUDEDIR_KF5}/KCalendarCore/kcalcore COMPONENT Devel ) install(FILES ${KCalCore_CamelCase_HEADERS} DESTINATION ${KDE_INSTALL_INCLUDEDIR_KF5}/KCalendarCore/KCalCore COMPONENT Devel ) endif() ecm_generate_pri_file(BASE_NAME KCalendarCore LIB_NAME KF5CalendarCore DEPS "Core" FILENAME_VAR PRI_FILENAME INCLUDE_INSTALL_DIR ${KDE_INSTALL_INCLUDEDIR_KF5}/KCalendarCore/KCalendarCore) install(FILES ${PRI_FILENAME} DESTINATION ${ECM_MKSPECS_INSTALL_DIR}) if(BUILD_QCH) ecm_add_qch( KF5CalendarCore_QCH NAME KCalendarCore BASE_NAME KF5CalendarCore VERSION ${KCalendarCore_VERSION} ORG_DOMAIN org.kde SOURCES # using only public headers, to cover only public API ${KCalendarCore_HEADERS} MD_MAINPAGE "${CMAKE_SOURCE_DIR}/README.md" LINK_QCHS Qt5Core_QCH INCLUDE_DIRS ${CMAKE_CURRENT_BINARY_DIR} BLANK_MACROS KCALENDARCORE_EXPORT TAGFILE_INSTALL_DESTINATION ${KDE_INSTALL_QTQCHDIR} QCH_INSTALL_DESTINATION ${KDE_INSTALL_QTQCHDIR} COMPONENT Devel ) endif() diff --git a/src/recurrence.cpp b/src/recurrence.cpp index a1b5f483d..080273fb6 100644 --- a/src/recurrence.cpp +++ b/src/recurrence.cpp @@ -1,1557 +1,1559 @@ /* This file is part of kcalcore library. Copyright (c) 1998 Preston Brown Copyright (c) 2001 Cornelius Schumacher Copyright (c) 2002,2006 David Jarvie Copyright (C) 2005 Reinhold Kainhofer This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "recurrence.h" #include "utils_p.h" #include "recurrencehelper_p.h" #include "kcalendarcore_debug.h" #include #include #include #include using namespace KCalendarCore; //@cond PRIVATE class Q_DECL_HIDDEN KCalendarCore::Recurrence::Private { public: Private() : mCachedType(rMax), mAllDay(false), mRecurReadOnly(false) { } Private(const Private &p) : mRDateTimes(p.mRDateTimes), mRDates(p.mRDates), mExDateTimes(p.mExDateTimes), mExDates(p.mExDates), mStartDateTime(p.mStartDateTime), mCachedType(p.mCachedType), mAllDay(p.mAllDay), mRecurReadOnly(p.mRecurReadOnly) { } bool operator==(const Private &p) const; RecurrenceRule::List mExRules; RecurrenceRule::List mRRules; QList mRDateTimes; DateList mRDates; QList mExDateTimes; DateList mExDates; QDateTime mStartDateTime; // date/time of first recurrence QList mObservers; // Cache the type of the recurrence with the old system (e.g. MonthlyPos) mutable ushort mCachedType; bool mAllDay = false; // the recurrence has no time, just a date bool mRecurReadOnly = false; }; bool Recurrence::Private::operator==(const Recurrence::Private &p) const { // qCDebug(KCALCORE_LOG) << mStartDateTime << p.mStartDateTime; if ((mStartDateTime != p.mStartDateTime && (mStartDateTime.isValid() || p.mStartDateTime.isValid())) || mAllDay != p.mAllDay || mRecurReadOnly != p.mRecurReadOnly || mExDates != p.mExDates || mExDateTimes != p.mExDateTimes || mRDates != p.mRDates || mRDateTimes != p.mRDateTimes) { return false; } // Compare the rrules, exrules! Assume they have the same order... This only // matters if we have more than one rule (which shouldn't be the default anyway) int i; int end = mRRules.count(); if (end != p.mRRules.count()) { return false; } for (i = 0; i < end; ++i) { if (*mRRules[i] != *p.mRRules[i]) { return false; } } end = mExRules.count(); if (end != p.mExRules.count()) { return false; } for (i = 0; i < end; ++i) { if (*mExRules[i] != *p.mExRules[i]) { return false; } } return true; } //@endcond Recurrence::Recurrence() : d(new KCalendarCore::Recurrence::Private()) { } Recurrence::Recurrence(const Recurrence &r) : RecurrenceRule::RuleObserver(), d(new KCalendarCore::Recurrence::Private(*r.d)) { int i, end; d->mRRules.reserve(r.d->mRRules.count()); for (i = 0, end = r.d->mRRules.count(); i < end; ++i) { RecurrenceRule *rule = new RecurrenceRule(*r.d->mRRules[i]); d->mRRules.append(rule); rule->addObserver(this); } d->mExRules.reserve(r.d->mExRules.count()); for (i = 0, end = r.d->mExRules.count(); i < end; ++i) { RecurrenceRule *rule = new RecurrenceRule(*r.d->mExRules[i]); d->mExRules.append(rule); rule->addObserver(this); } } Recurrence::~Recurrence() { qDeleteAll(d->mExRules); qDeleteAll(d->mRRules); delete d; } bool Recurrence::operator==(const Recurrence &recurrence) const { return *d == *recurrence.d; } +#if KCALENDARCORE_BUILD_DEPRECATED_SINCE(5, 64) Recurrence &Recurrence::operator=(const Recurrence &recurrence) { // check for self assignment if (&recurrence == this) { return *this; } // ### this copies the pointers in mExRules and mRRules eventually resulting in a double free! // fortunately however this function is unused, we just can't remove it just yet, due to ABI guarantees *d = *recurrence.d; return *this; } +#endif void Recurrence::addObserver(RecurrenceObserver *observer) { if (!d->mObservers.contains(observer)) { d->mObservers.append(observer); } } void Recurrence::removeObserver(RecurrenceObserver *observer) { d->mObservers.removeAll(observer); } QDateTime Recurrence::startDateTime() const { return d->mStartDateTime; } bool Recurrence::allDay() const { return d->mAllDay; } void Recurrence::setAllDay(bool allDay) { if (d->mRecurReadOnly || allDay == d->mAllDay) { return; } d->mAllDay = allDay; for (int i = 0, end = d->mRRules.count(); i < end; ++i) { d->mRRules[i]->setAllDay(allDay); } for (int i = 0, end = d->mExRules.count(); i < end; ++i) { d->mExRules[i]->setAllDay(allDay); } updated(); } RecurrenceRule *Recurrence::defaultRRule(bool create) const { if (d->mRRules.isEmpty()) { if (!create || d->mRecurReadOnly) { return nullptr; } RecurrenceRule *rrule = new RecurrenceRule(); rrule->setStartDt(startDateTime()); const_cast(this)->addRRule(rrule); return rrule; } else { return d->mRRules[0]; } } RecurrenceRule *Recurrence::defaultRRuleConst() const { return d->mRRules.isEmpty() ? nullptr : d->mRRules[0]; } void Recurrence::updated() { // recurrenceType() re-calculates the type if it's rMax d->mCachedType = rMax; for (int i = 0, end = d->mObservers.count(); i < end; ++i) { if (d->mObservers[i]) { d->mObservers[i]->recurrenceUpdated(this); } } } bool Recurrence::recurs() const { return !d->mRRules.isEmpty() || !d->mRDates.isEmpty() || !d->mRDateTimes.isEmpty(); } ushort Recurrence::recurrenceType() const { if (d->mCachedType == rMax) { d->mCachedType = recurrenceType(defaultRRuleConst()); } return d->mCachedType; } ushort Recurrence::recurrenceType(const RecurrenceRule *rrule) { if (!rrule) { return rNone; } RecurrenceRule::PeriodType type = rrule->recurrenceType(); // BYSETPOS, BYWEEKNUMBER and BYSECOND were not supported in old versions if (!rrule->bySetPos().isEmpty() || !rrule->bySeconds().isEmpty() || !rrule->byWeekNumbers().isEmpty()) { return rOther; } // It wasn't possible to set BYMINUTES, BYHOUR etc. by the old code. So if // it's set, it's none of the old types if (!rrule->byMinutes().isEmpty() || !rrule->byHours().isEmpty()) { return rOther; } // Possible combinations were: // BYDAY: with WEEKLY, MONTHLY, YEARLY // BYMONTHDAY: with MONTHLY, YEARLY // BYMONTH: with YEARLY // BYYEARDAY: with YEARLY if ((!rrule->byYearDays().isEmpty() && type != RecurrenceRule::rYearly) || (!rrule->byMonths().isEmpty() && type != RecurrenceRule::rYearly)) { return rOther; } if (!rrule->byDays().isEmpty()) { if (type != RecurrenceRule::rYearly && type != RecurrenceRule::rMonthly && type != RecurrenceRule::rWeekly) { return rOther; } } switch (type) { case RecurrenceRule::rNone: return rNone; case RecurrenceRule::rMinutely: return rMinutely; case RecurrenceRule::rHourly: return rHourly; case RecurrenceRule::rDaily: return rDaily; case RecurrenceRule::rWeekly: return rWeekly; case RecurrenceRule::rMonthly: { if (rrule->byDays().isEmpty()) { return rMonthlyDay; } else if (rrule->byMonthDays().isEmpty()) { return rMonthlyPos; } else { return rOther; // both position and date specified } } case RecurrenceRule::rYearly: { // Possible combinations: // rYearlyMonth: [BYMONTH &] BYMONTHDAY // rYearlyDay: BYYEARDAY // rYearlyPos: [BYMONTH &] BYDAY if (!rrule->byDays().isEmpty()) { // can only by rYearlyPos if (rrule->byMonthDays().isEmpty() && rrule->byYearDays().isEmpty()) { return rYearlyPos; } else { return rOther; } } else if (!rrule->byYearDays().isEmpty()) { // Can only be rYearlyDay if (rrule->byMonths().isEmpty() && rrule->byMonthDays().isEmpty()) { return rYearlyDay; } else { return rOther; } } else { return rYearlyMonth; } } default: return rOther; } } bool Recurrence::recursOn(const QDate &qd, const QTimeZone &timeZone) const { // Don't waste time if date is before the start of the recurrence if (QDateTime(qd, QTime(23, 59, 59), timeZone) < d->mStartDateTime) { return false; } // First handle dates. Exrules override if (std::binary_search(d->mExDates.constBegin(), d->mExDates.constEnd(), qd)) { return false; } int i, end; // For all-day events a matching exrule excludes the whole day // since exclusions take precedence over inclusions, we know it can't occur on that day. if (allDay()) { for (i = 0, end = d->mExRules.count(); i < end; ++i) { if (d->mExRules[i]->recursOn(qd, timeZone)) { return false; } } } if (std::binary_search(d->mRDates.constBegin(), d->mRDates.constEnd(), qd)) { return true; } // Check if it might recur today at all. bool recurs = (startDate() == qd); for (i = 0, end = d->mRDateTimes.count(); i < end && !recurs; ++i) { recurs = (d->mRDateTimes[i].toTimeZone(timeZone).date() == qd); } for (i = 0, end = d->mRRules.count(); i < end && !recurs; ++i) { recurs = d->mRRules[i]->recursOn(qd, timeZone); } // If the event wouldn't recur at all, simply return false, don't check ex* if (!recurs) { return false; } // Check if there are any times for this day excluded, either by exdate or exrule: bool exon = false; for (i = 0, end = d->mExDateTimes.count(); i < end && !exon; ++i) { exon = (d->mExDateTimes[i].toTimeZone(timeZone).date() == qd); } if (!allDay()) { // we have already checked all-day times above for (i = 0, end = d->mExRules.count(); i < end && !exon; ++i) { exon = d->mExRules[i]->recursOn(qd, timeZone); } } if (!exon) { // Simple case, nothing on that day excluded, return the value from before return recurs; } else { // Harder part: I don't think there is any way other than to calculate the // whole list of items for that day. //TODO: consider whether it would be more efficient to call // Rule::recurTimesOn() instead of Rule::recursOn() from the start TimeList timesForDay(recurTimesOn(qd, timeZone)); return !timesForDay.isEmpty(); } } bool Recurrence::recursAt(const QDateTime &dt) const { // Convert to recurrence's time zone for date comparisons, and for more efficient time comparisons const auto dtrecur = dt.toTimeZone(d->mStartDateTime.timeZone()); // if it's excluded anyway, don't bother to check if it recurs at all. if (std::binary_search(d->mExDateTimes.constBegin(), d->mExDateTimes.constEnd(), dtrecur) || std::binary_search(d->mExDates.constBegin(), d->mExDates.constEnd(), dtrecur.date())) { return false; } int i, end; for (i = 0, end = d->mExRules.count(); i < end; ++i) { if (d->mExRules[i]->recursAt(dtrecur)) { return false; } } // Check explicit recurrences, then rrules. if (startDateTime() == dtrecur || std::binary_search(d->mRDateTimes.constBegin(), d->mRDateTimes.constEnd(), dtrecur)) { return true; } for (i = 0, end = d->mRRules.count(); i < end; ++i) { if (d->mRRules[i]->recursAt(dtrecur)) { return true; } } return false; } /** Calculates the cumulative end of the whole recurrence (rdates and rrules). If any rrule is infinite, or the recurrence doesn't have any rrules or rdates, an invalid date is returned. */ QDateTime Recurrence::endDateTime() const { QList dts; dts << startDateTime(); if (!d->mRDates.isEmpty()) { dts << QDateTime(d->mRDates.last(), QTime(0, 0, 0), d->mStartDateTime.timeZone()); } if (!d->mRDateTimes.isEmpty()) { dts << d->mRDateTimes.last(); } for (int i = 0, end = d->mRRules.count(); i < end; ++i) { auto rl = d->mRRules[i]->endDt(); // if any of the rules is infinite, the whole recurrence is if (!rl.isValid()) { return QDateTime(); } dts << rl; } sortAndRemoveDuplicates(dts); return dts.isEmpty() ? QDateTime() : dts.last(); } /** Calculates the cumulative end of the whole recurrence (rdates and rrules). If any rrule is infinite, or the recurrence doesn't have any rrules or rdates, an invalid date is returned. */ QDate Recurrence::endDate() const { QDateTime end(endDateTime()); return end.isValid() ? end.date() : QDate(); } void Recurrence::setEndDate(const QDate &date) { QDateTime dt(date, d->mStartDateTime.time(), d->mStartDateTime.timeZone()); if (allDay()) { dt.setTime(QTime(23, 59, 59)); } setEndDateTime(dt); } void Recurrence::setEndDateTime(const QDateTime &dateTime) { if (d->mRecurReadOnly) { return; } RecurrenceRule *rrule = defaultRRule(true); if (!rrule) { return; } // If the recurrence rule has a duration, and we're trying to set an invalid end date, // we have to skip setting it to avoid setting the field dirty. // The end date is already invalid since the duration is set and end date/duration // are mutually exclusive. // We can't use inequality check below, because endDt() also returns a valid date // for a duration (it is calculated from the duration). if (rrule->duration() > 0 && !dateTime.isValid()) { return; } if (dateTime != rrule->endDt()) { rrule->setEndDt(dateTime); updated(); } } int Recurrence::duration() const { RecurrenceRule *rrule = defaultRRuleConst(); return rrule ? rrule->duration() : 0; } int Recurrence::durationTo(const QDateTime &datetime) const { // Emulate old behavior: This is just an interface to the first rule! RecurrenceRule *rrule = defaultRRuleConst(); return rrule ? rrule->durationTo(datetime) : 0; } int Recurrence::durationTo(const QDate &date) const { return durationTo(QDateTime(date, QTime(23, 59, 59), d->mStartDateTime.timeZone())); } void Recurrence::setDuration(int duration) { if (d->mRecurReadOnly) { return; } RecurrenceRule *rrule = defaultRRule(true); if (!rrule) { return; } if (duration != rrule->duration()) { rrule->setDuration(duration); updated(); } } void Recurrence::shiftTimes(const QTimeZone &oldTz, const QTimeZone &newTz) { if (d->mRecurReadOnly) { return; } d->mStartDateTime = d->mStartDateTime.toTimeZone(oldTz); d->mStartDateTime.setTimeZone(newTz); int i, end; for (i = 0, end = d->mRDateTimes.count(); i < end; ++i) { d->mRDateTimes[i] = d->mRDateTimes[i].toTimeZone(oldTz); d->mRDateTimes[i].setTimeZone(newTz); } for (i = 0, end = d->mExDateTimes.count(); i < end; ++i) { d->mExDateTimes[i] = d->mExDateTimes[i].toTimeZone(oldTz); d->mExDateTimes[i].setTimeZone(newTz); } for (i = 0, end = d->mRRules.count(); i < end; ++i) { d->mRRules[i]->shiftTimes(oldTz, newTz); } for (i = 0, end = d->mExRules.count(); i < end; ++i) { d->mExRules[i]->shiftTimes(oldTz, newTz); } } void Recurrence::unsetRecurs() { if (d->mRecurReadOnly) { return; } qDeleteAll(d->mRRules); d->mRRules.clear(); updated(); } void Recurrence::clear() { if (d->mRecurReadOnly) { return; } qDeleteAll(d->mRRules); d->mRRules.clear(); qDeleteAll(d->mExRules); d->mExRules.clear(); d->mRDates.clear(); d->mRDateTimes.clear(); d->mExDates.clear(); d->mExDateTimes.clear(); d->mCachedType = rMax; updated(); } void Recurrence::setRecurReadOnly(bool readOnly) { d->mRecurReadOnly = readOnly; } bool Recurrence::recurReadOnly() const { return d->mRecurReadOnly; } QDate Recurrence::startDate() const { return d->mStartDateTime.date(); } void Recurrence::setStartDateTime(const QDateTime &start, bool isAllDay) { if (d->mRecurReadOnly) { return; } d->mStartDateTime = start; setAllDay(isAllDay); // set all RRULEs and EXRULEs int i, end; for (i = 0, end = d->mRRules.count(); i < end; ++i) { d->mRRules[i]->setStartDt(start); } for (i = 0, end = d->mExRules.count(); i < end; ++i) { d->mExRules[i]->setStartDt(start); } updated(); } int Recurrence::frequency() const { RecurrenceRule *rrule = defaultRRuleConst(); return rrule ? rrule->frequency() : 0; } // Emulate the old behaviour. Make this methods just an interface to the // first rrule void Recurrence::setFrequency(int freq) { if (d->mRecurReadOnly || freq <= 0) { return; } RecurrenceRule *rrule = defaultRRule(true); if (rrule) { rrule->setFrequency(freq); } updated(); } // WEEKLY int Recurrence::weekStart() const { RecurrenceRule *rrule = defaultRRuleConst(); return rrule ? rrule->weekStart() : 1; } // Emulate the old behavior QBitArray Recurrence::days() const { QBitArray days(7); days.fill(0); RecurrenceRule *rrule = defaultRRuleConst(); if (rrule) { const QList &bydays = rrule->byDays(); for (int i = 0; i < bydays.size(); ++i) { if (bydays.at(i).pos() == 0) { days.setBit(bydays.at(i).day() - 1); } } } return days; } // MONTHLY // Emulate the old behavior QList Recurrence::monthDays() const { RecurrenceRule *rrule = defaultRRuleConst(); if (rrule) { return rrule->byMonthDays(); } else { return QList(); } } // Emulate the old behavior QList Recurrence::monthPositions() const { RecurrenceRule *rrule = defaultRRuleConst(); return rrule ? rrule->byDays() : QList(); } // YEARLY QList Recurrence::yearDays() const { RecurrenceRule *rrule = defaultRRuleConst(); return rrule ? rrule->byYearDays() : QList(); } QList Recurrence::yearDates() const { return monthDays(); } QList Recurrence::yearMonths() const { RecurrenceRule *rrule = defaultRRuleConst(); return rrule ? rrule->byMonths() : QList(); } QList Recurrence::yearPositions() const { return monthPositions(); } RecurrenceRule *Recurrence::setNewRecurrenceType(RecurrenceRule::PeriodType type, int freq) { if (d->mRecurReadOnly || freq <= 0) { return nullptr; } // Ignore the call if nothing has change if (defaultRRuleConst() && defaultRRuleConst()->recurrenceType() == type && frequency() == freq) { return nullptr; } qDeleteAll(d->mRRules); d->mRRules.clear(); updated(); RecurrenceRule *rrule = defaultRRule(true); if (!rrule) { return nullptr; } rrule->setRecurrenceType(type); rrule->setFrequency(freq); rrule->setDuration(-1); return rrule; } void Recurrence::setMinutely(int _rFreq) { if (setNewRecurrenceType(RecurrenceRule::rMinutely, _rFreq)) { updated(); } } void Recurrence::setHourly(int _rFreq) { if (setNewRecurrenceType(RecurrenceRule::rHourly, _rFreq)) { updated(); } } void Recurrence::setDaily(int _rFreq) { if (setNewRecurrenceType(RecurrenceRule::rDaily, _rFreq)) { updated(); } } void Recurrence::setWeekly(int freq, int weekStart) { RecurrenceRule *rrule = setNewRecurrenceType(RecurrenceRule::rWeekly, freq); if (!rrule) { return; } rrule->setWeekStart(weekStart); updated(); } void Recurrence::setWeekly(int freq, const QBitArray &days, int weekStart) { setWeekly(freq, weekStart); addMonthlyPos(0, days); } void Recurrence::addWeeklyDays(const QBitArray &days) { addMonthlyPos(0, days); } void Recurrence::setMonthly(int freq) { if (setNewRecurrenceType(RecurrenceRule::rMonthly, freq)) { updated(); } } void Recurrence::addMonthlyPos(short pos, const QBitArray &days) { // Allow 53 for yearly! if (d->mRecurReadOnly || pos > 53 || pos < -53) { return; } RecurrenceRule *rrule = defaultRRule(false); if (!rrule) { return; } bool changed = false; QList positions = rrule->byDays(); for (int i = 0; i < 7; ++i) { if (days.testBit(i)) { RecurrenceRule::WDayPos p(pos, i + 1); if (!positions.contains(p)) { changed = true; positions.append(p); } } } if (changed) { rrule->setByDays(positions); updated(); } } void Recurrence::addMonthlyPos(short pos, ushort day) { // Allow 53 for yearly! if (d->mRecurReadOnly || pos > 53 || pos < -53) { return; } RecurrenceRule *rrule = defaultRRule(false); if (!rrule) { return; } QList positions = rrule->byDays(); RecurrenceRule::WDayPos p(pos, day); if (!positions.contains(p)) { positions.append(p); setMonthlyPos(positions); } } void Recurrence::setMonthlyPos(const QList &monthlyDays) { if (d->mRecurReadOnly) { return; } RecurrenceRule *rrule = defaultRRule(true); if (!rrule) { return; } //TODO: sort lists // the position inside the list has no meaning, so sort the list before testing if it changed if (monthlyDays != rrule->byDays()) { rrule->setByDays(monthlyDays); updated(); } } void Recurrence::addMonthlyDate(short day) { if (d->mRecurReadOnly || day > 31 || day < -31) { return; } RecurrenceRule *rrule = defaultRRule(true); if (!rrule) { return; } QList monthDays = rrule->byMonthDays(); if (!monthDays.contains(day)) { monthDays.append(day); setMonthlyDate(monthDays); } } void Recurrence::setMonthlyDate(const QList< int > &monthlyDays) { if (d->mRecurReadOnly) { return; } RecurrenceRule *rrule = defaultRRule(true); if (!rrule) { return; } QList mD(monthlyDays); QList rbD(rrule->byMonthDays()); sortAndRemoveDuplicates(mD); sortAndRemoveDuplicates(rbD); if (mD != rbD) { rrule->setByMonthDays(monthlyDays); updated(); } } void Recurrence::setYearly(int freq) { if (setNewRecurrenceType(RecurrenceRule::rYearly, freq)) { updated(); } } // Daynumber within year void Recurrence::addYearlyDay(int day) { RecurrenceRule *rrule = defaultRRule(false); // It must already exist! if (!rrule) { return; } QList days = rrule->byYearDays(); if (!days.contains(day)) { days << day; setYearlyDay(days); } } void Recurrence::setYearlyDay(const QList &days) { RecurrenceRule *rrule = defaultRRule(false); // It must already exist! if (!rrule) { return; } QList d(days); QList bYD(rrule->byYearDays()); sortAndRemoveDuplicates(d); sortAndRemoveDuplicates(bYD); if (d != bYD) { rrule->setByYearDays(days); updated(); } } // day part of date within year void Recurrence::addYearlyDate(int day) { addMonthlyDate(day); } void Recurrence::setYearlyDate(const QList &dates) { setMonthlyDate(dates); } // day part of date within year, given as position (n-th weekday) void Recurrence::addYearlyPos(short pos, const QBitArray &days) { addMonthlyPos(pos, days); } void Recurrence::setYearlyPos(const QList &days) { setMonthlyPos(days); } // month part of date within year void Recurrence::addYearlyMonth(short month) { if (d->mRecurReadOnly || month < 1 || month > 12) { return; } RecurrenceRule *rrule = defaultRRule(false); if (!rrule) { return; } QList months = rrule->byMonths(); if (!months.contains(month)) { months << month; setYearlyMonth(months); } } void Recurrence::setYearlyMonth(const QList &months) { if (d->mRecurReadOnly) { return; } RecurrenceRule *rrule = defaultRRule(false); if (!rrule) { return; } QList m(months); QList bM(rrule->byMonths()); sortAndRemoveDuplicates(m); sortAndRemoveDuplicates(bM); if (m != bM) { rrule->setByMonths(months); updated(); } } TimeList Recurrence::recurTimesOn(const QDate &date, const QTimeZone &timeZone) const { // qCDebug(KCALCORE_LOG) << "recurTimesOn(" << date << ")"; int i, end; TimeList times; // The whole day is excepted if (std::binary_search(d->mExDates.constBegin(), d->mExDates.constEnd(), date)) { return times; } // EXRULE takes precedence over RDATE entries, so for all-day events, // a matching excule also excludes the whole day automatically if (allDay()) { for (i = 0, end = d->mExRules.count(); i < end; ++i) { if (d->mExRules[i]->recursOn(date, timeZone)) { return times; } } } QDateTime dt = startDateTime().toTimeZone(timeZone); if (dt.date() == date) { times << dt.time(); } bool foundDate = false; for (i = 0, end = d->mRDateTimes.count(); i < end; ++i) { dt = d->mRDateTimes[i].toTimeZone(timeZone); if (dt.date() == date) { times << dt.time(); foundDate = true; } else if (foundDate) { break; // <= Assume that the rdatetime list is sorted } } for (i = 0, end = d->mRRules.count(); i < end; ++i) { times += d->mRRules[i]->recurTimesOn(date, timeZone); } sortAndRemoveDuplicates(times); foundDate = false; TimeList extimes; for (i = 0, end = d->mExDateTimes.count(); i < end; ++i) { dt = d->mExDateTimes[i].toTimeZone(timeZone); if (dt.date() == date) { extimes << dt.time(); foundDate = true; } else if (foundDate) { break; } } if (!allDay()) { // we have already checked all-day times above for (i = 0, end = d->mExRules.count(); i < end; ++i) { extimes += d->mExRules[i]->recurTimesOn(date, timeZone); } } sortAndRemoveDuplicates(extimes); inplaceSetDifference(times, extimes); return times; } QList Recurrence::timesInInterval(const QDateTime &start, const QDateTime &end) const { int i, count; QList times; for (i = 0, count = d->mRRules.count(); i < count; ++i) { times += d->mRRules[i]->timesInInterval(start, end); } // add rdatetimes that fit in the interval for (i = 0, count = d->mRDateTimes.count(); i < count; ++i) { if (d->mRDateTimes[i] >= start && d->mRDateTimes[i] <= end) { times += d->mRDateTimes[i]; } } // add rdates that fit in the interval QDateTime kdt = d->mStartDateTime; for (i = 0, count = d->mRDates.count(); i < count; ++i) { kdt.setDate(d->mRDates[i]); if (kdt >= start && kdt <= end) { times += kdt; } } // Recurrence::timesInInterval(...) doesn't explicitly add mStartDateTime to the list // of times to be returned. It calls mRRules[i]->timesInInterval(...) which include // mStartDateTime. // So, If we have rdates/rdatetimes but don't have any rrule we must explicitly // add mStartDateTime to the list, otherwise we won't see the first occurrence. if ((!d->mRDates.isEmpty() || !d->mRDateTimes.isEmpty()) && d->mRRules.isEmpty() && start <= d->mStartDateTime && end >= d->mStartDateTime) { times += d->mStartDateTime; } sortAndRemoveDuplicates(times); // Remove excluded times int idt = 0; int enddt = times.count(); for (i = 0, count = d->mExDates.count(); i < count && idt < enddt; ++i) { while (idt < enddt && times[idt].date() < d->mExDates[i]) { ++idt; } while (idt < enddt && times[idt].date() == d->mExDates[i]) { times.removeAt(idt); --enddt; } } QList extimes; for (i = 0, count = d->mExRules.count(); i < count; ++i) { extimes += d->mExRules[i]->timesInInterval(start, end); } extimes += d->mExDateTimes; sortAndRemoveDuplicates(extimes); inplaceSetDifference(times, extimes); return times; } QDateTime Recurrence::getNextDateTime(const QDateTime &preDateTime) const { QDateTime nextDT = preDateTime; // prevent infinite loops, e.g. when an exrule extinguishes an rrule (e.g. // the exrule is identical to the rrule). If an occurrence is found, break // out of the loop by returning that QDateTime // TODO_Recurrence: Is a loop counter of 1000 really okay? I mean for secondly // recurrence, an exdate might exclude more than 1000 intervals! int loop = 0; while (loop < 1000) { // Outline of the algo: // 1) Find the next date/time after preDateTime when the event could recur // 1.0) Add the start date if it's after preDateTime // 1.1) Use the next occurrence from the explicit RDATE lists // 1.2) Add the next recurrence for each of the RRULEs // 2) Take the earliest recurrence of these = QDateTime nextDT // 3) If that date/time is not excluded, either explicitly by an EXDATE or // by an EXRULE, return nextDT as the next date/time of the recurrence // 4) If it's excluded, start all at 1), but starting at nextDT (instead // of preDateTime). Loop at most 1000 times. ++loop; // First, get the next recurrence from the RDate lists QList dates; if (nextDT < startDateTime()) { dates << startDateTime(); } // Assume that the rdatetime list is sorted const auto it = std::upper_bound(d->mRDateTimes.constBegin(), d->mRDateTimes.constEnd(), nextDT); if (it != d->mRDateTimes.constEnd()) { dates << *it; } QDateTime kdt(startDateTime()); for (const auto &date : qAsConst(d->mRDates)) { kdt.setDate(date); if (kdt > nextDT) { dates << kdt; break; } } // Add the next occurrences from all RRULEs. for (const auto &rule : qAsConst(d->mRRules)) { QDateTime dt = rule->getNextDate(nextDT); if (dt.isValid()) { dates << dt; } } // Take the first of these (all others can't be used later on) sortAndRemoveDuplicates(dates); if (dates.isEmpty()) { return QDateTime(); } nextDT = dates.first(); // Check if that date/time is excluded explicitly or by an exrule: if (!std::binary_search(d->mExDates.constBegin(), d->mExDates.constEnd(), nextDT.date()) && !std::binary_search(d->mExDateTimes.constBegin(), d->mExDateTimes.constEnd(), nextDT)) { bool allowed = true; for (const auto &rule : qAsConst(d->mExRules)) { allowed = allowed && !rule->recursAt(nextDT); } if (allowed) { return nextDT; } } } // Couldn't find a valid occurrences in 1000 loops, something is wrong! return QDateTime(); } QDateTime Recurrence::getPreviousDateTime(const QDateTime &afterDateTime) const { QDateTime prevDT = afterDateTime; // prevent infinite loops, e.g. when an exrule extinguishes an rrule (e.g. // the exrule is identical to the rrule). If an occurrence is found, break // out of the loop by returning that QDateTime int loop = 0; while (loop < 1000) { // Outline of the algo: // 1) Find the next date/time after preDateTime when the event could recur // 1.1) Use the next occurrence from the explicit RDATE lists // 1.2) Add the next recurrence for each of the RRULEs // 2) Take the earliest recurrence of these = QDateTime nextDT // 3) If that date/time is not excluded, either explicitly by an EXDATE or // by an EXRULE, return nextDT as the next date/time of the recurrence // 4) If it's excluded, start all at 1), but starting at nextDT (instead // of preDateTime). Loop at most 1000 times. ++loop; // First, get the next recurrence from the RDate lists QList dates; if (prevDT > startDateTime()) { dates << startDateTime(); } const auto it = strictLowerBound(d->mRDateTimes.constBegin(), d->mRDateTimes.constEnd(), prevDT); if (it != d->mRDateTimes.constEnd()) { dates << *it; } QDateTime kdt(startDateTime()); for (const auto &date : qAsConst(d->mRDates)) { kdt.setDate(date); if (kdt < prevDT) { dates << kdt; break; } } // Add the previous occurrences from all RRULEs. for (const auto &rule : qAsConst(d->mRRules)) { QDateTime dt = rule->getPreviousDate(prevDT); if (dt.isValid()) { dates << dt; } } // Take the last of these (all others can't be used later on) sortAndRemoveDuplicates(dates); if (dates.isEmpty()) { return QDateTime(); } prevDT = dates.last(); // Check if that date/time is excluded explicitly or by an exrule: if (!std::binary_search(d->mExDates.constBegin(), d->mExDates.constEnd(), prevDT.date()) && !std::binary_search(d->mExDateTimes.constBegin(), d->mExDateTimes.constEnd(), prevDT)) { bool allowed = true; for (const auto &rule : qAsConst(d->mExRules)) { allowed = allowed && !rule->recursAt(prevDT); } if (allowed) { return prevDT; } } } // Couldn't find a valid occurrences in 1000 loops, something is wrong! return QDateTime(); } /***************************** PROTECTED FUNCTIONS ***************************/ RecurrenceRule::List Recurrence::rRules() const { return d->mRRules; } void Recurrence::addRRule(RecurrenceRule *rrule) { if (d->mRecurReadOnly || !rrule) { return; } rrule->setAllDay(d->mAllDay); d->mRRules.append(rrule); rrule->addObserver(this); updated(); } void Recurrence::removeRRule(RecurrenceRule *rrule) { if (d->mRecurReadOnly) { return; } d->mRRules.removeAll(rrule); rrule->removeObserver(this); updated(); } void Recurrence::deleteRRule(RecurrenceRule *rrule) { if (d->mRecurReadOnly) { return; } d->mRRules.removeAll(rrule); delete rrule; updated(); } RecurrenceRule::List Recurrence::exRules() const { return d->mExRules; } void Recurrence::addExRule(RecurrenceRule *exrule) { if (d->mRecurReadOnly || !exrule) { return; } exrule->setAllDay(d->mAllDay); d->mExRules.append(exrule); exrule->addObserver(this); updated(); } void Recurrence::removeExRule(RecurrenceRule *exrule) { if (d->mRecurReadOnly) { return; } d->mExRules.removeAll(exrule); exrule->removeObserver(this); updated(); } void Recurrence::deleteExRule(RecurrenceRule *exrule) { if (d->mRecurReadOnly) { return; } d->mExRules.removeAll(exrule); delete exrule; updated(); } QList Recurrence::rDateTimes() const { return d->mRDateTimes; } void Recurrence::setRDateTimes(const QList &rdates) { if (d->mRecurReadOnly) { return; } d->mRDateTimes = rdates; sortAndRemoveDuplicates(d->mRDateTimes); updated(); } void Recurrence::addRDateTime(const QDateTime &rdate) { if (d->mRecurReadOnly) { return; } setInsert(d->mRDateTimes, rdate); updated(); } DateList Recurrence::rDates() const { return d->mRDates; } void Recurrence::setRDates(const DateList &rdates) { if (d->mRecurReadOnly) { return; } d->mRDates = rdates; sortAndRemoveDuplicates(d->mRDates); updated(); } void Recurrence::addRDate(const QDate &rdate) { if (d->mRecurReadOnly) { return; } setInsert(d->mRDates, rdate); updated(); } QList Recurrence::exDateTimes() const { return d->mExDateTimes; } void Recurrence::setExDateTimes(const QList &exdates) { if (d->mRecurReadOnly) { return; } d->mExDateTimes = exdates; sortAndRemoveDuplicates(d->mExDateTimes); } void Recurrence::addExDateTime(const QDateTime &exdate) { if (d->mRecurReadOnly) { return; } setInsert(d->mExDateTimes, exdate); updated(); } DateList Recurrence::exDates() const { return d->mExDates; } void Recurrence::setExDates(const DateList &exdates) { if (d->mRecurReadOnly) { return; } DateList l = exdates; sortAndRemoveDuplicates(l); if (d->mExDates != l) { d->mExDates = l; updated(); } } void Recurrence::addExDate(const QDate &exdate) { if (d->mRecurReadOnly) { return; } setInsert(d->mExDates, exdate); updated(); } void Recurrence::recurrenceChanged(RecurrenceRule *) { updated(); } // %%%%%%%%%%%%%%%%%% end:Recurrencerule %%%%%%%%%%%%%%%%%% void Recurrence::dump() const { int i; int count = d->mRRules.count(); qCDebug(KCALCORE_LOG) << " -)" << count << "RRULEs:"; for (i = 0; i < count; ++i) { qCDebug(KCALCORE_LOG) << " -) RecurrenceRule: "; d->mRRules[i]->dump(); } count = d->mExRules.count(); qCDebug(KCALCORE_LOG) << " -)" << count << "EXRULEs:"; for (i = 0; i < count; ++i) { qCDebug(KCALCORE_LOG) << " -) ExceptionRule :"; d->mExRules[i]->dump(); } count = d->mRDates.count(); qCDebug(KCALCORE_LOG) << " -)" << count << "Recurrence Dates:"; for (i = 0; i < count; ++i) { qCDebug(KCALCORE_LOG) << " " << d->mRDates[i]; } count = d->mRDateTimes.count(); qCDebug(KCALCORE_LOG) << " -)" << count << "Recurrence Date/Times:"; for (i = 0; i < count; ++i) { qCDebug(KCALCORE_LOG) << " " << d->mRDateTimes[i]; } count = d->mExDates.count(); qCDebug(KCALCORE_LOG) << " -)" << count << "Exceptions Dates:"; for (i = 0; i < count; ++i) { qCDebug(KCALCORE_LOG) << " " << d->mExDates[i]; } count = d->mExDateTimes.count(); qCDebug(KCALCORE_LOG) << " -)" << count << "Exception Date/Times:"; for (i = 0; i < count; ++i) { qCDebug(KCALCORE_LOG) << " " << d->mExDateTimes[i]; } } Recurrence::RecurrenceObserver::~RecurrenceObserver() { } KCALENDARCORE_EXPORT QDataStream &KCalendarCore::operator<<(QDataStream &out, KCalendarCore::Recurrence *r) { if (!r) { return out; } serializeQDateTimeList(out, r->d->mRDateTimes); serializeQDateTimeList(out, r->d->mExDateTimes); out << r->d->mRDates; serializeQDateTimeAsKDateTime(out, r->d->mStartDateTime); out << r->d->mCachedType << r->d->mAllDay << r->d->mRecurReadOnly << r->d->mExDates << r->d->mExRules.count() << r->d->mRRules.count(); for (RecurrenceRule *rule : qAsConst(r->d->mExRules)) { out << rule; } for (RecurrenceRule *rule : qAsConst(r->d->mRRules)) { out << rule; } return out; } KCALENDARCORE_EXPORT QDataStream &KCalendarCore::operator>>(QDataStream &in, KCalendarCore::Recurrence *r) { if (!r) { return in; } int rruleCount, exruleCount; deserializeQDateTimeList(in, r->d->mRDateTimes); deserializeQDateTimeList(in, r->d->mExDateTimes); in >> r->d->mRDates; deserializeKDateTimeAsQDateTime(in, r->d->mStartDateTime); in >> r->d->mCachedType >> r->d->mAllDay >> r->d->mRecurReadOnly >> r->d->mExDates >> exruleCount >> rruleCount; r->d->mExRules.clear(); r->d->mRRules.clear(); for (int i = 0; i < exruleCount; ++i) { RecurrenceRule *rule = new RecurrenceRule(); rule->addObserver(r); in >> rule; r->d->mExRules.append(rule); } for (int i = 0; i < rruleCount; ++i) { RecurrenceRule *rule = new RecurrenceRule(); rule->addObserver(r); in >> rule; r->d->mRRules.append(rule); } return in; } diff --git a/src/recurrence.h b/src/recurrence.h index 35336b984..e6dff9317 100644 --- a/src/recurrence.h +++ b/src/recurrence.h @@ -1,680 +1,685 @@ /* This file is part of the kcalcore library. Copyright (c) 1998 Preston Brown Copyright (c) 2001,2003 Cornelius Schumacher Copyright (c) 2002,2006 David Jarvie Copyright (C) 2005 Reinhold Kainhofer This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef KCALCORE_RECURRENCE_H #define KCALCORE_RECURRENCE_H #include "kcalendarcore_export.h" #include "recurrencerule.h" class QBitArray; class QTimeZone; namespace KCalendarCore { class RecurrenceRule; /** This class represents a recurrence rule for a calendar incidence. It manages all recurrence rules, recurrence date/times, exception rules and exception date times that can appear inside calendar items. Each recurrence rule and exception rule is represented as an object of type RecurrenceRule. For the simple case where at most one recurrence rule is present, this class provides shortcut methods to set the type: setMinutely() setHourly() setDaily() setWeekly() setMonthly() setYearly() to set/get general information about the recurrence: setEndDate() setEndDateTime() duration() durationTo() setDuration() frequency() setFrequency() and to set/get specific information about the recurrence within the interval: days() monthDays() monthPositions() yearDays() yearDates() yearMonths() yearPositions() addMonthlyPos() addMonthlyDate() addYearlyDay() addYearlyDate() addYearlyPos() addYearlyMonth() These are all available so that you don't have to work on the RecurrenceRule objects themselves. In other words, in that simple situation the interface stays almost the same compared to the old Recurrence class, which allowed only one recurrence rule. As soon as your recurrence consists of multiple recurrence rules or exception rules, you cannot use the methods mentioned above any more (since each rule will have a different type and different settings). If you still call any of them, the set*ly methods will remove all rules and add one rule with the specified type. The add* and the other set* methods will change only the first recurrence rule, but leave the others untouched. */ class KCALENDARCORE_EXPORT Recurrence : public RecurrenceRule::RuleObserver { public: class RecurrenceObserver { public: virtual ~RecurrenceObserver(); /** This method will be called on each change of the recurrence object */ virtual void recurrenceUpdated(Recurrence *r) = 0; }; /** enumeration for describing how an event recurs, if at all. */ enum { rNone = 0, rMinutely = 0x001, rHourly = 0x0002, rDaily = 0x0003, rWeekly = 0x0004, rMonthlyPos = 0x0005, rMonthlyDay = 0x0006, rYearlyMonth = 0x0007, rYearlyDay = 0x0008, rYearlyPos = 0x0009, rOther = 0x000A, rMax = 0x00FF }; /** Constructs an empty recurrence. */ Recurrence(); /** Copy constructor. @param r instance to copy from */ Recurrence(const Recurrence &r); /** Destructor. */ ~Recurrence() override; /** Comparison operator for equality. @param r instance to compare with @return true if recurrences are the same, false otherwise */ bool operator==(const Recurrence &r) const; /** Comparison operator for inequality. @param r instance to compare with @return true if recurrences are the different, false if the same */ bool operator!=(const Recurrence &r) const { return !operator==(r); } +#if KCALENDARCORE_BUILD_DEPRECATED_SINCE(5, 64) /** Assignment operator. @param r the recurrence which will be assigned to this. @deprecated Do not use, will be removed in KF6 @warning Broken implementation, do not use! */ - Recurrence &operator=(const Recurrence &r); // TODO KF6 mark as deleted + KCALENDARCORE_DEPRECATED_VERSION(5, 64, "Do not use") + Recurrence &operator=(const Recurrence &r); +#else + Recurrence &operator=(const Recurrence &r) = delete; +#endif /** Return the start date/time of the recurrence (Time for all-day recurrences will be 0:00). @return the current start/time of the recurrence. */ Q_REQUIRED_RESULT QDateTime startDateTime() const; /** Return the start date/time of the recurrence */ Q_REQUIRED_RESULT QDate startDate() const; /** Set start of recurrence. If @p start is date-only, the recurrence is set to all-day. Otherwise, the start is set to a date and time, and the recurrence is set to non-all-day. @param start the new start date or date/time of the recurrence. */ void setStartDateTime(const QDateTime &start, bool isAllDay); /** Set whether the recurrence has no time, just a date. * All-day means -- according to rfc2445 -- that the event has no time * associated. * N.B. This property is derived by default from whether setStartDateTime() is * called with a date-only or date/time parameter. * @return whether the recurrence has a time (false) or it is just a date (true). */ Q_REQUIRED_RESULT bool allDay() const; /** Sets whether the dtstart is a all-day (i.e. has no time attached) @param allDay If the recurrence is for all-day item (true) or has a time associated (false). */ void setAllDay(bool allDay); /** Set if recurrence is read-only or can be changed. */ void setRecurReadOnly(bool readOnly); /** Returns true if the recurrence is read-only, or false if it can be changed. */ Q_REQUIRED_RESULT bool recurReadOnly() const; /** Returns whether the event recurs at all. */ Q_REQUIRED_RESULT bool recurs() const; /** Returns the event's recurrence status. See the enumeration at the top * of this file for possible values. */ Q_REQUIRED_RESULT ushort recurrenceType() const; /** Returns the recurrence status for a recurrence rule. * See the enumeration at the top of this file for possible values. * * @param rrule the recurrence rule to get the type for */ static ushort recurrenceType(const RecurrenceRule *rrule); /** Returns true if the date specified is one on which the event will recur. @param date date to check. @param timeZone time zone for the @p date. */ bool recursOn(const QDate &date, const QTimeZone &timeZone) const; /** Returns true if the date/time specified is one at which the event will recur. Times are rounded down to the nearest minute to determine the result. @param dt is the date/time to check. */ bool recursAt(const QDateTime &dt) const; /** Removes all recurrence rules. Recurrence dates and exceptions are not removed. */ void unsetRecurs(); /** Removes all recurrence and exception rules and dates. */ void clear(); /** Returns a list of the times on the specified date at which the * recurrence will occur. The returned times should be interpreted in the * context of @p timeSpec. * @param date the date for which to find the recurrence times * @param timeZone timezone for @p date */ Q_REQUIRED_RESULT TimeList recurTimesOn(const QDate &date, const QTimeZone &timeZone) const; /** Returns a list of all the times at which the recurrence will occur * between two specified times. * * There is a (large) maximum limit to the number of times returned. If due to * this limit the list is incomplete, this is indicated by the last entry being * set to an invalid QDateTime value. If you need further values, call the * method again with a start time set to just after the last valid time returned. * * @param start inclusive start of interval * @param end inclusive end of interval * @return list of date/time values */ Q_REQUIRED_RESULT QList timesInInterval(const QDateTime &start, const QDateTime &end) const; /** Returns the date and time of the next recurrence, after the specified date/time. * If the recurrence has no time, the next date after the specified date is returned. * @param preDateTime the date/time after which to find the recurrence. * @return date/time of next recurrence (strictly later than the given * QDateTime), or invalid date if none. */ Q_REQUIRED_RESULT QDateTime getNextDateTime(const QDateTime &preDateTime) const; /** Returns the date and time of the last previous recurrence, before the specified date/time. * If a time later than 00:00:00 is specified and the recurrence has no time, 00:00:00 on * the specified date is returned if that date recurs. * * @param afterDateTime the date/time before which to find the recurrence. * @return date/time of previous recurrence (strictly earlier than the given * QDateTime), or invalid date if none. */ Q_REQUIRED_RESULT QDateTime getPreviousDateTime(const QDateTime &afterDateTime) const; /** Returns frequency of recurrence, in terms of the recurrence time period type. */ Q_REQUIRED_RESULT int frequency() const; /** Sets the frequency of recurrence, in terms of the recurrence time period type. */ void setFrequency(int freq); /** * Returns -1 if the event recurs infinitely, 0 if the end date is set, * otherwise the total number of recurrences, including the initial occurrence. */ Q_REQUIRED_RESULT int duration() const; /** Sets the total number of times the event is to occur, including both the * first and last. */ void setDuration(int duration); /** Returns the number of recurrences up to and including the date/time specified. * @warning This function can be very time consuming - use it sparingly! */ Q_REQUIRED_RESULT int durationTo(const QDateTime &dt) const; /** Returns the number of recurrences up to and including the date specified. * @warning This function can be very time consuming - use it sparingly! */ Q_REQUIRED_RESULT int durationTo(const QDate &date) const; /** Returns the date/time of the last recurrence. * An invalid date is returned if the recurrence has no end. */ Q_REQUIRED_RESULT QDateTime endDateTime() const; /** Returns the date of the last recurrence. * An invalid date is returned if the recurrence has no end. */ Q_REQUIRED_RESULT QDate endDate() const; /** Sets the date of the last recurrence. The end time is set to the recurrence start time. * @param endDate the ending date after which to stop recurring. If the * recurrence is not all-day, the end time will be 23:59.*/ void setEndDate(const QDate &endDate); /** Sets the date and time of the last recurrence. * @param endDateTime the ending date/time after which to stop recurring. */ void setEndDateTime(const QDateTime &endDateTime); /** Shift the times of the recurrence so that they appear at the same clock time as before but in a new time zone. The shift is done from a viewing time zone rather than from the actual recurrence time zone. For example, shifting a recurrence whose start time is 09:00 America/New York, using an old viewing time zone (@p oldSpec) of Europe/London, to a new time zone (@p newSpec) of Europe/Paris, will result in the time being shifted from 14:00 (which is the London time of the recurrence start) to 14:00 Paris time. @param oldZone the time specification which provides the clock times @param newZone the new time specification */ void shiftTimes(const QTimeZone &oldZone, const QTimeZone &newZone); /** Sets an event to recur minutely. By default infinite recurrence is used. To set an end date use the method setEndDate and to set the number of occurrences use setDuration. This method clears all recurrence rules and adds one rule with a minutely recurrence. All other recurrence components (recurrence date/times, exception date/times and exception rules) are not modified. * @param freq the frequency to recur, e.g. 2 is every other minute */ void setMinutely(int freq); /** Sets an event to recur hourly. By default infinite recurrence is used. The minute of the recurrence is taken from the start date (if you need to change it, you will have to modify the defaultRRule's byMinute list manually. To set an end date use the method setEndDate and to set the number of occurrences use setDuration. This method clears all recurrence rules and adds one rule with a hourly recurrence. All other recurrence components (recurrence date/times, exception date/times and exception rules) are not modified. * @param freq the frequency to recur, e.g. 2 is every other hour */ void setHourly(int freq); /** Sets an event to recur daily. By default infinite recurrence is used. The minute and second of the recurrence is taken from the start date (if you need to change them, you will have to modify the defaultRRule's byMinute list manually. To set an end date use the method setEndDate and to set the number of occurrences use setDuration. This method clears all recurrence rules and adds one rule with a daily recurrence. All other recurrence components (recurrence date/times, exception date/times and exception rules) are not modified. * @param freq the frequency to recur, e.g. 2 is every other day */ void setDaily(int freq); /** Sets an event to recur weekly. By default infinite recurrence is used. To set an end date use the method setEndDate and to set the number of occurrences use setDuration. This method clears all recurrence rules and adds one rule with a weekly recurrence. All other recurrence components (recurrence date/times, exception date/times and exception rules) are not modified. * @param freq the frequency to recur, e.g. every other week etc. * @param weekStart the first day of the week (Monday=1 .. Sunday=7, default is Monday). */ void setWeekly(int freq, int weekStart = 1); /** Sets an event to recur weekly. By default infinite recurrence is used. To set an end date use the method setEndDate and to set the number of occurrences use setDuration. This method clears all recurrence rules and adds one rule with a weekly recurrence. All other recurrence components (recurrence date/times, exception date/times and exception rules) are not modified. * @param freq the frequency to recur, e.g. every other week etc. * @param days a 7 bit array indicating which days on which to recur (bit 0 = Monday). * @param weekStart the first day of the week (Monday=1 .. Sunday=7, default is Monday). */ void setWeekly(int freq, const QBitArray &days, int weekStart = 1); /** Adds days to the weekly day recurrence list. * @param days a 7 bit array indicating which days on which to recur (bit 0 = Monday). */ void addWeeklyDays(const QBitArray &days); /** Returns the first day of the week. Uses only the * first RRULE if present (i.e. a second RRULE as well as all EXRULES are * ignored! * @return Weekday of the first day of the week (Monday=1 .. Sunday=7) */ int weekStart() const; /** Returns week day mask (bit 0 = Monday). */ Q_REQUIRED_RESULT QBitArray days() const; // Emulate the old behavior /** Sets an event to recur monthly. By default infinite recurrence is used. The date of the monthly recurrence will be taken from the start date unless you explicitly add one or more recurrence dates with addMonthlyDate or a recurrence position in the month (e.g. first monday) using addMonthlyPos. To set an end date use the method setEndDate and to set the number of occurrences use setDuration. This method clears all recurrence rules and adds one rule with a monthly recurrence. All other recurrence components (recurrence date/times, exception date/times and exception rules) are not modified. * @param freq the frequency to recur, e.g. 3 for every third month. */ void setMonthly(int freq); /** Adds a position (e.g. first monday) to the monthly recurrence rule. * @param pos the position in the month for the recurrence, with valid * values being 1-5 (5 weeks max in a month). * @param days the days for the position to recur on (bit 0 = Monday). * Example: pos = 2, and bits 0 and 2 are set in days: * the rule is to repeat every 2nd Monday and Wednesday in the month. */ void addMonthlyPos(short pos, const QBitArray &days); void addMonthlyPos(short pos, ushort day); void setMonthlyPos(const QList &monthlyDays); /** Adds a date (e.g. the 15th of each month) to the monthly day * recurrence list. * @param day the date in the month to recur. */ void addMonthlyDate(short day); void setMonthlyDate(const QList &monthlyDays); /** Returns list of day positions in months. */ Q_REQUIRED_RESULT QList monthPositions() const; /** Returns list of day numbers of a month. */ // Emulate old behavior Q_REQUIRED_RESULT QList monthDays() const; /** Sets an event to recur yearly. By default, this will recur every year * on the same date (e.g. every year on April 15 if the start date was * April 15). * The day of the year can be specified with addYearlyDay(). * The day of the month can be specified with addYearlyByDate * If both a month and a day ar specified with addYearlyMonth and * addYearlyDay, the day is understood as day number within the month. * * A position (e.g. 3rd Sunday of year/month, or last Friday of year/month) * can be specified with addYearlyPos. Again, if a month is specified, * this position is understood as within that month, otherwise within * the year. * * By default infinite recurrence is used. To set an end date use the * method setEndDate and to set the number of occurrences use setDuration. This method clears all recurrence rules and adds one rule with a yearly recurrence. All other recurrence components (recurrence date/times, exception date/times and exception rules) are not modified. * @param freq the frequency to recur, e.g. 3 for every third year. */ void setYearly(int freq); /** Adds day number of year within a yearly recurrence. * By default infinite recurrence is used. To set an end date use the * method setEndDate and to set the number of occurrences use setDuration. * @param day the day of the year for the event. E.g. if day is 60, this * means Feb 29 in leap years and March 1 in non-leap years. */ void addYearlyDay(int day); void setYearlyDay(const QList &days); /** Adds date within a yearly recurrence. The month(s) for the recurrence * can be specified with addYearlyMonth(), otherwise the month of the * start date is used. * * By default infinite recurrence is used. To set an end date use the * method setEndDate and to set the number of occurrences use setDuration. * @param date the day of the month for the event */ void addYearlyDate(int date); void setYearlyDate(const QList &dates); /** Adds month in yearly recurrence. You can specify specific day numbers * within the months (by calling addYearlyDate()) or specific day positions * within the month (by calling addYearlyPos). * @param _rNum the month in which the event shall recur. */ void addYearlyMonth(short _rNum); void setYearlyMonth(const QList< int > &months); /** Adds position within month/year within a yearly recurrence. If months * are specified (via addYearlyMonth()), the parameters are understood as * position within these months, otherwise within the year. * * By default infinite recurrence is used. * To set an end date use the method setEndDate and to set the number * of occurrences use setDuration. * @param pos the position in the month/year for the recurrence, with valid * values being 1 to 53 and -1 to -53 (53 weeks max in a year). * @param days the days for the position to recur on (bit 0 = Monday). * Example: pos = 2, and bits 0 and 2 are set in days * If months are specified (via addYearlyMonth), e.g. March, the rule is * to repeat every year on the 2nd Monday and Wednesday of March. * If no months are specified, the fule is to repeat every year on the * 2nd Monday and Wednesday of the year. */ void addYearlyPos(short pos, const QBitArray &days); void setYearlyPos(const QList &days); /** Returns the day numbers within a yearly recurrence. * @return the days of the year for the event. E.g. if the list contains * 60, this means the recurrence happens on day 60 of the year, i.e. * on Feb 29 in leap years and March 1 in non-leap years. */ Q_REQUIRED_RESULT QList yearDays() const; /** Returns the dates within a yearly recurrence. * @return the days of the month for the event. E.g. if the list contains * 13, this means the recurrence happens on the 13th of the month. * The months for the recurrence can be obtained through * yearlyMonths(). If this list is empty, the month of the start * date is used. */ Q_REQUIRED_RESULT QList yearDates() const; /** Returns the months within a yearly recurrence. * @return the months for the event. E.g. if the list contains * 11, this means the recurrence happens in November. * The days for the recurrence can be obtained either through * yearDates() if they are given as dates within the month or * through yearlyPositions() if they are given as positions within the * month. If none is specified, the date of the start date is used. */ Q_REQUIRED_RESULT QList yearMonths() const; /** Returns the positions within a yearly recurrence. * @return the positions for the event, either within a month (if months * are set through addYearlyMonth()) or within the year. * E.g. if the list contains {Pos=3, Day=5}, this means the third * friday. If a month is set this position is understoodas third * Friday in the given months, otherwise as third Friday of the * year. */ /** Returns list of day positions in months, for a recursYearlyPos recurrence rule. */ Q_REQUIRED_RESULT QList yearPositions() const; /** Upper date limit for recurrences */ static const QDate MAX_DATE; /** Debug output. */ void dump() const; // RRULE Q_REQUIRED_RESULT RecurrenceRule::List rRules() const; /** Add a recurrence rule to the recurrence. @param rrule the recurrence rule to add */ void addRRule(RecurrenceRule *rrule); /** Remove a recurrence rule from the recurrence. @p rrule is not deleted; it is the responsibility of the caller to ensure that it is deleted. @param rrule the recurrence rule to remove */ void removeRRule(RecurrenceRule *rrule); /** Remove a recurrence rule from the recurrence and delete it. @param rrule the recurrence rule to remove */ void deleteRRule(RecurrenceRule *rrule); // EXRULE Q_REQUIRED_RESULT RecurrenceRule::List exRules() const; /** Add an exception rule to the recurrence. @param exrule the exception rule to add */ void addExRule(RecurrenceRule *exrule); /** Remove an exception rule from the recurrence. @p exrule is not deleted; it is the responsibility of the caller to ensure that it is deleted. @param exrule the exception rule to remove */ void removeExRule(RecurrenceRule *exrule); /** Remove an exception rule from the recurrence and delete it. @param exrule the exception rule to remove */ void deleteExRule(RecurrenceRule *exrule); // RDATE Q_REQUIRED_RESULT QList rDateTimes() const; Q_REQUIRED_RESULT DateList rDates() const; void setRDateTimes(const QList &rdates); void setRDates(const DateList &rdates); void addRDateTime(const QDateTime &rdate); void addRDate(const QDate &rdate); // ExDATE Q_REQUIRED_RESULT QList exDateTimes() const; Q_REQUIRED_RESULT DateList exDates() const; void setExDateTimes(const QList &exdates); void setExDates(const DateList &exdates); void addExDateTime(const QDateTime &exdate); void addExDate(const QDate &exdate); RecurrenceRule *defaultRRule(bool create = false) const; RecurrenceRule *defaultRRuleConst() const; void updated(); /** Installs an observer. Whenever some setting of this recurrence object is changed, the recurrenceUpdated( Recurrence* ) method of each observer will be called to inform it of changes. @param observer the Recurrence::Observer-derived object, which will be installed as an observer of this object. */ void addObserver(RecurrenceObserver *observer); /** Removes an observer that was added with addObserver. If the given object was not an observer, it does nothing. @param observer the Recurrence::Observer-derived object to be removed from the list of observers of this object. */ void removeObserver(RecurrenceObserver *observer); void recurrenceChanged(RecurrenceRule *) override; protected: RecurrenceRule *setNewRecurrenceType(RecurrenceRule::PeriodType type, int freq); private: //@cond PRIVATE class Private; Private *const d; //@endcond friend KCALENDARCORE_EXPORT QDataStream &operator<<(QDataStream &out, KCalendarCore::Recurrence *); friend KCALENDARCORE_EXPORT QDataStream &operator>>(QDataStream &in, KCalendarCore::Recurrence *); }; /** * Recurrence serializer and deserializer. * @since 4.12 */ KCALENDARCORE_EXPORT QDataStream &operator<<(QDataStream &out, KCalendarCore::Recurrence *); KCALENDARCORE_EXPORT QDataStream &operator>>(QDataStream &in, KCalendarCore::Recurrence *); } #endif