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> 45 #include <KMessageBox> 50 class BusyCursorHelper :
public QObject
53 inline BusyCursorHelper( QObject *parent ) : QObject( parent )
56 qApp->setOverrideCursor( Qt::BusyCursor );
60 inline ~BusyCursorHelper()
63 qApp->restoreOverrideCursor();
70 using namespace MailTransport;
75 ::Ui::SMTPSettings ui;
78 QButtonGroup *encryptionGroup;
81 QList<int> noEncCapa, sslCapa, tlsCapa;
83 bool serverTestFailed;
85 static void addAuthenticationItem( KComboBox *combo,
86 int authenticationType )
89 QVariant( authenticationType ) );
92 void resetAuthCapabilities()
95 noEncCapa << Transport::EnumAuthenticationType::LOGIN
96 << Transport::EnumAuthenticationType::PLAIN
97 << Transport::EnumAuthenticationType::CRAM_MD5
98 << Transport::EnumAuthenticationType::DIGEST_MD5
99 << Transport::EnumAuthenticationType::NTLM
100 << Transport::EnumAuthenticationType::GSSAPI;
101 sslCapa = tlsCapa = noEncCapa;
102 updateAuthCapbilities();
106 void updateAuthCapbilities()
108 if ( serverTestFailed ) {
112 QList<int> capa = noEncCapa;
113 if ( ui.ssl->isChecked() ) {
115 }
else if ( ui.tls->isChecked() ) {
119 ui.authCombo->clear();
120 foreach (
int authType, capa ) {
121 addAuthenticationItem( ui.authCombo, authType );
125 const int idx = ui.authCombo->findData( transport->authenticationType() );
128 ui.authCombo->setCurrentIndex( idx );
132 if ( capa.count() == 0 ) {
133 ui.noAuthPossible->setVisible(
true );
134 ui.kcfg_requiresAuthentication->setChecked(
false );
135 ui.kcfg_requiresAuthentication->setEnabled(
false );
136 ui.kcfg_requiresAuthentication->setVisible(
false );
137 ui.authCombo->setEnabled(
false );
138 ui.authLabel->setEnabled(
false );
140 ui.noAuthPossible->setVisible(
false );
141 ui.kcfg_requiresAuthentication->setEnabled(
true );
142 ui.kcfg_requiresAuthentication->setVisible(
true );
143 ui.authCombo->setEnabled(
true );
144 ui.authLabel->setEnabled(
true );
149 SMTPConfigWidget::SMTPConfigWidget(
Transport *transport, QWidget *parent )
155 SMTPConfigWidget::SMTPConfigWidget( SMTPConfigWidgetPrivate &dd,
162 static void checkHighestEnabledButton( QButtonGroup *group )
166 for (
int i = group->buttons().count() - 1; i >= 0; --i ) {
167 QAbstractButton *b = group->buttons().at( i );
168 if ( b && b->isEnabled() ) {
175 void SMTPConfigWidget::init()
181 SLOT(passwordsLoaded()) );
183 d->serverTestFailed =
false;
185 d->ui.setupUi(
this );
186 d->manager->addWidget(
this );
187 d->manager->updateWidgets();
189 d->encryptionGroup =
new QButtonGroup(
this );
190 d->encryptionGroup->addButton( d->ui.none, Transport::EnumEncryption::None );
191 d->encryptionGroup->addButton( d->ui.ssl, Transport::EnumEncryption::SSL );
192 d->encryptionGroup->addButton( d->ui.tls, Transport::EnumEncryption::TLS );
194 d->resetAuthCapabilities();
196 if ( KProtocolInfo::capabilities( SMTP_PROTOCOL ).contains( QLatin1String(
"SASL" ) ) == 0 ) {
197 d->ui.authCombo->removeItem( d->ui.authCombo->findData(
198 Transport::EnumAuthenticationType::NTLM ) );
199 d->ui.authCombo->removeItem( d->ui.authCombo->findData(
200 Transport::EnumAuthenticationType::GSSAPI ) );
203 connect( d->ui.checkCapabilities, SIGNAL(clicked()),
204 SLOT(checkSmtpCapabilities()) );
205 connect( d->ui.kcfg_host, SIGNAL(textChanged(QString)),
206 SLOT(hostNameChanged(QString)) );
207 connect( d->encryptionGroup, SIGNAL(buttonClicked(
int)),
208 SLOT(encryptionChanged(
int)) );
209 connect( d->ui.kcfg_requiresAuthentication, SIGNAL(toggled(
bool)),
210 SLOT(ensureValidAuthSelection()) );
212 if ( !d->transport->isValid() ) {
213 checkHighestEnabledButton( d->encryptionGroup );
217 d->transport->updatePasswordState();
218 if ( d->transport->isComplete() ) {
219 d->ui.password->setText( d->transport->password() );
221 if ( d->transport->requiresAuthentication() ) {
226 hostNameChanged( d->transport->host() );
228 #ifdef KDEPIM_MOBILE_UI 229 d->ui.smtpSettingsGroupBox->hide();
233 void SMTPConfigWidget::checkSmtpCapabilities()
238 d->serverTest->setProtocol( SMTP_PROTOCOL );
239 d->serverTest->setServer( d->ui.kcfg_host->text().trimmed() );
240 if ( d->ui.kcfg_specifyHostname->isChecked() ) {
241 d->serverTest->setFakeHostname( d->ui.kcfg_localHostname->text() );
243 QAbstractButton *encryptionChecked = d->encryptionGroup->checkedButton();
244 if (encryptionChecked == d->ui.none) {
245 d->serverTest->setPort( Transport::EnumEncryption::None, d->ui.kcfg_port->value());
246 }
else if (encryptionChecked == d->ui.ssl) {
247 d->serverTest->setPort( Transport::EnumEncryption::SSL, d->ui.kcfg_port->value());
249 d->serverTest->setProgressBar( d->ui.checkCapabilitiesProgress );
250 d->ui.checkCapabilitiesStack->setCurrentIndex( 1 );
251 BusyCursorHelper *busyCursorHelper =
new BusyCursorHelper( d->serverTest );
253 connect( d->serverTest, SIGNAL(finished(QList<int>)),
254 SLOT(slotFinished(QList<int>)));
255 connect( d->serverTest, SIGNAL(finished(QList<int>)),
256 busyCursorHelper, SLOT(deleteLater()) );
257 d->ui.checkCapabilities->setEnabled(
false );
258 d->serverTest->start();
259 d->serverTestFailed =
false;
265 Q_ASSERT( d->manager );
266 d->manager->updateSettings();
267 d->transport->setPassword( d->ui.password->text() );
269 KConfigGroup group( d->transport->config(), d->transport->currentGroup() );
270 const int index = d->ui.authCombo->currentIndex();
272 group.writeEntry(
"authtype", d->ui.authCombo->itemData( index ).toInt() );
278 void SMTPConfigWidget::passwordsLoaded()
283 d->transport->updatePasswordState();
285 if ( d->ui.password->text().isEmpty() ) {
286 d->ui.password->setText( d->transport->password() );
291 void SMTPConfigWidget::slotFinished( QList<int> results )
295 d->ui.checkCapabilitiesStack->setCurrentIndex( 0 );
297 d->ui.checkCapabilities->setEnabled(
true );
298 d->serverTest->deleteLater();
302 if ( results.isEmpty() ) {
303 KMessageBox::error(
this, i18n(
"Failed to check capabilities. Please verify port and authentication mode."), i18n(
"Check Capabilities Failed"));
304 d->serverTestFailed =
true;
305 d->serverTest->deleteLater();
310 d->ui.none->setEnabled( results.contains( Transport::EnumEncryption::None ) );
311 d->ui.ssl->setEnabled( results.contains( Transport::EnumEncryption::SSL ) );
312 d->ui.tls->setEnabled( results.contains( Transport::EnumEncryption::TLS ) );
313 checkHighestEnabledButton( d->encryptionGroup );
315 d->noEncCapa = d->serverTest->normalProtocols();
316 if ( d->ui.tls->isEnabled() ) {
317 d->tlsCapa = d->serverTest->tlsProtocols();
321 d->sslCapa = d->serverTest->secureProtocols();
322 d->updateAuthCapbilities();
324 if (d->ui.ssl->isEnabled()) {
325 const int portValue = d->serverTest->port(Transport::EnumEncryption::SSL);
326 d->ui.kcfg_port->setValue(portValue == -1 ? SMTPS_PORT : portValue);
327 }
else if (d->ui.none->isEnabled()) {
328 const int portValue = d->serverTest->port(Transport::EnumEncryption::None);
329 d->ui.kcfg_port->setValue(portValue == -1 ? SMTP_PORT : portValue);
331 d->serverTest->deleteLater();
334 void SMTPConfigWidget::hostNameChanged(
const QString &text )
341 int pos = d->ui.kcfg_host->cursorPosition();
342 d->ui.kcfg_host->blockSignals(
true );
343 d->ui.kcfg_host->setText( text.trimmed() );
344 d->ui.kcfg_host->blockSignals(
false );
345 d->ui.kcfg_host->setCursorPosition( pos );
347 d->resetAuthCapabilities();
348 for (
int i = 0; d->encryptionGroup && i < d->encryptionGroup->buttons().count(); i++ ) {
349 d->encryptionGroup->buttons().at( i )->setEnabled(
true );
353 void SMTPConfigWidget::ensureValidAuthSelection()
358 d->updateAuthCapbilities();
361 void SMTPConfigWidget::encryptionChanged(
int enc )
367 if ( enc == Transport::EnumEncryption::SSL ) {
368 if ( d->ui.kcfg_port->value() == SMTP_PORT ) {
369 d->ui.kcfg_port->setValue( SMTPS_PORT );
372 if ( d->ui.kcfg_port->value() == SMTPS_PORT ) {
373 d->ui.kcfg_port->setValue( SMTP_PORT );
377 ensureValidAuthSelection();
static TransportManager * self()
Returns the TransportManager instance.
This class can be used to test certain server to see if they support stuff.
Internal file containing constant definitions etc.
void loadPasswordsAsync()
Tries to load passwords asynchronously from KWallet if needed.
bool isValid() const
Returns true if this transport is valid, ie.
Represents the settings of a specific mail transport.
QString authenticationTypeString() const
Returns a string representation of the authentication type.