1 /*
2     SPDX-FileCopyrightText: 2019 Michail Vourlakos <mvourlakos@gmail.com>
3     SPDX-License-Identifier: GPL-2.0-or-later
4 */
5 
6 #ifndef LAYOUTSSYNCHRONIZER_H
7 #define LAYOUTSSYNCHRONIZER_H
8 
9 // local
10 #include "../apptypes.h"
11 #include "../data/layoutdata.h"
12 #include "../data/layoutstable.h"
13 
14 // Qt
15 #include <QObject>
16 #include <QHash>
17 #include <QTimer>
18 
19 
20 namespace Latte {
21 class CentralLayout;
22 class View;
23 namespace Layout{
24 class GenericLayout;
25 }
26 namespace Layouts {
27 class Manager;
28 }
29 }
30 
31 namespace Plasma {
32 class Containment;
33 }
34 
35 namespace KActivities {
36 class Controller;
37 }
38 
39 namespace Latte {
40 namespace Layouts {
41 
42 //! This is a Layouts map in the following structure:
43 //! ACTIVITY ID -> Layout Names for that activity
44 typedef QHash<QString, QStringList> AssignedLayoutsHash;
45 
46 //! Layouts::Synchronizer is a very IMPORTANT class which is responsible
47 //! for all ACTIVE layouts, meaning layouts that have been loaded
48 //! in memory.
49 //!
50 //! The main task of Synchronizer is to load/unload layouts based
51 //! on "user preferences"/"activities settings"/"application current
52 //! phase" (e.g. startup/closing)
53 //!
54 
55 class Synchronizer : public QObject {
56     Q_OBJECT
57 
58 public:
59     Synchronizer(QObject *parent);
60     ~Synchronizer() override;
61 
62     void unloadLayouts();
63 
64     void hideAllViews();
65     void pauseLayout(QString layoutName);
66     void syncActiveLayoutsToOriginalFiles();
67     void syncLatteViewsToScreens();
68     void syncMultipleLayoutsToActivities();
69 
70     //! In that case single layout file must be removed after loading the new layout
71     void setIsSingleLayoutInDeprecatedRenaming(const bool &enabled);
72 
73     bool latteViewExists(Latte::View *view) const;
74     bool layoutExists(QString layoutName) const;
75     //! switch to specified layout, default previousMemoryUsage means that it didn't change
76     bool switchToLayout(QString layoutName,  MemoryUsage::LayoutsMemory newMemoryUsage = MemoryUsage::Current);
77 
78     int centralLayoutPos(QString id) const;
79 
80     QStringList centralLayoutsNames();
81     QStringList currentLayoutsNames() const;
82     QStringList layouts() const;
83     QStringList menuLayouts() const;
84 
85     QStringList activities();
86     QStringList freeActivities();
87     QStringList runningActivities();
88     QStringList freeRunningActivities(); //! These are activities that haven't been assigned to specific layout
89     QStringList validActivities(const QStringList &layoutActivities);
90 
91     int screenForContainment(Plasma::Containment *containment);
92     Latte::View *viewForContainment(Plasma::Containment *containment);
93     Latte::View *viewForContainment(uint id);
94 
95     QList<CentralLayout *> currentLayouts() const;
96     QList<Latte::View *> currentViews() const;
97     QList<Latte::View *> currentViewsWithPlasmaShortcuts() const;
98     QList<Latte::View *> sortedCurrentViews() const;
99     QList<Latte::View *> viewsBasedOnActivityId(const QString &id) const;
100 
101     CentralLayout *centralLayout(QString layoutname) const;
102     Layout::GenericLayout *layout(QString layoutname) const;
103 
104     QList<CentralLayout *> centralLayoutsForActivity(const QString activityid) const;
105 
106     KActivities::Controller *activitiesController() const;
107 
108     Data::Layout data(const QString &storedLayoutName) const;
109     Data::LayoutsTable layoutsTable() const;
110     void setLayoutsTable(const Data::LayoutsTable &table);
111 
112 public slots:
113     void initLayouts();
114     void updateKWinDisabledBorders();
115 
116     void updateLayoutsTable();
117 
118 signals:
119     void centralLayoutsChanged();
120     void layoutsChanged();
121     void runningActicitiesChanged();
122     void initializationFinished();
123 
124     void currentLayoutIsSwitching(QString layoutName);
125 
126     void newLayoutAdded(const Data::Layout &layout);
127     void layoutActivitiesChanged(const Data::Layout &layout);
128 
129 private slots:
130     void onActivityRemoved(const QString &activityid);
131     void onLayoutAdded(const QString &layoutpath);
132 
133     void reloadAssignedLayouts();
134     void updateBorderlessMaximizedAfterTimer();
135 
136 private:
137     void addLayout(CentralLayout *layout);
138     void unloadCentralLayout(CentralLayout *layout);
139     void unloadLayouts(const QStringList &layoutNames);
140 
141     bool initSingleMode(QString layoutName);
142     bool initMultipleMode(QString layoutName);
143 
144     bool switchToLayoutInMultipleMode(QString layoutName);
145     bool switchToLayoutInSingleMode(QString layoutName);
146     bool switchToLayoutInMultipleModeBasedOnActivities(const QString &layoutName);
147 
148     bool isAssigned(QString layoutName) const;
149     bool memoryInitialized() const;
150 
151     QString layoutPath(QString layoutName);
152 
153 private:
154     bool m_multipleModeInitialized{false};
155     bool m_isLoaded{false};
156     bool m_isSingleLayoutInDeprecatedRenaming{false};
157 
158     QTimer m_updateBorderlessMaximized;
159 
160     Data::LayoutsTable m_layouts;
161     QList<CentralLayout *> m_centralLayouts;
162     AssignedLayoutsHash m_assignedLayouts;
163 
164     Layouts::Manager *m_manager;
165     KActivities::Controller *m_activitiesController;
166 };
167 
168 }
169 }
170 
171 
172 #endif
173