1 | /* |
2 | Copyright (c) 2009 Volker Krause <vkrause@kde.org> |
3 | |
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. |
8 | |
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. |
13 | |
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. |
18 | */ |
19 | |
20 | #include "transport.h" |
21 | |
22 | #include <mailtransport/transportmanager.h> |
23 | |
24 | #include <KDebug> |
25 | #include <KLocalizedString> |
26 | |
27 | #define TABLE_SIZE x |
28 | |
29 | template <typename T> |
30 | struct StringValueTable { |
31 | const char * name; |
32 | typename T::type value; |
33 | typedef typename T::type value_type; |
34 | }; |
35 | |
36 | static const StringValueTable<MailTransport::Transport::EnumType> transportTypeEnums[] = { |
37 | { "smtp" , MailTransport::Transport::EnumType::SMTP }, |
38 | { "sendmail" , MailTransport::Transport::EnumType::Sendmail }, |
39 | { "akonadi" , MailTransport::Transport::EnumType::Akonadi } |
40 | }; |
41 | static const int transportTypeEnumsSize = sizeof( transportTypeEnums ) / sizeof ( *transportTypeEnums ); |
42 | |
43 | static const StringValueTable<MailTransport::Transport::EnumEncryption> encryptionEnum[] = { |
44 | { "none" , MailTransport::Transport::EnumEncryption::None }, |
45 | { "ssl" , MailTransport::Transport::EnumEncryption::SSL }, |
46 | { "tls" , MailTransport::Transport::EnumEncryption::TLS } |
47 | }; |
48 | static const int encryptionEnumSize = sizeof( encryptionEnum ) / sizeof( *encryptionEnum ); |
49 | |
50 | static const StringValueTable<MailTransport::Transport::EnumAuthenticationType> authenticationTypeEnum[] = { |
51 | { "login" , MailTransport::Transport::EnumAuthenticationType::LOGIN }, |
52 | { "plain" , MailTransport::Transport::EnumAuthenticationType::PLAIN }, |
53 | { "cram-md5" , MailTransport::Transport::EnumAuthenticationType::CRAM_MD5 }, |
54 | { "digest-md5" , MailTransport::Transport::EnumAuthenticationType::DIGEST_MD5 }, |
55 | { "gssapi" , MailTransport::Transport::EnumAuthenticationType::GSSAPI }, |
56 | { "ntlm" , MailTransport::Transport::EnumAuthenticationType::NTLM }, |
57 | { "apop" , MailTransport::Transport::EnumAuthenticationType::APOP }, |
58 | { "clear" , MailTransport::Transport::EnumAuthenticationType::CLEAR }, |
59 | { "anonymous" , MailTransport::Transport::EnumAuthenticationType::ANONYMOUS } |
60 | }; |
61 | static const int authenticationTypeEnumSize = sizeof( authenticationTypeEnum ) / sizeof( *authenticationTypeEnum ); |
62 | |
63 | template <typename T> |
64 | static typename T::value_type stringToValue( const T *table, const int tableSize, const QString &string ) |
65 | { |
66 | const QString ref = string.toLower(); |
67 | for ( int i = 0; i < tableSize; ++i ) { |
68 | if ( ref == QLatin1String( table[i].name ) ) |
69 | return table[i].value; |
70 | } |
71 | return table[0].value; // TODO: error handling |
72 | } |
73 | |
74 | Transport::Transport(const QString& type, QObject* parent) : |
75 | SetupObject( parent ), |
76 | m_transportId( -1 ), |
77 | m_port( -1 ), |
78 | m_encr( MailTransport::Transport::EnumEncryption::TLS ), |
79 | m_auth( MailTransport::Transport::EnumAuthenticationType::PLAIN ) |
80 | { |
81 | m_transportType = stringToValue( transportTypeEnums, transportTypeEnumsSize, type ); |
82 | if ( m_transportType == MailTransport::Transport::EnumType::SMTP ) |
83 | m_port = 25; |
84 | } |
85 | |
86 | void Transport::create() |
87 | { |
88 | emit info( i18n( "Setting up mail transport account..." ) ); |
89 | MailTransport::Transport* mt = MailTransport::TransportManager::self()->createTransport(); |
90 | mt->setName( m_name ); |
91 | mt->setHost( m_host ); |
92 | if ( m_port > 0 ) |
93 | mt->setPort( m_port ); |
94 | if ( !m_user.isEmpty() ) { |
95 | mt->setUserName( m_user ); |
96 | mt->setRequiresAuthentication( true ); |
97 | } |
98 | if ( !m_password.isEmpty() ) { |
99 | mt->setStorePassword( true ); |
100 | mt->setPassword( m_password ); |
101 | } |
102 | mt->setEncryption( m_encr ); |
103 | mt->setAuthenticationType( m_auth ); |
104 | m_transportId = mt->id(); |
105 | mt->writeConfig(); |
106 | MailTransport::TransportManager::self()->addTransport( mt ); |
107 | MailTransport::TransportManager::self()->setDefaultTransport( mt->id() ); |
108 | emit finished( i18n( "Mail transport account set up." ) ); |
109 | } |
110 | |
111 | void Transport::destroy() |
112 | { |
113 | MailTransport::TransportManager::self()->removeTransport( m_transportId ); |
114 | emit info( i18n( "Mail transport account deleted." ) ); |
115 | } |
116 | |
117 | void Transport::setName( const QString &name ) |
118 | { |
119 | m_name = name; |
120 | } |
121 | |
122 | void Transport::setHost( const QString &host ) |
123 | { |
124 | m_host = host; |
125 | } |
126 | |
127 | void Transport::setPort( int port ) |
128 | { |
129 | m_port = port; |
130 | } |
131 | |
132 | void Transport::setUsername( const QString &user ) |
133 | { |
134 | m_user = user; |
135 | } |
136 | |
137 | void Transport::setPassword( const QString &password ) |
138 | { |
139 | m_password = password; |
140 | } |
141 | |
142 | void Transport::setEncryption( const QString &encryption ) |
143 | { |
144 | m_encr = stringToValue( encryptionEnum, encryptionEnumSize, encryption ); |
145 | } |
146 | |
147 | void Transport::setAuthenticationType( const QString &authType ) |
148 | { |
149 | m_auth = stringToValue( authenticationTypeEnum, authenticationTypeEnumSize, authType ); |
150 | } |
151 | |
152 | int Transport::transportId() const |
153 | { |
154 | return m_transportId; |
155 | } |
156 | |
157 | |