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

mailtransport

transportconfigdialog.cpp

00001 /*
00002     Copyright (c) 2006 - 2007 Volker Krause <vkrause@kde.org>
00003     Copyright (c) 2007 KovoKs <kovoks@kovoks.nl>
00004 
00005     Based on KMail code by:
00006     Copyright (c) 2001-2002 Michael Haeckel <haeckel@kde.org>
00007 
00008     This library is free software; you can redistribute it and/or modify it
00009     under the terms of the GNU Library General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or (at your
00011     option) any later version.
00012 
00013     This library is distributed in the hope that it will be useful, but WITHOUT
00014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00015     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00016     License for more details.
00017 
00018     You should have received a copy of the GNU Library General Public License
00019     along with this library; see the file COPYING.LIB.  If not, write to the
00020     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00021     02110-1301, USA.
00022 */
00023 
00024 #include "transportconfigdialog.h"
00025 #include "transport.h"
00026 #include "transportmanager.h"
00027 #include "servertest.h"
00028 #include "mailtransport_defs.h"
00029 
00030 #include "ui_smtpsettings.h"
00031 #include "ui_sendmailsettings.h"
00032 
00033 #include <kconfigdialogmanager.h>
00034 #include <kfiledialog.h>
00035 #include <kmessagebox.h>
00036 #include <kprotocolinfo.h>
00037 
00038 #include <QButtonGroup>
00039 
00040 namespace {
00041 
00042 class BusyCursorHelper : public QObject
00043 {
00044   public:
00045     inline BusyCursorHelper( QObject *parent ) : QObject( parent )
00046     {
00047       qApp->setOverrideCursor( Qt::BusyCursor );
00048     }
00049 
00050     inline ~BusyCursorHelper()
00051     {
00052       qApp->restoreOverrideCursor();
00053     }
00054 };
00055 
00056 }
00057 
00058 using namespace MailTransport;
00059 
00060 class MailTransport::TransportConfigDialog::Private
00061 {
00062   public:
00063     Transport *transport;
00064 
00065     Ui::SMTPSettings smtp;
00066     Ui::SendmailSettings sendmail;
00067 
00068     KConfigDialogManager *manager;
00069     KLineEdit *passwordEdit;
00070     ServerTest *serverTest;
00071     QButtonGroup *encryptionGroup;
00072     QButtonGroup *authGroup;
00073 
00074     // detected authentication capabilities
00075     QList<int> noEncCapa, sslCapa, tlsCapa;
00076 
00077     bool serverTestFailed;
00078 
00079     void resetAuthCapabilities()
00080     {
00081       noEncCapa.clear();
00082       noEncCapa << Transport::EnumAuthenticationType::LOGIN
00083                 << Transport::EnumAuthenticationType::PLAIN
00084                 << Transport::EnumAuthenticationType::CRAM_MD5
00085                 << Transport::EnumAuthenticationType::DIGEST_MD5
00086                 << Transport::EnumAuthenticationType::NTLM
00087                 << Transport::EnumAuthenticationType::GSSAPI;
00088       sslCapa = tlsCapa = noEncCapa;
00089       if ( authGroup ) {
00090         updateAuthCapbilities();
00091       }
00092     }
00093 
00094     void updateAuthCapbilities()
00095     {
00096       Q_ASSERT( transport->type() == Transport::EnumType::SMTP );
00097 
00098       if ( serverTestFailed ) {
00099         return;
00100       }
00101 
00102       QList<int> capa = noEncCapa;
00103       if ( smtp.ssl->isChecked() ) {
00104         capa = sslCapa;
00105       } else if ( smtp.tls->isChecked() ) {
00106         capa = tlsCapa;
00107       }
00108 
00109       for ( int i = 0; i < authGroup->buttons().count(); ++i ) {
00110         authGroup->buttons().at( i )->setEnabled( capa.contains( i ) );
00111       }
00112 
00113       if ( capa.count() == 0 ) {
00114         smtp.noAuthPossible->setVisible( true );
00115         smtp.kcfg_requiresAuthentication->setChecked( false );
00116         smtp.kcfg_requiresAuthentication->setEnabled( false );
00117       } else {
00118         smtp.noAuthPossible->setVisible( false );
00119         smtp.kcfg_requiresAuthentication->setEnabled( true );
00120       }
00121     }
00122 };
00123 
00124 TransportConfigDialog::TransportConfigDialog( Transport *transport, QWidget *parent )
00125   : KDialog( parent ), d( new Private )
00126 {
00127   Q_ASSERT( transport );
00128 
00129   d->transport = transport;
00130   d->passwordEdit = 0;
00131   d->serverTest = 0;
00132   d->encryptionGroup = 0;
00133   d->authGroup = 0;
00134   d->resetAuthCapabilities();
00135 
00136   setButtons( Ok|Cancel|User3 );
00137   showButton( User3, false );
00138   setButtonText( User3, i18n( "Use Sendmail" ) );
00139   connect( this, SIGNAL( user3Clicked() ), SLOT( slotUser3() ) );
00140   connect( this, SIGNAL(okClicked()), SLOT(save()) );
00141   connect( TransportManager::self(), SIGNAL(passwordsChanged()),
00142            SLOT(passwordsLoaded()) );
00143 
00144   switch ( transport->type() ) {
00145     case Transport::EnumType::SMTP:
00146     {
00147       showButton( User3, true );
00148 
00149       d->smtp.setupUi( mainWidget() );
00150       d->passwordEdit = d->smtp.password;
00151 
00152       d->encryptionGroup = new QButtonGroup( this );
00153       d->encryptionGroup->addButton( d->smtp.none );
00154       d->encryptionGroup->addButton( d->smtp.ssl );
00155       d->encryptionGroup->addButton( d->smtp.tls );
00156 
00157       d->authGroup = new QButtonGroup( this );
00158       d->authGroup->addButton( d->smtp.login );
00159       d->authGroup->addButton( d->smtp.plain );
00160       d->authGroup->addButton( d->smtp.crammd5 );
00161       d->authGroup->addButton( d->smtp.digestmd5 );
00162       d->authGroup->addButton( d->smtp.gssapi );
00163       d->authGroup->addButton( d->smtp.ntlm );
00164 
00165       if ( KProtocolInfo::capabilities( SMTP_PROTOCOL ).contains( QLatin1String( "SASL" ) ) == 0 ) {
00166         d->smtp.ntlm->hide();
00167         d->smtp.gssapi->hide();
00168       }
00169 
00170       connect( d->smtp.checkCapabilities, SIGNAL(clicked()),
00171                SLOT(checkSmtpCapabilities()) );
00172       connect( d->smtp.kcfg_host, SIGNAL(textChanged(QString)),
00173                SLOT(hostNameChanged(QString)) );
00174       connect( d->smtp.kcfg_encryption, SIGNAL(clicked(int)),
00175                SLOT(encryptionChanged(int)) );
00176       connect( d->smtp.kcfg_requiresAuthentication, SIGNAL( toggled(bool) ),
00177                SLOT( ensureValidAuthSelection() ) );
00178       break;
00179     }
00180     case Transport::EnumType::Sendmail:
00181     {
00182       d->sendmail.setupUi( mainWidget() );
00183 
00184       connect( d->sendmail.chooseButton, SIGNAL(clicked()),
00185                SLOT(chooseSendmail()) );
00186       connect( d->sendmail.kcfg_host, SIGNAL(textChanged(QString)),
00187                SLOT(hostNameChanged(QString)) );
00188     }
00189   }
00190 
00191   // load the password if necessary
00192   if ( d->passwordEdit ) {
00193     if ( d->transport->isComplete() ) {
00194       d->passwordEdit->setText( d->transport->password() );
00195     } else {
00196       if ( d->transport->requiresAuthentication() ) {
00197         TransportManager::self()->loadPasswordsAsync();
00198       }
00199     }
00200   }
00201 
00202   d->manager = new KConfigDialogManager( this, transport );
00203   d->manager->updateWidgets();
00204   hostNameChanged( d->transport->host() );
00205 }
00206 
00207 TransportConfigDialog::~ TransportConfigDialog()
00208 {
00209   delete d;
00210 }
00211 
00212 void TransportConfigDialog::checkSmtpCapabilities()
00213 {
00214   Q_ASSERT( d->transport->type() == Transport::EnumType::SMTP );
00215 
00216   d->serverTest = new ServerTest( this );
00217   d->serverTest->setProtocol( SMTP_PROTOCOL );
00218   d->serverTest->setServer( d->smtp.kcfg_host->text().trimmed() );
00219   if ( d->smtp.kcfg_specifyHostname->isChecked() ) {
00220     d->serverTest->setFakeHostname( d->smtp.kcfg_localHostname->text() );
00221   }
00222   d->serverTest->setProgressBar( d->smtp.checkCapabilitiesProgress );
00223   BusyCursorHelper *busyCursorHelper = new BusyCursorHelper( d->serverTest );
00224 
00225   connect( d->serverTest, SIGNAL(finished( QList< int > )),
00226            SLOT(slotFinished( QList< int > )));
00227   connect( d->serverTest, SIGNAL(finished( QList< int > )),
00228            busyCursorHelper, SLOT(deleteLater()) );
00229   d->smtp.checkCapabilities->setEnabled( false );
00230   d->serverTest->start();
00231   d->serverTestFailed = false;
00232 }
00233 
00234 void TransportConfigDialog::save()
00235 {
00236   d->manager->updateSettings();
00237   if ( d->passwordEdit ) {
00238     d->transport->setPassword( d->passwordEdit->text() );
00239   }
00240 
00241   // enforce unique name
00242   QStringList existingNames;
00243   foreach ( Transport *t, TransportManager::self()->transports() ) {
00244     if ( t->id() != d->transport->id() ) {
00245       existingNames << t->name();
00246     }
00247   }
00248   int suffix = 1;
00249   QString origName = d->transport->name();
00250   while ( existingNames.contains( d->transport->name() ) ) {
00251     d->transport->setName( i18nc( "%1: name; %2: number appended to it to make "
00252                                   "it unique among a list of names", "%1 #%2", origName, suffix ) );
00253     ++suffix;
00254   }
00255 
00256   d->transport->writeConfig();
00257 }
00258 
00259 void TransportConfigDialog::slotUser3()
00260 {
00261   reject();
00262   emit sendmailClicked();
00263 }
00264 
00265 void TransportConfigDialog::chooseSendmail()
00266 {
00267   Q_ASSERT( d->transport->type() == Transport::EnumType::Sendmail );
00268 
00269   KFileDialog dialog( KUrl( "/" ), QString(), this );
00270   dialog.setCaption( i18n( "Choose sendmail Location" ) );
00271 
00272   if ( dialog.exec() == QDialog::Accepted ) {
00273     KUrl url = dialog.selectedUrl();
00274     if ( url.isEmpty() == true ) {
00275       return;
00276     }
00277     if ( !url.isLocalFile() ) {
00278       KMessageBox::sorry( this, i18n( "Only local files allowed." ) );
00279       return;
00280     }
00281     d->sendmail.kcfg_host->setText( url.path() );
00282   }
00283 }
00284 
00285 void TransportConfigDialog::passwordsLoaded()
00286 {
00287   Q_ASSERT( d->passwordEdit );
00288 
00289   // Load the password from the original to our cloned copy
00290   d->transport->updatePasswordState();
00291 
00292   if ( d->passwordEdit->text().isEmpty() ) {
00293     d->passwordEdit->setText( d->transport->password() );
00294   }
00295 }
00296 
00297 static void checkHighestEnabledButton( QButtonGroup *group )
00298 {
00299   Q_ASSERT( group );
00300 
00301   for ( int i = group->buttons().count() - 1; i >= 0; --i ) {
00302     QAbstractButton *b = group->buttons().at( i );
00303     if ( b && b->isEnabled() ) {
00304       b->animateClick();
00305       return;
00306     }
00307   }
00308 }
00309 
00310 void TransportConfigDialog::slotFinished( QList<int> results )
00311 {
00312   d->smtp.checkCapabilities->setEnabled( true );
00313   d->serverTest->deleteLater();
00314 
00315   // If the servertest did not find any useable authentication modes, assume the
00316   // connection failed and don't disable any of the radioboxes.
00317   if ( results.isEmpty() ) {
00318     d->serverTestFailed = true;
00319     return;
00320   }
00321 
00322   // encryption method
00323   d->smtp.none->setEnabled( results.contains( Transport::EnumEncryption::None ) );
00324   d->smtp.ssl->setEnabled( results.contains( Transport::EnumEncryption::SSL ) );
00325   d->smtp.tls->setEnabled( results.contains( Transport::EnumEncryption::TLS ) );
00326   checkHighestEnabledButton( d->encryptionGroup );
00327 
00328   d->noEncCapa = d->serverTest->normalProtocols();
00329   if ( d->smtp.tls->isEnabled() ) {
00330     d->tlsCapa = d->serverTest->tlsProtocols();
00331   } else {
00332     d->tlsCapa.clear();
00333   }
00334   d->sslCapa = d->serverTest->secureProtocols();
00335   d->updateAuthCapbilities();
00336   checkHighestEnabledButton( d->authGroup );
00337 }
00338 
00339 void TransportConfigDialog::hostNameChanged( const QString &text )
00340 {
00341   // sanitize hostname...
00342   if ( d->transport->type() == Transport::EnumType::Sendmail ) {
00343     int pos = d->sendmail.kcfg_host->cursorPosition();
00344     d->sendmail.kcfg_host->blockSignals( true );
00345     d->sendmail.kcfg_host->setText( text.trimmed() );
00346     d->sendmail.kcfg_host->blockSignals( false );
00347     d->sendmail.kcfg_host->setCursorPosition( pos );
00348   } else if ( d->transport->type() == Transport::EnumType::SMTP ) {
00349     int pos = d->smtp.kcfg_host->cursorPosition();
00350     d->smtp.kcfg_host->blockSignals( true );
00351     d->smtp.kcfg_host->setText( text.trimmed() );
00352     d->smtp.kcfg_host->blockSignals( false );
00353     d->smtp.kcfg_host->setCursorPosition( pos );
00354   }
00355 
00356   d->resetAuthCapabilities();
00357   enableButton( Ok, !text.isEmpty() );
00358   for ( int i = 0; d->encryptionGroup && i < d->encryptionGroup->buttons().count(); i++ ) {
00359     d->encryptionGroup->buttons().at( i )->setEnabled( true );
00360   }
00361 }
00362 
00363 void TransportConfigDialog::ensureValidAuthSelection()
00364 {
00365   // adjust available authentication methods
00366   d->updateAuthCapbilities();
00367   foreach ( QAbstractButton *b, d->authGroup->buttons() ) {
00368     if ( b->isChecked() && !b->isEnabled() ) {
00369       checkHighestEnabledButton( d->authGroup );
00370       break;
00371     }
00372   }
00373 }
00374 
00375 void TransportConfigDialog::encryptionChanged( int enc )
00376 {
00377   Q_ASSERT( d->transport->type() == Transport::EnumType::SMTP );
00378   kDebug() << enc;
00379 
00380   // adjust port
00381   if ( enc == Transport::EnumEncryption::SSL ) {
00382     if ( d->smtp.kcfg_port->value() == SMTP_PORT ) {
00383       d->smtp.kcfg_port->setValue( SMTPS_PORT );
00384     }
00385   } else {
00386     if ( d->smtp.kcfg_port->value() == SMTPS_PORT ) {
00387       d->smtp.kcfg_port->setValue( SMTP_PORT );
00388     }
00389   }
00390 
00391   ensureValidAuthSelection();
00392 }
00393 
00394 #include "transportconfigdialog.moc"

mailtransport

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

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  • kabc
  • kblog
  • kcal
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.6.1
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