File indexing completed on 2024-04-14 15:37:14

0001 /*
0002 *  Copyright 2019  Michail Vourlakos <mvourlakos@gmail.com>
0003 *
0004 *  This file is part of Latte-Dock
0005 *
0006 *  Latte-Dock is free software; you can redistribute it and/or
0007 *  modify it under the terms of the GNU General Public License as
0008 *  published by the Free Software Foundation; either version 2 of
0009 *  the License, or (at your option) any later version.
0010 *
0011 *  Latte-Dock is distributed in the hope that it will be useful,
0012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 *  GNU General Public License for more details.
0015 *
0016 *  You should have received a copy of the GNU General Public License
0017 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
0018 */
0019 
0020 #ifndef LAYOUTSSYNCHRONIZER_H
0021 #define LAYOUTSSYNCHRONIZER_H
0022 
0023 // Qt
0024 #include <QObject>
0025 #include <QHash>
0026 #include <QTimer>
0027 
0028 
0029 namespace Latte {
0030 class CentralLayout;
0031 class SharedLayout;
0032 class View;
0033 namespace Layout{
0034 class GenericLayout;
0035 }
0036 namespace Layouts {
0037 class Manager;
0038 }
0039 }
0040 
0041 namespace Plasma {
0042 class Containment;
0043 }
0044 
0045 namespace KActivities {
0046 class Controller;
0047 }
0048 
0049 namespace Latte {
0050 namespace Layouts {
0051 
0052 //! This is a Shares map in the following structure:
0053 //! SHARED LAYOUT NAME -> CENTRAL LAYOUT NAMES acting as SHARES
0054 typedef QHash<const QString, QStringList> SharesMap;
0055 
0056 
0057 //! Layouts::Synchronizer is a very IMPORTANT class which is responsible
0058 //! for all ACTIVE layouts, meaning layouts that have been loaded
0059 //! in memory.
0060 //!
0061 //! The main task of Synchronizer is to load/unload layouts based
0062 //! on "user preferences"/"activities settings"/"application current
0063 //! phase" (e.g. startup/closing)
0064 //!
0065 
0066 class Synchronizer : public QObject {
0067     Q_OBJECT
0068 
0069 public:
0070     Synchronizer(QObject *parent);
0071     ~Synchronizer() override;
0072 
0073     void loadLayouts();
0074     void unloadLayouts();
0075 
0076     void hideAllViews();
0077     void pauseLayout(QString layoutName);
0078     void syncActiveLayoutsToOriginalFiles();
0079     void syncLatteViewsToScreens();
0080     void syncMultipleLayoutsToActivities(QString layoutForOrphans = QString());
0081     void syncActiveShares(SharesMap &sharesMap, QStringList &deprecatedShares);
0082 
0083     bool latteViewExists(Latte::View *view) const;
0084     bool layoutExists(QString layoutName) const;
0085     bool mapHasRecord(const QString &record, SharesMap &map);
0086     bool registerAtSharedLayout(CentralLayout *central, QString id);
0087     //! switch to specified layout, default previousMemoryUsage means that it didn't change
0088     bool switchToLayout(QString layoutName, int previousMemoryUsage = -1);
0089 
0090     int centralLayoutPos(QString id) const;
0091 
0092     QString currentLayoutName() const;
0093     QString currentLayoutNameInMultiEnvironment() const;
0094     QString shouldSwitchToLayout(QString activityId);
0095 
0096     QStringList centralLayoutsNames();
0097     QStringList layouts() const;
0098     QStringList menuLayouts() const;
0099     void setMenuLayouts(QStringList layouts);
0100     QStringList sharedLayoutsNames();
0101     QStringList storedSharedLayouts() const;
0102 
0103     QStringList activities();
0104     QStringList runningActivities();
0105     QStringList orphanedActivities(); //! These are activities that haven't been assigned to specific layout
0106 
0107     Latte::View *viewForContainment(Plasma::Containment *containment);
0108 
0109     CentralLayout *currentLayout() const;
0110     CentralLayout *centralLayout(QString id) const;
0111     SharedLayout *sharedLayout(QString id) const;
0112     Layout::GenericLayout *layout(QString id) const;
0113 
0114 signals:
0115     void centralLayoutsChanged();
0116     void currentLayoutNameChanged();
0117     void layoutsChanged();
0118     void menuLayoutsChanged();
0119     void runningActicitiesChanged();
0120 
0121     void currentLayoutIsSwitching(QString layoutName);
0122 
0123 private slots:
0124     void confirmDynamicSwitch();
0125     void updateDynamicSwitchInterval();
0126     void updateCurrentLayoutNameInMultiEnvironment();
0127 
0128     void currentActivityChanged(const QString &id);
0129 
0130 private:
0131     void clearSharedLayoutsFromCentralLists();
0132 
0133     void addLayout(CentralLayout *layout);
0134     void unloadCentralLayout(CentralLayout *layout);
0135     void unloadSharedLayout(SharedLayout *layout);
0136 
0137     bool layoutIsAssigned(QString layoutName);
0138 
0139     QString layoutPath(QString layoutName);
0140 
0141     QStringList validActivities(QStringList currentList);
0142 
0143 private:
0144     bool m_multipleModeInitialized{false};
0145 
0146     QString m_currentLayoutNameInMultiEnvironment;
0147     QString m_shouldSwitchToLayout;
0148 
0149     QStringList m_layouts;
0150     QStringList m_menuLayouts;
0151     QStringList m_sharedLayoutIds;
0152 
0153     QHash<const QString, QString> m_assignedLayouts;
0154 
0155     QTimer m_dynamicSwitchTimer;
0156 
0157     QList<CentralLayout *> m_centralLayouts;
0158     QList<SharedLayout *> m_sharedLayouts;
0159 
0160     Layouts::Manager *m_manager;
0161     KActivities::Controller *m_activitiesController;
0162 };
0163 
0164 }
0165 }
0166 
0167 
0168 #endif