27 #include "smtpconfigwidget.h"
28 #include "transportconfigwidget_p.h"
29 #include "transport.h"
30 #include "transportmanager.h"
31 #include "servertest.h"
34 #ifndef KDEPIM_MOBILE_UI
35 #include "ui_smtpsettings_desktop.h"
37 #include "ui_smtpsettings_mobile.h"
40 #include <QAbstractButton>
41 #include <QButtonGroup>
43 #include <KProtocolInfo>
48 class BusyCursorHelper :
public QObject
51 inline BusyCursorHelper( QObject *parent ) : QObject( parent )
54 qApp->setOverrideCursor( Qt::BusyCursor );
58 inline ~BusyCursorHelper()
61 qApp->restoreOverrideCursor();
68 using namespace MailTransport;
73 ::Ui::SMTPSettings ui;
76 QButtonGroup *encryptionGroup;
79 QList<int> noEncCapa, sslCapa, tlsCapa;
81 bool serverTestFailed;
83 static void addAuthenticationItem( KComboBox *combo,
84 int authenticationType )
87 QVariant( authenticationType ) );
90 void resetAuthCapabilities()
93 noEncCapa << Transport::EnumAuthenticationType::LOGIN
94 << Transport::EnumAuthenticationType::PLAIN
95 << Transport::EnumAuthenticationType::CRAM_MD5
96 << Transport::EnumAuthenticationType::DIGEST_MD5
97 << Transport::EnumAuthenticationType::NTLM
98 << Transport::EnumAuthenticationType::GSSAPI;
99 sslCapa = tlsCapa = noEncCapa;
100 updateAuthCapbilities();
104 void updateAuthCapbilities()
106 if ( serverTestFailed ) {
110 QList<int> capa = noEncCapa;
111 if ( ui.ssl->isChecked() ) {
113 }
else if ( ui.tls->isChecked() ) {
117 ui.authCombo->clear();
118 foreach (
int authType, capa ) {
119 addAuthenticationItem( ui.authCombo, authType );
122 if ( transport->isValid() ) {
123 const int idx = ui.authCombo->findData( transport->authenticationType() );
126 ui.authCombo->setCurrentIndex( idx );
130 if ( capa.count() == 0 ) {
131 ui.noAuthPossible->setVisible(
true );
132 ui.kcfg_requiresAuthentication->setChecked(
false );
133 ui.kcfg_requiresAuthentication->setEnabled(
false );
134 ui.kcfg_requiresAuthentication->setVisible(
false );
135 ui.authCombo->setEnabled(
false );
136 ui.authLabel->setEnabled(
false );
138 ui.noAuthPossible->setVisible(
false );
139 ui.kcfg_requiresAuthentication->setEnabled(
true );
140 ui.kcfg_requiresAuthentication->setVisible(
true );
141 ui.authCombo->setEnabled(
true );
142 ui.authLabel->setEnabled(
true );
147 SMTPConfigWidget::SMTPConfigWidget(
Transport *transport, QWidget *parent )
153 SMTPConfigWidget::SMTPConfigWidget( SMTPConfigWidgetPrivate &dd,
160 static void checkHighestEnabledButton( QButtonGroup *group )
164 for (
int i = group->buttons().count() - 1; i >= 0; --i ) {
165 QAbstractButton *b = group->buttons().at( i );
166 if ( b && b->isEnabled() ) {
173 void SMTPConfigWidget::init()
179 SLOT(passwordsLoaded()) );
181 d->serverTestFailed =
false;
183 d->ui.setupUi(
this );
184 d->manager->addWidget(
this );
185 d->manager->updateWidgets();
187 d->encryptionGroup =
new QButtonGroup(
this );
188 d->encryptionGroup->addButton( d->ui.none, Transport::EnumEncryption::None );
189 d->encryptionGroup->addButton( d->ui.ssl, Transport::EnumEncryption::SSL );
190 d->encryptionGroup->addButton( d->ui.tls, Transport::EnumEncryption::TLS );
192 d->resetAuthCapabilities();
194 if ( KProtocolInfo::capabilities( SMTP_PROTOCOL ).contains( QLatin1String(
"SASL" ) ) == 0 ) {
195 d->ui.authCombo->removeItem( d->ui.authCombo->findData(
196 Transport::EnumAuthenticationType::NTLM ) );
197 d->ui.authCombo->removeItem( d->ui.authCombo->findData(
198 Transport::EnumAuthenticationType::GSSAPI ) );
201 connect( d->ui.checkCapabilities, SIGNAL(clicked()),
202 SLOT(checkSmtpCapabilities()) );
203 connect( d->ui.kcfg_host, SIGNAL(textChanged(QString)),
204 SLOT(hostNameChanged(QString)) );
205 connect( d->encryptionGroup, SIGNAL(buttonClicked(
int)),
206 SLOT(encryptionChanged(
int)) );
207 connect( d->ui.kcfg_requiresAuthentication, SIGNAL(toggled(
bool)),
208 SLOT(ensureValidAuthSelection()) );
210 if ( !d->transport->isValid() ) {
211 checkHighestEnabledButton( d->encryptionGroup );
215 d->transport->updatePasswordState();
216 if ( d->transport->isComplete() ) {
217 d->ui.password->setText( d->transport->password() );
219 if ( d->transport->requiresAuthentication() ) {
224 hostNameChanged( d->transport->host() );
226 #ifdef KDEPIM_MOBILE_UI
227 d->ui.smtpSettingsGroupBox->hide();
231 void SMTPConfigWidget::checkSmtpCapabilities()
236 d->serverTest->setProtocol( SMTP_PROTOCOL );
237 d->serverTest->setServer( d->ui.kcfg_host->text().trimmed() );
238 if ( d->ui.kcfg_specifyHostname->isChecked() ) {
239 d->serverTest->setFakeHostname( d->ui.kcfg_localHostname->text() );
241 d->serverTest->setProgressBar( d->ui.checkCapabilitiesProgress );
242 d->ui.checkCapabilitiesStack->setCurrentIndex( 1 );
243 BusyCursorHelper *busyCursorHelper =
new BusyCursorHelper( d->serverTest );
245 connect( d->serverTest, SIGNAL(finished(QList<int>)),
246 SLOT(slotFinished(QList<int>)));
247 connect( d->serverTest, SIGNAL(finished(QList<int>)),
248 busyCursorHelper, SLOT(deleteLater()) );
249 d->ui.checkCapabilities->setEnabled(
false );
250 d->serverTest->start();
251 d->serverTestFailed =
false;
257 Q_ASSERT( d->manager );
258 d->manager->updateSettings();
259 d->transport->setPassword( d->ui.password->text() );
261 KConfigGroup group( d->transport->config(), d->transport->currentGroup() );
262 const int index = d->ui.authCombo->currentIndex();
264 group.writeEntry(
"authtype", d->ui.authCombo->itemData( index ).toInt() );
270 void SMTPConfigWidget::passwordsLoaded()
275 d->transport->updatePasswordState();
277 if ( d->ui.password->text().isEmpty() ) {
278 d->ui.password->setText( d->transport->password() );
283 void SMTPConfigWidget::slotFinished( QList<int> results )
287 d->ui.checkCapabilitiesStack->setCurrentIndex( 0 );
289 d->ui.checkCapabilities->setEnabled(
true );
290 d->serverTest->deleteLater();
294 if ( results.isEmpty() ) {
295 d->serverTestFailed =
true;
300 d->ui.none->setEnabled( results.contains( Transport::EnumEncryption::None ) );
301 d->ui.ssl->setEnabled( results.contains( Transport::EnumEncryption::SSL ) );
302 d->ui.tls->setEnabled( results.contains( Transport::EnumEncryption::TLS ) );
303 checkHighestEnabledButton( d->encryptionGroup );
305 d->noEncCapa = d->serverTest->normalProtocols();
306 if ( d->ui.tls->isEnabled() ) {
307 d->tlsCapa = d->serverTest->tlsProtocols();
311 d->sslCapa = d->serverTest->secureProtocols();
312 d->updateAuthCapbilities();
315 void SMTPConfigWidget::hostNameChanged(
const QString &text )
322 int pos = d->ui.kcfg_host->cursorPosition();
323 d->ui.kcfg_host->blockSignals(
true );
324 d->ui.kcfg_host->setText( text.trimmed() );
325 d->ui.kcfg_host->blockSignals(
false );
326 d->ui.kcfg_host->setCursorPosition( pos );
328 d->resetAuthCapabilities();
329 for (
int i = 0; d->encryptionGroup && i < d->encryptionGroup->buttons().count(); i++ ) {
330 d->encryptionGroup->buttons().at( i )->setEnabled(
true );
334 void SMTPConfigWidget::ensureValidAuthSelection()
339 d->updateAuthCapbilities();
342 void SMTPConfigWidget::encryptionChanged(
int enc )
348 if ( enc == Transport::EnumEncryption::SSL ) {
349 if ( d->ui.kcfg_port->value() == SMTP_PORT ) {
350 d->ui.kcfg_port->setValue( SMTPS_PORT );
353 if ( d->ui.kcfg_port->value() == SMTPS_PORT ) {
354 d->ui.kcfg_port->setValue( SMTP_PORT );
358 ensureValidAuthSelection();