Commit f03cae18 authored by David Edmundson's avatar David Edmundson Committed by David Edmundson
Browse files

[libkworkspace] Support batch operations in UpdateLaunchEnvJob

This makes client code easier and faster.
A test is included.
parent 7079115d
add_executable(sessiontest sessiontest.cpp)
target_link_libraries(sessiontest PW::KWorkspace)
add_executable(syncdbusenv syncdbusenvtest.cpp)
target_link_libraries(syncdbusenv PW::KWorkspace)
/*
Copyright (C) 2019 David Edmundson <davidedmundson@kde.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the Lesser GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program 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 Lesser GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <QCoreApplication>
#include <updatelaunchenvjob.h>
// This test syncs the current environment of the spawned process to systemd/whatever
// akin to dbus-update-activation-environment
// it can then be compared with "systemd-run --user -P env" or watched with dbus-monitor
int main (int argc, char **argv)
{
QCoreApplication app(argc, argv);
auto job = new UpdateLaunchEnvJob(QProcessEnvironment::systemEnvironment());
return job->exec();
}
......@@ -29,8 +29,7 @@ public:
void monitorReply(const QDBusPendingReply<> &reply);
UpdateLaunchEnvJob *q;
QString varName;
QString value;
QProcessEnvironment environment;
int pendingReplies = 0;
};
......@@ -58,8 +57,14 @@ void UpdateLaunchEnvJob::Private::monitorReply(const QDBusPendingReply<> &reply)
UpdateLaunchEnvJob::UpdateLaunchEnvJob(const QString &varName, const QString &value)
: d(new Private(this))
{
d->varName = varName;
d->value = value;
d->environment.insert(varName, value);
start();
}
UpdateLaunchEnvJob::UpdateLaunchEnvJob(const QProcessEnvironment &environment)
: d(new Private(this))
{
d->environment = environment;
start();
}
......@@ -70,26 +75,37 @@ UpdateLaunchEnvJob::~UpdateLaunchEnvJob()
void UpdateLaunchEnvJob::start()
{
// KLauncher
org::kde::KLauncher klauncher(QStringLiteral("org.kde.klauncher5"),
QStringLiteral("/KLauncher"),
QDBusConnection::sessionBus());
auto klauncherReply = klauncher.setLaunchEnv(d->varName, d->value);
d->monitorReply(klauncherReply);
// plasma-session
org::kde::Startup startup(QStringLiteral("org.kde.Startup"),
QStringLiteral("/Startup"),
QDBusConnection::sessionBus());
auto startupReply = startup.updateLaunchEnv(d->varName, d->value);
d->monitorReply(startupReply);
// DBus-activation environment
qDBusRegisterMetaType<QMap<QString, QString>>();
const QMap<QString, QString> dbusActivationEnv{
{d->varName, d->value}
};
QMap<QString, QString> dbusActivationEnv;
QStringList systemdUpdates;
for (const auto &varName : d->environment.keys()) {
const QString value = d->environment.value(varName);
// KLauncher
org::kde::KLauncher klauncher(QStringLiteral("org.kde.klauncher5"),
QStringLiteral("/KLauncher"),
QDBusConnection::sessionBus());
auto klauncherReply = klauncher.setLaunchEnv(varName, value);
d->monitorReply(klauncherReply);
// plasma-session
org::kde::Startup startup(QStringLiteral("org.kde.Startup"),
QStringLiteral("/Startup"),
QDBusConnection::sessionBus());
auto startupReply = startup.updateLaunchEnv(varName, value);
d->monitorReply(startupReply);
// DBus-activation environment
dbusActivationEnv.insert(varName, value);
// _user_ systemd env
const QString updateString = varName + QStringLiteral("=") + value;
systemdUpdates.append(updateString);
}
// DBus-activation environment
QDBusMessage dbusActivationMsg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.DBus"),
QStringLiteral("/org/freedesktop/DBus"),
QStringLiteral("org.freedesktop.DBus"),
......@@ -101,11 +117,11 @@ void UpdateLaunchEnvJob::start()
// _user_ systemd env
QDBusMessage systemdActivationMsg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.systemd1"),
QStringLiteral("/org/freedesktop/systemd1"),
QStringLiteral("org.freedesktop.systemd1.Manager"),
QStringLiteral("SetEnvironment"));
const QString updateString = d->varName + QStringLiteral("=") + d->value;
systemdActivationMsg.setArguments({QVariant(QStringList{updateString})});
QStringLiteral("/org/freedesktop/systemd1"),
QStringLiteral("org.freedesktop.systemd1.Manager"),
QStringLiteral("SetEnvironment"));
systemdActivationMsg.setArguments({systemdUpdates});
auto systemdActivationReply = QDBusConnection::sessionBus().asyncCall(systemdActivationMsg);
d->monitorReply(systemdActivationReply);
......
......@@ -22,6 +22,7 @@
#include "kworkspace_export.h"
#include <KJob>
#include <QProcessEnvironment>
class QString;
......@@ -43,6 +44,7 @@ class KWORKSPACE_EXPORT UpdateLaunchEnvJob : public KJob
public:
explicit UpdateLaunchEnvJob(const QString &varName, const QString &value);
explicit UpdateLaunchEnvJob(const QProcessEnvironment &environment);
~UpdateLaunchEnvJob() override;
void start() override;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment