00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "kmultipart.h"
00021
00022
00023 #include <kcomponentdata.h>
00024 #include <kmimetype.h>
00025 #include <klocale.h>
00026 #include <kjobuidelegate.h>
00027 #include <kio/job.h>
00028 #include <QtCore/QFile>
00029 #include <ktemporaryfile.h>
00030 #include <kmessagebox.h>
00031 #include <kmimetypetrader.h>
00032 #include <kparts/genericfactory.h>
00033 #include <khtml_part.h>
00034 #include <unistd.h>
00035 #include <kxmlguifactory.h>
00036 #include <QtCore/QTimer>
00037 #include <kvbox.h>
00038
00039 typedef KParts::GenericFactory<KMultiPart> KMultiPartFactory;
00040 K_EXPORT_COMPONENT_FACTORY( libkmultipart , KMultiPartFactory )
00041
00042
00043
00044 class KLineParser
00045 {
00046 public:
00047 KLineParser() {
00048 m_lineComplete = false;
00049 }
00050 void addChar( char c, bool storeNewline ) {
00051 if ( !storeNewline && c == '\r' )
00052 return;
00053 Q_ASSERT( !m_lineComplete );
00054 if ( storeNewline || c != '\n' ) {
00055 int sz = m_currentLine.size();
00056 m_currentLine.resize( sz+1 );
00057 m_currentLine[sz] = c;
00058 }
00059 if ( c == '\n' )
00060 m_lineComplete = true;
00061 }
00062 bool isLineComplete() const {
00063 return m_lineComplete;
00064 }
00065 QByteArray currentLine() const {
00066 return m_currentLine;
00067 }
00068 void clearLine() {
00069 Q_ASSERT( m_lineComplete );
00070 reset();
00071 }
00072 void reset() {
00073 m_currentLine.resize( 0 );
00074 m_lineComplete = false;
00075 }
00076 private:
00077 QByteArray m_currentLine;
00078 bool m_lineComplete;
00079 };
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 KMultiPart::KMultiPart( QWidget *parentWidget,
00099 QObject *parent, const QStringList& )
00100 : KParts::ReadOnlyPart( parent )
00101 {
00102 m_filter = 0L;
00103
00104 setComponentData( KMultiPartFactory::componentData() );
00105
00106 KVBox *box = new KVBox( parentWidget );
00107 setWidget( box );
00108
00109 m_extension = new KParts::BrowserExtension( this );
00110
00111 m_part = 0L;
00112 m_isHTMLPart = false;
00113 m_job = 0L;
00114 m_lineParser = new KLineParser;
00115 m_tempFile = 0;
00116
00117 m_timer = new QTimer( this );
00118 connect( m_timer, SIGNAL( timeout() ), this, SLOT( slotProgressInfo() ) );
00119 }
00120
00121 KMultiPart::~KMultiPart()
00122 {
00123
00124
00125
00126
00127
00128
00129
00130 if ( m_part )
00131 delete static_cast<KParts::ReadOnlyPart *>( m_part );
00132 delete m_job;
00133 delete m_lineParser;
00134 if ( m_tempFile ) {
00135 m_tempFile->setAutoRemove( true );
00136 delete m_tempFile;
00137 }
00138 delete m_filter;
00139 m_filter = 0L;
00140 }
00141
00142
00143 void KMultiPart::startHeader()
00144 {
00145 m_bParsingHeader = true;
00146 m_bGotAnyHeader = false;
00147 m_gzip = false;
00148
00149 delete m_filter;
00150 m_filter = 0L;
00151 }
00152
00153
00154 bool KMultiPart::openUrl( const KUrl &url )
00155 {
00156 setUrl(url);
00157 m_lineParser->reset();
00158 startHeader();
00159
00160
00161
00162
00163
00164
00165 m_job = KIO::get( url,
00166 arguments().reload() ? KIO::Reload : KIO::NoReload,
00167 KIO::HideProgressInfo );
00168
00169 emit started( 0 );
00170
00171 connect( m_job, SIGNAL( result( KJob * ) ),
00172 this, SLOT( slotJobFinished( KJob * ) ) );
00173 connect( m_job, SIGNAL( data( KIO::Job *, const QByteArray & ) ),
00174 this, SLOT( slotData( KIO::Job *, const QByteArray & ) ) );
00175
00176 m_numberOfFrames = 0;
00177 m_numberOfFramesSkipped = 0;
00178 m_totalNumberOfFrames = 0;
00179 m_qtime.start();
00180 m_timer->start( 1000 );
00181
00182 return true;
00183 }
00184
00185
00186
00187
00188 void KMultiPart::slotData( KIO::Job *job, const QByteArray &data )
00189 {
00190 if (m_boundary.isNull())
00191 {
00192 QString tmp = job->queryMetaData("media-boundary");
00193 kDebug() << "Got Boundary from kio-http '" << tmp << "'";
00194 if ( !tmp.isEmpty() ) {
00195 if (tmp.startsWith(QLatin1String("--")))
00196 m_boundary = tmp.toLatin1();
00197 else
00198 m_boundary = QByteArray("--")+tmp.toLatin1();
00199 m_boundaryLength = m_boundary.length();
00200 }
00201 }
00202
00203 for ( int i = 0; i < data.size() ; ++i )
00204 {
00205
00206 m_lineParser->addChar( data[i], !m_bParsingHeader );
00207 if ( m_lineParser->isLineComplete() )
00208 {
00209 QByteArray line = m_lineParser->currentLine();
00210 #ifdef DEBUG_PARSING
00211 kDebug() << "line.size()=" << line.size();
00212 #endif
00213 #ifdef DEBUG_PARSING
00214 kDebug() << "[" << m_bParsingHeader << "] line='" << line << "'";
00215 #endif
00216 if ( m_bParsingHeader )
00217 {
00218 if ( !line.isEmpty() )
00219 m_bGotAnyHeader = true;
00220 if ( m_boundary.isNull() )
00221 {
00222 if ( !line.isEmpty() ) {
00223 #ifdef DEBUG_PARSING
00224 kDebug() << "Boundary is " << line;
00225 #endif
00226 m_boundary = line;
00227 m_boundaryLength = m_boundary.length();
00228 }
00229 }
00230 else if ( !qstrnicmp( line.data(), "Content-Encoding:", 17 ) )
00231 {
00232 QString encoding = QString::fromLatin1(line.data()+17).trimmed().toLower();
00233 if (encoding == "gzip" || encoding == "x-gzip") {
00234 m_gzip = true;
00235 } else {
00236 kDebug() << "FIXME: unhandled encoding type in KMultiPart: " << encoding;
00237 }
00238 }
00239
00240 else if ( !qstrnicmp( line.data(), "Content-Type:", 13 ) )
00241 {
00242 Q_ASSERT( m_nextMimeType.isNull() );
00243 m_nextMimeType = QString::fromLatin1( line.data() + 14 ).trimmed();
00244 int semicolon = m_nextMimeType.indexOf( ';' );
00245 if ( semicolon != -1 )
00246 m_nextMimeType = m_nextMimeType.left( semicolon );
00247 kDebug() << "m_nextMimeType=" << m_nextMimeType;
00248 }
00249
00250 else if ( line.isEmpty() && m_bGotAnyHeader )
00251 {
00252 m_bParsingHeader = false;
00253 #ifdef DEBUG_PARSING
00254 kDebug() << "end of headers";
00255 #endif
00256 startOfData();
00257 }
00258
00259 else if ( line == m_boundary )
00260 ;
00261 else if ( !line.isEmpty() )
00262 kDebug() << "Ignoring header " << line;
00263 } else {
00264 if ( !qstrncmp( line, m_boundary, m_boundaryLength ) )
00265 {
00266 #ifdef DEBUG_PARSING
00267 kDebug() << "boundary found!";
00268 kDebug() << "after it is " << line.data() + m_boundaryLength;
00269 #endif
00270
00271 if ( !qstrncmp( line.data() + m_boundaryLength, "--", 2 ) )
00272 {
00273 #ifdef DEBUG_PARSING
00274 kDebug() << "Completed!";
00275 #endif
00276 endOfData();
00277 emit completed();
00278 } else
00279 {
00280 char nextChar = *(line.data() + m_boundaryLength);
00281 #ifdef DEBUG_PARSING
00282 kDebug() << "KMultiPart::slotData nextChar='" << nextChar << "'";
00283 #endif
00284 if ( nextChar == '\n' || nextChar == '\r' ) {
00285 endOfData();
00286 startHeader();
00287 }
00288 else {
00289
00290 sendData(line);
00291 }
00292 }
00293 } else {
00294
00295 sendData(line);
00296 }
00297 }
00298 m_lineParser->clearLine();
00299 }
00300 }
00301 }
00302
00303 void KMultiPart::setPart( const QString& mimeType )
00304 {
00305 KXMLGUIFactory *guiFactory = factory();
00306 if ( guiFactory )
00307 guiFactory->removeClient( this );
00308 kDebug() << "KMultiPart::setPart " << mimeType;
00309 delete m_part;
00310
00311 m_part = KMimeTypeTrader::createPartInstanceFromQuery<KParts::ReadOnlyPart>
00312 ( m_mimeType, widget(), this );
00313 if ( !m_part ) {
00314
00315 KMessageBox::error( widget(), i18n("No handler found for %1.", m_mimeType) );
00316 return;
00317 }
00318
00319 insertChildClient( m_part );
00320 m_part->widget()->show();
00321
00322 connect( m_part, SIGNAL( completed() ),
00323 this, SLOT( slotPartCompleted() ) );
00324 connect( m_part, SIGNAL(completed(bool)),
00325 this, SLOT(slotPartCompleted()) );
00326
00327 m_isHTMLPart = ( mimeType == "text/html" );
00328 KParts::BrowserExtension* childExtension = KParts::BrowserExtension::childObject( m_part );
00329
00330 if ( childExtension )
00331 {
00332
00333
00334
00335
00336 connect( childExtension, SIGNAL( openURLNotify() ),
00337 m_extension, SIGNAL( openURLNotify() ) );
00338
00339 connect( childExtension, SIGNAL( openUrlRequestDelayed( const KUrl &, const KParts::OpenUrlArguments&, const KParts::BrowserArguments& ) ),
00340 m_extension, SIGNAL( openUrlRequest( const KUrl &, const KParts::OpenUrlArguments&, const KParts::BrowserArguments & ) ) );
00341
00342 connect( childExtension, SIGNAL( createNewWindow( const KUrl &, const KParts::OpenUrlArguments&, const KParts::BrowserArguments&, const KParts::WindowArgs &, KParts::ReadOnlyPart** ) ),
00343 m_extension, SIGNAL( createNewWindow( const KUrl &, const KParts::OpenUrlArguments&, const KParts::BrowserArguments& , const KParts::WindowArgs &, KParts::ReadOnlyPart**) ) );
00344
00345
00346 connect( childExtension, SIGNAL(popupMenu(QPoint,KFileItemList,KParts::OpenUrlArguments,KParts::BrowserArguments,KParts::BrowserExtension::PopupFlags,KParts::BrowserExtension::ActionGroupMap)),
00347 m_extension, SIGNAL(popupMenu(QPoint,KFileItemList,KParts::OpenUrlArguments,KParts::BrowserArguments,KParts::BrowserExtension::PopupFlags,KParts::BrowserExtension::ActionGroupMap)) );
00348 connect( childExtension, SIGNAL(popupMenu(QPoint,KUrl,mode_t,KParts::OpenUrlArguments,KParts::BrowserArguments,KParts::BrowserExtension::PopupFlags,KParts::BrowserExtension::ActionGroupMap)),
00349 m_extension, SIGNAL(popupMenu(QPoint,KUrl,mode_t,KParts::OpenUrlArguments,KParts::BrowserArguments,KParts::BrowserExtension::PopupFlags,KParts::BrowserExtension::ActionGroupMap)) );
00350
00351 if ( m_isHTMLPart )
00352 connect( childExtension, SIGNAL( infoMessage( const QString & ) ),
00353 m_extension, SIGNAL( infoMessage( const QString & ) ) );
00354
00355
00356 childExtension->setBrowserInterface( m_extension->browserInterface() );
00357
00358 connect( childExtension, SIGNAL( enableAction( const char *, bool ) ),
00359 m_extension, SIGNAL( enableAction( const char *, bool ) ) );
00360 connect( childExtension, SIGNAL( setLocationBarURL( const QString& ) ),
00361 m_extension, SIGNAL( setLocationBarURL( const QString& ) ) );
00362 connect( childExtension, SIGNAL( setIconURL( const KUrl& ) ),
00363 m_extension, SIGNAL( setIconURL( const KUrl& ) ) );
00364 connect( childExtension, SIGNAL( loadingProgress( int ) ),
00365 m_extension, SIGNAL( loadingProgress( int ) ) );
00366 if ( m_isHTMLPart )
00367 connect( childExtension, SIGNAL( speedProgress( int ) ),
00368 m_extension, SIGNAL( speedProgress( int ) ) );
00369 connect( childExtension, SIGNAL( selectionInfo( const KFileItemList& ) ),
00370 m_extension, SIGNAL( selectionInfo( const KFileItemList& ) ) );
00371 connect( childExtension, SIGNAL( selectionInfo( const QString& ) ),
00372 m_extension, SIGNAL( selectionInfo( const QString& ) ) );
00373 connect( childExtension, SIGNAL( selectionInfo( const KUrl::List& ) ),
00374 m_extension, SIGNAL( selectionInfo( const KUrl::List& ) ) );
00375 connect( childExtension, SIGNAL( mouseOverInfo( const KFileItem& ) ),
00376 m_extension, SIGNAL( mouseOverInfo( const KFileItem& ) ) );
00377 connect( childExtension, SIGNAL( moveTopLevelWidget( int, int ) ),
00378 m_extension, SIGNAL( moveTopLevelWidget( int, int ) ) );
00379 connect( childExtension, SIGNAL( resizeTopLevelWidget( int, int ) ),
00380 m_extension, SIGNAL( resizeTopLevelWidget( int, int ) ) );
00381 }
00382
00383 m_partIsLoading = false;
00384
00385
00386
00387 loadPlugins( this, m_part, m_part->componentData() );
00388
00389 if ( guiFactory )
00390 guiFactory->addClient( this );
00391 }
00392
00393 void KMultiPart::startOfData()
00394 {
00395 kDebug() << "KMultiPart::startOfData";
00396 Q_ASSERT( !m_nextMimeType.isNull() );
00397 if( m_nextMimeType.isNull() )
00398 return;
00399
00400 if ( m_gzip )
00401 {
00402
00403
00404
00405 m_filter = new HTTPFilterGZip;
00406 connect(m_filter, SIGNAL(output(QByteArray)), this, SLOT(reallySendData(QByteArray)));
00407 }
00408
00409 if ( m_mimeType != m_nextMimeType )
00410 {
00411
00412 m_mimeType = m_nextMimeType;
00413 setPart( m_mimeType );
00414 }
00415 Q_ASSERT( m_part );
00416
00417 m_part->setArguments( arguments() );
00418 KParts::BrowserExtension* childExtension = KParts::BrowserExtension::childObject( m_part );
00419 if ( childExtension )
00420 childExtension->setBrowserArguments( m_extension->browserArguments() );
00421
00422 m_nextMimeType.clear();
00423 if ( m_tempFile ) {
00424 m_tempFile->setAutoRemove( true );
00425 delete m_tempFile;
00426 m_tempFile = 0;
00427 }
00428 if ( m_isHTMLPart )
00429 {
00430 KHTMLPart* htmlPart = static_cast<KHTMLPart *>( static_cast<KParts::ReadOnlyPart *>( m_part ) );
00431 htmlPart->begin( url() );
00432 }
00433 else
00434 {
00435
00436 m_tempFile = new KTemporaryFile;
00437 m_tempFile->open();
00438 }
00439 }
00440
00441 void KMultiPart::sendData( const QByteArray& line )
00442 {
00443 if ( m_filter )
00444 {
00445 m_filter->slotInput( line );
00446 }
00447 else
00448 {
00449 reallySendData( line );
00450 }
00451 }
00452
00453 void KMultiPart::reallySendData( const QByteArray& line )
00454 {
00455 if ( m_isHTMLPart )
00456 {
00457 KHTMLPart* htmlPart = static_cast<KHTMLPart *>( static_cast<KParts::ReadOnlyPart *>( m_part ) );
00458 htmlPart->write( line.data(), line.size() );
00459 }
00460 else if ( m_tempFile )
00461 {
00462 m_tempFile->write( line.data(), line.size() );
00463 }
00464 }
00465
00466 void KMultiPart::endOfData()
00467 {
00468 Q_ASSERT( m_part );
00469 if ( m_isHTMLPart )
00470 {
00471 KHTMLPart* htmlPart = static_cast<KHTMLPart *>( static_cast<KParts::ReadOnlyPart *>( m_part ) );
00472 htmlPart->end();
00473 } else if ( m_tempFile )
00474 {
00475 const QString tempFileName = m_tempFile->fileName();
00476 m_tempFile->close();
00477 if ( m_partIsLoading )
00478 {
00479
00480
00481 kDebug() << "KMultiPart::endOfData part isn't ready, skipping frame";
00482 ++m_numberOfFramesSkipped;
00483 m_tempFile->setAutoRemove( true );
00484 }
00485 else
00486 {
00487 kDebug() << "KMultiPart::endOfData opening " << tempFileName;
00488 KUrl url(tempFileName);
00489 m_partIsLoading = true;
00490 (void) m_part->openUrl( url );
00491 }
00492 delete m_tempFile;
00493 m_tempFile = 0L;
00494 }
00495 }
00496
00497 void KMultiPart::slotPartCompleted()
00498 {
00499 if ( !m_isHTMLPart )
00500 {
00501 Q_ASSERT( m_part );
00502
00503 Q_ASSERT( m_part->url().isLocalFile() );
00504 kDebug() << "slotPartCompleted deleting " << m_part->url().toLocalFile();
00505 (void) unlink( QFile::encodeName( m_part->url().toLocalFile() ) );
00506 m_partIsLoading = false;
00507 ++m_numberOfFrames;
00508
00509 }
00510 }
00511
00512 bool KMultiPart::closeUrl()
00513 {
00514 m_timer->stop();
00515 if ( m_part )
00516 return m_part->closeUrl();
00517 return true;
00518 }
00519
00520 void KMultiPart::guiActivateEvent( KParts::GUIActivateEvent * )
00521 {
00522
00523
00524
00525 }
00526
00527 void KMultiPart::slotJobFinished( KJob *job )
00528 {
00529 if ( job->error() )
00530 {
00531
00532 job->uiDelegate()->showErrorMessage();
00533 emit canceled( job->errorString() );
00534 }
00535 else
00536 {
00537
00538
00539
00540
00541
00542
00543 emit completed();
00544
00545
00546 }
00547 m_job = 0L;
00548 }
00549
00550 void KMultiPart::slotProgressInfo()
00551 {
00552 int time = m_qtime.elapsed();
00553 if ( !time ) return;
00554 if ( m_totalNumberOfFrames == m_numberOfFrames + m_numberOfFramesSkipped )
00555 return;
00556
00557 QString str( "%1 frames per second, %2 frames skipped per second" );
00558 str = str.arg( 1000.0 * (double)m_numberOfFrames / (double)time );
00559 str = str.arg( 1000.0 * (double)m_numberOfFramesSkipped / (double)time );
00560 m_totalNumberOfFrames = m_numberOfFrames + m_numberOfFramesSkipped;
00561
00562 emit m_extension->infoMessage( str );
00563 }
00564
00565 KAboutData* KMultiPart::createAboutData()
00566 {
00567 KAboutData* aboutData = new KAboutData( "kmultipart", 0, ki18n("KMultiPart"),
00568 "0.1",
00569 ki18n( "Embeddable component for multipart/mixed" ),
00570 KAboutData::License_GPL,
00571 ki18n("Copyright 2001, David Faure <email>david@mandrakesoft.com</email>"));
00572 return aboutData;
00573 }
00574
00575 #if 0
00576 KMultiPartBrowserExtension::KMultiPartBrowserExtension( KMultiPart *parent, const char *name )
00577 : KParts::BrowserExtension( parent, name )
00578 {
00579 m_imgPart = parent;
00580 }
00581
00582 int KMultiPartBrowserExtension::xOffset()
00583 {
00584 return m_imgPart->doc()->view()->contentsX();
00585 }
00586
00587 int KMultiPartBrowserExtension::yOffset()
00588 {
00589 return m_imgPart->doc()->view()->contentsY();
00590 }
00591
00592 void KMultiPartBrowserExtension::print()
00593 {
00594 static_cast<KHTMLPartBrowserExtension *>( m_imgPart->doc()->browserExtension() )->print();
00595 }
00596
00597 void KMultiPartBrowserExtension::reparseConfiguration()
00598 {
00599 static_cast<KHTMLPartBrowserExtension *>( m_imgPart->doc()->browserExtension() )->reparseConfiguration();
00600 m_imgPart->doc()->setAutoloadImages( true );
00601 }
00602 #endif
00603
00604 #include "kmultipart.moc"