2
Copyright (c) 2012 Montel Laurent <montel@kde.org>
4
This program is free software; you can redistribute it and/or modify it
5
under the terms of the GNU General Public License, version 2, as
6
published by the Free Software Foundation.
8
This program is distributed in the hope that it will be useful, but
9
WITHOUT ANY WARRANTY; without even the implied warranty of
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
General Public License for more details.
13
You should have received a copy of the GNU General Public License along
14
with this program; if not, write to the Free Software Foundation, Inc.,
15
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
#include "evolutionsettings.h"
19
#include "evolutionutil.h"
21
#include <kpimidentities/identity.h>
23
#include <mailtransport/transportmanager.h>
26
#include <KConfigGroup>
31
#include <QDomDocument>
32
#include <QDomElement>
35
EvolutionSettings::EvolutionSettings( const QString& filename, ImportWizard *parent )
36
:AbstractSettings( parent )
40
if ( !file.open( QIODevice::ReadOnly ) ) {
41
kDebug()<<" We can't open file"<<filename;
45
if ( !EvolutionUtil::loadInDomDocument( &file, doc ) )
47
QDomElement config = doc.documentElement();
49
if ( config.isNull() ) {
50
kDebug() << "No config found";
53
for ( QDomElement e = config.firstChildElement(); !e.isNull(); e = e.nextSiblingElement() ) {
54
const QString tag = e.tagName();
55
if ( tag == QLatin1String( "entry" ) ) {
56
if ( e.hasAttribute( "name" ) ) {
57
const QString attr = e.attribute("name");
58
if ( attr == QLatin1String( "accounts" ) ) {
60
} else if ( attr == QLatin1String( "signatures" ) ) {
62
} else if( attr == QLatin1String("send_recv_all_on_start")) {
64
} else if( attr == QLatin1String("send_recv_on_start")) {
67
qDebug()<<" attr unknown "<<attr;
74
EvolutionSettings::~EvolutionSettings()
78
void EvolutionSettings::readSignatures(const QDomElement &account)
80
for ( QDomElement signatureConfig = account.firstChildElement(); !signatureConfig.isNull(); signatureConfig = signatureConfig.nextSiblingElement() ) {
81
if(signatureConfig.tagName() == QLatin1String("li")) {
82
QDomElement stringValue = signatureConfig.firstChildElement();
83
extractSignatureInfo(stringValue.text());
88
void EvolutionSettings::extractSignatureInfo( const QString&info )
90
qDebug()<<" signature info "<<info;
91
QDomDocument signature;
92
if ( !EvolutionUtil::loadInDomDocument( info, signature ) )
95
QDomElement domElement = signature.documentElement();
97
if ( domElement.isNull() ) {
98
kDebug() << "Signature not found";
101
for ( QDomElement e = domElement.firstChildElement(); !e.isNull(); e = e.nextSiblingElement() ) {
102
KPIMIdentities::Signature signature;
104
const QString tag = e.tagName();
105
const QString uid = e.attribute( QLatin1String( "uid" ) );
106
const QString signatureName = e.attribute( QLatin1String( "name" ) ); //Use it ?
107
const QString format = e.attribute( QLatin1String( "text" ) );
108
const bool automatic = ( e.attribute( QLatin1String( "auto" ) ) == QLatin1String( "true" ) );
110
if ( format == QLatin1String( "text/html" ) ) {
111
signature.setInlinedHtml( true );
112
} else if ( format == QLatin1String( "text/plain" ) ) {
113
signature.setInlinedHtml( false );
117
if ( tag == QLatin1String( "filename" ) ) {
118
if ( e.hasAttribute( QLatin1String( "script" ) ) && e.attribute( QLatin1String( "script" ) ) == QLatin1String( "true" ) ){
119
signature.setUrl( e.text(), true );
120
signature.setType( KPIMIdentities::Signature::FromCommand );
123
signature.setUrl( QDir::homePath() + QLatin1String( ".local/share/evolution/signatures/" ) + e.text(), false );
124
signature.setType( KPIMIdentities::Signature::FromFile );
133
mMapSignature.insert( uid, signature );
135
qDebug()<<" signature tag :"<<tag;
139
void EvolutionSettings::readAccount(const QDomElement &account)
141
for ( QDomElement accountConfig = account.firstChildElement(); !accountConfig.isNull(); accountConfig = accountConfig.nextSiblingElement() ) {
142
if(accountConfig.tagName() == QLatin1String("li")) {
143
QDomElement stringValue = accountConfig.firstChildElement();
144
extractAccountInfo(stringValue.text());
149
void EvolutionSettings::extractAccountInfo(const QString& info)
151
qDebug()<<" info "<<info;
153
QDomDocument account;
154
if ( !EvolutionUtil::loadInDomDocument( info, account ) )
157
QDomElement domElement = account.documentElement();
159
if ( domElement.isNull() ) {
160
kDebug() << "Account not found";
163
KPIMIdentities::Identity* newIdentity = createIdentity();
165
if(domElement.hasAttribute(QLatin1String("name"))) {
166
name = domElement.attribute(QLatin1String("name"));
168
for ( QDomElement e = domElement.firstChildElement(); !e.isNull(); e = e.nextSiblingElement() ) {
169
const QString tag = e.tagName();
170
if ( tag == QLatin1String( "identity" ) )
172
for ( QDomElement identity = e.firstChildElement(); !identity.isNull(); identity = identity.nextSiblingElement() ) {
173
const QString identityTag = identity.tagName();
174
if ( identityTag == QLatin1String( "name" ) )
176
const QString fullName( identity.text() );
177
newIdentity->setIdentityName( fullName );
178
newIdentity->setFullName( fullName );
180
else if ( identityTag == QLatin1String( "addr-spec" ) )
182
newIdentity->setPrimaryEmailAddress(identity.text());
184
else if ( identityTag == QLatin1String( "organization" ) )
186
newIdentity->setOrganization(identity.text());
188
else if ( identityTag == QLatin1String( "signature" ) )
190
if ( identity.hasAttribute( "uid" ) ) {
191
newIdentity->setSignature( mMapSignature.value( identity.attribute( "uid" ) ) );
194
else if ( identityTag == QLatin1String( "reply-to" ) )
196
newIdentity->setReplyToAddr( identity.text() );
200
qDebug()<<" tag identity not found :"<<identityTag;
204
else if ( tag == QLatin1String( "source" ) )
206
if(e.hasAttribute(QLatin1String("save-passwd"))&& e.attribute( "save-passwd" ) == QLatin1String( "true" ) ) {
210
bool intervalCheck = false;
211
if(e.hasAttribute(QLatin1String("auto-check"))) {
212
intervalCheck = ( e.attribute(QLatin1String("auto-check")) == QLatin1String( "true" ) );
214
if(e.hasAttribute(QLatin1String("auto-check-timeout"))) {
215
interval = e.attribute(QLatin1String("auto-check-timeout")).toInt();
217
for ( QDomElement server = e.firstChildElement(); !server.isNull(); server = server.nextSiblingElement() ) {
218
const QString serverTag = server.tagName();
219
if ( serverTag == QLatin1String( "url" ) ) {
220
qDebug()<<" server.text() :"<<server.text();
221
QUrl serverUrl( server.text() );
222
const QString scheme = serverUrl.scheme();
223
QMap<QString, QVariant> settings;
224
const int port = serverUrl.port();
226
const QString path = serverUrl.path();
227
qDebug()<<" path !"<<path;
228
const QString userName = serverUrl.userInfo();
230
const QStringList listArgument = path.split(QLatin1Char(';'));
232
//imapx://name@pop3.xx.org:993/;security-method=ssl-on-alternate-port;namespace;shell-command=ssh%20-C%20-l%20%25u%20%25h%20exec%20/usr/sbin/imapd%20;use-shell-command=true
233
if(scheme == QLatin1String("imap") || scheme == QLatin1String("imapx")) {
235
settings.insert(QLatin1String("ImapPort"),port);
236
//Perhaps imapx is specific don't know
237
if ( intervalCheck ) {
238
settings.insert( QLatin1String( "IntervalCheckEnabled" ), true );
240
if ( interval > -1 ) {
241
settings.insert(QLatin1String("IntervalCheckTime" ), interval );
245
const QString securityMethod = getSecurityMethod( listArgument, found );
247
if( securityMethod == QLatin1String("none")) {
248
settings.insert( QLatin1String( "Safety" ), QLatin1String("None") );
250
} else if(securityMethod == QLatin1String("ssl-on-alternate-port")){
251
settings.insert( QLatin1String( "Safety" ), QLatin1String("SSL") );
253
qDebug()<<" security method unknown : "<<path;
256
settings.insert( QLatin1String( "Safety" ), QLatin1String("STARTTLS") );
259
addAuth(settings, QLatin1String( "Authentication" ), userName);
260
AbstractBase::createResource( "akonadi_imap_resource", name,settings );
261
} else if(scheme == QLatin1String("pop")) {
263
settings.insert(QLatin1String("Port"),port);
265
const QString securityMethod = getSecurityMethod( listArgument, found );
267
if( securityMethod == QLatin1String("none")) {
269
} else if(securityMethod == QLatin1String("ssl-on-alternate-port")){
270
settings.insert( QLatin1String( "UseSSL" ), true );
272
qDebug()<<" security method unknown : "<<path;
275
settings.insert( QLatin1String( "UseTLS" ), true );
278
if ( intervalCheck ) {
279
settings.insert( QLatin1String( "IntervalCheckEnabled" ), true );
281
if ( interval > -1 ) {
282
settings.insert(QLatin1String("IntervalCheckInterval" ), interval );
284
if(e.hasAttribute(QLatin1String("keep-on-server"))&& e.attribute(QLatin1String("keep-on-server") ) == QLatin1String( "true" ) ) {
285
settings.insert(QLatin1String("LeaveOnServer"),true);
287
addAuth(settings, QLatin1String( "AuthenticationMethod" ), userName);
288
AbstractBase::createResource( "akonadi_pop3_resource", name, settings );
290
} else if( scheme == QLatin1String("spool") || scheme == QLatin1String("mbox") ) {
292
settings.insert(QLatin1String("Path"),path);
293
settings.insert(QLatin1String("DisplayName"),name);
294
AbstractBase::createResource( "akonadi_mbox_resource", name, settings );
295
} else if( scheme == QLatin1String("maildir") ||scheme == QLatin1String( "spooldir" ) ) {
296
settings.insert(QLatin1String("Path"),path);
297
AbstractBase::createResource( "akonadi_maildir_resource", name, settings );
298
} else if( scheme == QLatin1String("nntp")) {
299
//FIXME in the future
300
qDebug()<<" For the moment we can't import nntp resource";
302
qDebug()<<" unknown scheme "<<scheme;
305
qDebug()<<" server tag unknow :"<<serverTag;
309
else if ( tag == QLatin1String( "transport" ) )
311
if ( e.hasAttribute( "save-passwd" ) && e.attribute( "save-passwd" ) == QLatin1String( "true" ) )
313
//TODO save to kwallet ?
316
MailTransport::Transport *transport = createTransport();
317
for ( QDomElement smtp = e.firstChildElement(); !smtp.isNull(); smtp = smtp.nextSiblingElement() ) {
318
const QString smtpTag = smtp.tagName();
319
if ( smtpTag == QLatin1String( "url" ) ) {
320
qDebug()<<" smtp.text() :"<<smtp.text();
321
QUrl smtpUrl( smtp.text() );
322
const QString scheme = smtpUrl.scheme();
323
if(scheme == QLatin1String("sendmail")) {
324
transport->setType(MailTransport::Transport::EnumType::Sendmail);
326
transport->setHost( smtpUrl.host() );
327
transport->setName( smtpUrl.host() );
329
//transport->setRequiresAuthentication(true);
330
//transport->setUserName(....);
331
const int port = smtpUrl.port();
333
transport->setPort( port );
335
const QString userName = smtpUrl.userInfo();
337
const QString authMethod = getAuthMethod(userName, found);
339
if(authMethod==QLatin1String("PLAIN")) {
340
transport->setAuthenticationType(MailTransport::Transport::EnumAuthenticationType::PLAIN);
341
} else if(authMethod==QLatin1String("NTLM")) {
342
transport->setAuthenticationType(MailTransport::Transport::EnumAuthenticationType::NTLM);
343
} else if(authMethod==QLatin1String("DIGEST-MD5")) {
344
transport->setAuthenticationType(MailTransport::Transport::EnumAuthenticationType::DIGEST_MD5);
345
} else if(authMethod==QLatin1String("CRAM-MD5")) {
346
transport->setAuthenticationType(MailTransport::Transport::EnumAuthenticationType::CRAM_MD5);
347
} else if(authMethod==QLatin1String("LOGIN")) {
348
transport->setAuthenticationType(MailTransport::Transport::EnumAuthenticationType::LOGIN);
349
} else if(authMethod==QLatin1String("GSSAPI")) {
350
transport->setAuthenticationType(MailTransport::Transport::EnumAuthenticationType::GSSAPI);
351
} else if(authMethod==QLatin1String("POPB4SMTP")) {
352
transport->setAuthenticationType(MailTransport::Transport::EnumAuthenticationType::APOP); //????
354
qDebug()<<" smtp auth method unknown "<<authMethod;
358
const QString path = smtpUrl.path();
360
const QStringList listArgument = path.split(QLatin1Char(';'));
361
const QString securityMethod = getSecurityMethod( listArgument, found );
363
if( securityMethod == QLatin1String("none")) {
364
transport->setEncryption( MailTransport::Transport::EnumEncryption::None );
366
} else if(securityMethod == QLatin1String("ssl-on-alternate-port")){
367
transport->setEncryption( MailTransport::Transport::EnumEncryption::SSL );
369
qDebug()<<" security method unknown : "<<path;
372
transport->setEncryption( MailTransport::Transport::EnumEncryption::TLS );
376
qDebug()<<" smtp tag unknow :"<<smtpTag;
379
storeTransport(transport, true );
381
else if ( tag == QLatin1String( "drafts-folder" ) )
383
const QString selectedFolder = adaptFolder( e.text().remove( QLatin1String( "folder://" ) ) );
384
newIdentity->setDrafts(selectedFolder);
386
else if ( tag == QLatin1String( "sent-folder" ) )
388
const QString selectedFolder = adaptFolder( e.text().remove( QLatin1String( "folder://" ) ) );
389
newIdentity->setFcc(selectedFolder);
391
else if ( tag == QLatin1String( "auto-cc" ) )
393
if ( e.hasAttribute( "always" ) && ( e.attribute( "always" ) == QLatin1String( "true" ) ) )
395
QDomElement recipient = e.firstChildElement();
396
const QString text = recipient.text();
397
newIdentity->setCc(text);
400
else if( tag == QLatin1String("reply-to"))
402
newIdentity->setReplyToAddr(e.text());
404
else if ( tag == QLatin1String( "auto-bcc" ) )
406
if ( e.hasAttribute( "always" ) && ( e.attribute( "always" ) == QLatin1String( "true" ) ) )
408
QDomElement recipient = e.firstChildElement();
409
const QString text = recipient.text();
410
newIdentity->setBcc(text);
413
else if ( tag == QLatin1String( "receipt-policy" ) )
415
if ( e.hasAttribute( QLatin1String( "policy" ) ) ) {
416
const QString policy = e.attribute( QLatin1String( "policy" ) );
420
else if ( tag == QLatin1String( "pgp" ) )
422
if ( e.hasAttribute( QLatin1String( "encrypt-to-self" ) ) &&
423
( e.attribute( QLatin1String( "encrypt-to-self" ) ) == QLatin1String( "true" ) ) ) {
426
if ( e.hasAttribute( QLatin1String( "always-trust" ) ) &&
427
( e.attribute( QLatin1String( "always-trust" ) ) == QLatin1String( "true" ) ) ) {
430
if ( e.hasAttribute( QLatin1String( "always-sign" ) ) &&
431
( e.attribute( QLatin1String( "always-sign" ) ) == QLatin1String( "true" ) ) ) {
434
if ( e.hasAttribute( QLatin1String( "no-imip-sign" ) ) &&
435
( e.attribute( QLatin1String( "no-imip-sign" ) ) == QLatin1String( "true" ) ) ) {
439
else if ( tag == QLatin1String( "smime" ) )
441
if ( e.hasAttribute( QLatin1String( "sign-default" ) ) &&
442
( e.attribute( QLatin1String( "sign-default" ) ) == QLatin1String( "true" ) ) ) {
445
if ( e.hasAttribute( QLatin1String( "encrypt-default" ) ) &&
446
( e.attribute( QLatin1String( "encrypt-default" ) ) == QLatin1String( "true" ) ) ) {
449
if ( e.hasAttribute( QLatin1String( "encrypt-to-self" ) ) &&
450
( e.attribute( QLatin1String( "encrypt-to-self" ) ) == QLatin1String( "true" ) ) ) {
456
qDebug()<<" tag not know :"<<tag;
459
storeIdentity(newIdentity);
462
QString EvolutionSettings::getSecurityMethod( const QStringList& listArgument, bool & found )
465
if(listArgument.isEmpty())
467
Q_FOREACH( const QString& str, listArgument ) {
468
if(str.contains(QLatin1String("security-method="))) {
469
const int index = str.indexOf(QLatin1String("security-method="));
471
const QString securityMethod = str.right(str.length() - index - 16 /*security-method=*/);
473
return securityMethod;
480
QString EvolutionSettings::getAuthMethod( const QString& path, bool & found)
482
const int index = path.indexOf(QLatin1String("auth="));
484
const QString securityMethod = path.right(path.length() - index - 5 /*auth=*/);
486
return securityMethod;
492
void EvolutionSettings::addAuth(QMap<QString, QVariant>& settings, const QString & argument, const QString& userName)
495
const QString authMethod = getAuthMethod(userName, found);
497
if(authMethod==QLatin1String("PLAIN")) {
498
settings.insert( argument, MailTransport::Transport::EnumAuthenticationType::PLAIN );
499
} else if(authMethod==QLatin1String("NTLM")) {
500
settings.insert( argument, MailTransport::Transport::EnumAuthenticationType::NTLM );
501
} else if(authMethod==QLatin1String("DIGEST-MD5")) {
502
settings.insert( argument, MailTransport::Transport::EnumAuthenticationType::DIGEST_MD5 );
503
} else if(authMethod==QLatin1String("CRAM-MD5")) {
504
settings.insert( argument, MailTransport::Transport::EnumAuthenticationType::CRAM_MD5 );
505
} else if(authMethod==QLatin1String("LOGIN")) {
506
settings.insert( argument, MailTransport::Transport::EnumAuthenticationType::LOGIN );
507
} else if(authMethod==QLatin1String("POPB4SMTP")) {
508
settings.insert( argument, MailTransport::Transport::EnumAuthenticationType::APOP ); //????
510
qDebug()<<" smtp auth method unknown "<<authMethod;