kmail

kmservertest.cpp

Go to the documentation of this file.
00001 /*  -*- c++ -*-
00002     kmservertest.cpp
00003 
00004     This file is part of KMail, the KDE mail client.
00005     Copyright (c) 2001-2002 Michael Haeckel <haeckel@kde.org>
00006     Copyright (c) 2003 Marc Mutz <mutz@kde.org>
00007 
00008     KMail is free software; you can redistribute it and/or modify it
00009     under the terms of the GNU General Public License, version 2, as
00010     published by the Free Software Foundation.
00011 
00012     KMail is distributed in the hope that it will be useful, but
00013     WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program; if not, write to the Free Software
00019     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021     In addition, as a special exception, the copyright holders give
00022     permission to link the code of this program with any edition of
00023     the Qt library by Trolltech AS, Norway (or with modified versions
00024     of Qt that use the same license as Qt), and distribute linked
00025     combinations including the two.  You must obey the GNU General
00026     Public License in all respects for all of the code used other than
00027     Qt.  If you modify this file, you may extend this exception to
00028     your version of the file, but you are not obligated to do so.  If
00029     you do not wish to do so, delete this exception statement from
00030     your version.
00031 */
00032 
00033 #include <config.h>
00034 
00035 #include "kmservertest.h"
00036 
00037 #include <klocale.h>
00038 #include <kmessagebox.h>
00039 #include <kdebug.h>
00040 #include <kurl.h>
00041 #include <kapplication.h>
00042 #include <kio/scheduler.h>
00043 #include <kio/slave.h>
00044 #include <kio/job.h>
00045 #include <kio/global.h>
00046 
00047 //-----------------------------------------------------------------------------
00048 KMServerTest::KMServerTest( const QString & protocol, const QString & host, int port )
00049   : QObject(),
00050     mProtocol( protocol ), mHost( host ),
00051     mSSL( false ), mJob( 0 ), mSlave( 0 ), mConnectionErrorCount( 0 )
00052 {
00053   KIO::Scheduler::connect(
00054     SIGNAL(slaveError(KIO::Slave *, int, const QString &)),
00055     this, SLOT(slotSlaveResult(KIO::Slave *, int, const QString &)));
00056 
00057   if ( port == 993 || port == 995 || port == 465 )
00058     port = 0;
00059 
00060   startOffSlave( port );
00061 }
00062 
00063 //-----------------------------------------------------------------------------
00064 KMServerTest::~KMServerTest()
00065 {
00066   if (mJob) mJob->kill(TRUE);
00067 }
00068 
00069 
00070 KIO::MetaData KMServerTest::slaveConfig() const {
00071   KIO::MetaData md;
00072   md.insert( "nologin", "on" );
00073   return md;
00074 }
00075 
00076 void KMServerTest::startOffSlave( int port ) {
00077   KURL url;
00078   url.setProtocol( mSSL ? mProtocol + 's' : mProtocol );
00079   url.setHost( mHost );
00080   if ( port )
00081     url.setPort( port );
00082 
00083   mSlave = KIO::Scheduler::getConnectedSlave( url, slaveConfig() );
00084   if ( !mSlave ) {
00085     slotSlaveResult( 0, 1 );
00086     return;
00087   }
00088   connect( mSlave, SIGNAL(metaData(const KIO::MetaData&)),
00089        SLOT(slotMetaData(const KIO::MetaData&)) );
00090 
00091   QByteArray packedArgs;
00092   QDataStream stream( packedArgs, IO_WriteOnly );
00093 
00094   stream << (int) 'c';
00095 
00096   mJob = KIO::special( url, packedArgs, false );
00097   KIO::Scheduler::assignJobToSlave( mSlave, mJob );
00098   connect( mJob, SIGNAL(result(KIO::Job*)), SLOT(slotResult(KIO::Job*)) );
00099   connect( mJob, SIGNAL(infoMessage(KIO::Job*,const QString&)),
00100        SLOT(slotData(KIO::Job*,const QString&)) );
00101 }
00102 
00103 
00104 //-----------------------------------------------------------------------------
00105 void KMServerTest::slotData(KIO::Job *, const QString &data)
00106 {
00107   if ( mSSL )
00108     mListSSL = QStringList::split(' ', data);
00109   else
00110     mListNormal = QStringList::split(' ', data);
00111 }
00112 
00113 
00114 void KMServerTest::slotMetaData( const KIO::MetaData & md ) {
00115   KIO::MetaData::const_iterator it = md.find( "PLAIN AUTH METHODS" );
00116   if ( it != md.end() ) {
00117     mAuthNone = it.data();
00118     kdDebug(5006) << "mAuthNone: " << mAuthNone << endl;
00119   }
00120   it = md.find( "TLS AUTH METHODS" );
00121   if ( it != md.end() ) {
00122     mAuthTLS = it.data();
00123     kdDebug(5006) << "mAuthTLS: " << mAuthTLS << endl;
00124   }
00125   it = md.find( "SSL AUTH METHODS" );
00126   if ( it != md.end() ) {
00127     mAuthSSL = it.data();
00128     kdDebug(5006) << "mAuthSSL: " << mAuthSSL << endl;
00129   }
00130 }
00131 
00132 //-----------------------------------------------------------------------------
00133 void KMServerTest::slotResult(KIO::Job *job)
00134 {
00135   slotSlaveResult(mSlave, job->error());
00136 }
00137 
00138 //-----------------------------------------------------------------------------
00139 void KMServerTest::slotSlaveResult(KIO::Slave *aSlave, int error,
00140   const QString &errorText)
00141 {
00142   if (aSlave != mSlave) return;
00143   if ( mSSL && error == 0 ) {
00144     // add a dummy entry to the list of SSL capabilities so that the receiver
00145     // of the capabilities signal can use mListSSL.isEmpty() in order to find
00146     // out whether SSL is supported
00147     mListSSL.append("SSL");
00148   }
00149 
00150   if (error != KIO::ERR_SLAVE_DIED && mSlave)
00151   {
00152     // disconnect slave after every connect
00153     KIO::Scheduler::disconnectSlave(mSlave);
00154     mSlave = 0;
00155   }
00156   if ( error == KIO::ERR_COULD_NOT_CONNECT )
00157   {
00158     // if one of the two connection tests fails we ignore the error
00159     // if both fail the host is probably not correct so we display the error
00160     if ( mConnectionErrorCount == 0 )
00161     {
00162       error = 0;
00163     }
00164     ++mConnectionErrorCount;
00165   }
00166   if ( error )
00167   {
00168     mJob = 0;
00169     KMessageBox::error( kapp->activeWindow(),
00170         KIO::buildErrorString( error, errorText ),
00171         i18n("Error") );
00172     emit capabilities( mListNormal, mListSSL );
00173     emit capabilities( mListNormal, mListSSL, mAuthNone, mAuthSSL, mAuthTLS );
00174     return;
00175   }
00176   if (!mSSL) {
00177     mSSL = true;
00178     mListNormal.append("NORMAL-CONNECTION");
00179     startOffSlave();
00180   } else {
00181     mJob = 0;
00182 
00183     emit capabilities( mListNormal, mListSSL );
00184     emit capabilities( mListNormal, mListSSL, mAuthNone, mAuthSSL, mAuthTLS );
00185   }
00186 }
00187 
00188 
00189 #include "kmservertest.moc"