From e88aaf27a9d42cf3eac5a64266a6511bb355fd1a Mon Sep 17 00:00:00 2001 From: Michail Vourlakos Date: Mon, 6 Dec 2021 19:26:34 +0200 Subject: [PATCH] cmd:support "add-dock" through command line -- "add-dock" option now works even when application is not running and the user is executing it through command line. BUG: 446526 FIXED-IN: 0.10.5 --- app/lattecorona.cpp | 12 ++++- app/lattecorona.h | 2 + app/layouts/importer.cpp | 76 ++++++++++++++++++++++++++++++ app/layouts/importer.h | 7 +++ app/layouts/synchronizer.cpp | 4 ++ app/layouts/synchronizer.h | 1 + app/main.cpp | 29 +++++++++++- app/templates/templatesmanager.cpp | 18 +++++++ app/templates/templatesmanager.h | 2 + 9 files changed, 149 insertions(+), 2 deletions(-) diff --git a/app/lattecorona.cpp b/app/lattecorona.cpp index 319f0e795..5282d2f9d 100644 --- a/app/lattecorona.cpp +++ b/app/lattecorona.cpp @@ -81,9 +81,10 @@ namespace Latte { -Corona::Corona(bool defaultLayoutOnStartup, QString layoutNameOnStartUp, int userSetMemoryUsage, QObject *parent) +Corona::Corona(bool defaultLayoutOnStartup, QString layoutNameOnStartUp, QString addViewTemplateName, int userSetMemoryUsage, QObject *parent) : Plasma::Corona(parent), m_defaultLayoutOnStartup(defaultLayoutOnStartup), + m_startupAddViewTemplateName(addViewTemplateName), m_userSetMemoryUsage(userSetMemoryUsage), m_layoutNameOnStartUp(layoutNameOnStartUp), m_activitiesConsumer(new KActivities::Consumer(this)), @@ -267,6 +268,15 @@ void Corona::load() addOutput(screen); } + connect(m_layoutsManager->synchronizer(), &Layouts::Synchronizer::initializationFinished, [this]() { + if (!m_startupAddViewTemplateName.isEmpty()) { + //! user requested through cmd startup to add view from specific view template and we can add it after the startup + //! sequence has loaded all required layouts properly + addView(0, m_startupAddViewTemplateName); + m_startupAddViewTemplateName = ""; + } + }); + m_inStartup = false; connect(qGuiApp, &QGuiApplication::screenAdded, this, &Corona::addOutput, Qt::UniqueConnection); diff --git a/app/lattecorona.h b/app/lattecorona.h index 643d12b30..a5ab7bbe4 100644 --- a/app/lattecorona.h +++ b/app/lattecorona.h @@ -87,6 +87,7 @@ class Corona : public Plasma::Corona public: Corona(bool defaultLayoutOnStartup = false, QString layoutNameOnStartUp = QString(), + QString addViewTemplateName = QString(), int userSetMemoryUsage = -1, QObject *parent = nullptr); virtual ~Corona(); @@ -222,6 +223,7 @@ private: int m_userSetMemoryUsage{ -1}; QString m_layoutNameOnStartUp; + QString m_startupAddViewTemplateName; QString m_importFullConfigurationFile; QList m_alternativesObjects; diff --git a/app/layouts/importer.cpp b/app/layouts/importer.cpp index 72c95d28c..ae91cd2c9 100644 --- a/app/layouts/importer.cpp +++ b/app/layouts/importer.cpp @@ -14,6 +14,7 @@ #include "../screenpool.h" #include "../layout/abstractlayout.h" #include "../settings/universalsettings.h" +#include "../templates/templatesmanager.h" #include "../tools/commontools.h" // Qt @@ -583,6 +584,11 @@ void Importer::disableAutostart() } } +bool Importer::hasViewTemplate(const QString &name) +{ + return availableViewTemplates().contains(name); +} + QString Importer::importLayout(const QString &fileName, const QString &suggestedLayoutName) { QString newLayoutName = importLayoutHelper(fileName, suggestedLayoutName); @@ -606,6 +612,13 @@ QString Importer::importLayoutHelper(const QString &fileName, const QString &sug newLayoutName = uniqueLayoutName(newLayoutName); QString newPath = layoutUserFilePath(newLayoutName); + + QDir localLayoutsDir(layoutUserDir()); + + if (!localLayoutsDir.exists()) { + QDir(Latte::configPath()).mkdir("latte"); + } + QFile(fileName).copy(newPath); QFileInfo newFileInfo(newPath); @@ -633,6 +646,57 @@ QStringList Importer::availableLayouts() return layoutNames; } +QStringList Importer::availableViewTemplates() +{ + QStringList templates; + + QDir localDir(layoutUserDir() + "/templates"); + QStringList filter; + filter.append(QString("*.view.latte")); + QStringList files = localDir.entryList(filter, QDir::Files | QDir::NoSymLinks); + + for(const auto &file : files) { + templates.append(Templates::Manager::templateName(file)); + } + + QDir systemDir(systemShellDataPath()+"/contents/templates"); + QStringList sfiles = systemDir.entryList(filter, QDir::Files | QDir::NoSymLinks); + + for(const auto &file : sfiles) { + QString name = Templates::Manager::templateName(file); + if (!templates.contains(name)) { + templates.append(name); + } + } + + return templates; +} + +QStringList Importer::availableLayoutTemplates() +{ + QStringList templates; + + QDir localDir(layoutUserDir() + "/templates"); + QStringList filter; + filter.append(QString("*.layout.latte")); + QStringList files = localDir.entryList(filter, QDir::Files | QDir::NoSymLinks); + + for(const auto &file : files) { + templates.append(Templates::Manager::templateName(file)); + } + + QDir systemDir(systemShellDataPath()+"/contents/templates"); + QStringList sfiles = systemDir.entryList(filter, QDir::Files | QDir::NoSymLinks); + + for(const auto &file : sfiles) { + QString name = Templates::Manager::templateName(file); + if (!templates.contains(name)) { + templates.append(name); + } + } + + return templates; +} QString Importer::nameOfConfigFile(const QString &fileName) { @@ -662,6 +726,18 @@ QString Importer::layoutUserFilePath(QString layoutName) return QString(layoutUserDir() + "/" + layoutName + ".layout.latte"); } +QString Importer::systemShellDataPath() +{ + QStringList paths = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation); + QString rootpath = paths.count() > 0 ? paths[paths.count()-1] : "/usr/share"; + return rootpath + "/plasma/shells/org.kde.latte.shell"; +} + +QString Importer::layoutTemplateSystemFilePath(const QString &name) +{ + return systemShellDataPath() + "/contents/templates/" + name + ".layout.latte"; +} + QString Importer::uniqueLayoutName(QString name) { int pos_ = name.lastIndexOf(QRegExp(QString(" - [0-9]+"))); diff --git a/app/layouts/importer.h b/app/layouts/importer.h index c79b5d73c..275e4e272 100644 --- a/app/layouts/importer.h +++ b/app/layouts/importer.h @@ -89,11 +89,18 @@ public: static QString layoutUserFilePath(QString layoutName); //! returns the layouts user directory static QString layoutUserDir(); + //! returns the system path for latte shell data + static QString systemShellDataPath(); static QString nameOfConfigFile(const QString &fileName); static QString uniqueLayoutName(QString name); + static bool hasViewTemplate(const QString &name); + static QString layoutTemplateSystemFilePath(const QString &name); + static QStringList availableLayouts(); + static QStringList availableLayoutTemplates(); + static QStringList availableViewTemplates(); //! it checks the linked file if there are Containments in it that belong //! to Original Layouts and moves them accordingly. This is used mainly on //! startup and if such state occurs, it basically means that the app didn't diff --git a/app/layouts/synchronizer.cpp b/app/layouts/synchronizer.cpp index 1fdcb563e..0eb67621e 100644 --- a/app/layouts/synchronizer.cpp +++ b/app/layouts/synchronizer.cpp @@ -653,6 +653,8 @@ bool Synchronizer::initSingleMode(QString layoutName) } m_manager->corona()->universalSettings()->setSingleModeLayoutName(layoutName); + + emit initializationFinished(); }); return true; @@ -685,6 +687,8 @@ bool Synchronizer::initMultipleMode(QString layoutName) } syncMultipleLayoutsToActivities(); + + emit initializationFinished(); }); return true; diff --git a/app/layouts/synchronizer.h b/app/layouts/synchronizer.h index 3c000d342..c832ec69d 100644 --- a/app/layouts/synchronizer.h +++ b/app/layouts/synchronizer.h @@ -119,6 +119,7 @@ signals: void centralLayoutsChanged(); void layoutsChanged(); void runningActicitiesChanged(); + void initializationFinished(); void currentLayoutIsSwitching(QString layoutName); diff --git a/app/main.cpp b/app/main.cpp index 732077709..abdc06874 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -10,6 +10,7 @@ #include "apptypes.h" #include "lattecorona.h" #include "layouts/importer.h" +#include "templates/templatesmanager.h" // C++ #include @@ -214,6 +215,7 @@ int main(int argc, char **argv) bool defaultLayoutOnStartup = false; int memoryUsage = -1; QString layoutNameOnStartup = ""; + QString addViewTemplateNameOnStartup = ""; //! --default-layout option if (parser.isSet(QStringLiteral("default-layout"))) { @@ -326,6 +328,31 @@ int main(int argc, char **argv) memoryUsage = (int)(Latte::MemoryUsage::SingleLayout); } + //! add-dock usage option + if (parser.isSet(QStringLiteral("add-dock"))) { + QString viewTemplateName = parser.value(QStringLiteral("add-dock")); + QStringList viewTemplates = Latte::Layouts::Importer::availableViewTemplates(); + + if (viewTemplates.contains(viewTemplateName)) { + if (layoutNameOnStartup.isEmpty()) { + //! Clean layout template is applied and proper name is used + QString emptytemplatepath = Latte::Layouts::Importer::layoutTemplateSystemFilePath(Latte::Templates::EMPTYLAYOUTTEMPLATENAME); + QString suggestedname = parser.isSet(QStringLiteral("suggested-layout-name")) ? parser.value(QStringLiteral("suggested-layout-name")) : viewTemplateName; + QString importedLayout = Latte::Layouts::Importer::importLayoutHelper(emptytemplatepath, suggestedname); + + if (importedLayout.isEmpty()) { + qInfo() << i18n("The layout cannot be imported"); + qGuiApp->exit(); + return 0; + } else { + layoutNameOnStartup = importedLayout; + } + } + + addViewTemplateNameOnStartup = viewTemplateName; + } + } + //! text filter for debug messages if (parser.isSet(QStringLiteral("debug-text"))) { filterDebugMessageText = parser.value(QStringLiteral("debug-text")); @@ -350,7 +377,7 @@ int main(int argc, char **argv) KCrash::setDrKonqiEnabled(true); KCrash::setFlags(KCrash::AutoRestart | KCrash::AlwaysDirectly); - Latte::Corona corona(defaultLayoutOnStartup, layoutNameOnStartup, memoryUsage); + Latte::Corona corona(defaultLayoutOnStartup, layoutNameOnStartup, addViewTemplateNameOnStartup, memoryUsage); KDBusService service(KDBusService::Unique); return app.exec(); diff --git a/app/templates/templatesmanager.cpp b/app/templates/templatesmanager.cpp index f12688f6e..77f5efd9a 100644 --- a/app/templates/templatesmanager.cpp +++ b/app/templates/templatesmanager.cpp @@ -311,6 +311,24 @@ QString Manager::uniqueViewTemplateName(QString name) const return name; } +QString Manager::templateName(const QString &filePath) +{ + int lastSlash = filePath.lastIndexOf("/"); + QString tempFilePath = filePath; + QString templatename = tempFilePath.remove(0, lastSlash + 1); + + QString extension(".layout.latte"); + int ext = templatename.lastIndexOf(extension); + if (ext>0) { + templatename = templatename.remove(ext, extension.size()); + } else { + extension = ".view.latte"; + ext = templatename.lastIndexOf(extension); + templatename = templatename.remove(ext,extension.size()); + } + + return templatename; +} //! it is used in order to provide translations for system templates void Manager::exposeTranslatedTemplateNames() diff --git a/app/templates/templatesmanager.h b/app/templates/templatesmanager.h index f68dd2b3d..ffbf52aeb 100644 --- a/app/templates/templatesmanager.h +++ b/app/templates/templatesmanager.h @@ -60,6 +60,8 @@ public: QString viewTemplateFilePath(const QString templateName) const; + static QString templateName(const QString &filePath); + void importSystemLayouts(); void installCustomLayoutTemplate(const QString &templateFilePath);