mailtransport
smtpconfigwidget.cpp
00001 /* 00002 Copyright (c) 2009 Constantin Berzan <exit3219@gmail.com> 00003 00004 Based on MailTransport code by: 00005 Copyright (c) 2006 - 2007 Volker Krause <vkrause@kde.org> 00006 Copyright (c) 2007 KovoKs <kovoks@kovoks.nl> 00007 00008 Based on KMail code by: 00009 Copyright (c) 2001-2002 Michael Haeckel <haeckel@kde.org> 00010 00011 This library is free software; you can redistribute it and/or modify it 00012 under the terms of the GNU Library General Public License as published by 00013 the Free Software Foundation; either version 2 of the License, or (at your 00014 option) any later version. 00015 00016 This library is distributed in the hope that it will be useful, but WITHOUT 00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00018 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00019 License for more details. 00020 00021 You should have received a copy of the GNU Library General Public License 00022 along with this library; see the file COPYING.LIB. If not, write to the 00023 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00024 02110-1301, USA. 00025 */ 00026 00027 #include "smtpconfigwidget.h" 00028 #include "transportconfigwidget_p.h" 00029 #include "transport.h" 00030 #include "transportmanager.h" 00031 #include "servertest.h" 00032 #include "mailtransport_defs.h" 00033 00034 #ifndef KDEPIM_MOBILE_UI 00035 #include "ui_smtpsettings_desktop.h" 00036 #else 00037 #include "ui_smtpsettings_mobile.h" 00038 #endif 00039 00040 #include <QAbstractButton> 00041 #include <QButtonGroup> 00042 00043 #include <KProtocolInfo> 00044 00045 namespace { 00046 00047 // TODO: is this really necessary? 00048 class BusyCursorHelper : public QObject 00049 { 00050 public: 00051 inline BusyCursorHelper( QObject *parent ) : QObject( parent ) 00052 { 00053 #ifndef QT_NO_CURSOR 00054 qApp->setOverrideCursor( Qt::BusyCursor ); 00055 #endif 00056 } 00057 00058 inline ~BusyCursorHelper() 00059 { 00060 #ifndef QT_NO_CURSOR 00061 qApp->restoreOverrideCursor(); 00062 #endif 00063 } 00064 }; 00065 00066 } 00067 00068 using namespace MailTransport; 00069 00070 class MailTransport::SMTPConfigWidgetPrivate : public TransportConfigWidgetPrivate 00071 { 00072 public: 00073 ::Ui::SMTPSettings ui; 00074 00075 ServerTest *serverTest; 00076 QButtonGroup *encryptionGroup; 00077 00078 // detected authentication capabilities 00079 QList<int> noEncCapa, sslCapa, tlsCapa; 00080 00081 bool serverTestFailed; 00082 00083 static void addAuthenticationItem( KComboBox *combo, 00084 int authenticationType ) 00085 { 00086 combo->addItem( Transport::authenticationTypeString( authenticationType ), 00087 QVariant( authenticationType ) ); 00088 } 00089 00090 void resetAuthCapabilities() 00091 { 00092 noEncCapa.clear(); 00093 noEncCapa << Transport::EnumAuthenticationType::LOGIN 00094 << Transport::EnumAuthenticationType::PLAIN 00095 << Transport::EnumAuthenticationType::CRAM_MD5 00096 << Transport::EnumAuthenticationType::DIGEST_MD5 00097 << Transport::EnumAuthenticationType::NTLM 00098 << Transport::EnumAuthenticationType::GSSAPI; 00099 sslCapa = tlsCapa = noEncCapa; 00100 updateAuthCapbilities(); 00101 00102 } 00103 00104 void updateAuthCapbilities() 00105 { 00106 if ( serverTestFailed ) { 00107 return; 00108 } 00109 00110 QList<int> capa = noEncCapa; 00111 if ( ui.ssl->isChecked() ) { 00112 capa = sslCapa; 00113 } else if ( ui.tls->isChecked() ) { 00114 capa = tlsCapa; 00115 } 00116 00117 ui.authCombo->clear(); 00118 foreach ( int authType, capa ) { 00119 addAuthenticationItem( ui.authCombo, authType ); 00120 } 00121 00122 if ( transport->isValid() ) { 00123 const int idx = ui.authCombo->findData( transport->authenticationType() ); 00124 00125 if ( idx != -1 ) { 00126 ui.authCombo->setCurrentIndex( idx ); 00127 } 00128 } 00129 00130 if ( capa.count() == 0 ) { 00131 ui.noAuthPossible->setVisible( true ); 00132 ui.kcfg_requiresAuthentication->setChecked( false ); 00133 ui.kcfg_requiresAuthentication->setEnabled( false ); 00134 ui.kcfg_requiresAuthentication->setVisible( false ); 00135 ui.authCombo->setEnabled( false ); 00136 ui.authLabel->setEnabled( false ); 00137 } else { 00138 ui.noAuthPossible->setVisible( false ); 00139 ui.kcfg_requiresAuthentication->setEnabled( true ); 00140 ui.kcfg_requiresAuthentication->setVisible( true ); 00141 ui.authCombo->setEnabled( true ); 00142 ui.authLabel->setEnabled( true ); 00143 } 00144 } 00145 }; 00146 00147 SMTPConfigWidget::SMTPConfigWidget( Transport *transport, QWidget *parent ) 00148 : TransportConfigWidget( *new SMTPConfigWidgetPrivate, transport, parent ) 00149 { 00150 init(); 00151 } 00152 00153 SMTPConfigWidget::SMTPConfigWidget( SMTPConfigWidgetPrivate &dd, 00154 Transport *transport, QWidget *parent ) 00155 : TransportConfigWidget( dd, transport, parent ) 00156 { 00157 init(); 00158 } 00159 00160 static void checkHighestEnabledButton( QButtonGroup *group ) 00161 { 00162 Q_ASSERT( group ); 00163 00164 for ( int i = group->buttons().count() - 1; i >= 0; --i ) { 00165 QAbstractButton *b = group->buttons().at( i ); 00166 if ( b && b->isEnabled() ) { 00167 b->animateClick(); 00168 return; 00169 } 00170 } 00171 } 00172 00173 void SMTPConfigWidget::init() 00174 { 00175 Q_D( SMTPConfigWidget ); 00176 d->serverTest = 0; 00177 00178 connect( TransportManager::self(), SIGNAL(passwordsChanged()), 00179 SLOT(passwordsLoaded()) ); 00180 00181 d->serverTestFailed = false; 00182 00183 d->ui.setupUi( this ); 00184 d->manager->addWidget( this ); // otherwise it doesn't find out about these widgets 00185 d->manager->updateWidgets(); 00186 00187 d->encryptionGroup = new QButtonGroup( this ); 00188 d->encryptionGroup->addButton( d->ui.none, Transport::EnumEncryption::None ); 00189 d->encryptionGroup->addButton( d->ui.ssl, Transport::EnumEncryption::SSL ); 00190 d->encryptionGroup->addButton( d->ui.tls, Transport::EnumEncryption::TLS ); 00191 00192 d->resetAuthCapabilities(); 00193 00194 if ( KProtocolInfo::capabilities( SMTP_PROTOCOL ).contains( QLatin1String( "SASL" ) ) == 0 ) { 00195 d->ui.authCombo->removeItem( d->ui.authCombo->findData( 00196 Transport::EnumAuthenticationType::NTLM ) ); 00197 d->ui.authCombo->removeItem( d->ui.authCombo->findData( 00198 Transport::EnumAuthenticationType::GSSAPI ) ); 00199 } 00200 00201 connect( d->ui.checkCapabilities, SIGNAL(clicked()), 00202 SLOT(checkSmtpCapabilities()) ); 00203 connect( d->ui.kcfg_host, SIGNAL(textChanged(QString)), 00204 SLOT(hostNameChanged(QString)) ); 00205 connect( d->encryptionGroup, SIGNAL(buttonClicked(int)), 00206 SLOT(encryptionChanged(int)) ); 00207 connect( d->ui.kcfg_requiresAuthentication, SIGNAL(toggled(bool)), 00208 SLOT(ensureValidAuthSelection()) ); 00209 00210 if ( !d->transport->isValid() ) { 00211 checkHighestEnabledButton( d->encryptionGroup ); 00212 } 00213 00214 // load the password 00215 d->transport->updatePasswordState(); 00216 if ( d->transport->isComplete() ) { 00217 d->ui.password->setText( d->transport->password() ); 00218 } else { 00219 if ( d->transport->requiresAuthentication() ) { 00220 TransportManager::self()->loadPasswordsAsync(); 00221 } 00222 } 00223 00224 hostNameChanged( d->transport->host() ); 00225 00226 #ifdef KDEPIM_MOBILE_UI 00227 d->ui.smtpSettingsGroupBox->hide(); 00228 #endif 00229 } 00230 00231 void SMTPConfigWidget::checkSmtpCapabilities() 00232 { 00233 Q_D( SMTPConfigWidget ); 00234 00235 d->serverTest = new ServerTest( this ); 00236 d->serverTest->setProtocol( SMTP_PROTOCOL ); 00237 d->serverTest->setServer( d->ui.kcfg_host->text().trimmed() ); 00238 if ( d->ui.kcfg_specifyHostname->isChecked() ) { 00239 d->serverTest->setFakeHostname( d->ui.kcfg_localHostname->text() ); 00240 } 00241 d->serverTest->setProgressBar( d->ui.checkCapabilitiesProgress ); 00242 d->ui.checkCapabilitiesStack->setCurrentIndex( 1 ); 00243 BusyCursorHelper *busyCursorHelper = new BusyCursorHelper( d->serverTest ); 00244 00245 connect( d->serverTest, SIGNAL(finished(QList<int>)), 00246 SLOT(slotFinished(QList<int>))); 00247 connect( d->serverTest, SIGNAL(finished(QList<int>)), 00248 busyCursorHelper, SLOT(deleteLater()) ); 00249 d->ui.checkCapabilities->setEnabled( false ); 00250 d->serverTest->start(); 00251 d->serverTestFailed = false; 00252 } 00253 00254 void SMTPConfigWidget::apply() 00255 { 00256 Q_D( SMTPConfigWidget ); 00257 Q_ASSERT( d->manager ); 00258 d->manager->updateSettings(); 00259 d->transport->setPassword( d->ui.password->text() ); 00260 00261 KConfigGroup group( d->transport->config(), d->transport->currentGroup() ); 00262 const int index = d->ui.authCombo->currentIndex(); 00263 if ( index >= 0 ) { 00264 group.writeEntry( "authtype", d->ui.authCombo->itemData( index ).toInt() ); 00265 } 00266 00267 TransportConfigWidget::apply(); 00268 } 00269 00270 void SMTPConfigWidget::passwordsLoaded() 00271 { 00272 Q_D( SMTPConfigWidget ); 00273 00274 // Load the password from the original to our cloned copy 00275 d->transport->updatePasswordState(); 00276 00277 if ( d->ui.password->text().isEmpty() ) { 00278 d->ui.password->setText( d->transport->password() ); 00279 } 00280 } 00281 00282 // TODO rename 00283 void SMTPConfigWidget::slotFinished( QList<int> results ) 00284 { 00285 Q_D( SMTPConfigWidget ); 00286 00287 d->ui.checkCapabilitiesStack->setCurrentIndex( 0 ); 00288 00289 d->ui.checkCapabilities->setEnabled( true ); 00290 d->serverTest->deleteLater(); 00291 00292 // If the servertest did not find any useable authentication modes, assume the 00293 // connection failed and don't disable any of the radioboxes. 00294 if ( results.isEmpty() ) { 00295 d->serverTestFailed = true; 00296 return; 00297 } 00298 00299 // encryption method 00300 d->ui.none->setEnabled( results.contains( Transport::EnumEncryption::None ) ); 00301 d->ui.ssl->setEnabled( results.contains( Transport::EnumEncryption::SSL ) ); 00302 d->ui.tls->setEnabled( results.contains( Transport::EnumEncryption::TLS ) ); 00303 checkHighestEnabledButton( d->encryptionGroup ); 00304 00305 d->noEncCapa = d->serverTest->normalProtocols(); 00306 if ( d->ui.tls->isEnabled() ) { 00307 d->tlsCapa = d->serverTest->tlsProtocols(); 00308 } else { 00309 d->tlsCapa.clear(); 00310 } 00311 d->sslCapa = d->serverTest->secureProtocols(); 00312 d->updateAuthCapbilities(); 00313 } 00314 00315 void SMTPConfigWidget::hostNameChanged( const QString &text ) 00316 { 00317 // TODO: really? is this done at every change? wtf 00318 00319 Q_D( SMTPConfigWidget ); 00320 00321 // sanitize hostname... 00322 int pos = d->ui.kcfg_host->cursorPosition(); 00323 d->ui.kcfg_host->blockSignals( true ); 00324 d->ui.kcfg_host->setText( text.trimmed() ); 00325 d->ui.kcfg_host->blockSignals( false ); 00326 d->ui.kcfg_host->setCursorPosition( pos ); 00327 00328 d->resetAuthCapabilities(); 00329 for ( int i = 0; d->encryptionGroup && i < d->encryptionGroup->buttons().count(); i++ ) { 00330 d->encryptionGroup->buttons().at( i )->setEnabled( true ); 00331 } 00332 } 00333 00334 void SMTPConfigWidget::ensureValidAuthSelection() 00335 { 00336 Q_D( SMTPConfigWidget ); 00337 00338 // adjust available authentication methods 00339 d->updateAuthCapbilities(); 00340 } 00341 00342 void SMTPConfigWidget::encryptionChanged( int enc ) 00343 { 00344 Q_D( SMTPConfigWidget ); 00345 kDebug() << enc; 00346 00347 // adjust port 00348 if ( enc == Transport::EnumEncryption::SSL ) { 00349 if ( d->ui.kcfg_port->value() == SMTP_PORT ) { 00350 d->ui.kcfg_port->setValue( SMTPS_PORT ); 00351 } 00352 } else { 00353 if ( d->ui.kcfg_port->value() == SMTPS_PORT ) { 00354 d->ui.kcfg_port->setValue( SMTP_PORT ); 00355 } 00356 } 00357 00358 ensureValidAuthSelection(); 00359 } 00360 00361 #include "smtpconfigwidget.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 22:18:05 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 22:18:05 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.