// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. // // SPDX-License-Identifier: LGPL-3.0-or-later #include #include #include #include "global.h" #include "launchoptions.h" QStringList generateCommand(const QVariantMap &props) noexcept { std::vector> options; for (const auto &[key, value] : props.asKeyValueRange()) { if (key == setUserLaunchOption::key()) { options.push_back(std::make_unique(value)); } else if (key == setEnvLaunchOption::key()) { options.push_back(std::make_unique(value)); } else { qWarning() << "unsupported options" << key; } } options.push_back(std::make_unique()); std::sort(options.begin(), options.end(), [](const std::unique_ptr &lOption, const std::unique_ptr &rOption) { if (lOption->type() == AppExecOption and rOption->type() == systemdOption) { return false; } if (lOption->type() == AppExecOption and rOption->type() == splitOption) { return false; } if (lOption->type() == splitOption and rOption->type() == systemdOption) { return false; } return true; }); QStringList ret; std::for_each(options.begin(), options.end(), [&ret](const std::unique_ptr &option) { auto str = option->generateCommandLine(); if (!str.isEmpty()) { ret.append(std::move(str)); } }); return ret; } QStringList setUserLaunchOption::generateCommandLine() const noexcept { QStringList ret; bool ok{false}; auto uid = m_val.toUInt(&ok); if (!ok) { return ret; } auto curUID = getCurrentUID(); if (uid == curUID) { return ret; } ret.append("pkexec"); ret.append("--user"); struct passwd *destUser = getpwuid(uid); QString userName = destUser->pw_name; ret.append(userName); struct passwd *curUser = getpwuid(curUID); ret.append("env"); ret.append("DISPLAY=:0"); auto authFile = qgetenv("XAUTHORITY"); ret.append(QString{"XAUTHORITY=%1"}.arg(authFile)); return ret; } QStringList splitLaunchOption::generateCommandLine() const noexcept { return QStringList{m_val.toString()}; } QStringList setEnvLaunchOption::generateCommandLine() const noexcept { auto str = m_val.toString(); if (str.isEmpty()) { return {}; } return QStringList{QString{"--Environment=%1"}.arg(str)}; }