File indexing completed on 2024-05-19 05:38:23

0001 /*
0002     SPDX-FileCopyrightText: 2019 Kai Uwe Broulik <kde@privat.broulik.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QAbstractItemModel>
0010 #include <QHash>
0011 #include <QList>
0012 #include <QString>
0013 
0014 #include <KService>
0015 
0016 #include "eventsettings.h"
0017 
0018 // FIXME add constructors for KConfigGroup
0019 struct SourceData {
0020     static SourceData fromService(KService::Ptr service);
0021 
0022     QString name;
0023     QString comment;
0024     QString iconName;
0025     bool isDefault = true;
0026 
0027     QString notifyRcName;
0028     QString desktopEntry;
0029 
0030     QList<NotificationManager::EventSettings *> events;
0031 
0032     QString display() const
0033     {
0034         return !name.isEmpty() ? name : comment;
0035     }
0036 };
0037 
0038 class SourcesModel : public QAbstractItemModel
0039 {
0040     Q_OBJECT
0041 
0042 public:
0043     SourcesModel(QObject *parent = nullptr);
0044     ~SourcesModel() override;
0045 
0046     enum Roles {
0047         SourceTypeRole = Qt::UserRole + 1,
0048         NotifyRcNameRole,
0049         DesktopEntryRole,
0050         IsDefaultRole,
0051 
0052         CommentRole,
0053         ShowIconsRole,
0054         ActionsRole,
0055         SoundRole,
0056         DefaultActionsRole,
0057         DefaultSoundRole,
0058     };
0059     Q_ENUM(Roles)
0060 
0061     enum Type {
0062         ApplicationType,
0063         ServiceType,
0064     };
0065     Q_ENUM(Type)
0066 
0067     Q_INVOKABLE QPersistentModelIndex makePersistentModelIndex(const QModelIndex &idx) const;
0068 
0069     Q_INVOKABLE QPersistentModelIndex persistentIndexForDesktopEntry(const QString &desktopEntry) const;
0070     Q_INVOKABLE QPersistentModelIndex persistentIndexForNotifyRcName(const QString &notifyRcName) const;
0071 
0072     int columnCount(const QModelIndex &parent) const override;
0073     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0074 
0075     QVariant data(const QModelIndex &index, int role) const override;
0076     bool setData(const QModelIndex &index, const QVariant &value, int role) override;
0077 
0078     QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
0079     QModelIndex parent(const QModelIndex &child) const override;
0080 
0081     QHash<int, QByteArray> roleNames() const override;
0082 
0083     Q_INVOKABLE void load();
0084 
0085     void loadEvents();
0086     void saveEvents();
0087 
0088     bool isEventSaveNeeded() const;
0089     bool isEventDefaults() const;
0090     void setEventDefaults();
0091 
0092 private:
0093     QList<SourceData> m_data;
0094 };