observer.cpp Source File (kio Library)

| # kio Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# observer.cpp

    00001 /* This file is part of the KDE libraries
    00002    Copyright (C) 2000 Matej Koss <koss@miesto.sk>
    00003                       David Faure <faure@kde.org>
    00004
    00005    $Id: observer.cpp,v 1.47.2.2 2003/02/20 03:58:46 staikos Exp $
    00006
    00007    This library is free software; you can redistribute it and/or
    00008    modify it under the terms of the GNU Library General Public
    00009    License version 2 as published by the Free Software Foundation.
    00010
    00011    This library is distributed in the hope that it will be useful,
    00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00014    Library General Public License for more details.
    00015
    00016    You should have received a copy of the GNU Library General Public License
    00017    along with this library; see the file COPYING.LIB.  If not, write to
    00018    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00019    Boston, MA 02111-1307, USA.
    00020 */
    00021
    00022 #include <assert.h>
    00023
    00024 #include <kdebug.h>
    00025 #include <kapplication.h>
    00026 #include <dcopclient.h>
    00027 #include <kurl.h>
    00028
    00029 #include "jobclasses.h"
    00030 #include "observer.h"
    00031
    00032 #include "uiserver_stub.h"
    00033
    00034 #include "passdlg.h"
    00035 #include "slavebase.h"
    00036 #include "observer_stub.h"
    00037 #include <kmessagebox.h>
    00038 #include <ksslinfodlg.h>
    00039 #include <ksslcertdlg.h>
    00040 #include <ksslcertificate.h>
    00041 #include <ksslcertchain.h>
    00042 #include <klocale.h>
    00043
    00044 using namespace KIO;
    00045
    00046 template class QIntDict<KIO::Job>;
    00047
    00048 Observer * Observer::s_pObserver = 0L;
    00049
    00050 const int KDEBUG_OBSERVER = 7007; // Should be 7028
    00051
    00052 Observer::Observer() : DCOPObject("KIO::Observer")
    00053 {
    00054     // Register app as able to receive DCOP messages
    00055     if (kapp && !kapp->dcopClient()->isAttached())
    00056     {
    00057         kapp->dcopClient()->attach();
    00058     }
    00059
    00060     if ( !kapp->dcopClient()->isApplicationRegistered( "kio_uiserver" ) )
    00061     {
    00062         kdDebug(KDEBUG_OBSERVER) << "Starting kio_uiserver" << endl;
    00063         QString error;
    00064         int ret = KApplication::startServiceByDesktopPath( "kio_uiserver.desktop",
    00065                                                              QStringList(), &error );
    00066         if ( ret > 0 )
    00067         {
    00068             kdError() << "Couldn't start kio_uiserver from kio_uiserver.desktop: " << error << endl;
    00069         } else
    00070             kdDebug(KDEBUG_OBSERVER) << "startServiceByDesktopPath returned " << ret << endl;
    00071
    00072     }
    00073     if ( !kapp->dcopClient()->isApplicationRegistered( "kio_uiserver" ) )
    00074         kdDebug(KDEBUG_OBSERVER) << "The application kio_uiserver is STILL NOT REGISTERED" << endl;
    00075     else
    00076         kdDebug(KDEBUG_OBSERVER) << "kio_uiserver registered" << endl;
    00077
    00078     m_uiserver = new UIServer_stub( "kio_uiserver", "UIServer" );
    00079 }
    00080
    00081 int Observer::newJob( KIO::Job * job, bool showProgress )
    00082 {
    00083     // Tell the UI Server about this new job, and give it the application id
    00084     // at the same time
    00085     int progressId = m_uiserver->newJob( kapp->dcopClient()->appId(), showProgress );
    00086
    00087     // Keep the result in a dict
    00088     m_dctJobs.insert( progressId, job );
    00089
    00090     return progressId;
    00091 }
    00092
    00093 void Observer::jobFinished( int progressId )
    00094 {
    00095     m_uiserver->jobFinished( progressId );
    00096     m_dctJobs.remove( progressId );
    00097 }
    00098
    00099 void Observer::killJob( int progressId )
    00100 {
    00101     KIO::Job * job = m_dctJobs[ progressId ];
    00102     if (!job)
    00103     {
    00104         kdWarning() << "Can't find job to kill ! There is no job with progressId=" << progressId << " in this process" << endl;
    00105         return;
    00106     }
    00107     job->kill( false /* not quietly */ );
    00108 }
    00109
    00110 MetaData Observer::metadata( int progressId )
    00111 {
    00112     KIO::Job * job = m_dctJobs[ progressId ];
    00113     assert(job);
    00114     if ( job->inherits("KIO::TransferJob") )
    00115         return static_cast<KIO::TransferJob *>(job)->metaData();
    00116     else
    00117     {
    00118         kdWarning() << "Observer::metaData(" << progressId << ") called on a job that is a " << job->className() << endl;
    00119         return MetaData();
    00120     }
    00121 }
    00122
    00123 void Observer::slotTotalSize( KIO::Job* job, KIO::filesize_t size )
    00124 {
    00125   //kdDebug(KDEBUG_OBSERVER) << "** Observer::slotTotalSize " << job << " " << KIO::number(size) << endl;
    00126   m_uiserver->totalSize64( job->progressId(), size );
    00127 }
    00128
    00129 void Observer::slotTotalFiles( KIO::Job* job, unsigned long files )
    00130 {
    00131   //kdDebug(KDEBUG_OBSERVER) << "** Observer::slotTotalFiles " << job << " " << files << endl;
    00132   m_uiserver->totalFiles( job->progressId(), files );
    00133 }
    00134
    00135 void Observer::slotTotalDirs( KIO::Job* job, unsigned long dirs )
    00136 {
    00137   //kdDebug(KDEBUG_OBSERVER) << "** Observer::slotTotalDirs " << job << " " << dirs << endl;
    00138   m_uiserver->totalDirs( job->progressId(), dirs );
    00139 }
    00140
    00141 void Observer::slotProcessedSize( KIO::Job* job, KIO::filesize_t size )
    00142 {
    00143   //kdDebug(KDEBUG_OBSERVER) << "** Observer::slotProcessedSize " << job << " " << job->progressId() << " " << KIO::number(size) << endl;
    00144   m_uiserver->processedSize64( job->progressId(), size );
    00145 }
    00146
    00147 void Observer::slotProcessedFiles( KIO::Job* job, unsigned long files )
    00148 {
    00149   //kdDebug(KDEBUG_OBSERVER) << "** Observer::slotProcessedFiles " << job << " " << files << endl;
    00150   m_uiserver->processedFiles( job->progressId(), files );
    00151 }
    00152
    00153 void Observer::slotProcessedDirs( KIO::Job* job, unsigned long dirs )
    00154 {
    00155   //kdDebug(KDEBUG_OBSERVER) << "** Observer::slotProcessedDirs " << job << " " << dirs << endl;
    00156   m_uiserver->processedDirs( job->progressId(), dirs );
    00157 }
    00158
    00159 void Observer::slotSpeed( KIO::Job* job, unsigned long bytes_per_second )
    00160 {
    00161   //kdDebug(KDEBUG_OBSERVER) << "** Observer::slotSpeed " << job << " " << bytes_per_second << endl;
    00162   m_uiserver->speed( job->progressId(), bytes_per_second );
    00163 }
    00164
    00165 void Observer::slotPercent( KIO::Job* job, unsigned long percent )
    00166 {
    00167   //kdDebug(KDEBUG_OBSERVER) << "** Observer::slotPercent " << job << " " << percent << endl;
    00168   m_uiserver->percent( job->progressId(), percent );
    00169 }
    00170
    00171 void Observer::slotInfoMessage( KIO::Job* job, const QString & msg )
    00172 {
    00173   m_uiserver->infoMessage( job->progressId(), msg );
    00174 }
    00175
    00176 void Observer::slotCopying( KIO::Job* job, const KURL& from, const KURL& to )
    00177 {
    00178   //kdDebug(KDEBUG_OBSERVER) << "** Observer::slotCopying " << job << " " << from.url() << " " << to.url() << endl;
    00179   m_uiserver->copying( job->progressId(), from, to );
    00180 }
    00181
    00182 void Observer::slotMoving( KIO::Job* job, const KURL& from, const KURL& to )
    00183 {
    00184   //kdDebug(KDEBUG_OBSERVER) << "** Observer::slotMoving " << job << " " << from.url() << " " << to.url() << endl;
    00185   m_uiserver->moving( job->progressId(), from, to );
    00186 }
    00187
    00188 void Observer::slotDeleting( KIO::Job* job, const KURL& url )
    00189 {
    00190   //kdDebug(KDEBUG_OBSERVER) << "** Observer::slotDeleting " << job << " " << url.url() << endl;
    00191   m_uiserver->deleting( job->progressId(), url );
    00192 }
    00193
    00194 void Observer::slotTransferring( KIO::Job* job, const KURL& url )
    00195 {
    00196   //kdDebug(KDEBUG_OBSERVER) << "** Observer::slotTransferring " << job << " " << url.url() << endl;
    00197   m_uiserver->transferring( job->progressId(), url );
    00198 }
    00199
    00200 void Observer::slotCreatingDir( KIO::Job* job, const KURL& dir )
    00201 {
    00202   //kdDebug(KDEBUG_OBSERVER) << "** Observer::slotCreatingDir " << job << " " << dir.url() << endl;
    00203   m_uiserver->creatingDir( job->progressId(), dir );
    00204 }
    00205
    00206 void Observer::slotCanResume( KIO::Job* job, KIO::filesize_t offset )
    00207 {
    00208   //kdDebug(KDEBUG_OBSERVER) << "** Observer::slotCanResume " << job << " " << KIO::number(offset) << endl;
    00209   m_uiserver->canResume64( job->progressId(), offset );
    00210 }
    00211
    00212 void Observer::stating( KIO::Job* job, const KURL& url )
    00213 {
    00214   m_uiserver->stating( job->progressId(), url );
    00215 }
    00216
    00217 void Observer::mounting( KIO::Job* job, const QString & dev, const QString & point )
    00218 {
    00219   m_uiserver->mounting( job->progressId(), dev, point );
    00220 }
    00221
    00222 void Observer::unmounting( KIO::Job* job, const QString & point )
    00223 {
    00224   m_uiserver->unmounting( job->progressId(), point );
    00225 }
    00226
    00227 bool Observer::openPassDlg( const QString& prompt, QString& user,
    00228                             QString& pass, bool readOnly )
    00229 {
    00230    AuthInfo info;
    00231    info.prompt = prompt;
    00232    info.username = user;
    00233    info.password = pass;
    00234    info.readOnly = readOnly;
    00235    bool result = openPassDlg ( info );
    00236    if ( result )
    00237    {
    00238      user = info.username;
    00239      pass = info.password;
    00240    }
    00241    return result;
    00242 }
    00243
    00244 bool Observer::openPassDlg( KIO::AuthInfo& info )
    00245 {
    00246     kdDebug(KDEBUG_OBSERVER) << "Observer::openPassDlg: User= " << info.username
    00247                              << ", Message= " << info.prompt << endl;
    00248     int result = KIO::PasswordDialog::getNameAndPassword( info.username, info.password,
    00249                                                           &info.keepPassword, info.prompt,
    00250                                                           info.readOnly, info.caption,
    00251                                                           info.comment, info.commentLabel );
    00252     if ( result == QDialog::Accepted )
    00253     {
    00254         info.setModified( true );
    00255         return true;
    00256     }
    00257     return false;
    00258 }
    00259
    00260 int Observer::messageBox( int progressId, int type, const QString &text,
    00261                           const QString &caption, const QString &buttonYes,
    00262                           const QString &buttonNo )
    00263 {
    00264     kdDebug() << "Observer::messageBox " << type << " " << text << " - " << caption << endl;
    00265     int result = -1;
    00266
    00267     switch (type) {
    00268         case KIO::SlaveBase::QuestionYesNo:
    00269             result = KMessageBox::questionYesNo( 0L, // parent ?
    00270                                                text, caption, buttonYes, buttonNo );
    00271             break;
    00272         case KIO::SlaveBase::WarningYesNo:
    00273             result = KMessageBox::warningYesNo( 0L, // parent ?
    00274                                               text, caption, buttonYes, buttonNo );
    00275             break;
    00276         case KIO::SlaveBase::WarningContinueCancel:
    00277             result = KMessageBox::warningContinueCancel( 0L, // parent ?
    00278                                               text, caption, buttonYes );
    00279             break;
    00280         case KIO::SlaveBase::WarningYesNoCancel:
    00281             result = KMessageBox::warningYesNoCancel( 0L, // parent ?
    00282                                               text, caption, buttonYes, buttonNo );
    00283             break;
    00284         case KIO::SlaveBase::Information:
    00285             KMessageBox::information( 0L, // parent ?
    00286                                       text, caption );
    00287             result = 1; // whatever
    00288             break;
    00289         case KIO::SlaveBase::SSLMessageBox:
    00290         {
    00291             QCString observerAppId = caption.utf8(); // hack, see slaveinterface.cpp
    00292             // Contact the object "KIO::Observer" in the application <appId>
    00293             // Yes, this could be the same application we are, but not necessarily.
    00294             Observer_stub observer( observerAppId, "KIO::Observer" );
    00295
    00296             KIO::MetaData meta = observer.metadata( progressId );
    00297             KSSLInfoDlg *kid = new KSSLInfoDlg(meta["ssl_in_use"].upper()=="TRUE", 0L /*parent?*/, 0L, true);
    00298             KSSLCertificate *x = KSSLCertificate::fromString(meta["ssl_peer_certificate"].local8Bit());
    00299             if (x) {
    00300                // Set the chain back onto the certificate
    00301                QStringList cl =
    00302                       QStringList::split(QString("\n"), meta["ssl_peer_chain"]);
    00303                QPtrList<KSSLCertificate> ncl;
    00304
    00305                ncl.setAutoDelete(true);
    00306                for (QStringList::Iterator it = cl.begin(); it != cl.end(); ++it) {
    00307                   KSSLCertificate *y = KSSLCertificate::fromString((*it).local8Bit());
    00308                   if (y) ncl.append(y);
    00309                }
    00310
    00311                if (ncl.count() > 0)
    00312                   x->chain().setChain(ncl);
    00313
    00314                kid->setup( x,
    00315                            meta["ssl_peer_ip"],
    00316                            text, // the URL
    00317                            meta["ssl_cipher"],
    00318                            meta["ssl_cipher_desc"],
    00319                            meta["ssl_cipher_version"],
    00320                            meta["ssl_cipher_used_bits"].toInt(),
    00321                            meta["ssl_cipher_bits"].toInt(),
    00322                            KSSLCertificate::KSSLValidation(meta["ssl_cert_state"].toInt()));
    00323                kdDebug(7024) << "Showing SSL Info dialog" << endl;
    00324                kid->exec();
    00325                delete x;
    00326                kdDebug(7024) << "SSL Info dialog closed" << endl;
    00327             } else {
    00328                KMessageBox::information( 0L, // parent ?
    00329                                          i18n("The peer SSL certificate appears to be corrupt."), i18n("SSL") );
    00330             }
    00331             // This doesn't have to get deleted.  It deletes on it's own.
    00332             result = 1; // whatever
    00333             break;
    00334         }
    00335         default:
    00336             kdWarning() << "Observer::messageBox: unknown type " << type << endl;
    00337             result = 0;
    00338             break;
    00339     }
    00340     return result;
    00341 #if 0
    00342     QByteArray data, replyData;
    00343     QCString replyType;
    00344     QDataStream arg( data, IO_WriteOnly );
    00345     arg << progressId;
    00346     arg << type;
    00347     arg << text;
    00348     arg << caption;
    00349     arg << buttonYes;
    00350     arg << buttonNo;
    00351     if ( kapp->dcopClient()->call( "kio_uiserver", "UIServer", "messageBox(int,int,QString,QString,QString,QString)", data, replyType, replyData, true )
    00352         && replyType == "int" )
    00353     {
    00354         int result;
    00355         QDataStream _reply_stream( replyData, IO_ReadOnly );
    00356         _reply_stream >> result;
    00357         kdDebug(KDEBUG_OBSERVER) << "Observer::messageBox got result " << result << endl;
    00358         return result;
    00359     }
    00360     kdDebug(KDEBUG_OBSERVER) << "Observer::messageBox call failed" << endl;
    00361     return 0;
    00362 #endif
    00363 }
    00364
    00365 RenameDlg_Result Observer::open_RenameDlg( KIO::Job* job,
    00366                                            const QString & caption,
    00367                                            const QString& src, const QString & dest,
    00368                                            RenameDlg_Mode mode, QString& newDest,
    00369                                            KIO::filesize_t sizeSrc,
    00370                                            KIO::filesize_t sizeDest,
    00371                                            time_t ctimeSrc,
    00372                                            time_t ctimeDest,
    00373                                            time_t mtimeSrc,
    00374                                            time_t mtimeDest
    00375                                            )
    00376 {
    00377   kdDebug(KDEBUG_OBSERVER) << "Observer::open_RenameDlg job=" << job << endl;
    00378   if (job)
    00379     kdDebug(KDEBUG_OBSERVER) << "                         progressId=" << job->progressId() << endl;
    00380   // Hide existing dialog box if any
    00381   if (job)
    00382     m_uiserver->setJobVisible( job->progressId(), false );
    00383   // We now do it in process.
    00384   RenameDlg_Result res =  KIO::open_RenameDlg( caption, src, dest, mode,
    00385                                                newDest, sizeSrc, sizeDest,
    00386                                                ctimeSrc, ctimeDest, mtimeSrc,
    00387                                                mtimeDest );
    00388   if (job)
    00389     m_uiserver->setJobVisible( job->progressId(), true );
    00390   return res;
    00391 }
    00392
    00393 SkipDlg_Result Observer::open_SkipDlg( KIO::Job* job,
    00394                                        bool _multi,
    00395                                        const QString& _error_text )
    00396 {
    00397   kdDebug(KDEBUG_OBSERVER) << "Observer::open_SkipDlg job=" << job << " progressId=" << job->progressId() << endl;
    00398   // Hide existing dialog box if any
    00399   m_uiserver->setJobVisible( job->progressId(), false );
    00400   // We now do it in process. So this method is a useless wrapper around KIO::open_RenameDlg.
    00401   SkipDlg_Result res = KIO::open_SkipDlg( _multi, _error_text );
    00402   m_uiserver->setJobVisible( job->progressId(), true );
    00403   return res;
    00404 }
    00405
    00406 void Observer::virtual_hook( int id, void* data )
    00407 { DCOPObject::virtual_hook( id, data ); }
    00408
    00409 #include "observer.moc"

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:14:35 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001