SVN_SILENT made messages (.desktop file)
[kdepim.git] / sieveeditor / sieveeditorutil.cpp
blob6da5567291cc4266ecc53c9c626a1e0df637c81e
1 /*
2 Copyright (c) 2013, 2014 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.
22 #include "sieveeditorutil.h"
23 #include "sieveserversettings.h"
25 #include <kwallet.h>
27 #include <KConfig>
28 #include <KGlobal>
29 #include <KLocale>
30 #include <KConfigGroup>
32 #include <QRegExp>
35 KUrl SieveEditorUtil::SieveServerConfig::url() const {
36 KUrl u;
37 u.setHost(serverName);
38 u.setUserName(userName);
39 u.setPassword(password);
40 u.setPort(port);
42 QString authStr;
43 switch( authenticationType ) {
44 case MailTransport::Transport::EnumAuthenticationType::CLEAR:
45 case MailTransport::Transport::EnumAuthenticationType::PLAIN:
46 authStr = QLatin1String("PLAIN");
47 break;
48 case MailTransport::Transport::EnumAuthenticationType::LOGIN:
49 authStr = QLatin1String("LOGIN");
50 break;
51 case MailTransport::Transport::EnumAuthenticationType::CRAM_MD5:
52 authStr = QLatin1String("CRAM-MD5");
53 break;
54 case MailTransport::Transport::EnumAuthenticationType::DIGEST_MD5:
55 authStr = QLatin1String("DIGEST-MD5");
56 break;
57 case MailTransport::Transport::EnumAuthenticationType::GSSAPI:
58 authStr = QLatin1String("GSSAPI");
59 break;
60 case MailTransport::Transport::EnumAuthenticationType::ANONYMOUS:
61 authStr = QLatin1String("ANONYMOUS");
62 break;
63 default:
64 authStr = QLatin1String("PLAIN");
65 break;
67 u.addQueryItem( QLatin1String("x-mech"), authStr );
69 return u;
72 QList<SieveEditorUtil::SieveServerConfig> SieveEditorUtil::readServerSieveConfig()
74 QList<SieveServerConfig> lstConfig;
75 KSharedConfigPtr cfg = KGlobal::config();
76 QRegExp re( QLatin1String( "^ServerSieve (.+)$" ) );
77 const QStringList groups = cfg->groupList().filter( re );
78 KWallet::Wallet *wallet = SieveServerSettings::self()->wallet();
79 if ( wallet && !wallet->setFolder( QLatin1String("sieveeditor") ) ) {
80 wallet->createFolder( QLatin1String("sieveeditor"));
81 wallet->setFolder( QLatin1String("sieveeditor") );
85 Q_FOREACH (const QString &conf, groups) {
86 SieveServerConfig sieve;
87 KConfigGroup group = cfg->group(conf);
88 sieve.port = group.readEntry(QLatin1String("Port"), 0);
89 sieve.serverName = group.readEntry(QLatin1String("ServerName"));
90 sieve.userName = group.readEntry(QLatin1String("UserName"));
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(QLatin1String("Authentication"), static_cast<int>(MailTransport::Transport::EnumAuthenticationType::PLAIN)));
96 lstConfig.append(sieve);
98 return lstConfig;
101 void SieveEditorUtil::writeServerSieveConfig(const QList<SieveEditorUtil::SieveServerConfig> &lstConfig)
103 KSharedConfigPtr cfg = KGlobal::config();
104 const QRegExp re( QLatin1String( "^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( QLatin1String("sieveeditor") ) ) {
116 wallet->createFolder( QLatin1String("sieveeditor") );
118 wallet->setFolder( QLatin1String("sieveeditor") );
121 Q_FOREACH (const SieveEditorUtil::SieveServerConfig &conf, lstConfig) {
122 KConfigGroup group = cfg->group(QString::fromLatin1("ServerSieve %1").arg(i));
123 group.writeEntry(QLatin1String("Port"), conf.port);
124 group.writeEntry(QLatin1String("ServerName"), conf.serverName);
125 group.writeEntry(QLatin1String("UserName"), conf.userName);
126 const QString walletEntry = conf.userName + QLatin1Char('@') + conf.serverName;
127 if (wallet)
128 wallet->writePassword(walletEntry, conf.password);
129 group.writeEntry(QLatin1String("Authentication"), static_cast<int>(conf.authenticationType));
131 ++i;
133 cfg->sync();
134 cfg->reparseConfiguration();
137 void SieveEditorUtil::addServerSieveConfig(const SieveEditorUtil::SieveServerConfig &conf)
139 KWallet::Wallet *wallet = SieveServerSettings::self()->wallet();
140 if (wallet) {
141 if ( !wallet->hasFolder( QLatin1String("sieveeditor") ) ) {
142 wallet->createFolder( QLatin1String("sieveeditor") );
144 wallet->setFolder( QLatin1String("sieveeditor") );
146 KSharedConfigPtr cfg = KGlobal::config();
147 const QRegExp re( QLatin1String( "^ServerSieve (.+)$" ) );
148 const QStringList groups = cfg->groupList().filter( re );
149 KConfigGroup group = cfg->group(QString::fromLatin1("ServerSieve %1").arg(groups.count()));
150 group.writeEntry(QLatin1String("Port"), conf.port);
151 group.writeEntry(QLatin1String("ServerName"), conf.serverName);
152 group.writeEntry(QLatin1String("UserName"), conf.userName);
153 const QString walletEntry = conf.userName + QLatin1Char('@') + conf.serverName;
155 if (wallet)
156 wallet->writePassword(walletEntry, conf.password);
158 group.writeEntry(QLatin1String("Authentication"), static_cast<int>(conf.authenticationType));
159 cfg->sync();