00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "browserextension.h"
00021
00022 #include <qapplication.h>
00023 #include <qclipboard.h>
00024 #include <qtimer.h>
00025 #include <qobjectlist.h>
00026 #include <qmetaobject.h>
00027 #include <qregexp.h>
00028 #include <qstrlist.h>
00029 #include <qstylesheet.h>
00030
00031 #include <kdebug.h>
00032 #include <klocale.h>
00033 #include <kmessagebox.h>
00034 #include <kstaticdeleter.h>
00035 #include <kurifilter.h>
00036 #include <assert.h>
00037
00038 using namespace KParts;
00039
00040 const char *OpenURLEvent::s_strOpenURLEvent = "KParts/BrowserExtension/OpenURLevent";
00041
00042 class OpenURLEvent::OpenURLEventPrivate
00043 {
00044 public:
00045 OpenURLEventPrivate()
00046 {
00047 }
00048 ~OpenURLEventPrivate()
00049 {
00050 }
00051 };
00052
00053 OpenURLEvent::OpenURLEvent( ReadOnlyPart *part, const KURL &url, const URLArgs &args )
00054 : Event( s_strOpenURLEvent ), m_part( part ), m_url( url ), m_args( args )
00055 {
00056
00057 }
00058
00059 OpenURLEvent::~OpenURLEvent()
00060 {
00061
00062 }
00063
00064 namespace KParts
00065 {
00066
00067 struct URLArgsPrivate
00068 {
00069 URLArgsPrivate() {
00070 doPost = false;
00071 redirectedRequest = false;
00072 lockHistory = false;
00073 newTab = false;
00074 forcesNewWindow = false;
00075 }
00076 QString contentType;
00077 QMap<QString, QString> metaData;
00078 bool doPost;
00079 bool redirectedRequest;
00080 bool lockHistory;
00081 bool newTab;
00082 bool forcesNewWindow;
00083 };
00084
00085 }
00086
00087 URLArgs::URLArgs()
00088 {
00089 reload = false;
00090 xOffset = 0;
00091 yOffset = 0;
00092 trustedSource = false;
00093 d = 0L;
00094 }
00095
00096
00097 URLArgs::URLArgs( bool _reload, int _xOffset, int _yOffset, const QString &_serviceType )
00098 {
00099 reload = _reload;
00100 xOffset = _xOffset;
00101 yOffset = _yOffset;
00102 serviceType = _serviceType;
00103 d = 0L;
00104 }
00105
00106 URLArgs::URLArgs( const URLArgs &args )
00107 {
00108 d = 0L;
00109 (*this) = args;
00110 }
00111
00112 URLArgs &URLArgs::operator=(const URLArgs &args)
00113 {
00114 if (this == &args) return *this;
00115
00116 delete d; d= 0;
00117
00118 reload = args.reload;
00119 xOffset = args.xOffset;
00120 yOffset = args.yOffset;
00121 serviceType = args.serviceType;
00122 postData = args.postData;
00123 frameName = args.frameName;
00124 docState = args.docState;
00125 trustedSource = args.trustedSource;
00126
00127 if ( args.d )
00128 d = new URLArgsPrivate( * args.d );
00129
00130 return *this;
00131 }
00132
00133 URLArgs::~URLArgs()
00134 {
00135 delete d;
00136 d = 0;
00137 }
00138
00139 void URLArgs::setContentType( const QString & contentType )
00140 {
00141 if (!d)
00142 d = new URLArgsPrivate;
00143 d->contentType = contentType;
00144 }
00145
00146 void URLArgs::setRedirectedRequest( bool redirected )
00147 {
00148 if (!d)
00149 d = new URLArgsPrivate;
00150 d->redirectedRequest = redirected;
00151 }
00152
00153 bool URLArgs::redirectedRequest () const
00154 {
00155 return d ? d->redirectedRequest : false;
00156 }
00157
00158 QString URLArgs::contentType() const
00159 {
00160 return d ? d->contentType : QString::null;
00161 }
00162
00163 QMap<QString, QString> &URLArgs::metaData()
00164 {
00165 if (!d)
00166 d = new URLArgsPrivate;
00167 return d->metaData;
00168 }
00169
00170 void URLArgs::setDoPost( bool enable )
00171 {
00172 if ( !d )
00173 d = new URLArgsPrivate;
00174 d->doPost = enable;
00175 }
00176
00177 bool URLArgs::doPost() const
00178 {
00179 return d ? d->doPost : false;
00180 }
00181
00182 void URLArgs::setLockHistory( bool lock )
00183 {
00184 if (!d)
00185 d = new URLArgsPrivate;
00186 d->lockHistory = lock;
00187 }
00188
00189 bool URLArgs::lockHistory() const
00190 {
00191 return d ? d->lockHistory : false;
00192 }
00193
00194 void URLArgs::setNewTab( bool newTab )
00195 {
00196 if (!d)
00197 d = new URLArgsPrivate;
00198 d->newTab = newTab;
00199 }
00200
00201 bool URLArgs::newTab() const
00202 {
00203 return d ? d->newTab : false;
00204 }
00205
00206 void URLArgs::setForcesNewWindow( bool forcesNewWindow )
00207 {
00208 if (!d)
00209 d = new URLArgsPrivate;
00210 d->forcesNewWindow = forcesNewWindow;
00211 }
00212
00213 bool URLArgs::forcesNewWindow() const
00214 {
00215 return d ? d->forcesNewWindow : false;
00216 }
00217
00218 namespace KParts
00219 {
00220
00221 struct WindowArgsPrivate
00222 {
00223 };
00224
00225 }
00226
00227 WindowArgs::WindowArgs()
00228 {
00229 x = y = width = height = -1;
00230 fullscreen = false;
00231 menuBarVisible = true;
00232 toolBarsVisible = true;
00233 statusBarVisible = true;
00234 scrollBarsVisible = true;
00235 resizable = true;
00236 lowerWindow = false;
00237 d = 0;
00238 }
00239
00240 WindowArgs::WindowArgs( const WindowArgs &args )
00241 {
00242 d = 0;
00243 (*this) = args;
00244 }
00245
00246 WindowArgs::~WindowArgs()
00247 {
00248 delete d;
00249 }
00250
00251 WindowArgs &WindowArgs::operator=( const WindowArgs &args )
00252 {
00253 if ( this == &args ) return *this;
00254
00255 delete d; d = 0;
00256
00257 x = args.x;
00258 y = args.y;
00259 width = args.width;
00260 height = args.height;
00261 fullscreen = args.fullscreen;
00262 menuBarVisible = args.menuBarVisible;
00263 toolBarsVisible = args.toolBarsVisible;
00264 statusBarVisible = args.statusBarVisible;
00265 scrollBarsVisible = args.scrollBarsVisible;
00266 resizable = args.resizable;
00267 lowerWindow = args.lowerWindow;
00268
00269
00270
00271
00272
00273
00274
00275
00276 return *this;
00277 }
00278
00279 WindowArgs::WindowArgs( const QRect &_geometry, bool _fullscreen, bool _menuBarVisible,
00280 bool _toolBarsVisible, bool _statusBarVisible, bool _resizable )
00281 {
00282 d = 0;
00283 x = _geometry.x();
00284 y = _geometry.y();
00285 width = _geometry.width();
00286 height = _geometry.height();
00287 fullscreen = _fullscreen;
00288 menuBarVisible = _menuBarVisible;
00289 toolBarsVisible = _toolBarsVisible;
00290 statusBarVisible = _statusBarVisible;
00291 resizable = _resizable;
00292 lowerWindow = false;
00293 }
00294
00295 WindowArgs::WindowArgs( int _x, int _y, int _width, int _height, bool _fullscreen,
00296 bool _menuBarVisible, bool _toolBarsVisible,
00297 bool _statusBarVisible, bool _resizable )
00298 {
00299 d = 0;
00300 x = _x;
00301 y = _y;
00302 width = _width;
00303 height = _height;
00304 fullscreen = _fullscreen;
00305 menuBarVisible = _menuBarVisible;
00306 toolBarsVisible = _toolBarsVisible;
00307 statusBarVisible = _statusBarVisible;
00308 resizable = _resizable;
00309 lowerWindow = false;
00310 }
00311
00312 namespace KParts
00313 {
00314
00315
00316 class KBitArray
00317 {
00318 public:
00319 int val;
00320 KBitArray() { val = 0; }
00321 bool operator [](int index) { return (val & (1 << index)) ? true : false; }
00322 void setBit(int index, bool value) {
00323 if (value) val = val | (1 << index);
00324 else val = val & ~(1 << index);
00325 }
00326 };
00327
00328 class BrowserExtensionPrivate
00329 {
00330 public:
00331 BrowserExtensionPrivate()
00332 {
00333 m_browserInterface = 0;
00334 }
00335 ~BrowserExtensionPrivate()
00336 {
00337 }
00338
00339 struct DelayedRequest {
00340 KURL m_delayedURL;
00341 KParts::URLArgs m_delayedArgs;
00342 };
00343 QValueList<DelayedRequest> m_requests;
00344 bool m_urlDropHandlingEnabled;
00345 KBitArray m_actionStatus;
00346 QMap<int, QString> m_actionText;
00347 BrowserInterface *m_browserInterface;
00348 };
00349
00350 }
00351
00352 BrowserExtension::ActionSlotMap * BrowserExtension::s_actionSlotMap = 0L;
00353 static KStaticDeleter<BrowserExtension::ActionSlotMap> actionSlotMapsd;
00354 BrowserExtension::ActionNumberMap * BrowserExtension::s_actionNumberMap = 0L;
00355 static KStaticDeleter<BrowserExtension::ActionNumberMap> actionNumberMapsd;
00356
00357 BrowserExtension::BrowserExtension( KParts::ReadOnlyPart *parent,
00358 const char *name )
00359 : QObject( parent, name), m_part( parent )
00360 {
00361
00362 d = new BrowserExtensionPrivate;
00363 d->m_urlDropHandlingEnabled = false;
00364
00365 if ( !s_actionSlotMap )
00366
00367 createActionSlotMap();
00368
00369
00370
00371 ActionSlotMap::ConstIterator it = s_actionSlotMap->begin();
00372 ActionSlotMap::ConstIterator itEnd = s_actionSlotMap->end();
00373 QStrList slotNames = metaObject()->slotNames();
00374 for ( int i=0 ; it != itEnd ; ++it, ++i )
00375 {
00376
00377 d->m_actionStatus.setBit( i, slotNames.contains( it.key()+"()" ) );
00378 }
00379
00380 connect( m_part, SIGNAL( completed() ),
00381 this, SLOT( slotCompleted() ) );
00382 connect( this, SIGNAL( openURLRequest( const KURL &, const KParts::URLArgs & ) ),
00383 this, SLOT( slotOpenURLRequest( const KURL &, const KParts::URLArgs & ) ) );
00384 connect( this, SIGNAL( enableAction( const char *, bool ) ),
00385 this, SLOT( slotEnableAction( const char *, bool ) ) );
00386 connect( this, SIGNAL( setActionText( const char *, const QString& ) ),
00387 this, SLOT( slotSetActionText( const char *, const QString& ) ) );
00388 }
00389
00390 BrowserExtension::~BrowserExtension()
00391 {
00392
00393 delete d;
00394 }
00395
00396 void BrowserExtension::setURLArgs( const URLArgs &args )
00397 {
00398 m_args = args;
00399 }
00400
00401 URLArgs BrowserExtension::urlArgs() const
00402 {
00403 return m_args;
00404 }
00405
00406 int BrowserExtension::xOffset()
00407 {
00408 return 0;
00409 }
00410
00411 int BrowserExtension::yOffset()
00412 {
00413 return 0;
00414 }
00415
00416 void BrowserExtension::saveState( QDataStream &stream )
00417 {
00418 stream << m_part->url() << (Q_INT32)xOffset() << (Q_INT32)yOffset();
00419 }
00420
00421 void BrowserExtension::restoreState( QDataStream &stream )
00422 {
00423 KURL u;
00424 Q_INT32 xOfs, yOfs;
00425 stream >> u >> xOfs >> yOfs;
00426
00427 URLArgs args( urlArgs() );
00428 args.xOffset = xOfs;
00429 args.yOffset = yOfs;
00430
00431 setURLArgs( args );
00432
00433 m_part->openURL( u );
00434 }
00435
00436 bool BrowserExtension::isURLDropHandlingEnabled() const
00437 {
00438 return d->m_urlDropHandlingEnabled;
00439 }
00440
00441 void BrowserExtension::setURLDropHandlingEnabled( bool enable )
00442 {
00443 d->m_urlDropHandlingEnabled = enable;
00444 }
00445
00446 void BrowserExtension::slotCompleted()
00447 {
00448
00449 setURLArgs( URLArgs() );
00450 }
00451
00452 void BrowserExtension::pasteRequest()
00453 {
00454 QCString plain( "plain" );
00455 QString url = QApplication::clipboard()->text(plain, QClipboard::Selection).stripWhiteSpace();
00456
00457 url.remove(QRegExp("[\\ ]*\\n+[\\ ]*"));
00458
00459
00460 QStringList filters = KURIFilter::self()->pluginNames();
00461 filters.remove( "kuriikwsfilter" );
00462 filters.remove( "localdomainurifilter" );
00463 KURIFilterData filterData;
00464 filterData.setData( url );
00465 filterData.setCheckForExecutables( false );
00466 if ( KURIFilter::self()->filterURI( filterData, filters ) )
00467 {
00468 switch ( filterData.uriType() )
00469 {
00470 case KURIFilterData::LOCAL_FILE:
00471 case KURIFilterData::LOCAL_DIR:
00472 case KURIFilterData::NET_PROTOCOL:
00473 slotOpenURLRequest( filterData.uri(), KParts::URLArgs() );
00474 break;
00475 case KURIFilterData::ERROR:
00476 KMessageBox::sorry( m_part->widget(), filterData.errorMsg() );
00477 break;
00478 default:
00479 break;
00480 }
00481 }
00482 else if ( KURIFilter::self()->filterURI( filterData, "kuriikwsfilter" ) && url.length() < 250 )
00483 {
00484 if ( KMessageBox::questionYesNo( m_part->widget(),
00485 i18n( "<qt>Do you want to search the Internet for <b>%1</b>?" ).arg( QStyleSheet::escape(url) ),
00486 i18n( "Internet Search" ), KGuiItem( i18n( "&Search" ), "find"),
00487 KStdGuiItem::cancel(), "MiddleClickSearch" ) == KMessageBox::Yes)
00488 slotOpenURLRequest( filterData.uri(), KParts::URLArgs() );
00489 }
00490 }
00491
00492 void BrowserExtension::slotOpenURLRequest( const KURL &url, const KParts::URLArgs &args )
00493 {
00494
00495 BrowserExtensionPrivate::DelayedRequest req;
00496 req.m_delayedURL = url;
00497 req.m_delayedArgs = args;
00498 d->m_requests.append( req );
00499 QTimer::singleShot( 0, this, SLOT( slotEmitOpenURLRequestDelayed() ) );
00500 }
00501
00502 void BrowserExtension::slotEmitOpenURLRequestDelayed()
00503 {
00504 if (d->m_requests.isEmpty()) return;
00505 BrowserExtensionPrivate::DelayedRequest req = d->m_requests.front();
00506 d->m_requests.pop_front();
00507 emit openURLRequestDelayed( req.m_delayedURL, req.m_delayedArgs );
00508
00509 }
00510
00511 void BrowserExtension::setBrowserInterface( BrowserInterface *impl )
00512 {
00513 d->m_browserInterface = impl;
00514 }
00515
00516 BrowserInterface *BrowserExtension::browserInterface() const
00517 {
00518 return d->m_browserInterface;
00519 }
00520
00521 void BrowserExtension::slotEnableAction( const char * name, bool enabled )
00522 {
00523
00524 ActionNumberMap::ConstIterator it = s_actionNumberMap->find( name );
00525 if ( it != s_actionNumberMap->end() )
00526 {
00527 d->m_actionStatus.setBit( it.data(), enabled );
00528
00529 }
00530 else
00531 kdWarning() << "BrowserExtension::slotEnableAction unknown action " << name << endl;
00532 }
00533
00534 bool BrowserExtension::isActionEnabled( const char * name ) const
00535 {
00536 int actionNumber = (*s_actionNumberMap)[ name ];
00537 return d->m_actionStatus[ actionNumber ];
00538 }
00539
00540 void BrowserExtension::slotSetActionText( const char * name, const QString& text )
00541 {
00542 kdDebug() << "BrowserExtension::slotSetActionText " << name << " " << text << endl;
00543 ActionNumberMap::ConstIterator it = s_actionNumberMap->find( name );
00544 if ( it != s_actionNumberMap->end() )
00545 {
00546 d->m_actionText[ it.data() ] = text;
00547 }
00548 else
00549 kdWarning() << "BrowserExtension::slotSetActionText unknown action " << name << endl;
00550 }
00551
00552 QString BrowserExtension::actionText( const char * name ) const
00553 {
00554 int actionNumber = (*s_actionNumberMap)[ name ];
00555 QMap<int, QString>::ConstIterator it = d->m_actionText.find( actionNumber );
00556 if ( it != d->m_actionText.end() )
00557 return *it;
00558 return QString::null;
00559 }
00560
00561
00562 BrowserExtension::ActionSlotMap BrowserExtension::actionSlotMap()
00563 {
00564 return *actionSlotMapPtr();
00565 }
00566
00567 BrowserExtension::ActionSlotMap * BrowserExtension::actionSlotMapPtr()
00568 {
00569 if (!s_actionSlotMap)
00570 createActionSlotMap();
00571 return s_actionSlotMap;
00572 }
00573
00574 void BrowserExtension::createActionSlotMap()
00575 {
00576 assert(!s_actionSlotMap);
00577 s_actionSlotMap = actionSlotMapsd.setObject( s_actionSlotMap, new ActionSlotMap );
00578
00579 s_actionSlotMap->insert( "cut", SLOT( cut() ) );
00580 s_actionSlotMap->insert( "copy", SLOT( copy() ) );
00581 s_actionSlotMap->insert( "paste", SLOT( paste() ) );
00582 s_actionSlotMap->insert( "rename", SLOT( rename() ) );
00583 s_actionSlotMap->insert( "trash", SLOT( trash() ) );
00584 s_actionSlotMap->insert( "del", SLOT( del() ) );
00585 s_actionSlotMap->insert( "properties", SLOT( properties() ) );
00586 s_actionSlotMap->insert( "editMimeType", SLOT( editMimeType() ) );
00587 s_actionSlotMap->insert( "print", SLOT( print() ) );
00588
00589
00590
00591
00592
00593
00594
00595 assert(!s_actionNumberMap);
00596 s_actionNumberMap = actionNumberMapsd.setObject( s_actionNumberMap, new ActionNumberMap );
00597 ActionSlotMap::ConstIterator it = s_actionSlotMap->begin();
00598 ActionSlotMap::ConstIterator itEnd = s_actionSlotMap->end();
00599 for ( int i=0 ; it != itEnd ; ++it, ++i )
00600 {
00601
00602 s_actionNumberMap->insert( it.key(), i );
00603 }
00604 }
00605
00606 BrowserExtension *BrowserExtension::childObject( QObject *obj )
00607 {
00608 if ( !obj || !obj->children() )
00609 return 0L;
00610
00611
00612
00613 const QObjectList *children = obj->children();
00614 QObjectListIt it( *children );
00615 for (; it.current(); ++it )
00616 if ( it.current()->inherits( "KParts::BrowserExtension" ) )
00617 return static_cast<KParts::BrowserExtension *>( it.current() );
00618
00619 return 0L;
00620 }
00621
00622 namespace KParts
00623 {
00624
00625 class BrowserHostExtension::BrowserHostExtensionPrivate
00626 {
00627 public:
00628 BrowserHostExtensionPrivate()
00629 {
00630 }
00631 ~BrowserHostExtensionPrivate()
00632 {
00633 }
00634
00635 KParts::ReadOnlyPart *m_part;
00636 };
00637
00638 }
00639
00640 BrowserHostExtension::BrowserHostExtension( KParts::ReadOnlyPart *parent, const char *name )
00641 : QObject( parent, name )
00642 {
00643 d = new BrowserHostExtensionPrivate;
00644 d->m_part = parent;
00645 }
00646
00647 BrowserHostExtension::~BrowserHostExtension()
00648 {
00649 delete d;
00650 }
00651
00652 QStringList BrowserHostExtension::frameNames() const
00653 {
00654 return QStringList();
00655 }
00656
00657 const QPtrList<KParts::ReadOnlyPart> BrowserHostExtension::frames() const
00658 {
00659 return QPtrList<KParts::ReadOnlyPart>();
00660 }
00661
00662 bool BrowserHostExtension::openURLInFrame( const KURL &, const KParts::URLArgs & )
00663 {
00664 return false;
00665 }
00666
00667 BrowserHostExtension *BrowserHostExtension::childObject( QObject *obj )
00668 {
00669 if ( !obj || !obj->children() )
00670 return 0L;
00671
00672
00673
00674 const QObjectList *children = obj->children();
00675 QObjectListIt it( *children );
00676 for (; it.current(); ++it )
00677 if ( it.current()->inherits( "KParts::BrowserHostExtension" ) )
00678 return static_cast<KParts::BrowserHostExtension *>( it.current() );
00679
00680 return 0L;
00681 }
00682
00683 void BrowserExtension::virtual_hook( int, void* )
00684 { }
00685
00686 BrowserHostExtension *
00687 BrowserHostExtension::findFrameParent(KParts::ReadOnlyPart *callingPart, const QString &frame)
00688 {
00689 FindFrameParentParams param;
00690 param.parent = 0;
00691 param.callingPart = callingPart;
00692 param.frame = frame;
00693 virtual_hook(VIRTUAL_FIND_FRAME_PARENT, ¶m);
00694 return param.parent;
00695 }
00696
00697 void BrowserHostExtension::virtual_hook( int, void* )
00698 { }
00699
00700 LiveConnectExtension::LiveConnectExtension( KParts::ReadOnlyPart *parent, const char *name ) : QObject( parent, name) {}
00701
00702 bool LiveConnectExtension::get( const unsigned long, const QString &, Type &, unsigned long &, QString & ) {
00703 return false;
00704 }
00705
00706 bool LiveConnectExtension::put( const unsigned long, const QString &, const QString & ) {
00707 return false;
00708 }
00709
00710 bool LiveConnectExtension::call( const unsigned long, const QString &, const QStringList &, Type &, unsigned long &, QString & ) {
00711 return false;
00712 }
00713
00714 void LiveConnectExtension::unregister( const unsigned long ) {}
00715
00716 LiveConnectExtension *LiveConnectExtension::childObject( QObject *obj )
00717 {
00718 if ( !obj || !obj->children() )
00719 return 0L;
00720
00721
00722
00723 const QObjectList *children = obj->children();
00724 QObjectListIt it( *children );
00725 for (; it.current(); ++it )
00726 if ( it.current()->inherits( "KParts::LiveConnectExtension" ) )
00727 return static_cast<KParts::LiveConnectExtension *>( it.current() );
00728
00729 return 0L;
00730 }
00731
00732 #include "browserextension.moc"