From d97fe4c59c425b0573fe392ddd78323d972b1522 Mon Sep 17 00:00:00 2001 From: Michail Vourlakos Date: Tue, 18 Aug 2020 01:23:41 +0300 Subject: [PATCH] provide a new Layouts::Storage singleton --this class will be the replacement for layout::storage and will be responsible for any containment actions for all layouts either loaded or offloaded. Through this refactor loading and checking CentralLayouts should become lighter and also memory efficient --- app/layout/genericlayout.cpp | 26 +++++---- app/layout/storage.cpp | 62 +++----------------- app/layout/storage.h | 5 -- app/layouts/CMakeLists.txt | 1 + app/layouts/storage.cpp | 107 +++++++++++++++++++++++++++++++++++ app/layouts/storage.h | 66 +++++++++++++++++++++ 6 files changed, 196 insertions(+), 71 deletions(-) create mode 100644 app/layouts/storage.cpp create mode 100644 app/layouts/storage.h diff --git a/app/layout/genericlayout.cpp b/app/layout/genericlayout.cpp index 0453263aa..704a47ba0 100644 --- a/app/layout/genericlayout.cpp +++ b/app/layout/genericlayout.cpp @@ -27,6 +27,7 @@ #include "../screenpool.h" #include "../layouts/importer.h" #include "../layouts/manager.h" +#include "../layouts/storage.h" #include "../layouts/synchronizer.h" #include "../shortcuts/shortcutstracker.h" #include "../view/view.h" @@ -797,8 +798,9 @@ void GenericLayout::addView(Plasma::Containment *containment, bool forceOnPrimar qDebug() << "step 1..."; - if (!m_storage->isLatteContainment(containment)) + if (!Layouts::Storage::self()->isLatteContainment(containment)) { return; + } qDebug() << "step 2..."; @@ -1164,7 +1166,7 @@ bool GenericLayout::explicitDockOccupyEdge(int screen, Plasma::Types::Location l } for (const auto containment : m_containments) { - if (m_storage->isLatteContainment(containment)) { + if (Layouts::Storage::self()->isLatteContainment(containment)) { bool onPrimary = containment->config().readEntry("onPrimary", true); int id = containment->lastScreen(); Plasma::Types::Location contLocation = containment->location(); @@ -1185,7 +1187,7 @@ bool GenericLayout::primaryDockOccupyEdge(Plasma::Types::Location location) cons } for (const auto containment : m_containments) { - if (m_storage->isLatteContainment(containment)) { + if (Layouts::Storage::self()->isLatteContainment(containment)) { bool onPrimary{false}; if (m_latteViews.contains(containment)) { @@ -1238,7 +1240,7 @@ Layout::ViewsMap GenericLayout::validViewsMap(Layout::ViewsMap *occupiedMap) //! first step: primary docks must be placed in primary screen free edges for (const auto containment : m_containments) { - if (m_storage->isLatteContainment(containment)) { + if (Layouts::Storage::self()->isLatteContainment(containment)) { int screenId = 0; //! valid screen id @@ -1274,7 +1276,7 @@ Layout::ViewsMap GenericLayout::validViewsMap(Layout::ViewsMap *occupiedMap) //! second step: explicit docks must be placed in their screens if the screen edge is free for (const auto containment : m_containments) { - if (m_storage->isLatteContainment(containment)) { + if (Layouts::Storage::self()->isLatteContainment(containment)) { int screenId = 0; //! valid screen id @@ -1392,7 +1394,7 @@ QList GenericLayout::containmentSystrays(Plasma::Containment *containment) { QList trays; - if (m_storage->isLatteContainment(containment)) { + if (Layouts::Storage::self()->isLatteContainment(containment)) { auto applets = containment->config().group("Applets"); for (const auto &applet : applets.groupList()) { @@ -1469,7 +1471,7 @@ QString GenericLayout::reportHtml(const ScreenPool *screenPool) //! orphan systrays for (const auto containment : m_containments) { - if (!m_storage->isLatteContainment(containment) && !assignedSystrays.contains(containment->id())) { + if (!Layouts::Storage::self()->isLatteContainment(containment) && !assignedSystrays.contains(containment->id())) { orphanSystrays << containment->id(); } } @@ -1501,7 +1503,7 @@ QString GenericLayout::reportHtml(const ScreenPool *screenPool) if (isActive()) { //! collect viewData results for (const auto containment : m_containments) { - if (m_storage->isLatteContainment(containment)) { + if (Layouts::Storage::self()->isLatteContainment(containment)) { ViewData vData; vData.id = containment->id(); vData.active = latteViewExists(containment); @@ -1628,7 +1630,7 @@ QList GenericLayout::viewsScreens() if (isActive()) { for (const auto containment : m_containments) { - if (m_storage->isLatteContainment(containment)) { + if (Layouts::Storage::self()->isLatteContainment(containment)) { int screenId = -1; //! valid screen id @@ -1659,17 +1661,17 @@ QList GenericLayout::viewsScreens() bool GenericLayout::isWritable() const { - return m_storage->isWritable(); + return Layouts::Storage::self()->isWritable(this); } void GenericLayout::lock() { - m_storage->lock(); + Layouts::Storage::self()->lock(this); } void GenericLayout::unlock() { - m_storage->unlock(); + Layouts::Storage::self()->unlock(this); } void GenericLayout::syncToLayoutFile(bool removeLayoutId) diff --git a/app/layout/storage.cpp b/app/layout/storage.cpp index 855665c2a..7fef08937 100644 --- a/app/layout/storage.cpp +++ b/app/layout/storage.cpp @@ -25,7 +25,9 @@ #include "../screenpool.h" #include "../layouts/manager.h" #include "../layouts/importer.h" +#include "../layouts/storage.h" #include "../view/view.h" + // Qt #include #include @@ -53,54 +55,6 @@ Storage::~Storage() { } -bool Storage::isWritable() const -{ - QFileInfo layoutFileInfo(m_layout->file()); - - if (layoutFileInfo.exists() && !layoutFileInfo.isWritable()) { - return false; - } else { - return true; - } -} - -bool Storage::isLatteContainment(Plasma::Containment *containment) const -{ - if (!containment) { - return false; - } - - if (containment->pluginMetaData().pluginId() == "org.kde.latte.containment") { - return true; - } - - return false; -} - -bool Storage::isLatteContainment(const KConfigGroup &group) const -{ - QString pluginId = group.readEntry("plugin", ""); - return pluginId == "org.kde.latte.containment"; -} - -void Storage::lock() -{ - QFileInfo layoutFileInfo(m_layout->file()); - - if (layoutFileInfo.exists() && layoutFileInfo.isWritable()) { - QFile(m_layout->file()).setPermissions(QFileDevice::ReadUser | QFileDevice::ReadGroup | QFileDevice::ReadOther); - } -} - -void Storage::unlock() -{ - QFileInfo layoutFileInfo(m_layout->file()); - - if (layoutFileInfo.exists() && !layoutFileInfo.isWritable()) { - QFile(m_layout->file()).setPermissions(QFileDevice::ReadUser | QFileDevice::WriteUser | QFileDevice::ReadGroup | QFileDevice::ReadOther); - } -} - void Storage::setStorageTmpDir(const QString &tmpDir) { m_storageTmpDir = tmpDir; @@ -155,7 +109,7 @@ void Storage::importToCorona() void Storage::syncToLayoutFile(bool removeLayoutId) { - if (!m_layout->corona() || !isWritable()) { + if (!m_layout->corona() || !Layouts::Storage::self()->isWritable(m_layout)) { return; } @@ -341,7 +295,7 @@ QList Storage::importLayoutFile(QString file) //QList systrays; for (const auto containment : newContainments) { - if (isLatteContainment(containment)) { + if (Layouts::Storage::self()->isLatteContainment(containment)) { qDebug() << "new latte containment id: " << containment->id(); importedDocks << containment; } @@ -361,7 +315,7 @@ void Storage::systraysInformation(QHash> &systrays, QList & //! assigned systrays for (const auto &cId : containmentGroups.groupList()) { - if (isLatteContainment(containmentGroups.group(cId))) { + if (Layouts::Storage::self()->isLatteContainment(containmentGroups.group(cId))) { auto applets = containmentGroups.group(cId).group("Applets"); for (const auto &applet : applets.groupList()) { @@ -378,7 +332,7 @@ void Storage::systraysInformation(QHash> &systrays, QList & //! orphan systrays for (const auto &cId : containmentGroups.groupList()) { - if (!isLatteContainment(containmentGroups.group(cId)) && !assignedSystrays.contains(cId.toInt())) { + if (!Layouts::Storage::self()->isLatteContainment(containmentGroups.group(cId)) && !assignedSystrays.contains(cId.toInt())) { orphanSystrays << cId.toInt(); } } @@ -392,7 +346,7 @@ QList Storage::viewsData(const QHash> &systrays) KConfigGroup containmentGroups = KConfigGroup(lFile, "Containments"); for (const auto &cId : containmentGroups.groupList()) { - if (isLatteContainment(containmentGroups.group(cId))) { + if (Layouts::Storage::self()->isLatteContainment(containmentGroups.group(cId))) { ViewData vData; int id = cId.toInt(); @@ -430,7 +384,7 @@ QList Storage::viewsScreens() KConfigGroup containmentGroups = KConfigGroup(lFile, "Containments"); for (const auto &cId : containmentGroups.groupList()) { - if (isLatteContainment(containmentGroups.group(cId))) { + if (Layouts::Storage::self()->isLatteContainment(containmentGroups.group(cId))) { int screenId = containmentGroups.group(cId).readEntry("lastScreen", -1); if (screenId != -1 && !screens.contains(screenId)) { diff --git a/app/layout/storage.h b/app/layout/storage.h index 27668137a..bae753b3d 100644 --- a/app/layout/storage.h +++ b/app/layout/storage.h @@ -40,14 +40,9 @@ public: Storage(GenericLayout *parent); ~Storage() override; - bool isWritable() const; - bool isLatteContainment(Plasma::Containment *containment) const; - bool isLatteContainment(const KConfigGroup &group) const; bool layoutIsBroken(QStringList &errors) const; void importToCorona(); - void lock(); //! make it only read-only - void unlock(); //! make it writable which it should be the default void copyView(Plasma::Containment *containment); void syncToLayoutFile(bool removeLayoutId); diff --git a/app/layouts/CMakeLists.txt b/app/layouts/CMakeLists.txt index d31a6d032..17bba951a 100644 --- a/app/layouts/CMakeLists.txt +++ b/app/layouts/CMakeLists.txt @@ -3,6 +3,7 @@ set(lattedock-app_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/importer.cpp ${CMAKE_CURRENT_SOURCE_DIR}/launcherssignals.cpp ${CMAKE_CURRENT_SOURCE_DIR}/manager.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/storage.cpp ${CMAKE_CURRENT_SOURCE_DIR}/synchronizer.cpp PARENT_SCOPE ) diff --git a/app/layouts/storage.cpp b/app/layouts/storage.cpp new file mode 100644 index 000000000..a9c5aec8b --- /dev/null +++ b/app/layouts/storage.cpp @@ -0,0 +1,107 @@ +/* +* Copyright 2020 Michail Vourlakos +* +* This file is part of Latte-Dock +* +* Latte-Dock is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation; either version 2 of +* the License, or (at your option) any later version. +* +* Latte-Dock is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +*/ + +#include "storage.h" + +// local +#include "../layout/storage.h" + +// Qt +#include +#include +#include +#include + +// KDE +#include +#include + +// Plasma +#include +#include +#include + +namespace Latte { +namespace Layouts { + +Storage::Storage() +{ + qDebug() << " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LAYOUTS::STORAGE, TEMP DIR ::: " << m_storageTmpDir.path(); +} + +Storage::~Storage() +{ +} + +Storage *Storage::self() +{ + static Storage store; + return &store; +} + +bool Storage::isWritable(const Layout::GenericLayout *layout) const +{ + QFileInfo layoutFileInfo(layout->file()); + + if (layoutFileInfo.exists() && !layoutFileInfo.isWritable()) { + return false; + } else { + return true; + } +} + +bool Storage::isLatteContainment(Plasma::Containment *containment) const +{ + if (!containment) { + return false; + } + + if (containment->pluginMetaData().pluginId() == "org.kde.latte.containment") { + return true; + } + + return false; +} + +bool Storage::isLatteContainment(const KConfigGroup &group) const +{ + QString pluginId = group.readEntry("plugin", ""); + return pluginId == "org.kde.latte.containment"; +} + +void Storage::lock(Layout::GenericLayout *layout) const +{ + QFileInfo layoutFileInfo(layout->file()); + + if (layoutFileInfo.exists() && layoutFileInfo.isWritable()) { + QFile(layout->file()).setPermissions(QFileDevice::ReadUser | QFileDevice::ReadGroup | QFileDevice::ReadOther); + } +} + +void Storage::unlock(Layout::GenericLayout *layout) const +{ + QFileInfo layoutFileInfo(layout->file()); + + if (layoutFileInfo.exists() && !layoutFileInfo.isWritable()) { + QFile(layout->file()).setPermissions(QFileDevice::ReadUser | QFileDevice::WriteUser | QFileDevice::ReadGroup | QFileDevice::ReadOther); + } +} + +} +} diff --git a/app/layouts/storage.h b/app/layouts/storage.h new file mode 100644 index 000000000..31c0c9b05 --- /dev/null +++ b/app/layouts/storage.h @@ -0,0 +1,66 @@ +/* +* Copyright 2020 Michail Vourlakos +* +* This file is part of Latte-Dock +* +* Latte-Dock is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation; either version 2 of +* the License, or (at your option) any later version. +* +* Latte-Dock is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +*/ + +#ifndef LAYOUTSSTORAGE_H +#define LAYOUTSSTORAGE_H + +// Qt +#include + +// KDE +#include + +// Plasma +#include + +namespace Latte { +namespace Layout { +class GenericLayout; +} +} + +namespace Latte { +namespace Layouts { + +class Storage +{ + +public: + static Storage *self(); + ~Storage(); + + bool isWritable(const Layout::GenericLayout *layout) const; + bool isLatteContainment(Plasma::Containment *containment) const; + bool isLatteContainment(const KConfigGroup &group) const; + + void lock(Layout::GenericLayout *layout) const; //! make it only read-only + void unlock(Layout::GenericLayout *layout) const; //! make it writable which it should be the default + +private: + Storage(); + +private: + QTemporaryDir m_storageTmpDir; + +}; + +} +} + +#endif