From d8948f54dea731fad9a1057f2418718a7a85fc55 Mon Sep 17 00:00:00 2001 From: David Redondo Date: Thu, 16 Jul 2020 10:16:59 +0200 Subject: [PATCH] Interactive console: Fix loading of templates and port from deprecated api The existing method was overly complicated given that KPackage can load packages relative to the default package root. Also it didn't work because QStandardPaths defaults to LocateFile, not finding any packages. --- .../interactiveconsole/interactiveconsole.cpp | 53 ++++++------------- 1 file changed, 17 insertions(+), 36 deletions(-) diff --git a/components/shellprivate/interactiveconsole/interactiveconsole.cpp b/components/shellprivate/interactiveconsole/interactiveconsole.cpp index f1b81274f..ae954e8af 100644 --- a/components/shellprivate/interactiveconsole/interactiveconsole.cpp +++ b/components/shellprivate/interactiveconsole/interactiveconsole.cpp @@ -50,7 +50,6 @@ #include #include #include -#include #include #include @@ -371,48 +370,30 @@ void InteractiveConsole::loadScriptFromUrl(const QUrl &url) void InteractiveConsole::populateTemplatesMenu() { m_snippetsMenu->clear(); - - QMap sorted; - const QString constraint = QStringLiteral("[X-Plasma-Shell] == '%1'") - .arg(qApp->applicationName()); - KService::List templates = KServiceTypeTrader::self()->query(QStringLiteral("Plasma/LayoutTemplate"), constraint); - foreach (const KService::Ptr &service, templates) { - sorted.insert(service->name(), service); - } - - QMapIterator it(sorted); - + auto templates = KPackage::PackageLoader::self()->findPackages(QStringLiteral("Plasma/LayoutTemplate"), QString(), [] (const KPluginMetaData &metaData) { + return metaData.value(QStringLiteral("X-Plasma-Shell")) == qApp->applicationName(); + }); + std::sort(templates.begin(), templates.end(), [] (const KPluginMetaData &left, const KPluginMetaData &right) { + return left.name() < right.name(); + }); KPackage::Package package = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Plasma/LayoutTemplate")); - - while (it.hasNext()) { - it.next(); - KPluginInfo info(it.value()); - const QString path = QStandardPaths::locate(QStandardPaths::GenericDataLocation -, package.defaultPackageRoot() + '/' + info.pluginName() + '/'); - if (!path.isEmpty()) { - package.setPath(info.pluginName()); - const QString scriptFile = package.filePath("mainscript"); - if (!scriptFile.isEmpty()) { - QAction *action = m_snippetsMenu->addAction(info.name()); - action->setData(info.pluginName()); - } + for (const auto &templateMetaData : templates){ + package.setPath(templateMetaData.pluginId()); + const QString scriptFile = package.filePath("mainscript"); + if (!scriptFile.isEmpty()) { + QAction *action = m_snippetsMenu->addAction(templateMetaData.name()); + action->setData(templateMetaData.pluginId()); } } } void InteractiveConsole::loadTemplate(QAction *action) { - KPackage::Package package = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Plasma/LayoutTemplate")); - - const QString pluginName = action->data().toString(); - const QString path = QStandardPaths::locate(QStandardPaths::GenericDataLocation -, package.defaultPackageRoot() + '/' + pluginName + '/'); - if (!path.isEmpty()) { - package.setPath(pluginName); - const QString scriptFile = package.filePath("mainscript"); - if (!scriptFile.isEmpty()) { - loadScriptFromUrl(QUrl::fromLocalFile(scriptFile)); - } + KPackage::Package package = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Plasma/LayoutTemplate"), + action->data().toString()); + const QString scriptFile = package.filePath("mainscript"); + if (!scriptFile.isEmpty()) { + loadScriptFromUrl(QUrl::fromLocalFile(scriptFile)); } }