File indexing completed on 2024-04-14 05:24:26

0001 /*
0002     SPDX-FileCopyrightText: 2019 Michail Vourlakos <mvourlakos@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #ifndef LAYOUTSSYNCHRONIZER_H
0007 #define LAYOUTSSYNCHRONIZER_H
0008 
0009 // local
0010 #include "../apptypes.h"
0011 #include "../data/layoutdata.h"
0012 #include "../data/layoutstable.h"
0013 
0014 // Qt
0015 #include <QObject>
0016 #include <QHash>
0017 #include <QTimer>
0018 #include <QStringList>
0019 
0020 
0021 namespace Latte {
0022 class CentralLayout;
0023 class View;
0024 namespace Layout{
0025 class GenericLayout;
0026 }
0027 namespace Layouts {
0028 class Manager;
0029 }
0030 }
0031 
0032 namespace Plasma {
0033 class Containment;
0034 }
0035 
0036 namespace KActivities {
0037 class Controller;
0038 }
0039 
0040 namespace Latte {
0041 namespace Layouts {
0042 
0043 //! This is a Layouts map in the following structure:
0044 //! ACTIVITY ID -> Layout Names for that activity
0045 typedef QHash<QString, QStringList> AssignedLayoutsHash;
0046 
0047 //! Layouts::Synchronizer is a very IMPORTANT class which is responsible
0048 //! for all ACTIVE layouts, meaning layouts that have been loaded
0049 //! in memory.
0050 //!
0051 //! The main task of Synchronizer is to load/unload layouts based
0052 //! on "user preferences"/"activities settings"/"application current
0053 //! phase" (e.g. startup/closing)
0054 //!
0055 
0056 class Synchronizer : public QObject {
0057     Q_OBJECT
0058 
0059 public:
0060     Synchronizer(QObject *parent);
0061     ~Synchronizer() override;
0062 
0063     void unloadLayouts();
0064 
0065     void hideAllViews();
0066     void pauseLayout(QString layoutName);
0067     void syncActiveLayoutsToOriginalFiles();
0068     void syncLatteViewsToScreens();
0069     void syncMultipleLayoutsToActivities(QStringList preloadedLayouts = QStringList());
0070 
0071     //! In that case single layout file must be removed after loading the new layout
0072     void setIsSingleLayoutInDeprecatedRenaming(const bool &enabled);
0073 
0074     bool latteViewExists(Latte::View *view) const;
0075     bool layoutExists(QString layoutName) const;
0076     //! switch to specified layout, default previousMemoryUsage means that it didn't change
0077     bool switchToLayout(QString layoutName,  MemoryUsage::LayoutsMemory newMemoryUsage = MemoryUsage::Current);
0078 
0079     int centralLayoutPos(QString id) const;
0080 
0081     QStringList centralLayoutsNames();
0082     QStringList currentLayoutsNames() const;
0083     QStringList layouts() const;
0084     QStringList menuLayouts() const;
0085 
0086     QStringList activities();
0087     QStringList freeActivities();
0088     QStringList runningActivities();
0089     QStringList freeRunningActivities(); //! These are activities that haven't been assigned to specific layout
0090     QStringList validActivities(const QStringList &layoutActivities);
0091 
0092     int screenForContainment(Plasma::Containment *containment);
0093     Latte::View *viewForContainment(Plasma::Containment *containment);
0094     Latte::View *viewForContainment(uint id);
0095 
0096     QList<CentralLayout *> currentLayouts() const;
0097     QList<Latte::View *> currentViews() const;
0098     QList<Latte::View *> currentViewsWithPlasmaShortcuts() const;
0099     QList<Latte::View *> currentOriginalViews() const;
0100     QList<Latte::View *> sortedCurrentViews() const;
0101     QList<Latte::View *> sortedCurrentOriginalViews() const;
0102     QList<Latte::View *> viewsBasedOnActivityId(const QString &id) const;
0103 
0104     CentralLayout *centralLayout(QString layoutname) const;
0105     Layout::GenericLayout *layout(QString layoutname) const;
0106 
0107     QList<CentralLayout *> centralLayoutsForActivity(const QString activityid) const;
0108 
0109     KActivities::Controller *activitiesController() const;
0110 
0111     Data::Layout data(const QString &storedLayoutName) const;
0112     Data::LayoutsTable layoutsTable() const;
0113     void setLayoutsTable(const Data::LayoutsTable &table);
0114 
0115 public slots:
0116     void initLayouts();
0117     void updateKWinDisabledBorders();
0118 
0119     void updateLayoutsTable();
0120 
0121 signals:
0122     void centralLayoutsChanged();
0123     void layoutsChanged();
0124     void runningActicitiesChanged();
0125     void initializationFinished();
0126 
0127     void currentLayoutIsSwitching(QString layoutName);
0128 
0129     void newLayoutAdded(const Data::Layout &layout);
0130     void layoutActivitiesChanged(const Data::Layout &layout);
0131 
0132 private slots:
0133     void onActivityRemoved(const QString &activityid);
0134     void onLayoutAdded(const QString &layoutpath);
0135 
0136     void unloadPreloadedLayouts();
0137     void reloadAssignedLayouts();
0138     void updateBorderlessMaximizedAfterTimer();
0139 
0140 private:
0141     void addLayout(CentralLayout *layout);
0142     void unloadCentralLayout(CentralLayout *layout);
0143     void unloadLayouts(const QStringList &layoutNames, const QStringList &preloadedLayouts);
0144 
0145     bool initSingleMode(QString layoutName);
0146     bool initMultipleMode(QString layoutName);
0147 
0148     bool switchToLayoutInMultipleMode(QString layoutName);
0149     bool switchToLayoutInSingleMode(QString layoutName);
0150     bool switchToLayoutInMultipleModeBasedOnActivities(const QString &layoutName);
0151 
0152     bool isAssigned(QString layoutName) const;
0153     bool memoryInitialized() const;
0154 
0155     QString layoutPath(QString layoutName);
0156 
0157 private:
0158     bool m_multipleModeInitialized{false};
0159     bool m_isLoaded{false};
0160     bool m_isSingleLayoutInDeprecatedRenaming{false};
0161 
0162     QTimer m_updateBorderlessMaximized;
0163 
0164     Data::LayoutsTable m_layouts;
0165     QList<CentralLayout *> m_centralLayouts;
0166     AssignedLayoutsHash m_assignedLayouts;
0167 
0168     Layouts::Manager *m_manager;
0169     KActivities::Controller *m_activitiesController;
0170 };
0171 
0172 }
0173 }
0174 
0175 
0176 #endif