2 Copyright (C) 2013-2016 Montel Laurent <montel@kde.org>
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include "sieveeditorutil.h"
21 #include "sieveserversettings.h"
27 #include <KConfigGroup>
29 #include <QRegularExpression>
31 #include <KSharedConfig>
33 QUrl
SieveEditorUtil::SieveServerConfig::url() const
36 u
.setHost(serverName
);
37 u
.setUserName(userName
);
38 u
.setPassword(password
);
42 switch (authenticationType
) {
43 case MailTransport::Transport::EnumAuthenticationType::CLEAR
:
44 case MailTransport::Transport::EnumAuthenticationType::PLAIN
:
45 authStr
= QStringLiteral("PLAIN");
47 case MailTransport::Transport::EnumAuthenticationType::LOGIN
:
48 authStr
= QStringLiteral("LOGIN");
50 case MailTransport::Transport::EnumAuthenticationType::CRAM_MD5
:
51 authStr
= QStringLiteral("CRAM-MD5");
53 case MailTransport::Transport::EnumAuthenticationType::DIGEST_MD5
:
54 authStr
= QStringLiteral("DIGEST-MD5");
56 case MailTransport::Transport::EnumAuthenticationType::GSSAPI
:
57 authStr
= QStringLiteral("GSSAPI");
59 case MailTransport::Transport::EnumAuthenticationType::ANONYMOUS
:
60 authStr
= QStringLiteral("ANONYMOUS");
63 authStr
= QStringLiteral("PLAIN");
67 query
.addQueryItem(QStringLiteral("x-mech"), authStr
);
72 QVector
<SieveEditorUtil::SieveServerConfig
> SieveEditorUtil::readServerSieveConfig()
74 QVector
<SieveServerConfig
> lstConfig
;
75 KSharedConfigPtr cfg
= KSharedConfig::openConfig();
76 QRegularExpression
re(QStringLiteral("^ServerSieve (.+)$"));
77 const QStringList groups
= cfg
->groupList().filter(re
);
78 KWallet::Wallet
*wallet
= SieveServerSettings::self()->wallet();
79 if (wallet
&& !wallet
->setFolder(QStringLiteral("sieveeditor"))) {
80 wallet
->createFolder(QStringLiteral("sieveeditor"));
81 wallet
->setFolder(QStringLiteral("sieveeditor"));
84 Q_FOREACH (const QString
&conf
, groups
) {
85 SieveServerConfig sieve
;
86 KConfigGroup group
= cfg
->group(conf
);
87 sieve
.port
= group
.readEntry(QStringLiteral("Port"), 0);
88 sieve
.serverName
= group
.readEntry(QStringLiteral("ServerName"));
89 sieve
.userName
= group
.readEntry(QStringLiteral("UserName"));
90 sieve
.enabled
= group
.readEntry(QStringLiteral("Enabled"), true);
91 const QString walletEntry
= sieve
.userName
+ QLatin1Char('@') + sieve
.serverName
;
92 if (wallet
&& wallet
->hasEntry(walletEntry
)) {
93 wallet
->readPassword(walletEntry
, sieve
.password
);
95 sieve
.authenticationType
= static_cast<MailTransport::Transport::EnumAuthenticationType::type
>(group
.readEntry(QStringLiteral("Authentication"), static_cast<int>(MailTransport::Transport::EnumAuthenticationType::PLAIN
)));
96 lstConfig
.append(sieve
);
101 void SieveEditorUtil::writeServerSieveConfig(const QVector
<SieveServerConfig
> &lstConfig
)
103 KSharedConfigPtr cfg
= KSharedConfig::openConfig();
104 const QRegularExpression
re(QStringLiteral("^ServerSieve (.+)$"));
106 const QStringList groups
= cfg
->groupList().filter(re
);
107 Q_FOREACH (const QString
&conf
, groups
) {
108 KConfigGroup group
= cfg
->group(conf
);
113 KWallet::Wallet
*wallet
= SieveServerSettings::self()->wallet();
115 if (!wallet
->hasFolder(QStringLiteral("sieveeditor"))) {
116 wallet
->createFolder(QStringLiteral("sieveeditor"));
118 wallet
->setFolder(QStringLiteral("sieveeditor"));
121 Q_FOREACH (const SieveEditorUtil::SieveServerConfig
&conf
, lstConfig
) {
122 writeSieveSettings(wallet
, cfg
, conf
, i
);
126 cfg
->reparseConfiguration();
129 void SieveEditorUtil::writeSieveSettings(KWallet::Wallet
*wallet
, KSharedConfigPtr cfg
, const SieveEditorUtil::SieveServerConfig
&conf
, int index
)
131 KConfigGroup group
= cfg
->group(QStringLiteral("ServerSieve %1").arg(index
));
132 group
.writeEntry(QStringLiteral("Port"), conf
.port
);
133 group
.writeEntry(QStringLiteral("ServerName"), conf
.serverName
);
134 group
.writeEntry(QStringLiteral("UserName"), conf
.userName
);
135 group
.writeEntry(QStringLiteral("Enabled"), conf
.enabled
);
136 const QString walletEntry
= conf
.userName
+ QLatin1Char('@') + conf
.serverName
;
138 wallet
->writePassword(walletEntry
, conf
.password
);
140 group
.writeEntry(QStringLiteral("Authentication"), static_cast<int>(conf
.authenticationType
));
143 void SieveEditorUtil::addServerSieveConfig(const SieveEditorUtil::SieveServerConfig
&conf
)
145 KWallet::Wallet
*wallet
= SieveServerSettings::self()->wallet();
147 if (!wallet
->hasFolder(QStringLiteral("sieveeditor"))) {
148 wallet
->createFolder(QStringLiteral("sieveeditor"));
150 wallet
->setFolder(QStringLiteral("sieveeditor"));
152 KSharedConfigPtr cfg
= KSharedConfig::openConfig();
153 const QRegularExpression
re(QStringLiteral("^ServerSieve (.+)$"));
154 const QStringList groups
= cfg
->groupList().filter(re
);
156 writeSieveSettings(wallet
, cfg
, conf
, groups
.count());