File indexing completed on 2024-05-12 05:37:10

0001 /*
0002     SPDX-FileCopyrightText: 2020 Konrad Materka <materka@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QAbstractListModel>
0010 #include <QConcatenateTablesProxyModel>
0011 #include <QList>
0012 #include <QPointer>
0013 #include <qqmlregistration.h>
0014 
0015 #include <KPluginMetaData>
0016 #include <Plasma/Plasma>
0017 #include <Plasma5Support/Service>
0018 
0019 namespace Plasma
0020 {
0021 class Applet;
0022 class PluginLoader;
0023 }
0024 
0025 class PlasmoidRegistry;
0026 class SystemTraySettings;
0027 class StatusNotifierItemHost;
0028 
0029 /**
0030  * @brief Base class for models used in System Tray.
0031  *
0032  */
0033 class BaseModel : public QAbstractListModel
0034 {
0035     Q_OBJECT
0036 public:
0037     enum class BaseRole {
0038         ItemType = Qt::UserRole + 1,
0039         ItemId,
0040         CanRender,
0041         Category,
0042         Status,
0043         EffectiveStatus,
0044         LastBaseRole,
0045     };
0046 
0047     explicit BaseModel(QPointer<SystemTraySettings> settings, QObject *parent = nullptr);
0048 
0049     QHash<int, QByteArray> roleNames() const override;
0050 
0051 private Q_SLOTS:
0052     void onConfigurationChanged();
0053 
0054 protected:
0055     Plasma::Types::ItemStatus calculateEffectiveStatus(bool canRender, Plasma::Types::ItemStatus status, QString itemId) const;
0056 
0057 private:
0058     QPointer<SystemTraySettings> m_settings;
0059 
0060     bool m_showAllItems;
0061     QStringList m_shownItems;
0062     QStringList m_hiddenItems;
0063 };
0064 
0065 /**
0066  * @brief Data model for plasmoids/applets.
0067  */
0068 class PlasmoidModel : public BaseModel
0069 {
0070     Q_OBJECT
0071 public:
0072     enum class Role {
0073         Applet = static_cast<int>(BaseModel::BaseRole::LastBaseRole) + 1,
0074         HasApplet,
0075     };
0076 
0077     explicit PlasmoidModel(const QPointer<SystemTraySettings> &settings, const QPointer<PlasmoidRegistry> &plasmoidRegistry, QObject *parent = nullptr);
0078 
0079     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0080     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0081     QHash<int, QByteArray> roleNames() const override;
0082 
0083 public Q_SLOTS:
0084     void addApplet(Plasma::Applet *applet);
0085     void removeApplet(Plasma::Applet *applet);
0086 
0087 private Q_SLOTS:
0088     void appendRow(const KPluginMetaData &pluginMetaData);
0089     void removeRow(const QString &pluginId);
0090 
0091 private:
0092     struct Item {
0093         KPluginMetaData pluginMetaData;
0094         Plasma::Applet *applet = nullptr;
0095     };
0096 
0097     int indexOfPluginId(const QString &pluginId) const;
0098 
0099     QPointer<PlasmoidRegistry> m_plasmoidRegistry;
0100 
0101     QList<Item> m_items;
0102 };
0103 
0104 /**
0105  * @brief Data model for Status Notifier Items (SNI).
0106  * @note This model is exported to QML for plasma-mobile
0107  */
0108 class StatusNotifierModel : public BaseModel
0109 {
0110     Q_OBJECT
0111     QML_ELEMENT
0112 
0113 public:
0114     enum class Role {
0115         DataEngineSource = static_cast<int>(BaseModel::BaseRole::LastBaseRole) + 100,
0116         Service,
0117         AttentionIcon,
0118         AttentionIconName,
0119         AttentionMovieName,
0120         Category,
0121         Icon,
0122         IconName,
0123         IconThemePath,
0124         Id,
0125         ItemIsMenu,
0126         OverlayIconName,
0127         Status,
0128         Title,
0129         ToolTipSubTitle,
0130         ToolTipTitle,
0131         WindowId,
0132     };
0133 
0134     explicit StatusNotifierModel(QObject *parent = nullptr);
0135     explicit StatusNotifierModel(QPointer<SystemTraySettings> settings, QObject *parent = nullptr);
0136 
0137     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0138     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0139     QHash<int, QByteArray> roleNames() const override;
0140 
0141     struct Item {
0142         QString source;
0143         Plasma5Support::Service *service = nullptr;
0144     };
0145 
0146 public Q_SLOTS:
0147     void addSource(const QString &source);
0148     void removeSource(const QString &source);
0149     void dataUpdated(const QString &sourceName);
0150 
0151 private:
0152     int indexOfSource(const QString &source) const;
0153     void init();
0154 
0155     StatusNotifierItemHost *m_sniHost = nullptr;
0156     QList<Item> m_items;
0157 };
0158 Q_DECLARE_TYPEINFO(StatusNotifierModel::Item, Q_RELOCATABLE_TYPE);
0159 
0160 /**
0161  * @brief Cantenating model for system tray, that can expose multiple data models as one.
0162  */
0163 class SystemTrayModel : public QConcatenateTablesProxyModel
0164 {
0165     Q_OBJECT
0166 public:
0167     explicit SystemTrayModel(QObject *parent = nullptr);
0168 
0169     QHash<int, QByteArray> roleNames() const override;
0170 
0171     void addSourceModel(QAbstractItemModel *sourceModel);
0172 
0173 private:
0174     QHash<int, QByteArray> m_roleNames;
0175 };