File indexing completed on 2024-05-05 05:28:20

0001 /*
0002  * SPDX-FileCopyrightText: 2019 Dimitris Kardarakos <dimkard@posteo.net>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #ifndef INCIDENCEALARMSMODEL_H
0008 #define INCIDENCEALARMSMODEL_H
0009 
0010 #include <QAbstractListModel>
0011 #include <KCalendarCore/Alarm>
0012 #include <KCalendarCore/Duration>
0013 #include <QVariantList>
0014 #include <QVariantMap>
0015 #include <QHash>
0016 
0017 class LocalCalendar;
0018 /**
0019  * @brief Model that serves the alarms of an Incidence set in the input properties
0020  *
0021  */
0022 class IncidenceAlarmsModel : public QAbstractListModel
0023 {
0024     Q_OBJECT
0025 
0026     Q_PROPERTY(QVariantMap alarmProperties READ alarmProperties WRITE setAlarmProperties NOTIFY alarmPropertiesChanged)
0027 
0028 public:
0029     explicit IncidenceAlarmsModel(QObject *parent = nullptr);
0030     ~IncidenceAlarmsModel() override;
0031 
0032     enum RoleNames {
0033         StartOffsetValue = Qt::UserRole + 1,
0034         StartOffsetType,
0035         ActionType
0036     };
0037 
0038     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0039     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0040     QHash<int, QByteArray> roleNames() const override;
0041 
0042     /**
0043      * @return A QVariantMap of the input properties
0044      */
0045     QVariantMap alarmProperties() const;
0046     /**
0047      * @brief Sets the input properties of the model. \p alarmProps should be a QVariantMap with the following members: 1) uid: the uid of the Incidence 2) calendar: the LocalCalendar* that the Incidence belongs to
0048      */
0049     void setAlarmProperties(const QVariantMap &alarmProps);
0050 
0051     /**
0052      * @return A QVariantList of the items of the model. The members of the list are QHash<QString, QVariant> items that contain the following members: startOffsetValue, startOffsetType and actionType
0053      */
0054     Q_INVOKABLE QVariantList alarms() const;
0055 
0056 public Q_SLOTS:
0057 
0058     /**
0059      * @brief Removes an alarm from the model
0060      */
0061     void removeAlarm(const int row);
0062 
0063     /**
0064      * @brief Removes all alarms
0065      */
0066     void removeAll();
0067 
0068     /**
0069      * @brief Creates a model item and adds it to the model
0070      */
0071     void addAlarm(const int secondsFromStart);
0072 
0073 Q_SIGNALS:
0074     void alarmPropertiesChanged();
0075 
0076 private:
0077     void loadPersistentAlarms();
0078     QString alarmText(const int idx) const;
0079     QString alarmUid(const int idx) const;
0080     int alarmStartOffsetValue(const int idx) const;
0081     QString alarmStartOffsetType(const int idx) const;
0082     int alarmActionType(const int idx) const;
0083     QString displayText(const int idx) const;
0084 
0085     QVariantList mAlarms;
0086     QVariantMap mAlarmProperties;
0087 };
0088 
0089 #endif