SVN_SILENT made messages (after extraction)
[kdepim.git] / accountwizard / transport.cpp
blob2814d688b4a3af0d69b1074a49c9ed70fe1d95dd
1 /*
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
17 02110-1301, USA.
20 #include "transport.h"
22 #include <mailtransport/transportmanager.h>
24 #include <KLocalizedString>
26 #define TABLE_SIZE x
28 template <typename T>
29 struct StringValueTable {
30 const char *name;
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);
61 template <typename T>
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) :
74 SetupObject(parent),
75 m_transportId(-1),
76 m_port(-1),
77 m_encr(MailTransport::Transport::EnumEncryption::TLS),
78 m_auth(MailTransport::Transport::EnumAuthenticationType::PLAIN)
79 , m_editMode(false)
81 m_transportType = stringToValue(transportTypeEnums, transportTypeEnumsSize, type);
82 if (m_transportType == MailTransport::Transport::EnumType::SMTP) {
83 m_port = 25;
87 void Transport::create()
89 Q_EMIT info(i18n("Setting up mail transport account..."));
90 MailTransport::Transport *mt = MailTransport::TransportManager::self()->createTransport();
91 mt->setName(m_name);
92 mt->setHost(m_host);
93 if (m_port > 0) {
94 mt->setPort(m_port);
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();
107 mt->save();
108 MailTransport::TransportManager::self()->addTransport(mt);
109 MailTransport::TransportManager::self()->setDefaultTransport(mt->id());
110 if (m_editMode) {
111 edit();
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);
125 if (!mt) {
126 Q_EMIT error(i18n("Could not load config dialog for UID '%1'", m_transportId));
127 } else {
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)
139 m_name = name;
142 void Transport::setHost(const QString &host)
144 m_host = host;
147 void Transport::setPort(int port)
149 m_port = port;
152 void Transport::setUsername(const QString &user)
154 m_user = 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;