KWin
Loading...
Searching...
No Matches
rulebooksettings.cpp
Go to the documentation of this file.
1/*
2 KWin - the KDE window manager
3 This file is part of the KDE project.
4
5 SPDX-FileCopyrightText: 2020 Henri Chain <henri.chain@enioka.com>
6 SPDX-FileCopyrightText: 2021 Ismael Asensio <isma.af@gmail.com>
7
8 SPDX-License-Identifier: GPL-2.0-or-later
9*/
10
11#include "rulebooksettings.h"
12#include "rulesettings.h"
13
14#include <QUuid>
15
16namespace KWin
17{
18RuleBookSettings::RuleBookSettings(KSharedConfig::Ptr config, QObject *parent)
19 : RuleBookSettingsBase(config, parent)
20{
21}
22
23RuleBookSettings::RuleBookSettings(const QString &configname, KConfig::OpenFlags flags, QObject *parent)
24 : RuleBookSettings(KSharedConfig::openConfig(configname, flags), parent)
25{
26}
27
28RuleBookSettings::RuleBookSettings(KConfig::OpenFlags flags, QObject *parent)
29 : RuleBookSettings(QStringLiteral("kwinrulesrc"), flags, parent)
30{
31}
32
34 : RuleBookSettings(KConfig::FullConfig, parent)
35{
36}
37
39{
40 qDeleteAll(m_list);
41}
42
43void RuleBookSettings::setRules(const QList<Rules *> &rules)
44{
45 mCount = rules.count();
46 mRuleGroupList.clear();
47 mRuleGroupList.reserve(rules.count());
48
49 int i = 0;
50 const int list_length = m_list.length();
51 for (const auto &rule : rules) {
52 RuleSettings *settings;
53 if (i < list_length) {
54 // Optimization. Reuse RuleSettings already created
55 settings = m_list.at(i);
56 settings->setDefaults();
57 } else {
58 // If there are more rules than in cache
59 settings = new RuleSettings(this->sharedConfig(), QString::number(i + 1), this);
60 m_list.append(settings);
61 }
62
63 rule->write(settings);
64 mRuleGroupList.append(settings->currentGroup());
65
66 i++;
67 }
68
69 for (int j = m_list.count() - 1; j >= rules.count(); j--) {
70 delete m_list[j];
71 m_list.removeAt(j);
72 }
73}
74
76{
77 QList<Rules *> result;
78 result.reserve(m_list.count());
79 for (const auto &settings : std::as_const(m_list)) {
80 result.append(new Rules(settings));
81 }
82 return result;
83}
84
86{
87 bool result = true;
88 for (const auto &settings : std::as_const(m_list)) {
89 result &= settings->save();
90 }
91
92 // Remove deleted groups from config
93 for (const QString &groupName : std::as_const(m_storedGroups)) {
94 if (sharedConfig()->hasGroup(groupName) && !mRuleGroupList.contains(groupName)) {
95 sharedConfig()->deleteGroup(groupName);
96 }
97 }
98 m_storedGroups = mRuleGroupList;
99
100 return result;
101}
102
104{
105 qDeleteAll(m_list);
106 m_list.clear();
107
108 // Legacy path for backwards compatibility with older config files without a rules list
109 if (mRuleGroupList.isEmpty() && mCount > 0) {
110 mRuleGroupList.reserve(mCount);
111 for (int i = 1; i <= count(); i++) {
112 mRuleGroupList.append(QString::number(i));
113 }
114 save(); // Save the generated ruleGroupList property
115 }
116
117 mCount = mRuleGroupList.count();
118 m_storedGroups = mRuleGroupList;
119
120 m_list.reserve(mRuleGroupList.count());
121 for (const QString &groupName : std::as_const(mRuleGroupList)) {
122 m_list.append(new RuleSettings(sharedConfig(), groupName, this));
123 }
124}
125
127{
128 return isSaveNeeded() || std::any_of(m_list.cbegin(), m_list.cend(), [](const auto &settings) {
129 return settings->isSaveNeeded();
130 });
131}
132
134{
135 return m_list.count();
136}
137
138RuleSettings *RuleBookSettings::ruleSettingsAt(int row) const
139{
140 Q_ASSERT(row >= 0 && row < m_list.count());
141 return m_list.at(row);
142}
143
145{
146 Q_ASSERT(row >= 0 && row < m_list.count() + 1);
147
148 const QString groupName = generateGroupName();
149 RuleSettings *settings = new RuleSettings(sharedConfig(), groupName, this);
150 settings->setDefaults();
151
152 m_list.insert(row, settings);
153 mRuleGroupList.insert(row, groupName);
154 mCount++;
155
156 return settings;
157}
158
160{
161 Q_ASSERT(row >= 0 && row < m_list.count());
162
163 delete m_list.at(row);
164 m_list.removeAt(row);
165 mRuleGroupList.removeAt(row);
166 mCount--;
167}
168
169void RuleBookSettings::moveRuleSettings(int srcRow, int destRow)
170{
171 Q_ASSERT(srcRow >= 0 && srcRow < m_list.count() && destRow >= 0 && destRow < m_list.count());
172
173 m_list.insert(destRow, m_list.takeAt(srcRow));
174 mRuleGroupList.insert(destRow, mRuleGroupList.takeAt(srcRow));
175}
176
177QString RuleBookSettings::generateGroupName()
178{
179 return QUuid::createUuid().toString(QUuid::WithoutBraces);
180}
181}
RuleBookSettings(KSharedConfig::Ptr config, QObject *parent=nullptr)
void setRules(const QList< Rules * > &)
RuleSettings * ruleSettingsAt(int row) const
void moveRuleSettings(int srcRow, int destRow)
RuleSettings * insertRuleSettingsAt(int row)
QList< Rules * > rules()