2007-04-29 17:35:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2004 Lubos Lunak <l.lunak@kde.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2013-12-05 19:22:31 +00:00
|
|
|
#include <QCommandLineParser>
|
|
|
|
#include <QApplication>
|
2007-04-29 17:35:43 +00:00
|
|
|
#include <kconfig.h>
|
2014-03-17 15:24:10 +00:00
|
|
|
#include <KLocalizedString>
|
2007-04-30 15:48:34 +00:00
|
|
|
#include <kwindowsystem.h>
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
#include "ruleswidget.h"
|
|
|
|
#include "../../rules.h"
|
|
|
|
#include <QByteArray>
|
|
|
|
|
2018-12-23 07:56:15 +00:00
|
|
|
#include <QDBusConnection>
|
|
|
|
#include <QDBusMessage>
|
|
|
|
#include <QDBusPendingCallWatcher>
|
|
|
|
#include <QDBusPendingReply>
|
|
|
|
#include <QUuid>
|
|
|
|
|
|
|
|
Q_DECLARE_METATYPE(NET::WindowType)
|
|
|
|
|
2007-04-29 17:35:43 +00:00
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
static void loadRules(QList< Rules* >& rules)
|
|
|
|
{
|
2018-12-19 13:07:23 +00:00
|
|
|
KConfig _cfg("kwinrulesrc", KConfig::NoGlobals);
|
2011-01-30 14:34:42 +00:00
|
|
|
KConfigGroup cfg(&_cfg, "General");
|
|
|
|
int count = cfg.readEntry("count", 0);
|
|
|
|
for (int i = 1;
|
|
|
|
i <= count;
|
|
|
|
++i) {
|
|
|
|
cfg = KConfigGroup(&_cfg, QString::number(i));
|
|
|
|
Rules* rule = new Rules(cfg);
|
|
|
|
rules.append(rule);
|
2007-04-29 17:35:43 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
static void saveRules(const QList< Rules* >& rules)
|
|
|
|
{
|
2018-12-19 13:07:23 +00:00
|
|
|
KConfig cfg("kwinrulesrc", KConfig::NoGlobals);
|
2007-04-29 17:35:43 +00:00
|
|
|
QStringList groups = cfg.groupList();
|
2011-01-30 14:34:42 +00:00
|
|
|
for (QStringList::ConstIterator it = groups.constBegin();
|
|
|
|
it != groups.constEnd();
|
|
|
|
++it)
|
|
|
|
cfg.deleteGroup(*it);
|
|
|
|
cfg.group("General").writeEntry("count", rules.count());
|
2007-04-29 17:35:43 +00:00
|
|
|
int i = 1;
|
2011-01-30 14:34:42 +00:00
|
|
|
for (QList< Rules* >::ConstIterator it = rules.constBegin();
|
|
|
|
it != rules.constEnd();
|
|
|
|
++it) {
|
|
|
|
KConfigGroup cg(&cfg, QString::number(i));
|
|
|
|
(*it)->write(cg);
|
2007-04-29 17:35:43 +00:00
|
|
|
++i;
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2018-12-23 07:56:15 +00:00
|
|
|
static Rules* findRule(const QList< Rules* >& rules, const QVariantMap &data, bool whole_app)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2018-12-23 07:56:15 +00:00
|
|
|
QByteArray wmclass_class = data.value("resourceClass").toByteArray().toLower();
|
|
|
|
QByteArray wmclass_name = data.value("resourceName").toByteArray().toLower();
|
|
|
|
QByteArray role = data.value("role").toByteArray().toLower();
|
|
|
|
NET::WindowType type = data.value("type").value<NET::WindowType>();
|
|
|
|
QString title = data.value("caption").toString();
|
|
|
|
QByteArray machine = data.value("clientMachine").toByteArray();
|
2014-01-07 10:22:24 +00:00
|
|
|
Rules* best_match = nullptr;
|
2007-04-29 17:35:43 +00:00
|
|
|
int match_quality = 0;
|
2011-01-30 14:34:42 +00:00
|
|
|
for (QList< Rules* >::ConstIterator it = rules.constBegin();
|
|
|
|
it != rules.constEnd();
|
|
|
|
++it) {
|
2007-04-29 17:35:43 +00:00
|
|
|
// try to find an exact match, i.e. not a generic rule
|
|
|
|
Rules* rule = *it;
|
|
|
|
int quality = 0;
|
|
|
|
bool generic = true;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (rule->wmclassmatch != Rules::ExactMatch)
|
2007-04-29 17:35:43 +00:00
|
|
|
continue; // too generic
|
2011-01-30 14:34:42 +00:00
|
|
|
if (!rule->matchWMClass(wmclass_class, wmclass_name))
|
2007-04-29 17:35:43 +00:00
|
|
|
continue;
|
|
|
|
// from now on, it matches the app - now try to match for a specific window
|
2011-01-30 14:34:42 +00:00
|
|
|
if (rule->wmclasscomplete) {
|
2007-04-29 17:35:43 +00:00
|
|
|
quality += 1;
|
|
|
|
generic = false; // this can be considered specific enough (old X apps)
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
|
|
|
if (!whole_app) {
|
|
|
|
if (rule->windowrolematch != Rules::UnimportantMatch) {
|
2007-04-29 17:35:43 +00:00
|
|
|
quality += rule->windowrolematch == Rules::ExactMatch ? 5 : 1;
|
|
|
|
generic = false;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
|
|
|
if (rule->titlematch != Rules::UnimportantMatch) {
|
2007-04-29 17:35:43 +00:00
|
|
|
quality += rule->titlematch == Rules::ExactMatch ? 3 : 1;
|
|
|
|
generic = false;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
|
|
|
if (rule->types != NET::AllTypesMask) {
|
2007-04-29 17:35:43 +00:00
|
|
|
int bits = 0;
|
2011-01-30 14:34:42 +00:00
|
|
|
for (unsigned int bit = 1;
|
|
|
|
bit < 1U << 31;
|
|
|
|
bit <<= 1)
|
|
|
|
if (rule->types & bit)
|
2007-04-29 17:35:43 +00:00
|
|
|
++bits;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (bits == 1)
|
2007-04-29 17:35:43 +00:00
|
|
|
quality += 2;
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
if (generic) // ignore generic rules, use only the ones that are for this window
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
if (rule->types == NET::AllTypesMask)
|
2007-04-29 17:35:43 +00:00
|
|
|
quality += 2;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
|
|
|
if (!rule->matchType(type)
|
|
|
|
|| !rule->matchRole(role)
|
|
|
|
|| !rule->matchTitle(title)
|
2018-12-23 07:56:15 +00:00
|
|
|
|| !rule->matchClientMachine(machine, data.value("localhost").toBool()))
|
2007-04-29 17:35:43 +00:00
|
|
|
continue;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (quality > match_quality) {
|
2007-04-29 17:35:43 +00:00
|
|
|
best_match = rule;
|
|
|
|
match_quality = quality;
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2014-01-07 10:22:24 +00:00
|
|
|
if (best_match != nullptr)
|
2007-04-29 17:35:43 +00:00
|
|
|
return best_match;
|
|
|
|
Rules* ret = new Rules;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (whole_app) {
|
|
|
|
ret->description = i18n("Application settings for %1", QString::fromLatin1(wmclass_class));
|
2007-04-29 17:35:43 +00:00
|
|
|
// TODO maybe exclude some types? If yes, then also exclude them above
|
|
|
|
// when searching.
|
|
|
|
ret->types = NET::AllTypesMask;
|
|
|
|
ret->titlematch = Rules::UnimportantMatch;
|
|
|
|
ret->clientmachine = machine; // set, but make unimportant
|
|
|
|
ret->clientmachinematch = Rules::UnimportantMatch;
|
|
|
|
ret->windowrolematch = Rules::UnimportantMatch;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (wmclass_name == wmclass_class) {
|
2007-04-29 17:35:43 +00:00
|
|
|
ret->wmclasscomplete = false;
|
|
|
|
ret->wmclass = wmclass_class;
|
|
|
|
ret->wmclassmatch = Rules::ExactMatch;
|
2011-01-30 14:34:42 +00:00
|
|
|
} else {
|
2007-04-29 17:35:43 +00:00
|
|
|
// WM_CLASS components differ - perhaps the app got -name argument
|
|
|
|
ret->wmclasscomplete = true;
|
|
|
|
ret->wmclass = wmclass_name + ' ' + wmclass_class;
|
|
|
|
ret->wmclassmatch = Rules::ExactMatch;
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
ret->description = i18n("Window settings for %1", QString::fromLatin1(wmclass_class));
|
|
|
|
if (type == NET::Unknown)
|
2007-04-29 17:35:43 +00:00
|
|
|
ret->types = NET::NormalMask;
|
|
|
|
else
|
2014-02-03 08:37:08 +00:00
|
|
|
ret->types = NET::WindowTypeMask( 1 << type); // convert type to its mask
|
2007-04-29 17:35:43 +00:00
|
|
|
ret->title = title; // set, but make unimportant
|
|
|
|
ret->titlematch = Rules::UnimportantMatch;
|
|
|
|
ret->clientmachine = machine; // set, but make unimportant
|
|
|
|
ret->clientmachinematch = Rules::UnimportantMatch;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (!role.isEmpty()
|
|
|
|
&& role != "unknown" && role != "unnamed") { // Qt sets this if not specified
|
2007-04-29 17:35:43 +00:00
|
|
|
ret->windowrole = role;
|
|
|
|
ret->windowrolematch = Rules::ExactMatch;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (wmclass_name == wmclass_class) {
|
2007-04-29 17:35:43 +00:00
|
|
|
ret->wmclasscomplete = false;
|
|
|
|
ret->wmclass = wmclass_class;
|
|
|
|
ret->wmclassmatch = Rules::ExactMatch;
|
2011-01-30 14:34:42 +00:00
|
|
|
} else {
|
2007-04-29 17:35:43 +00:00
|
|
|
// WM_CLASS components differ - perhaps the app got -name argument
|
|
|
|
ret->wmclasscomplete = true;
|
|
|
|
ret->wmclass = wmclass_name + ' ' + wmclass_class;
|
|
|
|
ret->wmclassmatch = Rules::ExactMatch;
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
} else { // no role set
|
|
|
|
if (wmclass_name != wmclass_class) {
|
2007-04-29 17:35:43 +00:00
|
|
|
ret->wmclasscomplete = true;
|
|
|
|
ret->wmclass = wmclass_name + ' ' + wmclass_class;
|
|
|
|
ret->wmclassmatch = Rules::ExactMatch;
|
2011-01-30 14:34:42 +00:00
|
|
|
} else {
|
2007-04-29 17:35:43 +00:00
|
|
|
// This is a window that has no role set, and both components of WM_CLASS
|
|
|
|
// match (possibly only differing in case), which most likely means either
|
|
|
|
// the application doesn't give a damn about distinguishing its various
|
|
|
|
// windows, or it's an app that uses role for that, but this window
|
|
|
|
// lacks it for some reason. Use non-complete WM_CLASS matching, also
|
|
|
|
// include window title in the matching, and pray it causes many more positive
|
|
|
|
// matches than negative matches.
|
|
|
|
ret->titlematch = Rules::ExactMatch;
|
|
|
|
ret->wmclasscomplete = false;
|
|
|
|
ret->wmclass = wmclass_class;
|
|
|
|
ret->wmclassmatch = Rules::ExactMatch;
|
|
|
|
}
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2018-12-23 07:56:15 +00:00
|
|
|
static void edit(const QVariantMap &data, bool whole_app)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2007-04-29 17:35:43 +00:00
|
|
|
QList< Rules* > rules;
|
2011-01-30 14:34:42 +00:00
|
|
|
loadRules(rules);
|
2018-12-23 07:56:15 +00:00
|
|
|
Rules* orig_rule = findRule(rules, data, whole_app);
|
2007-04-29 17:35:43 +00:00
|
|
|
RulesDialog dlg;
|
2012-03-14 07:48:51 +00:00
|
|
|
if (whole_app)
|
|
|
|
dlg.setWindowTitle(i18nc("Window caption for the application wide rules dialog", "Edit Application-Specific Settings"));
|
2007-04-29 17:35:43 +00:00
|
|
|
// dlg.edit() creates new Rules instance if edited
|
2018-12-23 07:56:15 +00:00
|
|
|
Rules* edited_rule = dlg.edit(orig_rule, data, true);
|
2014-01-07 10:22:24 +00:00
|
|
|
if (edited_rule == nullptr || edited_rule->isEmpty()) {
|
2011-01-30 14:34:42 +00:00
|
|
|
rules.removeAll(orig_rule);
|
2007-04-29 17:35:43 +00:00
|
|
|
delete orig_rule;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (orig_rule != edited_rule)
|
2007-04-29 17:35:43 +00:00
|
|
|
delete edited_rule;
|
2011-01-30 14:34:42 +00:00
|
|
|
} else if (edited_rule != orig_rule) {
|
|
|
|
int pos = rules.indexOf(orig_rule);
|
|
|
|
if (pos != -1)
|
2007-04-29 17:35:43 +00:00
|
|
|
rules[ pos ] = edited_rule;
|
|
|
|
else
|
2011-01-30 14:34:42 +00:00
|
|
|
rules.prepend(edited_rule);
|
2007-04-29 17:35:43 +00:00
|
|
|
delete orig_rule;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
|
|
|
saveRules(rules);
|
2007-04-30 11:32:47 +00:00
|
|
|
// Send signal to all kwin instances
|
|
|
|
QDBusMessage message =
|
2008-01-30 16:08:23 +00:00
|
|
|
QDBusMessage::createSignal("/KWin", "org.kde.KWin", "reloadConfig");
|
2007-04-30 11:32:47 +00:00
|
|
|
QDBusConnection::sessionBus().send(message);
|
2018-12-23 07:56:15 +00:00
|
|
|
qApp->quit();
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-01-22 12:45:51 +00:00
|
|
|
int main(int argc, char* argv[])
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2013-12-05 19:22:31 +00:00
|
|
|
QApplication app(argc, argv);
|
2019-09-16 11:49:52 +00:00
|
|
|
app.setAttribute(Qt::AA_UseHighDpiPixmaps, true);
|
2013-12-05 19:22:31 +00:00
|
|
|
app.setApplicationDisplayName(i18n("KWin"));
|
|
|
|
app.setApplicationName("kwin_rules_dialog");
|
|
|
|
app.setApplicationVersion("1.0");
|
|
|
|
bool whole_app = false;
|
2018-12-23 07:56:15 +00:00
|
|
|
QUuid uuid;
|
2013-12-05 19:22:31 +00:00
|
|
|
{
|
|
|
|
QCommandLineParser parser;
|
|
|
|
parser.setApplicationDescription(i18n("KWin helper utility"));
|
2018-12-23 07:56:15 +00:00
|
|
|
parser.addOption(QCommandLineOption("uuid", i18n("KWin id of the window for special window settings."), "uuid"));
|
2013-12-05 19:22:31 +00:00
|
|
|
parser.addOption(QCommandLineOption("whole-app", i18n("Whether the settings should affect all windows of the application.")));
|
|
|
|
parser.process(app);
|
|
|
|
|
2018-12-23 07:56:15 +00:00
|
|
|
uuid = QUuid::fromString(parser.value("uuid"));
|
2013-12-05 19:22:31 +00:00
|
|
|
whole_app = parser.isSet("whole-app");
|
|
|
|
}
|
|
|
|
|
2018-12-23 07:56:15 +00:00
|
|
|
|
|
|
|
if (uuid.isNull()) {
|
2013-12-05 19:22:31 +00:00
|
|
|
printf("%s\n", qPrintable(i18n("This helper utility is not supposed to be called directly.")));
|
2011-01-30 14:34:42 +00:00
|
|
|
return 1;
|
2007-04-29 17:35:43 +00:00
|
|
|
}
|
2018-12-23 07:56:15 +00:00
|
|
|
QDBusMessage message = QDBusMessage::createMethodCall(QStringLiteral("org.kde.KWin"),
|
|
|
|
QStringLiteral("/KWin"),
|
|
|
|
QStringLiteral("org.kde.KWin"),
|
|
|
|
QStringLiteral("getWindowInfo"));
|
|
|
|
message.setArguments({uuid.toString()});
|
|
|
|
QDBusPendingReply<QVariantMap> async = QDBusConnection::sessionBus().asyncCall(message);
|
|
|
|
|
|
|
|
QDBusPendingCallWatcher *callWatcher = new QDBusPendingCallWatcher(async, &app);
|
|
|
|
QObject::connect(callWatcher, &QDBusPendingCallWatcher::finished, &app,
|
|
|
|
[&whole_app] (QDBusPendingCallWatcher *self) {
|
|
|
|
QDBusPendingReply<QVariantMap> reply = *self;
|
|
|
|
self->deleteLater();
|
|
|
|
if (!reply.isValid() || reply.value().isEmpty()) {
|
|
|
|
qApp->quit();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
KWin::edit(reply.value(), whole_app);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return app.exec();
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|