Make it compiles when deprecated method are disabled. Add more find package
[kdepim.git] / sieveeditor / src / sieveeditorutil.cpp
bloba861c9a5d04e2fb27571943a0d7e4badb18b1472
1 /*
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"
23 #include <kwallet.h>
25 #include <KConfig>
27 #include <KConfigGroup>
29 #include <QRegularExpression>
30 #include <QUrlQuery>
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 QUrlQuery query;
67 query.addQueryItem(QStringLiteral("x-mech"), authStr);
68 u.setQuery(query);
69 return u;
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);
98 return lstConfig;
101 void SieveEditorUtil::writeServerSieveConfig(const QVector<SieveServerConfig> &lstConfig)
103 KSharedConfigPtr cfg = KSharedConfig::openConfig();
104 const QRegularExpression re(QStringLiteral("^ServerSieve (.+)$"));
105 //Delete Old Group
106 const QStringList groups = cfg->groupList().filter(re);
107 Q_FOREACH (const QString &conf, groups) {
108 KConfigGroup group = cfg->group(conf);
109 group.deleteGroup();
112 int i = 0;
113 KWallet::Wallet *wallet = SieveServerSettings::self()->wallet();
114 if (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);
123 ++i;
125 cfg->sync();
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;
137 if (wallet) {
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();
146 if (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());
157 cfg->sync();