diff --git a/src/alarm.cpp b/src/alarm.cpp index 3a98165d6..12df812a9 100644 --- a/src/alarm.cpp +++ b/src/alarm.cpp @@ -1,866 +1,879 @@ /* This file is part of the kcalcore library. Copyright (c) 1998 Preston Brown Copyright (c) 2001 Cornelius Schumacher Copyright (c) 2003 David Jarvie 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. */ /** @file This file is part of the API for handling calendar data and defines the Alarm class. @brief Represents an alarm notification. @author Cornelius Schumacher \ */ #include "alarm.h" #include "duration.h" #include "incidence.h" #include "utils.h" #include #include using namespace KCalCore; /** Private class that helps to provide binary compatibility between releases. @internal */ //@cond PRIVATE class Q_DECL_HIDDEN KCalCore::Alarm::Private { public: Private() : mParent(nullptr), mType(Alarm::Invalid), mAlarmSnoozeTime(5), mAlarmRepeatCount(0), mEndOffset(false), mHasTime(false), mAlarmEnabled(false), mHasLocationRadius(false), mLocationRadius(0) {} Incidence *mParent = nullptr; // the incidence which this alarm belongs to Type mType; // type of alarm QString mDescription;// text to display/email body/procedure arguments QString mFile; // program to run/optional audio file to play QString mMailSubject;// subject of email QStringList mMailAttachFiles; // filenames to attach to email Person::List mMailAddresses; // who to mail for reminder QDateTime mAlarmTime;// time at which to trigger the alarm Duration mAlarmSnoozeTime; // how long after alarm to snooze before // triggering again int mAlarmRepeatCount;// number of times for alarm to repeat // after the initial time Duration mOffset; // time relative to incidence DTSTART // to trigger the alarm bool mEndOffset; // if true, mOffset relates to DTEND, not DTSTART bool mHasTime; // use mAlarmTime, not mOffset bool mAlarmEnabled; bool mHasLocationRadius; int mLocationRadius; // location radius for the alarm }; //@endcond Alarm::Alarm(Incidence *parent) : d(new KCalCore::Alarm::Private) { d->mParent = parent; } Alarm::Alarm(const Alarm &other) : CustomProperties(other), d(new KCalCore::Alarm::Private(*other.d)) { } Alarm::~Alarm() { delete d; } Alarm &Alarm::operator=(const Alarm &a) { if (&a != this) { d->mParent = a.d->mParent; d->mType = a.d->mType; d->mDescription = a.d->mDescription; d->mFile = a.d->mFile; d->mMailAttachFiles = a.d->mMailAttachFiles; d->mMailAddresses = a.d->mMailAddresses; d->mMailSubject = a.d->mMailSubject; d->mAlarmSnoozeTime = a.d->mAlarmSnoozeTime; d->mAlarmRepeatCount = a.d->mAlarmRepeatCount; d->mAlarmTime = a.d->mAlarmTime; d->mOffset = a.d->mOffset; d->mEndOffset = a.d->mEndOffset; d->mHasTime = a.d->mHasTime; d->mAlarmEnabled = a.d->mAlarmEnabled; } return *this; } static bool compareMailAddresses(const Person::List &list1, const Person::List &list2) { if (list1.count() == list2.count()) { for (int i = 0; i < list1.count(); ++i) { if (*list1.at(i) != *list2.at(i)) { return false; } } return true; } return false; } bool Alarm::operator==(const Alarm &rhs) const { if (d->mType != rhs.d->mType || d->mAlarmSnoozeTime != rhs.d->mAlarmSnoozeTime || d->mAlarmRepeatCount != rhs.d->mAlarmRepeatCount || d->mAlarmEnabled != rhs.d->mAlarmEnabled || d->mHasTime != rhs.d->mHasTime || d->mHasLocationRadius != rhs.d->mHasLocationRadius || d->mLocationRadius != rhs.d->mLocationRadius) { return false; } if (d->mHasTime) { if (d->mAlarmTime != rhs.d->mAlarmTime) { return false; } } else { if (d->mOffset != rhs.d->mOffset || d->mEndOffset != rhs.d->mEndOffset) { return false; } } switch (d->mType) { case Display: return d->mDescription == rhs.d->mDescription; case Email: return d->mDescription == rhs.d->mDescription && d->mMailAttachFiles == rhs.d->mMailAttachFiles && compareMailAddresses(d->mMailAddresses, rhs.d->mMailAddresses) && d->mMailSubject == rhs.d->mMailSubject; case Procedure: return d->mFile == rhs.d->mFile && d->mDescription == rhs.d->mDescription; case Audio: return d->mFile == rhs.d->mFile; case Invalid: break; } return false; } bool Alarm::operator!=(const Alarm &a) const { return !operator==(a); } void Alarm::setType(Alarm::Type type) { if (type == d->mType) { return; } if (d->mParent) { d->mParent->update(); } switch (type) { case Display: d->mDescription = QStringLiteral(""); break; case Procedure: d->mFile = d->mDescription = QStringLiteral(""); break; case Audio: d->mFile = QStringLiteral(""); break; case Email: d->mMailSubject = d->mDescription = QStringLiteral(""); d->mMailAddresses.clear(); d->mMailAttachFiles.clear(); break; case Invalid: break; default: if (d->mParent) { d->mParent->updated(); // not really } return; } d->mType = type; if (d->mParent) { d->mParent->updated(); } } Alarm::Type Alarm::type() const { return d->mType; } void Alarm::setAudioAlarm(const QString &audioFile) { if (d->mParent) { d->mParent->update(); } d->mType = Audio; d->mFile = audioFile; if (d->mParent) { d->mParent->updated(); } } void Alarm::setAudioFile(const QString &audioFile) { if (d->mType == Audio) { if (d->mParent) { d->mParent->update(); } d->mFile = audioFile; if (d->mParent) { d->mParent->updated(); } } } QString Alarm::audioFile() const { return (d->mType == Audio) ? d->mFile : QString(); } void Alarm::setProcedureAlarm(const QString &programFile, const QString &arguments) { if (d->mParent) { d->mParent->update(); } d->mType = Procedure; d->mFile = programFile; d->mDescription = arguments; if (d->mParent) { d->mParent->updated(); } } void Alarm::setProgramFile(const QString &programFile) { if (d->mType == Procedure) { if (d->mParent) { d->mParent->update(); } d->mFile = programFile; if (d->mParent) { d->mParent->updated(); } } } QString Alarm::programFile() const { return (d->mType == Procedure) ? d->mFile : QString(); } void Alarm::setProgramArguments(const QString &arguments) { if (d->mType == Procedure) { if (d->mParent) { d->mParent->update(); } d->mDescription = arguments; if (d->mParent) { d->mParent->updated(); } } } QString Alarm::programArguments() const { return (d->mType == Procedure) ? d->mDescription : QString(); } void Alarm::setEmailAlarm(const QString &subject, const QString &text, const Person::List &addressees, const QStringList &attachments) { if (d->mParent) { d->mParent->update(); } d->mType = Email; d->mMailSubject = subject; d->mDescription = text; d->mMailAddresses = addressees; d->mMailAttachFiles = attachments; if (d->mParent) { d->mParent->updated(); } } void Alarm::setMailAddress(const Person::Ptr &mailAddress) { if (d->mType == Email) { if (d->mParent) { d->mParent->update(); } d->mMailAddresses.clear(); d->mMailAddresses.append(mailAddress); if (d->mParent) { d->mParent->updated(); } } } void Alarm::setMailAddresses(const Person::List &mailAddresses) { if (d->mType == Email) { if (d->mParent) { d->mParent->update(); } d->mMailAddresses += mailAddresses; if (d->mParent) { d->mParent->updated(); } } } void Alarm::addMailAddress(const Person::Ptr &mailAddress) { if (d->mType == Email) { if (d->mParent) { d->mParent->update(); } d->mMailAddresses.append(mailAddress); if (d->mParent) { d->mParent->updated(); } } } Person::List Alarm::mailAddresses() const { return (d->mType == Email) ? d->mMailAddresses : Person::List(); } void Alarm::setMailSubject(const QString &mailAlarmSubject) { if (d->mType == Email) { if (d->mParent) { d->mParent->update(); } d->mMailSubject = mailAlarmSubject; if (d->mParent) { d->mParent->updated(); } } } QString Alarm::mailSubject() const { return (d->mType == Email) ? d->mMailSubject : QString(); } void Alarm::setMailAttachment(const QString &mailAttachFile) { if (d->mType == Email) { if (d->mParent) { d->mParent->update(); } d->mMailAttachFiles.clear(); d->mMailAttachFiles += mailAttachFile; if (d->mParent) { d->mParent->updated(); } } } void Alarm::setMailAttachments(const QStringList &mailAttachFiles) { if (d->mType == Email) { if (d->mParent) { d->mParent->update(); } d->mMailAttachFiles = mailAttachFiles; if (d->mParent) { d->mParent->updated(); } } } void Alarm::addMailAttachment(const QString &mailAttachFile) { if (d->mType == Email) { if (d->mParent) { d->mParent->update(); } d->mMailAttachFiles += mailAttachFile; if (d->mParent) { d->mParent->updated(); } } } QStringList Alarm::mailAttachments() const { return (d->mType == Email) ? d->mMailAttachFiles : QStringList(); } void Alarm::setMailText(const QString &text) { if (d->mType == Email) { if (d->mParent) { d->mParent->update(); } d->mDescription = text; if (d->mParent) { d->mParent->updated(); } } } QString Alarm::mailText() const { return (d->mType == Email) ? d->mDescription : QString(); } void Alarm::setDisplayAlarm(const QString &text) { if (d->mParent) { d->mParent->update(); } d->mType = Display; if (!text.isNull()) { d->mDescription = text; } if (d->mParent) { d->mParent->updated(); } } void Alarm::setText(const QString &text) { if (d->mType == Display) { if (d->mParent) { d->mParent->update(); } d->mDescription = text; if (d->mParent) { d->mParent->updated(); } } } QString Alarm::text() const { return (d->mType == Display) ? d->mDescription : QString(); } void Alarm::setTime(const QDateTime &alarmTime) { if (d->mParent) { d->mParent->update(); } d->mAlarmTime = alarmTime; d->mHasTime = true; if (d->mParent) { d->mParent->updated(); } } QDateTime Alarm::time() const { if (hasTime()) { return d->mAlarmTime; } else if (d->mParent) { if (d->mEndOffset) { QDateTime dt = d->mParent->dateTime(Incidence::RoleAlarmEndOffset); return d->mOffset.end(dt); } else { QDateTime dt = d->mParent->dateTime(Incidence::RoleAlarmStartOffset); return d->mOffset.end(dt); } } else { return QDateTime(); } } QDateTime Alarm::nextTime(const QDateTime &preTime, bool ignoreRepetitions) const { if (d->mParent && d->mParent->recurs()) { QDateTime dtEnd = d->mParent->dateTime(Incidence::RoleAlarmEndOffset); QDateTime dtStart = d->mParent->dtStart(); // Find the incidence's earliest alarm // Alarm time is defined by an offset from the event start or end time. QDateTime alarmStart = d->mOffset.end(d->mEndOffset ? dtEnd : dtStart); // Find the offset from the event start time, which is also used as the // offset from the recurrence time. Duration alarmOffset(dtStart, alarmStart); /* qCDebug(KCALCORE_LOG) << "dtStart " << dtStart; qCDebug(KCALCORE_LOG) << "dtEnd " << dtEnd; qCDebug(KCALCORE_LOG) << "alarmStart " << alarmStart; qCDebug(KCALCORE_LOG) << "alarmOffset " << alarmOffset.value(); qCDebug(KCALCORE_LOG) << "preTime " << preTime; */ if (alarmStart > preTime) { // No need to go further. return alarmStart; } if (d->mAlarmRepeatCount && !ignoreRepetitions) { // The alarm has repetitions, so check whether repetitions of previous // recurrences happen after given time. QDateTime prevRecurrence = d->mParent->recurrence()->getPreviousDateTime(preTime); if (prevRecurrence.isValid()) { QDateTime prevLastRepeat = alarmOffset.end(duration().end(prevRecurrence)); // qCDebug(KCALCORE_LOG) << "prevRecurrence" << prevRecurrence; // qCDebug(KCALCORE_LOG) << "prevLastRepeat" << prevLastRepeat; if (prevLastRepeat > preTime) { // Yes they did, return alarm offset to previous recurrence. QDateTime prevAlarm = alarmOffset.end(prevRecurrence); // qCDebug(KCALCORE_LOG) << "prevAlarm " << prevAlarm; return prevAlarm; } } } // Check the next recurrence now. QDateTime nextRecurrence = d->mParent->recurrence()->getNextDateTime(preTime); if (nextRecurrence.isValid()) { QDateTime nextAlarm = alarmOffset.end(nextRecurrence); /* qCDebug(KCALCORE_LOG) << "nextRecurrence" << nextRecurrence; qCDebug(KCALCORE_LOG) << "nextAlarm " << nextAlarm; */ if (nextAlarm > preTime) { // It's first alarm takes place after given time. return nextAlarm; } } } else { // Not recurring. QDateTime alarmTime = time(); if (alarmTime > preTime) { return alarmTime; } } return QDateTime(); } bool Alarm::hasTime() const { return d->mHasTime; } void Alarm::shiftTimes(const QTimeZone &oldZone, const QTimeZone &newZone) { if (d->mParent) { d->mParent->update(); } d->mAlarmTime = d->mAlarmTime.toTimeZone(oldZone); d->mAlarmTime.setTimeZone(newZone); if (d->mParent) { d->mParent->updated(); } } void Alarm::setSnoozeTime(const Duration &alarmSnoozeTime) { if (alarmSnoozeTime.value() > 0) { if (d->mParent) { d->mParent->update(); } d->mAlarmSnoozeTime = alarmSnoozeTime; if (d->mParent) { d->mParent->updated(); } } } Duration Alarm::snoozeTime() const { return d->mAlarmSnoozeTime; } void Alarm::setRepeatCount(int alarmRepeatCount) { if (d->mParent) { d->mParent->update(); } d->mAlarmRepeatCount = alarmRepeatCount; if (d->mParent) { d->mParent->updated(); } } int Alarm::repeatCount() const { return d->mAlarmRepeatCount; } Duration Alarm::duration() const { return Duration(d->mAlarmSnoozeTime.value() * d->mAlarmRepeatCount, d->mAlarmSnoozeTime.type()); } QDateTime Alarm::nextRepetition(const QDateTime &preTime) const { QDateTime at = nextTime(preTime); if (at > preTime) { return at; } if (!d->mAlarmRepeatCount) { // there isn't an occurrence after the specified time return QDateTime(); } qint64 repetition; int interval = d->mAlarmSnoozeTime.value(); bool daily = d->mAlarmSnoozeTime.isDaily(); if (daily) { qint64 daysTo = at.daysTo(preTime); if (preTime.time() <= at.time()) { --daysTo; } repetition = daysTo / interval + 1; } else { repetition = at.secsTo(preTime) / interval + 1; } if (repetition > d->mAlarmRepeatCount) { // all repetitions have finished before the specified time return QDateTime(); } return daily ? at.addDays(int(repetition * interval)) : at.addSecs(repetition * interval); } QDateTime Alarm::previousRepetition(const QDateTime &afterTime) const { QDateTime at = time(); if (at >= afterTime) { // alarm's first/only time is at/after the specified time return QDateTime(); } if (!d->mAlarmRepeatCount) { return at; } qint64 repetition; int interval = d->mAlarmSnoozeTime.value(); bool daily = d->mAlarmSnoozeTime.isDaily(); if (daily) { qint64 daysTo = at.daysTo(afterTime); if (afterTime.time() <= at.time()) { --daysTo; } repetition = daysTo / interval; } else { repetition = (at.secsTo(afterTime) - 1) / interval; } if (repetition > d->mAlarmRepeatCount) { repetition = d->mAlarmRepeatCount; } return daily ? at.addDays(int(repetition * interval)) : at.addSecs(repetition * interval); } QDateTime Alarm::endTime() const { if (!d->mAlarmRepeatCount) { return time(); } if (d->mAlarmSnoozeTime.isDaily()) { return time().addDays(d->mAlarmRepeatCount * d->mAlarmSnoozeTime.asDays()); } else { return time().addSecs(d->mAlarmRepeatCount * d->mAlarmSnoozeTime.asSeconds()); } } void Alarm::toggleAlarm() { if (d->mParent) { d->mParent->update(); } d->mAlarmEnabled = !d->mAlarmEnabled; if (d->mParent) { d->mParent->updated(); } } void Alarm::setEnabled(bool enable) { if (d->mParent) { d->mParent->update(); } d->mAlarmEnabled = enable; if (d->mParent) { d->mParent->updated(); } } bool Alarm::enabled() const { return d->mAlarmEnabled; } void Alarm::setStartOffset(const Duration &offset) { if (d->mParent) { d->mParent->update(); } d->mOffset = offset; d->mEndOffset = false; d->mHasTime = false; if (d->mParent) { d->mParent->updated(); } } Duration Alarm::startOffset() const { return (d->mHasTime || d->mEndOffset) ? Duration(0) : d->mOffset; } bool Alarm::hasStartOffset() const { return !d->mHasTime && !d->mEndOffset; } bool Alarm::hasEndOffset() const { return !d->mHasTime && d->mEndOffset; } void Alarm::setEndOffset(const Duration &offset) { if (d->mParent) { d->mParent->update(); } d->mOffset = offset; d->mEndOffset = true; d->mHasTime = false; if (d->mParent) { d->mParent->updated(); } } Duration Alarm::endOffset() const { return (d->mHasTime || !d->mEndOffset) ? Duration(0) : d->mOffset; } void Alarm::setParent(Incidence *parent) { d->mParent = parent; } QString Alarm::parentUid() const { return d->mParent ? d->mParent->uid() : QString(); } void Alarm::customPropertyUpdated() { if (d->mParent) { d->mParent->update(); d->mParent->updated(); } } void Alarm::setHasLocationRadius(bool hasLocationRadius) { if (d->mParent) { d->mParent->update(); } d->mHasLocationRadius = hasLocationRadius; if (hasLocationRadius) { setNonKDECustomProperty("X-LOCATION-RADIUS", QString::number(d->mLocationRadius)); } else { removeNonKDECustomProperty("X-LOCATION-RADIUS"); } if (d->mParent) { d->mParent->updated(); } } bool Alarm::hasLocationRadius() const { return d->mHasLocationRadius; } void Alarm::setLocationRadius(int locationRadius) { if (d->mParent) { d->mParent->update(); } d->mLocationRadius = locationRadius; if (d->mParent) { d->mParent->updated(); } } int Alarm::locationRadius() const { return d->mLocationRadius; } QDataStream &KCalCore::operator<<(QDataStream &out, const KCalCore::Alarm::Ptr &a) { if (a) { - out << ((quint32)a->d->mType) << a->d->mAlarmSnoozeTime << a->d->mAlarmRepeatCount << a->d->mEndOffset << a->d->mHasTime - << a->d->mAlarmEnabled << a->d->mHasLocationRadius << a->d->mLocationRadius << a->d->mOffset; + out << ((quint32)a->d->mType) + << a->d->mAlarmSnoozeTime + << a->d->mAlarmRepeatCount + << a->d->mEndOffset + << a->d->mHasTime + << a->d->mAlarmEnabled + << a->d->mHasLocationRadius + << a->d->mLocationRadius + << a->d->mOffset; + serializeQDateTimeAsKDateTime(out, a->d->mAlarmTime); - out << a->d->mFile << a->d->mMailSubject << a->d->mDescription << a->d->mMailAttachFiles << a->d->mMailAddresses; + + out << a->d->mFile + << a->d->mMailSubject + << a->d->mDescription + << a->d->mMailAttachFiles + << a->d->mMailAddresses; } return out; } QDataStream &KCalCore::operator>>(QDataStream &in, const KCalCore::Alarm::Ptr &a) { if (a) { quint32 type; in >> type; a->d->mType = static_cast(type); in >> a->d->mAlarmSnoozeTime >> a->d->mAlarmRepeatCount >> a->d->mEndOffset >> a->d->mHasTime >> a->d->mAlarmEnabled >> a->d->mHasLocationRadius >> a->d->mLocationRadius >> a->d->mOffset; deserializeKDateTimeAsQDateTime(in, a->d->mAlarmTime); in >> a->d->mFile >> a->d->mMailSubject >> a->d->mDescription >> a->d->mMailAttachFiles >> a->d->mMailAddresses; } return in; } void Alarm::virtual_hook(int id, void *data) { Q_UNUSED(id); Q_UNUSED(data); Q_ASSERT(false); } diff --git a/src/attachment.cpp b/src/attachment.cpp index 9fe34b38b..51f25a71c 100644 --- a/src/attachment.cpp +++ b/src/attachment.cpp @@ -1,258 +1,272 @@ /* This file is part of the kcalcore library. Copyright (c) 2002 Michael Brade 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. */ /** @file This file is part of the API for handling calendar data and defines the Attachment class. @brief Represents information related to an attachment for a Calendar Incidence. @author Michael Brade \ */ #include "attachment.h" #include using namespace KCalCore; /** Private class that helps to provide binary compatibility between releases. @internal */ //@cond PRIVATE class Q_DECL_HIDDEN KCalCore::Attachment::Private { public: Private(const QString &mime, bool binary) : mSize(0), mMimeType(mime), mBinary(binary), mLocal(false), mShowInline(false) {} Private(const Private &other) : mSize(other.mSize), mMimeType(other.mMimeType), mUri(other.mUri), mEncodedData(other.mEncodedData), mLabel(other.mLabel), mBinary(other.mBinary), mLocal(other.mLocal), mShowInline(other.mShowInline) {} ~Private() { } QByteArray mDecodedDataCache; uint mSize; QString mMimeType; QString mUri; QByteArray mEncodedData; QString mLabel; bool mBinary = false; bool mLocal = false; bool mShowInline = false; }; //@endcond Attachment::Attachment(const Attachment &attachment) : d(new Attachment::Private(*attachment.d)) { } Attachment::Attachment(const QString &uri, const QString &mime) : d(new Attachment::Private(mime, false)) { d->mUri = uri; } Attachment::Attachment(const QByteArray &base64, const QString &mime) : d(new Attachment::Private(mime, true)) { d->mEncodedData = base64; } Attachment::~Attachment() { delete d; } bool Attachment::isUri() const { return !d->mBinary; } QString Attachment::uri() const { if (!d->mBinary) { return d->mUri; } else { return QString(); } } void Attachment::setUri(const QString &uri) { d->mUri = uri; d->mBinary = false; } bool Attachment::isBinary() const { return d->mBinary; } QByteArray Attachment::data() const { if (d->mBinary) { return d->mEncodedData; } else { return QByteArray(); } } QByteArray Attachment::decodedData() const { if (d->mDecodedDataCache.isNull()) { d->mDecodedDataCache = QByteArray::fromBase64(d->mEncodedData); } return d->mDecodedDataCache; } void Attachment::setDecodedData(const QByteArray &data) { setData(data.toBase64()); d->mDecodedDataCache = data; d->mSize = d->mDecodedDataCache.size(); } void Attachment::setData(const QByteArray &base64) { d->mEncodedData = base64; d->mBinary = true; d->mDecodedDataCache = QByteArray(); d->mSize = 0; } uint Attachment::size() const { if (isUri()) { return 0; } if (!d->mSize) { d->mSize = decodedData().size(); } return d->mSize; } QString Attachment::mimeType() const { return d->mMimeType; } void Attachment::setMimeType(const QString &mime) { d->mMimeType = mime; } bool Attachment::showInline() const { return d->mShowInline; } void Attachment::setShowInline(bool showinline) { d->mShowInline = showinline; } QString Attachment::label() const { return d->mLabel; } void Attachment::setLabel(const QString &label) { d->mLabel = label; } bool Attachment::isLocal() const { return d->mLocal; } void Attachment::setLocal(bool local) { d->mLocal = local; } Attachment &Attachment::operator=(const Attachment &other) { if (this != &other) { d->mSize = other.d->mSize; d->mMimeType = other.d->mMimeType; d->mUri = other.d->mUri; d->mEncodedData = other.d->mEncodedData; d->mLabel = other.d->mLabel; d->mBinary = other.d->mBinary; d->mLocal = other.d->mLocal; d->mShowInline = other.d->mShowInline; } return *this; } bool Attachment::operator==(const Attachment &a2) const { return uri() == a2.uri() && d->mLabel == a2.label() && d->mLocal == a2.isLocal() && d->mBinary == a2.isBinary() && d->mShowInline == a2.showInline() && size() == a2.size() && decodedData() == a2.decodedData(); } bool Attachment::operator!=(const Attachment &a2) const { return !(*this == a2); } QDataStream &KCalCore::operator<<(QDataStream &out, const KCalCore::Attachment::Ptr &a) { if (a) { - out << a->d->mSize << a->d->mMimeType << a->d->mUri << a->d->mEncodedData << a->d->mLabel << a->d->mBinary << a->d->mLocal << a->d->mShowInline; + out << a->d->mSize + << a->d->mMimeType + << a->d->mUri + << a->d->mEncodedData + << a->d->mLabel + << a->d->mBinary + << a->d->mLocal + << a->d->mShowInline; } return out; } QDataStream &KCalCore::operator>>(QDataStream &in, const KCalCore::Attachment::Ptr &a) { if (a) { - in >> a->d->mSize >> a->d->mMimeType >> a->d->mUri >> a->d->mEncodedData >> a->d->mLabel >> a->d->mBinary >> a->d->mLocal >> a->d->mShowInline; + in >> a->d->mSize + >> a->d->mMimeType + >> a->d->mUri + >> a->d->mEncodedData + >> a->d->mLabel + >> a->d->mBinary + >> a->d->mLocal + >> a->d->mShowInline; } return in; } diff --git a/src/calendar.cpp b/src/calendar.cpp index 1c13f3cd4..6470edfd0 100644 --- a/src/calendar.cpp +++ b/src/calendar.cpp @@ -1,1369 +1,1370 @@ /* This file is part of the kcalcore library. Copyright (c) 1998 Preston Brown Copyright (c) 2000-2004 Cornelius Schumacher Copyright (C) 2003-2004 Reinhold Kainhofer Copyright (c) 2006 David Jarvie 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. */ /** @file This file is part of the API for handling calendar data and defines the Calendar class. @brief Represents the main calendar class. @author Preston Brown \ @author Cornelius Schumacher \ @author Reinhold Kainhofer \ @author David Jarvie \ */ #include "calendar.h" #include "calendar_p.h" #include "calfilter.h" #include "icaltimezones_p.h" #include "sorting.h" #include "visitor.h" #include "utils.h" #include "kcalcore_debug.h" #include extern "C" { #include } #include // for std::remove() using namespace KCalCore; /** Make a QHash::value that returns a QVector. */ template QVector values(const QMultiHash &c) { QVector v; v.reserve(c.size()); for (typename QMultiHash::const_iterator it = c.begin(), end = c.end(); it != end; ++it) { v.push_back(it.value()); } return v; } template QVector values(const QMultiHash &c, const K &x) { QVector v; typename QMultiHash::const_iterator it = c.find(x); while (it != c.end() && it.key() == x) { v.push_back(it.value()); ++it; } return v; } /** Template for a class that implements a visitor for adding an Incidence to a resource supporting addEvent(), addTodo() and addJournal() calls. */ template class AddVisitor : public Visitor { public: AddVisitor(T *r) : mResource(r) {} bool visit(const Event::Ptr &e) override { return mResource->addEvent(e); } bool visit(const Todo::Ptr &t) override { return mResource->addTodo(t); } bool visit(const Journal::Ptr &j) override { return mResource->addJournal(j); } bool visit(const FreeBusy::Ptr &) override { return false; } private: T *mResource; }; /** Template for a class that implements a visitor for deleting an Incidence from a resource supporting deleteEvent(), deleteTodo() and deleteJournal() calls. */ template class DeleteVisitor : public Visitor { public: DeleteVisitor(T *r) : mResource(r) {} bool visit(const Event::Ptr &e) override { mResource->deleteEvent(e); return true; } bool visit(const Todo::Ptr &t) override { mResource->deleteTodo(t); return true; } bool visit(const Journal::Ptr &j) override { mResource->deleteJournal(j); return true; } bool visit(const FreeBusy::Ptr &) override { return false; } private: T *mResource; }; //@endcond Calendar::Calendar(const QTimeZone &timeZone) : d(new KCalCore::Calendar::Private) { d->mTimeZone = timeZone; } Calendar::Calendar(const QByteArray &timeZoneId) : d(new KCalCore::Calendar::Private) { setTimeZoneId(timeZoneId); } Calendar::~Calendar() { delete d; } Person::Ptr Calendar::owner() const { return d->mOwner; } void Calendar::setOwner(const Person::Ptr &owner) { Q_ASSERT(owner); d->mOwner = owner; setModified(true); } void Calendar::setTimeZone(const QTimeZone &timeZone) { d->mTimeZone = timeZone; doSetTimeZone(d->mTimeZone); } QTimeZone Calendar::timeZone() const { return d->mTimeZone; } void Calendar::setTimeZoneId(const QByteArray &timeZoneId) { d->mTimeZone = d->timeZoneIdSpec(timeZoneId); doSetTimeZone(d->mTimeZone); } //@cond PRIVATE QTimeZone Calendar::Private::timeZoneIdSpec(const QByteArray &timeZoneId) { if (timeZoneId == QByteArrayLiteral("UTC")) { return QTimeZone::utc(); } auto tz = QTimeZone(timeZoneId); - if (tz.isValid()) + if (tz.isValid()) { return tz; + } return QTimeZone::systemTimeZone(); } //@endcond QByteArray Calendar::timeZoneId() const { return d->mTimeZone.id(); } void Calendar::shiftTimes(const QTimeZone &oldZone, const QTimeZone &newZone) { setTimeZone(newZone); int i, end; Event::List ev = events(); for (i = 0, end = ev.count(); i < end; ++i) { ev[i]->shiftTimes(oldZone, newZone); } Todo::List to = todos(); for (i = 0, end = to.count(); i < end; ++i) { to[i]->shiftTimes(oldZone, newZone); } Journal::List jo = journals(); for (i = 0, end = jo.count(); i < end; ++i) { jo[i]->shiftTimes(oldZone, newZone); } } void Calendar::setFilter(CalFilter *filter) { if (filter) { d->mFilter = filter; } else { d->mFilter = d->mDefaultFilter; } emit filterChanged(); } CalFilter *Calendar::filter() const { return d->mFilter; } QStringList Calendar::categories() const { Incidence::List rawInc(rawIncidences()); QStringList cats, thisCats; // @TODO: For now just iterate over all incidences. In the future, // the list of categories should be built when reading the file. for (Incidence::List::ConstIterator i = rawInc.constBegin(); i != rawInc.constEnd(); ++i) { thisCats = (*i)->categories(); for (QStringList::ConstIterator si = thisCats.constBegin(); si != thisCats.constEnd(); ++si) { if (!cats.contains(*si)) { cats.append(*si); } } } return cats; } Incidence::List Calendar::incidences(const QDate &date) const { return mergeIncidenceList(events(date), todos(date), journals(date)); } Incidence::List Calendar::incidences() const { return mergeIncidenceList(events(), todos(), journals()); } Incidence::List Calendar::rawIncidences() const { return mergeIncidenceList(rawEvents(), rawTodos(), rawJournals()); } Incidence::List Calendar::instances(const Incidence::Ptr &incidence) const { if (incidence) { Event::List elist; Todo::List tlist; Journal::List jlist; if (incidence->type() == Incidence::TypeEvent) { elist = eventInstances(incidence); } else if (incidence->type() == Incidence::TypeTodo) { tlist = todoInstances(incidence); } else if (incidence->type() == Incidence::TypeJournal) { jlist = journalInstances(incidence); } return mergeIncidenceList(elist, tlist, jlist); } else { return Incidence::List(); } } Incidence::List Calendar::duplicates(const Incidence::Ptr &incidence) { if (incidence) { Incidence::List list; Incidence::List vals = values(d->mNotebookIncidences); Incidence::List::const_iterator it; for (it = vals.constBegin(); it != vals.constEnd(); ++it) { if (((incidence->dtStart() == (*it)->dtStart()) || (!incidence->dtStart().isValid() && !(*it)->dtStart().isValid())) && (incidence->summary() == (*it)->summary())) { list.append(*it); } } return list; } else { return Incidence::List(); } } bool Calendar::addNotebook(const QString ¬ebook, bool isVisible) { if (d->mNotebooks.contains(notebook)) { return false; } else { d->mNotebooks.insert(notebook, isVisible); return true; } } bool Calendar::updateNotebook(const QString ¬ebook, bool isVisible) { if (!d->mNotebooks.contains(notebook)) { return false; } else { d->mNotebooks.insert(notebook, isVisible); return true; } } bool Calendar::deleteNotebook(const QString ¬ebook) { if (!d->mNotebooks.contains(notebook)) { return false; } else { return d->mNotebooks.remove(notebook); } } bool Calendar::setDefaultNotebook(const QString ¬ebook) { if (!d->mNotebooks.contains(notebook)) { return false; } else { d->mDefaultNotebook = notebook; return true; } } QString Calendar::defaultNotebook() const { return d->mDefaultNotebook; } bool Calendar::hasValidNotebook(const QString ¬ebook) const { return d->mNotebooks.contains(notebook); } bool Calendar::isVisible(const Incidence::Ptr &incidence) const { if (d->mIncidenceVisibility.contains(incidence)) { return d->mIncidenceVisibility[incidence]; } const QString nuid = notebook(incidence); bool rv; if (d->mNotebooks.contains(nuid)) { rv = d->mNotebooks.value(nuid); } else { // NOTE returns true also for nonexisting notebooks for compatibility rv = true; } d->mIncidenceVisibility[incidence] = rv; return rv; } void Calendar::clearNotebookAssociations() { d->mNotebookIncidences.clear(); d->mUidToNotebook.clear(); d->mIncidenceVisibility.clear(); } bool Calendar::setNotebook(const Incidence::Ptr &inc, const QString ¬ebook) { if (!inc) { return false; } if (!notebook.isEmpty() && !incidence(inc->uid(), inc->recurrenceId())) { qCWarning(KCALCORE_LOG) << "cannot set notebook until incidence has been added"; return false; } if (d->mUidToNotebook.contains(inc->uid())) { QString old = d->mUidToNotebook.value(inc->uid()); if (!old.isEmpty() && notebook != old) { if (inc->hasRecurrenceId()) { qCWarning(KCALCORE_LOG) << "cannot set notebook for child incidences"; return false; } // Move all possible children also. Incidence::List list = instances(inc); Incidence::List::Iterator it; for (it = list.begin(); it != list.end(); ++it) { d->mNotebookIncidences.remove(old, *it); d->mNotebookIncidences.insert(notebook, *it); } notifyIncidenceChanged(inc); // for removing from old notebook // don not remove from mUidToNotebook to keep deleted incidences d->mNotebookIncidences.remove(old, inc); } } if (!notebook.isEmpty()) { d->mUidToNotebook.insert(inc->uid(), notebook); d->mNotebookIncidences.insert(notebook, inc); qCDebug(KCALCORE_LOG) << "setting notebook" << notebook << "for" << inc->uid(); notifyIncidenceChanged(inc); // for inserting into new notebook } return true; } QString Calendar::notebook(const Incidence::Ptr &incidence) const { if (incidence) { return d->mUidToNotebook.value(incidence->uid()); } else { return QString(); } } QString Calendar::notebook(const QString &uid) const { return d->mUidToNotebook.value(uid); } QStringList Calendar::notebooks() const { return d->mNotebookIncidences.uniqueKeys(); } Incidence::List Calendar::incidences(const QString ¬ebook) const { if (notebook.isEmpty()) { return values(d->mNotebookIncidences); } else { return values(d->mNotebookIncidences, notebook); } } /** static */ Event::List Calendar::sortEvents(const Event::List &eventList, EventSortField sortField, SortDirection sortDirection) { - if (eventList.isEmpty()) { return Event::List(); } Event::List eventListSorted; // Notice we alphabetically presort Summaries first. // We do this so comparison "ties" stay in a nice order. eventListSorted = eventList; switch (sortField) { case EventSortUnsorted: break; case EventSortStartDate: if (sortDirection == SortDirectionAscending) { std::sort(eventListSorted.begin(), eventListSorted.end(), Events::startDateLessThan); } else { std::sort(eventListSorted.begin(), eventListSorted.end(), Events::startDateMoreThan); } break; case EventSortEndDate: if (sortDirection == SortDirectionAscending) { std::sort(eventListSorted.begin(), eventListSorted.end(), Events::endDateLessThan); } else { std::sort(eventListSorted.begin(), eventListSorted.end(), Events::endDateMoreThan); } break; case EventSortSummary: if (sortDirection == SortDirectionAscending) { std::sort(eventListSorted.begin(), eventListSorted.end(), Events::summaryLessThan); } else { std::sort(eventListSorted.begin(), eventListSorted.end(), Events::summaryMoreThan); } break; } return eventListSorted; } Event::List Calendar::events(const QDate &date, const QTimeZone &timeZone, EventSortField sortField, SortDirection sortDirection) const { Event::List el = rawEventsForDate(date, timeZone, sortField, sortDirection); d->mFilter->apply(&el); return el; } Event::List Calendar::events(const QDateTime &dt) const { Event::List el = rawEventsForDate(dt); d->mFilter->apply(&el); return el; } Event::List Calendar::events(const QDate &start, const QDate &end, const QTimeZone &timeZone, bool inclusive) const { Event::List el = rawEvents(start, end, timeZone, inclusive); d->mFilter->apply(&el); return el; } Event::List Calendar::events(EventSortField sortField, SortDirection sortDirection) const { Event::List el = rawEvents(sortField, sortDirection); d->mFilter->apply(&el); return el; } bool Calendar::addIncidence(const Incidence::Ptr &incidence) { if (!incidence) { return false; } AddVisitor v(this); return incidence->accept(v, incidence); } bool Calendar::deleteIncidence(const Incidence::Ptr &incidence) { if (!incidence) { return false; } if (beginChange(incidence)) { DeleteVisitor v(this); const bool result = incidence->accept(v, incidence); endChange(incidence); return result; } else { return false; } } Incidence::Ptr Calendar::createException(const Incidence::Ptr &incidence, const QDateTime &recurrenceId, bool thisAndFuture) { Q_ASSERT(recurrenceId.isValid()); if (!incidence || !incidence->recurs() || !recurrenceId.isValid()) { return Incidence::Ptr(); } Incidence::Ptr newInc(incidence->clone()); newInc->setCreated(QDateTime::currentDateTimeUtc()); newInc->setRevision(0); //Recurring exceptions are not support for now newInc->clearRecurrence(); newInc->setRecurrenceId(recurrenceId); newInc->setThisAndFuture(thisAndFuture); newInc->setDtStart(recurrenceId); // Calculate and set the new end of the incidence QDateTime end = incidence->dateTime(IncidenceBase::RoleEnd); if (end.isValid()) { if (incidence->allDay()) { qint64 offset = incidence->dtStart().daysTo(recurrenceId); end = end.addDays(offset); } else { qint64 offset = incidence->dtStart().secsTo(recurrenceId); end = end.addSecs(offset); } newInc->setDateTime(end, IncidenceBase::RoleEnd); } return newInc; } Incidence::Ptr Calendar::incidence(const QString &uid, const QDateTime &recurrenceId) const { Incidence::Ptr i = event(uid, recurrenceId); if (i) { return i; } i = todo(uid, recurrenceId); if (i) { return i; } i = journal(uid, recurrenceId); return i; } Incidence::Ptr Calendar::deleted(const QString &uid, const QDateTime &recurrenceId) const { Incidence::Ptr i = deletedEvent(uid, recurrenceId); if (i) { return i; } i = deletedTodo(uid, recurrenceId); if (i) { return i; } i = deletedJournal(uid, recurrenceId); return i; } Incidence::List Calendar::incidencesFromSchedulingID(const QString &sid) const { Incidence::List result; const Incidence::List incidences = rawIncidences(); Incidence::List::const_iterator it = incidences.begin(); for (; it != incidences.end(); ++it) { if ((*it)->schedulingID() == sid) { result.append(*it); } } return result; } Incidence::Ptr Calendar::incidenceFromSchedulingID(const QString &uid) const { const Incidence::List incidences = rawIncidences(); Incidence::List::const_iterator it = incidences.begin(); for (; it != incidences.end(); ++it) { if ((*it)->schedulingID() == uid) { // Touchdown, and the crowd goes wild return *it; } } // Not found return Incidence::Ptr(); } /** static */ Todo::List Calendar::sortTodos(const Todo::List &todoList, TodoSortField sortField, SortDirection sortDirection) { if (todoList.isEmpty()) { return Todo::List(); } Todo::List todoListSorted; // Notice we alphabetically presort Summaries first. // We do this so comparison "ties" stay in a nice order. // Note that To-dos may not have Start DateTimes nor due DateTimes. todoListSorted = todoList; switch (sortField) { case TodoSortUnsorted: break; case TodoSortStartDate: if (sortDirection == SortDirectionAscending) { std::sort(todoListSorted.begin(), todoListSorted.end(), Todos::startDateLessThan); } else { std::sort(todoListSorted.begin(), todoListSorted.end(), Todos::startDateMoreThan); } break; case TodoSortDueDate: if (sortDirection == SortDirectionAscending) { std::sort(todoListSorted.begin(), todoListSorted.end(), Todos::dueDateLessThan); } else { std::sort(todoListSorted.begin(), todoListSorted.end(), Todos::dueDateMoreThan); } break; case TodoSortPriority: if (sortDirection == SortDirectionAscending) { std::sort(todoListSorted.begin(), todoListSorted.end(), Todos::priorityLessThan); } else { std::sort(todoListSorted.begin(), todoListSorted.end(), Todos::priorityMoreThan); } break; case TodoSortPercentComplete: if (sortDirection == SortDirectionAscending) { std::sort(todoListSorted.begin(), todoListSorted.end(), Todos::percentLessThan); } else { std::sort(todoListSorted.begin(), todoListSorted.end(), Todos::percentMoreThan); } break; case TodoSortSummary: if (sortDirection == SortDirectionAscending) { std::sort(todoListSorted.begin(), todoListSorted.end(), Todos::summaryLessThan); } else { std::sort(todoListSorted.begin(), todoListSorted.end(), Todos::summaryMoreThan); } break; case TodoSortCreated: if (sortDirection == SortDirectionAscending) { std::sort(todoListSorted.begin(), todoListSorted.end(), Todos::createdLessThan); } else { std::sort(todoListSorted.begin(), todoListSorted.end(), Todos::createdMoreThan); } break; } return todoListSorted; } Todo::List Calendar::todos(TodoSortField sortField, SortDirection sortDirection) const { Todo::List tl = rawTodos(sortField, sortDirection); d->mFilter->apply(&tl); return tl; } Todo::List Calendar::todos(const QDate &date) const { Todo::List el = rawTodosForDate(date); d->mFilter->apply(&el); return el; } Todo::List Calendar::todos(const QDate &start, const QDate &end, const QTimeZone &timeZone, bool inclusive) const { Todo::List tl = rawTodos(start, end, timeZone, inclusive); d->mFilter->apply(&tl); return tl; } /** static */ Journal::List Calendar::sortJournals(const Journal::List &journalList, JournalSortField sortField, SortDirection sortDirection) { if (journalList.isEmpty()) { return Journal::List(); } Journal::List journalListSorted = journalList; switch (sortField) { case JournalSortUnsorted: break; case JournalSortDate: if (sortDirection == SortDirectionAscending) { std::sort(journalListSorted.begin(), journalListSorted.end(), Journals::dateLessThan); } else { std::sort(journalListSorted.begin(), journalListSorted.end(), Journals::dateMoreThan); } break; case JournalSortSummary: if (sortDirection == SortDirectionAscending) { std::sort(journalListSorted.begin(), journalListSorted.end(), Journals::summaryLessThan); } else { std::sort(journalListSorted.begin(), journalListSorted.end(), Journals::summaryMoreThan); } break; } return journalListSorted; } Journal::List Calendar::journals(JournalSortField sortField, SortDirection sortDirection) const { Journal::List jl = rawJournals(sortField, sortDirection); d->mFilter->apply(&jl); return jl; } Journal::List Calendar::journals(const QDate &date) const { Journal::List el = rawJournalsForDate(date); d->mFilter->apply(&el); return el; } // When this is called, the to-dos have already been added to the calendar. // This method is only about linking related to-dos. void Calendar::setupRelations(const Incidence::Ptr &forincidence) { if (!forincidence) { return; } const QString uid = forincidence->uid(); // First, go over the list of orphans and see if this is their parent Incidence::List l = values(d->mOrphans, uid); d->mOrphans.remove(uid); if (!l.isEmpty()) { Incidence::List &relations = d->mIncidenceRelations[uid]; relations.reserve(relations.count() + l.count()); for (int i = 0, end = l.count(); i < end; ++i) { relations.append(l[i]); d->mOrphanUids.remove(l[i]->uid()); } } // Now see about this incidences parent if (forincidence->relatedTo().isEmpty() && !forincidence->relatedTo().isEmpty()) { // Incidence has a uid it is related to but is not registered to it yet. // Try to find it Incidence::Ptr parent = incidence(forincidence->relatedTo()); if (parent) { // Found it // look for hierarchy loops if (isAncestorOf(forincidence, parent)) { forincidence->setRelatedTo(QString()); - qCWarning(KCALCORE_LOG) << "hierarchy loop beetween " << forincidence->uid() << " and " << parent->uid(); + qCWarning(KCALCORE_LOG) << "hierarchy loop between " + << forincidence->uid() + << " and " << parent->uid(); } else { d->mIncidenceRelations[parent->uid()].append(forincidence); } } else { // Not found, put this in the mOrphans list // Note that the mOrphans dict might contain multiple entries with the // same key! which are multiple children that wait for the parent // incidence to be inserted. d->mOrphans.insert(forincidence->relatedTo(), forincidence); d->mOrphanUids.insert(forincidence->uid(), forincidence); } } } // If a to-do with sub-to-dos is deleted, move it's sub-to-dos to the orphan list void Calendar::removeRelations(const Incidence::Ptr &incidence) { if (!incidence) { qCDebug(KCALCORE_LOG) << "Warning: incidence is 0"; return; } const QString uid = incidence->uid(); for (const Incidence::Ptr &i : qAsConst(d->mIncidenceRelations[uid])) { if (!d->mOrphanUids.contains(i->uid())) { d->mOrphans.insert(uid, i); d->mOrphanUids.insert(i->uid(), i); i->setRelatedTo(uid); } } const QString parentUid = incidence->relatedTo(); // If this incidence is related to something else, tell that about it if (!parentUid.isEmpty()) { Incidence::List &relations = d->mIncidenceRelations[parentUid]; relations.erase( std::remove(relations.begin(), relations.end(), incidence), relations.end()); } // Remove this one from the orphans list if (d->mOrphanUids.remove(uid)) { // This incidence is located in the orphans list - it should be removed // Since the mOrphans dict might contain the same key (with different // child incidence pointers!) multiple times, take care that we remove // the correct one. So we need to remove all items with the given // parent UID, and readd those that are not for this item. Also, there // might be other entries with differnet UID that point to this // incidence (this might happen when the relatedTo of the item is // changed before its parent is inserted. This might happen with // groupware servers....). Remove them, too QStringList relatedToUids; // First, create a list of all keys in the mOrphans list which point // to the removed item relatedToUids << incidence->relatedTo(); for (QMultiHash::Iterator it = d->mOrphans.begin(); it != d->mOrphans.end(); ++it) { if (it.value()->uid() == uid) { relatedToUids << it.key(); } } // now go through all uids that have one entry that point to the incidence for (QStringList::const_iterator uidit = relatedToUids.constBegin(); uidit != relatedToUids.constEnd(); ++uidit) { Incidence::List tempList; // Remove all to get access to the remaining entries const Incidence::List l = values(d->mOrphans, *uidit); d->mOrphans.remove(*uidit); for (const Incidence::Ptr &i : l) { if (i != incidence) { tempList.append(i); } } // Readd those that point to a different orphan incidence for (Incidence::List::Iterator incit = tempList.begin(); incit != tempList.end(); ++incit) { d->mOrphans.insert(*uidit, *incit); } } } // Make sure the deleted incidence doesn't relate to a non-deleted incidence, // since that would cause trouble in MemoryCalendar::close(), as the deleted // incidences are destroyed after the non-deleted incidences. The destructor // of the deleted incidences would then try to access the already destroyed // non-deleted incidence, which would segfault. // // So in short: Make sure dead incidences don't point to alive incidences // via the relation. // // This crash is tested in MemoryCalendarTest::testRelationsCrash(). // incidence->setRelatedTo( Incidence::Ptr() ); } bool Calendar::isAncestorOf(const Incidence::Ptr &ancestor, const Incidence::Ptr &incidence) const { if (!incidence || incidence->relatedTo().isEmpty()) { return false; } else if (incidence->relatedTo() == ancestor->uid()) { return true; } else { return isAncestorOf(ancestor, this->incidence(incidence->relatedTo())); } } Incidence::List Calendar::relations(const QString &uid) const { return d->mIncidenceRelations[uid]; } Calendar::CalendarObserver::~CalendarObserver() { } void Calendar::CalendarObserver::calendarModified(bool modified, Calendar *calendar) { Q_UNUSED(modified); Q_UNUSED(calendar); } void Calendar::CalendarObserver::calendarIncidenceAdded(const Incidence::Ptr &incidence) { Q_UNUSED(incidence); } void Calendar::CalendarObserver::calendarIncidenceChanged(const Incidence::Ptr &incidence) { Q_UNUSED(incidence); } void Calendar::CalendarObserver::calendarIncidenceAboutToBeDeleted(const Incidence::Ptr &incidence) { Q_UNUSED(incidence); } void Calendar::CalendarObserver::calendarIncidenceDeleted(const Incidence::Ptr &incidence, const Calendar *calendar) { Q_UNUSED(incidence); Q_UNUSED(calendar); } void Calendar::CalendarObserver::calendarIncidenceAdditionCanceled(const Incidence::Ptr &incidence) { Q_UNUSED(incidence); } void Calendar::registerObserver(CalendarObserver *observer) { if (!observer) { return; } if (!d->mObservers.contains(observer)) { d->mObservers.append(observer); } else { d->mNewObserver = true; } } void Calendar::unregisterObserver(CalendarObserver *observer) { if (!observer) { return; } else { d->mObservers.removeAll(observer); } } bool Calendar::isSaving() const { return false; } void Calendar::setModified(bool modified) { if (modified != d->mModified || d->mNewObserver) { d->mNewObserver = false; for (CalendarObserver *observer : qAsConst(d->mObservers)) { observer->calendarModified(modified, this); } d->mModified = modified; } } bool Calendar::isModified() const { return d->mModified; } bool Calendar::save() { return true; } bool Calendar::reload() { return true; } void Calendar::incidenceUpdated(const QString &uid, const QDateTime &recurrenceId) { - Incidence::Ptr inc = incidence(uid, recurrenceId); if (!inc) { return; } inc->setLastModified(QDateTime::currentDateTimeUtc()); // we should probably update the revision number here, // or internally in the Event itself when certain things change. // need to verify with ical documentation. notifyIncidenceChanged(inc); setModified(true); } void Calendar::doSetTimeZone(const QTimeZone &timeZone) { Q_UNUSED(timeZone); } void Calendar::notifyIncidenceAdded(const Incidence::Ptr &incidence) { if (!incidence) { return; } if (!d->mObserversEnabled) { return; } for (CalendarObserver *observer : qAsConst(d->mObservers)) { observer->calendarIncidenceAdded(incidence); } for (auto role : { IncidenceBase::RoleStartTimeZone, IncidenceBase::RoleEndTimeZone }) { const auto dt = incidence->dateTime(role); if (dt.isValid() && dt.timeZone() != QTimeZone::utc()) { if (!d->mTimeZones.contains(dt.timeZone())) { d->mTimeZones.push_back(dt.timeZone()); } } } } void Calendar::notifyIncidenceChanged(const Incidence::Ptr &incidence) { if (!incidence) { return; } if (!d->mObserversEnabled) { return; } for (CalendarObserver *observer : qAsConst(d->mObservers)) { observer->calendarIncidenceChanged(incidence); } } void Calendar::notifyIncidenceAboutToBeDeleted(const Incidence::Ptr &incidence) { if (!incidence) { return; } if (!d->mObserversEnabled) { return; } for (CalendarObserver *observer : qAsConst(d->mObservers)) { observer->calendarIncidenceAboutToBeDeleted(incidence); } } void Calendar::notifyIncidenceDeleted(const Incidence::Ptr &incidence) { if (!incidence) { return; } if (!d->mObserversEnabled) { return; } for (CalendarObserver *observer : qAsConst(d->mObservers)) { observer->calendarIncidenceDeleted(incidence, this); } } void Calendar::notifyIncidenceAdditionCanceled(const Incidence::Ptr &incidence) { if (!incidence) { return; } if (!d->mObserversEnabled) { return; } for (CalendarObserver *observer : qAsConst(d->mObservers)) { observer->calendarIncidenceAdditionCanceled(incidence); } } void Calendar::customPropertyUpdated() { setModified(true); } void Calendar::setProductId(const QString &id) { d->mProductId = id; } QString Calendar::productId() const { return d->mProductId; } /** static */ Incidence::List Calendar::mergeIncidenceList(const Event::List &events, const Todo::List &todos, const Journal::List &journals) { Incidence::List incidences; incidences.reserve(events.count() + todos.count() + journals.count()); int i, end; for (i = 0, end = events.count(); i < end; ++i) { incidences.append(events[i]); } for (i = 0, end = todos.count(); i < end; ++i) { incidences.append(todos[i]); } for (i = 0, end = journals.count(); i < end; ++i) { incidences.append(journals[i]); } return incidences; } bool Calendar::beginChange(const Incidence::Ptr &incidence) { Q_UNUSED(incidence); return true; } bool Calendar::endChange(const Incidence::Ptr &incidence) { Q_UNUSED(incidence); return true; } void Calendar::setObserversEnabled(bool enabled) { d->mObserversEnabled = enabled; } void Calendar::appendAlarms(Alarm::List &alarms, const Incidence::Ptr &incidence, const QDateTime &from, const QDateTime &to) const { QDateTime preTime = from.addSecs(-1); Alarm::List alarmlist = incidence->alarms(); for (int i = 0, iend = alarmlist.count(); i < iend; ++i) { if (alarmlist[i]->enabled()) { QDateTime dt = alarmlist[i]->nextRepetition(preTime); if (dt.isValid() && dt <= to) { qCDebug(KCALCORE_LOG) << incidence->summary() << "':" << dt.toString(); alarms.append(alarmlist[i]); } } } } void Calendar::appendRecurringAlarms(Alarm::List &alarms, const Incidence::Ptr &incidence, const QDateTime &from, const QDateTime &to) const { QDateTime dt; bool endOffsetValid = false; Duration endOffset(0); Duration period(from, to); Alarm::List alarmlist = incidence->alarms(); for (int i = 0, iend = alarmlist.count(); i < iend; ++i) { Alarm::Ptr a = alarmlist[i]; if (a->enabled()) { if (a->hasTime()) { // The alarm time is defined as an absolute date/time dt = a->nextRepetition(from.addSecs(-1)); if (!dt.isValid() || dt > to) { continue; } } else { // Alarm time is defined by an offset from the event start or end time. // Find the offset from the event start time, which is also used as the // offset from the recurrence time. Duration offset(0); if (a->hasStartOffset()) { offset = a->startOffset(); } else if (a->hasEndOffset()) { offset = a->endOffset(); if (!endOffsetValid) { endOffset = Duration(incidence->dtStart(), incidence->dateTime(Incidence::RoleAlarmEndOffset)); endOffsetValid = true; } } // Find the incidence's earliest alarm QDateTime alarmStart = offset.end(a->hasEndOffset() ? incidence->dateTime(Incidence::RoleAlarmEndOffset) : incidence->dtStart()); if (alarmStart > to) { continue; } QDateTime baseStart = incidence->dtStart(); if (from > alarmStart) { alarmStart = from; // don't look earlier than the earliest alarm baseStart = (-offset).end((-endOffset).end(alarmStart)); } // Adjust the 'alarmStart' date/time and find the next recurrence at or after it. // Treate the two offsets separately in case one is daily and the other not. dt = incidence->recurrence()->getNextDateTime(baseStart.addSecs(-1)); if (!dt.isValid() || (dt = endOffset.end(offset.end(dt))) > to) { // adjust 'dt' to get the alarm time // The next recurrence is too late. if (!a->repeatCount()) { continue; } // The alarm has repetitions, so check whether repetitions of previous // recurrences fall within the time period. bool found = false; Duration alarmDuration = a->duration(); for (QDateTime base = baseStart; (dt = incidence->recurrence()->getPreviousDateTime(base)).isValid(); base = dt) { if (a->duration().end(dt) < base) { break; // this recurrence's last repetition is too early, so give up } // The last repetition of this recurrence is at or after 'alarmStart' time. // Check if a repetition occurs between 'alarmStart' and 'to'. int snooze = a->snoozeTime().value(); // in seconds or days if (a->snoozeTime().isDaily()) { Duration toFromDuration(dt, base); int toFrom = toFromDuration.asDays(); if (a->snoozeTime().end(from) <= to || (toFromDuration.isDaily() && toFrom % snooze == 0) || (toFrom / snooze + 1) * snooze <= toFrom + period.asDays()) { found = true; #ifndef NDEBUG // for debug output dt = offset.end(dt).addDays(((toFrom - 1) / snooze + 1) * snooze); #endif break; } } else { int toFrom = dt.secsTo(base); if (period.asSeconds() >= snooze || toFrom % snooze == 0 || (toFrom / snooze + 1) * snooze <= toFrom + period.asSeconds()) { found = true; #ifndef NDEBUG // for debug output dt = offset.end(dt).addSecs(((toFrom - 1) / snooze + 1) * snooze); #endif break; } } } if (!found) { continue; } } } qCDebug(KCALCORE_LOG) << incidence->summary() << "':" << dt.toString(); alarms.append(a); } } } void Calendar::startBatchAdding() { d->batchAddingInProgress = true; } void Calendar::endBatchAdding() { d->batchAddingInProgress = false; } bool Calendar::batchAdding() const { return d->batchAddingInProgress; } void Calendar::setDeletionTracking(bool enable) { d->mDeletionTracking = enable; } bool Calendar::deletionTracking() const { return d->mDeletionTracking; } void Calendar::virtual_hook(int id, void *data) { Q_UNUSED(id); Q_UNUSED(data); Q_ASSERT(false); } diff --git a/src/exceptions.cpp b/src/exceptions.cpp index c499869bf..b9c4bde0e 100644 --- a/src/exceptions.cpp +++ b/src/exceptions.cpp @@ -1,73 +1,76 @@ /* This file is part of the kcalcore library. Copyright (c) 2001 Cornelius Schumacher 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. */ /** @file This file is part of the API for handling calendar data and defines the Exception class. We don't use actual C++ exceptions right now. These classes are currently returned by an error function; but we can build upon them, if/when we start to use C++ exceptions. @brief Exception base class. @author Cornelius Schumacher \ */ #include "exceptions.h" #include "calformat.h" using namespace KCalCore; namespace KCalCore { -class ExceptionPrivate { + +class ExceptionPrivate +{ public: /** The current exception code. */ Exception::ErrorCode mCode; /** Arguments to pass to i18n(). */ QStringList mArguments; }; + } Exception::Exception(const ErrorCode code, const QStringList &arguments) : d(new ExceptionPrivate) { d->mCode = code; d->mArguments = arguments; } Exception::~Exception() { } Exception::ErrorCode Exception::code() const { return d->mCode; } QStringList Exception::arguments() const { return d->mArguments; } diff --git a/src/freebusy.cpp b/src/freebusy.cpp index d66d4519a..0fc0d00ba 100644 --- a/src/freebusy.cpp +++ b/src/freebusy.cpp @@ -1,434 +1,434 @@ /* This file is part of the kcalcore library. Copyright (c) 2001 Cornelius Schumacher Copyright (C) 2004 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. */ /** @file This file is part of the API for handling calendar data and defines the FreeBusy class. @brief Provides information about the free/busy time of a calendar user. @author Cornelius Schumacher \ @author Reinhold Kainhofer \ */ #include "freebusy.h" #include "visitor.h" #include "utils.h" #include "icalformat.h" #include "kcalcore_debug.h" #include using namespace KCalCore; //@cond PRIVATE class Q_DECL_HIDDEN KCalCore::FreeBusy::Private { private: FreeBusy *q; public: Private(FreeBusy *qq) : q(qq) {} Private(const KCalCore::FreeBusy::Private &other, FreeBusy *qq) : q(qq) { init(other); } Private(const FreeBusyPeriod::List &busyPeriods, FreeBusy *qq) : q(qq), mBusyPeriods(busyPeriods) {} void init(const KCalCore::FreeBusy::Private &other); void init(const Event::List &events, const QDateTime &start, const QDateTime &end); QDateTime mDtEnd; // end datetime FreeBusyPeriod::List mBusyPeriods; // list of periods // This is used for creating a freebusy object for the current user bool addLocalPeriod(FreeBusy *fb, const QDateTime &start, const QDateTime &end); }; void KCalCore::FreeBusy::Private::init(const KCalCore::FreeBusy::Private &other) { mDtEnd = other.mDtEnd; mBusyPeriods = other.mBusyPeriods; } //@endcond FreeBusy::FreeBusy() : d(new KCalCore::FreeBusy::Private(this)) { } FreeBusy::FreeBusy(const FreeBusy &other) : IncidenceBase(other), d(new KCalCore::FreeBusy::Private(*other.d, this)) { } FreeBusy::FreeBusy(const QDateTime &start, const QDateTime &end) : d(new KCalCore::FreeBusy::Private(this)) { setDtStart(start); setDtEnd(end); } FreeBusy::FreeBusy(const Event::List &events, const QDateTime &start, const QDateTime &end) : d(new KCalCore::FreeBusy::Private(this)) { setDtStart(start); setDtEnd(end); d->init(events, start, end); } //@cond PRIVATE void FreeBusy::Private::init(const Event::List &eventList, const QDateTime &start, const QDateTime &end) { qint64 extraDays; int i; int x; qint64 duration = start.daysTo(end); QDate day; QDateTime tmpStart; QDateTime tmpEnd; // Loops through every event in the calendar Event::List::ConstIterator it; for (it = eventList.constBegin(); it != eventList.constEnd(); ++it) { Event::Ptr event = *it; // If this event is transparent it shouldn't be in the freebusy list. if (event->transparency() == Event::Transparent) { continue; } // The code below can not handle all-day events. Fixing this resulted // in a lot of duplicated code. Instead, make a copy of the event and // set the period to the full day(s). This trick works for recurring, // multiday, and single day all-day events. Event::Ptr allDayEvent; if (event->allDay()) { // addDay event. Do the hack qCDebug(KCALCORE_LOG) << "All-day event"; allDayEvent = Event::Ptr(new Event(*event)); // Set the start and end times to be on midnight QDateTime st = allDayEvent->dtStart(); st.setTime(QTime(0, 0)); QDateTime nd = allDayEvent->dtEnd(); nd.setTime(QTime(23, 59, 59, 999)); allDayEvent->setAllDay(false); allDayEvent->setDtStart(st); allDayEvent->setDtEnd(nd); qCDebug(KCALCORE_LOG) << "Use:" << st.toString() << "to" << nd.toString(); // Finally, use this event for the setting below event = allDayEvent; } // This whole for loop is for recurring events, it loops through // each of the days of the freebusy request for (i = 0; i <= duration; ++i) { day = start.addDays(i).date(); tmpStart.setDate(day); tmpEnd.setDate(day); if (event->recurs()) { if (event->isMultiDay()) { // FIXME: This doesn't work for sub-daily recurrences or recurrences with // a different time than the original event. extraDays = event->dtStart().daysTo(event->dtEnd()); for (x = 0; x <= extraDays; ++x) { if (event->recursOn(day.addDays(-x), start.timeZone())) { tmpStart.setDate(day.addDays(-x)); tmpStart.setTime(event->dtStart().time()); tmpEnd = event->duration().end(tmpStart); addLocalPeriod(q, tmpStart, tmpEnd); break; } } } else { if (event->recursOn(day, start.timeZone())) { tmpStart.setTime(event->dtStart().time()); tmpEnd.setTime(event->dtEnd().time()); addLocalPeriod(q, tmpStart, tmpEnd); } } } - } + // Non-recurring events addLocalPeriod(q, event->dtStart(), event->dtEnd()); } q->sortList(); } //@endcond FreeBusy::FreeBusy(const Period::List &busyPeriods) : d(new KCalCore::FreeBusy::Private(this)) { addPeriods(busyPeriods); } FreeBusy::FreeBusy(const FreeBusyPeriod::List &busyPeriods) : d(new KCalCore::FreeBusy::Private(busyPeriods, this)) { } FreeBusy::~FreeBusy() { delete d; } IncidenceBase::IncidenceType FreeBusy::type() const { return TypeFreeBusy; } QByteArray FreeBusy::typeStr() const { return QByteArrayLiteral("FreeBusy"); } void FreeBusy::setDtStart(const QDateTime &start) { IncidenceBase::setDtStart(start.toUTC()); updated(); } void FreeBusy::setDtEnd(const QDateTime &end) { d->mDtEnd = end; } QDateTime FreeBusy::dtEnd() const { return d->mDtEnd; } Period::List FreeBusy::busyPeriods() const { Period::List res; res.reserve(d->mBusyPeriods.count()); for (const FreeBusyPeriod &p : qAsConst(d->mBusyPeriods)) { res << p; } return res; } FreeBusyPeriod::List FreeBusy::fullBusyPeriods() const { return d->mBusyPeriods; } void FreeBusy::sortList() { std::sort(d->mBusyPeriods.begin(), d->mBusyPeriods.end()); return; } void FreeBusy::addPeriods(const Period::List &list) { d->mBusyPeriods.reserve(d->mBusyPeriods.count() + list.count()); for (const Period &p : qAsConst(list)) { d->mBusyPeriods << FreeBusyPeriod(p); } sortList(); } void FreeBusy::addPeriods(const FreeBusyPeriod::List &list) { d->mBusyPeriods += list; sortList(); } void FreeBusy::addPeriod(const QDateTime &start, const QDateTime &end) { d->mBusyPeriods.append(FreeBusyPeriod(start, end)); sortList(); } void FreeBusy::addPeriod(const QDateTime &start, const Duration &duration) { d->mBusyPeriods.append(FreeBusyPeriod(start, duration)); sortList(); } void FreeBusy::merge(const FreeBusy::Ptr &freeBusy) { if (freeBusy->dtStart() < dtStart()) { setDtStart(freeBusy->dtStart()); } if (freeBusy->dtEnd() > dtEnd()) { setDtEnd(freeBusy->dtEnd()); } Period::List periods = freeBusy->busyPeriods(); Period::List::ConstIterator it; d->mBusyPeriods.reserve(d->mBusyPeriods.count() + periods.count()); for (it = periods.constBegin(); it != periods.constEnd(); ++it) { d->mBusyPeriods.append(FreeBusyPeriod((*it).start(), (*it).end())); } sortList(); } void FreeBusy::shiftTimes(const QTimeZone &oldZone, const QTimeZone &newZone) { if (oldZone.isValid() && newZone.isValid() && oldZone != newZone) { IncidenceBase::shiftTimes(oldZone, newZone); d->mDtEnd = d->mDtEnd.toTimeZone(oldZone); d->mDtEnd.setTimeZone(newZone); for (FreeBusyPeriod p : qAsConst(d->mBusyPeriods)) { p.shiftTimes(oldZone, newZone); } } } IncidenceBase &FreeBusy::assign(const IncidenceBase &other) { if (&other != this) { IncidenceBase::assign(other); const FreeBusy *f = static_cast(&other); d->init(*(f->d)); } return *this; } bool FreeBusy::equals(const IncidenceBase &freeBusy) const { if (!IncidenceBase::equals(freeBusy)) { return false; } else { // If they weren't the same type IncidenceBase::equals would had returned false already const FreeBusy *fb = static_cast(&freeBusy); return dtEnd() == fb->dtEnd() && d->mBusyPeriods == fb->d->mBusyPeriods; } } bool FreeBusy::accept(Visitor &v, const IncidenceBase::Ptr &incidence) { return v.visit(incidence.staticCast()); } QDateTime FreeBusy::dateTime(DateTimeRole role) const { Q_UNUSED(role); // No roles affecting freeBusy yet return QDateTime(); } void FreeBusy::setDateTime(const QDateTime &dateTime, DateTimeRole role) { Q_UNUSED(dateTime); Q_UNUSED(role); } void FreeBusy::virtual_hook(VirtualHook id, void *data) { Q_UNUSED(id); Q_UNUSED(data); Q_ASSERT(false); } //@cond PRIVATE bool FreeBusy::Private::addLocalPeriod(FreeBusy *fb, const QDateTime &eventStart, const QDateTime &eventEnd) { QDateTime tmpStart; QDateTime tmpEnd; //Check to see if the start *or* end of the event is //between the start and end of the freebusy dates. QDateTime start = fb->dtStart(); if (!(((start.secsTo(eventStart) >= 0) && (eventStart.secsTo(mDtEnd) >= 0)) || ((start.secsTo(eventEnd) >= 0) && (eventEnd.secsTo(mDtEnd) >= 0)))) { return false; } if (eventStart.secsTo(start) >= 0) { tmpStart = start; } else { tmpStart = eventStart; } if (eventEnd.secsTo(mDtEnd) <= 0) { tmpEnd = mDtEnd; } else { tmpEnd = eventEnd; } FreeBusyPeriod p(tmpStart, tmpEnd); mBusyPeriods.append(p); return true; } //@endcond QLatin1String FreeBusy::mimeType() const { return FreeBusy::freeBusyMimeType(); } QLatin1String KCalCore::FreeBusy::freeBusyMimeType() { return QLatin1String("application/x-vnd.akonadi.calendar.freebusy"); } QDataStream &KCalCore::operator<<(QDataStream &stream, const KCalCore::FreeBusy::Ptr &freebusy) { KCalCore::ICalFormat format; QString data = format.createScheduleMessage(freebusy, iTIPPublish); return stream << data; } QDataStream &KCalCore::operator>>(QDataStream &stream, KCalCore::FreeBusy::Ptr &freebusy) { QString freeBusyVCal; stream >> freeBusyVCal; KCalCore::ICalFormat format; freebusy = format.parseFreeBusy(freeBusyVCal); if (!freebusy) { qCDebug(KCALCORE_LOG) << "Error parsing free/busy"; qCDebug(KCALCORE_LOG) << freeBusyVCal; } return stream; } diff --git a/src/icaltimezones.cpp b/src/icaltimezones.cpp index fdd327168..9c669759e 100644 --- a/src/icaltimezones.cpp +++ b/src/icaltimezones.cpp @@ -1,737 +1,736 @@ /* This file is part of the kcalcore library. Copyright (c) 2005-2007 David Jarvie 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 "icaltimezones_p.h" #include "icalformat.h" #include "icalformat_p.h" #include "recurrence.h" #include "recurrencerule.h" #include "recurrencehelper_p.h" #include "utils.h" #include "kcalcore_debug.h" #include #include extern "C" { #include #include } using namespace KCalCore; // Minimum repetition counts for VTIMEZONE RRULEs static const int minRuleCount = 5; // for any RRULE static const int minPhaseCount = 8; // for separate STANDARD/DAYLIGHT component // Convert an ical time to QDateTime, preserving the UTC indicator static QDateTime toQDateTime(const icaltimetype &t) { return QDateTime(QDate(t.year, t.month, t.day), QTime(t.hour, t.minute, t.second), #if defined(USE_ICAL_3) (icaltime_is_utc(t) ? Qt::UTC : Qt::LocalTime)); #else (t.is_utc ? Qt::UTC : Qt::LocalTime)); #endif } // Maximum date for time zone data. // It's not sensible to try to predict them very far in advance, because // they can easily change. Plus, it limits the processing required. static QDateTime MAX_DATE() { static QDateTime dt; if (!dt.isValid()) { dt = QDateTime(QDate::currentDate().addYears(20), QTime(0, 0, 0)); } return dt; } static icaltimetype writeLocalICalDateTime(const QDateTime &utc, int offset) { const QDateTime local = utc.addSecs(offset); icaltimetype t = icaltime_null_time(); t.year = local.date().year(); t.month = local.date().month(); t.day = local.date().day(); t.hour = local.time().hour(); t.minute = local.time().minute(); t.second = local.time().second(); t.is_date = 0; t.zone = nullptr; #if !defined(USE_ICAL_3) t.is_utc = 0; #endif return t; } namespace KCalCore { void ICalTimeZonePhase::dump() { qDebug() << " ~~~ ICalTimeZonePhase ~~~"; qDebug() << " Abbreviations:" << abbrevs; qDebug() << " UTC offset:" << utcOffset; qDebug() << " Transitions:" << transitions; qDebug() << " ~~~~~~~~~~~~~~~~~~~~~~~~~"; } void ICalTimeZone::dump() { qDebug() << "~~~ ICalTimeZone ~~~"; qDebug() << "ID:" << id; qDebug() << "QZONE:" << qZone.id(); qDebug() << "STD:"; standard.dump(); qDebug() << "DST:"; daylight.dump(); qDebug() << "~~~~~~~~~~~~~~~~~~~~"; } ICalTimeZoneCache::ICalTimeZoneCache() { } void ICalTimeZoneCache::insert(const QByteArray &id, const ICalTimeZone &tz) { mCache.insert(id, tz); } namespace { template typename T::const_iterator greatestSmallerThan(const T &c, const typename T::value_type &v) { auto it = std::lower_bound(c.cbegin(), c.cend(), v); if (it != c.cbegin()) { return --it; } return c.cend(); } } QTimeZone ICalTimeZoneCache::tzForTime(const QDateTime &dt, const QByteArray &tzid) const { if (QTimeZone::isTimeZoneIdAvailable(tzid)) { return QTimeZone(tzid); } const ICalTimeZone tz = mCache.value(tzid); if (!tz.qZone.isValid()) { return QTimeZone::systemTimeZone(); } // If the matched timezone is one of the UTC offset timezones, we need to make // sure it's in the correct DTS. // The lookup in ICalTimeZoneParser will only find TZ in standard time, but // if the datetim in question fits in the DTS zone, we need to use another UTC // offset timezone if (tz.qZone.id().startsWith("UTC")) { //krazy:exclude=strings // Find the nearest standard and DST transitions that occur BEFORE the "dt" const auto stdPrev = greatestSmallerThan(tz.standard.transitions, dt); const auto dstPrev = greatestSmallerThan(tz.daylight.transitions, dt); if (stdPrev != tz.standard.transitions.cend() && dstPrev != tz.daylight.transitions.cend()) { if (*dstPrev > *stdPrev) { // Previous DTS is closer to "dt" than previous standard, which // means we are in DTS right now const auto tzids = QTimeZone::availableTimeZoneIds(tz.daylight.utcOffset); auto dtsTzId = std::find_if(tzids.cbegin(), tzids.cend(), [](const QByteArray &id) { return id.startsWith("UTC"); //krazy:exclude=strings }); if (dtsTzId != tzids.cend()) { return QTimeZone(*dtsTzId); } } } } return tz.qZone; } ICalTimeZoneParser::ICalTimeZoneParser(ICalTimeZoneCache *cache) : mCache(cache) { } void ICalTimeZoneParser::updateTzEarliestDate(const IncidenceBase::Ptr &incidence, TimeZoneEarliestDate *earliest) { for (auto role : { IncidenceBase::RoleStartTimeZone, IncidenceBase::RoleEndTimeZone }) { const auto dt = incidence->dateTime(role); if (dt.isValid()) { if (dt.timeZone() == QTimeZone::utc()) { continue; } const auto prev = earliest->value(incidence->dtStart().timeZone()); if (!prev.isValid() || incidence->dtStart() < prev) { earliest->insert(incidence->dtStart().timeZone(), prev); } } } } - icalcomponent *ICalTimeZoneParser::icalcomponentFromQTimeZone(const QTimeZone &tz, const QDateTime &earliest) { // VTIMEZONE RRULE types enum { DAY_OF_MONTH = 0x01, WEEKDAY_OF_MONTH = 0x02, LAST_WEEKDAY_OF_MONTH = 0x04 }; // Write the time zone data into an iCal component icalcomponent *tzcomp = icalcomponent_new(ICAL_VTIMEZONE_COMPONENT); icalcomponent_add_property(tzcomp, icalproperty_new_tzid(tz.id().constData())); // icalcomponent_add_property(tzcomp, icalproperty_new_location( tz.name().toUtf8() )); // Compile an ordered list of transitions so that we can know the phases // which occur before and after each transition. QTimeZone::OffsetDataList transits = tz.transitions(QDateTime(), MAX_DATE()); if (transits.isEmpty()) { // If there is no way to compile a complete list of transitions // transitions() can return an empty list // In that case try get one transition to write a valid VTIMEZONE entry. if (transits.isEmpty()) { qCDebug(KCALCORE_LOG) << "No transition information available VTIMEZONE will be invalid."; } } if (earliest.isValid()) { // Remove all transitions earlier than those we are interested in for (int i = 0, end = transits.count(); i < end; ++i) { if (transits.at(i).atUtc >= earliest) { if (i > 0) { transits.erase(transits.begin(), transits.begin() + i); } break; } } } int trcount = transits.count(); QVector transitionsDone(trcount, false); // Go through the list of transitions and create an iCal component for each // distinct combination of phase after and UTC offset before the transition. icaldatetimeperiodtype dtperiod; dtperiod.period = icalperiodtype_null_period(); for (;;) { int i = 0; for (; i < trcount && transitionsDone[i]; ++i) { ; } if (i >= trcount) { break; } // Found a phase combination which hasn't yet been processed const int preOffset = (i > 0) ? transits.at(i - 1).offsetFromUtc : 0; const auto transit = transits.at(i); if (transit.offsetFromUtc == preOffset) { transitionsDone[i] = true; while (++i < trcount) { if (transitionsDone[i] || transits.at(i).offsetFromUtc != transit.offsetFromUtc || transits.at(i).daylightTimeOffset != transit.daylightTimeOffset || transits.at(i - 1).offsetFromUtc != preOffset) { continue; } transitionsDone[i] = true; } continue; } const bool isDst = transit.daylightTimeOffset > 0; icalcomponent *phaseComp = icalcomponent_new(isDst ? ICAL_XDAYLIGHT_COMPONENT : ICAL_XSTANDARD_COMPONENT); if (!transit.abbreviation.isEmpty()) { icalcomponent_add_property(phaseComp, icalproperty_new_tzname( static_cast(transit.abbreviation.toUtf8().constData()))); } icalcomponent_add_property(phaseComp, icalproperty_new_tzoffsetfrom(preOffset)); icalcomponent_add_property(phaseComp, icalproperty_new_tzoffsetto(transit.offsetFromUtc)); // Create a component to hold initial RRULE if any, plus all RDATEs icalcomponent *phaseComp1 = icalcomponent_new_clone(phaseComp); icalcomponent_add_property(phaseComp1, icalproperty_new_dtstart( writeLocalICalDateTime(transits.at(i).atUtc, preOffset))); bool useNewRRULE = false; // Compile the list of UTC transition dates/times, and check // if the list can be reduced to an RRULE instead of multiple RDATEs. QTime time; QDate date; int year = 0, month = 0, daysInMonth = 0, dayOfMonth = 0; // avoid compiler warnings int dayOfWeek = 0; // Monday = 1 int nthFromStart = 0; // nth (weekday) of month int nthFromEnd = 0; // nth last (weekday) of month int newRule; int rule = 0; QList rdates;// dates which (probably) need to be written as RDATEs QList times; QDateTime qdt = transits.at(i).atUtc; // set 'qdt' for start of loop times += qdt; transitionsDone[i] = true; do { if (!rule) { // Initialise data for detecting a new rule rule = DAY_OF_MONTH | WEEKDAY_OF_MONTH | LAST_WEEKDAY_OF_MONTH; time = qdt.time(); date = qdt.date(); year = date.year(); month = date.month(); daysInMonth = date.daysInMonth(); dayOfWeek = date.dayOfWeek(); // Monday = 1 dayOfMonth = date.day(); nthFromStart = (dayOfMonth - 1) / 7 + 1; // nth (weekday) of month nthFromEnd = (daysInMonth - dayOfMonth) / 7 + 1; // nth last (weekday) of month } if (++i >= trcount) { newRule = 0; times += QDateTime(); // append a dummy value since last value in list is ignored } else { if (transitionsDone[i] || transits.at(i).offsetFromUtc != transit.offsetFromUtc || transits.at(i).daylightTimeOffset != transit.daylightTimeOffset || transits.at(i - 1).offsetFromUtc != preOffset) { continue; } transitionsDone[i] = true; qdt = transits.at(i).atUtc; if (!qdt.isValid()) { continue; } newRule = rule; times += qdt; date = qdt.date(); if (qdt.time() != time || date.month() != month || date.year() != ++year) { newRule = 0; } else { const int day = date.day(); if ((newRule & DAY_OF_MONTH) && day != dayOfMonth) { newRule &= ~DAY_OF_MONTH; } if (newRule & (WEEKDAY_OF_MONTH | LAST_WEEKDAY_OF_MONTH)) { if (date.dayOfWeek() != dayOfWeek) { newRule &= ~(WEEKDAY_OF_MONTH | LAST_WEEKDAY_OF_MONTH); } else { if ((newRule & WEEKDAY_OF_MONTH) && (day - 1) / 7 + 1 != nthFromStart) { newRule &= ~WEEKDAY_OF_MONTH; } if ((newRule & LAST_WEEKDAY_OF_MONTH) && (daysInMonth - day) / 7 + 1 != nthFromEnd) { newRule &= ~LAST_WEEKDAY_OF_MONTH; } } } } } if (!newRule) { // The previous rule (if any) no longer applies. // Write all the times up to but not including the current one. // First check whether any of the last RDATE values fit this rule. int yr = times[0].date().year(); while (!rdates.isEmpty()) { qdt = rdates.last(); date = qdt.date(); if (qdt.time() != time || date.month() != month || date.year() != --yr) { break; } const int day = date.day(); if (rule & DAY_OF_MONTH) { if (day != dayOfMonth) { break; } } else { if (date.dayOfWeek() != dayOfWeek || ((rule & WEEKDAY_OF_MONTH) && (day - 1) / 7 + 1 != nthFromStart) || ((rule & LAST_WEEKDAY_OF_MONTH) && (daysInMonth - day) / 7 + 1 != nthFromEnd)) { break; } } times.prepend(qdt); rdates.pop_back(); } if (times.count() > (useNewRRULE ? minPhaseCount : minRuleCount)) { // There are enough dates to combine into an RRULE icalrecurrencetype r; icalrecurrencetype_clear(&r); r.freq = ICAL_YEARLY_RECURRENCE; r.count = (year >= 2030) ? 0 : times.count() - 1; r.by_month[0] = month; if (rule & DAY_OF_MONTH) { r.by_month_day[0] = dayOfMonth; } else if (rule & WEEKDAY_OF_MONTH) { r.by_day[0] = (dayOfWeek % 7 + 1) + (nthFromStart * 8); // Sunday = 1 } else if (rule & LAST_WEEKDAY_OF_MONTH) { r.by_day[0] = -(dayOfWeek % 7 + 1) - (nthFromEnd * 8); // Sunday = 1 } r.until = writeLocalICalDateTime(times.takeAt(times.size() - 1), preOffset); icalproperty *prop = icalproperty_new_rrule(r); if (useNewRRULE) { // This RRULE doesn't start from the phase start date, so set it into // a new STANDARD/DAYLIGHT component in the VTIMEZONE. icalcomponent *c = icalcomponent_new_clone(phaseComp); icalcomponent_add_property( c, icalproperty_new_dtstart(writeLocalICalDateTime(times[0], preOffset))); icalcomponent_add_property(c, prop); icalcomponent_add_component(tzcomp, c); } else { icalcomponent_add_property(phaseComp1, prop); } } else { // Save dates for writing as RDATEs for (int t = 0, tend = times.count() - 1; t < tend; ++t) { rdates += times[t]; } } useNewRRULE = true; // All date/time values but the last have been added to the VTIMEZONE. // Remove them from the list. qdt = times.last(); // set 'qdt' for start of loop times.clear(); times += qdt; } rule = newRule; } while (i < trcount); // Write remaining dates as RDATEs for (int rd = 0, rdend = rdates.count(); rd < rdend; ++rd) { dtperiod.time = writeLocalICalDateTime(rdates[rd], preOffset); icalcomponent_add_property(phaseComp1, icalproperty_new_rdate(dtperiod)); } icalcomponent_add_component(tzcomp, phaseComp1); icalcomponent_free(phaseComp); } return tzcomp; } icaltimezone *ICalTimeZoneParser::icaltimezoneFromQTimeZone(const QTimeZone &tz, const QDateTime &earliest) { auto itz = icaltimezone_new(); icaltimezone_set_component(itz, icalcomponentFromQTimeZone(tz, earliest)); return itz; } void ICalTimeZoneParser::parse(icalcomponent *calendar) { for (auto *c = icalcomponent_get_first_component(calendar, ICAL_VTIMEZONE_COMPONENT); c; c = icalcomponent_get_next_component(calendar, ICAL_VTIMEZONE_COMPONENT)) { auto icalZone = parseTimeZone(c); //icalZone.dump(); if (!icalZone.id.isEmpty()) { if (!icalZone.qZone.isValid()) { icalZone.qZone = resolveICalTimeZone(icalZone); } if (!icalZone.qZone.isValid()) { qCWarning(KCALCORE_LOG) << "Failed to map" << icalZone.id << "to a known IANA timezone"; continue; } mCache->insert(icalZone.id, icalZone); } } } QTimeZone ICalTimeZoneParser::resolveICalTimeZone(const ICalTimeZone &icalZone) { const auto phase = icalZone.standard; const auto now = QDateTime::currentDateTimeUtc(); const auto candidates = QTimeZone::availableTimeZoneIds(phase.utcOffset); QMap matchedCandidates; for (const auto &tzid : candidates) { const QTimeZone candidate(tzid); // This would be a fallback, candidate has transitions, but the phase does not if (candidate.hasTransitions() == phase.transitions.isEmpty()) { matchedCandidates.insert(0, candidate); continue; } // Without transitions, we can't do any more precise matching, so just // accept this candidate and be done with it if (!candidate.hasTransitions() && phase.transitions.isEmpty()) { return candidate; } // Calculate how many transitions this candidate shares with the phase. // The candidate with the most matching transitions will win. auto begin = std::lower_bound(phase.transitions.cbegin(), phase.transitions.cend(), now.addYears(-20)); // If no transition older than 20 years is found, we will start from beginning if (begin == phase.transitions.cend()) { begin = phase.transitions.cbegin(); } auto end = std::upper_bound(begin, phase.transitions.cend(), now); int matchedTransitions = 0; for (auto it = begin; it != end; ++it) { const auto &transition = *it; const QTimeZone::OffsetDataList candidateTransitions = candidate.transitions(transition, transition); if (candidateTransitions.isEmpty()) { continue; } ++matchedTransitions; // 1 point for a matching transition const auto candidateTransition = candidateTransitions[0]; // FIXME: THIS IS HOW IT SHOULD BE: //const auto abvs = transition.abbreviations(); const auto abvs = phase.abbrevs; for (const auto &abv : abvs) { if (candidateTransition.abbreviation == QString::fromUtf8(abv)) { matchedTransitions += 1024; // lots of points for a transition with a matching abbreviation break; } } } matchedCandidates.insert(matchedTransitions, candidate); } if (!matchedCandidates.isEmpty()) { return matchedCandidates.value(matchedCandidates.lastKey()); } return {}; } ICalTimeZone ICalTimeZoneParser::parseTimeZone(icalcomponent *vtimezone) { ICalTimeZone icalTz; if (auto tzidProp = icalcomponent_get_first_property(vtimezone, ICAL_TZID_PROPERTY)) { icalTz.id = icalproperty_get_value_as_string(tzidProp); // If the VTIMEZONE is a known IANA time zone don't bother parsing the rest // of the VTIMEZONE, get QTimeZone directly from Qt if (QTimeZone::isTimeZoneIdAvailable(icalTz.id)) { icalTz.qZone = QTimeZone(icalTz.id); return icalTz; } else { // Not IANA, but maybe we can match it from Windows ID? const auto ianaTzid = QTimeZone::windowsIdToDefaultIanaId(icalTz.id); if (!ianaTzid.isEmpty()) { icalTz.qZone = QTimeZone(ianaTzid); return icalTz; } } } for (icalcomponent *c = icalcomponent_get_first_component(vtimezone, ICAL_ANY_COMPONENT); c; c = icalcomponent_get_next_component(vtimezone, ICAL_ANY_COMPONENT)) { icalcomponent_kind kind = icalcomponent_isa(c); switch (kind) { case ICAL_XSTANDARD_COMPONENT: //qCDebug(KCALCORE_LOG) << "---standard phase: found"; parsePhase(c, false, icalTz.standard); break; case ICAL_XDAYLIGHT_COMPONENT: //qCDebug(KCALCORE_LOG) << "---daylight phase: found"; parsePhase(c, true, icalTz.daylight); break; default: qCDebug(KCALCORE_LOG) << "Unknown component:" << int(kind); break; } } return icalTz; } bool ICalTimeZoneParser::parsePhase(icalcomponent *c, bool daylight, ICalTimeZonePhase &phase) { // Read the observance data for this standard/daylight savings phase int utcOffset = 0; int prevOffset = 0; bool recurs = false; bool found_dtstart = false; bool found_tzoffsetfrom = false; bool found_tzoffsetto = false; icaltimetype dtstart = icaltime_null_time(); QSet abbrevs; // Now do the ical reading. icalproperty *p = icalcomponent_get_first_property(c, ICAL_ANY_PROPERTY); while (p) { icalproperty_kind kind = icalproperty_isa(p); switch (kind) { case ICAL_TZNAME_PROPERTY: { // abbreviated name for this time offset // TZNAME can appear multiple times in order to provide language // translations of the time zone offset name. // TODO: Does this cope with multiple language specifications? QByteArray name = icalproperty_get_tzname(p); // Outlook (2000) places "Standard Time" and "Daylight Time" in the TZNAME // strings, which is totally useless. So ignore those. if ((!daylight && name == "Standard Time") || (daylight && name == "Daylight Time")) { break; } abbrevs.insert(name); break; } case ICAL_DTSTART_PROPERTY: // local time at which phase starts dtstart = icalproperty_get_dtstart(p); found_dtstart = true; break; case ICAL_TZOFFSETFROM_PROPERTY: // UTC offset immediately before start of phase prevOffset = icalproperty_get_tzoffsetfrom(p); found_tzoffsetfrom = true; break; case ICAL_TZOFFSETTO_PROPERTY: utcOffset = icalproperty_get_tzoffsetto(p); found_tzoffsetto = true; break; case ICAL_RDATE_PROPERTY: case ICAL_RRULE_PROPERTY: recurs = true; break; default: break; } p = icalcomponent_get_next_property(c, ICAL_ANY_PROPERTY); } // Validate the phase data if (!found_dtstart || !found_tzoffsetfrom || !found_tzoffsetto) { qCDebug(KCALCORE_LOG) << "DTSTART/TZOFFSETFROM/TZOFFSETTO missing"; return false; } // Convert DTSTART to QDateTime, and from local time to UTC const QDateTime localStart = toQDateTime(dtstart); // local time dtstart.second -= prevOffset; #if defined(USE_ICAL_3) dtstart = icaltime_convert_to_zone(dtstart, icaltimezone_get_utc_timezone()); #else dtstart.is_utc = 1; #endif const QDateTime utcStart = toQDateTime(icaltime_normalize(dtstart)); // UTC phase.abbrevs.unite(abbrevs); phase.utcOffset = utcOffset; phase.transitions += utcStart; if (recurs) { /* RDATE or RRULE is specified. There should only be one or the other, but * it doesn't really matter - the code can cope with both. * Note that we had to get DTSTART, TZOFFSETFROM, TZOFFSETTO before reading * recurrences. */ const QDateTime klocalStart(localStart); const QDateTime maxTime(MAX_DATE()); Recurrence recur; icalproperty *p = icalcomponent_get_first_property(c, ICAL_ANY_PROPERTY); while (p) { icalproperty_kind kind = icalproperty_isa(p); switch (kind) { case ICAL_RDATE_PROPERTY: { icaltimetype t = icalproperty_get_rdate(p).time; if (icaltime_is_date(t)) { // RDATE with a DATE value inherits the (local) time from DTSTART t.hour = dtstart.hour; t.minute = dtstart.minute; t.second = dtstart.second; #if !defined(USE_ICAL_3) t.is_utc = 0; // dtstart is in local time #endif t.is_date = 0; } // RFC2445 states that RDATE must be in local time, // but we support UTC as well to be safe. #if defined(USE_ICAL_3) if (!icaltime_is_utc(t)) { #else if (!t.is_utc) { #endif t.second -= prevOffset; // convert to UTC #if defined(USE_ICAL_3) t = icaltime_convert_to_zone(t, icaltimezone_get_utc_timezone()); #else t.is_utc = 1; #endif t = icaltime_normalize(t); } phase.transitions += toQDateTime(t); break; } case ICAL_RRULE_PROPERTY: { RecurrenceRule r; ICalFormat icf; ICalFormatImpl impl(&icf); impl.readRecurrence(icalproperty_get_rrule(p), &r); r.setStartDt(klocalStart); // The end date time specified in an RRULE should be in UTC. // Convert to local time to avoid timesInInterval() getting things wrong. if (r.duration() == 0) { QDateTime end(r.endDt()); if (end.timeSpec() == Qt::UTC) { end.setTimeSpec(Qt::LocalTime); r.setEndDt(end.addSecs(prevOffset)); } } const auto dts = r.timesInInterval(klocalStart, maxTime); for (int i = 0, end = dts.count(); i < end; ++i) { QDateTime utc = dts[i]; utc.setTimeSpec(Qt::UTC); phase.transitions += utc.addSecs(-prevOffset); } break; } default: break; } p = icalcomponent_get_next_property(c, ICAL_ANY_PROPERTY); } sortAndRemoveDuplicates(phase.transitions); } return true; } QByteArray ICalTimeZoneParser::vcaltimezoneFromQTimeZone(const QTimeZone &qtz, const QDateTime &earliest) { auto icalTz = icalcomponentFromQTimeZone(qtz, earliest); const QByteArray result(icalcomponent_as_ical_string(icalTz)); icalmemory_free_ring(); icalcomponent_free(icalTz); return result; } } // namespace KCalCore diff --git a/src/incidence.cpp b/src/incidence.cpp index a1ab2065a..8d6137bdd 100644 --- a/src/incidence.cpp +++ b/src/incidence.cpp @@ -1,1217 +1,1217 @@ /* This file is part of the kcalcore library. Copyright (c) 2001 Cornelius Schumacher Copyright (C) 2003-2004 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. */ /** @file This file is part of the API for handling calendar data and defines the Incidence class. @brief Provides the class common to non-FreeBusy (Events, To-dos, Journals) calendar components known as incidences. @author Cornelius Schumacher \ @author Reinhold Kainhofer \ */ #include "incidence.h" #include "calformat.h" #include "utils.h" #include #include #include // for .toHtmlEscaped() and Qt::mightBeRichText() #include #include #include #include using namespace KCalCore; /** Private class that helps to provide binary compatibility between releases. @internal */ //@cond PRIVATE class Q_DECL_HIDDEN KCalCore::Incidence::Private { public: Private() : mGeoLatitude(INVALID_LATLON) , mGeoLongitude(INVALID_LATLON) , mRecurrence(nullptr) , mRevision(0) , mPriority(0) , mStatus(StatusNone) , mSecrecy(SecrecyPublic) , mDescriptionIsRich(false) , mSummaryIsRich(false) , mLocationIsRich(false) , mHasGeo(false) , mThisAndFuture(false) , mLocalOnly(false) { } Private(const Private &p) : mCreated(p.mCreated) , mDescription(p.mDescription) , mSummary(p.mSummary) , mLocation(p.mLocation) , mCategories(p.mCategories) , mResources(p.mResources) , mStatusString(p.mStatusString) , mSchedulingID(p.mSchedulingID) , mRelatedToUid(p.mRelatedToUid) , mRecurrenceId(p.mRecurrenceId) , mGeoLatitude(p.mGeoLatitude) , mGeoLongitude(p.mGeoLongitude) , mRecurrence(nullptr) , mRevision(p.mRevision) , mPriority(p.mPriority) , mStatus(p.mStatus) , mSecrecy(p.mSecrecy) , mDescriptionIsRich(p.mDescriptionIsRich) , mSummaryIsRich(p.mSummaryIsRich) , mLocationIsRich(p.mLocationIsRich) , mHasGeo(p.mHasGeo) , mThisAndFuture(p.mThisAndFuture) , mLocalOnly(false) { } void clear() { mAlarms.clear(); mAttachments.clear(); delete mRecurrence; mRecurrence = nullptr; } void init(Incidence *dest, const Incidence &src) { mRevision = src.d->mRevision; mCreated = src.d->mCreated; mDescription = src.d->mDescription; mSummary = src.d->mSummary; mCategories = src.d->mCategories; mRelatedToUid = src.d->mRelatedToUid; mResources = src.d->mResources; mStatusString = src.d->mStatusString; mStatus = src.d->mStatus; mSecrecy = src.d->mSecrecy; mPriority = src.d->mPriority; mLocation = src.d->mLocation; mGeoLatitude = src.d->mGeoLatitude; mGeoLongitude = src.d->mGeoLongitude; mHasGeo = src.d->mHasGeo; mRecurrenceId = src.d->mRecurrenceId; mThisAndFuture = src.d->mThisAndFuture; mLocalOnly = src.d->mLocalOnly; // Alarms and Attachments are stored in ListBase<...>, which is a QValueList<...*>. // We need to really duplicate the objects stored therein, otherwise deleting // i will also delete all attachments from this object (setAutoDelete...) mAlarms.reserve(src.d->mAlarms.count()); for (const Alarm::Ptr &alarm : qAsConst(src.d->mAlarms)) { Alarm::Ptr b(new Alarm(*alarm.data())); b->setParent(dest); mAlarms.append(b); } mAttachments.reserve(src.d->mAttachments.count()); for (const Attachment::Ptr &attachment : qAsConst(src.d->mAttachments)) { Attachment::Ptr a(new Attachment(*attachment)); mAttachments.append(a); } if (src.d->mRecurrence) { mRecurrence = new Recurrence(*(src.d->mRecurrence)); mRecurrence->addObserver(dest); } else { mRecurrence = nullptr; } } QDateTime mCreated; // creation datetime QString mDescription; // description string QString mSummary; // summary string QString mLocation; // location string QStringList mCategories; // category list Attachment::List mAttachments; // attachments list Alarm::List mAlarms; // alarms list QStringList mResources; // resources list (not calendar resources) QString mStatusString; // status string, for custom status QString mSchedulingID; // ID for scheduling mails QMap mRelatedToUid; // incidence uid this is related to, for each relType QHash mTempFiles; // Temporary files for writing attachments to. QDateTime mRecurrenceId; // recurrenceId float mGeoLatitude; // Specifies latitude in decimal degrees float mGeoLongitude; // Specifies longitude in decimal degrees mutable Recurrence *mRecurrence; // recurrence int mRevision; // revision number int mPriority; // priority: 1 = highest, 2 = less, etc. Status mStatus; // status Secrecy mSecrecy; // secrecy bool mDescriptionIsRich = false; // description string is richtext. bool mSummaryIsRich = false; // summary string is richtext. bool mLocationIsRich = false; // location string is richtext. bool mHasGeo = false; // if incidence has geo data bool mThisAndFuture = false; bool mLocalOnly = false; // allow changes that won't go to the server }; //@endcond Incidence::Incidence() : IncidenceBase() , d(new KCalCore::Incidence::Private) { recreate(); resetDirtyFields(); } Incidence::Incidence(const Incidence &i) : IncidenceBase(i) , Recurrence::RecurrenceObserver() , d(new KCalCore::Incidence::Private(*i.d)) { d->init(this, i); resetDirtyFields(); } Incidence::~Incidence() { // Alarm has a raw incidence pointer, so we must set it to 0 // so Alarm doesn't use it after Incidence is destroyed for (const Alarm::Ptr &alarm : qAsConst(d->mAlarms)) { alarm->setParent(nullptr); } clearTempFiles(); delete d->mRecurrence; delete d; } //@cond PRIVATE // A string comparison that considers that null and empty are the same static bool stringCompare(const QString &s1, const QString &s2) { return (s1.isEmpty() && s2.isEmpty()) || (s1 == s2); } //@endcond IncidenceBase &Incidence::assign(const IncidenceBase &other) { if (&other != this) { d->clear(); //TODO: should relations be cleared out, as in destructor??? IncidenceBase::assign(other); const Incidence *i = static_cast(&other); d->init(this, *i); } return *this; } bool Incidence::equals(const IncidenceBase &incidence) const { if (!IncidenceBase::equals(incidence)) { return false; } // If they weren't the same type IncidenceBase::equals would had returned false already const Incidence *i2 = static_cast(&incidence); const auto alarmList = alarms(); const auto otherAlarmsList = i2->alarms(); if (alarmList.count() != otherAlarmsList.count()) { return false; } Alarm::List::ConstIterator a1 = alarmList.constBegin(); Alarm::List::ConstIterator a1end = alarmList.constEnd(); Alarm::List::ConstIterator a2 = otherAlarmsList.constBegin(); Alarm::List::ConstIterator a2end = otherAlarmsList.constEnd(); for (; a1 != a1end && a2 != a2end; ++a1, ++a2) { if (**a1 == **a2) { continue; } else { return false; } } const auto attachmentList = attachments(); const auto otherAttachmentList = i2->attachments(); if (attachmentList.count() != otherAttachmentList.count()) { return false; } Attachment::List::ConstIterator att1 = attachmentList.constBegin(); const Attachment::List::ConstIterator att1end = attachmentList.constEnd(); Attachment::List::ConstIterator att2 = otherAttachmentList.constBegin(); const Attachment::List::ConstIterator att2end = otherAttachmentList.constEnd(); for (; att1 != att1end && att2 != att2end; ++att1, ++att2) { if (**att1 == **att2) { continue; } else { return false; } } bool recurrenceEqual = (d->mRecurrence == nullptr && i2->d->mRecurrence == nullptr); if (!recurrenceEqual) { recurrence(); // create if doesn't exist i2->recurrence(); // create if doesn't exist - recurrenceEqual = d->mRecurrence != nullptr - && i2->d->mRecurrence != nullptr - && *d->mRecurrence == *i2->d->mRecurrence; + recurrenceEqual = d->mRecurrence != nullptr && + i2->d->mRecurrence != nullptr && + *d->mRecurrence == *i2->d->mRecurrence; } if (d->mHasGeo == i2->d->mHasGeo) { - if (d->mHasGeo && (!qFuzzyCompare(d->mGeoLatitude, i2->d->mGeoLatitude) || !qFuzzyCompare(d->mGeoLongitude, i2->d->mGeoLongitude))) { + if (d->mHasGeo && (!qFuzzyCompare(d->mGeoLatitude, i2->d->mGeoLatitude) || + !qFuzzyCompare(d->mGeoLongitude, i2->d->mGeoLongitude))) { return false; } } else { return false; } return recurrenceEqual && created() == i2->created() && stringCompare(description(), i2->description()) && stringCompare(summary(), i2->summary()) && categories() == i2->categories() && stringCompare(relatedTo(), i2->relatedTo()) && resources() == i2->resources() && d->mStatus == i2->d->mStatus - && (d->mStatus == StatusNone - || stringCompare(d->mStatusString, i2->d->mStatusString)) + && (d->mStatus == StatusNone || stringCompare(d->mStatusString, i2->d->mStatusString)) && secrecy() == i2->secrecy() && priority() == i2->priority() && stringCompare(location(), i2->location()) && stringCompare(schedulingID(), i2->schedulingID()) && recurrenceId() == i2->recurrenceId() && thisAndFuture() == i2->thisAndFuture(); } QString Incidence::instanceIdentifier() const { if (hasRecurrenceId()) { return uid() + recurrenceId().toString(Qt::ISODate); } return uid(); } void Incidence::recreate() { const QDateTime nowUTC = QDateTime::currentDateTimeUtc(); setCreated(nowUTC); setSchedulingID(QString(), CalFormat::createUniqueId()); setRevision(0); setLastModified(nowUTC); } void Incidence::setLastModified(const QDateTime &lm) { if (!d->mLocalOnly) { IncidenceBase::setLastModified(lm); } } void Incidence::setReadOnly(bool readOnly) { IncidenceBase::setReadOnly(readOnly); if (d->mRecurrence) { d->mRecurrence->setRecurReadOnly(readOnly); } } void Incidence::setLocalOnly(bool localOnly) { if (mReadOnly) { return; } d->mLocalOnly = localOnly; } bool Incidence::localOnly() const { return d->mLocalOnly; } void Incidence::setAllDay(bool allDay) { if (mReadOnly) { return; } if (d->mRecurrence) { d->mRecurrence->setAllDay(allDay); } IncidenceBase::setAllDay(allDay); } void Incidence::setCreated(const QDateTime &created) { if (mReadOnly || d->mLocalOnly) { return; } d->mCreated = created.toUTC(); const auto ct = d->mCreated.time(); // Remove milliseconds d->mCreated.setTime(QTime(ct.hour(), ct.minute(), ct.second())); setFieldDirty(FieldCreated); // FIXME: Shouldn't we call updated for the creation date, too? // updated(); } QDateTime Incidence::created() const { return d->mCreated; } void Incidence::setRevision(int rev) { if (mReadOnly || d->mLocalOnly) { return; } update(); d->mRevision = rev; setFieldDirty(FieldRevision); updated(); } int Incidence::revision() const { return d->mRevision; } void Incidence::setDtStart(const QDateTime &dt) { IncidenceBase::setDtStart(dt); if (d->mRecurrence && dirtyFields().contains(FieldDtStart)) { d->mRecurrence->setStartDateTime(dt, allDay()); } } void Incidence::shiftTimes(const QTimeZone &oldZone, const QTimeZone &newZone) { IncidenceBase::shiftTimes(oldZone, newZone); if (d->mRecurrence) { d->mRecurrence->shiftTimes(oldZone, newZone); } for (int i = 0, end = d->mAlarms.count(); i < end; ++i) { d->mAlarms[i]->shiftTimes(oldZone, newZone); } } void Incidence::setDescription(const QString &description, bool isRich) { if (mReadOnly) { return; } update(); d->mDescription = description; d->mDescriptionIsRich = isRich; setFieldDirty(FieldDescription); updated(); } void Incidence::setDescription(const QString &description) { setDescription(description, Qt::mightBeRichText(description)); } QString Incidence::description() const { return d->mDescription; } QString Incidence::richDescription() const { if (descriptionIsRich()) { return d->mDescription; } else { return d->mDescription.toHtmlEscaped().replace(QLatin1Char('\n'), QStringLiteral("
")); } } bool Incidence::descriptionIsRich() const { return d->mDescriptionIsRich; } void Incidence::setSummary(const QString &summary, bool isRich) { if (mReadOnly) { return; } if (d->mSummary != summary || d->mSummaryIsRich != isRich) { update(); d->mSummary = summary; d->mSummaryIsRich = isRich; setFieldDirty(FieldSummary); updated(); } } void Incidence::setSummary(const QString &summary) { setSummary(summary, Qt::mightBeRichText(summary)); } QString Incidence::summary() const { return d->mSummary; } QString Incidence::richSummary() const { if (summaryIsRich()) { return d->mSummary; } else { return d->mSummary.toHtmlEscaped().replace(QLatin1Char('\n'), QStringLiteral("
")); } } bool Incidence::summaryIsRich() const { return d->mSummaryIsRich; } void Incidence::setCategories(const QStringList &categories) { if (mReadOnly) { return; } update(); d->mCategories = categories; updated(); } void Incidence::setCategories(const QString &catStr) { if (mReadOnly) { return; } update(); setFieldDirty(FieldCategories); d->mCategories.clear(); if (catStr.isEmpty()) { updated(); return; } d->mCategories = catStr.split(QLatin1Char(',')); QStringList::Iterator it; for (it = d->mCategories.begin(); it != d->mCategories.end(); ++it) { *it = (*it).trimmed(); } updated(); } QStringList Incidence::categories() const { return d->mCategories; } QString Incidence::categoriesStr() const { return d->mCategories.join(QLatin1Char(',')); } void Incidence::setRelatedTo(const QString &relatedToUid, RelType relType) { // TODO: RFC says that an incidence can have more than one related-to field // even for the same relType. if (d->mRelatedToUid[relType] != relatedToUid) { update(); d->mRelatedToUid[relType] = relatedToUid; setFieldDirty(FieldRelatedTo); updated(); } } QString Incidence::relatedTo(RelType relType) const { return d->mRelatedToUid.value(relType); } // %%%%%%%%%%%% Recurrence-related methods %%%%%%%%%%%%%%%%%%%% Recurrence *Incidence::recurrence() const { if (!d->mRecurrence) { d->mRecurrence = new Recurrence(); d->mRecurrence->setStartDateTime(dateTime(RoleRecurrenceStart), allDay()); d->mRecurrence->setAllDay(allDay()); d->mRecurrence->setRecurReadOnly(mReadOnly); d->mRecurrence->addObserver(const_cast(this)); } return d->mRecurrence; } void Incidence::clearRecurrence() { delete d->mRecurrence; d->mRecurrence = nullptr; } ushort Incidence::recurrenceType() const { if (d->mRecurrence) { return d->mRecurrence->recurrenceType(); } else { return Recurrence::rNone; } } bool Incidence::recurs() const { if (d->mRecurrence) { return d->mRecurrence->recurs(); } else { return false; } } bool Incidence::recursOn(const QDate &date, const QTimeZone &timeZone) const { return d->mRecurrence && d->mRecurrence->recursOn(date, timeZone); } bool Incidence::recursAt(const QDateTime &qdt) const { return d->mRecurrence && d->mRecurrence->recursAt(qdt); } QList Incidence::startDateTimesForDate(const QDate &date, const QTimeZone &timeZone) const { QDateTime start = dtStart(); QDateTime end = dateTime(RoleEndRecurrenceBase); QList result; // TODO_Recurrence: Also work if only due date is given... if (!start.isValid() && !end.isValid()) { return result; } // if the incidence doesn't recur, QDateTime kdate(date, {}, timeZone); if (!recurs()) { if (!(start > kdate || end < kdate)) { result << start; } return result; } qint64 days = start.daysTo(end); // Account for possible recurrences going over midnight, while the original event doesn't QDate tmpday(date.addDays(-days - 1)); QDateTime tmp; while (tmpday <= date) { if (recurrence()->recursOn(tmpday, timeZone)) { const QList times = recurrence()->recurTimesOn(tmpday, timeZone); for (const QTime &time : times) { tmp = QDateTime(tmpday, time, start.timeZone()); if (endDateForStart(tmp) >= kdate) { result << tmp; } } } tmpday = tmpday.addDays(1); } return result; } QList Incidence::startDateTimesForDateTime(const QDateTime &datetime) const { QDateTime start = dtStart(); QDateTime end = dateTime(RoleEndRecurrenceBase); QList result; // TODO_Recurrence: Also work if only due date is given... if (!start.isValid() && !end.isValid()) { return result; } // if the incidence doesn't recur, if (!recurs()) { if (!(start > datetime || end < datetime)) { result << start; } return result; } qint64 days = start.daysTo(end); // Account for possible recurrences going over midnight, while the original event doesn't QDate tmpday(datetime.date().addDays(-days - 1)); QDateTime tmp; while (tmpday <= datetime.date()) { if (recurrence()->recursOn(tmpday, datetime.timeZone())) { // Get the times during the day (in start date's time zone) when recurrences happen const QList times = recurrence()->recurTimesOn(tmpday, start.timeZone()); for (const QTime &time : times) { tmp = QDateTime(tmpday, time, start.timeZone()); if (!(tmp > datetime || endDateForStart(tmp) < datetime)) { result << tmp; } } } tmpday = tmpday.addDays(1); } return result; } QDateTime Incidence::endDateForStart(const QDateTime &startDt) const { QDateTime start = dtStart(); QDateTime end = dateTime(RoleEndRecurrenceBase); if (!end.isValid()) { return start; } if (!start.isValid()) { return end; } return startDt.addSecs(start.secsTo(end)); } void Incidence::addAttachment(const Attachment::Ptr &attachment) { if (mReadOnly || !attachment) { return; } Q_ASSERT(!d->mAttachments.contains(attachment)); update(); d->mAttachments.append(attachment); setFieldDirty(FieldAttachment); updated(); } void Incidence::deleteAttachment(const Attachment::Ptr &attachment) { int index = d->mAttachments.indexOf(attachment); if (index > -1) { setFieldDirty(FieldAttachment); d->mAttachments.remove(index); } } void Incidence::deleteAttachments(const QString &mime) { Attachment::List result; Attachment::List::Iterator it = d->mAttachments.begin(); while (it != d->mAttachments.end()) { if ((*it)->mimeType() != mime) { result += *it; } ++it; } d->mAttachments = result; setFieldDirty(FieldAttachment); } Attachment::List Incidence::attachments() const { return d->mAttachments; } Attachment::List Incidence::attachments(const QString &mime) const { Attachment::List attachments; for (const Attachment::Ptr &attachment : qAsConst(d->mAttachments)) { if (attachment->mimeType() == mime) { attachments.append(attachment); } } return attachments; } void Incidence::clearAttachments() { setFieldDirty(FieldAttachment); d->mAttachments.clear(); } QString Incidence::writeAttachmentToTempFile(const Attachment::Ptr &attachment) const { const QString attachementPath = d->mTempFiles.value(attachment); if (!attachementPath.isEmpty()) { return attachementPath; } QTemporaryFile file; QMimeDatabase mimeDb; QStringList patterns = mimeDb.mimeTypeForName(attachment->mimeType()).globPatterns(); if (!patterns.empty()) { file.setFileTemplate(file.fileTemplate() + QString(patterns.first()).remove(QLatin1Char('*'))); } file.setAutoRemove(false); file.open(); // read-only not to give the idea that it could be written to file.setPermissions(QFile::ReadUser); file.write(QByteArray::fromBase64(attachment->data())); d->mTempFiles.insert(attachment, file.fileName()); file.close(); return d->mTempFiles.value(attachment); } void Incidence::clearTempFiles() { QHash::const_iterator it = d->mTempFiles.constBegin(); const QHash::const_iterator end = d->mTempFiles.constEnd(); for (; it != end; ++it) { QFile f(it.value()); // On Windows the file must be writeable before we can remove it f.setPermissions(QFile::WriteUser); f.remove(); } d->mTempFiles.clear(); } void Incidence::setResources(const QStringList &resources) { if (mReadOnly) { return; } update(); d->mResources = resources; setFieldDirty(FieldResources); updated(); } QStringList Incidence::resources() const { return d->mResources; } void Incidence::setPriority(int priority) { if (mReadOnly) { return; } update(); d->mPriority = priority; setFieldDirty(FieldPriority); updated(); } int Incidence::priority() const { return d->mPriority; } void Incidence::setStatus(Incidence::Status status) { if (mReadOnly || status == StatusX) { return; } update(); d->mStatus = status; d->mStatusString.clear(); setFieldDirty(FieldStatus); updated(); } void Incidence::setCustomStatus(const QString &status) { if (mReadOnly) { return; } update(); d->mStatus = status.isEmpty() ? StatusNone : StatusX; d->mStatusString = status; setFieldDirty(FieldStatus); updated(); } Incidence::Status Incidence::status() const { return d->mStatus; } QString Incidence::customStatus() const { if (d->mStatus == StatusX) { return d->mStatusString; } else { return QString(); } } void Incidence::setSecrecy(Incidence::Secrecy secrecy) { if (mReadOnly) { return; } update(); d->mSecrecy = secrecy; setFieldDirty(FieldSecrecy); updated(); } Incidence::Secrecy Incidence::secrecy() const { return d->mSecrecy; } Alarm::List Incidence::alarms() const { return d->mAlarms; } Alarm::Ptr Incidence::newAlarm() { Alarm::Ptr alarm(new Alarm(this)); d->mAlarms.append(alarm); return alarm; } void Incidence::addAlarm(const Alarm::Ptr &alarm) { update(); d->mAlarms.append(alarm); setFieldDirty(FieldAlarms); updated(); } void Incidence::removeAlarm(const Alarm::Ptr &alarm) { const int index = d->mAlarms.indexOf(alarm); if (index > -1) { update(); d->mAlarms.remove(index); setFieldDirty(FieldAlarms); updated(); } } void Incidence::clearAlarms() { update(); d->mAlarms.clear(); setFieldDirty(FieldAlarms); updated(); } bool Incidence::hasEnabledAlarms() const { for (const Alarm::Ptr &alarm : qAsConst(d->mAlarms)) { if (alarm->enabled()) { return true; } } return false; } void Incidence::setLocation(const QString &location, bool isRich) { if (mReadOnly) { return; } if (d->mLocation != location || d->mLocationIsRich != isRich) { update(); d->mLocation = location; d->mLocationIsRich = isRich; setFieldDirty(FieldLocation); updated(); } } void Incidence::setLocation(const QString &location) { setLocation(location, Qt::mightBeRichText(location)); } QString Incidence::location() const { return d->mLocation; } QString Incidence::richLocation() const { if (locationIsRich()) { return d->mLocation; } else { return d->mLocation.toHtmlEscaped().replace(QLatin1Char('\n'), QStringLiteral("
")); } } bool Incidence::locationIsRich() const { return d->mLocationIsRich; } void Incidence::setSchedulingID(const QString &sid, const QString &uid) { if (!uid.isEmpty()) { setUid(uid); } if (sid != d->mSchedulingID) { d->mSchedulingID = sid; setFieldDirty(FieldSchedulingId); } } QString Incidence::schedulingID() const { if (d->mSchedulingID.isNull()) { // Nothing set, so use the normal uid return uid(); } return d->mSchedulingID; } bool Incidence::hasGeo() const { return d->mHasGeo; } void Incidence::setHasGeo(bool hasGeo) { if (mReadOnly) { return; } if (hasGeo == d->mHasGeo) { return; } update(); d->mHasGeo = hasGeo; setFieldDirty(FieldGeoLatitude); setFieldDirty(FieldGeoLongitude); updated(); } float Incidence::geoLatitude() const { return d->mGeoLatitude; } void Incidence::setGeoLatitude(float geolatitude) { if (mReadOnly) { return; } update(); d->mGeoLatitude = geolatitude; setFieldDirty(FieldGeoLatitude); updated(); } float Incidence::geoLongitude() const { return d->mGeoLongitude; } void Incidence::setGeoLongitude(float geolongitude) { if (!mReadOnly) { update(); d->mGeoLongitude = geolongitude; setFieldDirty(FieldGeoLongitude); updated(); } } bool Incidence::hasRecurrenceId() const { return (allDay() && d->mRecurrenceId.date().isValid()) || d->mRecurrenceId.isValid(); } QDateTime Incidence::recurrenceId() const { return d->mRecurrenceId; } void Incidence::setThisAndFuture(bool thisAndFuture) { d->mThisAndFuture = thisAndFuture; } bool Incidence::thisAndFuture() const { return d->mThisAndFuture; } void Incidence::setRecurrenceId(const QDateTime &recurrenceId) { if (!mReadOnly) { update(); d->mRecurrenceId = recurrenceId; setFieldDirty(FieldRecurrenceId); updated(); } } /** Observer interface for the recurrence class. If the recurrence is changed, this method will be called for the incidence the recurrence object belongs to. */ void Incidence::recurrenceUpdated(Recurrence *recurrence) { if (recurrence == d->mRecurrence) { update(); setFieldDirty(FieldRecurrence); updated(); } } //@cond PRIVATE #define ALT_DESC_FIELD "X-ALT-DESC" #define ALT_DESC_PARAMETERS QStringLiteral("FMTTYPE=text/html") //@endcond bool Incidence::hasAltDescription() const { const QString value = nonKDECustomProperty(ALT_DESC_FIELD); const QString parameter = nonKDECustomPropertyParameters(ALT_DESC_FIELD); return parameter == ALT_DESC_PARAMETERS && !value.isEmpty(); } void Incidence::setAltDescription(const QString &altdescription) { if (altdescription.isEmpty()) { removeNonKDECustomProperty(ALT_DESC_FIELD); } else { setNonKDECustomProperty(ALT_DESC_FIELD, altdescription, ALT_DESC_PARAMETERS); } } QString Incidence::altDescription() const { if (!hasAltDescription()) { return QString(); } else { return nonKDECustomProperty(ALT_DESC_FIELD); } } /** static */ QStringList Incidence::mimeTypes() { return QStringList() << QStringLiteral("text/calendar") << KCalCore::Event::eventMimeType() << KCalCore::Todo::todoMimeType() << KCalCore::Journal::journalMimeType(); } void Incidence::serialize(QDataStream &out) { serializeQDateTimeAsKDateTime(out, d->mCreated); out << d->mRevision << d->mDescription << d->mDescriptionIsRich << d->mSummary << d->mSummaryIsRich << d->mLocation << d->mLocationIsRich << d->mCategories << d->mResources << d->mStatusString << d->mPriority << d->mSchedulingID << d->mGeoLatitude << d->mGeoLongitude << d->mHasGeo; serializeQDateTimeAsKDateTime(out, d->mRecurrenceId); out << d->mThisAndFuture << d->mLocalOnly << d->mStatus << d->mSecrecy << (d->mRecurrence ? true : false) << d->mAttachments.count() << d->mAlarms.count() << d->mRelatedToUid; if (d->mRecurrence) { out << d->mRecurrence; } for (const Attachment::Ptr &attachment : qAsConst(d->mAttachments)) { out << attachment; } for (const Alarm::Ptr &alarm : qAsConst(d->mAlarms)) { out << alarm; } } void Incidence::deserialize(QDataStream &in) { quint32 status, secrecy; bool hasRecurrence; int attachmentCount, alarmCount; QMap relatedToUid; deserializeKDateTimeAsQDateTime(in, d->mCreated); in >> d->mRevision >> d->mDescription >> d->mDescriptionIsRich >> d->mSummary >> d->mSummaryIsRich >> d->mLocation >> d->mLocationIsRich >> d->mCategories >> d->mResources >> d->mStatusString >> d->mPriority >> d->mSchedulingID >> d->mGeoLatitude >> d->mGeoLongitude >> d->mHasGeo; deserializeKDateTimeAsQDateTime(in, d->mRecurrenceId); in >> d->mThisAndFuture >> d->mLocalOnly >> status >> secrecy >> hasRecurrence >> attachmentCount >> alarmCount >> relatedToUid; if (hasRecurrence) { d->mRecurrence = new Recurrence(); d->mRecurrence->addObserver(const_cast(this)); in >> d->mRecurrence; } d->mAttachments.clear(); d->mAlarms.clear(); d->mAttachments.reserve(attachmentCount); for (int i = 0; i < attachmentCount; ++i) { Attachment::Ptr attachment = Attachment::Ptr(new Attachment(QString())); in >> attachment; d->mAttachments.append(attachment); } d->mAlarms.reserve(alarmCount); for (int i = 0; i < alarmCount; ++i) { Alarm::Ptr alarm = Alarm::Ptr(new Alarm(this)); in >> alarm; d->mAlarms.append(alarm); } d->mStatus = static_cast(status); d->mSecrecy = static_cast(secrecy); d->mRelatedToUid.clear(); auto it = relatedToUid.cbegin(), end = relatedToUid.cend(); for (; it != end; ++it) { d->mRelatedToUid.insert(static_cast(it.key()), it.value()); } } diff --git a/src/incidencebase.cpp b/src/incidencebase.cpp index a075a56e5..3f45cab28 100644 --- a/src/incidencebase.cpp +++ b/src/incidencebase.cpp @@ -1,758 +1,760 @@ /* This file is part of the kcalcore library. Copyright (c) 2001,2004 Cornelius Schumacher Copyright (C) 2003-2004 Reinhold Kainhofer Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. Contact: Alvaro Manera 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. */ /** @file This file is part of the API for handling calendar data and defines the IncidenceBase class. @brief An abstract base class that provides a common base for all calendar incidence classes. @author Cornelius Schumacher \ @author Reinhold Kainhofer \ */ #include "incidencebase.h" #include "calformat.h" #include "visitor.h" #include "utils.h" #include #include "kcalcore_debug.h" #include #include #define KCALCORE_MAGIC_NUMBER 0xCA1C012E #define KCALCORE_SERIALIZATION_VERSION 1 using namespace KCalCore; /** Private class that helps to provide binary compatibility between releases. @internal */ //@cond PRIVATE class Q_DECL_HIDDEN KCalCore::IncidenceBase::Private { public: Private() : mOrganizer(nullptr) , mUpdateGroupLevel(0) , mUpdatedPending(false) , mAllDay(false) , mHasDuration(false) { } Private(const Private &other) : mUpdateGroupLevel(0) , mUpdatedPending(false) , mAllDay(true) , mHasDuration(false) { init(other); } ~Private() { } void init(const Private &other); QDateTime mLastModified; // incidence last modified date QDateTime mDtStart; // incidence start time Person::Ptr mOrganizer; // incidence person (owner) QString mUid; // incidence unique id Duration mDuration; // incidence duration int mUpdateGroupLevel; // if non-zero, suppresses update() calls bool mUpdatedPending = false; // true if an update has occurred since startUpdates() bool mAllDay = false; // true if the incidence is all-day bool mHasDuration = false; // true if the incidence has a duration Attendee::List mAttendees; // list of incidence attendees QStringList mComments; // list of incidence comments QStringList mContacts; // list of incidence contacts QList mObservers; // list of incidence observers QSet mDirtyFields; // Fields that changed since last time the incidence was created // or since resetDirtyFlags() was called QUrl mUrl; // incidence url property }; void IncidenceBase::Private::init(const Private &other) { mLastModified = other.mLastModified; mDtStart = other.mDtStart; mOrganizer = other.mOrganizer; mUid = other.mUid; mDuration = other.mDuration; mAllDay = other.mAllDay; mHasDuration = other.mHasDuration; mComments = other.mComments; mContacts = other.mContacts; mAttendees.clear(); mAttendees.reserve(other.mAttendees.count()); - for (Attendee::List::ConstIterator it = other.mAttendees.constBegin(), end = other.mAttendees.constEnd(); it != end; ++it) { + for (Attendee::List::ConstIterator it = other.mAttendees.constBegin(), + end = other.mAttendees.constEnd(); + it != end; ++it) { mAttendees.append(Attendee::Ptr(new Attendee(*(*it)))); } mUrl = other.mUrl; } //@endcond IncidenceBase::IncidenceBase() : d(new KCalCore::IncidenceBase::Private) { mReadOnly = false; setUid(CalFormat::createUniqueId()); } IncidenceBase::IncidenceBase(const IncidenceBase &i) : CustomProperties(i) , d(new KCalCore::IncidenceBase::Private(*i.d)) { mReadOnly = i.mReadOnly; } IncidenceBase::~IncidenceBase() { delete d; } IncidenceBase &IncidenceBase::operator=(const IncidenceBase &other) { Q_ASSERT(type() == other.type()); startUpdates(); // assign is virtual, will call the derived class's IncidenceBase &ret = assign(other); endUpdates(); return ret; } IncidenceBase &IncidenceBase::assign(const IncidenceBase &other) { CustomProperties::operator=(other); d->init(*other.d); mReadOnly = other.mReadOnly; d->mDirtyFields.clear(); d->mDirtyFields.insert(FieldUnknown); return *this; } bool IncidenceBase::operator==(const IncidenceBase &i2) const { if (i2.type() != type()) { return false; } else { // equals is virtual, so here we're calling the derived class method return equals(i2); } } bool IncidenceBase::operator!=(const IncidenceBase &i2) const { return !operator==(i2); } bool IncidenceBase::equals(const IncidenceBase &i2) const { if (attendees().count() != i2.attendees().count()) { // qCDebug(KCALCORE_LOG) << "Attendee count is different"; return false; } Attendee::List al1 = attendees(); Attendee::List al2 = i2.attendees(); Attendee::List::ConstIterator a1 = al1.constBegin(); Attendee::List::ConstIterator a2 = al2.constBegin(); //TODO Does the order of attendees in the list really matter? //Please delete this comment if you know it's ok, kthx for (; a1 != al1.constEnd() && a2 != al2.constEnd(); ++a1, ++a2) { if (!(**a1 == **a2)) { // qCDebug(KCALCORE_LOG) << "Attendees are different"; return false; } } if (!CustomProperties::operator==(i2)) { // qCDebug(KCALCORE_LOG) << "Properties are different"; return false; } // Don't compare lastModified, otherwise the operator is not // of much use. We are not comparing for identity, after all. // no need to compare mObserver bool a = ((dtStart() == i2.dtStart()) || (!dtStart().isValid() && !i2.dtStart().isValid())); bool b = *(organizer().data()) == *(i2.organizer().data()); bool c = uid() == i2.uid(); bool d = allDay() == i2.allDay(); bool e = duration() == i2.duration(); bool f = hasDuration() == i2.hasDuration(); bool g = url() == i2.url(); //qCDebug(KCALCORE_LOG) << a << b << c << d << e << f << g; return a && b && c && d && e && f && g; } bool IncidenceBase::accept(Visitor &v, const IncidenceBase::Ptr &incidence) { Q_UNUSED(v); Q_UNUSED(incidence); return false; } void IncidenceBase::setUid(const QString &uid) { if (d->mUid != uid) { update(); d->mUid = uid; d->mDirtyFields.insert(FieldUid); updated(); } } QString IncidenceBase::uid() const { return d->mUid; } void IncidenceBase::setLastModified(const QDateTime &lm) { // DON'T! updated() because we call this from // Calendar::updateEvent(). d->mDirtyFields.insert(FieldLastModified); // Convert to UTC and remove milliseconds part. QDateTime current = lm.toUTC(); QTime t = current.time(); t.setHMS(t.hour(), t.minute(), t.second(), 0); current.setTime(t); d->mLastModified = current; } QDateTime IncidenceBase::lastModified() const { return d->mLastModified; } void IncidenceBase::setOrganizer(const Person::Ptr &organizer) { if (organizer) { update(); // we don't check for readonly here, because it is // possible that by setting the organizer we are changing // the event's readonly status... d->mOrganizer = organizer; d->mDirtyFields.insert(FieldOrganizer); updated(); } } void IncidenceBase::setOrganizer(const QString &o) { QString mail(o); if (mail.startsWith(QLatin1String("MAILTO:"), Qt::CaseInsensitive)) { mail = mail.remove(0, 7); } // split the string into full name plus email. const Person::Ptr organizer = Person::fromFullName(mail); setOrganizer(organizer); } Person::Ptr IncidenceBase::organizer() const { if (!d->mOrganizer) { d->mOrganizer = Person::Ptr(new Person()); // init at first use only to save memory } return d->mOrganizer; } void IncidenceBase::setReadOnly(bool readOnly) { mReadOnly = readOnly; } bool IncidenceBase::isReadOnly() const { return mReadOnly; } void IncidenceBase::setDtStart(const QDateTime &dtStart) { // if ( mReadOnly ) return; if (!dtStart.isValid() && type() != IncidenceBase::TypeTodo) { qCWarning(KCALCORE_LOG) << "Invalid dtStart"; } if (d->mDtStart != dtStart) { update(); d->mDtStart = dtStart; d->mDirtyFields.insert(FieldDtStart); updated(); } } QDateTime IncidenceBase::dtStart() const { return d->mDtStart; } bool IncidenceBase::allDay() const { return d->mAllDay; } void IncidenceBase::setAllDay(bool f) { if (mReadOnly || f == d->mAllDay) { return; } update(); d->mAllDay = f; if (d->mDtStart.isValid()) { d->mDirtyFields.insert(FieldDtStart); } updated(); } void IncidenceBase::shiftTimes(const QTimeZone &oldZone, const QTimeZone &newZone) { update(); d->mDtStart = d->mDtStart.toTimeZone(oldZone); d->mDtStart.setTimeZone(newZone); d->mDirtyFields.insert(FieldDtStart); d->mDirtyFields.insert(FieldDtEnd); updated(); } void IncidenceBase::addComment(const QString &comment) { d->mComments += comment; } bool IncidenceBase::removeComment(const QString &comment) { bool found = false; QStringList::Iterator i; for (i = d->mComments.begin(); !found && i != d->mComments.end(); ++i) { if ((*i) == comment) { found = true; d->mComments.erase(i); } } if (found) { d->mDirtyFields.insert(FieldComment); } return found; } void IncidenceBase::clearComments() { d->mDirtyFields.insert(FieldComment); d->mComments.clear(); } QStringList IncidenceBase::comments() const { return d->mComments; } void IncidenceBase::addContact(const QString &contact) { if (!contact.isEmpty()) { d->mContacts += contact; d->mDirtyFields.insert(FieldContact); } } bool IncidenceBase::removeContact(const QString &contact) { bool found = false; QStringList::Iterator i; for (i = d->mContacts.begin(); !found && i != d->mContacts.end(); ++i) { if ((*i) == contact) { found = true; d->mContacts.erase(i); } } if (found) { d->mDirtyFields.insert(FieldContact); } return found; } void IncidenceBase::clearContacts() { d->mDirtyFields.insert(FieldContact); d->mContacts.clear(); } QStringList IncidenceBase::contacts() const { return d->mContacts; } void IncidenceBase::addAttendee(const Attendee::Ptr &a, bool doupdate) { if (!a || mReadOnly) { return; } Q_ASSERT(!d->mAttendees.contains(a)); if (doupdate) { update(); } if (a->name().left(7).toUpper() == QLatin1String("MAILTO:")) { a->setName(a->name().remove(0, 7)); } /* If Uid is empty, just use the pointer to Attendee (encoded to * string) as Uid. Only thing that matters is that the Uid is unique * insofar IncidenceBase is concerned, and this does that (albeit * not very nicely). If these are ever saved to disk, should use * (considerably more expensive) CalFormat::createUniqueId(). As Uid * is not part of Attendee in iCal std, it's fairly safe bet that * these will never hit disc though so faster generation speed is * more important than actually being forever unique.*/ if (a->uid().isEmpty()) { a->setUid(QString::number((qlonglong)a.data())); } d->mAttendees.append(a); if (doupdate) { d->mDirtyFields.insert(FieldAttendees); updated(); } } void IncidenceBase::deleteAttendee(const Attendee::Ptr &a, bool doupdate) { if (!a || mReadOnly) { return; } int index = d->mAttendees.indexOf(a); if (index >= 0) { if (doupdate) { update(); } d->mAttendees.remove(index); if (doupdate) { d->mDirtyFields.insert(FieldAttendees); updated(); } } } Attendee::List IncidenceBase::attendees() const { return d->mAttendees; } int IncidenceBase::attendeeCount() const { return d->mAttendees.count(); } void IncidenceBase::clearAttendees() { if (mReadOnly) { return; } d->mDirtyFields.insert(FieldAttendees); d->mAttendees.clear(); } Attendee::Ptr IncidenceBase::attendeeByMail(const QString &email) const { Attendee::List::ConstIterator it; for (it = d->mAttendees.constBegin(); it != d->mAttendees.constEnd(); ++it) { if ((*it)->email() == email) { return *it; } } return Attendee::Ptr(); } Attendee::Ptr IncidenceBase::attendeeByMails(const QStringList &emails, const QString &email) const { QStringList mails = emails; if (!email.isEmpty()) { mails.append(email); } Attendee::List::ConstIterator itA; for (itA = d->mAttendees.constBegin(); itA != d->mAttendees.constEnd(); ++itA) { for (QStringList::const_iterator it = mails.constBegin(); it != mails.constEnd(); ++it) { if ((*itA)->email() == (*it)) { return *itA; } } } return Attendee::Ptr(); } Attendee::Ptr IncidenceBase::attendeeByUid(const QString &uid) const { Attendee::List::ConstIterator it; for (it = d->mAttendees.constBegin(); it != d->mAttendees.constEnd(); ++it) { if ((*it)->uid() == uid) { return *it; } } return Attendee::Ptr(); } void IncidenceBase::setDuration(const Duration &duration) { update(); d->mDuration = duration; setHasDuration(true); d->mDirtyFields.insert(FieldDuration); updated(); } Duration IncidenceBase::duration() const { return d->mDuration; } void IncidenceBase::setHasDuration(bool hasDuration) { d->mHasDuration = hasDuration; } bool IncidenceBase::hasDuration() const { return d->mHasDuration; } void IncidenceBase::setUrl(const QUrl &url) { d->mDirtyFields.insert(FieldUrl); d->mUrl = url; } QUrl IncidenceBase::url() const { return d->mUrl; } void IncidenceBase::registerObserver(IncidenceBase::IncidenceObserver *observer) { if (observer && !d->mObservers.contains(observer)) { d->mObservers.append(observer); } } void IncidenceBase::unRegisterObserver(IncidenceBase::IncidenceObserver *observer) { d->mObservers.removeAll(observer); } void IncidenceBase::update() { if (!d->mUpdateGroupLevel) { d->mUpdatedPending = true; const auto rid = recurrenceId(); for (IncidenceObserver *o : qAsConst(d->mObservers)) { o->incidenceUpdate(uid(), rid); } } } void IncidenceBase::updated() { if (d->mUpdateGroupLevel) { d->mUpdatedPending = true; } else { const auto rid = recurrenceId(); for (IncidenceObserver *o : qAsConst(d->mObservers)) { o->incidenceUpdated(uid(), rid); } } } void IncidenceBase::startUpdates() { update(); ++d->mUpdateGroupLevel; } void IncidenceBase::endUpdates() { if (d->mUpdateGroupLevel > 0) { if (--d->mUpdateGroupLevel == 0 && d->mUpdatedPending) { d->mUpdatedPending = false; updated(); } } } void IncidenceBase::customPropertyUpdate() { update(); } void IncidenceBase::customPropertyUpdated() { updated(); } QDateTime IncidenceBase::recurrenceId() const { return QDateTime(); } void IncidenceBase::resetDirtyFields() { d->mDirtyFields.clear(); } QSet IncidenceBase::dirtyFields() const { return d->mDirtyFields; } void IncidenceBase::setFieldDirty(IncidenceBase::Field field) { d->mDirtyFields.insert(field); } QUrl IncidenceBase::uri() const { return QUrl(QStringLiteral("urn:x-ical:") + uid()); } void IncidenceBase::setDirtyFields(const QSet &dirtyFields) { d->mDirtyFields = dirtyFields; } /** static */ quint32 IncidenceBase::magicSerializationIdentifier() { return KCALCORE_MAGIC_NUMBER; } QDataStream &KCalCore::operator<<(QDataStream &out, const KCalCore::IncidenceBase::Ptr &i) { if (!i) { return out; } out << static_cast(KCALCORE_MAGIC_NUMBER); // Magic number to identify KCalCore data out << static_cast(KCALCORE_SERIALIZATION_VERSION); out << static_cast(i->type()); out << *(static_cast(i.data())); serializeQDateTimeAsKDateTime(out, i->d->mLastModified); serializeQDateTimeAsKDateTime(out, i->d->mDtStart); out << i->organizer() << i->d->mUid << i->d->mDuration << i->d->mAllDay << i->d->mHasDuration << i->d->mComments << i->d->mContacts << i->d->mAttendees.count() << i->d->mUrl; for (const Attendee::Ptr &attendee : qAsConst(i->d->mAttendees)) { out << attendee; } // Serialize the sub-class data. In KDE5 we can add new virtuals. i->virtual_hook(KCalCore::IncidenceBase::SerializerHook, &out); return out; } QDataStream &KCalCore::operator>>(QDataStream &in, KCalCore::IncidenceBase::Ptr &i) { if (!i) { return in; } qint32 attendeeCount, type; quint32 magic, version; in >> magic; if (magic != KCALCORE_MAGIC_NUMBER) { qCWarning(KCALCORE_LOG) << "Invalid magic on serialized data"; return in; } in >> version; if (version > KCALCORE_MAGIC_NUMBER) { qCWarning(KCALCORE_LOG) << "Invalid version on serialized data"; return in; } in >> type; in >> *(static_cast(i.data())); deserializeKDateTimeAsQDateTime(in, i->d->mLastModified); deserializeKDateTimeAsQDateTime(in, i->d->mDtStart); in >> i->d->mOrganizer >> i->d->mUid >> i->d->mDuration >> i->d->mAllDay >> i->d->mHasDuration >> i->d->mComments >> i->d->mContacts >> attendeeCount >> i->d->mUrl; i->d->mAttendees.clear(); i->d->mAttendees.reserve(attendeeCount); for (int it = 0; it < attendeeCount; it++) { Attendee::Ptr attendee = Attendee::Ptr(new Attendee(QString(), QString())); in >> attendee; i->d->mAttendees.append(attendee); } // Deserialize the sub-class data. In KDE5 we can add new virtuals. i->virtual_hook(KCalCore::IncidenceBase::DeserializerHook, &in); return in; } IncidenceBase::IncidenceObserver::~IncidenceObserver() { } diff --git a/src/memorycalendar.h b/src/memorycalendar.h index 98a1f2e8e..1d3ba7304 100644 --- a/src/memorycalendar.h +++ b/src/memorycalendar.h @@ -1,334 +1,333 @@ /* This file is part of the kcalcore library. Copyright (c) 1998 Preston Brown Copyright (c) 2001,2003 Cornelius Schumacher 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. */ /** @file This file is part of the API for handling calendar data and defines the MemoryCalendar class. Very simple implementation of a Calendar that is only in memory @author Preston Brown \ @author Cornelius Schumacher \ */ #ifndef KCALCORE_MEMORYCALENDAR_H #define KCALCORE_MEMORYCALENDAR_H #include "kcalcore_export.h" #include "calendar.h" namespace KCalCore { - - /** @brief This class provides a calendar stored in memory. */ class KCALCORE_EXPORT MemoryCalendar : public Calendar { Q_OBJECT public: /** A shared pointer to a MemoryCalendar */ typedef QSharedPointer Ptr; /** @copydoc Calendar::Calendar(const QTimeZone &) */ explicit MemoryCalendar(const QTimeZone &timeZone); /** @copydoc Calendar::Calendar(const QString &) */ explicit MemoryCalendar(const QByteArray &timeZoneId); /** @copydoc Calendar::~Calendar() */ ~MemoryCalendar() override; /** Clears out the current calendar, freeing all used memory etc. etc. */ void close() override; /** @copydoc Calendar::deleteIncidence() */ bool deleteIncidence(const Incidence::Ptr &incidence) override; /** @copydoc Calendar::deleteIncidenceInstances */ bool deleteIncidenceInstances(const Incidence::Ptr &incidence) override; /** @copydoc Calendar::addIncidence() */ bool addIncidence(const Incidence::Ptr &incidence) override; // Event Specific Methods // /** @copydoc Calendar::addEvent() */ bool addEvent(const Event::Ptr &event) override; /** @copydoc Calendar::deleteEvent() */ bool deleteEvent(const Event::Ptr &event) override; /** @copydoc Calendar::deleteEventInstances() */ bool deleteEventInstances(const Event::Ptr &event) override; /** @copydoc Calendar::rawEvents(EventSortField, SortDirection)const */ Q_REQUIRED_RESULT Event::List rawEvents( EventSortField sortField = EventSortUnsorted, SortDirection sortDirection = SortDirectionAscending) const override; /** @copydoc Calendar::rawEvents(const QDate &, const QDate &, const QTimeZone &, bool)const */ Q_REQUIRED_RESULT Event::List rawEvents(const QDate &start, const QDate &end, const QTimeZone &timeZone = {}, bool inclusive = false) const override; /** Returns an unfiltered list of all Events which occur on the given date. @param date request unfiltered Event list for this QDate only. @param timeZone time zone to interpret @p date, or the calendar's default time zone if none is specified @param sortField specifies the EventSortField. @param sortDirection specifies the SortDirection. @return the list of unfiltered Events occurring on the specified QDate. */ Q_REQUIRED_RESULT Event::List rawEventsForDate( const QDate &date, const QTimeZone &timeZone = {}, EventSortField sortField = EventSortUnsorted, SortDirection sortDirection = SortDirectionAscending) const override; /** @copydoc Calendar::rawEventsForDate(const QDateTime &)const */ Q_REQUIRED_RESULT Event::List rawEventsForDate(const QDateTime &dt) const override; /** * Returns an incidence by identifier. * @see Incidence::instanceIdentifier() * @since 4.11 */ Incidence::Ptr instance(const QString &identifier) const; /** @copydoc Calendar::event() */ Q_REQUIRED_RESULT Event::Ptr event(const QString &uid, const QDateTime &recurrenceId = {}) const override; /** @copydoc Calendar::deletedEvent() */ Q_REQUIRED_RESULT Event::Ptr deletedEvent(const QString &uid, const QDateTime &recurrenceId = {}) const override; /** @copydoc Calendar::deletedEvents(EventSortField, SortDirection)const */ Q_REQUIRED_RESULT Event::List deletedEvents( EventSortField sortField = EventSortUnsorted, SortDirection sortDirection = SortDirectionAscending) const override; /** @copydoc Calendar::eventInstances(const Incidence::Ptr &, EventSortField, SortDirection)const */ Q_REQUIRED_RESULT Event::List eventInstances( const Incidence::Ptr &event, EventSortField sortField = EventSortUnsorted, SortDirection sortDirection = SortDirectionAscending) const override; // To-do Specific Methods // /** @copydoc Calendar::addTodo() */ bool addTodo(const Todo::Ptr &todo) override; /** @copydoc Calendar::deleteTodo() */ bool deleteTodo(const Todo::Ptr &todo) override; /** @copydoc Calendar::deleteTodoInstances() */ bool deleteTodoInstances(const Todo::Ptr &todo) override; /** @copydoc Calendar::rawTodos(TodoSortField, SortDirection)const */ Q_REQUIRED_RESULT Todo::List rawTodos( TodoSortField sortField = TodoSortUnsorted, SortDirection sortDirection = SortDirectionAscending) const override; /** @copydoc Calendar::rawTodos(const QDate &, const QDate &, const QTimeZone &, bool)const */ Q_REQUIRED_RESULT Todo::List rawTodos( const QDate &start, const QDate &end, const QTimeZone &timeZone = {}, bool inclusive = false) const override; /** @copydoc Calendar::rawTodosForDate() */ Q_REQUIRED_RESULT Todo::List rawTodosForDate(const QDate &date) const override; /** @copydoc Calendar::todo() */ Q_REQUIRED_RESULT Todo::Ptr todo(const QString &uid, const QDateTime &recurrenceId = {}) const override; /** @copydoc Calendar::deletedTodo() */ Q_REQUIRED_RESULT Todo::Ptr deletedTodo(const QString &uid, const QDateTime &recurrenceId = {}) const override; /** @copydoc Calendar::deletedTodos(TodoSortField, SortDirection)const */ Q_REQUIRED_RESULT Todo::List deletedTodos( TodoSortField sortField = TodoSortUnsorted, SortDirection sortDirection = SortDirectionAscending) const override; /** @copydoc Calendar::todoInstances(const Incidence::Ptr &, TodoSortField, SortDirection)const */ Q_REQUIRED_RESULT Todo::List todoInstances(const Incidence::Ptr &todo, TodoSortField sortField = TodoSortUnsorted, SortDirection sortDirection = SortDirectionAscending) const override; // Journal Specific Methods // /** @copydoc Calendar::addJournal() */ bool addJournal(const Journal::Ptr &journal) override; /** @copydoc Calendar::deleteJournal() */ bool deleteJournal(const Journal::Ptr &journal) override; /** @copydoc Calendar::deleteJournalInstances() */ bool deleteJournalInstances(const Journal::Ptr &journal) override; /** @copydoc Calendar::rawJournals() */ Q_REQUIRED_RESULT Journal::List rawJournals( JournalSortField sortField = JournalSortUnsorted, SortDirection sortDirection = SortDirectionAscending) const override; /** @copydoc Calendar::rawJournalsForDate() */ Q_REQUIRED_RESULT Journal::List rawJournalsForDate(const QDate &date) const override; /** @copydoc Calendar::journal() */ Journal::Ptr journal(const QString &uid, const QDateTime &recurrenceId = {}) const override; /** @copydoc Calendar::deletedJournal() */ Journal::Ptr deletedJournal(const QString &uid, const QDateTime &recurrenceId = {}) const override; /** @copydoc Calendar::deletedJournals(JournalSortField, SortDirection)const */ Q_REQUIRED_RESULT Journal::List deletedJournals( JournalSortField sortField = JournalSortUnsorted, SortDirection sortDirection = SortDirectionAscending) const override; /** @copydoc Calendar::journalInstances(const Incidence::Ptr &, JournalSortField, SortDirection)const */ Q_REQUIRED_RESULT Journal::List journalInstances(const Incidence::Ptr &journal, JournalSortField sortField = JournalSortUnsorted, SortDirection sortDirection = SortDirectionAscending) const override; // Alarm Specific Methods // /** @copydoc Calendar::alarms() */ - Q_REQUIRED_RESULT Alarm::List alarms(const QDateTime &from, const QDateTime &to, bool excludeBlockedAlarms = false) const override; + Q_REQUIRED_RESULT Alarm::List alarms(const QDateTime &from, const QDateTime &to, + bool excludeBlockedAlarms = false) const override; /** Return a list of Alarms that occur before the specified timestamp. @param to is the ending timestamp. @return the list of Alarms occurring before the specified QDateTime. */ Q_REQUIRED_RESULT Alarm::List alarmsTo(const QDateTime &to) const; /** @copydoc Calendar::incidenceUpdate(const QString &,const QDateTime &) */ void incidenceUpdate(const QString &uid, const QDateTime &recurrenceId) override; /** @copydoc Calendar::incidenceUpdated(const QString &,const QDateTime &) */ void incidenceUpdated(const QString &uid, const QDateTime &recurrenceId) override; using QObject::event; // prevent warning about hidden virtual method protected: /** @copydoc IncidenceBase::virtual_hook() */ void virtual_hook(int id, void *data) override; private: //@cond PRIVATE class Private; Private *const d; //@endcond Q_DISABLE_COPY(MemoryCalendar) }; } #endif diff --git a/src/occurrenceiterator.cpp b/src/occurrenceiterator.cpp index 4e66db1e3..21ea852c1 100644 --- a/src/occurrenceiterator.cpp +++ b/src/occurrenceiterator.cpp @@ -1,254 +1,255 @@ /* This file is part of the kcalcore library. Copyright (C) 2013 Christian Mollekopf 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. */ /** @file This file is part of the API for handling calendar data and defines the OccurrenceIterator class. @brief This class provides an iterator to iterate over all occurrences of incidences. @author Christian Mollekopf \ */ #include "occurrenceiterator.h" #include "calendar.h" #include "calfilter.h" #include "utils.h" #include using namespace KCalCore; /** Private class that helps to provide binary compatibility between releases. @internal */ //@cond PRIVATE class Q_DECL_HIDDEN KCalCore::OccurrenceIterator::Private { public: Private(OccurrenceIterator *qq) : q(qq), occurrenceIt(occurrenceList) { } OccurrenceIterator *q; QDateTime start; QDateTime end; struct Occurrence { Occurrence() { } Occurrence(const Incidence::Ptr &i, const QDateTime &recurrenceId, const QDateTime &startDate) : incidence(i), recurrenceId(recurrenceId), startDate(startDate) { } Incidence::Ptr incidence; QDateTime recurrenceId; QDateTime startDate; }; QList occurrenceList; QListIterator occurrenceIt; Occurrence current; /* * KCalCore::CalFilter can't handle individual occurrences. * When filtering completed to-dos, the CalFilter doesn't hide * them if it's a recurring to-do. */ bool occurrenceIsHidden(const Calendar &calendar, const Incidence::Ptr &inc, const QDateTime &occurrenceDate) { if ((inc->type() == Incidence::TypeTodo) && calendar.filter() && (calendar.filter()->criteria() & KCalCore::CalFilter::HideCompletedTodos)) { if (inc->recurs()) { const Todo::Ptr todo = inc.staticCast(); if (todo && (occurrenceDate < todo->dtDue())) { return true; } } else if (inc->hasRecurrenceId()) { const Todo::Ptr mainTodo = calendar.todo(inc->uid()); if (mainTodo && mainTodo->isCompleted()) { return true; } } } return false; } void setupIterator(const Calendar &calendar, const Incidence::List &incidences) { for (const Incidence::Ptr &inc : qAsConst(incidences)) { if (inc->hasRecurrenceId()) { continue; } if (inc->recurs()) { QHash recurrenceIds; QDateTime incidenceRecStart = inc->dateTime(Incidence::RoleRecurrenceStart); //const bool isAllDay = inc->allDay(); const auto lstInstances = calendar.instances(inc); for (const Incidence::Ptr &exception : lstInstances) { if (incidenceRecStart.isValid()) { - recurrenceIds.insert(exception->recurrenceId().toTimeZone(incidenceRecStart.timeZone()), exception); + recurrenceIds.insert( + exception->recurrenceId().toTimeZone(incidenceRecStart.timeZone()), + exception); } } const auto occurrences = inc->recurrence()->timesInInterval(start, end); Incidence::Ptr incidence(inc), lastInc(inc); qint64 offset(0), lastOffset(0); QDateTime occurrenceStartDate; - for(auto recurrenceId : qAsConst(occurrences)) { + for (auto recurrenceId : qAsConst(occurrences)) { occurrenceStartDate = recurrenceId; bool resetIncidence = false; if (recurrenceIds.contains(recurrenceId)) { // TODO: exclude exceptions where the start/end is not within // (so the occurrence of the recurrence is omitted, but no exception is added) if (recurrenceIds.value(recurrenceId)->status() == Incidence::StatusCanceled) { continue; } incidence = recurrenceIds.value(recurrenceId); occurrenceStartDate = incidence->dtStart(); resetIncidence = !incidence->thisAndFuture(); offset = incidence->recurrenceId().secsTo(incidence->dtStart()); if (incidence->thisAndFuture()) { lastInc = incidence; lastOffset = offset; } } else if (inc != incidence) { //thisAndFuture exception is active occurrenceStartDate = occurrenceStartDate.addSecs(offset); } if (!occurrenceIsHidden(calendar, incidence, occurrenceStartDate)) { occurrenceList << Private::Occurrence(incidence, recurrenceId, occurrenceStartDate); } if (resetIncidence) { incidence = lastInc; offset = lastOffset; } } } else { occurrenceList << Private::Occurrence(inc, {}, inc->dtStart()); } } occurrenceIt = QListIterator(occurrenceList); } }; //@endcond - /** * Right now there is little point in the iterator, but: * With an iterator it should be possible to solve this more memory efficiently * and with immediate results at the beginning of the selected timeframe. * Either all events are iterated simoulatneously, resulting in occurrences * of all events in parallel in the correct time-order, or incidence after * incidence, which would be even more efficient. * * By making this class a friend of calendar, we could also use the internally * available data structures. */ OccurrenceIterator::OccurrenceIterator(const Calendar &calendar, const QDateTime &start, const QDateTime &end) : d(new KCalCore::OccurrenceIterator::Private(this)) { d->start = start; d->end = end; Event::List events = calendar.rawEvents(start.date(), end.date(), start.timeZone()); if (calendar.filter()) { calendar.filter()->apply(&events); } Todo::List todos = calendar.rawTodos(start.date(), end.date(), start.timeZone()); if (calendar.filter()) { calendar.filter()->apply(&todos); } Journal::List journals; const Journal::List allJournals = calendar.rawJournals(); for (const KCalCore::Journal::Ptr &journal : allJournals) { const QDate journalStart = journal->dtStart().toTimeZone(start.timeZone()).date(); if (journal->dtStart().isValid() && journalStart >= start.date() && journalStart <= end.date()) { journals << journal; } } if (calendar.filter()) { calendar.filter()->apply(&journals); } const Incidence::List incidences = KCalCore::Calendar::mergeIncidenceList(events, todos, journals); d->setupIterator(calendar, incidences); } OccurrenceIterator::OccurrenceIterator(const Calendar &calendar, const Incidence::Ptr &incidence, const QDateTime &start, const QDateTime &end) : d(new KCalCore::OccurrenceIterator::Private(this)) { Q_ASSERT(incidence); d->start = start; d->end = end; d->setupIterator(calendar, Incidence::List() << incidence); } OccurrenceIterator::~OccurrenceIterator() { } bool OccurrenceIterator::hasNext() const { return d->occurrenceIt.hasNext(); } void OccurrenceIterator::next() { d->current = d->occurrenceIt.next(); } Incidence::Ptr OccurrenceIterator::incidence() const { return d->current.incidence; } QDateTime OccurrenceIterator::occurrenceStartDate() const { return d->current.startDate; } QDateTime OccurrenceIterator::recurrenceId() const { return d->current.recurrenceId; } diff --git a/src/recurrence.cpp b/src/recurrence.cpp index 4a6abc3d9..b0eb41f7c 100644 --- a/src/recurrence.cpp +++ b/src/recurrence.cpp @@ -1,1554 +1,1555 @@ /* 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.h" #include "recurrencehelper_p.h" #include "kcalcore_debug.h" #include #include #include #include using namespace KCalCore; //@cond PRIVATE class Q_DECL_HIDDEN KCalCore::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 KCalCore::Recurrence::Private()) { } Recurrence::Recurrence(const Recurrence &r) : RecurrenceRule::RuleObserver(), d(new KCalCore::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; } Recurrence &Recurrence::operator=(const Recurrence &recurrence) { // check for self assignment if (&recurrence == this) { return *this; } *d = *recurrence.d; return *this; } 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)) { + 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) { 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 : d->mRDates) { kdt.setDate(date); if (kdt > nextDT) { dates << kdt; break; } } // Add the next occurrences from all RRULEs. for (auto rule : 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 (auto rule : 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 : d->mRDates) { kdt.setDate(date); if (kdt < prevDT) { dates << kdt; break; } } // Add the previous occurrences from all RRULEs. for (auto rule : 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 (auto rule : 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() { } KCALCORE_EXPORT QDataStream &KCalCore::operator<<(QDataStream &out, KCalCore::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; } KCALCORE_EXPORT QDataStream &KCalCore::operator>>(QDataStream &in, KCalCore::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/sorting.cpp b/src/sorting.cpp index a3e7a1ac3..e04faabc9 100644 --- a/src/sorting.cpp +++ b/src/sorting.cpp @@ -1,434 +1,439 @@ /* This file is part of the kcalcore library. Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. Contact: Alvaro Manera 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 "sorting.h" #include "event.h" #include "journal.h" #include "todo.h" #include "utils.h" // PENDING(kdab) Review // The QString::compare() need to be replace by a DUI string comparisons. // See http://qt.gitorious.org/maemo-6-ui-framework/libdui // If not compiled in "meego-mode" should we be using locale compares? using namespace KCalCore; /** * How one QDateTime compares with another. * * If any all-day events are involved, comparison of QDateTime values * requires them to be considered as representing time periods. An all-day * instance represents a time period from 00:00:00 to 23:59:59.999 on a given * date, while a date/time instance can be considered to represent a time * period whose start and end times are the same. They may therefore be * earlier or later, or may overlap or be contained one within the other. * * Values may be OR'ed with each other in any combination of 'consecutive' * intervals to represent different types of relationship. * * In the descriptions of the values below, * - s1 = start time of first instance * - e1 = end time of first instance * - s2 = start time of second instance * - e2 = end time of second instance. */ enum DateTimeComparison { Before = 0x01, /**< The first QDateTime is strictly earlier than the second, * i.e. e1 < s2. */ AtStart = 0x02, /**< The first QDateTime starts at the same time as the second, * and ends before the end of the second, * i.e. s1 = s2, e1 < e2. */ Inside = 0x04, /**< The first QDateTime starts after the start of the second, * and ends before the end of the second, * i.e. s1 > s2, e1 < e2. */ AtEnd = 0x08, /**< The first QDateTime starts after the start of the second, * and ends at the same time as the second, * i.e. s1 > s2, e1 = e2. */ After = 0x10, /**< The first QDateTime is strictly later than the second, * i.e. s1 > e2. */ Equal = AtStart | Inside | AtEnd, /**< Simultaneous, i.e. s1 = s2 && e1 = e2. */ Outside = Before | AtStart | Inside | AtEnd | After, /**< The first QDateTime starts before the start of the other, * and ends after the end of the other, * i.e. s1 < s2, e1 > e2. */ StartsAt = AtStart | Inside | AtEnd | After, /**< The first QDateTime starts at the same time as the other, * and ends after the end of the other, * i.e. s1 = s2, e1 > e2. */ EndsAt = Before | AtStart | Inside | AtEnd /**< The first QDateTime starts before the start of the other, * and ends at the same time as the other, * i.e. s1 < s2, e1 = e2. */ }; /** * Compare two QDateTime instances to determine whether they are * simultaneous, earlier or later. * The comparison takes time zones into account: if the two instances have * different time zones, they are first converted to UTC before comparing. * * If both instances are not all-day values, the first instance is considered to * be either simultaneous, earlier or later, and does not overlap. * * If one instance is all-day and the other is a not all-day, the first instance * is either strictly earlier, strictly later, or overlaps. * * If both instance are all-day, they are considered simultaneous if both * their start of day and end of day times are simultaneous with each * other. (Both start and end of day times need to be considered in case a * daylight savings change occurs during that day.) Otherwise, the first instance * can be strictly earlier, earlier but overlapping, later but overlapping, * or strictly later. * * Note that if either instance is a local time (Qt::TimeSpec of Qt::LocalTime), * the result cannot be guaranteed to be correct, since by definition they * contain no information about time zones or daylight savings changes. * * @return DateTimeComparison indicating teh relationship of dt1 to dt2 * @see operator==(), operator!=(), operator<(), operator<=(), operator>=(), operator>() */ DateTimeComparison compare(const QDateTime &dt1, bool isAllDay1, const QDateTime &dt2, bool isAllDay2) { QDateTime start1, start2; //FIXME When secondOccurrence is available in QDateTime //const bool conv = (!d->equalSpec(*other.d) || d->secondOccurrence() != other.d->secondOccurrence()); - const bool conv = dt1.timeSpec() != dt2.timeSpec() - || (dt1.timeSpec() == Qt::OffsetFromUTC && dt1.offsetFromUtc() != dt2.offsetFromUtc()) - || (dt1.timeSpec() == Qt::TimeZone && dt1.timeZone() != dt2.timeZone()); + const bool conv = + dt1.timeSpec() != dt2.timeSpec() || + (dt1.timeSpec() == Qt::OffsetFromUTC && dt1.offsetFromUtc() != dt2.offsetFromUtc()) || (dt1.timeSpec() == Qt::TimeZone && dt1.timeZone() != dt2.timeZone()); if (conv) { // Different time specs or one is a time which occurs twice, // so convert to UTC before comparing start1 = dt1.toUTC(); start2 = dt2.toUTC(); } else { // Same time specs, so no need to convert to UTC start1 = dt1; start2 = dt2; } if (isAllDay1 || isAllDay2) { // At least one of the instances is date-only, so we need to compare // time periods rather than just times. QDateTime end1, end2; if (conv) { if (isAllDay1) { QDateTime dt(dt1); dt.setTime(QTime(23, 59, 59, 999)); end1 = dt.toUTC(); } else { end1 = start1; } if (isAllDay2) { QDateTime dt(dt2); dt.setTime(QTime(23, 59, 59, 999)); end2 = dt.toUTC(); } else { end2 = start2; } } else { if (isAllDay1) { end1 = QDateTime(dt1.date(), QTime(23, 59, 59, 999), Qt::LocalTime); } else { end1 = dt1; } if (isAllDay2) { end2 = QDateTime(dt2.date(), QTime(23, 59, 59, 999), Qt::LocalTime); } else { end2 = dt2; } } - if (start1 == start2) + + if (start1 == start2) { return !isAllDay1 ? AtStart : (end1 == end2) ? Equal : (end1 < end2) ? static_cast(AtStart | Inside) : static_cast(AtStart | Inside | AtEnd | After); - if (start1 < start2) + } + + if (start1 < start2) { return (end1 < start2) ? Before : (end1 == end2) ? static_cast(Before | AtStart | Inside | AtEnd) : (end1 == start2) ? static_cast(Before | AtStart) : (end1 < end2) ? static_cast(Before | AtStart | Inside) : Outside; - else + } else { return (start1 > end2) ? After : (start1 == end2) ? (end1 == end2 ? AtEnd : static_cast(AtEnd | After)) : (end1 == end2) ? static_cast(Inside | AtEnd) : (end1 < end2) ? Inside : static_cast(Inside | AtEnd | After); + } } return (start1 == start2) ? Equal : (start1 < start2) ? Before : After; } bool KCalCore::Events::startDateLessThan(const Event::Ptr &e1, const Event::Ptr &e2) { DateTimeComparison res = compare(e1->dtStart(), e1->allDay(), e2->dtStart(), e2->allDay()); if (res == Equal) { return Events::summaryLessThan(e1, e2); } else { return (res & Before || res & AtStart); } } bool KCalCore::Events::startDateMoreThan(const Event::Ptr &e1, const Event::Ptr &e2) { DateTimeComparison res = compare(e1->dtStart(), e1->allDay(), e2->dtStart(), e2->allDay()); if (res == Equal) { return Events::summaryMoreThan(e1, e2); } else { return (res & After || res & AtEnd); } } bool KCalCore::Events::summaryLessThan(const Event::Ptr &e1, const Event::Ptr &e2) { return QString::compare(e1->summary(), e2->summary(), Qt::CaseInsensitive) < 0; } bool KCalCore::Events::summaryMoreThan(const Event::Ptr &e1, const Event::Ptr &e2) { return QString::compare(e1->summary(), e2->summary(), Qt::CaseInsensitive) > 0; } bool KCalCore::Events::endDateLessThan(const Event::Ptr &e1, const Event::Ptr &e2) { DateTimeComparison res = compare(e1->dtEnd(), e1->allDay(), e2->dtEnd(), e2->allDay()); if (res == Equal) { return Events::summaryLessThan(e1, e2); } else { return (res & Before || res & AtStart); } } bool KCalCore::Events::endDateMoreThan(const Event::Ptr &e1, const Event::Ptr &e2) { DateTimeComparison res = compare(e1->dtEnd(), e1->allDay(), e2->dtEnd(), e2->allDay()); if (res == Equal) { return Events::summaryMoreThan(e1, e2); } else { return (res & After || res & AtEnd); } } bool KCalCore::Journals::dateLessThan(const Journal::Ptr &j1, const Journal::Ptr &j2) { DateTimeComparison res = compare(j1->dtStart(), j1->allDay(), j2->dtStart(), j2->allDay()); return (res & Before || res & AtStart); } bool KCalCore::Journals::dateMoreThan(const Journal::Ptr &j1, const Journal::Ptr &j2) { DateTimeComparison res = compare(j1->dtStart(), j1->allDay(), j2->dtStart(), j2->allDay()); return (res & After || res & AtEnd); } bool KCalCore::Journals::summaryLessThan(const Journal::Ptr &j1, const Journal::Ptr &j2) { - return QString::compare(j1->summary(), j2->summary(), Qt::CaseInsensitive) < 0; } bool KCalCore::Journals::summaryMoreThan(const Journal::Ptr &j1, const Journal::Ptr &j2) { return QString::compare(j1->summary(), j2->summary(), Qt::CaseInsensitive) > 0; } bool KCalCore::Todos::startDateLessThan(const Todo::Ptr &t1, const Todo::Ptr &t2) { DateTimeComparison res = compare(t1->dtStart(), t1->allDay(), t2->dtStart(), t2->allDay()); if (res == Equal) { return Todos::summaryLessThan(t1, t2); } else { return (res & Before || res & AtStart); } } bool KCalCore::Todos::startDateMoreThan(const Todo::Ptr &t1, const Todo::Ptr &t2) { DateTimeComparison res = compare(t1->dtStart(), t1->allDay(), t2->dtStart(), t2->allDay()); if (res == Equal) { return Todos::summaryMoreThan(t1, t2); } else { return (res & After || res & AtEnd); } } bool KCalCore::Todos::dueDateLessThan(const Todo::Ptr &t1, const Todo::Ptr &t2) { DateTimeComparison res = compare(t1->dtDue(), t1->allDay(), t2->dtDue(), t2->allDay()); if (res == Equal) { return Todos::summaryLessThan(t1, t2); } else { return (res & Before || res & AtStart); } } bool KCalCore::Todos::dueDateMoreThan(const Todo::Ptr &t1, const Todo::Ptr &t2) { DateTimeComparison res = compare(t1->dtDue(), t1->allDay(), t2->dtDue(), t2->allDay()); if (res == Equal) { return Todos::summaryMoreThan(t1, t2); } else { return (res & After || res & AtEnd); } } bool KCalCore::Todos::priorityLessThan(const Todo::Ptr &t1, const Todo::Ptr &t2) { if (t1->priority() < t2->priority()) { return true; } else if (t1->priority() == t2->priority()) { return Todos::summaryLessThan(t1, t2); } else { return false; } } bool KCalCore::Todos::priorityMoreThan(const Todo::Ptr &t1, const Todo::Ptr &t2) { if (t1->priority() > t2->priority()) { return true; } else if (t1->priority() == t2->priority()) { return Todos::summaryMoreThan(t1, t2); } else { return false; } } bool KCalCore::Todos::percentLessThan(const Todo::Ptr &t1, const Todo::Ptr &t2) { if (t1->percentComplete() < t2->percentComplete()) { return true; } else if (t1->percentComplete() == t2->percentComplete()) { return Todos::summaryLessThan(t1, t2); } else { return false; } } bool KCalCore::Todos::percentMoreThan(const Todo::Ptr &t1, const Todo::Ptr &t2) { if (t1->percentComplete() > t2->percentComplete()) { return true; } else if (t1->percentComplete() == t2->percentComplete()) { return Todos::summaryMoreThan(t1, t2); } else { return false; } } bool KCalCore::Todos::summaryLessThan(const Todo::Ptr &t1, const Todo::Ptr &t2) { return QString::compare(t1->summary(), t2->summary(), Qt::CaseInsensitive) < 0; } bool KCalCore::Todos::summaryMoreThan(const Todo::Ptr &t1, const Todo::Ptr &t2) { return QString::compare(t1->summary(), t2->summary(), Qt::CaseInsensitive) > 0; } bool KCalCore::Todos::createdLessThan(const Todo::Ptr &t1, const Todo::Ptr &t2) { DateTimeComparison res = compare(t1->created(), t1->allDay(), t2->created(), t2->allDay()); if (res == Equal) { return Todos::summaryLessThan(t1, t2); } else { return (res & Before || res & AtStart); } } bool KCalCore::Todos::createdMoreThan(const Todo::Ptr &t1, const Todo::Ptr &t2) { DateTimeComparison res = compare(t1->created(), t1->allDay(), t2->created(), t2->allDay()); if (res == Equal) { return Todos::summaryMoreThan(t1, t2); } else { return (res & After || res & AtEnd); } } bool KCalCore::Incidences::dateLessThan(const Incidence::Ptr &i1, const Incidence::Ptr &i2) { - DateTimeComparison res = compare(i1->dateTime(Incidence::RoleSort), i1->allDay(), i2->dateTime(Incidence::RoleSort), i2->allDay()); + DateTimeComparison res = compare(i1->dateTime(Incidence::RoleSort), i1->allDay(), + i2->dateTime(Incidence::RoleSort), i2->allDay()); if (res == Equal) { return Incidences::summaryLessThan(i1, i2); } else { return (res & Before || res & AtStart); } } bool KCalCore::Incidences::dateMoreThan(const Incidence::Ptr &i1, const Incidence::Ptr &i2) { - DateTimeComparison res = compare(i1->dateTime(Incidence::RoleSort), i1->allDay(), i2->dateTime(Incidence::RoleSort), i2->allDay()); + DateTimeComparison res = compare(i1->dateTime(Incidence::RoleSort), i1->allDay(), + i2->dateTime(Incidence::RoleSort), i2->allDay()); if (res == Equal) { return Incidences::summaryMoreThan(i1, i2); } else { return (res & After || res & AtEnd); } } bool KCalCore::Incidences::createdLessThan(const Incidence::Ptr &i1, const Incidence::Ptr &i2) { DateTimeComparison res = compare(i1->created(), i1->allDay(), i2->created(), i2->allDay()); if (res == Equal) { return Incidences::summaryLessThan(i1, i2); } else { return (res & Before || res & AtStart); } } bool KCalCore::Incidences::createdMoreThan(const Incidence::Ptr &i1, const Incidence::Ptr &i2) { DateTimeComparison res = compare(i1->created(), i1->allDay(), i2->created(), i2->allDay()); if (res == Equal) { return Incidences::summaryMoreThan(i1, i2); } else { return (res & After || res & AtEnd); } } bool KCalCore::Incidences::summaryLessThan(const Incidence::Ptr &i1, const Incidence::Ptr &i2) { return QString::compare(i1->summary(), i2->summary(), Qt::CaseInsensitive) < 0; } bool KCalCore::Incidences::summaryMoreThan(const Incidence::Ptr &i1, const Incidence::Ptr &i2) { return QString::compare(i1->summary(), i2->summary(), Qt::CaseInsensitive) > 0; } bool KCalCore::Persons::countMoreThan(const Person::Ptr &p1, const Person::Ptr &p2) { return p1->count() > p2->count(); } diff --git a/src/utils.cpp b/src/utils.cpp index 147e8c9c7..0e450da70 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -1,134 +1,133 @@ /* This file is part of the kcalcore library. Copyright (c) 2017 Daniel Vrátil 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 "utils.h" #include #include - // To remain backwards compatible we need to (de)serialize QDateTime the way KDateTime // was (de)serialized void KCalCore::serializeQDateTimeAsKDateTime(QDataStream &out, const QDateTime &dt) { out << dt.date() << dt.time(); switch (dt.timeSpec()) { case Qt::UTC: out << static_cast('u'); break; case Qt::OffsetFromUTC: out << static_cast('o') << dt.offsetFromUtc(); break; case Qt::TimeZone: serializeQTimeZoneAsSpec(out, dt.timeZone()); break; case Qt::LocalTime: out << static_cast('c'); break; } const bool isDateOnly = dt.date().isValid() && !dt.time().isValid(); out << quint8(isDateOnly ? 0x01 : 0x00); } void KCalCore::deserializeKDateTimeAsQDateTime(QDataStream &in, QDateTime &dt) { QDate date; QTime time; quint8 ts, flags; in >> date >> time >> ts; switch (static_cast(ts)) { case 'u': dt = QDateTime(date, time, Qt::UTC); break; case 'o': { int offset; in >> offset; dt = QDateTime(date, time, Qt::OffsetFromUTC, offset); break; } case 'z': { QString tzid; in >> tzid; dt = QDateTime(date, time, QTimeZone(tzid.toUtf8())); break; } case 'c': dt = QDateTime(date, time, Qt::LocalTime); break; } // unused, we don't have a special handling for date-only QDateTime in >> flags; } -void KCalCore::serializeQTimeZoneAsSpec(QDataStream &out, const QTimeZone& tz) +void KCalCore::serializeQTimeZoneAsSpec(QDataStream &out, const QTimeZone &tz) { out << static_cast('z') << (tz.isValid() ? QString::fromUtf8(tz.id()) : QString()); } void KCalCore::deserializeSpecAsQTimeZone(QDataStream &in, QTimeZone &tz) { quint8 ts; in >> ts; switch (static_cast(ts)) { case 'u': tz = QTimeZone::utc(); return; case 'o': { int offset; in >> offset; tz = QTimeZone(offset); return; } case 'z': { QString tzid; in >> tzid; tz = QTimeZone(tzid.toUtf8()); return; } case 'c': tz = QTimeZone::systemTimeZone(); break; } } void KCalCore::serializeQDateTimeList(QDataStream &out, const QList &list) { out << list.size(); for (const auto &i : list) { serializeQDateTimeAsKDateTime(out, i); } } -void KCalCore::deserializeQDateTimeList(QDataStream& in, QList& list) +void KCalCore::deserializeQDateTimeList(QDataStream &in, QList &list) { int size; in >> size; list.clear(); list.reserve(size); for (int i = 0; i < size; ++i) { QDateTime dt; deserializeKDateTimeAsQDateTime(in, dt); list << dt; } }