File indexing completed on 2024-05-19 05:38:05
0001 /* 0002 SPDX-FileCopyrightText: 2021 Dan Leinir Turthra Jensen <admin@leinir.dk> 0003 0004 SPDX-License-Identifier: LGPL-2.0-only 0005 */ 0006 0007 #include "themesmodel.h" 0008 0009 #include <Plasma/Theme> 0010 0011 #include <KLocalizedString> 0012 0013 #include <QApplication> 0014 #include <QCommandLineParser> 0015 #include <QDebug> 0016 #include <QTimer> 0017 0018 int main(int argc, char **argv) 0019 { 0020 // This is a CLI application, but we require at least a QGuiApplication for things 0021 // in Plasma::Theme, so let's just roll with one of these 0022 QApplication app(argc, argv); 0023 QCoreApplication::setApplicationName(QStringLiteral("plasma-apply-desktoptheme")); 0024 QCoreApplication::setApplicationVersion(QStringLiteral("1.0")); 0025 QCoreApplication::setOrganizationDomain(QStringLiteral("kde.org")); 0026 KLocalizedString::setApplicationDomain(QByteArrayLiteral("plasma-apply-desktoptheme")); 0027 0028 QCommandLineParser *parser = new QCommandLineParser; 0029 parser->addHelpOption(); 0030 parser->setApplicationDescription( 0031 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 " 0032 "which is already set.")); 0033 parser->addPositionalArgument( 0034 QStringLiteral("themename"), 0035 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)")); 0036 parser->addOption(QCommandLineOption(QStringLiteral("list-themes"), i18n("Show all the themes available on the system (and which is the current theme)"))); 0037 parser->process(app); 0038 0039 int errorCode{0}; 0040 QTextStream ts(stdout); 0041 ThemesModel *model{new ThemesModel(&app)}; 0042 if (!parser->positionalArguments().isEmpty()) { 0043 QString requestedTheme{parser->positionalArguments().first()}; 0044 const QString dirSplit{"/"}; 0045 if (requestedTheme.contains(dirSplit)) { 0046 requestedTheme = requestedTheme.split(dirSplit, Qt::SkipEmptyParts).last(); 0047 } 0048 if (Plasma::Theme().themeName() == requestedTheme) { 0049 ts << i18n("The requested theme \"%1\" is already set as the theme for the current Plasma session.", requestedTheme) << Qt::endl; 0050 // Not an error condition really, let's just ignore that 0051 } else { 0052 bool found{false}; 0053 QStringList availableThemes; 0054 model->load(); 0055 for (int i = 0; i < model->rowCount(); ++i) { 0056 QString currentTheme{model->data(model->index(i), ThemesModel::PluginNameRole).toString()}; 0057 if (currentTheme == requestedTheme) { 0058 Plasma::Theme().setThemeName(requestedTheme); 0059 found = true; 0060 break; 0061 } 0062 availableThemes << currentTheme; 0063 } 0064 if (found) { 0065 ts << i18n("The current Plasma session's theme has been set to %1", requestedTheme) << Qt::endl; 0066 } else { 0067 ts << i18n("Could not find theme \"%1\". The theme should be one of the following options: %2", 0068 requestedTheme, 0069 availableThemes.join(QLatin1String{", "})) 0070 << Qt::endl; 0071 errorCode = -1; 0072 } 0073 } 0074 } else if (parser->isSet(QStringLiteral("list-themes"))) { 0075 ts << i18n("You have the following Plasma themes on your system:") << Qt::endl; 0076 model->load(); 0077 for (int i = 0; i < model->rowCount(); ++i) { 0078 QString themeName{model->data(model->index(i), ThemesModel::PluginNameRole).toString()}; 0079 if (Plasma::Theme().themeName() == themeName) { 0080 ts << QString(" * %1 (current theme for this Plasma session)").arg(themeName) << Qt::endl; 0081 } else { 0082 ts << QString(" * %1").arg(themeName) << Qt::endl; 0083 } 0084 } 0085 } else { 0086 parser->showHelp(); 0087 } 0088 QTimer::singleShot(0, &app, [&app, &errorCode]() { 0089 app.exit(errorCode); 0090 }); 0091 0092 return app.exec(); 0093 }