Verified Commit 49346b0b authored by Xuetian Weng's avatar Xuetian Weng

Support importing environment variables from environment.d

systemd provides a desktop diagnostic place for user to set environment
variables. Currently we only export to it but didn't import from it.
This would also import from it so we are able to use environment.d to
set environment even systemd unit based plasma is not used.
parent 87e5b40a
......@@ -86,6 +86,11 @@ int main(int argc, char** argv)
return 1;
}
// We import systemd environment after we sync the dbus environment here.
// Otherwise it may leads to some unwanted order of applying environment
// variables (e.g. LANG and LC_*)
importSystemdEnvrionment();
QStringList args;
if (argc > 1) {
args.reserve(argc);
......
......@@ -101,6 +101,11 @@ int main(int argc, char** argv)
return 1;
}
// We import systemd environment after we sync the dbus environment here.
// Otherwise it may leads to some unwanted order of applying environment
// variables (e.g. LANG and LC_*)
importSystemdEnvrionment();
if (!startPlasmaSession(false))
return 1;
......
......@@ -88,6 +88,23 @@ int runSync(const QString& program, const QStringList &args, const QStringList &
return p.exitCode();
}
// A helper function to set an environment string in the form of VAR=VALUE.
void setEnvironmentVariable(const QByteArray &env) {
if (env.startsWith("_=") || env.startsWith("SHLVL")) {
return;
}
const int idx = env.indexOf('=');
if (Q_UNLIKELY(idx <= 0)) {
return;
}
if (qgetenv(env.left(idx)) != env.mid(idx+1)) {
// qCDebug(PLASMA_STARTUP) << "setting..." << env.left(idx) << env.mid(idx+1) << "was" << qgetenv(env.left(idx));
qputenv(env.left(idx), env.mid(idx+1));
}
}
void sourceFiles(const QStringList &files)
{
QStringList filteredFiles;
......@@ -106,17 +123,7 @@ void sourceFiles(const QStringList &files)
auto envs = fullEnv.split('\0');
for (auto &env: envs) {
if (env.startsWith("_=") || env.startsWith("SHLVL"))
continue;
const int idx = env.indexOf('=');
if (Q_UNLIKELY(idx <= 0))
continue;
if (qgetenv(env.left(idx)) != env.mid(idx+1)) {
// qCDebug(PLASMA_STARTUP) << "setting..." << env.left(idx) << env.mid(idx+1) << "was" << qgetenv(env.left(idx));
qputenv(env.left(idx), env.mid(idx+1));
}
setEnvironmentVariable(env);
}
}
......@@ -181,6 +188,37 @@ void setupCursor(bool wayland)
qputenv("XCURSOR_SIZE", QByteArray::number(kcminputrc_mouse_cursorsize));
}
// Import systemd user environment.
//
// Systemd read ~/.config/environment.d which applies to all systemd user unit.
// But it won't work if plasma is not started by systemd.
void importSystemdEnvrionment() {
auto msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.systemd1"),
QStringLiteral("/org/freedesktop/systemd1"),
QStringLiteral("org.freedesktop.DBus.Properties"),
QStringLiteral("Get"));
msg << QStringLiteral("org.freedesktop.systemd1.Manager") << QStringLiteral("Environment");
auto reply = QDBusConnection::sessionBus().call(msg);
if (reply.type() == QDBusMessage::ErrorMessage) {
return;
}
// Make sure the returned type is correct.
auto arguments = reply.arguments();
if (arguments.isEmpty() || arguments[0].userType() != qMetaTypeId<QDBusVariant>()) {
return;
}
auto variant = qdbus_cast<QVariant>(arguments[0]);
if (variant.type() != QVariant::StringList) {
return;
}
for (auto &envString : variant.toStringList()) {
setEnvironmentVariable(envString.toLocal8Bit());
}
}
// Source scripts found in <config locations>/plasma-workspace/env/*.sh
// (where <config locations> correspond to the system and user's configuration
// directory.
......
......@@ -34,6 +34,7 @@ void messageBox(const QString &text);
void createConfigDirectory();
void runStartupConfig();
void setupCursor(bool wayland);
void importSystemdEnvrionment();
void runEnvironmentScripts();
void setupPlasmaEnvironment();
void cleanupPlasmaEnvironment();
......
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