• Skip to content
  • Skip to link menu
KDE 4.4 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

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 #include "ui_smtpsettings.h"
00035 
00036 #include <QAbstractButton>
00037 #include <QButtonGroup>
00038 
00039 #include <KProtocolInfo>
00040 
00041 namespace {
00042 
00043 // TODO: is this really necessary?
00044 class BusyCursorHelper : public QObject
00045 {
00046   public:
00047     inline BusyCursorHelper( QObject *parent ) : QObject( parent )
00048     {
00049       qApp->setOverrideCursor( Qt::BusyCursor );
00050     }
00051 
00052     inline ~BusyCursorHelper()
00053     {
00054       qApp->restoreOverrideCursor();
00055     }
00056 };
00057 
00058 }
00059 
00060 using namespace MailTransport;
00061 
00062 class MailTransport::SMTPConfigWidgetPrivate : public TransportConfigWidgetPrivate
00063 {
00064   public:
00065     ::Ui::SMTPSettings ui;
00066 
00067     ServerTest *serverTest;
00068     QButtonGroup *encryptionGroup;
00069     QButtonGroup *authGroup;
00070 
00071     // detected authentication capabilities
00072     QList<int> noEncCapa, sslCapa, tlsCapa;
00073 
00074     bool serverTestFailed;
00075 
00076     void resetAuthCapabilities()
00077     {
00078       noEncCapa.clear();
00079       noEncCapa << Transport::EnumAuthenticationType::LOGIN
00080                 << Transport::EnumAuthenticationType::PLAIN
00081                 << Transport::EnumAuthenticationType::CRAM_MD5
00082                 << Transport::EnumAuthenticationType::DIGEST_MD5
00083                 << Transport::EnumAuthenticationType::NTLM
00084                 << Transport::EnumAuthenticationType::GSSAPI;
00085       sslCapa = tlsCapa = noEncCapa;
00086       if ( authGroup ) {
00087         updateAuthCapbilities();
00088       }
00089     }
00090 
00091     void updateAuthCapbilities()
00092     {
00093       if ( serverTestFailed ) {
00094         return;
00095       }
00096 
00097       QList<int> capa = noEncCapa;
00098       if ( ui.ssl->isChecked() ) {
00099         capa = sslCapa;
00100       } else if ( ui.tls->isChecked() ) {
00101         capa = tlsCapa;
00102       }
00103 
00104       for ( int i = 0; i < authGroup->buttons().count(); ++i ) {
00105         authGroup->buttons().at( i )->setEnabled( capa.contains( i ) );
00106       }
00107 
00108       if ( capa.count() == 0 ) {
00109         ui.noAuthPossible->setVisible( true );
00110         ui.kcfg_requiresAuthentication->setChecked( false );
00111         ui.kcfg_requiresAuthentication->setEnabled( false );
00112         ui.kcfg_requiresAuthentication->setVisible( false );
00113       } else {
00114         ui.noAuthPossible->setVisible( false );
00115         ui.kcfg_requiresAuthentication->setEnabled( true );
00116         ui.kcfg_requiresAuthentication->setVisible( true );
00117       }
00118     }
00119 };
00120 
00121 SMTPConfigWidget::SMTPConfigWidget( Transport *transport, QWidget *parent )
00122   : TransportConfigWidget( *new SMTPConfigWidgetPrivate, transport, parent )
00123 {
00124   init();
00125 }
00126 
00127 SMTPConfigWidget::SMTPConfigWidget( SMTPConfigWidgetPrivate &dd,
00128                                     Transport *transport, QWidget *parent )
00129   : TransportConfigWidget( dd, transport, parent )
00130 {
00131   init();
00132 }
00133 
00134 void SMTPConfigWidget::init()
00135 {
00136   Q_D( SMTPConfigWidget );
00137   d->serverTest = 0;
00138 
00139   connect( TransportManager::self(), SIGNAL(passwordsChanged()),
00140            SLOT(passwordsLoaded()) );
00141 
00142   d->ui.setupUi( this );
00143   d->manager->addWidget( this ); // otherwise it doesn't find out about these widgets
00144   d->manager->updateWidgets();
00145 
00146   d->encryptionGroup = new QButtonGroup( this );
00147   d->encryptionGroup->addButton( d->ui.none );
00148   d->encryptionGroup->addButton( d->ui.ssl );
00149   d->encryptionGroup->addButton( d->ui.tls );
00150 
00151   d->authGroup = new QButtonGroup( this );
00152   d->authGroup->addButton( d->ui.login );
00153   d->authGroup->addButton( d->ui.plain );
00154   d->authGroup->addButton( d->ui.crammd5 );
00155   d->authGroup->addButton( d->ui.digestmd5 );
00156   d->authGroup->addButton( d->ui.gssapi );
00157   d->authGroup->addButton( d->ui.ntlm );
00158   d->resetAuthCapabilities();
00159 
00160   if ( KProtocolInfo::capabilities( SMTP_PROTOCOL ).contains( QLatin1String( "SASL" ) ) == 0 ) {
00161     d->ui.ntlm->hide();
00162     d->ui.gssapi->hide();
00163   }
00164 
00165   connect( d->ui.checkCapabilities, SIGNAL( clicked() ),
00166            SLOT( checkSmtpCapabilities() ) );
00167   connect( d->ui.kcfg_host, SIGNAL( textChanged(QString) ),
00168            SLOT( hostNameChanged(QString) ) );
00169   connect( d->ui.kcfg_encryption, SIGNAL( clicked(int) ),
00170            SLOT( encryptionChanged(int) ) );
00171   connect( d->ui.kcfg_requiresAuthentication, SIGNAL( toggled(bool) ),
00172            SLOT( ensureValidAuthSelection() ) );
00173 
00174   // load the password
00175   d->transport->updatePasswordState();
00176   if ( d->transport->isComplete() ) {
00177     d->ui.password->setText( d->transport->password() );
00178   } else {
00179     if ( d->transport->requiresAuthentication() ) {
00180       TransportManager::self()->loadPasswordsAsync();
00181     }
00182   }
00183 
00184   hostNameChanged( d->transport->host() );
00185 }
00186 
00187 void SMTPConfigWidget::checkSmtpCapabilities()
00188 {
00189   Q_D( SMTPConfigWidget );
00190 
00191   d->serverTest = new ServerTest( this );
00192   d->serverTest->setProtocol( SMTP_PROTOCOL );
00193   d->serverTest->setServer( d->ui.kcfg_host->text().trimmed() );
00194   if ( d->ui.kcfg_specifyHostname->isChecked() ) {
00195     d->serverTest->setFakeHostname( d->ui.kcfg_localHostname->text() );
00196   }
00197   d->serverTest->setProgressBar( d->ui.checkCapabilitiesProgress );
00198   BusyCursorHelper *busyCursorHelper = new BusyCursorHelper( d->serverTest );
00199 
00200   connect( d->serverTest, SIGNAL( finished( QList<int> ) ),
00201            SLOT(slotFinished( QList<int> )));
00202   connect( d->serverTest, SIGNAL( finished( QList<int> ) ),
00203            busyCursorHelper, SLOT( deleteLater() ) );
00204   d->ui.checkCapabilities->setEnabled( false );
00205   d->serverTest->start();
00206   d->serverTestFailed = false;
00207 }
00208 
00209 void SMTPConfigWidget::apply()
00210 {
00211   Q_D( SMTPConfigWidget );
00212 
00213   Q_ASSERT( d->manager );
00214   d->manager->updateSettings();
00215   d->transport->setPassword( d->ui.password->text() );
00216 
00217   TransportConfigWidget::apply();
00218 }
00219 
00220 void SMTPConfigWidget::passwordsLoaded()
00221 {
00222   Q_D( SMTPConfigWidget );
00223 
00224   // Load the password from the original to our cloned copy
00225   d->transport->updatePasswordState();
00226 
00227   if ( d->ui.password->text().isEmpty() ) {
00228     d->ui.password->setText( d->transport->password() );
00229   }
00230 }
00231 
00232 static void checkHighestEnabledButton( QButtonGroup *group )
00233 {
00234   Q_ASSERT( group );
00235 
00236   for ( int i = group->buttons().count() - 1; i >= 0; --i ) {
00237     QAbstractButton *b = group->buttons().at( i );
00238     if ( b && b->isEnabled() ) {
00239       b->animateClick();
00240       return;
00241     }
00242   }
00243 }
00244 
00245 // TODO rename
00246 void SMTPConfigWidget::slotFinished( QList<int> results )
00247 {
00248   Q_D( SMTPConfigWidget );
00249 
00250   d->ui.checkCapabilities->setEnabled( true );
00251   d->serverTest->deleteLater();
00252 
00253   // If the servertest did not find any useable authentication modes, assume the
00254   // connection failed and don't disable any of the radioboxes.
00255   if ( results.isEmpty() ) {
00256     d->serverTestFailed = true;
00257     return;
00258   }
00259 
00260   // encryption method
00261   d->ui.none->setEnabled( results.contains( Transport::EnumEncryption::None ) );
00262   d->ui.ssl->setEnabled( results.contains( Transport::EnumEncryption::SSL ) );
00263   d->ui.tls->setEnabled( results.contains( Transport::EnumEncryption::TLS ) );
00264   checkHighestEnabledButton( d->encryptionGroup );
00265 
00266   d->noEncCapa = d->serverTest->normalProtocols();
00267   if ( d->ui.tls->isEnabled() ) {
00268     d->tlsCapa = d->serverTest->tlsProtocols();
00269   } else {
00270     d->tlsCapa.clear();
00271   }
00272   d->sslCapa = d->serverTest->secureProtocols();
00273   d->updateAuthCapbilities();
00274   checkHighestEnabledButton( d->authGroup );
00275 }
00276 
00277 void SMTPConfigWidget::hostNameChanged( const QString &text )
00278 {
00279   // TODO: really? is this done at every change? wtf
00280 
00281   Q_D( SMTPConfigWidget );
00282 
00283   // sanitize hostname...
00284   int pos = d->ui.kcfg_host->cursorPosition();
00285   d->ui.kcfg_host->blockSignals( true );
00286   d->ui.kcfg_host->setText( text.trimmed() );
00287   d->ui.kcfg_host->blockSignals( false );
00288   d->ui.kcfg_host->setCursorPosition( pos );
00289 
00290   d->resetAuthCapabilities();
00291   for ( int i = 0; d->encryptionGroup && i < d->encryptionGroup->buttons().count(); i++ ) {
00292     d->encryptionGroup->buttons().at( i )->setEnabled( true );
00293   }
00294 }
00295 
00296 void SMTPConfigWidget::ensureValidAuthSelection()
00297 {
00298   Q_D( SMTPConfigWidget );
00299 
00300   // adjust available authentication methods
00301   d->updateAuthCapbilities();
00302   foreach ( QAbstractButton *b, d->authGroup->buttons() ) {
00303     if ( b->isChecked() && !b->isEnabled() ) {
00304       checkHighestEnabledButton( d->authGroup );
00305       break;
00306     }
00307   }
00308 }
00309 
00310 void SMTPConfigWidget::encryptionChanged( int enc )
00311 {
00312   Q_D( SMTPConfigWidget );
00313   kDebug() << enc;
00314 
00315   // adjust port
00316   if ( enc == Transport::EnumEncryption::SSL ) {
00317     if ( d->ui.kcfg_port->value() == SMTP_PORT ) {
00318       d->ui.kcfg_port->setValue( SMTPS_PORT );
00319     }
00320   } else {
00321     if ( d->ui.kcfg_port->value() == SMTPS_PORT ) {
00322       d->ui.kcfg_port->setValue( SMTP_PORT );
00323     }
00324   }
00325 
00326   ensureValidAuthSelection();
00327 }
00328 
00329 #include "smtpconfigwidget.moc"

mailtransport

Skip menu "mailtransport"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kblog
  • kcal
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.6.2-20100208
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal