File indexing completed on 2024-05-19 05:38:21

0001 /*
0002     SPDX-FileCopyrightText: 2017 Marco Martin <mart@kde.org>
0003     SPDX-FileCopyrightText: 2022 Dominic Hayes <ferenosdev@outlook.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "kcm.h"
0009 
0010 #include <iostream>
0011 
0012 #include <QApplication>
0013 #include <QCommandLineParser>
0014 #include <QDebug>
0015 
0016 // Frameworks
0017 #include <KAboutData>
0018 #include <KLocalizedString>
0019 
0020 #include <KPackage/Package>
0021 #include <KPackage/PackageLoader>
0022 
0023 #include "lookandfeelsettings.h"
0024 
0025 int main(int argc, char **argv)
0026 {
0027     QApplication app(argc, argv);
0028 
0029     const char version[] = "1.0";
0030 
0031     // About data
0032     KAboutData aboutData("plasma-apply-lookandfeel",
0033                          i18n("Global Theme Tool"),
0034                          version,
0035                          i18n("Command line tool to apply global theme packages for changing the look and feel."),
0036                          KAboutLicense::GPL,
0037                          i18n("Copyright 2017, Marco Martin"));
0038     aboutData.addAuthor(i18n("Marco Martin"), i18n("Maintainer"), QStringLiteral("mart@kde.org"));
0039     aboutData.setDesktopFileName("org.kde.plasma-apply-lookandfeel");
0040     KAboutData::setApplicationData(aboutData);
0041 
0042     const static auto _l = QStringLiteral("list");
0043     const static auto _a = QStringLiteral("apply");
0044     const static auto _r = QStringLiteral("resetLayout");
0045 
0046     QCommandLineOption _list = QCommandLineOption(QStringList() << QStringLiteral("l") << _l, i18n("List available global theme packages"));
0047     QCommandLineOption _apply =
0048         QCommandLineOption(QStringList() << QStringLiteral("a") << _a,
0049                            i18n("Apply a global theme package. This can be the name of a package, or a full path to an installed package, at which point this "
0050                                 "tool will ensure it is a global theme package and then attempt to apply it"),
0051                            i18n("packagename"));
0052     QCommandLineOption _resetLayout = QCommandLineOption(QStringList() << _r, i18n("Reset the Plasma Desktop layout"));
0053 
0054     QCommandLineParser parser;
0055     parser.addOption(_list);
0056     parser.addOption(_apply);
0057     parser.addOption(_resetLayout);
0058     aboutData.setupCommandLine(&parser);
0059 
0060     parser.process(app);
0061     aboutData.processCommandLine(&parser);
0062 
0063     if (!parser.isSet(_list) && !parser.isSet(_apply)) {
0064         parser.showHelp();
0065     }
0066 
0067     if (parser.isSet(_list)) {
0068         const QList<KPluginMetaData> pkgs = KPackage::PackageLoader::self()->listPackages("Plasma/LookAndFeel");
0069 
0070         for (const KPluginMetaData &data : pkgs) {
0071             std::cout << data.pluginId().toStdString() << std::endl;
0072         }
0073 
0074     } else if (parser.isSet(_apply)) {
0075         QString requestedTheme{parser.value(_apply)};
0076         QFileInfo info(requestedTheme);
0077         // Check if the theme name passed validates as the absolute path for a folder
0078         if (info.isDir()) {
0079             // absolute paths need to be passed with trailing shash to KPackage
0080             requestedTheme += QStringLiteral("/");
0081         }
0082         KPackage::Package p = KPackage::PackageLoader::self()->loadPackage("Plasma/LookAndFeel");
0083         p.setPath(requestedTheme);
0084 
0085         // can't use package.isValid as lnf packages always fallback, even when not existing
0086         if (p.metadata().pluginId() != requestedTheme) {
0087             if (!p.path().isEmpty() && p.path() == requestedTheme && QFile(p.path()).exists()) {
0088                 std::cout << "Absolute path to theme passed in, set that" << std::endl;
0089                 requestedTheme = p.metadata().pluginId();
0090             } else {
0091                 std::cout << "Unable to find the theme named " << requestedTheme.toStdString() << std::endl;
0092                 return 1;
0093             }
0094         }
0095 
0096         KCMLookandFeel *kcm = new KCMLookandFeel(nullptr, KPluginMetaData());
0097         kcm->load();
0098         kcm->lookAndFeelSettings()->setLookAndFeelPackage(requestedTheme);
0099         // By default do not modify the layout, unless explicitely specified
0100         LookAndFeelManager::Contents selection = LookAndFeelManager::AppearanceSettings;
0101         if (parser.isSet(_resetLayout)) {
0102             selection |= LookAndFeelManager::LayoutSettings;
0103         }
0104         kcm->setSelectedContents(selection);
0105         // Save manually as we aren't in an event loop
0106         kcm->lookAndFeelSettings()->save();
0107         kcm->save();
0108         delete kcm;
0109     }
0110 
0111     return 0;
0112 }