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 );
123 if ( transport->isValid() ) {
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 d->serverTest->setProgressBar( d->ui.checkCapabilitiesProgress );
243 d->ui.checkCapabilitiesStack->setCurrentIndex( 1 );
244 BusyCursorHelper *busyCursorHelper =
new BusyCursorHelper( d->serverTest );
246 connect( d->serverTest, SIGNAL(finished(QList<int>)),
247 SLOT(slotFinished(QList<int>)));
248 connect( d->serverTest, SIGNAL(finished(QList<int>)),
249 busyCursorHelper, SLOT(deleteLater()) );
250 d->ui.checkCapabilities->setEnabled(
false );
251 d->serverTest->start();
252 d->serverTestFailed =
false;
258 Q_ASSERT( d->manager );
259 d->manager->updateSettings();
260 d->transport->setPassword( d->ui.password->text() );
262 KConfigGroup group( d->transport->config(), d->transport->currentGroup() );
263 const int index = d->ui.authCombo->currentIndex();
265 group.writeEntry(
"authtype", d->ui.authCombo->itemData( index ).toInt() );
271 void SMTPConfigWidget::passwordsLoaded()
276 d->transport->updatePasswordState();
278 if ( d->ui.password->text().isEmpty() ) {
279 d->ui.password->setText( d->transport->password() );
284 void SMTPConfigWidget::slotFinished( QList<int> results )
288 d->ui.checkCapabilitiesStack->setCurrentIndex( 0 );
290 d->ui.checkCapabilities->setEnabled(
true );
291 d->serverTest->deleteLater();
295 if ( results.isEmpty() ) {
296 d->serverTestFailed =
true;
301 d->ui.none->setEnabled( results.contains( Transport::EnumEncryption::None ) );
302 d->ui.ssl->setEnabled( results.contains( Transport::EnumEncryption::SSL ) );
303 d->ui.tls->setEnabled( results.contains( Transport::EnumEncryption::TLS ) );
304 checkHighestEnabledButton( d->encryptionGroup );
306 d->noEncCapa = d->serverTest->normalProtocols();
307 if ( d->ui.tls->isEnabled() ) {
308 d->tlsCapa = d->serverTest->tlsProtocols();
312 d->sslCapa = d->serverTest->secureProtocols();
313 d->updateAuthCapbilities();
316 void SMTPConfigWidget::hostNameChanged(
const QString &text )
323 int pos = d->ui.kcfg_host->cursorPosition();
324 d->ui.kcfg_host->blockSignals(
true );
325 d->ui.kcfg_host->setText( text.trimmed() );
326 d->ui.kcfg_host->blockSignals(
false );
327 d->ui.kcfg_host->setCursorPosition( pos );
329 d->resetAuthCapabilities();
330 for (
int i = 0; d->encryptionGroup && i < d->encryptionGroup->buttons().count(); i++ ) {
331 d->encryptionGroup->buttons().at( i )->setEnabled(
true );
335 void SMTPConfigWidget::ensureValidAuthSelection()
340 d->updateAuthCapbilities();
343 void SMTPConfigWidget::encryptionChanged(
int 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();
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.
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.