2 Copyright (c) 2009 Volker Krause <vkrause@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
20 #include "transport.h"
22 #include <mailtransport/transportmanager.h>
24 #include <KLocalizedString>
29 struct StringValueTable
{
31 typename
T::type value
;
32 typedef typename
T::type value_type
;
35 static const StringValueTable
<MailTransport::Transport::EnumType
> transportTypeEnums
[] = {
36 { "smtp", MailTransport::Transport::EnumType::SMTP
},
37 { "akonadi", MailTransport::Transport::EnumType::Akonadi
}
39 static const int transportTypeEnumsSize
= sizeof(transportTypeEnums
) / sizeof(*transportTypeEnums
);
41 static const StringValueTable
<MailTransport::Transport::EnumEncryption
> encryptionEnum
[] = {
42 { "none", MailTransport::Transport::EnumEncryption::None
},
43 { "ssl", MailTransport::Transport::EnumEncryption::SSL
},
44 { "tls", MailTransport::Transport::EnumEncryption::TLS
}
46 static const int encryptionEnumSize
= sizeof(encryptionEnum
) / sizeof(*encryptionEnum
);
48 static const StringValueTable
<MailTransport::Transport::EnumAuthenticationType
> authenticationTypeEnum
[] = {
49 { "login", MailTransport::Transport::EnumAuthenticationType::LOGIN
},
50 { "plain", MailTransport::Transport::EnumAuthenticationType::PLAIN
},
51 { "cram-md5", MailTransport::Transport::EnumAuthenticationType::CRAM_MD5
},
52 { "digest-md5", MailTransport::Transport::EnumAuthenticationType::DIGEST_MD5
},
53 { "gssapi", MailTransport::Transport::EnumAuthenticationType::GSSAPI
},
54 { "ntlm", MailTransport::Transport::EnumAuthenticationType::NTLM
},
55 { "apop", MailTransport::Transport::EnumAuthenticationType::APOP
},
56 { "clear", MailTransport::Transport::EnumAuthenticationType::CLEAR
},
57 { "anonymous", MailTransport::Transport::EnumAuthenticationType::ANONYMOUS
}
59 static const int authenticationTypeEnumSize
= sizeof(authenticationTypeEnum
) / sizeof(*authenticationTypeEnum
);
62 static typename
T::value_type
stringToValue(const T
*table
, const int tableSize
, const QString
&string
)
64 const QString ref
= string
.toLower();
65 for (int i
= 0; i
< tableSize
; ++i
) {
66 if (ref
== QLatin1String(table
[i
].name
)) {
67 return table
[i
].value
;
70 return table
[0].value
; // TODO: error handling
73 Transport::Transport(const QString
&type
, QObject
*parent
) :
77 m_encr(MailTransport::Transport::EnumEncryption::TLS
),
78 m_auth(MailTransport::Transport::EnumAuthenticationType::PLAIN
)
81 m_transportType
= stringToValue(transportTypeEnums
, transportTypeEnumsSize
, type
);
82 if (m_transportType
== MailTransport::Transport::EnumType::SMTP
) {
87 void Transport::create()
89 Q_EMIT
info(i18n("Setting up mail transport account..."));
90 MailTransport::Transport
*mt
= MailTransport::TransportManager::self()->createTransport();
96 if (!m_user
.isEmpty()) {
97 mt
->setUserName(m_user
);
98 mt
->setRequiresAuthentication(true);
100 if (!m_password
.isEmpty()) {
101 mt
->setStorePassword(true);
102 mt
->setPassword(m_password
);
104 mt
->setEncryption(m_encr
);
105 mt
->setAuthenticationType(m_auth
);
106 m_transportId
= mt
->id();
108 MailTransport::TransportManager::self()->addTransport(mt
);
109 MailTransport::TransportManager::self()->setDefaultTransport(mt
->id());
113 Q_EMIT
finished(i18n("Mail transport account set up."));
116 void Transport::destroy()
118 MailTransport::TransportManager::self()->removeTransport(m_transportId
);
119 Q_EMIT
info(i18n("Mail transport account deleted."));
122 void Transport::edit()
124 MailTransport::Transport
*mt
= MailTransport::TransportManager::self()->transportById(m_transportId
, false);
126 Q_EMIT
error(i18n("Could not load config dialog for UID '%1'", m_transportId
));
128 MailTransport::TransportManager::self()->configureTransport(mt
, 0);
132 void Transport::setEditMode(const bool editMode
)
134 m_editMode
= editMode
;
137 void Transport::setName(const QString
&name
)
142 void Transport::setHost(const QString
&host
)
147 void Transport::setPort(int port
)
152 void Transport::setUsername(const QString
&user
)
157 void Transport::setPassword(const QString
&password
)
159 m_password
= password
;
162 void Transport::setEncryption(const QString
&encryption
)
164 m_encr
= stringToValue(encryptionEnum
, encryptionEnumSize
, encryption
);
167 void Transport::setAuthenticationType(const QString
&authType
)
169 m_auth
= stringToValue(authenticationTypeEnum
, authenticationTypeEnumSize
, authType
);
172 int Transport::transportId() const
174 return m_transportId
;