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>
49 class BusyCursorHelper :
public QObject
52 inline BusyCursorHelper( QObject *parent ) : QObject( parent )
55 qApp->setOverrideCursor( Qt::BusyCursor );
59 inline ~BusyCursorHelper()
62 qApp->restoreOverrideCursor();
69 using namespace MailTransport;
74 ::Ui::SMTPSettings ui;
77 QButtonGroup *encryptionGroup;
80 QList<int> noEncCapa, sslCapa, tlsCapa;
82 bool serverTestFailed;
84 static void addAuthenticationItem( KComboBox *combo,
85 int authenticationType )
88 QVariant( authenticationType ) );
91 void resetAuthCapabilities()
94 noEncCapa << Transport::EnumAuthenticationType::LOGIN
95 << Transport::EnumAuthenticationType::PLAIN
96 << Transport::EnumAuthenticationType::CRAM_MD5
97 << Transport::EnumAuthenticationType::DIGEST_MD5
98 << Transport::EnumAuthenticationType::NTLM
99 << Transport::EnumAuthenticationType::GSSAPI;
100 sslCapa = tlsCapa = noEncCapa;
101 updateAuthCapbilities();
105 void updateAuthCapbilities()
107 if ( serverTestFailed ) {
111 QList<int> capa = noEncCapa;
112 if ( ui.ssl->isChecked() ) {
114 }
else if ( ui.tls->isChecked() ) {
118 ui.authCombo->clear();
119 foreach (
int authType, capa ) {
120 addAuthenticationItem( ui.authCombo, authType );
124 const int idx = ui.authCombo->findData( transport->authenticationType() );
127 ui.authCombo->setCurrentIndex( idx );
131 if ( capa.count() == 0 ) {
132 ui.noAuthPossible->setVisible(
true );
133 ui.kcfg_requiresAuthentication->setChecked(
false );
134 ui.kcfg_requiresAuthentication->setEnabled(
false );
135 ui.kcfg_requiresAuthentication->setVisible(
false );
136 ui.authCombo->setEnabled(
false );
137 ui.authLabel->setEnabled(
false );
139 ui.noAuthPossible->setVisible(
false );
140 ui.kcfg_requiresAuthentication->setEnabled(
true );
141 ui.kcfg_requiresAuthentication->setVisible(
true );
142 ui.authCombo->setEnabled(
true );
143 ui.authLabel->setEnabled(
true );
148 SMTPConfigWidget::SMTPConfigWidget(
Transport *transport, QWidget *parent )
154 SMTPConfigWidget::SMTPConfigWidget( SMTPConfigWidgetPrivate &dd,
161 static void checkHighestEnabledButton( QButtonGroup *group )
165 for (
int i = group->buttons().count() - 1; i >= 0; --i ) {
166 QAbstractButton *b = group->buttons().at( i );
167 if ( b && b->isEnabled() ) {
174 void SMTPConfigWidget::init()
180 SLOT(passwordsLoaded()) );
182 d->serverTestFailed =
false;
184 d->ui.setupUi(
this );
185 d->manager->addWidget(
this );
186 d->manager->updateWidgets();
188 d->encryptionGroup =
new QButtonGroup(
this );
189 d->encryptionGroup->addButton( d->ui.none, Transport::EnumEncryption::None );
190 d->encryptionGroup->addButton( d->ui.ssl, Transport::EnumEncryption::SSL );
191 d->encryptionGroup->addButton( d->ui.tls, Transport::EnumEncryption::TLS );
193 d->resetAuthCapabilities();
195 if ( KProtocolInfo::capabilities( SMTP_PROTOCOL ).contains( QLatin1String(
"SASL" ) ) == 0 ) {
196 d->ui.authCombo->removeItem( d->ui.authCombo->findData(
197 Transport::EnumAuthenticationType::NTLM ) );
198 d->ui.authCombo->removeItem( d->ui.authCombo->findData(
199 Transport::EnumAuthenticationType::GSSAPI ) );
202 connect( d->ui.checkCapabilities, SIGNAL(clicked()),
203 SLOT(checkSmtpCapabilities()) );
204 connect( d->ui.kcfg_host, SIGNAL(textChanged(QString)),
205 SLOT(hostNameChanged(QString)) );
206 connect( d->encryptionGroup, SIGNAL(buttonClicked(
int)),
207 SLOT(encryptionChanged(
int)) );
208 connect( d->ui.kcfg_requiresAuthentication, SIGNAL(toggled(
bool)),
209 SLOT(ensureValidAuthSelection()) );
211 if ( !d->transport->isValid() ) {
212 checkHighestEnabledButton( d->encryptionGroup );
216 d->transport->updatePasswordState();
217 if ( d->transport->isComplete() ) {
218 d->ui.password->setText( d->transport->password() );
220 if ( d->transport->requiresAuthentication() ) {
225 hostNameChanged( d->transport->host() );
227 #ifdef KDEPIM_MOBILE_UI
228 d->ui.smtpSettingsGroupBox->hide();
232 void SMTPConfigWidget::checkSmtpCapabilities()
237 d->serverTest->setProtocol( SMTP_PROTOCOL );
238 d->serverTest->setServer( d->ui.kcfg_host->text().trimmed() );
239 if ( d->ui.kcfg_specifyHostname->isChecked() ) {
240 d->serverTest->setFakeHostname( d->ui.kcfg_localHostname->text() );
242 QAbstractButton *encryptionChecked = d->encryptionGroup->checkedButton();
243 if (encryptionChecked == d->ui.none) {
244 d->serverTest->setPort( Transport::EnumEncryption::None, d->ui.kcfg_port->value());
245 }
else if (encryptionChecked == d->ui.ssl) {
246 d->serverTest->setPort( Transport::EnumEncryption::SSL, d->ui.kcfg_port->value());
248 d->serverTest->setProgressBar( d->ui.checkCapabilitiesProgress );
249 d->ui.checkCapabilitiesStack->setCurrentIndex( 1 );
250 BusyCursorHelper *busyCursorHelper =
new BusyCursorHelper( d->serverTest );
252 connect( d->serverTest, SIGNAL(finished(QList<int>)),
253 SLOT(slotFinished(QList<int>)));
254 connect( d->serverTest, SIGNAL(finished(QList<int>)),
255 busyCursorHelper, SLOT(deleteLater()) );
256 d->ui.checkCapabilities->setEnabled(
false );
257 d->serverTest->start();
258 d->serverTestFailed =
false;
264 Q_ASSERT( d->manager );
265 d->manager->updateSettings();
266 d->transport->setPassword( d->ui.password->text() );
268 KConfigGroup group( d->transport->config(), d->transport->currentGroup() );
269 const int index = d->ui.authCombo->currentIndex();
271 group.writeEntry(
"authtype", d->ui.authCombo->itemData( index ).toInt() );
277 void SMTPConfigWidget::passwordsLoaded()
282 d->transport->updatePasswordState();
284 if ( d->ui.password->text().isEmpty() ) {
285 d->ui.password->setText( d->transport->password() );
290 void SMTPConfigWidget::slotFinished( QList<int> results )
294 d->ui.checkCapabilitiesStack->setCurrentIndex( 0 );
296 d->ui.checkCapabilities->setEnabled(
true );
297 d->serverTest->deleteLater();
301 if ( results.isEmpty() ) {
302 d->serverTestFailed =
true;
303 d->serverTest->deleteLater();
308 d->ui.none->setEnabled( results.contains( Transport::EnumEncryption::None ) );
309 d->ui.ssl->setEnabled( results.contains( Transport::EnumEncryption::SSL ) );
310 d->ui.tls->setEnabled( results.contains( Transport::EnumEncryption::TLS ) );
311 checkHighestEnabledButton( d->encryptionGroup );
313 d->noEncCapa = d->serverTest->normalProtocols();
314 if ( d->ui.tls->isEnabled() ) {
315 d->tlsCapa = d->serverTest->tlsProtocols();
319 d->sslCapa = d->serverTest->secureProtocols();
320 d->updateAuthCapbilities();
322 if (d->ui.ssl->isEnabled()) {
323 const int portValue = d->serverTest->port(Transport::EnumEncryption::SSL);
324 d->ui.kcfg_port->setValue(portValue == -1 ? SMTPS_PORT : portValue);
325 }
else if (d->ui.none->isEnabled()) {
326 const int portValue = d->serverTest->port(Transport::EnumEncryption::None);
327 d->ui.kcfg_port->setValue(portValue == -1 ? SMTP_PORT : portValue);
329 d->serverTest->deleteLater();
332 void SMTPConfigWidget::hostNameChanged(
const QString &text )
339 int pos = d->ui.kcfg_host->cursorPosition();
340 d->ui.kcfg_host->blockSignals(
true );
341 d->ui.kcfg_host->setText( text.trimmed() );
342 d->ui.kcfg_host->blockSignals(
false );
343 d->ui.kcfg_host->setCursorPosition( pos );
345 d->resetAuthCapabilities();
346 for (
int i = 0; d->encryptionGroup && i < d->encryptionGroup->buttons().count(); i++ ) {
347 d->encryptionGroup->buttons().at( i )->setEnabled(
true );
351 void SMTPConfigWidget::ensureValidAuthSelection()
356 d->updateAuthCapbilities();
359 void SMTPConfigWidget::encryptionChanged(
int enc )
365 if ( enc == Transport::EnumEncryption::SSL ) {
366 if ( d->ui.kcfg_port->value() == SMTP_PORT ) {
367 d->ui.kcfg_port->setValue( SMTPS_PORT );
370 if ( d->ui.kcfg_port->value() == SMTPS_PORT ) {
371 d->ui.kcfg_port->setValue( SMTP_PORT );
375 ensureValidAuthSelection();
static TransportManager * self()
Returns the TransportManager instance.
This class can be used to test certain server to see if they support stuff.
QString authenticationTypeString() const
Returns a string representation of the authentication type.
bool isValid() const
Returns true if this transport is valid, ie.
Internal file containing constant definitions etc.
void loadPasswordsAsync()
Tries to load passwords asynchronously from KWallet if needed.
Represents the settings of a specific mail transport.