File indexing completed on 2025-01-05 04:54:59

0001 /*
0002     Copyright (c) 2018 Michael Bohlender <michael.bohlender@kdemail.net>
0003     Copyright (c) 2018 Christian Mollekopf <mollekopf@kolabsys.com>
0004     Copyright (c) 2018 RĂ©mi Nicole <minijackson@riseup.net>
0005 
0006     This library is free software; you can redistribute it and/or modify it
0007     under the terms of the GNU Library General Public License as published by
0008     the Free Software Foundation; either version 2 of the License, or (at your
0009     option) any later version.
0010 
0011     This library is distributed in the hope that it will be useful, but WITHOUT
0012     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0013     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
0014     License for more details.
0015 
0016     You should have received a copy of the GNU Library General Public License
0017     along with this library; see the file COPYING.LIB.  If not, write to the
0018     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0019     02110-1301, USA.
0020 */
0021 
0022 #pragma once
0023 #include "kube_export.h"
0024 
0025 #include <QAbstractItemModel>
0026 #include <QList>
0027 #include <QSet>
0028 #include <QSharedPointer>
0029 #include <QDateTime>
0030 #include "debouncer.h"
0031 
0032 namespace KCalendarCore {
0033     class MemoryCalendar;
0034     class Incidence;
0035 }
0036 namespace Sink {
0037     namespace ApplicationDomain {
0038         struct Event;
0039     }
0040 }
0041 class EntityCacheInterface;
0042 
0043 /**
0044  * Loads all event occurrences within the given period and matching the given filter.
0045  *
0046  * Recurrences are expanded
0047  */
0048 class KUBE_EXPORT EventOccurrenceModel : public QAbstractItemModel
0049 {
0050     Q_OBJECT
0051     Q_PROPERTY(QDate start READ start WRITE setStart)
0052     Q_PROPERTY(int length READ length WRITE setLength)
0053     Q_PROPERTY(QList<QString> calendarFilter WRITE setCalendarFilter)
0054     Q_PROPERTY(QVariantMap filter WRITE setFilter)
0055 
0056 public:
0057     enum Roles {
0058         Summary = Qt::UserRole + 1,
0059         Description,
0060         StartTime,
0061         EndTime,
0062         Color,
0063         AllDay,
0064         Event,
0065         EventOccurrence,
0066         LastRole
0067     };
0068     Q_ENUM(Roles);
0069     EventOccurrenceModel(QObject *parent = nullptr);
0070     ~EventOccurrenceModel() = default;
0071 
0072     QModelIndex index(int row, int column, const QModelIndex &parent = {}) const override;
0073     QModelIndex parent(const QModelIndex &index) const override;
0074 
0075     int rowCount(const QModelIndex &parent = {}) const override;
0076     int columnCount(const QModelIndex &parent) const override;
0077 
0078     QVariant data(const QModelIndex &index, int role) const override;
0079 
0080     void updateQuery(const QDate &start, const QDate &end, const QSet<QByteArray> &calendarFilter);
0081 
0082     void setStart(const QDate &);
0083     QDate start() const;
0084     void setLength(int);
0085     int length() const;
0086     void setCalendarFilter(const QList<QString> &);
0087     void setFilter(const QVariantMap &);
0088 
0089     struct Occurrence {
0090         QDateTime start;
0091         QDateTime end;
0092         QSharedPointer<KCalendarCore::Incidence> incidence;
0093         QByteArray color;
0094         bool allDay;
0095         QSharedPointer<Sink::ApplicationDomain::Event> domainObject;
0096     };
0097 
0098     bool initialItemsComplete() const;
0099 
0100 signals:
0101     void initialItemsLoaded();
0102 
0103 private:
0104     void updateQuery();
0105 
0106     void refreshView();
0107     void updateFromSource();
0108     QByteArray getColor(const QByteArray &calendar) const;
0109 
0110     QSharedPointer<QAbstractItemModel> mSourceModel;
0111     QSet<QByteArray> mCalendarFilter;
0112     QDate mStart;
0113     QDate mEnd;
0114     int mLength{0};
0115     QSharedPointer<EntityCacheInterface> mCalendarCache;
0116 
0117     Debouncer mUpdateFromSourceDebouncer;
0118 
0119     QList<Occurrence> mEvents;
0120     QVariantMap mFilter;
0121     bool mInitialItemsLoaded{false};
0122 };
0123 
0124 Q_DECLARE_METATYPE(EventOccurrenceModel::Occurrence);