File indexing completed on 2024-10-13 04:54:38

0001 /*
0002     SPDX-FileCopyrightText: 2020 Michail Vourlakos <mvourlakos@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #ifndef LAYOUTSSTORAGE_H
0007 #define LAYOUTSSTORAGE_H
0008 
0009 // local
0010 #include "../data/appletdata.h"
0011 #include "../data/errordata.h"
0012 #include "../data/genericdata.h"
0013 #include "../data/generictable.h"
0014 #include "../data/viewstable.h"
0015 
0016 // Qt
0017 #include <QTemporaryDir>
0018 
0019 // KDE
0020 #include <KConfigGroup>
0021 
0022 // Plasma
0023 #include <Plasma/Applet>
0024 #include <Plasma/Containment>
0025 
0026 namespace Latte {
0027 class Corona;
0028 namespace Layout {
0029 class GenericLayout;
0030 }
0031 }
0032 
0033 namespace Latte {
0034 namespace Layouts {
0035 
0036 struct SubContaimentIdentityData
0037 {
0038     QString cfgGroup;
0039     QString cfgProperty;
0040 };
0041 
0042 class Storage
0043 {
0044 
0045 public:
0046     static Storage *self();
0047     ~Storage();
0048 
0049     static const int IDNULL;
0050     static const int IDBASE;
0051 
0052     bool isWritable(const Layout::GenericLayout *layout) const;
0053     bool isLatteContainment(const Plasma::Containment *containment) const;
0054     bool isLatteContainment(const KConfigGroup &group) const;
0055     bool isSubContainment(const Plasma::Corona *corona, const Plasma::Applet *applet) const;
0056 
0057     bool hasContainment(const Layout::GenericLayout *layout, const int &id);
0058     bool containsView(const QString &filepath, const int &viewId);
0059 
0060     bool isClonedView(const Plasma::Containment *containment) const;
0061     bool isClonedView(const KConfigGroup &containmentGroup) const;
0062     void removeAllClonedViews(const QString &filepath);
0063 
0064     int subContainmentId(const KConfigGroup &appletGroup) const;
0065 
0066     Plasma::Containment *subContainmentOf(const Plasma::Corona *corona, const Plasma::Applet *applet);
0067 
0068     void lock(const Layout::GenericLayout *layout); //! make it only read-only
0069     void unlock(const Layout::GenericLayout *layout); //! make it writable which it should be the default
0070 
0071     void importToCorona(const Layout::GenericLayout *layout);
0072     void syncToLayoutFile(const Layout::GenericLayout *layout, bool removeLayoutId);
0073 
0074     Data::View newView(const Layout::GenericLayout *destination, const Data::View &nextViewData);
0075     void removeView(const QString &filepath, const Data::View &viewData);
0076     void updateView(const Layout::GenericLayout *layout, const Data::View &viewData);
0077     void updateView(KConfigGroup viewGroup, const Data::View &viewData);
0078     QString storedView(const Layout::GenericLayout *layout, const int &containmentId); //returns temp filepath containing all view data
0079 
0080     void moveToLayoutFile(const QString &layoutName);
0081     QStringList storedLayoutsInMultipleFile();
0082 
0083     void removeContainment(const QString &filepath, const QString &containmentId);
0084 
0085     bool exportTemplate(const QString &originFile, const QString &destinationFile, const Data::AppletsTable &approvedApplets);
0086     bool exportTemplate(const Layout::GenericLayout *layout, Plasma::Containment *containment, const QString &destinationFile, const Data::AppletsTable &approvedApplets);
0087 
0088     int expectedViewScreenId(const Latte::Corona *corona, const Data::View &view) const;
0089     int expectedViewScreenId(const Latte::Corona *corona, const KConfigGroup &containmentGroup) const;
0090     int expectedViewScreenId(const Layout::GenericLayout *layout, const Plasma::Containment *lattecontainment) const;
0091 
0092     /// STATIC
0093     //! Check if an applet config group is valid or belongs to removed applet
0094     static bool appletGroupIsValid(const KConfigGroup &appletGroup);
0095     static bool isValid(const int &id);
0096 
0097     //! AppletsData Information
0098     Data::Applet metadata(const QString &pluginId);
0099     Data::AppletsTable plugins(const Layout::GenericLayout *layout, const int containmentid = IDNULL);
0100     Data::AppletsTable plugins(const QString &layoutfile, const int containmentid = IDNULL);
0101 
0102     Data::GenericTable<Data::Generic> subcontainments(const KConfigGroup &containmentGroup);
0103     Data::GenericTable<Data::Generic> subcontainments(const Layout::GenericLayout *layout, const Plasma::Containment *lattecontainment) const;
0104     Data::View view(const KConfigGroup &containmentGroup);
0105     Data::View view(const Layout::GenericLayout *layout, const Plasma::Containment *lattecontainment);
0106     Data::ViewsTable views(const QString &file);
0107     Data::ViewsTable views(const Layout::GenericLayout *layout);
0108 
0109     //! errors/warning;
0110     Data::ErrorsList errors(const Layout::GenericLayout *layout);
0111     Data::WarningsList warnings(const Layout::GenericLayout *layout);
0112 
0113 private:
0114     Storage();
0115 
0116     void clearExportedLayoutSettings(KConfigGroup &layoutSettingsGroup);
0117     void importContainments(const QString &originFile, const QString &destinationFile);
0118     void syncContainmentConfig(Plasma::Containment *containment);
0119 
0120     bool isSubContainment(const KConfigGroup &appletGroup) const;
0121     int subIdentityIndex(const KConfigGroup &appletGroup) const;
0122 
0123     //! STORAGE !////
0124     QString availableId(QStringList all, QStringList assigned, int base);
0125     //! provides a new file path based the provided file. The new file
0126     //! has updated ids for containments and applets based on the corona
0127     //! loaded ones
0128     QString newUniqueIdsFile(QString originFile, const Layout::GenericLayout *destinationLayout);
0129     //! imports a layout file and returns the containments for the docks
0130     QList<Plasma::Containment *> importLayoutFile(const Layout::GenericLayout *layout, QString file);
0131 
0132     QStringList containmentsIds(const QString &filepath);
0133     QStringList appletsIds(const QString &filepath);
0134 
0135     //! errors checkers
0136     bool hasDifferentAppletsWithSameId(const Layout::GenericLayout *layout, Data::Error &error);
0137     bool hasOrphanedParentAppletOfSubContainment(const Layout::GenericLayout *layout, Data::Error &error);
0138     //! warnings checkers
0139     bool hasAppletsAndContainmentsWithSameId(const Layout::GenericLayout *layout, Data::Warning &warning);
0140     bool hasOrphanedSubContainments(const Layout::GenericLayout *layout, Data::Warning &warning);
0141 private:
0142     QTemporaryDir m_storageTmpDir;
0143 
0144     Data::GenericTable<Data::Generic> s_knownErrors;
0145 
0146     QList<SubContaimentIdentityData> m_subIdentities;
0147 };
0148 
0149 }
0150 }
0151 
0152 #endif