1 /*
2     SPDX-FileCopyrightText: 2020 Michail Vourlakos <mvourlakos@gmail.com>
3     SPDX-License-Identifier: GPL-2.0-or-later
4 */
5 
6 #ifndef LAYOUTSSTORAGE_H
7 #define LAYOUTSSTORAGE_H
8 
9 // local
10 #include "../data/appletdata.h"
11 #include "../data/errordata.h"
12 #include "../data/genericdata.h"
13 #include "../data/generictable.h"
14 #include "../data/viewstable.h"
15 
16 // Qt
17 #include <QTemporaryDir>
18 
19 // KDE
20 #include <KConfigGroup>
21 
22 // Plasma
23 #include <Plasma/Applet>
24 #include <Plasma/Containment>
25 
26 namespace Latte {
27 namespace Layout {
28 class GenericLayout;
29 }
30 }
31 
32 namespace Latte {
33 namespace Layouts {
34 
35 struct SubContaimentIdentityData
36 {
37     QString cfgGroup;
38     QString cfgProperty;
39 };
40 
41 class Storage
42 {
43 
44 public:
45     static Storage *self();
46     ~Storage();
47 
48     static const int IDNULL;
49     static const int IDBASE;
50 
51     bool isWritable(const Layout::GenericLayout *layout) const;
52     bool isLatteContainment(const Plasma::Containment *containment) const;
53     bool isLatteContainment(const KConfigGroup &group) const;
54     bool isSubContainment(const Layout::GenericLayout *layout, const Plasma::Applet *applet) const;
55 
56     bool hasContainment(const Layout::GenericLayout *layout, const int &id);
57     bool containsView(const QString &filepath, const int &viewId);
58 
59     int subContainmentId(const KConfigGroup &appletGroup) const;
60 
61     Plasma::Containment *subContainmentOf(const Layout::GenericLayout *layout, const Plasma::Applet *applet);
62 
63     void lock(const Layout::GenericLayout *layout); //! make it only read-only
64     void unlock(const Layout::GenericLayout *layout); //! make it writable which it should be the default
65 
66     void importToCorona(const Layout::GenericLayout *layout);
67     void syncToLayoutFile(const Layout::GenericLayout *layout, bool removeLayoutId);
68 
69     Data::View newView(const Layout::GenericLayout *destination, const Data::View &nextViewData);
70     void removeView(const QString &filepath, const Data::View &viewData);
71     void updateView(const Layout::GenericLayout *layout, const Data::View &viewData);
72     void updateView(KConfigGroup viewGroup, const Data::View &viewData);
73     QString storedView(const Layout::GenericLayout *layout, const int &containmentId); //returns temp filepath containing all view data
74 
75     void removeContainment(const QString &filepath, const QString &containmentId);
76 
77     bool exportTemplate(const QString &originFile, const QString &destinationFile, const Data::AppletsTable &approvedApplets);
78     bool exportTemplate(const Layout::GenericLayout *layout, Plasma::Containment *containment, const QString &destinationFile, const Data::AppletsTable &approvedApplets);
79 
80     /// STATIC
81     //! Check if an applet config group is valid or belongs to removed applet
82     static bool appletGroupIsValid(const KConfigGroup &appletGroup);
83     static bool isValid(const int &id);
84 
85 
86     //! AppletsData Information
87     Data::Applet metadata(const QString &pluginId);
88     Data::AppletsTable plugins(const Layout::GenericLayout *layout, const int containmentid = IDNULL);
89     Data::AppletsTable plugins(const QString &layoutfile, const int containmentid = IDNULL);
90 
91     Data::GenericTable<Data::Generic> subcontainments(const KConfigGroup &containmentGroup);
92     Data::GenericTable<Data::Generic> subcontainments(const Layout::GenericLayout *layout, const Plasma::Containment *lattecontainment) const;
93     Data::View view(const KConfigGroup &containmentGroup);
94     Data::View view(const Layout::GenericLayout *layout, const Plasma::Containment *lattecontainment);
95     Data::ViewsTable views(const QString &file);
96     Data::ViewsTable views(const Layout::GenericLayout *layout);
97 
98     //! errors/warning;
99     Data::ErrorsList errors(const Layout::GenericLayout *layout);
100     Data::WarningsList warnings(const Layout::GenericLayout *layout);
101 
102 private:
103     Storage();
104 
105     void clearExportedLayoutSettings(KConfigGroup &layoutSettingsGroup);
106     void importContainments(const QString &originFile, const QString &destinationFile);
107     void syncContainmentConfig(Plasma::Containment *containment);
108 
109     bool isSubContainment(const KConfigGroup &appletGroup) const;
110     int subIdentityIndex(const KConfigGroup &appletGroup) const;
111 
112     //! STORAGE !////
113     QString availableId(QStringList all, QStringList assigned, int base);
114     //! provides a new file path based the provided file. The new file
115     //! has updated ids for containments and applets based on the corona
116     //! loaded ones
117     QString newUniqueIdsFile(QString originFile, const Layout::GenericLayout *destinationLayout);
118     //! imports a layout file and returns the containments for the docks
119     QList<Plasma::Containment *> importLayoutFile(const Layout::GenericLayout *layout, QString file);
120 
121     QStringList containmentsIds(const QString &filepath);
122     QStringList appletsIds(const QString &filepath);
123 
124     //! errors checkers
125     bool hasDifferentAppletsWithSameId(const Layout::GenericLayout *layout, Data::Error &error);
126     bool hasOrphanedParentAppletOfSubContainment(const Layout::GenericLayout *layout, Data::Error &error);
127     //! warnings checkers
128     bool hasAppletsAndContainmentsWithSameId(const Layout::GenericLayout *layout, Data::Warning &warning);
129     bool hasOrphanedSubContainments(const Layout::GenericLayout *layout, Data::Warning &warning);
130 private:
131     QTemporaryDir m_storageTmpDir;
132 
133     Data::GenericTable<Data::Generic> s_knownErrors;
134 
135     QList<SubContaimentIdentityData> m_subIdentities;
136 };
137 
138 }
139 }
140 
141 #endif
142