Adapt to KPackage API changes for install/uninstall

This simplifies the code, because we can access the package after installing it.

Depends on https://invent.kde.org/frameworks/kpackage/-/merge_requests/68
This commit is contained in:
Alexander Lohnau 2023-03-12 16:46:06 +01:00
parent d9077e36c5
commit 4e9303a6cc
2 changed files with 19 additions and 36 deletions

View file

@ -19,8 +19,8 @@
#include <KMessageWidget>
#include <KNSWidgets/Button>
#include <KPackage/Package>
#include <KPackage/PackageJob>
#include <KPackage/PackageLoader>
#include <KPackage/PackageStructure>
#include <KPluginFactory>
#include <KSharedConfig>
@ -61,12 +61,23 @@ void Module::importScript()
}
using namespace KPackage;
PackageStructure *structure = PackageLoader::self()->loadPackageStructure(QStringLiteral("KWin/Script"));
Package package(structure);
KJob *installJob = package.update(path);
installJob->setProperty("packagePath", path); // so we can retrieve it later for showing the script's name
connect(installJob, &KJob::result, this, &Module::importScriptInstallFinished);
auto job = PackageJob::update(QStringLiteral("KWin/Script"), path);
connect(job, &KJob::result, this, [job, this]() {
if (job->error() != KJob::NoError) {
setErrorMessage(i18nc("Placeholder is error message returned from the install service", "Cannot import selected script.\n%1", job->errorString()));
return;
}
m_infoMessage = i18nc("Placeholder is name of the script that was imported", "The script \"%1\" was successfully imported.", job->package().metadata().name());
m_errorMessage.clear();
Q_EMIT messageChanged();
m_model->clear();
m_model->addPlugins(m_kwinScriptsData->pluginMetaDataList(), QString());
setNeedsSave(false);
});
}
void Module::configure(const KPluginMetaData &data)
@ -88,32 +99,6 @@ void Module::togglePendingDeletion(const KPluginMetaData &data)
Q_EMIT pendingDeletionsChanged();
}
void Module::importScriptInstallFinished(KJob *job)
{
// if the applet is already installed, just add it to the containment
if (job->error() != KJob::NoError) {
setErrorMessage(i18nc("Placeholder is error message returned from the install service", "Cannot import selected script.\n%1", job->errorString()));
return;
}
using namespace KPackage;
// so we can show the name of the package we just imported
PackageStructure *structure = PackageLoader::self()->loadPackageStructure(QStringLiteral("KWin/Script"));
Package package(structure);
package.setPath(job->property("packagePath").toString());
Q_ASSERT(package.isValid());
m_infoMessage = i18nc("Placeholder is name of the script that was imported", "The script \"%1\" was successfully imported.", package.metadata().name());
m_errorMessage.clear();
Q_EMIT messageChanged();
m_model->clear();
m_model->addPlugins(m_kwinScriptsData->pluginMetaDataList(), QString());
setNeedsSave(false);
}
void Module::defaults()
{
m_model->defaults();
@ -136,12 +121,11 @@ void Module::load()
void Module::save()
{
using namespace KPackage;
PackageStructure *structure = PackageLoader::self()->loadPackageStructure(QStringLiteral("KWin/Script"));
for (const KPluginMetaData &info : std::as_const(m_pendingDeletions)) {
// We can get the package root from the entry path
QDir root = QFileInfo(info.fileName()).dir();
root.cdUp();
KJob *uninstallJob = Package(structure).uninstall(info.pluginId(), root.absolutePath());
KJob *uninstallJob = PackageJob::uninstall(QStringLiteral("KWin/Script"), info.pluginId(), root.absolutePath());
connect(uninstallJob, &KJob::result, this, [this, uninstallJob]() {
if (!uninstallJob->errorString().isEmpty()) {
setErrorMessage(i18n("Error when uninstalling KWin Script: %1", uninstallJob->errorString()));

View file

@ -7,6 +7,7 @@
#pragma once
#include <KCModule>
#include <KPackage/Package>
#include <KPluginMetaData>
#include <KPluginModel>
#include <KQuickAddons/ConfigModule>
@ -74,8 +75,6 @@ Q_SIGNALS:
void pendingDeletionsChanged();
private:
void importScriptInstallFinished(KJob *job);
KWinScriptsData *m_kwinScriptsData;
QList<KPluginMetaData> m_pendingDeletions;
KPluginModel *m_model;