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>
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.
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.
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
22 #include "personaldatapage.h"
24 #include "config-enterprise.h"
27 #include "transport.h"
29 #include "ispdb/ispdb.h"
31 #include <Libkdepim/EmailValidator>
32 #include <KEmailAddress>
34 #include <mailtransport/transport.h>
36 #include "accountwizard_debug.h"
38 QString
accountName(Ispdb
*ispdb
, QString username
)
40 const int pos(username
.indexOf(QLatin1Char('@')));
41 username
= username
.left(pos
);
42 return ispdb
->name(Ispdb::Long
) + QStringLiteral(" (%1)").arg(username
);
45 PersonalDataPage::PersonalDataPage(Dialog
*parent
) :
46 Page(parent
), mIspdb(0), mSetupManager(parent
->setupManager())
48 QWidget
*pageParent
= this;
50 ui
.setupUi(pageParent
);
52 KPIM::EmailValidator
*emailValidator
= new KPIM::EmailValidator(this);
53 ui
.emailEdit
->setValidator(emailValidator
);
55 // KEmailSettings defaults
56 ui
.nameEdit
->setText(mSetupManager
->name());
57 ui
.emailEdit
->setText(mSetupManager
->email());
59 connect(ui
.emailEdit
, &QLineEdit::textChanged
, this, &PersonalDataPage::slotTextChanged
);
60 connect(ui
.nameEdit
, &QLineEdit::textChanged
, this, &PersonalDataPage::slotTextChanged
);
61 connect(ui
.createAccountPb
, &QPushButton::clicked
, this, &PersonalDataPage::slotCreateAccountClicked
);
62 connect(ui
.buttonGroup
, static_cast<void (QButtonGroup::*)(QAbstractButton
*)>(&QButtonGroup::buttonClicked
), this, &PersonalDataPage::slotRadioButtonClicked
);
63 #ifdef KDEPIM_ENTERPRISE_BUILD
64 ui
.checkOnlineGroupBox
->setChecked(false);
68 void PersonalDataPage::setHideOptionInternetSearch(bool hide
)
70 ui
.checkOnlineGroupBox
->setChecked(!hide
);
71 ui
.checkOnlineGroupBox
->setVisible(!hide
);
74 void PersonalDataPage::slotRadioButtonClicked(QAbstractButton
*button
)
77 if (!mIspdb
->smtpServers().isEmpty()) {
78 Server s
= mIspdb
->smtpServers().at(0);
79 smptHostname
= s
.hostname
;
81 ui
.outgoingLabel
->setText(i18n("SMTP, %1", smptHostname
));
82 if (button
== ui
.imapAccount
) {
83 Server simap
= mIspdb
->imapServers().at(0); // should be ok.
84 ui
.incommingLabel
->setText(i18n("IMAP, %1", simap
.hostname
));
85 ui
.usernameLabel
->setText(simap
.username
);
86 } else if (button
== ui
.pop3Account
) {
87 Server spop3
= mIspdb
->pop3Servers().at(0); // should be ok.
88 ui
.incommingLabel
->setText(i18n("POP3, %1", spop3
.hostname
));
89 ui
.usernameLabel
->setText(spop3
.username
);
93 void PersonalDataPage::slotCreateAccountClicked()
95 configureSmtpAccount();
96 if (ui
.imapAccount
->isChecked()) {
97 configureImapAccount();
99 configurePop3Account();
101 Q_EMIT
leavePageNextOk(); // go to the next page
102 mSetupManager
->execute();
105 void PersonalDataPage::slotTextChanged()
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 KEmailAddress::isValidSimpleAddress(ui
.emailEdit
->text()));
113 void PersonalDataPage::leavePageNext()
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());
122 if (ui
.checkOnlineGroupBox
->isChecked()) {
123 // since the user can go back and forth, explicitly disable the man page
124 Q_EMIT
manualWanted(false);
125 setCursor(Qt::BusyCursor
);
126 ui
.mProgress
->start();
127 qCDebug(ACCOUNTWIZARD_LOG
) << "Searching on internet";
129 mIspdb
= new Ispdb(this);
130 connect(mIspdb
, &Ispdb::searchType
, this, &PersonalDataPage::slotSearchType
);
131 mIspdb
->setEmail(ui
.emailEdit
->text());
134 connect(mIspdb
, &Ispdb::finished
, this, &PersonalDataPage::ispdbSearchFinished
);
136 Q_EMIT
manualWanted(true); // enable the manual page
137 Q_EMIT
leavePageNextOk(); // go to the next page
141 void PersonalDataPage::ispdbSearchFinished(bool ok
)
143 qCDebug(ACCOUNTWIZARD_LOG
) << ok
;
146 ui
.mProgress
->stop();
149 if (!mIspdb
->imapServers().isEmpty() && !mIspdb
->pop3Servers().isEmpty()) {
150 ui
.stackedPage
->setCurrentIndex(1);
151 slotRadioButtonClicked(ui
.imapAccount
);
153 automaticConfigureAccount();
157 Q_EMIT
manualWanted(true); // enable the manual page
158 Q_EMIT
leavePageNextOk();
162 void PersonalDataPage::slotSearchType(const QString
&type
)
164 ui
.mProgress
->setActiveLabel(type
);
167 void PersonalDataPage::configureSmtpAccount()
169 if (!mIspdb
->smtpServers().isEmpty()) {
170 Server s
= mIspdb
->smtpServers().at(0); // should be ok.
171 qCDebug(ACCOUNTWIZARD_LOG
) << "Configuring transport for" << s
.hostname
;
173 QObject
*object
= mSetupManager
->createTransport(QStringLiteral("smtp"));
174 Transport
*t
= qobject_cast
<Transport
*>(object
);
175 t
->setName(accountName(mIspdb
, s
.username
));
176 t
->setHost(s
.hostname
);
178 t
->setUsername(s
.username
);
179 t
->setPassword(ui
.passwordEdit
->text());
180 switch (s
.authentication
) {
181 case Ispdb::Plain
: t
->setAuthenticationType(QStringLiteral("plain")); break;
182 case Ispdb::CramMD5
: t
->setAuthenticationType(QStringLiteral("cram-md5")); break;
183 case Ispdb::NTLM
: t
->setAuthenticationType(QStringLiteral("ntlm")); break;
184 case Ispdb::GSSAPI
: t
->setAuthenticationType(QStringLiteral("gssapi")); break;
185 case Ispdb::ClientIP
: break;
186 case Ispdb::NoAuth
: break;
189 switch (s
.socketType
) {
190 case Ispdb::None
: t
->setEncryption(QStringLiteral("none")); break;
191 case Ispdb::SSL
: t
->setEncryption(QStringLiteral("ssl")); break;
192 case Ispdb::StartTLS
: t
->setEncryption(QStringLiteral("tls")); break;
196 qCDebug(ACCOUNTWIZARD_LOG
) << "No transport to be created....";
200 void PersonalDataPage::configureImapAccount()
202 if (!mIspdb
->imapServers().isEmpty()) {
203 Server s
= mIspdb
->imapServers().at(0); // should be ok.
204 qCDebug(ACCOUNTWIZARD_LOG
) << "Configuring imap for" << s
.hostname
;
206 QObject
*object
= mSetupManager
->createResource(QStringLiteral("akonadi_imap_resource"));
207 Resource
*t
= qobject_cast
<Resource
*>(object
);
208 t
->setName(accountName(mIspdb
, s
.username
));
209 t
->setOption(QStringLiteral("ImapServer"), s
.hostname
);
210 t
->setOption(QStringLiteral("ImapPort"), s
.port
);
211 t
->setOption(QStringLiteral("UserName"), s
.username
);
212 t
->setOption(QStringLiteral("Password"), ui
.passwordEdit
->text());
213 switch (s
.authentication
) {
214 case Ispdb::Plain
: t
->setOption(QStringLiteral("Authentication"), MailTransport::Transport::EnumAuthenticationType::CLEAR
); break;
215 case Ispdb::CramMD5
: t
->setOption(QStringLiteral("Authentication"), MailTransport::Transport::EnumAuthenticationType::CRAM_MD5
); break;
216 case Ispdb::NTLM
: t
->setOption(QStringLiteral("Authentication"), MailTransport::Transport::EnumAuthenticationType::NTLM
); break;
217 case Ispdb::GSSAPI
: t
->setOption(QStringLiteral("Authentication"), MailTransport::Transport::EnumAuthenticationType::GSSAPI
); break;
218 case Ispdb::ClientIP
: break;
219 case Ispdb::NoAuth
: break;
222 switch (s
.socketType
) {
223 case Ispdb::None
: t
->setOption(QStringLiteral("Safety"), QStringLiteral("None")); break;
224 case Ispdb::SSL
: t
->setOption(QStringLiteral("Safety"), QStringLiteral("SSL")); break;
225 case Ispdb::StartTLS
: t
->setOption(QStringLiteral("Safety"), QStringLiteral("STARTTLS")); break;
231 void PersonalDataPage::configurePop3Account()
233 if (!mIspdb
->pop3Servers().isEmpty()) {
234 Server s
= mIspdb
->pop3Servers().at(0); // should be ok.
235 qCDebug(ACCOUNTWIZARD_LOG
) << "No Imap to be created, configuring pop3 for" << s
.hostname
;
237 QObject
*object
= mSetupManager
->createResource(QStringLiteral("akonadi_pop3_resource"));
238 Resource
*t
= qobject_cast
<Resource
*>(object
);
239 t
->setName(accountName(mIspdb
, s
.username
));
240 t
->setOption(QStringLiteral("Host"), s
.hostname
);
241 t
->setOption(QStringLiteral("Port"), s
.port
);
242 t
->setOption(QStringLiteral("Login"), s
.username
);
243 t
->setOption(QStringLiteral("Password"), ui
.passwordEdit
->text());
244 switch (s
.authentication
) {
245 case Ispdb::Plain
: t
->setOption(QStringLiteral("AuthenticationMethod"), MailTransport::Transport::EnumAuthenticationType::PLAIN
); break;
246 case Ispdb::CramMD5
: t
->setOption(QStringLiteral("AuthenticationMethod"), MailTransport::Transport::EnumAuthenticationType::CRAM_MD5
); break;
247 case Ispdb::NTLM
: t
->setOption(QStringLiteral("AuthenticationMethod"), MailTransport::Transport::EnumAuthenticationType::NTLM
); break;
248 case Ispdb::GSSAPI
: t
->setOption(QStringLiteral("AuthenticationMethod"), MailTransport::Transport::EnumAuthenticationType::GSSAPI
); break;
249 case Ispdb::ClientIP
:
251 default: t
->setOption(QStringLiteral("AuthenticationMethod"), MailTransport::Transport::EnumAuthenticationType::CLEAR
); break;
253 switch (s
.socketType
) {
254 case Ispdb::SSL
: t
->setOption(QStringLiteral("UseSSL"), 1); break;
255 case Ispdb::StartTLS
: t
->setOption(QStringLiteral("UseTLS"), 1); break;
257 default: t
->setOption(QStringLiteral("UseTLS"), 1); break;
262 void PersonalDataPage::automaticConfigureAccount()
264 configureSmtpAccount();
265 configureImapAccount();
266 configurePop3Account();
267 Q_EMIT
leavePageNextOk(); // go to the next page
268 mSetupManager
->execute();
271 void PersonalDataPage::leavePageNextRequested()
273 // override base class with doing nothing...