File indexing completed on 2024-04-21 03:52:45

0001 /*
0002   This file is part of the kcalcore library.
0003 
0004   SPDX-FileCopyrightText: 2001 Cornelius Schumacher <schumacher@kde.org>
0005   SPDX-FileCopyrightText: 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
0006   SPDX-FileCopyrightText: 2004 Bram Schoenmakers <bramschoenmakers@kde.nl>
0007 
0008   SPDX-License-Identifier: LGPL-2.0-or-later
0009 */
0010 /**
0011   @file
0012   This file is part of the API for handling calendar data and
0013   defines the CalFilter class.
0014 
0015   @brief
0016   Provides a filter for calendars.
0017 
0018   @author Cornelius Schumacher \<schumacher@kde.org\>
0019   @author Reinhold Kainhofer \<reinhold@kainhofer.com\>
0020   @author Bram Schoenmakers \<bramschoenmakers@kde.nl\>
0021 */
0022 
0023 #include "calfilter.h"
0024 
0025 using namespace KCalendarCore;
0026 
0027 /**
0028   Private class that helps to provide binary compatibility between releases.
0029   @internal
0030 */
0031 //@cond PRIVATE
0032 class Q_DECL_HIDDEN KCalendarCore::CalFilter::Private
0033 {
0034 public:
0035     Private()
0036     {
0037     }
0038     QString mName; // filter name
0039     QStringList mCategoryList;
0040     QStringList mEmailList;
0041     int mCriteria = 0;
0042     int mCompletedTimeSpan = 0;
0043     bool mEnabled = true;
0044 };
0045 //@endcond
0046 
0047 CalFilter::CalFilter()
0048     : d(new KCalendarCore::CalFilter::Private)
0049 {
0050 }
0051 
0052 CalFilter::CalFilter(const QString &name)
0053     : d(new KCalendarCore::CalFilter::Private)
0054 {
0055     d->mName = name;
0056 }
0057 
0058 CalFilter::~CalFilter()
0059 {
0060     delete d;
0061 }
0062 
0063 bool KCalendarCore::CalFilter::operator==(const CalFilter &filter) const
0064 {
0065     return d->mName == filter.d->mName && d->mCriteria == filter.d->mCriteria && d->mCategoryList == filter.d->mCategoryList
0066         && d->mEmailList == filter.d->mEmailList && d->mCompletedTimeSpan == filter.d->mCompletedTimeSpan;
0067 }
0068 
0069 void CalFilter::apply(Event::List *eventList) const
0070 {
0071     if (!d->mEnabled) {
0072         return;
0073     }
0074 
0075     auto it = std::remove_if(eventList->begin(), eventList->end(), [this](const Incidence::Ptr &incidence) {
0076         return !filterIncidence(incidence);
0077     });
0078     eventList->erase(it, eventList->end());
0079 }
0080 
0081 // TODO: avoid duplicating apply() code
0082 void CalFilter::apply(Todo::List *todoList) const
0083 {
0084     if (!d->mEnabled) {
0085         return;
0086     }
0087 
0088     auto it = std::remove_if(todoList->begin(), todoList->end(), [this](const Incidence::Ptr &incidence) {
0089         return !filterIncidence(incidence);
0090     });
0091     todoList->erase(it, todoList->end());
0092 }
0093 
0094 void CalFilter::apply(Journal::List *journalList) const
0095 {
0096     if (!d->mEnabled) {
0097         return;
0098     }
0099 
0100     auto it = std::remove_if(journalList->begin(), journalList->end(), [this](const Incidence::Ptr &incidence) {
0101         return !filterIncidence(incidence);
0102     });
0103     journalList->erase(it, journalList->end());
0104 }
0105 
0106 bool CalFilter::filterIncidence(const Incidence::Ptr &incidence) const
0107 {
0108     if (!d->mEnabled) {
0109         return true;
0110     }
0111 
0112     Todo::Ptr todo = incidence.dynamicCast<Todo>();
0113     if (todo) {
0114         if ((d->mCriteria & HideCompletedTodos) && todo->isCompleted()) {
0115             // Check if completion date is suffently long ago:
0116             if (todo->completed().addDays(d->mCompletedTimeSpan) < QDateTime::currentDateTimeUtc()) {
0117                 return false;
0118             }
0119         }
0120 
0121         if ((d->mCriteria & HideInactiveTodos) && ((todo->hasStartDate() && QDateTime::currentDateTimeUtc() < todo->dtStart()) || todo->isCompleted())) {
0122             return false;
0123         }
0124 
0125         if (d->mCriteria & HideNoMatchingAttendeeTodos) {
0126             bool iAmOneOfTheAttendees = false;
0127             const Attendee::List &attendees = todo->attendees();
0128             if (!attendees.isEmpty()) {
0129                 iAmOneOfTheAttendees = std::any_of(attendees.cbegin(), attendees.cend(), [this](const Attendee &att) {
0130                     return d->mEmailList.contains(att.email());
0131                 });
0132             } else {
0133                 // no attendees, must be me only
0134                 iAmOneOfTheAttendees = true;
0135             }
0136             if (!iAmOneOfTheAttendees) {
0137                 return false;
0138             }
0139         }
0140     }
0141 
0142     if (d->mCriteria & HideRecurring) {
0143         if (incidence->recurs() || incidence->hasRecurrenceId()) {
0144             return false;
0145         }
0146     }
0147 
0148     const QStringList incidenceCategories = incidence->categories();
0149     bool isFound = false;
0150     for (const auto &category : std::as_const(d->mCategoryList)) {
0151         if (incidenceCategories.contains(category)) {
0152             isFound = true;
0153             break;
0154         }
0155     }
0156 
0157     return (d->mCriteria & ShowCategories) ? isFound : !isFound;
0158 }
0159 
0160 void CalFilter::setName(const QString &name)
0161 {
0162     d->mName = name;
0163 }
0164 
0165 QString CalFilter::name() const
0166 {
0167     return d->mName;
0168 }
0169 
0170 void CalFilter::setEnabled(bool enabled)
0171 {
0172     d->mEnabled = enabled;
0173 }
0174 
0175 bool CalFilter::isEnabled() const
0176 {
0177     return d->mEnabled;
0178 }
0179 
0180 void CalFilter::setCriteria(int criteria)
0181 {
0182     d->mCriteria = criteria;
0183 }
0184 
0185 int CalFilter::criteria() const
0186 {
0187     return d->mCriteria;
0188 }
0189 
0190 void CalFilter::setCategoryList(const QStringList &categoryList)
0191 {
0192     d->mCategoryList = categoryList;
0193 }
0194 
0195 QStringList CalFilter::categoryList() const
0196 {
0197     return d->mCategoryList;
0198 }
0199 
0200 void CalFilter::setEmailList(const QStringList &emailList)
0201 {
0202     d->mEmailList = emailList;
0203 }
0204 
0205 QStringList CalFilter::emailList() const
0206 {
0207     return d->mEmailList;
0208 }
0209 
0210 void CalFilter::setCompletedTimeSpan(int timespan)
0211 {
0212     d->mCompletedTimeSpan = timespan;
0213 }
0214 
0215 int CalFilter::completedTimeSpan() const
0216 {
0217     return d->mCompletedTimeSpan;
0218 }