kio Library API Documentation

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.51 2004/06/27 10:43:57 binner 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     return messageBox( progressId, type, text, caption, buttonYes, buttonNo, QString::null );
00265 }
00266 
00267 int Observer::messageBox( int progressId, int type, const QString &text,
00268                           const QString &caption, const QString &buttonYes,
00269                           const QString &buttonNo, const QString &dontAskAgainName )
00270 {
00271     kdDebug() << "Observer::messageBox " << type << " " << text << " - " << caption << endl;
00272     int result = -1;
00273     KConfig *config = new KConfig("kioslaverc");
00274     KMessageBox::setDontShowAskAgainConfig(config);
00275 
00276     switch (type) {
00277         case KIO::SlaveBase::QuestionYesNo:
00278             result = KMessageBox::questionYesNo( 0L, // parent ?
00279                                                text, caption, buttonYes, buttonNo, dontAskAgainName );
00280             break;
00281         case KIO::SlaveBase::WarningYesNo:
00282             result = KMessageBox::warningYesNo( 0L, // parent ?
00283                                               text, caption, buttonYes, buttonNo, dontAskAgainName );
00284             break;
00285         case KIO::SlaveBase::WarningContinueCancel:
00286             result = KMessageBox::warningContinueCancel( 0L, // parent ?
00287                                               text, caption, buttonYes, dontAskAgainName );
00288             break;
00289         case KIO::SlaveBase::WarningYesNoCancel:
00290             result = KMessageBox::warningYesNoCancel( 0L, // parent ?
00291                                               text, caption, buttonYes, buttonNo, dontAskAgainName );
00292             break;
00293         case KIO::SlaveBase::Information:
00294             KMessageBox::information( 0L, // parent ?
00295                                       text, caption, dontAskAgainName );
00296             result = 1; // whatever
00297             break;
00298         case KIO::SlaveBase::SSLMessageBox:
00299         {
00300             QCString observerAppId = caption.utf8(); // hack, see slaveinterface.cpp
00301             // Contact the object "KIO::Observer" in the application <appId>
00302             // Yes, this could be the same application we are, but not necessarily.
00303             Observer_stub observer( observerAppId, "KIO::Observer" );
00304 
00305             KIO::MetaData meta = observer.metadata( progressId );
00306             KSSLInfoDlg *kid = new KSSLInfoDlg(meta["ssl_in_use"].upper()=="TRUE", 0L /*parent?*/, 0L, true);
00307             KSSLCertificate *x = KSSLCertificate::fromString(meta["ssl_peer_certificate"].local8Bit());
00308             if (x) {
00309                // Set the chain back onto the certificate
00310                QStringList cl =
00311                       QStringList::split(QString("\n"), meta["ssl_peer_chain"]);
00312                QPtrList<KSSLCertificate> ncl;
00313 
00314                ncl.setAutoDelete(true);
00315                for (QStringList::Iterator it = cl.begin(); it != cl.end(); ++it) {
00316                   KSSLCertificate *y = KSSLCertificate::fromString((*it).local8Bit());
00317                   if (y) ncl.append(y);
00318                }
00319 
00320                if (ncl.count() > 0)
00321                   x->chain().setChain(ncl);
00322 
00323                kid->setup( x,
00324                            meta["ssl_peer_ip"],
00325                            text, // the URL
00326                            meta["ssl_cipher"],
00327                            meta["ssl_cipher_desc"],
00328                            meta["ssl_cipher_version"],
00329                            meta["ssl_cipher_used_bits"].toInt(),
00330                            meta["ssl_cipher_bits"].toInt(),
00331                            KSSLCertificate::KSSLValidation(meta["ssl_cert_state"].toInt()));
00332                kdDebug(7024) << "Showing SSL Info dialog" << endl;
00333                kid->exec();
00334                delete x;
00335                kdDebug(7024) << "SSL Info dialog closed" << endl;
00336             } else {
00337                KMessageBox::information( 0L, // parent ?
00338                                          i18n("The peer SSL certificate appears to be corrupt."), i18n("SSL") );
00339             }
00340             // This doesn't have to get deleted.  It deletes on it's own.
00341             result = 1; // whatever
00342             break;
00343         }
00344         default:
00345             kdWarning() << "Observer::messageBox: unknown type " << type << endl;
00346             result = 0;
00347             break;
00348     }
00349     KMessageBox::setDontShowAskAgainConfig(0);
00350     delete config;
00351     return result;
00352 #if 0
00353     QByteArray data, replyData;
00354     QCString replyType;
00355     QDataStream arg( data, IO_WriteOnly );
00356     arg << progressId;
00357     arg << type;
00358     arg << text;
00359     arg << caption;
00360     arg << buttonYes;
00361     arg << buttonNo;
00362     if ( kapp->dcopClient()->call( "kio_uiserver", "UIServer", "messageBox(int,int,QString,QString,QString,QString)", data, replyType, replyData, true )
00363         && replyType == "int" )
00364     {
00365         int result;
00366         QDataStream _reply_stream( replyData, IO_ReadOnly );
00367         _reply_stream >> result;
00368         kdDebug(KDEBUG_OBSERVER) << "Observer::messageBox got result " << result << endl;
00369         return result;
00370     }
00371     kdDebug(KDEBUG_OBSERVER) << "Observer::messageBox call failed" << endl;
00372     return 0;
00373 #endif
00374 }
00375 
00376 RenameDlg_Result Observer::open_RenameDlg( KIO::Job* job,
00377                                            const QString & caption,
00378                                            const QString& src, const QString & dest,
00379                                            RenameDlg_Mode mode, QString& newDest,
00380                                            KIO::filesize_t sizeSrc,
00381                                            KIO::filesize_t sizeDest,
00382                                            time_t ctimeSrc,
00383                                            time_t ctimeDest,
00384                                            time_t mtimeSrc,
00385                                            time_t mtimeDest
00386                                            )
00387 {
00388   kdDebug(KDEBUG_OBSERVER) << "Observer::open_RenameDlg job=" << job << endl;
00389   if (job)
00390     kdDebug(KDEBUG_OBSERVER) << "                        progressId=" << job->progressId() << endl;
00391   // Hide existing dialog box if any
00392   if (job && job->progressId())
00393     m_uiserver->setJobVisible( job->progressId(), false );
00394   // We now do it in process => KDE4: move this code out of Observer (back to job.cpp), so that
00395   // opening the rename dialog doesn't start uiserver for nothing if progressId=0 (e.g. F2 in konq)
00396   RenameDlg_Result res = KIO::open_RenameDlg( caption, src, dest, mode,
00397                                                newDest, sizeSrc, sizeDest,
00398                                                ctimeSrc, ctimeDest, mtimeSrc,
00399                                                mtimeDest );
00400   if (job && job->progressId())
00401     m_uiserver->setJobVisible( job->progressId(), true );
00402   return res;
00403 }
00404 
00405 SkipDlg_Result Observer::open_SkipDlg( KIO::Job* job,
00406                                        bool _multi,
00407                                        const QString& _error_text )
00408 {
00409   kdDebug(KDEBUG_OBSERVER) << "Observer::open_SkipDlg job=" << job << " progressId=" << job->progressId() << endl;
00410   // Hide existing dialog box if any
00411   if (job && job->progressId())
00412       m_uiserver->setJobVisible( job->progressId(), false );
00413   // We now do it in process. So this method is a useless wrapper around KIO::open_RenameDlg.
00414   SkipDlg_Result res = KIO::open_SkipDlg( _multi, _error_text );
00415   if (job && job->progressId())
00416       m_uiserver->setJobVisible( job->progressId(), true );
00417   return res;
00418 }
00419 
00420 void Observer::virtual_hook( int id, void* data )
00421 { DCOPObject::virtual_hook( id, data ); }
00422 
00423 #include "observer.moc"
KDE Logo
This file is part of the documentation for kio Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Nov 27 13:45:43 2004 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003