Commit 7c1c9d81 authored by Henri Chain's avatar Henri Chain

Port kwinrules kcm to kconfigxt

Reviewers: meven, crossi, bport, ervin, #kwin

Subscribers: iasensio, ognarb, zzag, kwin

Tags: #kwin

Differential Revision: https://phabricator.kde.org/D27682
parent ede53f73
......@@ -466,6 +466,7 @@ set(kwin_SRCS
popup_input_filter.cpp
rootinfo_filter.cpp
rules.cpp
rulebooksettings.cpp
scene.cpp
screenedge.cpp
screenlockerwatcher.cpp
......@@ -556,6 +557,8 @@ endif()
kconfig_add_kcfg_files(kwin_SRCS settings.kcfgc)
kconfig_add_kcfg_files(kwin_SRCS colorcorrection/colorcorrect_settings.kcfgc)
kconfig_add_kcfg_files(kwin_SRCS rulesettings.kcfgc)
kconfig_add_kcfg_files(kwin_SRCS rulebooksettingsbase.kcfgc)
qt5_add_dbus_adaptor(kwin_SRCS org.kde.KWin.xml dbusinterface.h KWin::DBusInterface)
qt5_add_dbus_adaptor(kwin_SRCS org.kde.kwin.Compositing.xml dbusinterface.h KWin::CompositorDBusInterface)
......
......@@ -6,10 +6,13 @@ add_definitions(-DKCMRULES)
include_directories(../../)
set(kwinrules_MOC_HDRS yesnobox.h ../../cursor.h ../../plugins/platforms/x11/standalone/x11cursor.h)
qt5_wrap_cpp(kwinrules_MOC_SRCS ${kwinrules_MOC_HDRS})
set(kwinrules_SRCS ruleswidget.cpp ruleslist.cpp kwinsrc.cpp detectwidget.cpp main.cpp ${kwinrules_MOC_SRCS})
set(kwinrules_SRCS ../../rulebooksettings.cpp ruleswidget.cpp ruleslist.cpp kwinsrc.cpp detectwidget.cpp main.cpp ${kwinrules_MOC_SRCS})
ki18n_wrap_ui(kwinrules_SRCS ruleslist.ui detectwidget.ui editshortcut.ui ruleswidgetbase.ui)
kconfig_add_kcfg_files(kwinrules_SRCS ../../rulesettings.kcfgc)
kconfig_add_kcfg_files(kwinrules_SRCS ../../rulebooksettingsbase.kcfgc)
add_executable(kwin_rules_dialog ${kwinrules_SRCS})
set(kwin_kcm_rules_XCB_LIBS
......
......@@ -39,7 +39,6 @@ namespace KWin
KCMRules::KCMRules(QWidget *parent, const QVariantList &)
: KCModule(parent)
, config("kwinrulesrc", KConfig::NoGlobals)
{
QVBoxLayout *layout = new QVBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
......@@ -56,7 +55,6 @@ KCMRules::KCMRules(QWidget *parent, const QVariantList &)
void KCMRules::load()
{
config.reparseConfiguration();
widget->load();
emit KCModule::changed(false);
}
......@@ -65,8 +63,6 @@ void KCMRules::save()
{
widget->save();
emit KCModule::changed(false);
// Send signal to kwin
config.sync();
// Send signal to all kwin instances
QDBusMessage message =
QDBusMessage::createSignal("/KWin", "org.kde.KWin", "reloadConfig");
......
......@@ -44,7 +44,6 @@ protected Q_SLOTS:
void moduleChanged(bool state);
private:
KCMRulesList* widget;
KConfig config;
};
} // namespace
......
......@@ -19,7 +19,6 @@
// Include some code from kwin core in order to avoid
// double implementation.
#include "ruleslist.h"
#include "../../cursor.cpp"
#include "../../plugins/platforms/x11/standalone/x11cursor.cpp"
#include "../../rules.cpp"
......
......@@ -23,6 +23,7 @@
#include <kwindowsystem.h>
#include "ruleswidget.h"
#include "rulebooksettings.h"
#include "../../rules.h"
#include <QByteArray>
......@@ -37,40 +38,7 @@ Q_DECLARE_METATYPE(NET::WindowType)
namespace KWin
{
static void loadRules(QList< Rules* >& rules)
{
KConfig _cfg("kwinrulesrc", KConfig::NoGlobals);
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);
}
}
static void saveRules(const QList< Rules* >& rules)
{
KConfig cfg("kwinrulesrc", KConfig::NoGlobals);
QStringList groups = cfg.groupList();
for (QStringList::ConstIterator it = groups.constBegin();
it != groups.constEnd();
++it)
cfg.deleteGroup(*it);
cfg.group("General").writeEntry("count", rules.count());
int i = 1;
for (QList< Rules* >::ConstIterator it = rules.constBegin();
it != rules.constEnd();
++it) {
KConfigGroup cg(&cfg, QString::number(i));
(*it)->write(cg);
++i;
}
}
static Rules* findRule(const QList< Rules* >& rules, const QVariantMap &data, bool whole_app)
static Rules *findRule(const QVector<Rules *> &rules, const QVariantMap &data, bool whole_app)
{
QByteArray wmclass_class = data.value("resourceClass").toByteArray().toLower();
QByteArray wmclass_name = data.value("resourceName").toByteArray().toLower();
......@@ -80,11 +48,8 @@ static Rules* findRule(const QList< Rules* >& rules, const QVariantMap &data, bo
QByteArray machine = data.value("clientMachine").toByteArray();
Rules* best_match = nullptr;
int match_quality = 0;
for (QList< Rules* >::ConstIterator it = rules.constBegin();
it != rules.constEnd();
++it) {
for (const auto rule : rules) {
// try to find an exact match, i.e. not a generic rule
Rules* rule = *it;
int quality = 0;
bool generic = true;
if (rule->wmclassmatch != Rules::ExactMatch)
......@@ -202,9 +167,9 @@ static Rules* findRule(const QList< Rules* >& rules, const QVariantMap &data, bo
static void edit(const QVariantMap &data, bool whole_app)
{
QList< Rules* > rules;
loadRules(rules);
Rules* orig_rule = findRule(rules, data, whole_app);
RuleBookSettings settings(KConfig::NoGlobals);
QVector<Rules *> rules = settings.rules();
Rules *orig_rule = findRule(rules, data, whole_app);
RulesDialog dlg;
if (whole_app)
dlg.setWindowTitle(i18nc("Window caption for the application wide rules dialog", "Edit Application-Specific Settings"));
......@@ -223,7 +188,8 @@ static void edit(const QVariantMap &data, bool whole_app)
rules.prepend(edited_rule);
delete orig_rule;
}
saveRules(rules);
settings.setRules(rules);
settings.save();
// Send signal to all kwin instances
QDBusMessage message =
QDBusMessage::createSignal("/KWin", "org.kde.KWin", "reloadConfig");
......
......@@ -22,6 +22,8 @@
#include <kconfig.h>
#include <QFileDialog>
#include "../../rules.h"
#include "rulesettings.h"
#include "ruleswidget.h"
namespace KWin
......@@ -57,11 +59,8 @@ KCMRulesList::KCMRulesList(QWidget* parent)
KCMRulesList::~KCMRulesList()
{
for (QVector< Rules* >::Iterator it = rules.begin();
it != rules.end();
++it)
delete *it;
rules.clear();
qDeleteAll(m_rules);
m_rules.clear();
}
void KCMRulesList::activeChanged()
......@@ -87,7 +86,7 @@ void KCMRulesList::newClicked()
int pos = rules_listbox->currentRow() + 1;
rules_listbox->insertItem(pos , rule->description);
rules_listbox->setCurrentRow(pos, QItemSelectionModel::ClearAndSelect);
rules.insert(rules.begin() + pos, rule);
m_rules.insert(m_rules.begin() + pos, rule);
emit changed(true);
}
......@@ -97,11 +96,11 @@ void KCMRulesList::modifyClicked()
if (pos == -1)
return;
RulesDialog dlg(this);
Rules* rule = dlg.edit(rules[ pos ], {}, false);
if (rule == rules[ pos ])
Rules *rule = dlg.edit(m_rules[pos], {}, false);
if (rule == m_rules[pos])
return;
delete rules[ pos ];
rules[ pos ] = rule;
delete m_rules[pos];
m_rules[pos] = rule;
rules_listbox->item(pos)->setText(rule->description);
emit changed(true);
}
......@@ -111,7 +110,7 @@ void KCMRulesList::deleteClicked()
int pos = rules_listbox->currentRow();
Q_ASSERT(pos != -1);
delete rules_listbox->takeItem(pos);
rules.erase(rules.begin() + pos);
m_rules.erase(m_rules.begin() + pos);
emit changed(true);
}
......@@ -123,9 +122,9 @@ void KCMRulesList::moveupClicked()
QListWidgetItem * item = rules_listbox->takeItem(pos);
rules_listbox->insertItem(pos - 1 , item);
rules_listbox->setCurrentItem(item, QItemSelectionModel::ClearAndSelect);
Rules* rule = rules[ pos ];
rules[ pos ] = rules[ pos - 1 ];
rules[ pos - 1 ] = rule;
Rules *rule = m_rules[pos];
m_rules[pos] = m_rules[pos - 1];
m_rules[pos - 1] = rule;
}
emit changed(true);
}
......@@ -138,9 +137,9 @@ void KCMRulesList::movedownClicked()
QListWidgetItem * item = rules_listbox->takeItem(pos);
rules_listbox->insertItem(pos + 1 , item);
rules_listbox->setCurrentItem(item, QItemSelectionModel::ClearAndSelect);
Rules* rule = rules[ pos ];
rules[ pos ] = rules[ pos + 1 ];
rules[ pos + 1 ] = rule;
Rules *rule = m_rules[pos];
m_rules[pos] = m_rules[pos + 1];
m_rules[pos + 1] = rule;
}
emit changed(true);
}
......@@ -153,10 +152,11 @@ void KCMRulesList::exportClicked()
i18n("KWin Rules (*.kwinrule)"));
if (path.isEmpty())
return;
KConfig config(path, KConfig::SimpleConfig);
KConfigGroup group(&config, rules[pos]->description);
group.deleteGroup();
rules[pos]->write(group);
const auto cfg = KSharedConfig::openConfig(path, KConfig::SimpleConfig);
RuleSettings settings(cfg, m_rules[pos]->description);
settings.setDefaults();
m_rules[pos]->write(&settings);
settings.save();
}
void KCMRulesList::importClicked()
......@@ -165,29 +165,28 @@ void KCMRulesList::importClicked()
i18n("KWin Rules (*.kwinrule)"));
if (path.isEmpty())
return;
KConfig config(path, KConfig::SimpleConfig);
QStringList groups = config.groupList();
const auto config = KSharedConfig::openConfig(path, KConfig::SimpleConfig);
QStringList groups = config->groupList();
if (groups.isEmpty())
return;
int pos = qMax(0, rules_listbox->currentRow());
foreach (const QString &group, groups) {
KConfigGroup grp(&config, group);
const bool remove = grp.readEntry("DeleteRule", false);
Rules* new_rule = new Rules(grp);
for (const QString &group : groups) {
RuleSettings settings(config, group);
const bool remove = settings.deleteRule();
Rules *new_rule = new Rules(&settings);
// try to replace existing rule first
for (int i = 0; i < rules.count(); ++i) {
if (rules[i]->description == new_rule->description) {
delete rules[i];
for (int i = 0; i < m_rules.count(); ++i) {
if (m_rules[i]->description == new_rule->description) {
delete m_rules[i];
if (remove) {
rules.remove(i);
m_rules.remove(i);
delete rules_listbox->takeItem(i);
delete new_rule;
pos = qMax(0, rules_listbox->currentRow()); // might have changed!
}
else
rules[i] = new_rule;
} else
m_rules[i] = new_rule;
new_rule = nullptr;
break;
}
......@@ -201,7 +200,7 @@ void KCMRulesList::importClicked()
// plain insertion
if (new_rule) {
rules.insert(pos, new_rule);
m_rules.insert(pos, new_rule);
rules_listbox->insertItem(pos++, new_rule->description);
}
}
......@@ -211,24 +210,13 @@ void KCMRulesList::importClicked()
void KCMRulesList::load()
{
rules_listbox->clear();
for (QVector< Rules* >::Iterator it = rules.begin();
it != rules.end();
++it)
delete *it;
rules.clear();
KConfig _cfg("kwinrulesrc");
KConfigGroup cfg(&_cfg, "General");
int count = cfg.readEntry("count", 0);
rules.reserve(count);
for (int i = 1;
i <= count;
++i) {
cfg = KConfigGroup(&_cfg, QString::number(i));
Rules* rule = new Rules(cfg);
rules.append(rule);
m_settings.load();
m_rules = m_settings.rules();
for (const auto rule : qAsConst(m_rules)) {
rules_listbox->addItem(rule->description);
}
if (rules.count() > 0)
if (m_rules.count() > 0)
rules_listbox->setCurrentItem(rules_listbox->item(0));
else
rules_listbox->setCurrentItem(nullptr);
......@@ -237,21 +225,8 @@ void KCMRulesList::load()
void KCMRulesList::save()
{
KConfig cfg(QLatin1String("kwinrulesrc"));
QStringList groups = cfg.groupList();
for (QStringList::ConstIterator it = groups.constBegin();
it != groups.constEnd();
++it)
cfg.deleteGroup(*it);
cfg.group("General").writeEntry("count", rules.count());
int i = 1;
for (QVector< Rules* >::ConstIterator it = rules.constBegin();
it != rules.constEnd();
++it) {
KConfigGroup cg(&cfg, QString::number(i));
(*it)->write(cg);
++i;
}
m_settings.setRules(m_rules);
m_settings.save();
}
void KCMRulesList::defaults()
......
......@@ -20,12 +20,12 @@
#ifndef __RULESLIST_H__
#define __RULESLIST_H__
#include "../../rules.h"
#include "ui_ruleslist.h"
#include "rulebooksettings.h"
namespace KWin
{
class Rules;
class KCMRulesList
: public QWidget, Ui_KCMRulesList
......@@ -49,7 +49,8 @@ private Q_SLOTS:
void importClicked();
void activeChanged();
private:
QVector< Rules* > rules;
QVector<Rules *> m_rules;
RuleBookSettings m_settings;
};
} // namespace
......
/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2020 Henri Chain <henri.chain@enioka.com>
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, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#include "rulebooksettings.h"
#include "rulesettings.h"
namespace KWin
{
RuleBookSettings::RuleBookSettings(KSharedConfig::Ptr config, QObject *parent)
: RuleBookSettingsBase(config, parent)
{
for (int i = 1; i <= count(); i++) {
m_list.append(new RuleSettings(config, QString::number(i), this));
}
}
RuleBookSettings::RuleBookSettings(const QString &configname, KConfig::OpenFlags flags, QObject *parent)
: RuleBookSettings(KSharedConfig::openConfig(configname, flags), parent)
{
}
RuleBookSettings::RuleBookSettings(KConfig::OpenFlags flags, QObject *parent)
: RuleBookSettings(QStringLiteral("kwinrulesrc"), flags, parent)
{
}
RuleBookSettings::RuleBookSettings(QObject *parent)
: RuleBookSettings(KConfig::FullConfig, parent)
{
}
void RuleBookSettings::setRules(const QVector<Rules *> &rules)
{
int i = 1;
const int list_length = m_list.length();
for (const auto &rule : rules) {
RuleSettings *settings;
if (i <= list_length) {
settings = m_list[i - 1];
settings->setDefaults();
} else {
// If there are more rules than in cache
settings = new RuleSettings(this->sharedConfig(), QString::number(i), this);
m_list.append(settings);
}
rule->write(settings);
i++;
}
setCount(rules.length());
}
QVector<Rules *> RuleBookSettings::rules()
{
QVector<Rules *> result;
result.reserve(mCount);
// mCount is always <= m_list.length()
for (int i = 0; i < mCount; i++) {
result.append(new Rules(m_list[i]));
}
return result;
}
bool RuleBookSettings::usrSave()
{
bool result = true;
for (const auto &settings : qAsConst(m_list)) {
result &= settings->save();
}
int nRuleGroups = sharedConfig()->groupList().length() - 1;
// Remove any extra groups currently in config
for (int i = mCount + 1; i <= nRuleGroups; i++) {
sharedConfig()->deleteGroup(QString::number(i));
}
return result;
}
void RuleBookSettings::usrRead()
{
const int list_length = m_list.length();
for (int i = 1; i <= mCount; i++) {
if (i <= list_length) {
m_list[i - 1]->load();
} else {
// If there are more groups than in cache
m_list.append(new RuleSettings(sharedConfig(), QString::number(i), this));
}
}
}
}
/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2020 Henri Chain <henri.chain@enioka.com>
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, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#ifndef RULEBOOKSETTINGS_H
#define RULEBOOKSETTINGS_H
#include "rulebooksettingsbase.h"
#include <KSharedConfig>
namespace KWin
{
class Rules;
class RuleSettings;
class RuleBookSettings : public RuleBookSettingsBase
{
public:
RuleBookSettings(KSharedConfig::Ptr config, QObject *parent = nullptr);
RuleBookSettings(const QString &configname, KConfig::OpenFlags, QObject *parent = nullptr);
RuleBookSettings(KConfig::OpenFlags, QObject *parent = nullptr);
RuleBookSettings(QObject *parent = nullptr);
void setRules(const QVector<Rules *> &);
QVector<Rules *> rules();
bool usrSave() override;
void usrRead() override;
private:
QVector<RuleSettings *> m_list;
};
}
#endif // RULEBOOKSETTINGS_H
<?xml version="1.0" encoding="UTF-8"?>
<kcfg xmlns="http://www.kde.org/standards/kcfg/1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.kde.org/standards/kcfg/1.0
http://www.kde.org/standards/kcfg/1.0/kcfg.xsd">
<kcfgfile arg="true">
</kcfgfile>
<group name="General">
<entry name="count" type="int">
<label>Total rules count</label>
<default>0</default>
</entry>
</group>
</kcfg>
File=rulebooksettingsbase.kcfg
NameSpace=KWin
ClassName=RuleBookSettingsBase
Mutators=true
ParentInConstructor=true
This diff is collapsed.
......@@ -25,7 +25,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <netwm_def.h>
#include <QRect>
#include <QVector>
#include <kconfiggroup.h>
#include "placement.h"
#include "options.h"
......@@ -40,6 +39,7 @@ namespace KWin
class AbstractClient;
class Rules;
class RuleSettings;
#ifndef KCMRULES // only for kwin core
......@@ -101,7 +101,7 @@ class Rules
{
public:
Rules();
explicit Rules(const KConfigGroup&);
explicit Rules(const RuleSettings*);
Rules(const QString&, bool temporary);
enum Type {
Position = 1<<0, Size = 1<<1, Desktop = 1<<2,
......@@ -130,7 +130,15 @@ public:
RegExpMatch,
LastStringMatch = RegExpMatch
};
void write(KConfigGroup&) const;
enum SetRule {
UnusedSetRule = Unused,
SetRuleDummy = 256 // so that it's at least short int
};
enum ForceRule {
UnusedForceRule = Unused,
ForceRuleDummy = 256 // so that it's at least short int
};
void write(RuleSettings*) const;
bool isEmpty() const;
#ifndef KCMRULES
bool discardUsed(bool withdrawn);
......@@ -183,19 +191,9 @@ private:
bool matchRole(const QByteArray& match_role) const;
bool matchTitle(const QString& match_title) const;
bool matchClientMachine(const QByteArray& match_machine, bool local) const;
enum SetRule {
UnusedSetRule = Unused,
SetRuleDummy = 256 // so that it's at least short int
};
enum ForceRule {
UnusedForceRule = Unused,
ForceRuleDummy = 256 // so that it's at least short int
};
void readFromCfg(const KConfigGroup& cfg);
static SetRule readSetRule(const KConfigGroup&, const QString& key);
static ForceRule readForceRule(const KConfigGroup&, const QString& key);
static NET::WindowType readType(const KConfigGroup&, const QString& key);
static QString readDecoColor(const KConfigGroup &cfg);
void readFromSettings(const RuleSettings *settings);
static ForceRule convertForceRule(int v);
static QString getDecoColor(const QString &themeName);
#ifndef KCMRULES
static bool checkSetRule(SetRule rule, bool init);
static bool checkForceRule(ForceRule rule);
......
This diff is collapsed.
File=rulesettings.kcfg
IncludeFiles=\"rules.h\",\"placement.h\",netwm_def.h
NameSpace=KWin
ClassName=RuleSettings
UseEnumTypes=true
Mutators=true
ParentInConstructor=true
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment