• Skip to content
  • Skip to link menu
KDE 4.0 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 using namespace MailTransport;
00041 
00042 class MailTransport::TransportConfigDialog::Private
00043 {
00044   public:
00045     Transport *transport;
00046 
00047     Ui::SMTPSettings smtp;
00048     Ui::SendmailSettings sendmail;
00049 
00050     KConfigDialogManager* manager;
00051     KLineEdit* passwordEdit;
00052     ServerTest* serverTest;
00053     QButtonGroup* encryptionGroup;
00054     QButtonGroup* authGroup;
00055 
00056     // detected authentication capabilities
00057     QList<int> noEncCapa, sslCapa, tlsCapa;
00058 
00059     void resetAuthCapabilities()
00060     {
00061       noEncCapa.clear();
00062       noEncCapa << Transport::EnumAuthenticationType::LOGIN
00063                 << Transport::EnumAuthenticationType::PLAIN
00064                 << Transport::EnumAuthenticationType::CRAM_MD5
00065                 << Transport::EnumAuthenticationType::DIGEST_MD5
00066                 << Transport::EnumAuthenticationType::NTLM
00067                 << Transport::EnumAuthenticationType::GSSAPI;
00068       sslCapa = tlsCapa = noEncCapa;
00069       if ( authGroup )
00070         updateAuthCapbilities();
00071     }
00072 
00073     void updateAuthCapbilities()
00074     {
00075       Q_ASSERT( transport->type() == Transport::EnumType::SMTP );
00076 
00077       QList<int> capa = noEncCapa;
00078       if ( smtp.ssl->isChecked() )
00079         capa = sslCapa;
00080       else if ( smtp.tls->isChecked() )
00081         capa = tlsCapa;
00082 
00083       for ( int i = 0; i < authGroup->buttons().count(); ++i )
00084         authGroup->buttons().at( i )->setEnabled( capa.contains( i ) );
00085 
00086       if ( capa.count() == 0 ) {
00087         smtp.noAuthPossible->setVisible( true );
00088         smtp.kcfg_requiresAuthentication->setChecked( false );
00089         smtp.kcfg_requiresAuthentication->setEnabled( false );
00090       } else {
00091         smtp.noAuthPossible->setVisible( false );
00092         smtp.kcfg_requiresAuthentication->setEnabled( true );
00093       }
00094     }
00095 };
00096 
00097 TransportConfigDialog::TransportConfigDialog( Transport* transport,
00098                                               QWidget * parent) :
00099     KDialog( parent ),
00100     d( new Private )
00101 {
00102   Q_ASSERT( transport );
00103 
00104   d->transport = transport;
00105   d->passwordEdit = 0;
00106   d->serverTest = 0;
00107   d->encryptionGroup = 0;
00108   d->authGroup = 0;
00109   d->resetAuthCapabilities();
00110 
00111   setButtons( Ok|Cancel );
00112   connect( this, SIGNAL(okClicked()), SLOT(save()) );
00113   connect( TransportManager::self(), SIGNAL(passwordsChanged()),
00114            SLOT(passwordsLoaded()) );
00115 
00116   switch ( transport->type() ) {
00117     case Transport::EnumType::SMTP:
00118     {
00119       d->smtp.setupUi( mainWidget() );
00120       d->passwordEdit = d->smtp.password;
00121 
00122       d->encryptionGroup = new QButtonGroup( this );
00123       d->encryptionGroup->addButton( d->smtp.none );
00124       d->encryptionGroup->addButton( d->smtp.ssl );
00125       d->encryptionGroup->addButton( d->smtp.tls );
00126 
00127       d->authGroup = new QButtonGroup( this );
00128       d->authGroup->addButton( d->smtp.login );
00129       d->authGroup->addButton( d->smtp.plain );
00130       d->authGroup->addButton( d->smtp.crammd5 );
00131       d->authGroup->addButton( d->smtp.digestmd5 );
00132       d->authGroup->addButton( d->smtp.ntlm );
00133       d->authGroup->addButton( d->smtp.gssapi );
00134 
00135       if ( KProtocolInfo::capabilities(SMTP_PROTOCOL).contains( QLatin1String("SASL") ) == 0 ) {
00136         d->smtp.ntlm->hide();
00137         d->smtp.gssapi->hide();
00138       }
00139 
00140       connect( d->smtp.checkCapabilities, SIGNAL(clicked()),
00141                SLOT(checkSmtpCapabilities()) );
00142       connect( d->smtp.kcfg_host, SIGNAL(textChanged(QString)),
00143                SLOT(hostNameChanged(QString)) );
00144       connect( d->smtp.kcfg_encryption, SIGNAL(clicked(int)),
00145                SLOT(encryptionChanged(int)) );
00146       break;
00147     }
00148     case Transport::EnumType::Sendmail:
00149     {
00150       d->sendmail.setupUi( mainWidget() );
00151 
00152       connect( d->sendmail.chooseButton, SIGNAL(clicked()),
00153                SLOT(chooseSendmail()) );
00154       connect( d->sendmail.kcfg_host, SIGNAL(textChanged(QString)),
00155                SLOT(hostNameChanged(QString)) );
00156     }
00157   }
00158 
00159   // load the password if necessary
00160   if ( d->passwordEdit ) {
00161     if ( d->transport->isComplete() )
00162       d->passwordEdit->setText( d->transport->password() );
00163     else
00164       if ( d->transport->requiresAuthentication() )
00165         TransportManager::self()->loadPasswordsAsync();
00166   }
00167 
00168   d->manager = new KConfigDialogManager( this, transport );
00169   d->manager->updateWidgets();
00170   hostNameChanged( d->transport->host() );
00171 }
00172 
00173 TransportConfigDialog::~ TransportConfigDialog()
00174 {
00175   delete d;
00176 }
00177 
00178 void TransportConfigDialog::checkSmtpCapabilities()
00179 {
00180   Q_ASSERT( d->transport->type() == Transport::EnumType::SMTP );
00181 
00182   d->serverTest = new ServerTest( this );
00183   d->serverTest->setProtocol( SMTP_PROTOCOL );
00184   d->serverTest->setServer( d->smtp.kcfg_host->text() );
00185   if ( d->smtp.kcfg_specifyHostname->isChecked() )
00186     d->serverTest->setFakeHostname( d->smtp.kcfg_localHostname->text() );
00187   d->serverTest->setProgressBar( d->smtp.checkCapabilitiesProgress );
00188 
00189   connect( d->serverTest, SIGNAL(finished( QList< int > )),
00190            SLOT(slotFinished( QList< int > )));
00191   d->smtp.checkCapabilities->setEnabled( false );
00192   d->serverTest->start();
00193 }
00194 
00195 void TransportConfigDialog::save()
00196 {
00197   d->manager->updateSettings();
00198   if ( d->passwordEdit )
00199     d->transport->setPassword( d->passwordEdit->text() );
00200 
00201   // enforce unique name
00202   QStringList existingNames;
00203   foreach ( Transport *t, TransportManager::self()->transports() )
00204     if ( t->id() != d->transport->id() )
00205       existingNames << t->name();
00206   int suffix = 1;
00207   QString origName = d->transport->name();
00208   while ( existingNames.contains( d->transport->name() ) ) {
00209     d->transport->setName( i18nc("%1: name; %2: number appended to it to make "
00210             "it unique among a list of names", "%1 %2", origName, suffix ) );
00211     ++suffix;
00212   }
00213 
00214   d->transport->writeConfig();
00215 }
00216 
00217 void TransportConfigDialog::chooseSendmail()
00218 {
00219   Q_ASSERT( d->transport->type() == Transport::EnumType::Sendmail );
00220 
00221   KFileDialog dialog( KUrl("/"), QString(), this );
00222   dialog.setCaption( i18n("Choose sendmail Location") );
00223 
00224   if ( dialog.exec() == QDialog::Accepted ) {
00225     KUrl url = dialog.selectedUrl();
00226     if ( url.isEmpty() == true )
00227       return;
00228     if ( !url.isLocalFile() ) {
00229       KMessageBox::sorry( this, i18n( "Only local files allowed." ) );
00230       return;
00231     }
00232     d->sendmail.kcfg_host->setText( url.path() );
00233   }
00234 }
00235 
00236 void TransportConfigDialog::passwordsLoaded()
00237 {
00238   Q_ASSERT( d->passwordEdit );
00239 
00240   if ( d->passwordEdit->text().isEmpty() )
00241     d->passwordEdit->setText( d->transport->password() );
00242 }
00243 
00244 static void checkHighestEnabledButton( QButtonGroup *group )
00245 {
00246   Q_ASSERT( group );
00247 
00248   for ( int i = group->buttons().count() - 1; i >= 0 ; --i ) {
00249     QAbstractButton *b = group->buttons().at( i );
00250     if ( b && b->isEnabled() ) {
00251       b->animateClick();
00252       return;
00253     }
00254   }
00255 }
00256 
00257 void TransportConfigDialog::slotFinished( QList<int> results )
00258 {
00259   d->smtp.checkCapabilities->setEnabled( true );
00260 
00261   // encryption method
00262   d->smtp.none->setEnabled( results.contains( Transport::EnumEncryption::None ) );
00263   d->smtp.ssl->setEnabled( results.contains( Transport::EnumEncryption::SSL ) );
00264   d->smtp.tls->setEnabled( results.contains( Transport::EnumEncryption::TLS ) );
00265   checkHighestEnabledButton( d->encryptionGroup );
00266 
00267   kDebug(5324) << "normal:" <<  d->serverTest->normalProtocols();
00268   kDebug(5324) << "secure:" <<  d->serverTest->secureProtocols();
00269 
00270   d->noEncCapa = d->serverTest->normalProtocols();
00271   if ( d->smtp.tls->isEnabled() )
00272     d->tlsCapa = d->noEncCapa;
00273   else
00274     d->tlsCapa.clear();
00275   d->sslCapa = d->serverTest->secureProtocols();
00276   d->updateAuthCapbilities();
00277   checkHighestEnabledButton( d->authGroup );
00278 
00279   d->serverTest->deleteLater();
00280 }
00281 
00282 void TransportConfigDialog::hostNameChanged( const QString &text )
00283 {
00284   d->resetAuthCapabilities();
00285   enableButton( Ok, !text.isEmpty() );
00286   for ( int i = 0;
00287         d->encryptionGroup && i < d->encryptionGroup->buttons().count(); i++ )
00288     d->encryptionGroup->buttons().at( i )->setEnabled( true );
00289 }
00290 
00291 void TransportConfigDialog::encryptionChanged(int enc)
00292 {
00293   Q_ASSERT( d->transport->type() == Transport::EnumType::SMTP );
00294   kDebug(5324) << enc;
00295 
00296   // adjust port
00297   if ( enc == Transport::EnumEncryption::SSL ) {
00298     if ( d->smtp.kcfg_port->value() == SMTP_PORT )
00299       d->smtp.kcfg_port->setValue( SMTPS_PORT );
00300   } else {
00301     if ( d->smtp.kcfg_port->value() == SMTPS_PORT )
00302       d->smtp.kcfg_port->setValue( SMTP_PORT );
00303   }
00304 
00305   // adjust available authentication methods
00306   d->updateAuthCapbilities();
00307   foreach ( QAbstractButton* b, d->authGroup->buttons() ) {
00308     if ( b->isChecked() && !b->isEnabled() ) {
00309       checkHighestEnabledButton( d->authGroup );
00310       break;
00311     }
00312   }
00313 }
00314 
00315 #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"
  • kabc
  • kblog
  • kcal
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.5
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