kmail

accountwizard.cpp

00001 /*******************************************************************************
00002 **
00003 ** Filename   : accountwizard.cpp
00004 ** Created on : 07 February, 2005
00005 ** Copyright  : (c) 2005 Tobias Koenig
00006 ** Email      : tokoe@kde.org
00007 **
00008 *******************************************************************************/
00009 
00010 /*******************************************************************************
00011 **
00012 **   This program is free software; you can redistribute it and/or modify
00013 **   it under the terms of the GNU General Public License as published by
00014 **   the Free Software Foundation; either version 2 of the License, or
00015 **   (at your option) any later version.
00016 **
00017 **   In addition, as a special exception, the copyright holders give
00018 **   permission to link the code of this program with any edition of
00019 **   the Qt library by Trolltech AS, Norway (or with modified versions
00020 **   of Qt that use the same license as Qt), and distribute linked
00021 **   combinations including the two.  You must obey the GNU General
00022 **   Public License in all respects for all of the code used other than
00023 **   Qt.  If you modify this file, you may extend this exception to
00024 **   your version of the file, but you are not obligated to do so.  If
00025 **   you do not wish to do so, delete this exception statement from
00026 **   your version.
00027 *******************************************************************************/
00028 
00029 #include <kdialog.h>
00030 #include <kfiledialog.h>
00031 #include <klineedit.h>
00032 #include <klistbox.h>
00033 #include <klocale.h>
00034 
00035 #include <qcheckbox.h>
00036 #include <qdir.h>
00037 #include <qhbox.h>
00038 #include <qlabel.h>
00039 #include <qlayout.h>
00040 #include <qpushbutton.h>
00041 #include <qvbox.h>
00042 
00043 #include "kmacctlocal.h"
00044 #include "kmkernel.h"
00045 #include "popaccount.h"
00046 #include "kmacctimap.h"
00047 #include "kmacctcachedimap.h"
00048 #include "kmacctmaildir.h"
00049 #include "accountmanager.h"
00050 using KMail::AccountManager;
00051 
00052 #include "globalsettings.h"
00053 #include "kmservertest.h"
00054 #include "kmtransport.h"
00055 #include "libkpimidentities/identity.h"
00056 #include "libkpimidentities/identitymanager.h"
00057 #include "protocols.h"
00058 
00059 #include "accountwizard.h"
00060 
00061 enum Capabilities
00062 {
00063   Plain      =   1,
00064   Login      =   2,
00065   CRAM_MD5   =   4,
00066   Digest_MD5 =   8,
00067   Anonymous  =  16,
00068   APOP       =  32,
00069   Pipelining =  64,
00070   TOP        = 128,
00071   UIDL       = 256,
00072   STLS       = 512, // TLS for POP
00073   STARTTLS   = 512, // TLS for IMAP
00074   GSSAPI     = 1024,
00075   NTLM       = 2048,
00076   AllCapa    = 0xffffffff
00077 };
00078 
00079 class AccountTypeBox : public KListBox
00080 {
00081   public:
00082     enum Type { Local, POP3, IMAP, dIMAP, Maildir };
00083 
00084     AccountTypeBox( QWidget *parent )
00085       : KListBox( parent, "AccountTypeBox" )
00086     {
00087       mTypeList << i18n( "Local mailbox" );
00088       mTypeList << i18n( "POP3" );
00089       mTypeList << i18n( "IMAP" );
00090       mTypeList << i18n( "Disconnected IMAP" );
00091       mTypeList << i18n( "Maildir mailbox" );
00092 
00093       insertStringList( mTypeList );
00094     }
00095 
00096     void setType( Type type )
00097     {
00098       setCurrentItem( (int)type );
00099     }
00100 
00101     Type type() const
00102     {
00103       return (Type)currentItem();
00104     }
00105 
00106   private:
00107     QStringList mTypeList;
00108 };
00109 
00110 AccountWizard::AccountWizard( KMKernel *kernel, QWidget *parent )
00111   : KWizard( parent, "KWizard" ), mKernel( kernel ),
00112     mAccount( 0 ), mTransportInfo( 0 ), mServerTest( 0 )
00113 {
00114   helpButton()->hide();
00115   setupWelcomePage();
00116   setupAccountTypePage();
00117   setupAccountInformationPage();
00118   setupLoginInformationPage();
00119   setupServerInformationPage();
00120 }
00121 
00122 void AccountWizard::start( KMKernel *kernel, QWidget *parent )
00123 {
00124   KConfigGroup wizardConfig( KMKernel::config(), "AccountWizard" );
00125 
00126   if ( wizardConfig.readBoolEntry( "ShowOnStartup", true ) ) {
00127     AccountWizard wizard( kernel, parent );
00128     int result = wizard.exec();
00129     if ( result == QDialog::Accepted ) {
00130       wizardConfig.writeEntry( "ShowOnStartup", false );
00131       kernel->slotConfigChanged();
00132     }
00133   }
00134 }
00135 
00136 void AccountWizard::showPage( QWidget *page )
00137 {
00138   if ( page == mWelcomePage ) {
00139     // do nothing
00140   } else if ( page == mAccountTypePage ) {
00141     if ( mTypeBox->currentItem() == -1 )
00142       mTypeBox->setType( AccountTypeBox::POP3 );
00143   } else if ( page == mAccountInformationPage ) {
00144     if ( mRealName->text().isEmpty() && mEMailAddress->text().isEmpty() &&
00145          mOrganization->text().isEmpty() ) {
00146       KPIM::IdentityManager *manager = mKernel->identityManager();
00147       const KPIM::Identity &identity = manager->defaultIdentity();
00148 
00149       mRealName->setText( identity.fullName() );
00150       mEMailAddress->setText( identity.emailAddr() );
00151       mOrganization->setText( identity.organization() );
00152     }
00153   } else if ( page == mLoginInformationPage ) {
00154     if ( mLoginName->text().isEmpty() ) {
00155       // try to extract login from email address
00156       QString email = mEMailAddress->text();
00157       int pos = email.find( '@' );
00158       if ( pos != -1 )
00159         mLoginName->setText( email.left( pos ) );
00160 
00161       // take the whole email as login otherwise?!?
00162     }
00163   } else if ( page == mServerInformationPage ) {
00164     if ( mTypeBox->type() == AccountTypeBox::Local ||
00165          mTypeBox->type() == AccountTypeBox::Maildir ) {
00166       mIncomingServerWdg->hide();
00167       mIncomingLocationWdg->show();
00168       mIncomingLabel->setText( i18n( "Location:" ) );
00169 
00170       if ( mTypeBox->type() == AccountTypeBox::Local )
00171         mIncomingLocation->setText( QDir::homeDirPath() + "/inbox" );
00172       else
00173         mIncomingLocation->setText( QDir::homeDirPath() + "/Mail/" );
00174     } else {
00175       mIncomingLocationWdg->hide();
00176       mIncomingServerWdg->show();
00177       mIncomingLabel->setText( i18n( "Incoming server:" ) );
00178     }
00179 
00180     setFinishEnabled( mServerInformationPage, true );
00181   }
00182 
00183   QWizard::showPage( page );
00184 }
00185 
00186 void AccountWizard::setupWelcomePage()
00187 {
00188   mWelcomePage = new QVBox( this );
00189   ((QVBox*)mWelcomePage)->setSpacing( KDialog::spacingHint() );
00190 
00191   QLabel *label = new QLabel( i18n( "Welcome to KMail" ), mWelcomePage );
00192   QFont font = label->font();
00193   font.setBold( true );
00194   label->setFont( font );
00195 
00196   new QLabel( i18n( "<qt>It seems you have started KMail for the first time. "
00197                     "You can use this wizard to setup your mail accounts. Just "
00198                     "enter the connection data that you received from your email provider "
00199                     "into the following pages.</qt>" ), mWelcomePage );
00200 
00201   addPage( mWelcomePage, i18n( "Welcome" ) );
00202 }
00203 
00204 void AccountWizard::setupAccountTypePage()
00205 {
00206   mAccountTypePage = new QVBox( this );
00207   ((QVBox*)mAccountTypePage)->setSpacing( KDialog::spacingHint() );
00208 
00209   new QLabel( i18n( "Select what kind of account you would like to create" ), mAccountTypePage );
00210 
00211   mTypeBox = new AccountTypeBox( mAccountTypePage );
00212 
00213   addPage( mAccountTypePage, i18n( "Account Type" ) );
00214 }
00215 
00216 void AccountWizard::setupAccountInformationPage()
00217 {
00218   mAccountInformationPage = new QWidget( this );
00219   QGridLayout *layout = new QGridLayout( mAccountInformationPage, 3, 2,
00220                                          KDialog::marginHint(), KDialog::spacingHint() );
00221 
00222   QLabel *label = new QLabel( i18n( "Real name:" ), mAccountInformationPage );
00223   mRealName = new KLineEdit( mAccountInformationPage );
00224   label->setBuddy( mRealName );
00225 
00226   layout->addWidget( label, 0, 0 );
00227   layout->addWidget( mRealName, 0, 1 );
00228 
00229   label = new QLabel( i18n( "E-mail address:" ), mAccountInformationPage );
00230   mEMailAddress = new KLineEdit( mAccountInformationPage );
00231   label->setBuddy( mEMailAddress );
00232 
00233   layout->addWidget( label, 1, 0 );
00234   layout->addWidget( mEMailAddress, 1, 1 );
00235 
00236   label = new QLabel( i18n( "Organization:" ), mAccountInformationPage );
00237   mOrganization = new KLineEdit( mAccountInformationPage );
00238   label->setBuddy( mOrganization );
00239 
00240   layout->addWidget( label, 2, 0 );
00241   layout->addWidget( mOrganization, 2, 1 );
00242 
00243   addPage( mAccountInformationPage, i18n( "Account Information" ) );
00244 }
00245 
00246 void AccountWizard::setupLoginInformationPage()
00247 {
00248   mLoginInformationPage = new QWidget( this );
00249   QGridLayout *layout = new QGridLayout( mLoginInformationPage, 2, 2,
00250                                          KDialog::marginHint(), KDialog::spacingHint() );
00251 
00252   QLabel *label = new QLabel( i18n( "Login name:" ), mLoginInformationPage );
00253   mLoginName = new KLineEdit( mLoginInformationPage );
00254   label->setBuddy( mLoginName );
00255 
00256   layout->addWidget( label, 0, 0 );
00257   layout->addWidget( mLoginName, 0, 1 );
00258 
00259   label = new QLabel( i18n( "Password:" ), mLoginInformationPage );
00260   mPassword = new KLineEdit( mLoginInformationPage );
00261   mPassword->setEchoMode( QLineEdit::Password );
00262   label->setBuddy( mPassword );
00263 
00264   layout->addWidget( label, 1, 0 );
00265   layout->addWidget( mPassword, 1, 1 );
00266 
00267   addPage( mLoginInformationPage, i18n( "Login Information" ) );
00268 }
00269 
00270 void AccountWizard::setupServerInformationPage()
00271 {
00272   mServerInformationPage = new QWidget( this );
00273   QGridLayout *layout = new QGridLayout( mServerInformationPage, 3, 2,
00274                                          KDialog::marginHint(), KDialog::spacingHint() );
00275 
00276   mIncomingLabel = new QLabel( mServerInformationPage );
00277 
00278   mIncomingServerWdg = new QVBox( mServerInformationPage );
00279   mIncomingServer = new KLineEdit( mIncomingServerWdg );
00280   mIncomingUseSSL = new QCheckBox( i18n( "Use secure connection (SSL)" ), mIncomingServerWdg );
00281 
00282   mIncomingLocationWdg = new QHBox( mServerInformationPage );
00283   mIncomingLocation = new KLineEdit( mIncomingLocationWdg );
00284   mChooseLocation = new QPushButton( i18n( "Choose..." ), mIncomingLocationWdg );
00285 
00286   connect( mChooseLocation, SIGNAL( clicked() ),
00287            this, SLOT( chooseLocation() ) );
00288 
00289   layout->addWidget( mIncomingLabel, 0, 0, AlignTop );
00290   layout->addWidget( mIncomingLocationWdg, 0, 1 );
00291   layout->addWidget( mIncomingServerWdg, 0, 1 );
00292 
00293   QLabel *label = new QLabel( i18n( "Outgoing server:" ), mServerInformationPage );
00294   mOutgoingServer = new KLineEdit( mServerInformationPage );
00295   label->setBuddy( mOutgoingServer );
00296 
00297   layout->addWidget( label, 1, 0 );
00298   layout->addWidget( mOutgoingServer, 1, 1 );
00299 
00300   mOutgoingUseSSL = new QCheckBox( i18n( "Use secure connection (SSL)" ), mServerInformationPage );
00301   layout->addWidget( mOutgoingUseSSL, 2, 1 );
00302 
00303   mLocalDelivery = new QCheckBox( i18n( "Use local delivery" ),
00304                                   mServerInformationPage );
00305   layout->addWidget( mLocalDelivery, 3, 0 );
00306 
00307   connect( mLocalDelivery, SIGNAL( toggled( bool ) ),
00308            mOutgoingServer, SLOT( setDisabled( bool ) ) );
00309 
00310   addPage( mServerInformationPage, i18n( "Server Information" ) );
00311 }
00312 
00313 void AccountWizard::chooseLocation()
00314 {
00315   QString location;
00316 
00317   if ( mTypeBox->type() == AccountTypeBox::Local ) {
00318     location = KFileDialog::getSaveFileName( QString(), QString(), this );
00319   } else if ( mTypeBox->type() == AccountTypeBox::Maildir ) {
00320     location = KFileDialog::getExistingDirectory( QString(), this );
00321   }
00322 
00323   if ( !location.isEmpty() )
00324     mIncomingLocation->setText( location );
00325 }
00326 
00327 QString AccountWizard::accountName() const
00328 {
00329   // create account name
00330   QString name( i18n( "None" ) );
00331 
00332   QString email = mEMailAddress->text();
00333   int pos = email.find( '@' );
00334   if ( pos != -1 ) {
00335     name = email.mid( pos + 1 );
00336     name[ 0 ] = name[ 0 ].upper();
00337   }
00338 
00339   return name;
00340 }
00341 
00342 QLabel *AccountWizard::createInfoLabel( const QString &msg )
00343 {
00344   QLabel *label = new QLabel( msg, this );
00345   label->setFrameStyle( QFrame::Panel | QFrame::Raised );
00346   label->resize( fontMetrics().width( msg ) + 20, label->height() * 2 );
00347   label->move( width() / 2 - label->width() / 2, height() / 2 - label->height() / 2 );
00348   label->show();
00349 
00350   return label;
00351 }
00352 
00353 void AccountWizard::accept()
00354 {
00355   // store identity information
00356   KPIM::IdentityManager *manager = mKernel->identityManager();
00357   KPIM::Identity &identity = manager->modifyIdentityForUoid( manager->defaultIdentity().uoid() );
00358 
00359   identity.setFullName( mRealName->text() );
00360   identity.setEmailAddr( mEMailAddress->text() );
00361   identity.setOrganization( mOrganization->text() );
00362 
00363   manager->commit();
00364 
00365   QTimer::singleShot( 0, this, SLOT( createTransport() ) );
00366 }
00367 
00368 void AccountWizard::createTransport()
00369 {
00370   // create outgoing account
00371   KConfigGroup general( KMKernel::config(), "General" );
00372 
00373   uint numTransports = general.readNumEntry( "transports", 0 );
00374 
00375   for ( uint i = 1 ; i <= numTransports ; i++ ) {
00376     KMTransportInfo *info = new KMTransportInfo();
00377     info->readConfig( i );
00378     mTransportInfoList.append( info );
00379   }
00380 
00381   mTransportInfo = new KMTransportInfo();
00382 
00383   if ( mLocalDelivery->isChecked() ) { // local delivery
00384     mTransportInfo->type = "sendmail";
00385     mTransportInfo->name = i18n( "Sendmail" );
00386     mTransportInfo->host = "/usr/sbin/sendmail"; // TODO: search for sendmail in PATH
00387     mTransportInfo->auth = false;
00388     mTransportInfo->setStorePasswd( false );
00389 
00390     QTimer::singleShot( 0, this, SLOT( transportCreated() ) );
00391   } else { // delivery via SMTP
00392     mTransportInfo->type = "smtp";
00393     mTransportInfo->name = accountName();
00394     mTransportInfo->host = mOutgoingServer->text();
00395     mTransportInfo->user = mLoginName->text();
00396     mTransportInfo->setPasswd( mPassword->text() );
00397 
00398     int port = (mOutgoingUseSSL->isChecked() ? 465 : 25);
00399     checkSmtpCapabilities( mTransportInfo->host, port );
00400   }
00401 }
00402 
00403 void AccountWizard::transportCreated()
00404 {
00405   mTransportInfoList.append( mTransportInfo );
00406 
00407   KConfigGroup general( KMKernel::config(), "General" );
00408   general.writeEntry( "transports", mTransportInfoList.count() );
00409 
00410   for ( uint i = 0 ; i < mTransportInfoList.count() ; i++ )
00411     mTransportInfo->writeConfig( i + 1 );
00412 
00413     // No default transport? => set the first transport as the default
00414   if ( GlobalSettings::self()->defaultTransport().isEmpty() ) {
00415     KConfigGroup general( KMKernel::config(), "General" );
00416 
00417     if ( mTransportInfoList.count() > 0 ) {
00418       KMTransportInfo info;
00419       info.readConfig( 1 );
00420       KConfigGroup composer( KMKernel::config(), "Composer" );
00421       GlobalSettings::self()->setDefaultTransport( info.name );
00422       GlobalSettings::self()->setCurrentTransport( info.name );
00423     }
00424   }
00425 
00426   mTransportInfoList.setAutoDelete( true );
00427   mTransportInfoList.clear();
00428 
00429   QTimer::singleShot( 0, this, SLOT( createAccount() ) );
00430 }
00431 
00432 void AccountWizard::createAccount()
00433 {
00434   // create incoming account
00435   AccountManager *acctManager = mKernel->acctMgr();
00436 
00437   int port = 0;
00438 
00439   switch ( mTypeBox->type() ) {
00440     case AccountTypeBox::Local:
00441     {
00442       mAccount = acctManager->create( "local", i18n( "Local Account" ) );
00443       static_cast<KMAcctLocal*>( mAccount )->setLocation( mIncomingLocation->text() );
00444       break;
00445     }
00446     case AccountTypeBox::POP3:
00447     {
00448       mAccount = acctManager->create( "pop", accountName() );
00449       KMail::PopAccount *acct = static_cast<KMail::PopAccount*>( mAccount );
00450       acct->setLogin( mLoginName->text() );
00451       acct->setPasswd( mPassword->text() );
00452       acct->setHost( mIncomingServer->text() );
00453       port = mIncomingUseSSL->isChecked() ? 995 : 110;
00454       break;
00455     }
00456     case AccountTypeBox::IMAP:
00457     {
00458       mAccount = acctManager->create( "imap", accountName() );
00459       KMAcctImap *acct = static_cast<KMAcctImap*>( mAccount );
00460       acct->setLogin( mLoginName->text() );
00461       acct->setPasswd( mPassword->text() );
00462       acct->setHost( mIncomingServer->text() );
00463       port = mIncomingUseSSL->isChecked() ? 993 : 143;
00464       break;
00465     }
00466     case AccountTypeBox::dIMAP:
00467     {
00468       mAccount = acctManager->create( "cachedimap", accountName() );
00469       KMAcctCachedImap *acct = static_cast<KMAcctCachedImap*>( mAccount );
00470       acct->setLogin( mLoginName->text() );
00471       acct->setPasswd( mPassword->text() );
00472       acct->setHost( mIncomingServer->text() );
00473       port = mIncomingUseSSL->isChecked() ? 993 : 143;
00474       break;
00475     }
00476     case AccountTypeBox::Maildir:
00477     {
00478       mAccount = acctManager->create( "maildir", i18n( "Local Account" ) );
00479       static_cast<KMAcctMaildir*>( mAccount )->setLocation( mIncomingLocation->text() );
00480       break;
00481     }
00482   }
00483 
00484   if ( mTypeBox->type() == AccountTypeBox::POP3 )
00485     checkPopCapabilities( mIncomingServer->text(), port );
00486   else if ( mTypeBox->type() == AccountTypeBox::IMAP || mTypeBox->type() == AccountTypeBox::dIMAP )
00487     checkImapCapabilities( mIncomingServer->text(), port );
00488   else
00489     QTimer::singleShot( 0, this, SLOT( accountCreated() ) );
00490 }
00491 
00492 void AccountWizard::accountCreated()
00493 {
00494   if ( mAccount )
00495   {
00496     mKernel->acctMgr()->add( mAccount );
00497     mKernel->cleanupImapFolders();
00498   }
00499 
00500   finished();
00501 }
00502 
00503 void AccountWizard::finished()
00504 {
00505   GlobalSettings::self()->writeConfig();
00506 
00507   QWizard::accept();
00508 }
00509 
00510 // ----- Security Checks --------------
00511 
00512 void AccountWizard::checkPopCapabilities( const QString &server, int port )
00513 {
00514   delete mServerTest;
00515   mServerTest = new KMServerTest( POP_PROTOCOL, server, port );
00516 
00517   connect( mServerTest, SIGNAL( capabilities( const QStringList&, const QStringList& ) ),
00518            this, SLOT( popCapabilities( const QStringList&, const QStringList& ) ) );
00519 
00520   mAuthInfoLabel = createInfoLabel( i18n( "Check for supported security capabilities of %1..." ).arg( server ) );
00521 }
00522 
00523 void AccountWizard::checkImapCapabilities( const QString &server, int port )
00524 {
00525   delete mServerTest;
00526   mServerTest = new KMServerTest( IMAP_PROTOCOL, server, port );
00527 
00528   connect( mServerTest, SIGNAL( capabilities( const QStringList&, const QStringList& ) ),
00529            this, SLOT( imapCapabilities( const QStringList&, const QStringList& ) ) );
00530 
00531   mAuthInfoLabel = createInfoLabel( i18n( "Check for supported security capabilities of %1..." ).arg( server ) );
00532 }
00533 
00534 void AccountWizard::checkSmtpCapabilities( const QString &server, int port )
00535 {
00536   delete mServerTest;
00537   mServerTest = new KMServerTest( SMTP_PROTOCOL, server, port );
00538 
00539   connect( mServerTest, SIGNAL( capabilities( const QStringList&, const QStringList&,
00540                                               const QString&, const QString&, const QString& ) ),
00541            this, SLOT( smtpCapabilities( const QStringList&, const QStringList&,
00542                                          const QString&, const QString&, const QString& ) ) );
00543 
00544   mAuthInfoLabel = createInfoLabel( i18n( "Check for supported security capabilities of %1..." ).arg( server ) );
00545 }
00546 
00547 void AccountWizard::popCapabilities( const QStringList &capaNormalList,
00548                                      const QStringList &capaSSLList )
00549 {
00550   uint capaNormal = popCapabilitiesFromStringList( capaNormalList );
00551   uint capaTLS = 0;
00552 
00553   if ( capaNormal & STLS )
00554     capaTLS = capaNormal;
00555 
00556   uint capaSSL = popCapabilitiesFromStringList( capaSSLList );
00557 
00558   KMail::NetworkAccount *account = static_cast<KMail::NetworkAccount*>( mAccount );
00559 
00560   bool useSSL = !capaSSLList.isEmpty();
00561   bool useTLS = capaTLS != 0;
00562 
00563   account->setUseSSL( useSSL );
00564   account->setUseTLS( useTLS );
00565 
00566   uint capa = (useSSL ? capaSSL : (useTLS ? capaTLS : capaNormal));
00567 
00568   if ( capa & Plain )
00569     account->setAuth( "PLAIN" );
00570   else if ( capa & Login )
00571     account->setAuth( "LOGIN" );
00572   else if ( capa & CRAM_MD5 )
00573     account->setAuth( "CRAM-MD5" );
00574   else if ( capa & Digest_MD5 )
00575     account->setAuth( "DIGEST-MD5" );
00576   else if ( capa & NTLM )
00577     account->setAuth( "NTLM" );
00578   else if ( capa & GSSAPI )
00579     account->setAuth( "GSSAPI" );
00580   else if ( capa & APOP )
00581     account->setAuth( "APOP" );
00582   else
00583     account->setAuth( "USER" );
00584 
00585   account->setPort( useSSL ? 995 : 110 );
00586 
00587   mServerTest->deleteLater();
00588   mServerTest = 0;
00589 
00590   delete mAuthInfoLabel;
00591   mAuthInfoLabel = 0;
00592 
00593   accountCreated();
00594 }
00595 
00596 
00597 void AccountWizard::imapCapabilities( const QStringList &capaNormalList,
00598                                       const QStringList &capaSSLList )
00599 {
00600   uint capaNormal = imapCapabilitiesFromStringList( capaNormalList );
00601   uint capaTLS = 0;
00602   if ( capaNormal & STARTTLS )
00603     capaTLS = capaNormal;
00604 
00605   uint capaSSL = imapCapabilitiesFromStringList( capaSSLList );
00606 
00607   KMail::NetworkAccount *account = static_cast<KMail::NetworkAccount*>( mAccount );
00608 
00609   bool useSSL = !capaSSLList.isEmpty();
00610   bool useTLS = (capaTLS != 0);
00611 
00612   account->setUseSSL( useSSL );
00613   account->setUseTLS( useTLS );
00614 
00615   uint capa = (useSSL ? capaSSL : (useTLS ? capaTLS : capaNormal));
00616 
00617   if ( capa & CRAM_MD5 )
00618     account->setAuth( "CRAM-MD5" );
00619   else if ( capa & Digest_MD5 )
00620     account->setAuth( "DIGEST-MD5" );
00621   else if ( capa & NTLM )
00622     account->setAuth( "NTLM" );
00623   else if ( capa & GSSAPI )
00624     account->setAuth( "GSSAPI" );
00625   else if ( capa & Anonymous )
00626     account->setAuth( "ANONYMOUS" );
00627   else if ( capa & Login )
00628     account->setAuth( "LOGIN" );
00629   else if ( capa & Plain )
00630     account->setAuth( "PLAIN" );
00631   else
00632     account->setAuth( "*" );
00633 
00634   account->setPort( useSSL ? 993 : 143 );
00635 
00636   mServerTest->deleteLater();
00637   mServerTest = 0;
00638 
00639   delete mAuthInfoLabel;
00640   mAuthInfoLabel = 0;
00641 
00642   accountCreated();
00643 }
00644 
00645 void AccountWizard::smtpCapabilities( const QStringList &capaNormal,
00646                                       const QStringList &capaSSL,
00647                                       const QString &authNone,
00648                                       const QString &authSSL,
00649                                       const QString &authTLS )
00650 {
00651   uint authBitsNone, authBitsSSL, authBitsTLS;
00652 
00653   if ( authNone.isEmpty() && authSSL.isEmpty() && authTLS.isEmpty() ) {
00654     // slave doesn't seem to support "* AUTH METHODS" metadata (or server can't do AUTH)
00655     authBitsNone = authMethodsFromStringList( capaNormal );
00656     if ( capaNormal.findIndex( "STARTTLS" ) != -1 )
00657       authBitsTLS = authBitsNone;
00658     else
00659       authBitsTLS = 0;
00660     authBitsSSL = authMethodsFromStringList( capaSSL );
00661   } else {
00662     authBitsNone = authMethodsFromString( authNone );
00663     authBitsSSL = authMethodsFromString( authSSL );
00664     authBitsTLS = authMethodsFromString( authTLS );
00665   }
00666 
00667   uint authBits = 0;
00668   if ( capaNormal.findIndex( "STARTTLS" ) != -1 ) {
00669     mTransportInfo->encryption = "TLS";
00670     authBits = authBitsTLS;
00671   } else if ( !capaSSL.isEmpty() ) {
00672     mTransportInfo->encryption = "SSL";
00673     authBits = authBitsSSL;
00674   } else {
00675     mTransportInfo->encryption = "NONE";
00676     authBits = authBitsNone;
00677   }
00678 
00679   if ( authBits & Login )
00680     mTransportInfo->authType = "LOGIN";
00681   else if ( authBits & CRAM_MD5 )
00682     mTransportInfo->authType = "CRAM-MD5";
00683   else if ( authBits & Digest_MD5 )
00684     mTransportInfo->authType = "DIGEST-MD5";
00685   else if ( authBits & NTLM )
00686     mTransportInfo->authType = "NTLM";
00687   else if ( authBits & GSSAPI )
00688     mTransportInfo->authType = "GSSAPI";
00689   else
00690     mTransportInfo->authType = "PLAIN";
00691 
00692   mTransportInfo->port = ( !capaSSL.isEmpty() ? "465" : "25" );
00693 
00694   mServerTest->deleteLater();
00695   mServerTest = 0;
00696 
00697   delete mAuthInfoLabel;
00698   mAuthInfoLabel = 0;
00699 
00700   transportCreated();
00701 }
00702 
00703 uint AccountWizard::popCapabilitiesFromStringList( const QStringList & l )
00704 {
00705   unsigned int capa = 0;
00706 
00707   for ( QStringList::const_iterator it = l.begin() ; it != l.end() ; ++it ) {
00708     QString cur = (*it).upper();
00709     if ( cur == "PLAIN" )
00710       capa |= Plain;
00711     else if ( cur == "LOGIN" )
00712       capa |= Login;
00713     else if ( cur == "CRAM-MD5" )
00714       capa |= CRAM_MD5;
00715     else if ( cur == "DIGEST-MD5" )
00716       capa |= Digest_MD5;
00717     else if ( cur == "NTLM" )
00718       capa |= NTLM;
00719     else if ( cur == "GSSAPI" )
00720       capa |= GSSAPI;
00721     else if ( cur == "APOP" )
00722       capa |= APOP;
00723     else if ( cur == "STLS" )
00724       capa |= STLS;
00725   }
00726 
00727   return capa;
00728 }
00729 
00730 uint AccountWizard::imapCapabilitiesFromStringList( const QStringList & l )
00731 {
00732   unsigned int capa = 0;
00733 
00734   for ( QStringList::const_iterator it = l.begin() ; it != l.end() ; ++it ) {
00735     QString cur = (*it).upper();
00736     if ( cur == "AUTH=PLAIN" )
00737       capa |= Plain;
00738     else if ( cur == "AUTH=LOGIN" )
00739       capa |= Login;
00740     else if ( cur == "AUTH=CRAM-MD5" )
00741       capa |= CRAM_MD5;
00742     else if ( cur == "AUTH=DIGEST-MD5" )
00743       capa |= Digest_MD5;
00744     else if ( cur == "AUTH=NTLM" )
00745       capa |= NTLM;
00746     else if ( cur == "AUTH=GSSAPI" )
00747       capa |= GSSAPI;
00748     else if ( cur == "AUTH=ANONYMOUS" )
00749       capa |= Anonymous;
00750     else if ( cur == "STARTTLS" )
00751       capa |= STARTTLS;
00752   }
00753 
00754   return capa;
00755 }
00756 
00757 uint AccountWizard::authMethodsFromString( const QString & s )
00758 {
00759   unsigned int result = 0;
00760 
00761   QStringList sl = QStringList::split( '\n', s.upper() );
00762   for ( QStringList::const_iterator it = sl.begin() ; it != sl.end() ; ++it )
00763     if (  *it == "SASL/LOGIN" )
00764       result |= Login;
00765     else if ( *it == "SASL/PLAIN" )
00766       result |= Plain;
00767     else if ( *it == "SASL/CRAM-MD5" )
00768       result |= CRAM_MD5;
00769     else if ( *it == "SASL/DIGEST-MD5" )
00770       result |= Digest_MD5;
00771     else if ( *it == "SASL/NTLM" )
00772       result |= NTLM;
00773     else if ( *it == "SASL/GSSAPI" )
00774       result |= GSSAPI;
00775 
00776   return result;
00777 }
00778 
00779 uint AccountWizard::authMethodsFromStringList( const QStringList & sl )
00780 {
00781   unsigned int result = 0;
00782 
00783   for ( QStringList::const_iterator it = sl.begin() ; it != sl.end() ; ++it )
00784     if ( *it == "LOGIN" )
00785       result |= Login;
00786     else if ( *it == "PLAIN" )
00787       result |= Plain;
00788     else if ( *it == "CRAM-MD5" )
00789       result |= CRAM_MD5;
00790     else if ( *it == "DIGEST-MD5" )
00791       result |= Digest_MD5;
00792     else if ( *it == "NTLM" )
00793       result |= NTLM;
00794     else if ( *it == "GSSAPI" )
00795       result |= GSSAPI;
00796 
00797   return result;
00798 }
00799 
00800 #include "accountwizard.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys