00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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;
00051
00052 Observer::Observer() : DCOPObject("KIO::Observer")
00053 {
00054
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
00084
00085 int progressId = m_uiserver->newJob( kapp->dcopClient()->appId(), showProgress );
00086
00087
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 );
00108 }
00109
00110 MetaData Observer::metadata( int progressId )
00111 {
00112 KIO::Job * job = m_dctJobs[ progressId ];
00113 assert(job);
00114 return job->metaData();
00115 }
00116
00117 void Observer::slotTotalSize( KIO::Job* job, KIO::filesize_t size )
00118 {
00119
00120 m_uiserver->totalSize64( job->progressId(), size );
00121 }
00122
00123 void Observer::slotTotalFiles( KIO::Job* job, unsigned long files )
00124 {
00125
00126 m_uiserver->totalFiles( job->progressId(), files );
00127 }
00128
00129 void Observer::slotTotalDirs( KIO::Job* job, unsigned long dirs )
00130 {
00131
00132 m_uiserver->totalDirs( job->progressId(), dirs );
00133 }
00134
00135 void Observer::slotProcessedSize( KIO::Job* job, KIO::filesize_t size )
00136 {
00137
00138 m_uiserver->processedSize64( job->progressId(), size );
00139 }
00140
00141 void Observer::slotProcessedFiles( KIO::Job* job, unsigned long files )
00142 {
00143
00144 m_uiserver->processedFiles( job->progressId(), files );
00145 }
00146
00147 void Observer::slotProcessedDirs( KIO::Job* job, unsigned long dirs )
00148 {
00149
00150 m_uiserver->processedDirs( job->progressId(), dirs );
00151 }
00152
00153 void Observer::slotSpeed( KIO::Job* job, unsigned long speed )
00154 {
00155
00156 m_uiserver->speed( job->progressId(), speed );
00157 }
00158
00159 void Observer::slotPercent( KIO::Job* job, unsigned long percent )
00160 {
00161
00162 m_uiserver->percent( job->progressId(), percent );
00163 }
00164
00165 void Observer::slotInfoMessage( KIO::Job* job, const QString & msg )
00166 {
00167 m_uiserver->infoMessage( job->progressId(), msg );
00168 }
00169
00170 void Observer::slotCopying( KIO::Job* job, const KURL& from, const KURL& to )
00171 {
00172
00173 m_uiserver->copying( job->progressId(), from, to );
00174 }
00175
00176 void Observer::slotMoving( KIO::Job* job, const KURL& from, const KURL& to )
00177 {
00178
00179 m_uiserver->moving( job->progressId(), from, to );
00180 }
00181
00182 void Observer::slotDeleting( KIO::Job* job, const KURL& url )
00183 {
00184
00185 m_uiserver->deleting( job->progressId(), url );
00186 }
00187
00188 void Observer::slotTransferring( KIO::Job* job, const KURL& url )
00189 {
00190
00191 m_uiserver->transferring( job->progressId(), url );
00192 }
00193
00194 void Observer::slotCreatingDir( KIO::Job* job, const KURL& dir )
00195 {
00196
00197 m_uiserver->creatingDir( job->progressId(), dir );
00198 }
00199
00200 void Observer::slotCanResume( KIO::Job* job, KIO::filesize_t offset )
00201 {
00202
00203 m_uiserver->canResume64( job->progressId(), offset );
00204 }
00205
00206 void Observer::stating( KIO::Job* job, const KURL& url )
00207 {
00208 m_uiserver->stating( job->progressId(), url );
00209 }
00210
00211 void Observer::mounting( KIO::Job* job, const QString & dev, const QString & point )
00212 {
00213 m_uiserver->mounting( job->progressId(), dev, point );
00214 }
00215
00216 void Observer::unmounting( KIO::Job* job, const QString & point )
00217 {
00218 m_uiserver->unmounting( job->progressId(), point );
00219 }
00220
00221 bool Observer::openPassDlg( const QString& prompt, QString& user,
00222 QString& pass, bool readOnly )
00223 {
00224 AuthInfo info;
00225 info.prompt = prompt;
00226 info.username = user;
00227 info.password = pass;
00228 info.readOnly = readOnly;
00229 bool result = openPassDlg ( info );
00230 if ( result )
00231 {
00232 user = info.username;
00233 pass = info.password;
00234 }
00235 return result;
00236 }
00237
00238 bool Observer::openPassDlg( KIO::AuthInfo& info )
00239 {
00240 kdDebug(KDEBUG_OBSERVER) << "Observer::openPassDlg: User= " << info.username
00241 << ", Message= " << info.prompt << endl;
00242 int result = KIO::PasswordDialog::getNameAndPassword( info.username, info.password,
00243 &info.keepPassword, info.prompt,
00244 info.readOnly, info.caption,
00245 info.comment, info.commentLabel );
00246 if ( result == QDialog::Accepted )
00247 {
00248 info.setModified( true );
00249 return true;
00250 }
00251 return false;
00252 }
00253
00254 int Observer::messageBox( int progressId, int type, const QString &text,
00255 const QString &caption, const QString &buttonYes,
00256 const QString &buttonNo )
00257 {
00258 return messageBox( progressId, type, text, caption, buttonYes, buttonNo, QString::null );
00259 }
00260
00261 int Observer::messageBox( int progressId, int type, const QString &text,
00262 const QString &caption, const QString &buttonYes,
00263 const QString &buttonNo, const QString &dontAskAgainName )
00264 {
00265 kdDebug() << "Observer::messageBox " << type << " " << text << " - " << caption << endl;
00266 int result = -1;
00267 KConfig *config = new KConfig("kioslaverc");
00268 KMessageBox::setDontShowAskAgainConfig(config);
00269
00270 switch (type) {
00271 case KIO::SlaveBase::QuestionYesNo:
00272 result = KMessageBox::questionYesNo( 0L,
00273 text, caption, buttonYes, buttonNo, dontAskAgainName );
00274 break;
00275 case KIO::SlaveBase::WarningYesNo:
00276 result = KMessageBox::warningYesNo( 0L,
00277 text, caption, buttonYes, buttonNo, dontAskAgainName );
00278 break;
00279 case KIO::SlaveBase::WarningContinueCancel:
00280 result = KMessageBox::warningContinueCancel( 0L,
00281 text, caption, buttonYes, dontAskAgainName );
00282 break;
00283 case KIO::SlaveBase::WarningYesNoCancel:
00284 result = KMessageBox::warningYesNoCancel( 0L,
00285 text, caption, buttonYes, buttonNo, dontAskAgainName );
00286 break;
00287 case KIO::SlaveBase::Information:
00288 KMessageBox::information( 0L,
00289 text, caption, dontAskAgainName );
00290 result = 1;
00291 break;
00292 case KIO::SlaveBase::SSLMessageBox:
00293 {
00294 QCString observerAppId = caption.utf8();
00295
00296
00297 Observer_stub observer( observerAppId, "KIO::Observer" );
00298
00299 KIO::MetaData meta = observer.metadata( progressId );
00300 KSSLInfoDlg *kid = new KSSLInfoDlg(meta["ssl_in_use"].upper()=="TRUE", 0L , 0L, true);
00301 KSSLCertificate *x = KSSLCertificate::fromString(meta["ssl_peer_certificate"].local8Bit());
00302 if (x) {
00303
00304 QStringList cl =
00305 QStringList::split(QString("\n"), meta["ssl_peer_chain"]);
00306 QPtrList<KSSLCertificate> ncl;
00307
00308 ncl.setAutoDelete(true);
00309 for (QStringList::Iterator it = cl.begin(); it != cl.end(); ++it) {
00310 KSSLCertificate *y = KSSLCertificate::fromString((*it).local8Bit());
00311 if (y) ncl.append(y);
00312 }
00313
00314 if (ncl.count() > 0)
00315 x->chain().setChain(ncl);
00316
00317 kid->setup( x,
00318 meta["ssl_peer_ip"],
00319 text,
00320 meta["ssl_cipher"],
00321 meta["ssl_cipher_desc"],
00322 meta["ssl_cipher_version"],
00323 meta["ssl_cipher_used_bits"].toInt(),
00324 meta["ssl_cipher_bits"].toInt(),
00325 KSSLCertificate::KSSLValidation(meta["ssl_cert_state"].toInt()));
00326 kdDebug(7024) << "Showing SSL Info dialog" << endl;
00327 kid->exec();
00328 delete x;
00329 kdDebug(7024) << "SSL Info dialog closed" << endl;
00330 } else {
00331 KMessageBox::information( 0L,
00332 i18n("The peer SSL certificate appears to be corrupt."), i18n("SSL") );
00333 }
00334
00335 result = 1;
00336 break;
00337 }
00338 default:
00339 kdWarning() << "Observer::messageBox: unknown type " << type << endl;
00340 result = 0;
00341 break;
00342 }
00343 KMessageBox::setDontShowAskAgainConfig(0);
00344 delete config;
00345 return result;
00346 #if 0
00347 QByteArray data, replyData;
00348 QCString replyType;
00349 QDataStream arg( data, IO_WriteOnly );
00350 arg << progressId;
00351 arg << type;
00352 arg << text;
00353 arg << caption;
00354 arg << buttonYes;
00355 arg << buttonNo;
00356 if ( kapp->dcopClient()->call( "kio_uiserver", "UIServer", "messageBox(int,int,QString,QString,QString,QString)", data, replyType, replyData, true )
00357 && replyType == "int" )
00358 {
00359 int result;
00360 QDataStream _reply_stream( replyData, IO_ReadOnly );
00361 _reply_stream >> result;
00362 kdDebug(KDEBUG_OBSERVER) << "Observer::messageBox got result " << result << endl;
00363 return result;
00364 }
00365 kdDebug(KDEBUG_OBSERVER) << "Observer::messageBox call failed" << endl;
00366 return 0;
00367 #endif
00368 }
00369
00370 RenameDlg_Result Observer::open_RenameDlg( KIO::Job* job,
00371 const QString & caption,
00372 const QString& src, const QString & dest,
00373 RenameDlg_Mode mode, QString& newDest,
00374 KIO::filesize_t sizeSrc,
00375 KIO::filesize_t sizeDest,
00376 time_t ctimeSrc,
00377 time_t ctimeDest,
00378 time_t mtimeSrc,
00379 time_t mtimeDest
00380 )
00381 {
00382 kdDebug(KDEBUG_OBSERVER) << "Observer::open_RenameDlg job=" << job << endl;
00383 if (job)
00384 kdDebug(KDEBUG_OBSERVER) << " progressId=" << job->progressId() << endl;
00385
00386 if (job && job->progressId())
00387 m_uiserver->setJobVisible( job->progressId(), false );
00388
00389
00390 RenameDlg_Result res = KIO::open_RenameDlg( caption, src, dest, mode,
00391 newDest, sizeSrc, sizeDest,
00392 ctimeSrc, ctimeDest, mtimeSrc,
00393 mtimeDest );
00394 if (job && job->progressId())
00395 m_uiserver->setJobVisible( job->progressId(), true );
00396 return res;
00397 }
00398
00399 SkipDlg_Result Observer::open_SkipDlg( KIO::Job* job,
00400 bool _multi,
00401 const QString& _error_text )
00402 {
00403 kdDebug(KDEBUG_OBSERVER) << "Observer::open_SkipDlg job=" << job << " progressId=" << job->progressId() << endl;
00404
00405 if (job && job->progressId())
00406 m_uiserver->setJobVisible( job->progressId(), false );
00407
00408 SkipDlg_Result res = KIO::open_SkipDlg( _multi, _error_text );
00409 if (job && job->progressId())
00410 m_uiserver->setJobVisible( job->progressId(), true );
00411 return res;
00412 }
00413
00414 void Observer::virtual_hook( int id, void* data )
00415 { DCOPObject::virtual_hook( id, data ); }
00416
00417 #include "observer.moc"