14 #include "smtpconfigwidget.h"
15 #include "ui_smtpsettings.h"
18 #include "mailtransportplugin_smtp_debug.h"
19 #include "servertest.h"
20 #include "transport.h"
21 #include "transportmanager.h"
22 #include "widgets/transportconfigwidget_p.h"
24 #include <QAbstractButton>
25 #include <QButtonGroup>
27 #include "mailtransport_debug.h"
28 #include <KAuthorized>
29 #include <KMessageBox>
30 #include <KProtocolInfo>
32 using namespace MailTransport;
34 class MailTransport::SMTPConfigWidgetPrivate :
public TransportConfigWidgetPrivate
37 ::Ui::SMTPSettings ui;
45 bool serverTestFailed;
47 static void addAuthenticationItem(
QComboBox *combo,
int authenticationType)
52 void resetAuthCapabilities()
55 noEncCapa << Transport::EnumAuthenticationType::LOGIN << Transport::EnumAuthenticationType::PLAIN << Transport::EnumAuthenticationType::CRAM_MD5
56 << Transport::EnumAuthenticationType::DIGEST_MD5 << Transport::EnumAuthenticationType::NTLM << Transport::EnumAuthenticationType::GSSAPI
57 << Transport::EnumAuthenticationType::XOAUTH2;
58 sslCapa = tlsCapa = noEncCapa;
59 updateAuthCapbilities();
62 void enablePasswordLine()
64 ui.password->setEnabled(ui.kcfg_storePassword->isChecked());
67 void updateAuthCapbilities()
69 if (serverTestFailed) {
74 if (ui.encryptionSsl->isChecked()) {
76 }
else if (ui.encryptionTls->isChecked()) {
80 ui.authCombo->
clear();
81 for (
int authType : std::as_const(capa)) {
82 addAuthenticationItem(ui.authCombo, authType);
85 if (transport->isValid()) {
86 const int idx = ui.authCombo->findData(transport->authenticationType());
89 ui.authCombo->setCurrentIndex(idx);
94 ui.noAuthPossible->setVisible(
true);
95 ui.kcfg_requiresAuthentication->setChecked(
false);
96 ui.kcfg_requiresAuthentication->setEnabled(
false);
97 ui.kcfg_requiresAuthentication->setVisible(
false);
98 ui.authCombo->setEnabled(
false);
99 ui.authLabel->setEnabled(
false);
101 ui.noAuthPossible->setVisible(
false);
102 ui.kcfg_requiresAuthentication->setEnabled(
true);
103 ui.kcfg_requiresAuthentication->setVisible(
true);
104 ui.authCombo->setEnabled(
true);
105 ui.authLabel->setEnabled(
true);
106 enablePasswordLine();
117 static void checkHighestEnabledButton(
QButtonGroup *group)
121 for (
int i = group->
buttons().count() - 1; i >= 0; --i) {
130 void SMTPConfigWidget::init()
133 d->serverTest =
nullptr;
137 d->serverTestFailed =
false;
140 d->ui.password->setRevealPasswordAvailable(
KAuthorized::authorize(QStringLiteral(
"lineedit_reveal_password")));
141 d->manager->addWidget(
this);
142 d->manager->updateWidgets();
144 d->ui.password->setWhatsThis(
i18n(
"The password to send to the server for authorization."));
146 d->ui.kcfg_userName->setClearButtonEnabled(
true);
148 d->encryptionGroup->addButton(d->ui.encryptionNone, Transport::EnumEncryption::None);
149 d->encryptionGroup->addButton(d->ui.encryptionSsl, Transport::EnumEncryption::SSL);
150 d->encryptionGroup->addButton(d->ui.encryptionTls, Transport::EnumEncryption::TLS);
152 d->ui.encryptionNone->setChecked(d->transport->encryption() == Transport::EnumEncryption::None);
153 d->ui.encryptionSsl->setChecked(d->transport->encryption() == Transport::EnumEncryption::SSL);
154 d->ui.encryptionTls->setChecked(d->transport->encryption() == Transport::EnumEncryption::TLS);
156 d->resetAuthCapabilities();
159 d->ui.authCombo->removeItem(d->ui.authCombo->findData(Transport::EnumAuthenticationType::NTLM));
160 d->ui.authCombo->removeItem(d->ui.authCombo->findData(Transport::EnumAuthenticationType::GSSAPI));
169 if (!d->transport->isValid()) {
170 checkHighestEnabledButton(d->encryptionGroup);
174 d->transport->updatePasswordState();
175 if (d->transport->isComplete()) {
176 d->ui.password->setPassword(d->transport->password());
178 if (d->transport->requiresAuthentication()) {
183 hostNameChanged(d->transport->host());
186 void SMTPConfigWidget::enablePasswordLine()
189 d->enablePasswordLine();
192 void SMTPConfigWidget::checkSmtpCapabilities()
197 d->serverTest->setProtocol(SMTP_PROTOCOL);
198 d->serverTest->setServer(d->ui.kcfg_host->text().trimmed());
199 if (d->ui.kcfg_specifyHostname->isChecked()) {
200 d->serverTest->setFakeHostname(d->ui.kcfg_localHostname->text());
202 QAbstractButton *encryptionChecked = d->encryptionGroup->checkedButton();
203 if (encryptionChecked == d->ui.encryptionNone) {
204 d->serverTest->setPort(Transport::EnumEncryption::None, d->ui.kcfg_port->value());
205 }
else if (encryptionChecked == d->ui.encryptionSsl) {
206 d->serverTest->setPort(Transport::EnumEncryption::SSL, d->ui.kcfg_port->value());
208 d->serverTest->setProgressBar(d->ui.checkCapabilitiesProgress);
209 d->ui.checkCapabilitiesStack->setCurrentIndex(1);
214 qApp->restoreOverrideCursor();
216 d->ui.checkCapabilities->setEnabled(
false);
217 d->serverTest->start();
218 d->serverTestFailed =
false;
224 Q_ASSERT(d->manager);
225 d->manager->updateSettings();
226 if (!d->ui.kcfg_storePassword->isChecked() && d->ui.kcfg_requiresAuthentication->isChecked()) {
230 d->transport->setPassword(d->ui.password->password());
232 KConfigGroup group(d->transport->config(), d->transport->currentGroup());
233 const int index = d->ui.authCombo->currentIndex();
235 group.
writeEntry(
"authtype", d->ui.authCombo->itemData(index).toInt());
238 if (d->ui.encryptionNone->isChecked()) {
239 d->transport->setEncryption(Transport::EnumEncryption::None);
240 }
else if (d->ui.encryptionSsl->isChecked()) {
241 d->transport->setEncryption(Transport::EnumEncryption::SSL);
242 }
else if (d->ui.encryptionTls->isChecked()) {
243 d->transport->setEncryption(Transport::EnumEncryption::TLS);
249 void SMTPConfigWidget::passwordsLoaded()
254 d->transport->updatePasswordState();
256 if (d->ui.password->password().isEmpty()) {
257 d->ui.password->setPassword(d->transport->password());
262 void SMTPConfigWidget::slotFinished(
const QList<int> &results)
266 d->ui.checkCapabilitiesStack->setCurrentIndex(0);
268 d->ui.checkCapabilities->setEnabled(
true);
269 d->serverTest->deleteLater();
274 KMessageBox::error(
this,
i18n(
"Failed to check capabilities. Please verify port and authentication mode."),
i18n(
"Check Capabilities Failed"));
275 d->serverTestFailed =
true;
276 d->serverTest->deleteLater();
281 d->ui.encryptionNone->setEnabled(results.
contains(Transport::EnumEncryption::None));
282 d->ui.encryptionSsl->setEnabled(results.
contains(Transport::EnumEncryption::SSL));
283 d->ui.encryptionTls->setEnabled(results.
contains(Transport::EnumEncryption::TLS));
284 checkHighestEnabledButton(d->encryptionGroup);
286 d->noEncCapa = d->serverTest->normalProtocols();
287 if (d->ui.encryptionTls->isEnabled()) {
288 d->tlsCapa = d->serverTest->tlsProtocols();
292 d->sslCapa = d->serverTest->secureProtocols();
293 d->updateAuthCapbilities();
295 if (d->ui.encryptionSsl->isEnabled()) {
296 const int portValue = d->serverTest->port(Transport::EnumEncryption::SSL);
297 d->ui.kcfg_port->setValue(portValue == -1 ? SMTPS_PORT : portValue);
298 }
else if (d->ui.encryptionNone->isEnabled()) {
299 const int portValue = d->serverTest->port(Transport::EnumEncryption::None);
300 d->ui.kcfg_port->setValue(portValue == -1 ? SMTP_PORT : portValue);
302 d->serverTest->deleteLater();
305 void SMTPConfigWidget::hostNameChanged(
const QString &text)
312 const int pos = d->ui.kcfg_host->cursorPosition();
313 d->ui.kcfg_host->blockSignals(
true);
314 d->ui.kcfg_host->setText(text.
trimmed());
315 d->ui.kcfg_host->blockSignals(
false);
316 d->ui.kcfg_host->setCursorPosition(
pos);
318 d->resetAuthCapabilities();
319 if (d->encryptionGroup) {
320 for (
int i = 0; i < d->encryptionGroup->buttons().count(); ++i) {
321 d->encryptionGroup->buttons().at(i)->setEnabled(
true);
326 void SMTPConfigWidget::ensureValidAuthSelection()
331 d->updateAuthCapbilities();
332 d->enablePasswordLine();
335 void SMTPConfigWidget::encryptionAbstractButtonChanged(
QAbstractButton *button)
339 encryptionChanged(d->encryptionGroup->id(button));
343 void SMTPConfigWidget::encryptionChanged(
int enc)
346 qCDebug(MAILTRANSPORT_SMTP_LOG) << enc;
349 if (enc == Transport::EnumEncryption::SSL) {
350 if (d->ui.kcfg_port->value() == SMTP_PORT) {
351 d->ui.kcfg_port->setValue(SMTPS_PORT);
354 if (d->ui.kcfg_port->value() == SMTPS_PORT) {
355 d->ui.kcfg_port->setValue(SMTP_PORT);
359 ensureValidAuthSelection();
362 #include "moc_smtpconfigwidget.cpp"