1 /*
2 SPDX-FileCopyrightText: 2021 Dan Leinir Turthra Jensen <admin@leinir.dk>
3
4 SPDX-License-Identifier: LGPL-2.0-only
5 */
6
7 #include "themesmodel.h"
8
9 #include <Plasma/Theme>
10
11 #include <KLocalizedString>
12
13 #include <QApplication>
14 #include <QCommandLineParser>
15 #include <QDebug>
16 #include <QTimer>
17
main(int argc,char ** argv)18 int main(int argc, char **argv)
19 {
20 // This is a CLI application, but we require at least a QGuiApplication for things
21 // in Plasma::Theme, so let's just roll with one of these
22 QApplication app(argc, argv);
23 QCoreApplication::setApplicationName(QStringLiteral("plasma-apply-desktoptheme"));
24 QCoreApplication::setApplicationVersion(QStringLiteral("1.0"));
25 QCoreApplication::setOrganizationDomain(QStringLiteral("kde.org"));
26 KLocalizedString::setApplicationDomain("plasma-apply-desktoptheme");
27
28 QCommandLineParser *parser = new QCommandLineParser;
29 parser->addHelpOption();
30 parser->setApplicationDescription(
31 i18n("This tool allows you to set the theme of the current Plasma session, without accidentally setting it to one that is either not available, or "
32 "which is already set."));
33 parser->addPositionalArgument(
34 QStringLiteral("themename"),
35 i18n("The name of the theme you wish to set for your current Plasma session (passing a full path will only use the last part of the path)"));
36 parser->addOption(QCommandLineOption(QStringLiteral("list-themes"), i18n("Show all the themes available on the system (and which is the current theme)")));
37 parser->process(app);
38
39 int errorCode{0};
40 QTextStream ts(stdout);
41 ThemesModel *model{new ThemesModel(&app)};
42 if (!parser->positionalArguments().isEmpty()) {
43 QString requestedTheme{parser->positionalArguments().first()};
44 const QString dirSplit{"/"};
45 if (requestedTheme.contains(dirSplit)) {
46 requestedTheme = requestedTheme.split(dirSplit, Qt::SkipEmptyParts).last();
47 }
48 if (Plasma::Theme().themeName() == requestedTheme) {
49 ts << i18n("The requested theme \"%1\" is already set as the theme for the current Plasma session.", requestedTheme) << Qt::endl;
50 // Not an error condition really, let's just ignore that
51 } else {
52 bool found{false};
53 QStringList availableThemes;
54 model->load();
55 for (int i = 0; i < model->rowCount(); ++i) {
56 QString currentTheme{model->data(model->index(i), ThemesModel::PluginNameRole).toString()};
57 if (currentTheme == requestedTheme) {
58 Plasma::Theme().setThemeName(requestedTheme);
59 found = true;
60 break;
61 }
62 availableThemes << currentTheme;
63 }
64 if (found) {
65 ts << i18n("The current Plasma session's theme has been set to %1", requestedTheme) << Qt::endl;
66 } else {
67 ts << i18n("Could not find theme \"%1\". The theme should be one of the following options: %2",
68 requestedTheme,
69 availableThemes.join(QLatin1String{", "}))
70 << Qt::endl;
71 errorCode = -1;
72 }
73 }
74 } else if (parser->isSet(QStringLiteral("list-themes"))) {
75 ts << i18n("You have the following Plasma themes on your system:") << Qt::endl;
76 model->load();
77 for (int i = 0; i < model->rowCount(); ++i) {
78 QString themeName{model->data(model->index(i), ThemesModel::PluginNameRole).toString()};
79 if (Plasma::Theme().themeName() == themeName) {
80 ts << QString(" * %1 (current theme for this Plasma session)").arg(themeName) << Qt::endl;
81 } else {
82 ts << QString(" * %1").arg(themeName) << Qt::endl;
83 }
84 }
85 } else {
86 parser->showHelp();
87 }
88 QTimer::singleShot(0, &app, [&app, &errorCode]() {
89 app.exit(errorCode);
90 });
91
92 return app.exec();
93 }
94