File indexing completed on 2025-03-09 04:45:31

0001 /*
0002     SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
0003     SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 
0006 #ifndef CALENDARIMPORTMODEL_H
0007 #define CALENDARIMPORTMODEL_H
0008 
0009 #include <QAbstractListModel>
0010 
0011 #include <KCalendarCore/Calendar>
0012 
0013 /** List of possible events to import from a selected calendar. */
0014 class CalendarImportModel : public QAbstractListModel
0015 {
0016     Q_OBJECT
0017     Q_PROPERTY(KCalendarCore::Calendar* calendar READ calendar WRITE setCalendar NOTIFY calendarChanged)
0018     Q_PROPERTY(bool hasSelection READ hasSelection NOTIFY hasSelectionChanged)
0019 
0020 public:
0021     explicit CalendarImportModel(QObject *parent = nullptr);
0022     ~CalendarImportModel();
0023 
0024     enum Role {
0025         TitleRole = Qt::DisplayRole,
0026         SubtitleRole = Qt::UserRole,
0027         IconNameRole,
0028         ReservationsRole,
0029         SelectedRole,
0030     };
0031 
0032     KCalendarCore::Calendar *calendar() const;
0033     void setCalendar(KCalendarCore::Calendar *calendar);
0034 
0035     Q_INVOKABLE QVector<QVariant> selectedReservations() const;
0036 
0037     int rowCount(const QModelIndex &parent = {}) const override;
0038     QVariant data(const QModelIndex &index, int role) const override;
0039     bool setData(const QModelIndex &index, const QVariant &value, int role) override;
0040     QHash<int, QByteArray> roleNames() const override;
0041 
0042     bool hasSelection() const;
0043 
0044 Q_SIGNALS:
0045     void calendarChanged();
0046     void hasSelectionChanged();
0047 
0048 private:
0049     void reload();
0050     QDate today() const;
0051 
0052     KCalendarCore::Calendar *m_calendar = nullptr;
0053 
0054     struct Event {
0055         KCalendarCore::Event::Ptr event;
0056         QVector<QVariant> data;
0057         bool selected;
0058     };
0059     std::vector<Event> m_events;
0060 
0061     friend class CalendarImportModelTest;
0062     QDate m_todayOverride;
0063 };
0064 
0065 #endif // CALENDARIMPORTMODEL_H