1/*
2 Copyright (c) 2009 Volker Krause <vkrause@kde.org>
3 Copyright (c) 2010 Tom Albers <toma@kde.org>
4 Copyright (c) 2012 Laurent Montel <montel@kde.org>
5
6 This library is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Library General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or (at your
9 option) any later version.
10
11 This library is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14 License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA.
20*/
21
22#include "personaldatapage.h"
23
24#include "config-enterprise.h"
25#include "global.h"
26#include "dialog.h"
27#include "transport.h"
28#include "resource.h"
29#include "ispdb/ispdb.h"
30
31#include <kpimutils/emailvalidator.h>
32#include <kpimutils/email.h>
33
34#include <mailtransport/transport.h>
35
36#include <KDebug>
37
38
39QString accountName(Ispdb *ispdb, QString username)
40{
41 const int pos(username.indexOf(QLatin1Char('@')));
42 username = username.left(pos);
43 return ispdb->name( Ispdb::Long ) + QString::fromLatin1(" (%1)").arg(username);
44}
45
46PersonalDataPage::PersonalDataPage(Dialog* parent) :
47 Page( parent ), mIspdb( 0 ), mSetupManager( parent->setupManager() )
48{
49 QWidget *pageParent = this;
50
51 ui.setupUi( pageParent );
52
53 KPIMUtils::EmailValidator* emailValidator = new KPIMUtils::EmailValidator( this );
54 ui.emailEdit->setValidator( emailValidator );
55
56 // KEmailSettings defaults
57 ui.nameEdit->setText( mSetupManager->name() );
58 ui.emailEdit->setText( mSetupManager->email() );
59 slotTextChanged();
60 connect( ui.emailEdit, SIGNAL(textChanged(QString)), SLOT(slotTextChanged()) );
61 connect( ui.nameEdit, SIGNAL(textChanged(QString)), SLOT(slotTextChanged()) );
62 connect( ui.createAccountPb, SIGNAL(clicked()), SLOT(slotCreateAccountClicked()) );
63 connect( ui.buttonGroup, SIGNAL(buttonClicked(QAbstractButton*)), SLOT(slotRadioButtonClicked(QAbstractButton*)) );
64#ifdef KDEPIM_ENTERPRISE_BUILD
65 ui.checkOnlineGroupBox->setChecked( false );
66#endif
67}
68
69void PersonalDataPage::setHideOptionInternetSearch( bool hide )
70{
71 ui.checkOnlineGroupBox->setChecked( !hide );
72 ui.checkOnlineGroupBox->setVisible( !hide );
73}
74
75void PersonalDataPage::slotRadioButtonClicked( QAbstractButton* button)
76{
77 QString smptHostname;
78 if ( !mIspdb->smtpServers().isEmpty() ) {
79 server s = mIspdb->smtpServers().first();
80 smptHostname = s.hostname;
81 }
82 ui.outgoingLabel->setText( i18n( "SMTP, %1", smptHostname ) );
83 if ( button == ui.imapAccount ) {
84 server simap = mIspdb->imapServers().first(); // should be ok.
85 ui.incommingLabel->setText( i18n( "IMAP, %1", simap.hostname ) );
86 ui.usernameLabel->setText( simap.username );
87 } else if ( button == ui.pop3Account ) {
88 server spop3 = mIspdb->pop3Servers().first(); // should be ok.
89 ui.incommingLabel->setText( i18n( "POP3, %1", spop3.hostname ) );
90 ui.usernameLabel->setText( spop3.username );
91 }
92}
93
94void PersonalDataPage::slotCreateAccountClicked()
95{
96 configureSmtpAccount();
97 if ( ui.imapAccount->isChecked() )
98 configureImapAccount();
99 else
100 configurePop3Account();
101 emit leavePageNextOk(); // go to the next page
102 mSetupManager->execute();
103}
104
105void PersonalDataPage::slotTextChanged()
106{
107 // Ignore the password field, as that can be empty when auth is based on ip-address.
108 setValid( !ui.emailEdit->text().isEmpty() &&
109 !ui.nameEdit->text().isEmpty() &&
110 KPIMUtils::isValidSimpleAddress( ui.emailEdit->text() ) );
111}
112
113void PersonalDataPage::leavePageNext()
114{
115 ui.stackedPage->setCurrentIndex( 0 );
116 ui.imapAccount->setChecked( true );
117 mSetupManager->setPersonalDataAvailable( true );
118 mSetupManager->setName( ui.nameEdit->text() );
119 mSetupManager->setPassword( ui.passwordEdit->text() );
120 mSetupManager->setEmail( ui.emailEdit->text().trimmed() );
121
122 if ( ui.checkOnlineGroupBox->isChecked() ) {
123 // since the user can go back and forth, explicitly disable the man page
124 emit manualWanted( false );
125 setCursor( Qt::BusyCursor );
126 ui.mProgress->start();
127 kDebug() << "Searching on internet";
128 delete mIspdb;
129 mIspdb = new Ispdb( this );
130 connect(mIspdb, SIGNAL(searchType(QString)), this, SLOT(slotSearchType(QString)));
131 mIspdb->setEmail( ui.emailEdit->text() );
132 mIspdb->start();
133
134 connect( mIspdb, SIGNAL(finished(bool)),
135 SLOT(ispdbSearchFinished(bool)) );
136 } else {
137 emit manualWanted( true ); // enable the manual page
138 emit leavePageNextOk(); // go to the next page
139 }
140}
141
142void PersonalDataPage::ispdbSearchFinished( bool ok )
143{
144 kDebug() << ok;
145
146 unsetCursor();
147 ui.mProgress->stop();
148 if ( ok ) {
149
150 if ( !mIspdb->imapServers().isEmpty() && !mIspdb->pop3Servers().isEmpty() )
151 {
152 ui.stackedPage->setCurrentIndex( 1 );
153 slotRadioButtonClicked( ui.imapAccount );
154 }
155 else
156 automaticConfigureAccount();
157
158 } else {
159 emit manualWanted( true ); // enable the manual page
160 emit leavePageNextOk();
161 }
162}
163
164void PersonalDataPage::slotSearchType(const QString &type)
165{
166 ui.mProgress->setActiveLabel(type);
167}
168
169void PersonalDataPage::configureSmtpAccount()
170{
171 if ( !mIspdb->smtpServers().isEmpty() ) {
172 server s = mIspdb->smtpServers().first(); // should be ok.
173 kDebug() << "Configuring transport for" << s.hostname;
174
175 QObject* object = mSetupManager->createTransport( QLatin1String("smtp") );
176 Transport* t = qobject_cast<Transport*>( object );
177 t->setName( accountName(mIspdb,s.username) );
178 t->setHost( s.hostname );
179 t->setPort( s.port );
180 t->setUsername( s.username );
181 t->setPassword( ui.passwordEdit->text() );
182 switch ( s.authentication ) {
183 case Ispdb::Plain: t->setAuthenticationType( QLatin1String("plain") ); break;
184 case Ispdb::CramMD5: t->setAuthenticationType( QLatin1String("cram-md5") ); break;
185 case Ispdb::NTLM: t->setAuthenticationType( QLatin1String("ntlm") ); break;
186 case Ispdb::GSSAPI: t->setAuthenticationType( QLatin1String("gssapi") ); break;
187 case Ispdb::ClientIP: break;
188 case Ispdb::NoAuth: break;
189 default: break;
190 }
191 switch ( s.socketType ) {
192 case Ispdb::Plain: t->setEncryption( QLatin1String("none") );break;
193 case Ispdb::SSL: t->setEncryption( QLatin1String("ssl") );break;
194 case Ispdb::StartTLS: t->setEncryption( QLatin1String("tls") );break;
195 default: break;
196 }
197 } else
198 kDebug() << "No transport to be created....";
199}
200
201void PersonalDataPage::configureImapAccount()
202{
203 if ( !mIspdb->imapServers().isEmpty() ) {
204 server s = mIspdb->imapServers().first(); // should be ok.
205 kDebug() << "Configuring imap for" << s.hostname;
206
207 QObject* object = mSetupManager->createResource( QLatin1String("akonadi_imap_resource") );
208 Resource* t = qobject_cast<Resource*>( object );
209 t->setName( accountName(mIspdb,s.username) );
210 t->setOption( QLatin1String("ImapServer"), s.hostname );
211 t->setOption( QLatin1String("ImapPort"), s.port );
212 t->setOption( QLatin1String("UserName"), s.username );
213 t->setOption( QLatin1String("Password"), ui.passwordEdit->text() );
214 switch ( s.authentication ) {
215 case Ispdb::Plain: t->setOption( QLatin1String("Authentication"), MailTransport::Transport::EnumAuthenticationType::CLEAR ); break;
216 case Ispdb::CramMD5: t->setOption( QLatin1String("Authentication"), MailTransport::Transport::EnumAuthenticationType::CRAM_MD5 ); break;
217 case Ispdb::NTLM: t->setOption( QLatin1String("Authentication"), MailTransport::Transport::EnumAuthenticationType::NTLM ); break;
218 case Ispdb::GSSAPI: t->setOption( QLatin1String("Authentication"), MailTransport::Transport::EnumAuthenticationType::GSSAPI ); break;
219 case Ispdb::ClientIP: break;
220 case Ispdb::NoAuth: break;
221 default: break;
222 }
223 switch ( s.socketType ) {
224 case Ispdb::None: t->setOption( QLatin1String("Safety"), QLatin1String("None") );break;
225 case Ispdb::SSL: t->setOption( QLatin1String("Safety"), QLatin1String("SSL") );break;
226 case Ispdb::StartTLS: t->setOption( QLatin1String("Safety"), QLatin1String("STARTTLS") );break;
227 default: break;
228 }
229 }
230}
231
232void PersonalDataPage::configurePop3Account()
233{
234 if ( !mIspdb->pop3Servers().isEmpty() ) {
235 server s = mIspdb->pop3Servers().first(); // should be ok.
236 kDebug() << "No Imap to be created, configuring pop3 for" << s.hostname;
237
238 QObject* object = mSetupManager->createResource( QLatin1String("akonadi_pop3_resource") );
239 Resource* t = qobject_cast<Resource*>( object );
240 t->setName( accountName(mIspdb,s.username) );
241 t->setOption( QLatin1String("Host"), s.hostname );
242 t->setOption( QLatin1String("Port"), s.port );
243 t->setOption( QLatin1String("Login"), s.username );
244 t->setOption( QLatin1String("Password"), ui.passwordEdit->text() );
245 switch ( s.authentication ) {
246 case Ispdb::Plain: t->setOption( QLatin1String("AuthenticationMethod"), MailTransport::Transport::EnumAuthenticationType::PLAIN ); break;
247 case Ispdb::CramMD5: t->setOption( QLatin1String("AuthenticationMethod"), MailTransport::Transport::EnumAuthenticationType::CRAM_MD5 ); break;
248 case Ispdb::NTLM: t->setOption( QLatin1String("AuthenticationMethod"), MailTransport::Transport::EnumAuthenticationType::NTLM ); break;
249 case Ispdb::GSSAPI: t->setOption( QLatin1String("AuthenticationMethod"), MailTransport::Transport::EnumAuthenticationType::GSSAPI ); break;
250 case Ispdb::ClientIP:
251 case Ispdb::NoAuth:
252 default: t->setOption( QLatin1String("AuthenticationMethod"), MailTransport::Transport::EnumAuthenticationType::CLEAR ); break;
253 }
254 switch ( s.socketType ) {
255 case Ispdb::SSL: t->setOption( QLatin1String("UseSSL"), 1 );break;
256 case Ispdb::StartTLS: t->setOption( QLatin1String("UseTLS"), 1 );break;
257 case Ispdb::None:
258 default: t->setOption( QLatin1String("UseTLS"), 1 ); break;
259 }
260 }
261}
262
263void PersonalDataPage::automaticConfigureAccount()
264{
265 configureSmtpAccount();
266 configureImapAccount();
267 configurePop3Account();
268 emit leavePageNextOk(); // go to the next page
269 mSetupManager->execute();
270}
271
272void PersonalDataPage::leavePageNextRequested()
273{
274 // override base class with doing nothing...
275}
276
277
278
279