14#include "smtpconfigwidget.h"
15#include "ui_smtpsettings.h"
18#include "mailtransportplugin_smtp_debug.h"
19#include "servertest.h"
21#include "transportmanager.h"
22#include "widgets/transportconfigwidget_p.h"
24#include <QAbstractButton>
25#include <QButtonGroup>
27#include "mailtransport_debug.h"
29#include <KLocalizedString>
31#include <KProtocolInfo>
33using namespace MailTransport;
35class MailTransport::SMTPConfigWidgetPrivate :
public TransportConfigWidgetPrivate
38 ::Ui::SMTPSettings ui;
46 bool serverTestFailed;
48 static void addAuthenticationItem(
QComboBox *combo,
int authenticationType)
53 void resetAuthCapabilities()
56 noEncCapa << Transport::EnumAuthenticationType::LOGIN << Transport::EnumAuthenticationType::PLAIN << Transport::EnumAuthenticationType::CRAM_MD5
57 << Transport::EnumAuthenticationType::DIGEST_MD5 << Transport::EnumAuthenticationType::NTLM << Transport::EnumAuthenticationType::GSSAPI
58 << Transport::EnumAuthenticationType::XOAUTH2;
59 sslCapa = tlsCapa = noEncCapa;
60 updateAuthCapbilities();
63 void enablePasswordLine()
65 ui.password->setEnabled(ui.kcfg_storePassword->isChecked());
68 void updateAuthCapbilities()
70 if (serverTestFailed) {
75 if (ui.encryptionSsl->isChecked()) {
77 }
else if (ui.encryptionTls->isChecked()) {
81 ui.authCombo->
clear();
82 for (
int authType :
std::as_const(capa)) {
83 addAuthenticationItem(ui.authCombo, authType);
86 if (transport->isValid()) {
87 const int idx = ui.authCombo->findData(transport->authenticationType());
90 ui.authCombo->setCurrentIndex(idx);
95 ui.noAuthPossible->setVisible(
true);
96 ui.kcfg_requiresAuthentication->setChecked(
false);
97 ui.kcfg_requiresAuthentication->setEnabled(
false);
98 ui.kcfg_requiresAuthentication->setVisible(
false);
99 ui.authCombo->setEnabled(
false);
100 ui.authLabel->setEnabled(
false);
102 ui.noAuthPossible->setVisible(
false);
103 ui.kcfg_requiresAuthentication->setEnabled(
true);
104 ui.kcfg_requiresAuthentication->setVisible(
true);
105 ui.authCombo->setEnabled(
true);
106 ui.authLabel->setEnabled(
true);
107 enablePasswordLine();
118static void checkHighestEnabledButton(
QButtonGroup *group)
122 for (
int i = group->
buttons().count() - 1; i >= 0; --i) {
131void SMTPConfigWidget::init()
134 d->serverTest =
nullptr;
138 d->serverTestFailed =
false;
141 d->ui.password->setRevealPasswordMode(
KAuthorized::authorize(QStringLiteral(
"lineedit_reveal_password")) ? KPassword::RevealMode::OnlyNew
142 : KPassword::RevealMode::Never);
143 d->manager->addWidget(
this);
144 d->manager->updateWidgets();
146 d->ui.password->setWhatsThis(
i18n(
"The password to send to the server for authorization."));
148 d->ui.kcfg_userName->setClearButtonEnabled(
true);
150 d->encryptionGroup->addButton(d->ui.encryptionNone, Transport::EnumEncryption::None);
151 d->encryptionGroup->addButton(d->ui.encryptionSsl, Transport::EnumEncryption::SSL);
152 d->encryptionGroup->addButton(d->ui.encryptionTls, Transport::EnumEncryption::TLS);
154 d->ui.encryptionNone->setChecked(d->transport->encryption() == Transport::EnumEncryption::None);
155 d->ui.encryptionSsl->setChecked(d->transport->encryption() == Transport::EnumEncryption::SSL);
156 d->ui.encryptionTls->setChecked(d->transport->encryption() == Transport::EnumEncryption::TLS);
158 d->ui.checkCapabilitiesProgress->setFormat(
i18nc(
"Percent value; %p is the value, % is the percent sign",
"%p%"));
160 d->resetAuthCapabilities();
162 if (!KProtocolInfo::capabilities(SMTP_PROTOCOL).contains(
QLatin1StringView(
"SASL"))) {
163 d->ui.authCombo->removeItem(d->ui.authCombo->findData(Transport::EnumAuthenticationType::NTLM));
164 d->ui.authCombo->removeItem(d->ui.authCombo->findData(Transport::EnumAuthenticationType::GSSAPI));
173 if (!d->transport->isValid()) {
174 checkHighestEnabledButton(d->encryptionGroup);
178 d->transport->updatePasswordState();
179 if (d->transport->isComplete()) {
180 d->ui.password->setPassword(d->transport->password());
182 if (d->transport->requiresAuthentication()) {
187 hostNameChanged(d->transport->host());
190void SMTPConfigWidget::enablePasswordLine()
193 d->enablePasswordLine();
196void SMTPConfigWidget::checkSmtpCapabilities()
201 d->serverTest->setProtocol(SMTP_PROTOCOL);
202 d->serverTest->setServer(d->ui.kcfg_host->text().trimmed());
203 if (d->ui.kcfg_specifyHostname->isChecked()) {
204 d->serverTest->setFakeHostname(d->ui.kcfg_localHostname->text());
206 QAbstractButton *encryptionChecked = d->encryptionGroup->checkedButton();
207 if (encryptionChecked == d->ui.encryptionNone) {
208 d->serverTest->setPort(Transport::EnumEncryption::None, d->ui.kcfg_port->value());
209 }
else if (encryptionChecked == d->ui.encryptionSsl) {
210 d->serverTest->setPort(Transport::EnumEncryption::SSL, d->ui.kcfg_port->value());
212 d->serverTest->setProgressBar(d->ui.checkCapabilitiesProgress);
213 d->ui.checkCapabilitiesStack->setCurrentIndex(1);
218 qApp->restoreOverrideCursor();
220 d->ui.checkCapabilities->setEnabled(
false);
221 d->serverTest->start();
222 d->serverTestFailed =
false;
228 Q_ASSERT(d->manager);
229 d->manager->updateSettings();
230 if (!d->ui.kcfg_storePassword->isChecked() && d->ui.kcfg_requiresAuthentication->isChecked()) {
234 d->transport->setPassword(d->ui.password->password());
236 KConfigGroup group(d->transport->config(), d->transport->currentGroup());
237 const int index = d->ui.authCombo->currentIndex();
239 group.
writeEntry(
"authtype", d->ui.authCombo->itemData(index).toInt());
242 if (d->ui.encryptionNone->isChecked()) {
243 d->transport->setEncryption(Transport::EnumEncryption::None);
244 }
else if (d->ui.encryptionSsl->isChecked()) {
245 d->transport->setEncryption(Transport::EnumEncryption::SSL);
246 }
else if (d->ui.encryptionTls->isChecked()) {
247 d->transport->setEncryption(Transport::EnumEncryption::TLS);
253void SMTPConfigWidget::passwordsLoaded()
258 d->transport->updatePasswordState();
260 if (d->ui.password->password().isEmpty()) {
261 d->ui.password->setPassword(d->transport->password());
266void SMTPConfigWidget::slotFinished(
const QList<int> &results)
270 d->ui.checkCapabilitiesStack->setCurrentIndex(0);
272 d->ui.checkCapabilities->setEnabled(
true);
273 d->serverTest->deleteLater();
278 KMessageBox::error(
this,
i18n(
"Failed to check capabilities. Please verify port and authentication mode."),
i18n(
"Check Capabilities Failed"));
279 d->serverTestFailed =
true;
280 d->serverTest->deleteLater();
285 d->ui.encryptionNone->setEnabled(results.
contains(Transport::EnumEncryption::None));
286 d->ui.encryptionSsl->setEnabled(results.
contains(Transport::EnumEncryption::SSL));
287 d->ui.encryptionTls->setEnabled(results.
contains(Transport::EnumEncryption::TLS));
288 checkHighestEnabledButton(d->encryptionGroup);
290 d->noEncCapa = d->serverTest->normalProtocols();
291 if (d->ui.encryptionTls->isEnabled()) {
292 d->tlsCapa = d->serverTest->tlsProtocols();
296 d->sslCapa = d->serverTest->secureProtocols();
297 d->updateAuthCapbilities();
299 if (d->ui.encryptionSsl->isEnabled()) {
300 const int portValue = d->serverTest->port(Transport::EnumEncryption::SSL);
301 d->ui.kcfg_port->setValue(portValue == -1 ? SMTPS_PORT : portValue);
302 }
else if (d->ui.encryptionNone->isEnabled()) {
303 const int portValue = d->serverTest->port(Transport::EnumEncryption::None);
304 d->ui.kcfg_port->setValue(portValue == -1 ? SMTP_PORT : portValue);
306 d->serverTest->deleteLater();
309void SMTPConfigWidget::hostNameChanged(
const QString &text)
316 const int pos = d->ui.kcfg_host->cursorPosition();
317 d->ui.kcfg_host->blockSignals(
true);
318 d->ui.kcfg_host->setText(text.
trimmed());
319 d->ui.kcfg_host->blockSignals(
false);
320 d->ui.kcfg_host->setCursorPosition(
pos);
322 d->resetAuthCapabilities();
323 if (d->encryptionGroup) {
324 for (
int i = 0; i < d->encryptionGroup->buttons().count(); ++i) {
325 d->encryptionGroup->buttons().at(i)->setEnabled(
true);
330void SMTPConfigWidget::ensureValidAuthSelection()
335 d->updateAuthCapbilities();
336 d->enablePasswordLine();
339void SMTPConfigWidget::encryptionAbstractButtonChanged(
QAbstractButton *button)
343 encryptionChanged(d->encryptionGroup->id(button));
347void SMTPConfigWidget::encryptionChanged(
int enc)
350 qCDebug(MAILTRANSPORT_SMTP_LOG) << enc;
353 if (enc == Transport::EnumEncryption::SSL) {
354 if (d->ui.kcfg_port->value() == SMTP_PORT) {
355 d->ui.kcfg_port->setValue(SMTPS_PORT);
358 if (d->ui.kcfg_port->value() == SMTPS_PORT) {
359 d->ui.kcfg_port->setValue(SMTP_PORT);
363 ensureValidAuthSelection();
366#include "moc_smtpconfigwidget.cpp"
static Q_INVOKABLE bool authorize(const QString &action)
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
This class can be used to test certain server to see if they support stuff.
void finished(const QList< int > &)
This will be emitted when the test is done.
void loadPasswordsAsync()
Tries to load passwords asynchronously from KWallet if needed.
void passwordsChanged()
Emitted when passwords have been loaded from the wallet.
static TransportManager * self()
Returns the TransportManager instance.
Represents the settings of a specific mail transport.
QString authenticationTypeString() const
Returns a string representation of the authentication type.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
Internal file containing constant definitions etc.
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
void addItem(const QIcon &icon, const QString &text, const QVariant &userData)
void textChanged(const QString &text)
bool contains(const AT &value) const const
bool isEmpty() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QString trimmed() const const