SVN_SILENT made messages (after extraction)
[kdepim.git] / sieveeditor / sieveeditorutil.cpp
blobccc1de001a1cb8776447f9f18552bd5c8dbbc54b
1 /*
2 Copyright (c) 2013-2015 Montel Laurent <montel@kde.org>
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
21 #include "sieveeditorutil.h"
22 #include "sieveserversettings.h"
24 #include <kwallet.h>
26 #include <KConfig>
28 #include <KConfigGroup>
30 #include <QRegularExpression>
31 #include <KSharedConfig>
33 QUrl SieveEditorUtil::SieveServerConfig::url() const
35 QUrl u;
36 u.setHost(serverName);
37 u.setUserName(userName);
38 u.setPassword(password);
39 u.setPort(port);
41 QString authStr;
42 switch (authenticationType) {
43 case MailTransport::Transport::EnumAuthenticationType::CLEAR:
44 case MailTransport::Transport::EnumAuthenticationType::PLAIN:
45 authStr = QStringLiteral("PLAIN");
46 break;
47 case MailTransport::Transport::EnumAuthenticationType::LOGIN:
48 authStr = QStringLiteral("LOGIN");
49 break;
50 case MailTransport::Transport::EnumAuthenticationType::CRAM_MD5:
51 authStr = QStringLiteral("CRAM-MD5");
52 break;
53 case MailTransport::Transport::EnumAuthenticationType::DIGEST_MD5:
54 authStr = QStringLiteral("DIGEST-MD5");
55 break;
56 case MailTransport::Transport::EnumAuthenticationType::GSSAPI:
57 authStr = QStringLiteral("GSSAPI");
58 break;
59 case MailTransport::Transport::EnumAuthenticationType::ANONYMOUS:
60 authStr = QStringLiteral("ANONYMOUS");
61 break;
62 default:
63 authStr = QStringLiteral("PLAIN");
64 break;
66 u.addQueryItem(QStringLiteral("x-mech"), authStr);
68 return u;
71 QVector<SieveEditorUtil::SieveServerConfig> SieveEditorUtil::readServerSieveConfig()
73 QVector<SieveServerConfig> lstConfig;
74 KSharedConfigPtr cfg = KSharedConfig::openConfig();
75 QRegularExpression re(QStringLiteral("^ServerSieve (.+)$"));
76 const QStringList groups = cfg->groupList().filter(re);
77 KWallet::Wallet *wallet = SieveServerSettings::self()->wallet();
78 if (wallet && !wallet->setFolder(QStringLiteral("sieveeditor"))) {
79 wallet->createFolder(QStringLiteral("sieveeditor"));
80 wallet->setFolder(QStringLiteral("sieveeditor"));
83 Q_FOREACH (const QString &conf, groups) {
84 SieveServerConfig sieve;
85 KConfigGroup group = cfg->group(conf);
86 sieve.port = group.readEntry(QStringLiteral("Port"), 0);
87 sieve.serverName = group.readEntry(QStringLiteral("ServerName"));
88 sieve.userName = group.readEntry(QStringLiteral("UserName"));
89 sieve.enabled = group.readEntry(QStringLiteral("Enabled"), true);
90 const QString walletEntry = sieve.userName + QLatin1Char('@') + sieve.serverName;
91 if (wallet && wallet->hasEntry(walletEntry)) {
92 wallet->readPassword(walletEntry, sieve.password);
94 sieve.authenticationType = static_cast<MailTransport::Transport::EnumAuthenticationType::type>(group.readEntry(QStringLiteral("Authentication"), static_cast<int>(MailTransport::Transport::EnumAuthenticationType::PLAIN)));
95 lstConfig.append(sieve);
97 return lstConfig;
100 void SieveEditorUtil::writeServerSieveConfig(const QVector<SieveServerConfig> &lstConfig)
102 KSharedConfigPtr cfg = KSharedConfig::openConfig();
103 const QRegularExpression re(QStringLiteral("^ServerSieve (.+)$"));
104 //Delete Old Group
105 const QStringList groups = cfg->groupList().filter(re);
106 Q_FOREACH (const QString &conf, groups) {
107 KConfigGroup group = cfg->group(conf);
108 group.deleteGroup();
111 int i = 0;
112 KWallet::Wallet *wallet = SieveServerSettings::self()->wallet();
113 if (wallet) {
114 if (!wallet->hasFolder(QStringLiteral("sieveeditor"))) {
115 wallet->createFolder(QStringLiteral("sieveeditor"));
117 wallet->setFolder(QStringLiteral("sieveeditor"));
120 Q_FOREACH (const SieveEditorUtil::SieveServerConfig &conf, lstConfig) {
121 writeSieveSettings(wallet, cfg, conf, i);
122 ++i;
124 cfg->sync();
125 cfg->reparseConfiguration();
128 void SieveEditorUtil::writeSieveSettings(KWallet::Wallet *wallet, KSharedConfigPtr cfg, const SieveEditorUtil::SieveServerConfig &conf, int index)
130 KConfigGroup group = cfg->group(QStringLiteral("ServerSieve %1").arg(index));
131 group.writeEntry(QStringLiteral("Port"), conf.port);
132 group.writeEntry(QStringLiteral("ServerName"), conf.serverName);
133 group.writeEntry(QStringLiteral("UserName"), conf.userName);
134 group.writeEntry(QStringLiteral("Enabled"), conf.enabled);
135 const QString walletEntry = conf.userName + QLatin1Char('@') + conf.serverName;
136 if (wallet) {
137 wallet->writePassword(walletEntry, conf.password);
139 group.writeEntry(QStringLiteral("Authentication"), static_cast<int>(conf.authenticationType));
142 void SieveEditorUtil::addServerSieveConfig(const SieveEditorUtil::SieveServerConfig &conf)
144 KWallet::Wallet *wallet = SieveServerSettings::self()->wallet();
145 if (wallet) {
146 if (!wallet->hasFolder(QStringLiteral("sieveeditor"))) {
147 wallet->createFolder(QStringLiteral("sieveeditor"));
149 wallet->setFolder(QStringLiteral("sieveeditor"));
151 KSharedConfigPtr cfg = KSharedConfig::openConfig();
152 const QRegularExpression re(QStringLiteral("^ServerSieve (.+)$"));
153 const QStringList groups = cfg->groupList().filter(re);
155 writeSieveSettings(wallet, cfg, conf, groups.count());
156 cfg->sync();