2020-08-02 22:22:19 +00:00
|
|
|
/*
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
2020-02-18 13:52:08 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-FileCopyrightText: 2020 Henri Chain <henri.chain@enioka.com>
|
2021-03-19 18:43:47 +00:00
|
|
|
SPDX-FileCopyrightText: 2021 Ismael Asensio <isma.af@gmail.com>
|
2020-02-18 13:52:08 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2020-02-18 13:52:08 +00:00
|
|
|
|
|
|
|
#include "rulebooksettings.h"
|
|
|
|
#include "rulesettings.h"
|
|
|
|
|
2021-03-19 18:43:47 +00:00
|
|
|
#include <QUuid>
|
|
|
|
|
2020-02-18 13:52:08 +00:00
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
RuleBookSettings::RuleBookSettings(KSharedConfig::Ptr config, QObject *parent)
|
|
|
|
: RuleBookSettingsBase(config, parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-03-19 18:43:47 +00:00
|
|
|
RuleBookSettings::~RuleBookSettings()
|
|
|
|
{
|
|
|
|
qDeleteAll(m_list);
|
|
|
|
}
|
|
|
|
|
2020-02-18 13:52:08 +00:00
|
|
|
void RuleBookSettings::setRules(const QVector<Rules *> &rules)
|
|
|
|
{
|
2021-03-19 18:43:47 +00:00
|
|
|
mCount = rules.count();
|
|
|
|
mRuleGroupList.clear();
|
|
|
|
mRuleGroupList.reserve(rules.count());
|
|
|
|
|
|
|
|
int i = 0;
|
2020-02-18 13:52:08 +00:00
|
|
|
const int list_length = m_list.length();
|
|
|
|
for (const auto &rule : rules) {
|
|
|
|
RuleSettings *settings;
|
2021-03-19 18:43:47 +00:00
|
|
|
if (i < list_length) {
|
|
|
|
// Optimization. Reuse RuleSettings already created
|
|
|
|
settings = m_list.at(i);
|
2020-02-18 13:52:08 +00:00
|
|
|
settings->setDefaults();
|
|
|
|
} else {
|
|
|
|
// If there are more rules than in cache
|
2021-03-19 18:43:47 +00:00
|
|
|
settings = new RuleSettings(this->sharedConfig(), QString::number(i + 1), this);
|
2020-02-18 13:52:08 +00:00
|
|
|
m_list.append(settings);
|
|
|
|
}
|
2021-03-19 18:43:47 +00:00
|
|
|
|
2020-02-18 13:52:08 +00:00
|
|
|
rule->write(settings);
|
2021-03-19 18:43:47 +00:00
|
|
|
mRuleGroupList.append(settings->currentGroup());
|
|
|
|
|
2020-02-18 13:52:08 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2021-03-19 18:43:47 +00:00
|
|
|
for (int j = m_list.count() - 1; j >= rules.count(); j--) {
|
|
|
|
delete m_list[j];
|
|
|
|
m_list.removeAt(j);
|
|
|
|
}
|
2020-02-18 13:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QVector<Rules *> RuleBookSettings::rules()
|
|
|
|
{
|
|
|
|
QVector<Rules *> result;
|
2021-03-19 18:43:47 +00:00
|
|
|
result.reserve(m_list.count());
|
|
|
|
for (const auto &settings : qAsConst(m_list)) {
|
|
|
|
result.append(new Rules(settings));
|
2020-02-18 13:52:08 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RuleBookSettings::usrSave()
|
|
|
|
{
|
|
|
|
bool result = true;
|
|
|
|
for (const auto &settings : qAsConst(m_list)) {
|
|
|
|
result &= settings->save();
|
|
|
|
}
|
2021-03-19 18:43:47 +00:00
|
|
|
|
|
|
|
// Remove deleted groups from config
|
|
|
|
for (const QString &groupName : qAsConst(m_storedGroups)) {
|
|
|
|
if (sharedConfig()->hasGroup(groupName) && !mRuleGroupList.contains(groupName)) {
|
|
|
|
sharedConfig()->deleteGroup(groupName);
|
|
|
|
}
|
2020-02-18 13:52:08 +00:00
|
|
|
}
|
2021-03-19 18:43:47 +00:00
|
|
|
m_storedGroups = mRuleGroupList;
|
|
|
|
|
2020-02-18 13:52:08 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RuleBookSettings::usrRead()
|
|
|
|
{
|
2021-03-19 18:43:47 +00:00
|
|
|
qDeleteAll(m_list);
|
|
|
|
m_list.clear();
|
|
|
|
|
|
|
|
// Legacy path for backwards compatibility with older config files without a rules list
|
|
|
|
if (mRuleGroupList.isEmpty() && mCount > 0) {
|
|
|
|
mRuleGroupList.reserve(mCount);
|
|
|
|
for (int i = 1; i <= count(); i++) {
|
|
|
|
mRuleGroupList.append(QString::number(i));
|
2020-02-18 13:52:08 +00:00
|
|
|
}
|
2021-03-19 18:43:47 +00:00
|
|
|
save(); // Save the generated ruleGroupList property
|
2020-02-18 13:52:08 +00:00
|
|
|
}
|
2021-03-19 18:43:47 +00:00
|
|
|
|
|
|
|
mCount = mRuleGroupList.count();
|
|
|
|
m_storedGroups = mRuleGroupList;
|
|
|
|
|
|
|
|
m_list.reserve(mRuleGroupList.count());
|
|
|
|
for (const QString &groupName : qAsConst(mRuleGroupList)) {
|
|
|
|
m_list.append(new RuleSettings(sharedConfig(), groupName, this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RuleBookSettings::usrIsSaveNeeded() const
|
|
|
|
{
|
|
|
|
return isSaveNeeded() || std::any_of(m_list.cbegin(), m_list.cend(), [](const auto &settings) {
|
|
|
|
return settings->isSaveNeeded();
|
|
|
|
});
|
2020-02-18 13:52:08 +00:00
|
|
|
}
|
|
|
|
|
2021-03-19 18:43:47 +00:00
|
|
|
int RuleBookSettings::ruleCount() const
|
|
|
|
{
|
|
|
|
return m_list.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
RuleSettings *RuleBookSettings::ruleSettingsAt(int row) const
|
|
|
|
{
|
|
|
|
Q_ASSERT(row >= 0 && row < m_list.count());
|
|
|
|
return m_list.at(row);
|
|
|
|
}
|
|
|
|
|
|
|
|
RuleSettings *RuleBookSettings::insertRuleSettingsAt(int row)
|
|
|
|
{
|
|
|
|
Q_ASSERT(row >= 0 && row < m_list.count() + 1);
|
|
|
|
|
|
|
|
const QString groupName = generateGroupName();
|
|
|
|
RuleSettings *settings = new RuleSettings(sharedConfig(), groupName, this);
|
|
|
|
settings->setDefaults();
|
|
|
|
|
|
|
|
m_list.insert(row, settings);
|
|
|
|
mRuleGroupList.insert(row, groupName);
|
|
|
|
mCount++;
|
|
|
|
|
|
|
|
return settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RuleBookSettings::removeRuleSettingsAt(int row)
|
|
|
|
{
|
|
|
|
Q_ASSERT(row >= 0 && row < m_list.count());
|
|
|
|
|
|
|
|
delete m_list.at(row);
|
|
|
|
m_list.removeAt(row);
|
|
|
|
mRuleGroupList.removeAt(row);
|
|
|
|
mCount--;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RuleBookSettings::moveRuleSettings(int srcRow, int destRow)
|
|
|
|
{
|
|
|
|
Q_ASSERT(srcRow >= 0 && srcRow < m_list.count() && destRow >= 0 && destRow < m_list.count());
|
|
|
|
|
|
|
|
m_list.insert(destRow, m_list.takeAt(srcRow));
|
|
|
|
mRuleGroupList.insert(destRow, mRuleGroupList.takeAt(srcRow));
|
|
|
|
}
|
|
|
|
|
|
|
|
QString RuleBookSettings::generateGroupName()
|
|
|
|
{
|
|
|
|
return QUuid::createUuid().toString(QUuid::WithoutBraces);
|
|
|
|
}
|
2020-02-18 13:52:08 +00:00
|
|
|
}
|