Add CLI tool to set window decorations, and use for KNS adoptions

This adds a command line tool which allows the user to set the window
decoration, and then that tool is used in the two knsrc files to allow
the user to switch window decoration directly from either a KNS dialog,
or from Discover.
This commit is contained in:
Dan Leinir Turthra Jensen 2021-02-11 16:44:33 +00:00
parent 93d5901c85
commit df498d9be4
5 changed files with 150 additions and 2 deletions

View file

@ -33,10 +33,29 @@ target_link_libraries(kcm_kwindecoration
kcoreaddons_desktop_to_json(kcm_kwindecoration "kwindecoration.desktop" SERVICE_TYPES kcmodule.desktop)
set(kwin-applywindowdecoration_SRCS
kwin-applywindowdecoration.cpp
decorationmodel.cpp
utils.cpp
)
kconfig_add_kcfg_files(kwin-applywindowdecoration_SRCS kwindecorationsettings.kcfgc GENERATE_MOC)
add_executable(kwin-applywindowdecoration ${kwin-applywindowdecoration_SRCS})
target_link_libraries(kwin-applywindowdecoration
KDecoration2::KDecoration
Qt::DBus
KF5::I18n
KF5::KCMUtils
)
configure_file(window-decorations.knsrc.cmake ${CMAKE_CURRENT_BINARY_DIR}/window-decorations.knsrc)
# This desktop file is installed only for retrocompatibility with sycoca
install(FILES kwindecorationsettings.kcfg DESTINATION ${KDE_INSTALL_KCFGDIR})
install(FILES kwindecoration.desktop DESTINATION ${KDE_INSTALL_KSERVICES5DIR})
install(FILES window-decorations.knsrc DESTINATION ${KDE_INSTALL_KNSRCDIR})
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/window-decorations.knsrc DESTINATION ${KDE_INSTALL_KNSRCDIR})
install(TARGETS kcm_kwindecoration DESTINATION ${KDE_INSTALL_PLUGINDIR}/kcms)
install(TARGETS kwin-applywindowdecoration DESTINATION ${KDE_INSTALL_LIBEXECDIR})
kpackage_install_package(package kcm_kwindecoration kcms)

View file

@ -0,0 +1,125 @@
/*
SPDX-FileCopyrightText: 2021 Dan Leinir Turthra Jensen <admin@leinir.dk>
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include "kwindecorationsettings.h"
#include "decorationmodel.h"
#include <KLocalizedString>
#include <QCoreApplication>
#include <QCommandLineParser>
#include <QDebug>
#include <QDBusMessage>
#include <QDBusConnection>
#include <QFileInfo>
#include <QTimer>
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
int exitCode{0};
QCoreApplication::setApplicationName(QStringLiteral("kwin-applywindowdecoration"));
QCoreApplication::setApplicationVersion(QStringLiteral("1.0"));
QCoreApplication::setOrganizationDomain(QStringLiteral("kde.org"));
KLocalizedString::setApplicationDomain("kwin-applywindowdecoration");
QCommandLineParser *parser = new QCommandLineParser;
parser->addHelpOption();
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."));
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."));
parser->addOption(QCommandLineOption(QStringLiteral("list-themes"), i18n("Show all the themes available on the system (and which is the current theme)")));
parser->process(app);
KDecoration2::Configuration::DecorationsModel *model = new KDecoration2::Configuration::DecorationsModel(&app);
model->init();
KWinDecorationSettings *settings = new KWinDecorationSettings(&app);
QTextStream ts(stdout);
if (!parser->positionalArguments().isEmpty()) {
QString requestedTheme{parser->positionalArguments().first()};
if (requestedTheme.endsWith(QStringLiteral("/*"))) {
// Themes installed through KNewStuff will commonly be given an installed files entry
// which has the main directory name and an asterix to say the cursors are all in that directory,
// and since one of the main purposes of this tool is to allow adopting things from a kns dialog,
// we handle that little weirdness here.
requestedTheme.remove(requestedTheme.length() - 2, 2);
}
bool themeResolved{true};
if (requestedTheme.contains(QStringLiteral("/"))) {
themeResolved = false;
if (QFileInfo::exists(requestedTheme) && QFileInfo(requestedTheme).isDir()) {
// Since this is the name of a directory, let's do a bit of checking to see
// if we know enough about it to deduce that this is, in fact, a theme.
QStringList splitTheme = requestedTheme.split(QStringLiteral("/"), Qt::SkipEmptyParts);
if (splitTheme.count() > 3 && splitTheme[splitTheme.count() - 3] == QStringLiteral("aurorae") && splitTheme[splitTheme.count() - 2] == QStringLiteral("themes")) {
// We think this is an aurorae theme, but let's just make a little more certain...
QString file(QStringLiteral("aurorae/themes/%1/metadata.desktop").arg(splitTheme.last()));
QString path = QStandardPaths::locate(QStandardPaths::GenericDataLocation, file);
if (!path.isEmpty() && path == QStringLiteral("%1/metadata.desktop").arg(requestedTheme)) {
requestedTheme = QString("__aurorae__svg__").append(splitTheme.last());
themeResolved = true;
ts << i18n("Resolved %1 to the KWin Aurorae theme \"%2\", and will attempt to set that as your current theme.").arg(parser->positionalArguments().first()).arg(requestedTheme) << endl;
}
}
} else {
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") << endl;
exitCode = -1;
}
}
if (settings->theme() == requestedTheme) {
ts << i18n("The requested theme \"%1\" is already set as the window decoration theme.", requestedTheme) << endl;
// not an error condition, just nothing happens
} else if (themeResolved) {
int index{-1};
QStringList availableThemes;
for (int i = 0 ; i < model->rowCount(); ++i) {
const QString themeName = model->data(model->index(i), KDecoration2::Configuration::DecorationsModel::ThemeNameRole).toString();
if (requestedTheme == themeName) {
index = i;
break;
}
availableThemes << themeName;
}
if (index > -1) {
settings->setTheme(model->data(model->index(index), KDecoration2::Configuration::DecorationsModel::ThemeNameRole).toString());
settings->setPluginName(model->data(model->index(index), KDecoration2::Configuration::DecorationsModel::PluginNameRole).toString());
if (settings->save()) {
// Send a signal to all kwin instances
QDBusMessage message = QDBusMessage::createSignal(QStringLiteral("/KWin"),
QStringLiteral("org.kde.KWin"),
QStringLiteral("reloadConfig"));
QDBusConnection::sessionBus().send(message);
ts << i18n("Successfully applied the cursor theme %1 to your current Plasma session",
model->data(model->index(index), KDecoration2::Configuration::DecorationsModel::ThemeNameRole).toString()) << endl;
} else {
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.");
exitCode = -1;
}
} else {
ts << i18n("Could not find theme \"%1\". The theme should be one of the following options: %2", requestedTheme, availableThemes.join(QStringLiteral(", "))) << endl;
exitCode = -1;
}
}
} else if (parser->isSet(QStringLiteral("list-themes"))) {
ts << i18n("You have the following KWin window decoration themes on your system:") << endl;
for (int i = 0 ; i < model->rowCount(); ++i) {
const QString displayName = model->data(model->index(i), Qt::DisplayRole).toString();
const QString themeName = model->data(model->index(i), KDecoration2::Configuration::DecorationsModel::ThemeNameRole).toString();
if (settings->theme() == themeName) {
ts << QStringLiteral(" * %1 (theme name: %2 - current theme for this Plasma session)").arg(displayName).arg(themeName) << endl;
} else {
ts << QStringLiteral(" * %1 (theme name: %2)").arg(displayName).arg(themeName) << endl;
}
}
} else {
parser->showHelp();
}
QTimer::singleShot(0, &app, [&app,&exitCode](){ app.exit(exitCode); });
return app.exec();
}

View file

@ -65,3 +65,4 @@ ProvidersUrl=https://autoconfig.kde.org/ocs/providers.xml
Categories=Window Decoration Aurorae
TargetDir=aurorae/themes
Uncompress=archive
AdoptionCommand=@KDE_INSTALL_LIBEXECDIR@/kwin-applywindowdecoration %f

View file

@ -43,9 +43,11 @@ target_link_libraries(decorationplugin
)
install(TARGETS decorationplugin DESTINATION ${KDE_INSTALL_QMLDIR}/org/kde/kwin/decoration)
configure_file(aurorae.knsrc.cmake ${CMAKE_CURRENT_BINARY_DIR}/aurorae.knsrc)
########### install files ###############
install(FILES aurorae.knsrc DESTINATION ${KDE_INSTALL_KNSRCDIR})
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/aurorae.knsrc DESTINATION ${KDE_INSTALL_KNSRCDIR})
install(
FILES
qml/AppMenuButton.qml

View file

@ -44,3 +44,4 @@ ProvidersUrl=https://download.kde.org/ocs/providers.xml
Categories=Window Decoration Aurorae
Uncompress=archive
TargetDir=aurorae/themes
AdoptionCommand=@KDE_INSTALL_LIBEXECDIR@/kwin-applywindowdecoration %f