File indexing completed on 2024-11-24 04:43:06
0001 /* 0002 SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "confirmbeforedeletingmanager.h" 0008 #include <KConfigGroup> 0009 #include <QRegularExpression> 0010 0011 ConfirmBeforeDeletingManager::ConfirmBeforeDeletingManager(QObject *parent) 0012 : QObject(parent) 0013 { 0014 loadRules(); 0015 } 0016 0017 ConfirmBeforeDeletingManager::~ConfirmBeforeDeletingManager() = default; 0018 0019 ConfirmBeforeDeletingManager *ConfirmBeforeDeletingManager::self() 0020 { 0021 static ConfirmBeforeDeletingManager s_self; 0022 return &s_self; 0023 } 0024 0025 QString ConfirmBeforeDeletingManager::defaultConfigFileName() const 0026 { 0027 return QStringLiteral("confirmbeforedeletingrc"); 0028 } 0029 0030 void ConfirmBeforeDeletingManager::loadRules() 0031 { 0032 const KSharedConfig::Ptr &config = KSharedConfig::openConfig(defaultConfigFileName(), KConfig::NoGlobals); 0033 const QStringList rulesGroups = ruleGroups(config); 0034 0035 mRules.clear(); 0036 for (const QString &groupName : rulesGroups) { 0037 ConfirmBeforeDeletingRule r; 0038 KConfigGroup group = config->group(groupName); 0039 r.load(group); 0040 if (r.isValid()) { 0041 mRules.append(r); 0042 } 0043 } 0044 } 0045 0046 QString defaultGroupName() 0047 { 0048 return QStringLiteral("Confirm Deleting Rule"); 0049 } 0050 0051 QStringList ConfirmBeforeDeletingManager::ruleGroups(const KSharedConfig::Ptr &config) const 0052 { 0053 return config->groupList().filter(QRegularExpression(defaultGroupName() + QStringLiteral(" #\\d+"))); 0054 } 0055 0056 void ConfirmBeforeDeletingManager::setRules(const QList<ConfirmBeforeDeletingRule> &newRules) 0057 { 0058 mRules = newRules; 0059 } 0060 0061 const QList<ConfirmBeforeDeletingRule> &ConfirmBeforeDeletingManager::rules() const 0062 { 0063 return mRules; 0064 } 0065 0066 void ConfirmBeforeDeletingManager::saveRules() 0067 { 0068 const KSharedConfig::Ptr &config = KSharedConfig::openConfig(defaultConfigFileName(), KConfig::NoGlobals); 0069 const QStringList rulesGroups = ruleGroups(config); 0070 0071 for (const QString &group : rulesGroups) { 0072 config->deleteGroup(group); 0073 } 0074 for (int i = 0, total = mRules.count(); i < total; ++i) { 0075 const QString groupName = defaultGroupName() + QStringLiteral(" #%1").arg(i); 0076 KConfigGroup group = config->group(groupName); 0077 const ConfirmBeforeDeletingRule &rule = mRules.at(i); 0078 rule.save(group); 0079 } 0080 } 0081 0082 bool ConfirmBeforeDeletingManager::deletingNeedToConfirm(const Akonadi::Item &item, QString &checkFound, ConfirmBeforeDeletingRule &rule) const 0083 { 0084 for (const auto &r : std::as_const(mRules)) { 0085 if (r.deletingNeedToConfirm(item, checkFound)) { 0086 rule = r; 0087 return true; 0088 } 0089 } 0090 return false; 0091 } 0092 0093 #include "moc_confirmbeforedeletingmanager.cpp"