Messagelib

dkimmanagerrules.cpp
1 /*
2  SPDX-FileCopyrightText: 2019-2021 Laurent Montel <[email protected]>
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 
12 using namespace MessageViewer;
13 DKIMManagerRules::DKIMManagerRules(QObject *parent)
14  : QObject(parent)
15 {
16  (void)loadRules();
17 }
18 
19 DKIMManagerRules::~DKIMManagerRules()
20 {
21  save();
22 }
23 
24 DKIMManagerRules *DKIMManagerRules::self()
25 {
26  static DKIMManagerRules s_self;
27  return &s_self;
28 }
29 
30 void DKIMManagerRules::addRule(const DKIMRule &rule)
31 {
32  if (!mRules.contains(rule)) {
33  mRules.append(rule);
34  save();
35  }
36 }
37 
38 bool DKIMManagerRules::isEmpty() const
39 {
40  return mRules.isEmpty();
41 }
42 
43 QVector<DKIMRule> DKIMManagerRules::rules() const
44 {
45  return mRules;
46 }
47 
48 QStringList DKIMManagerRules::ruleGroups(const KSharedConfig::Ptr &config) const
49 {
50  return config->groupList().filter(QRegularExpression(QStringLiteral("DKIM Rule #\\d+")));
51 }
52 
53 int 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()) {
81  numberOfRulesAdded++;
82  mRules.append(rule);
83  }
84  }
85  return numberOfRulesAdded;
86 }
87 
88 void DKIMManagerRules::saveRules(const QVector<DKIMRule> &lst)
89 {
90  mRules = lst;
91  save();
92 }
93 
94 void DKIMManagerRules::clear()
95 {
96  mRules.clear();
97  save();
98 }
99 
100 int DKIMManagerRules::importRules(const QString &fileName)
101 {
102  return loadRules(fileName);
103 }
104 
105 void DKIMManagerRules::exportRules(const QString &fileName, const QVector<DKIMRule> &lst)
106 {
107  save(fileName, lst);
108 }
109 
110 void DKIMManagerRules::save(const QString &fileName, const QVector<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  QVector<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 }
void writeEntry(const QString &key, const QVariant &value, WriteConfigFlags pFlags=Normal)
QStringList filter(QStringView str, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
The DKIMManagerRules class.
const QList< QKeySequence > & save()
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
KConfigGroup group(const QString &group)
const T & at(int i) const const
bool isEmpty() const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
int count(const T &value) const const
T readEntry(const QString &key, const T &aDefault) const
The DKIMRule class.
Definition: dkimrule.h:17
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Dec 6 2021 23:04:56 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.