File indexing completed on 2024-04-21 05:31:07

0001 /*
0002     SPDX-FileCopyrightText: 2019 Michail Vourlakos <mvourlakos@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #ifndef VIEWCONTAINMENTINTERFACE_H
0007 #define VIEWCONTAINMENTINTERFACE_H
0008 
0009 // local
0010 #include "tasksmodel.h"
0011 
0012 // Qt
0013 #include <QHash>
0014 #include <QMetaMethod>
0015 #include <QObject>
0016 #include <QPointer>
0017 #include <QQuickItem>
0018 #include <QTimer>
0019 #include <QUrl>
0020 
0021 namespace Plasma {
0022 class Applet;
0023 }
0024 
0025 namespace PlasmaQuick {
0026 class AppletQuickItem;
0027 }
0028 
0029 namespace KDeclarative {
0030 class ConfigPropertyMap;
0031 }
0032 
0033 namespace Latte {
0034 class Corona;
0035 class View;
0036 }
0037 
0038 namespace Latte {
0039 namespace ViewPart {
0040 
0041 struct AppletInterfaceData
0042 {
0043     int id{-1};
0044     QString plugin;
0045     int lastValidIndex{-1};
0046     Plasma::Applet *applet{nullptr};
0047     PlasmaQuick::AppletQuickItem *plasmoid{nullptr};
0048     KDeclarative::ConfigPropertyMap *configuration{nullptr};
0049 };
0050 
0051 class ContainmentInterface: public QObject
0052 {
0053     Q_OBJECT
0054     Q_PROPERTY(bool hasExpandedApplet READ hasExpandedApplet NOTIFY hasExpandedAppletChanged)
0055     Q_PROPERTY(bool hasLatteTasks READ hasLatteTasks NOTIFY hasLatteTasksChanged)
0056     Q_PROPERTY(bool hasPlasmaTasks READ hasPlasmaTasks NOTIFY hasPlasmaTasksChanged)
0057 
0058     Q_PROPERTY(QObject *plasmoid READ plasmoid() WRITE setPlasmoid NOTIFY plasmoidChanged)
0059 
0060     Q_PROPERTY(QAbstractListModel *latteTasksModel READ latteTasksModel() NOTIFY latteTasksModelChanged)
0061     Q_PROPERTY(QAbstractListModel *plasmaTasksModel READ plasmaTasksModel() NOTIFY plasmaTasksModelChanged)
0062 
0063     //! specified from containment qml side
0064     Q_PROPERTY(QObject* layoutManager READ layoutManager WRITE setLayoutManager NOTIFY layoutManagerChanged)
0065 
0066 public:
0067     ContainmentInterface(Latte::View *parent);
0068     virtual ~ContainmentInterface();
0069 
0070     bool hasExpandedApplet() const;
0071     bool hasLatteTasks() const;
0072     bool hasPlasmaTasks() const;
0073 
0074     bool applicationLauncherInPopup() const;
0075     bool applicationLauncherHasGlobalShortcut() const;
0076     bool containsApplicationLauncher() const;
0077     bool isCapableToShowShortcutBadges();
0078 
0079     bool activateEntry(const int index);
0080     bool newInstanceForEntry(const int index);
0081 
0082     bool activatePlasmaTask(const int index);
0083     bool newInstanceForPlasmaTask(const int index);
0084 
0085     bool hideShortcutBadges();
0086     bool showOnlyMeta();
0087     bool showShortcutBadges(const bool showLatteShortcuts, const bool showMeta);
0088 
0089     //! this is updated from external apps e.g. a thunderbird plugin
0090     bool updateBadgeForLatteTask(const QString identifier, const QString value);
0091 
0092     int applicationLauncherId() const;
0093     int appletIdForVisualIndex(const int index);
0094 
0095     int indexOfApplet(const int &id);
0096     QList<int> appletsOrder() const;
0097     ViewPart::AppletInterfaceData appletDataAtIndex(const int &index);
0098     ViewPart::AppletInterfaceData appletDataForId(const int &id);
0099 
0100     QObject *plasmoid() const;
0101     void setPlasmoid(QObject *plasmoid);
0102 
0103     QObject *layoutManager() const;
0104     void setLayoutManager(QObject *manager);
0105 
0106     QAbstractListModel *latteTasksModel() const;
0107     QAbstractListModel *plasmaTasksModel() const;
0108 
0109 public slots:
0110     Q_INVOKABLE void deactivateApplets();
0111     Q_INVOKABLE void toggleAppletExpanded(const int id);
0112 
0113     Q_INVOKABLE bool appletIsExpandable(const int id) const;
0114     Q_INVOKABLE bool appletIsExpanded(const int id) const;
0115     Q_INVOKABLE bool appletIsActivationTogglesExpanded(const int id) const;
0116 
0117     Q_INVOKABLE bool isApplication(const QUrl &url) const;
0118 
0119     void addApplet(const QString &pluginId);
0120     void addApplet(QObject *metadata, int x, int y);
0121     void removeApplet(const int &id);
0122     void setAppletsOrder(const QList<int> &order);
0123     void setAppletsInLockedZoom(const QList<int> &applets);
0124     void setAppletsDisabledColoring(const QList<int> &applets);
0125     void setAppletInScheduledDestruction(const int &id, const bool &enabled);
0126     void updateContainmentConfigProperty(const QString &key, const QVariant &value);
0127     void updateAppletConfigProperty(const int &id, const QString &key, const QVariant &value);    
0128 
0129 signals:
0130     void expandedAppletStateChanged();
0131     void hasExpandedAppletChanged();
0132     void hasLatteTasksChanged();
0133     void hasPlasmaTasksChanged();
0134     void initializationCompleted();
0135     void latteTasksModelChanged();
0136     void layoutManagerChanged();
0137     void plasmaTasksModelChanged();
0138     void plasmoidChanged();
0139 
0140     //! syncing signals
0141     void appletRemoved(const int &id);
0142 
0143     void appletConfigPropertyChanged(const int &id, const QString &key, const QVariant &value);
0144     void appletCreated(const QString &pluginId);
0145     void appletDataCreated(const int &id);
0146     void appletDropped(QObject *data, int x, int y);
0147     void containmentConfigPropertyChanged(const QString &key, const QVariant &value);
0148     void appletsOrderChanged();
0149     void appletsInLockedZoomChanged(const QList<int> &applets);
0150     void appletsDisabledColoringChanged(const QList<int> &applets);
0151     void appletInScheduledDestructionChanged(const int &id, const bool &enabled);
0152 
0153     void appletRequestedVisualIndicator(const int &plasmoidId);
0154 
0155 private slots:
0156     void identifyShortcutsHost();
0157     void identifyMethods();
0158 
0159     void updateAppletsOrder();
0160     void updateAppletsInLockedZoom();
0161     void updateAppletsDisabledColoring();
0162     void updateAppletsTracking();
0163     void updateAppletDelayedConfiguration();
0164 
0165     void onAppletAdded(Plasma::Applet *applet);
0166     void onAppletExpandedChanged();
0167     void onLatteTasksCountChanged();
0168     void onPlasmaTasksCountChanged();
0169 
0170 private:
0171     void addExpandedApplet(PlasmaQuick::AppletQuickItem * appletQuickItem);
0172     void removeExpandedApplet(PlasmaQuick::AppletQuickItem *appletQuickItem);
0173     void initAppletConfigurationSignals(const int &id, KDeclarative::ConfigPropertyMap *configuration);
0174 
0175     bool appletIsExpandable(PlasmaQuick::AppletQuickItem *appletQuickItem) const;
0176 
0177     KDeclarative::ConfigPropertyMap *appletConfiguration(const Plasma::Applet *applet);
0178 
0179     QList<int> toIntList(const QVariantList &list);
0180 
0181 private:
0182     bool m_hasLatteTasks{false};
0183     bool m_hasPlasmaTasks{false};
0184 
0185     QMetaMethod m_activateEntryMethod;
0186     QMetaMethod m_appletIdForIndexMethod;
0187     QMetaMethod m_newInstanceMethod;
0188     QMetaMethod m_showShortcutsMethod;
0189 
0190     QPointer<Latte::Corona> m_corona;
0191     QPointer<Latte::View> m_view;
0192     QPointer<QQuickItem> m_shortcutsHost;
0193 
0194     //! startup timer to initialize
0195     //! applets tracking
0196     QTimer m_appletsExpandedConnectionsTimer;
0197 
0198     TasksModel *m_latteTasksModel;
0199     TasksModel *m_plasmaTasksModel;
0200 
0201     //!follow containment plasmoid
0202     QPointer<QObject> m_plasmoid;
0203     QPointer<QObject> m_layoutManager;
0204     QPointer<KDeclarative::ConfigPropertyMap> m_configuration;
0205 
0206     //!keep record of applet ids and avoid crashes when trying to access ids for already destroyed applets
0207     QHash<PlasmaQuick::AppletQuickItem *, int> m_expandedAppletIds;
0208     QHash<PlasmaQuick::AppletQuickItem *, QMetaObject::Connection> m_appletsExpandedConnections;
0209 
0210     //!all applet data
0211     QList<int> m_appletOrder; //includes justify splitters
0212     QList<int> m_appletsInLockedZoom;
0213     QList<int> m_appletsDisabledColoring;
0214     QHash<int, ViewPart::AppletInterfaceData> m_appletData;
0215     QTimer m_appletDelayedConfigurationTimer;
0216 };
0217 
0218 }
0219 }
0220 
0221 #endif