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
22 #include "sieveeditorutil.h"
23 #include "sieveserversettings.h"
30 #include <KConfigGroup>
35 KUrl
SieveEditorUtil::SieveServerConfig::url() const {
37 u
.setHost(serverName
);
38 u
.setUserName(userName
);
39 u
.setPassword(password
);
43 switch( authenticationType
) {
44 case MailTransport::Transport::EnumAuthenticationType::CLEAR
:
45 case MailTransport::Transport::EnumAuthenticationType::PLAIN
:
46 authStr
= QLatin1String("PLAIN");
48 case MailTransport::Transport::EnumAuthenticationType::LOGIN
:
49 authStr
= QLatin1String("LOGIN");
51 case MailTransport::Transport::EnumAuthenticationType::CRAM_MD5
:
52 authStr
= QLatin1String("CRAM-MD5");
54 case MailTransport::Transport::EnumAuthenticationType::DIGEST_MD5
:
55 authStr
= QLatin1String("DIGEST-MD5");
57 case MailTransport::Transport::EnumAuthenticationType::GSSAPI
:
58 authStr
= QLatin1String("GSSAPI");
60 case MailTransport::Transport::EnumAuthenticationType::ANONYMOUS
:
61 authStr
= QLatin1String("ANONYMOUS");
64 authStr
= QLatin1String("PLAIN");
67 u
.addQueryItem( QLatin1String("x-mech"), authStr
);
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
);
101 void SieveEditorUtil::writeServerSieveConfig(const QList
<SieveEditorUtil::SieveServerConfig
> &lstConfig
)
103 KSharedConfigPtr cfg
= KGlobal::config();
104 const QRegExp
re( QLatin1String( "^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( 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
;
128 wallet
->writePassword(walletEntry
, conf
.password
);
129 group
.writeEntry(QLatin1String("Authentication"), static_cast<int>(conf
.authenticationType
));
134 cfg
->reparseConfiguration();
137 void SieveEditorUtil::addServerSieveConfig(const SieveEditorUtil::SieveServerConfig
&conf
)
139 KWallet::Wallet
*wallet
= SieveServerSettings::self()->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
;
156 wallet
->writePassword(walletEntry
, conf
.password
);
158 group
.writeEntry(QLatin1String("Authentication"), static_cast<int>(conf
.authenticationType
));