Messagelib

dkimmanagerkey.cpp
1/*
2 SPDX-FileCopyrightText: 2018-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "dkimmanagerkey.h"
8#include "dkimutil.h"
9#include <KConfig>
10#include <KConfigGroup>
11#include <QRegularExpression>
12
13using namespace MessageViewer;
14DKIMManagerKey::DKIMManagerKey(QObject *parent)
15 : QObject(parent)
16{
17 loadKeys();
18}
19
20DKIMManagerKey::~DKIMManagerKey()
21{
22 saveKeys();
23}
24
25DKIMManagerKey *DKIMManagerKey::self()
26{
27 static DKIMManagerKey s_self;
28 return &s_self;
29}
30
31QString DKIMManagerKey::keyValue(const QString &selector, const QString &domain)
32{
33 for (const KeyInfo &keyInfo : std::as_const(mKeys)) {
34 if (keyInfo.selector == selector && keyInfo.domain == domain) {
35 return keyInfo.keyValue;
36 }
37 }
38 return {};
39}
40
41void DKIMManagerKey::updateLastUsed(const QString &selector, const QString &domain)
42{
43 for (const KeyInfo &keyInfo : std::as_const(mKeys)) {
44 if (keyInfo.selector == selector && keyInfo.domain == domain) {
45 KeyInfo newKey = keyInfo;
46 newKey.lastUsedDateTime = QDateTime::currentDateTime();
47 addKey(newKey);
48 return;
49 }
50 }
51}
52
53void DKIMManagerKey::addKey(const KeyInfo &key)
54{
55 const QList<KeyInfo> keys = mKeys;
56 for (const KeyInfo &keyInfo : keys) {
57 if (keyInfo.selector == key.selector && keyInfo.domain == key.domain) {
58 mKeys.removeAll(keyInfo);
59 }
60 }
61 mKeys.append(key);
62}
63
64void DKIMManagerKey::removeKey(const QString &key)
65{
66 for (const KeyInfo &keyInfo : std::as_const(mKeys)) {
67 if (keyInfo.keyValue == key) {
68 mKeys.removeAll(keyInfo);
69 break;
70 }
71 }
72}
73
74QList<KeyInfo> DKIMManagerKey::keys() const
75{
76 return mKeys;
77}
78
79QStringList DKIMManagerKey::keyRecorderList(KSharedConfig::Ptr &config) const
80{
81 config = KSharedConfig::openConfig(MessageViewer::DKIMUtil::defaultConfigFileName(), KConfig::NoGlobals);
82 const QStringList keyGroups = config->groupList().filter(QRegularExpression(QStringLiteral("DKIM Key Record #\\d+")));
83 return keyGroups;
84}
85
86void DKIMManagerKey::loadKeys()
87{
88 KSharedConfig::Ptr config;
89 const QStringList keyGroups = keyRecorderList(config);
90
91 mKeys.clear();
92 for (const QString &groupName : keyGroups) {
93 KConfigGroup group = config->group(groupName);
94 const QString selector = group.readEntry(QStringLiteral("Selector"), QString());
95 const QString domain = group.readEntry(QStringLiteral("Domain"), QString());
96 const QString key = group.readEntry(QStringLiteral("Key"), QString());
97 const QDateTime storedAt = QDateTime::fromString(group.readEntry(QStringLiteral("StoredAt"), QString()), Qt::ISODate);
98 QDateTime lastUsed = QDateTime::fromString(group.readEntry(QStringLiteral("LastUsed"), QString()), Qt::ISODate);
99 if (!lastUsed.isValid()) {
100 lastUsed = storedAt;
101 }
102 mKeys.append(KeyInfo{key, selector, domain, storedAt, lastUsed});
103 }
104}
105
106void DKIMManagerKey::saveKeys()
107{
108 KSharedConfig::Ptr config;
109 const QStringList filterGroups = keyRecorderList(config);
110
111 for (const QString &group : filterGroups) {
112 config->deleteGroup(group);
113 }
114 for (int i = 0, total = mKeys.count(); i < total; ++i) {
115 const QString groupName = QStringLiteral("DKIM Key Record #%1").arg(i);
116 KConfigGroup group = config->group(groupName);
117 const KeyInfo &info = mKeys.at(i);
118 group.writeEntry(QStringLiteral("Selector"), info.selector);
119 group.writeEntry(QStringLiteral("Domain"), info.domain);
120 group.writeEntry(QStringLiteral("Key"), info.keyValue);
121 group.writeEntry(QStringLiteral("StoredAt"), info.storedAtDateTime.toString(Qt::ISODate));
122 group.writeEntry(QStringLiteral("LastUsed"), info.lastUsedDateTime.toString(Qt::ISODate));
123 }
124}
125
126void DKIMManagerKey::saveKeys(const QList<MessageViewer::KeyInfo> &lst)
127{
128 mKeys = lst;
129 saveKeys();
130}
131
132bool KeyInfo::operator==(const KeyInfo &other) const
133{
134 return keyValue == other.keyValue && selector == other.selector && domain == other.domain;
135}
136
137bool KeyInfo::operator!=(const KeyInfo &other) const
138{
139 return !(operator==(other));
140}
141
142QDebug operator<<(QDebug d, const KeyInfo &t)
143{
144 d << " keyvalue " << t.keyValue;
145 d << " selector " << t.selector;
146 d << " domain " << t.domain;
147 d << " storedAtDateTime " << t.storedAtDateTime;
148 d << " lastUsedDateTime " << t.lastUsedDateTime;
149 return d;
150}
151
152#include "moc_dkimmanagerkey.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 DKIMManagerKey class.
QDebug operator<<(QDebug dbg, const PerceptualColor::MultiSpinBoxSection &value)
QDateTime currentDateTime()
QDateTime fromString(QStringView string, QStringView format, QCalendar cal)
bool isValid() const const
QString toString(QStringView format, QCalendar cal) const const
QString arg(Args &&... args) const const
QStringList filter(QStringView str, Qt::CaseSensitivity cs) const const
The KeyInfo struct.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:07:25 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.