File indexing completed on 2024-02-25 17:23:50

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText: 2020 Henri Chain <henri.chain@enioka.com>
0006     SPDX-FileCopyrightText: 2021 Ismael Asensio <isma.af@gmail.com>
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #include "rulebooksettings.h"
0012 #include "rulesettings.h"
0013 
0014 #include <QUuid>
0015 
0016 namespace KWin
0017 {
0018 RuleBookSettings::RuleBookSettings(KSharedConfig::Ptr config, QObject *parent)
0019     : RuleBookSettingsBase(config, parent)
0020 {
0021 }
0022 
0023 RuleBookSettings::RuleBookSettings(const QString &configname, KConfig::OpenFlags flags, QObject *parent)
0024     : RuleBookSettings(KSharedConfig::openConfig(configname, flags), parent)
0025 {
0026 }
0027 
0028 RuleBookSettings::RuleBookSettings(KConfig::OpenFlags flags, QObject *parent)
0029     : RuleBookSettings(QStringLiteral("kwinrulesrc"), flags, parent)
0030 {
0031 }
0032 
0033 RuleBookSettings::RuleBookSettings(QObject *parent)
0034     : RuleBookSettings(KConfig::FullConfig, parent)
0035 {
0036 }
0037 
0038 RuleBookSettings::~RuleBookSettings()
0039 {
0040     qDeleteAll(m_list);
0041 }
0042 
0043 void RuleBookSettings::setRules(const QVector<Rules *> &rules)
0044 {
0045     mCount = rules.count();
0046     mRuleGroupList.clear();
0047     mRuleGroupList.reserve(rules.count());
0048 
0049     int i = 0;
0050     const int list_length = m_list.length();
0051     for (const auto &rule : rules) {
0052         RuleSettings *settings;
0053         if (i < list_length) {
0054             // Optimization. Reuse RuleSettings already created
0055             settings = m_list.at(i);
0056             settings->setDefaults();
0057         } else {
0058             // If there are more rules than in cache
0059             settings = new RuleSettings(this->sharedConfig(), QString::number(i + 1), this);
0060             m_list.append(settings);
0061         }
0062 
0063         rule->write(settings);
0064         mRuleGroupList.append(settings->currentGroup());
0065 
0066         i++;
0067     }
0068 
0069     for (int j = m_list.count() - 1; j >= rules.count(); j--) {
0070         delete m_list[j];
0071         m_list.removeAt(j);
0072     }
0073 }
0074 
0075 QVector<Rules *> RuleBookSettings::rules()
0076 {
0077     QVector<Rules *> result;
0078     result.reserve(m_list.count());
0079     for (const auto &settings : std::as_const(m_list)) {
0080         result.append(new Rules(settings));
0081     }
0082     return result;
0083 }
0084 
0085 bool RuleBookSettings::usrSave()
0086 {
0087     bool result = true;
0088     for (const auto &settings : std::as_const(m_list)) {
0089         result &= settings->save();
0090     }
0091 
0092     // Remove deleted groups from config
0093     for (const QString &groupName : std::as_const(m_storedGroups)) {
0094         if (sharedConfig()->hasGroup(groupName) && !mRuleGroupList.contains(groupName)) {
0095             sharedConfig()->deleteGroup(groupName);
0096         }
0097     }
0098     m_storedGroups = mRuleGroupList;
0099 
0100     return result;
0101 }
0102 
0103 void RuleBookSettings::usrRead()
0104 {
0105     qDeleteAll(m_list);
0106     m_list.clear();
0107 
0108     // Legacy path for backwards compatibility with older config files without a rules list
0109     if (mRuleGroupList.isEmpty() && mCount > 0) {
0110         mRuleGroupList.reserve(mCount);
0111         for (int i = 1; i <= count(); i++) {
0112             mRuleGroupList.append(QString::number(i));
0113         }
0114         save(); // Save the generated ruleGroupList property
0115     }
0116 
0117     mCount = mRuleGroupList.count();
0118     m_storedGroups = mRuleGroupList;
0119 
0120     m_list.reserve(mRuleGroupList.count());
0121     for (const QString &groupName : std::as_const(mRuleGroupList)) {
0122         m_list.append(new RuleSettings(sharedConfig(), groupName, this));
0123     }
0124 }
0125 
0126 bool RuleBookSettings::usrIsSaveNeeded() const
0127 {
0128     return isSaveNeeded() || std::any_of(m_list.cbegin(), m_list.cend(), [](const auto &settings) {
0129                return settings->isSaveNeeded();
0130            });
0131 }
0132 
0133 int RuleBookSettings::ruleCount() const
0134 {
0135     return m_list.count();
0136 }
0137 
0138 RuleSettings *RuleBookSettings::ruleSettingsAt(int row) const
0139 {
0140     Q_ASSERT(row >= 0 && row < m_list.count());
0141     return m_list.at(row);
0142 }
0143 
0144 RuleSettings *RuleBookSettings::insertRuleSettingsAt(int row)
0145 {
0146     Q_ASSERT(row >= 0 && row < m_list.count() + 1);
0147 
0148     const QString groupName = generateGroupName();
0149     RuleSettings *settings = new RuleSettings(sharedConfig(), groupName, this);
0150     settings->setDefaults();
0151 
0152     m_list.insert(row, settings);
0153     mRuleGroupList.insert(row, groupName);
0154     mCount++;
0155 
0156     return settings;
0157 }
0158 
0159 void RuleBookSettings::removeRuleSettingsAt(int row)
0160 {
0161     Q_ASSERT(row >= 0 && row < m_list.count());
0162 
0163     delete m_list.at(row);
0164     m_list.removeAt(row);
0165     mRuleGroupList.removeAt(row);
0166     mCount--;
0167 }
0168 
0169 void RuleBookSettings::moveRuleSettings(int srcRow, int destRow)
0170 {
0171     Q_ASSERT(srcRow >= 0 && srcRow < m_list.count() && destRow >= 0 && destRow < m_list.count());
0172 
0173     m_list.insert(destRow, m_list.takeAt(srcRow));
0174     mRuleGroupList.insert(destRow, mRuleGroupList.takeAt(srcRow));
0175 }
0176 
0177 QString RuleBookSettings::generateGroupName()
0178 {
0179     return QUuid::createUuid().toString(QUuid::WithoutBraces);
0180 }
0181 }