dkimmanagerrules.cpp 4.21 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
   Copyright (C) 2019 Laurent Montel <montel@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "dkimmanagerrules.h"
21
#include "dkimutil.h"
Laurent Montel's avatar
Laurent Montel committed
22
23
24
#include <KConfigGroup>
#include <KSharedConfig>
#include <QRegularExpression>
Laurent Montel's avatar
Laurent Montel committed
25

26
27
28
29
using namespace MessageViewer;
DKIMManagerRules::DKIMManagerRules(QObject *parent)
    : QObject(parent)
{
30
    loadRules();
31
32
33
34
}

DKIMManagerRules::~DKIMManagerRules()
{
35
    save();
36
37
38
39
40
41
42
}

DKIMManagerRules *DKIMManagerRules::self()
{
    static DKIMManagerRules s_self;
    return &s_self;
}
43

Laurent Montel's avatar
Laurent Montel committed
44
45
void DKIMManagerRules::addRule(const DKIMRule &rule)
{
Laurent Montel's avatar
Laurent Montel committed
46
47
48
49
    if (!mRules.contains(rule)) {
        mRules.append(rule);
        save();
    }
Laurent Montel's avatar
Laurent Montel committed
50
51
}

52
53
54
55
56
QVector<DKIMRule> DKIMManagerRules::rules() const
{
    return mRules;
}

57
58
59
60
61
QStringList DKIMManagerRules::ruleGroups(const KSharedConfig::Ptr &config) const
{
    return config->groupList().filter(QRegularExpression(QStringLiteral("DKIM Rule #\\d+")));
}

62
void DKIMManagerRules::loadRules(const QString &fileName)
63
{
64
    const KSharedConfig::Ptr &config = KSharedConfig::openConfig(fileName.isEmpty() ? MessageViewer::DKIMUtil::defaultConfigFileName() : fileName, KConfig::NoGlobals);
65
    const QStringList rulesGroups = ruleGroups(config);
Laurent Montel's avatar
Laurent Montel committed
66
67

    mRules.clear();
68
    for (const QString &groupName : rulesGroups) {
Laurent Montel's avatar
Laurent Montel committed
69
70
71
72
73
        KConfigGroup group = config->group(groupName);
        const QStringList signedDomainIdentifier = group.readEntry(QLatin1String("SignedDomainIdentifier"), QStringList());
        const QString from = group.readEntry(QLatin1String("From"), QString());
        const QString domain = group.readEntry(QLatin1String("Domain"), QString());
        const bool enabled = group.readEntry(QLatin1String("Enabled"), true);
Laurent Montel's avatar
Laurent Montel committed
74
75
        const int ruleType = group.readEntry(QLatin1String("RuleType"), 0);
        const QString listId = group.readEntry(QLatin1String("List-Id"), QString());
Laurent Montel's avatar
Laurent Montel committed
76
77
78
79
        DKIMRule rule;
        rule.setEnabled(enabled);
        rule.setDomain(domain);
        rule.setFrom(from);
Laurent Montel's avatar
Laurent Montel committed
80
        rule.setListId(listId);
Laurent Montel's avatar
Laurent Montel committed
81
        rule.setSignedDomainIdentifier(signedDomainIdentifier);
Laurent Montel's avatar
Laurent Montel committed
82
        rule.setRuleType(static_cast<DKIMRule::RuleType>(ruleType));
Laurent Montel's avatar
Laurent Montel committed
83
84
        mRules.append(rule);
    }
85
86
87
88
89
90
91
92
}

void DKIMManagerRules::saveRules(const QVector<DKIMRule> &lst)
{
    mRules = lst;
    save();
}

93
void DKIMManagerRules::clear()
94
{
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
    mRules.clear();
    save();
}

void DKIMManagerRules::importRules(const QString &fileName)
{
    loadRules(fileName);
}

void DKIMManagerRules::exportRules(const QString &fileName)
{
    save(fileName);
}

void DKIMManagerRules::save(const QString &fileName)
{
111
    const KSharedConfig::Ptr &config = KSharedConfig::openConfig(fileName.isEmpty() ? MessageViewer::DKIMUtil::defaultConfigFileName() : fileName, KConfig::NoGlobals);
112
    const QStringList rulesGroups = ruleGroups(config);
Laurent Montel's avatar
Laurent Montel committed
113

114
    for (const QString &group : rulesGroups) {
Laurent Montel's avatar
Laurent Montel committed
115
116
117
        config->deleteGroup(group);
    }
    for (int i = 0, total = mRules.count(); i < total; ++i) {
118
        const QString groupName = QStringLiteral("DKIM Rule #%1").arg(i);
Laurent Montel's avatar
Laurent Montel committed
119
120
121
122
123
124
125
        KConfigGroup group = config->group(groupName);
        const DKIMRule &rule = mRules.at(i);

        group.writeEntry(QLatin1String("SignedDomainIdentifier"), rule.signedDomainIdentifier());
        group.writeEntry(QLatin1String("From"), rule.from());
        group.writeEntry(QLatin1String("Domain"), rule.domain());
        group.writeEntry(QLatin1String("Enabled"), rule.enabled());
Laurent Montel's avatar
Laurent Montel committed
126
127
        group.writeEntry(QLatin1String("RuleType"), static_cast<int>(rule.ruleType()));
        group.writeEntry(QLatin1String("List-Id"), rule.listId());
Laurent Montel's avatar
Laurent Montel committed
128
    }
129
}