KWin
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
kwin-applywindowdecoration.cpp
Go to the documentation of this file.
1/*
2 SPDX-FileCopyrightText: 2021 Dan Leinir Turthra Jensen <admin@leinir.dk>
3
4 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
5*/
6
7#include "kwindecorationsettings.h"
8
9#include "decorationmodel.h"
10
11#include <KLocalizedString>
12
13#include <QCommandLineParser>
14#include <QCoreApplication>
15#include <QDBusConnection>
16#include <QDBusMessage>
17#include <QDebug>
18#include <QFileInfo>
19#include <QTimer>
20
21int main(int argc, char **argv)
22{
23 QCoreApplication app(argc, argv);
24 int exitCode{0};
25 QCoreApplication::setApplicationName(QStringLiteral("kwin-applywindowdecoration"));
26 QCoreApplication::setApplicationVersion(QStringLiteral("1.0"));
27 QCoreApplication::setOrganizationDomain(QStringLiteral("kde.org"));
28 KLocalizedString::setApplicationDomain(QByteArrayLiteral("kwin-applywindowdecoration"));
29
30 QCommandLineParser *parser = new QCommandLineParser;
31 parser->addHelpOption();
32 parser->setApplicationDescription(i18n("This tool allows you to set the window decoration theme for the currently active session, without accidentally setting it to one that is either not available, or which is already set."));
33 parser->addPositionalArgument(QStringLiteral("theme"), i18n("The name of the window decoration theme you wish to set for KWin. Passing a full path will attempt to find a theme in that directory, and then apply that if one can be deduced."));
34 parser->addOption(QCommandLineOption(QStringLiteral("list-themes"), i18n("Show all the themes available on the system (and which is the current theme)")));
35 parser->process(app);
36
38 model->init();
39 KWinDecorationSettings *settings = new KWinDecorationSettings(&app);
40 QTextStream ts(stdout);
41 if (!parser->positionalArguments().isEmpty()) {
42 QString requestedTheme{parser->positionalArguments().constFirst()};
43 if (requestedTheme.endsWith(QStringLiteral("/*"))) {
44 // Themes installed through KNewStuff will commonly be given an installed files entry
45 // which has the main directory name and an asterix to say the cursors are all in that directory,
46 // and since one of the main purposes of this tool is to allow adopting things from a kns dialog,
47 // we handle that little weirdness here.
48 requestedTheme.remove(requestedTheme.length() - 2, 2);
49 }
50
51 bool themeResolved{true};
52 if (requestedTheme.contains(QStringLiteral("/"))) {
53 themeResolved = false;
54 if (QFileInfo::exists(requestedTheme) && QFileInfo(requestedTheme).isDir()) {
55 // Since this is the name of a directory, let's do a bit of checking to see
56 // if we know enough about it to deduce that this is, in fact, a theme.
57 QStringList splitTheme = requestedTheme.split(QStringLiteral("/"), Qt::SkipEmptyParts);
58 if (splitTheme.count() > 3 && splitTheme[splitTheme.count() - 3] == QStringLiteral("aurorae") && splitTheme[splitTheme.count() - 2] == QStringLiteral("themes")) {
59 // We think this is an aurorae theme, but let's just make a little more certain...
60 QString file(QStringLiteral("aurorae/themes/%1/metadata.desktop").arg(splitTheme.last()));
61 QString path = QStandardPaths::locate(QStandardPaths::GenericDataLocation, file);
62 if (!path.isEmpty() && path == QStringLiteral("%1/metadata.desktop").arg(requestedTheme)) {
63 requestedTheme = QString("__aurorae__svg__").append(splitTheme.last());
64 themeResolved = true;
65 ts << i18n("Resolved %1 to the KWin Aurorae theme \"%2\", and will attempt to set that as your current theme.")
66 .arg(parser->positionalArguments().first(), requestedTheme)
67 << Qt::endl;
68 }
69 }
70 } else {
71 ts << i18n("You attempted to pass a file path, but this could not be resolved to a theme, and we will have to abort, due to having no theme to set") << Qt::endl;
72 exitCode = -1;
73 }
74 }
75
76 if (settings->theme() == requestedTheme) {
77 ts << i18n("The requested theme \"%1\" is already set as the window decoration theme.", requestedTheme) << Qt::endl;
78 // not an error condition, just nothing happens
79 } else if (themeResolved) {
80 int index{-1};
81 QStringList availableThemes;
82 for (int i = 0; i < model->rowCount(); ++i) {
83 const QString themeName = model->data(model->index(i), KDecoration2::Configuration::DecorationsModel::ThemeNameRole).toString();
84 if (requestedTheme == themeName) {
85 index = i;
86 break;
87 }
88 availableThemes << themeName;
89 }
90 if (index > -1) {
91 settings->setTheme(model->data(model->index(index), KDecoration2::Configuration::DecorationsModel::ThemeNameRole).toString());
92 settings->setPluginName(model->data(model->index(index), KDecoration2::Configuration::DecorationsModel::PluginNameRole).toString());
93 if (settings->save()) {
94 // Send a signal to all kwin instances
95 QDBusMessage message = QDBusMessage::createSignal(QStringLiteral("/KWin"),
96 QStringLiteral("org.kde.KWin"),
97 QStringLiteral("reloadConfig"));
98 QDBusConnection::sessionBus().send(message);
99 ts << i18n("Successfully applied the cursor theme %1 to your current Plasma session",
100 model->data(model->index(index), KDecoration2::Configuration::DecorationsModel::ThemeNameRole).toString())
101 << Qt::endl;
102 } else {
103 ts << i18n("Failed to save your theme settings - the reason is unknown, but this is an unrecoverable error. You may find that simply trying again will work.");
104 exitCode = -1;
105 }
106 } else {
107 ts << i18n("Could not find theme \"%1\". The theme should be one of the following options: %2", requestedTheme, availableThemes.join(QStringLiteral(", "))) << Qt::endl;
108 exitCode = -1;
109 }
110 }
111 } else if (parser->isSet(QStringLiteral("list-themes"))) {
112 ts << i18n("You have the following KWin window decoration themes on your system:") << Qt::endl;
113 for (int i = 0; i < model->rowCount(); ++i) {
114 const QString displayName = model->data(model->index(i), Qt::DisplayRole).toString();
115 const QString themeName = model->data(model->index(i), KDecoration2::Configuration::DecorationsModel::ThemeNameRole).toString();
116 if (settings->theme() == themeName) {
117 ts << QStringLiteral(" * %1 (theme name: %2 - current theme for this Plasma session)").arg(displayName, themeName) << Qt::endl;
118 } else {
119 ts << QStringLiteral(" * %1 (theme name: %2)").arg(displayName, themeName) << Qt::endl;
120 }
121 }
122 } else {
123 parser->showHelp();
124 }
125 QTimer::singleShot(0, &app, [&app, &exitCode]() {
126 app.exit(exitCode);
127 });
128
129 return app.exec();
130}
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
int rowCount(const QModelIndex &parent=QModelIndex()) const override