File indexing completed on 2024-04-28 05:27:05

0001 /*
0002  *   SPDX-FileCopyrightText: 2021 Aleix Pol Gonzalez <aleixpol@kde.org>
0003  *
0004  *   SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include <KAboutData>
0008 #include <KIO/CommandLauncherJob>
0009 #include <KLocalizedString>
0010 #include <KRuntimePlatform>
0011 #include <QGuiApplication>
0012 #include <QStandardPaths>
0013 #include <QTextStream>
0014 #include <QUrl>
0015 
0016 KIO::CommandLauncherJob *openPlasmaSettings(QString &moduleName)
0017 {
0018     // TODO needs --args support in plasma-settings
0019     KIO::CommandLauncherJob *job = new KIO::CommandLauncherJob(QStringLiteral("plasma-settings"), {"-m", moduleName});
0020     job->setDesktopName(QStringLiteral("org.kde.plasma.settings"));
0021     return job;
0022 }
0023 
0024 int main(int argc, char **argv)
0025 {
0026     QGuiApplication app(argc, argv);
0027     KLocalizedString::setApplicationDomain(QByteArrayLiteral("plasma-open-settings"));
0028 
0029     KAboutData aboutData(QStringLiteral("plasma-open-settings"), //
0030                          i18n("App to open Settings app"),
0031                          QLatin1String(PROJECT_VERSION),
0032                          i18n("A tool to start system settings"),
0033                          KAboutLicense::GPL,
0034                          i18n("(c) 2021, The KDE Developers"));
0035 
0036     aboutData.addAuthor(QStringLiteral("Aleix Pol i Gonzalez"), {}, QStringLiteral("aleixpol@kde.org"));
0037 
0038     const QUrl url(app.arguments().constLast());
0039     QString moduleName = url.host().isEmpty() ? url.path() : url.host();
0040     if (moduleName.startsWith('/')) {
0041         moduleName = moduleName.mid(1);
0042     }
0043 
0044     QString args;
0045     if (int idx = moduleName.indexOf('/'); idx > 0) {
0046         args = moduleName.mid(idx + 1);
0047         moduleName = moduleName.left(idx);
0048     } else {
0049         args = url.path();
0050         args = args.mid(1);
0051     }
0052 
0053     KIO::CommandLauncherJob *job = nullptr;
0054     int ret = 0;
0055 
0056     if (KRuntimePlatform::runtimePlatform().contains("phone") && !QStandardPaths::findExecutable("plasma-settings").isEmpty()) {
0057         // plasma-settings has priority for mobile
0058         job = openPlasmaSettings(moduleName);
0059     } else if (!QStandardPaths::findExecutable("systemsettings").isEmpty()) {
0060         job = new KIO::CommandLauncherJob(QStringLiteral("systemsettings"), {moduleName, QStringLiteral("--args"), args});
0061         job->setDesktopName(QStringLiteral("org.kde.systemsettings"));
0062     } else if (!QStandardPaths::findExecutable("plasma-settings").isEmpty()) {
0063         job = openPlasmaSettings(moduleName);
0064     } else if (!QStandardPaths::findExecutable("kcmshell6").isEmpty()) {
0065         job = new KIO::CommandLauncherJob(QStringLiteral("kcmshell6"), {moduleName, QStringLiteral("--args"), args});
0066     } else if (!QStandardPaths::findExecutable("kdialog").isEmpty()) {
0067         job = new KIO::CommandLauncherJob(QStringLiteral("kdialog"), {"--error", i18n("Could not open: %1", moduleName)});
0068         ret = 1;
0069     } else {
0070         QTextStream err(stderr);
0071         err << "Could not open:" << moduleName << url.toString() << Qt::endl;
0072         return 32;
0073     }
0074 
0075     if (!qEnvironmentVariableIsEmpty("XDG_ACTIVATION_TOKEN")) {
0076         job->setStartupId(qgetenv("XDG_ACTIVATION_TOKEN"));
0077     }
0078     return !job->exec() + ret;
0079 }