File indexing completed on 2022-08-04 16:19:26

0001 /*
0002  *  SPDX-FileCopyrightText: 2016 Marco Martin <mart@kde.org>
0003  *  SPDX-FileCopyrightText: 2016 David Edmundson <davidedmundson@kde.org>
0004  *
0005  *  SPDX-License-Identifier: GPL-2.0-or-later
0006  *
0007  */
0008 #include <QApplication>
0009 #include <QCommandLineOption>
0010 #include <QCommandLineParser>
0011 #include <QDebug>
0012 #include <QDir>
0013 #include <QFile>
0014 #include <QProcess>
0015 
0016 #include <kauth_version.h>
0017 #if KAUTH_VERSION >= QT_VERSION_CHECK(5, 92, 0)
0018 #include <KAuth/Action>
0019 #include <KAuth/ExecuteJob>
0020 #else
0021 #include <KAuthAction>
0022 #include <KAuthExecuteJob>
0023 #endif
0024 
0025 #include <KConfigGroup>
0026 #include <KLocalizedString>
0027 #include <KMessageBox>
0028 #include <KSharedConfig>
0029 
0030 int main(int argc, char **argv)
0031 {
0032     QCommandLineParser parser;
0033     QApplication app(argc, argv); // because GHNS doesn't do it's own error reporting on installation failing..
0034 
0035     const QString description = i18n("SDDM theme installer");
0036     const char version[] = "1.0";
0037 
0038     app.setApplicationVersion(QString::fromLatin1(version));
0039     parser.addVersionOption();
0040     parser.addHelpOption();
0041     parser.setApplicationDescription(description);
0042     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("i") << QStringLiteral("install"), i18n("Install a theme.")));
0043     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("u") << QStringLiteral("uninstall"), i18n("Uninstall a theme.")));
0044 
0045     parser.addPositionalArgument(QStringLiteral("themefile"), i18n("The theme to install, must be an existing archive file."));
0046 
0047     parser.process(app);
0048 
0049     const QStringList args = parser.positionalArguments();
0050     if (args.isEmpty()) {
0051         qWarning() << "No theme file specified.";
0052         return 0;
0053     }
0054 
0055     if (parser.isSet(QStringLiteral("install"))) {
0056         const QFileInfo themefile(args.first());
0057         if (!themefile.exists()) {
0058             qWarning() << "Specified theme file does not exist";
0059             return 0;
0060         }
0061 
0062         KAuth::Action action(QStringLiteral("org.kde.kcontrol.kcmsddm.installtheme"));
0063         action.setHelperId(QStringLiteral("org.kde.kcontrol.kcmsddm"));
0064         action.addArgument(QStringLiteral("filePath"), themefile.absoluteFilePath());
0065 
0066         KAuth::ExecuteJob *job = action.execute();
0067         bool rc = job->exec();
0068         if (!rc) {
0069             QString errorString = job->errorString();
0070             if (!errorString.isEmpty()) {
0071                 KMessageBox::error(nullptr, errorString, i18n("Unable to install theme"));
0072             }
0073             return -1;
0074         }
0075 
0076         KConfigGroup cg(KSharedConfig::openConfig(QStringLiteral("sddmthemeinstallerrc"), KConfig::SimpleConfig), "DownloadedThemes");
0077         cg.writeEntry(themefile.absoluteFilePath(), job->data().value(QStringLiteral("installedPaths")).toStringList());
0078         return 0;
0079     }
0080     if (parser.isSet(QStringLiteral("uninstall"))) {
0081         KConfigGroup cg(KSharedConfig::openConfig(QStringLiteral("sddmthemeinstallerrc"), KConfig::SimpleConfig), "DownloadedThemes");
0082         const QStringList installed = cg.readEntry(args.first(), QStringList());
0083         for (const QString &installedTheme : installed) {
0084             KAuth::Action action(QStringLiteral("org.kde.kcontrol.kcmsddm.uninstalltheme"));
0085             action.setHelperId(QStringLiteral("org.kde.kcontrol.kcmsddm"));
0086             action.addArgument(QStringLiteral("filePath"), installedTheme);
0087             KAuth::ExecuteJob *job = action.execute();
0088             // We want KNS to be able to tell if the entry was manually deleted, see BUG: 416255
0089             if (job->exec()) {
0090                 QFile::remove(args.first());
0091                 cg.deleteEntry(args.first());
0092             }
0093         }
0094         return 0;
0095     }
0096     qWarning() << "either install or uninstall must be passed as an argument";
0097     return -1;
0098 }