Messagelib

dkimmanagerrules.cpp
1/*
2 SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "dkimmanagerrules.h"
8#include "dkimutil.h"
9#include <KConfigGroup>
10#include <QRegularExpression>
11
12using namespace MessageViewer;
13DKIMManagerRules::DKIMManagerRules(QObject *parent)
14 : QObject(parent)
15{
16 (void)loadRules();
17}
18
19DKIMManagerRules::~DKIMManagerRules()
20{
21 save();
22}
23
24DKIMManagerRules *DKIMManagerRules::self()
25{
27 return &s_self;
28}
29
30void DKIMManagerRules::addRule(const DKIMRule &rule)
31{
32 if (!mRules.contains(rule)) {
33 mRules.append(rule);
34 save();
35 }
36}
37
38bool DKIMManagerRules::isEmpty() const
39{
40 return mRules.isEmpty();
41}
42
43QList<DKIMRule> DKIMManagerRules::rules() const
44{
45 return mRules;
46}
47
48QStringList DKIMManagerRules::ruleGroups(const KSharedConfig::Ptr &config) const
49{
50 return config->groupList().filter(QRegularExpression(QStringLiteral("DKIM Rule #\\d+")));
51}
52
53int DKIMManagerRules::loadRules(const QString &fileName)
54{
55 const KSharedConfig::Ptr &config =
56 KSharedConfig::openConfig(fileName.isEmpty() ? MessageViewer::DKIMUtil::defaultConfigFileName() : fileName, KConfig::NoGlobals);
57 const QStringList rulesGroups = ruleGroups(config);
58
59 if (fileName.isEmpty()) {
60 mRules.clear();
61 }
62 int numberOfRulesAdded = 0;
63 for (const QString &groupName : rulesGroups) {
64 KConfigGroup group = config->group(groupName);
65 const QStringList signedDomainIdentifier = group.readEntry(QStringLiteral("SignedDomainIdentifier"), QStringList());
66 const QString from = group.readEntry(QStringLiteral("From"), QString());
67 const QString domain = group.readEntry(QStringLiteral("Domain"), QString());
68 const bool enabled = group.readEntry(QStringLiteral("Enabled"), true);
69 const int ruleType = group.readEntry(QStringLiteral("RuleType"), 0);
70 const QString listId = group.readEntry(QStringLiteral("List-Id"), QString());
71 const int priority = group.readEntry(QStringLiteral("Priority"), 1000);
72 DKIMRule rule;
73 rule.setEnabled(enabled);
74 rule.setDomain(domain);
75 rule.setFrom(from);
76 rule.setListId(listId);
77 rule.setSignedDomainIdentifier(signedDomainIdentifier);
78 rule.setRuleType(static_cast<DKIMRule::RuleType>(ruleType));
79 rule.setPriority(priority);
80 if (rule.isValid()) {
82 mRules.append(rule);
83 }
84 }
85 return numberOfRulesAdded;
86}
87
88void DKIMManagerRules::saveRules(const QList<DKIMRule> &lst)
89{
90 mRules = lst;
91 save();
92}
93
94void DKIMManagerRules::clear()
95{
96 mRules.clear();
97 save();
98}
99
100int DKIMManagerRules::importRules(const QString &fileName)
101{
102 return loadRules(fileName);
103}
104
105void DKIMManagerRules::exportRules(const QString &fileName, const QList<DKIMRule> &lst)
106{
107 save(fileName, lst);
108}
109
110void DKIMManagerRules::save(const QString &fileName, const QList<DKIMRule> &lst)
111{
112 const KSharedConfig::Ptr &config =
113 KSharedConfig::openConfig(fileName.isEmpty() ? MessageViewer::DKIMUtil::defaultConfigFileName() : fileName, KConfig::NoGlobals);
114 const QStringList rulesGroups = ruleGroups(config);
115
116 for (const QString &group : rulesGroups) {
117 config->deleteGroup(group);
118 }
119 QList<DKIMRule> rules = lst;
120 if (lst.isEmpty()) {
121 rules = mRules;
122 }
123 for (int i = 0, total = rules.count(); i < total; ++i) {
124 const QString groupName = QStringLiteral("DKIM Rule #%1").arg(i);
125 KConfigGroup group = config->group(groupName);
126 const DKIMRule &rule = rules.at(i);
127
128 group.writeEntry(QStringLiteral("SignedDomainIdentifier"), rule.signedDomainIdentifier());
129 group.writeEntry(QStringLiteral("From"), rule.from());
130 group.writeEntry(QStringLiteral("Domain"), rule.domain());
131 group.writeEntry(QStringLiteral("Enabled"), rule.enabled());
132 group.writeEntry(QStringLiteral("RuleType"), static_cast<int>(rule.ruleType()));
133 group.writeEntry(QStringLiteral("List-Id"), rule.listId());
134 group.writeEntry(QStringLiteral("Priority"), rule.priority());
135 }
136}
137
138#include "moc_dkimmanagerrules.cpp"
KConfigGroup group(const QString &group)
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
QString readEntry(const char *key, const char *aDefault=nullptr) const
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
The DKIMManagerRules class.
The DKIMRule class.
Definition dkimrule.h:18
const T & at(int i) const const
int count(const T &value) const const
bool isEmpty() const const
T qobject_cast(QObject *object)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
bool isEmpty() const const
QStringList filter(QStringView str, Qt::CaseSensitivity cs) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sun Feb 25 2024 18:37:31 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.