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 <qtimer.h>
00023 #include <qobjectlist.h>
00024 #include <qmetaobject.h>
00025 #include <qstrlist.h>
00026 #include <kdebug.h>
00027 #include <kstaticdeleter.h>
00028 #include <assert.h>
00029
00030 using namespace KParts;
00031
00032 const char *OpenURLEvent::s_strOpenURLEvent = "KParts/BrowserExtension/OpenURLevent";
00033
00034 class OpenURLEvent::OpenURLEventPrivate
00035 {
00036 public:
00037 OpenURLEventPrivate()
00038 {
00039 }
00040 ~OpenURLEventPrivate()
00041 {
00042 }
00043 };
00044
00045 OpenURLEvent::OpenURLEvent( ReadOnlyPart *part, const KURL &url, const URLArgs &args )
00046 : Event( s_strOpenURLEvent ), m_part( part ), m_url( url ), m_args( args )
00047 {
00048
00049 }
00050
00051 OpenURLEvent::~OpenURLEvent()
00052 {
00053
00054 }
00055
00056 namespace KParts
00057 {
00058
00059 struct URLArgsPrivate
00060 {
00061 URLArgsPrivate() {
00062 doPost = false;
00063 lockHistory = false;
00064 newTab = false;
00065 }
00066 QString contentType;
00067 QMap<QString, QString> metaData;
00068 bool doPost;
00069 bool lockHistory;
00070 bool newTab;
00071 };
00072
00073 };
00074
00075 URLArgs::URLArgs()
00076 {
00077 reload = false;
00078 xOffset = 0;
00079 yOffset = 0;
00080 trustedSource = false;
00081 d = 0L;
00082 }
00083
00084
00085 URLArgs::URLArgs( bool _reload, int _xOffset, int _yOffset, const QString &_serviceType )
00086 {
00087 reload = _reload;
00088 xOffset = _xOffset;
00089 yOffset = _yOffset;
00090 serviceType = _serviceType;
00091 d = 0L;
00092 }
00093
00094 URLArgs::URLArgs( const URLArgs &args )
00095 {
00096 d = 0L;
00097 (*this) = args;
00098 }
00099
00100 URLArgs &URLArgs::operator=(const URLArgs &args)
00101 {
00102 if (this == &args) return *this;
00103
00104 delete d; d= 0;
00105
00106 reload = args.reload;
00107 xOffset = args.xOffset;
00108 yOffset = args.yOffset;
00109 serviceType = args.serviceType;
00110 postData = args.postData;
00111 frameName = args.frameName;
00112 docState = args.docState;
00113 trustedSource = args.trustedSource;
00114
00115 if ( args.d )
00116 d = new URLArgsPrivate( * args.d );
00117
00118 return *this;
00119 }
00120
00121 URLArgs::~URLArgs()
00122 {
00123 delete d; d = 0;
00124 }
00125
00126 void URLArgs::setContentType( const QString & contentType )
00127 {
00128 if (!d)
00129 d = new URLArgsPrivate;
00130 d->contentType = contentType;
00131 }
00132
00133 QString URLArgs::contentType() const
00134 {
00135 return d ? d->contentType : QString::null;
00136 }
00137
00138 QMap<QString, QString> &URLArgs::metaData()
00139 {
00140 if (!d)
00141 d = new URLArgsPrivate;
00142 return d->metaData;
00143 }
00144
00145 void URLArgs::setDoPost( bool enable )
00146 {
00147 if ( !d )
00148 d = new URLArgsPrivate;
00149 d->doPost = enable;
00150 }
00151
00152 bool URLArgs::doPost() const
00153 {
00154 return d ? d->doPost : false;
00155 }
00156
00157 void URLArgs::setLockHistory( bool lock )
00158 {
00159 if (!d)
00160 d = new URLArgsPrivate;
00161 d->lockHistory = lock;
00162 }
00163
00164 bool URLArgs::lockHistory() const
00165 {
00166 return d ? d->lockHistory : false;
00167 }
00168
00169 void URLArgs::setNewTab( bool newTab )
00170 {
00171 if (!d)
00172 d = new URLArgsPrivate;
00173 d->newTab = newTab;
00174 }
00175
00176 bool URLArgs::newTab() const
00177 {
00178 return d ? d->newTab : false;
00179 }
00180
00181 namespace KParts
00182 {
00183
00184 struct WindowArgsPrivate
00185 {
00186 };
00187
00188 };
00189
00190 WindowArgs::WindowArgs()
00191 {
00192 x = y = width = height = -1;
00193 fullscreen = false;
00194 menuBarVisible = true;
00195 toolBarsVisible = true;
00196 statusBarVisible = true;
00197 resizable = true;
00198 lowerWindow = false;
00199 d = 0;
00200 }
00201
00202 WindowArgs::WindowArgs( const WindowArgs &args )
00203 {
00204 d = 0;
00205 (*this) = args;
00206 }
00207
00208 WindowArgs &WindowArgs::operator=( const WindowArgs &args )
00209 {
00210 if ( this == &args ) return *this;
00211
00212 delete d; d = 0;
00213
00214 x = args.x;
00215 y = args.y;
00216 width = args.width;
00217 height = args.height;
00218 fullscreen = args.fullscreen;
00219 menuBarVisible = args.menuBarVisible;
00220 toolBarsVisible = args.toolBarsVisible;
00221 statusBarVisible = args.statusBarVisible;
00222 resizable = args.resizable;
00223 lowerWindow = args.lowerWindow;
00224
00225
00226
00227
00228
00229
00230
00231
00232 return *this;
00233 }
00234
00235 WindowArgs::WindowArgs( const QRect &_geometry, bool _fullscreen, bool _menuBarVisible,
00236 bool _toolBarsVisible, bool _statusBarVisible, bool _resizable )
00237 {
00238 d = 0;
00239 x = _geometry.x();
00240 y = _geometry.y();
00241 width = _geometry.width();
00242 height = _geometry.height();
00243 fullscreen = _fullscreen;
00244 menuBarVisible = _menuBarVisible;
00245 toolBarsVisible = _toolBarsVisible;
00246 statusBarVisible = _statusBarVisible;
00247 resizable = _resizable;
00248 lowerWindow = false;
00249 }
00250
00251 WindowArgs::WindowArgs( int _x, int _y, int _width, int _height, bool _fullscreen,
00252 bool _menuBarVisible, bool _toolBarsVisible,
00253 bool _statusBarVisible, bool _resizable )
00254 {
00255 d = 0;
00256 x = _x;
00257 y = _y;
00258 width = _width;
00259 height = _height;
00260 fullscreen = _fullscreen;
00261 menuBarVisible = _menuBarVisible;
00262 toolBarsVisible = _toolBarsVisible;
00263 statusBarVisible = _statusBarVisible;
00264 resizable = _resizable;
00265 lowerWindow = false;
00266 }
00267
00268 namespace KParts
00269 {
00270
00271
00272 class KBitArray
00273 {
00274 public:
00275 int val;
00276 KBitArray() { val = 0; }
00277 bool operator [](int index) { return (val & (1 << index)) ? true : false; }
00278 void setBit(int index, bool value) {
00279 if (value) val = val | (1 << index);
00280 else val = val & ~(1 << index);
00281 }
00282 };
00283
00284 class BrowserExtensionPrivate
00285 {
00286 public:
00287 BrowserExtensionPrivate()
00288 {
00289 m_browserInterface = 0;
00290 }
00291 ~BrowserExtensionPrivate()
00292 {
00293 }
00294
00295 struct DelayedRequest {
00296 KURL m_delayedURL;
00297 KParts::URLArgs m_delayedArgs;
00298 };
00299 QValueList<DelayedRequest> m_requests;
00300 bool m_urlDropHandlingEnabled;
00301 KBitArray m_actionStatus;
00302 BrowserInterface *m_browserInterface;
00303 };
00304
00305 };
00306
00307 BrowserExtension::ActionSlotMap * BrowserExtension::s_actionSlotMap = 0L;
00308 KStaticDeleter<BrowserExtension::ActionSlotMap> actionSlotMapsd;
00309 BrowserExtension::ActionNumberMap * BrowserExtension::s_actionNumberMap = 0L;
00310 KStaticDeleter<BrowserExtension::ActionNumberMap> actionNumberMapsd;
00311
00312 BrowserExtension::BrowserExtension( KParts::ReadOnlyPart *parent,
00313 const char *name )
00314 : QObject( parent, name), m_part( parent )
00315 {
00316
00317 d = new BrowserExtensionPrivate;
00318 d->m_urlDropHandlingEnabled = false;
00319
00320 if ( !s_actionSlotMap )
00321
00322 createActionSlotMap();
00323
00324
00325
00326 ActionSlotMap::ConstIterator it = s_actionSlotMap->begin();
00327 ActionSlotMap::ConstIterator itEnd = s_actionSlotMap->end();
00328 QStrList slotNames = metaObject()->slotNames();
00329 for ( int i=0 ; it != itEnd ; ++it, ++i )
00330 {
00331
00332 d->m_actionStatus.setBit( i, slotNames.contains( it.key()+"()" ) );
00333 }
00334
00335 connect( m_part, SIGNAL( completed() ),
00336 this, SLOT( slotCompleted() ) );
00337 connect( this, SIGNAL( openURLRequest( const KURL &, const KParts::URLArgs & ) ),
00338 this, SLOT( slotOpenURLRequest( const KURL &, const KParts::URLArgs & ) ) );
00339 connect( this, SIGNAL( enableAction( const char *, bool ) ),
00340 this, SLOT( slotEnableAction( const char *, bool ) ) );
00341 }
00342
00343 BrowserExtension::~BrowserExtension()
00344 {
00345
00346 delete d;
00347 }
00348
00349 void BrowserExtension::setURLArgs( const URLArgs &args )
00350 {
00351 m_args = args;
00352 }
00353
00354 URLArgs BrowserExtension::urlArgs() const
00355 {
00356 return m_args;
00357 }
00358
00359 int BrowserExtension::xOffset()
00360 {
00361 return 0;
00362 }
00363
00364 int BrowserExtension::yOffset()
00365 {
00366 return 0;
00367 }
00368
00369 void BrowserExtension::saveState( QDataStream &stream )
00370 {
00371 stream << m_part->url() << (Q_INT32)xOffset() << (Q_INT32)yOffset();
00372 }
00373
00374 void BrowserExtension::restoreState( QDataStream &stream )
00375 {
00376 KURL u;
00377 Q_INT32 xOfs, yOfs;
00378 stream >> u >> xOfs >> yOfs;
00379
00380 URLArgs args( urlArgs() );
00381 args.xOffset = xOfs;
00382 args.yOffset = yOfs;
00383
00384 setURLArgs( args );
00385
00386 m_part->openURL( u );
00387 }
00388
00389 bool BrowserExtension::isURLDropHandlingEnabled() const
00390 {
00391 return d->m_urlDropHandlingEnabled;
00392 }
00393
00394 void BrowserExtension::setURLDropHandlingEnabled( bool enable )
00395 {
00396 d->m_urlDropHandlingEnabled = enable;
00397 }
00398
00399 void BrowserExtension::slotCompleted()
00400 {
00401
00402 setURLArgs( URLArgs() );
00403 }
00404
00405 void BrowserExtension::slotOpenURLRequest( const KURL &url, const KParts::URLArgs &args )
00406 {
00407
00408 BrowserExtensionPrivate::DelayedRequest req;
00409 req.m_delayedURL = url;
00410 req.m_delayedArgs = args;
00411 d->m_requests.append( req );
00412 QTimer::singleShot( 0, this, SLOT( slotEmitOpenURLRequestDelayed() ) );
00413 }
00414
00415 void BrowserExtension::slotEmitOpenURLRequestDelayed()
00416 {
00417 if (d->m_requests.isEmpty()) return;
00418 BrowserExtensionPrivate::DelayedRequest req = d->m_requests.front();
00419 d->m_requests.pop_front();
00420 emit openURLRequestDelayed( req.m_delayedURL, req.m_delayedArgs );
00421
00422 }
00423
00424 void BrowserExtension::setBrowserInterface( BrowserInterface *impl )
00425 {
00426 d->m_browserInterface = impl;
00427 }
00428
00429 BrowserInterface *BrowserExtension::browserInterface() const
00430 {
00431 return d->m_browserInterface;
00432 }
00433
00434 void BrowserExtension::slotEnableAction( const char * name, bool enabled )
00435 {
00436
00437 ActionNumberMap::ConstIterator it = s_actionNumberMap->find( name );
00438 if ( it != s_actionNumberMap->end() )
00439 {
00440 d->m_actionStatus.setBit( it.data(), enabled );
00441
00442 }
00443 else
00444 kdWarning() << "BrowserExtension::slotEnableAction unknown action " << name << endl;
00445 }
00446
00447 bool BrowserExtension::isActionEnabled( const char * name ) const
00448 {
00449 int actionNumber = (*s_actionNumberMap)[ name ];
00450 return d->m_actionStatus[ actionNumber ];
00451 }
00452
00453
00454 BrowserExtension::ActionSlotMap BrowserExtension::actionSlotMap()
00455 {
00456 return *actionSlotMapPtr();
00457 }
00458
00459 BrowserExtension::ActionSlotMap * BrowserExtension::actionSlotMapPtr()
00460 {
00461 if (!s_actionSlotMap)
00462 createActionSlotMap();
00463 return s_actionSlotMap;
00464 }
00465
00466 void BrowserExtension::createActionSlotMap()
00467 {
00468 assert(!s_actionSlotMap);
00469 s_actionSlotMap = new ActionSlotMap;
00470 actionSlotMapsd.setObject( s_actionSlotMap );
00471
00472 s_actionSlotMap->insert( "cut", SLOT( cut() ) );
00473 s_actionSlotMap->insert( "copy", SLOT( copy() ) );
00474 s_actionSlotMap->insert( "paste", SLOT( paste() ) );
00475 s_actionSlotMap->insert( "rename", SLOT( rename() ) );
00476 s_actionSlotMap->insert( "trash", SLOT( trash() ) );
00477 s_actionSlotMap->insert( "del", SLOT( del() ) );
00478 s_actionSlotMap->insert( "shred", SLOT( shred() ) );
00479 s_actionSlotMap->insert( "properties", SLOT( properties() ) );
00480 s_actionSlotMap->insert( "editMimeType", SLOT( editMimeType() ) );
00481 s_actionSlotMap->insert( "print", SLOT( print() ) );
00482
00483
00484
00485
00486
00487
00488
00489 assert(!s_actionNumberMap);
00490 s_actionNumberMap = new ActionNumberMap;
00491 actionNumberMapsd.setObject( s_actionNumberMap );
00492 ActionSlotMap::ConstIterator it = s_actionSlotMap->begin();
00493 ActionSlotMap::ConstIterator itEnd = s_actionSlotMap->end();
00494 for ( int i=0 ; it != itEnd ; ++it, ++i )
00495 {
00496
00497 s_actionNumberMap->insert( it.key(), i );
00498 }
00499 }
00500
00501 BrowserExtension *BrowserExtension::childObject( QObject *obj )
00502 {
00503 if ( !obj || !obj->children() )
00504 return 0L;
00505
00506
00507
00508 const QObjectList *children = obj->children();
00509 QObjectListIt it( *children );
00510 for (; it.current(); ++it )
00511 if ( it.current()->inherits( "KParts::BrowserExtension" ) )
00512 return static_cast<KParts::BrowserExtension *>( it.current() );
00513
00514 return 0L;
00515 }
00516
00517 namespace KParts
00518 {
00519
00520 class BrowserHostExtension::BrowserHostExtensionPrivate
00521 {
00522 public:
00523 BrowserHostExtensionPrivate()
00524 {
00525 }
00526 ~BrowserHostExtensionPrivate()
00527 {
00528 }
00529
00530 KParts::ReadOnlyPart *m_part;
00531 };
00532
00533 };
00534
00535 BrowserHostExtension::BrowserHostExtension( KParts::ReadOnlyPart *parent, const char *name )
00536 : QObject( parent, name )
00537 {
00538 d = new BrowserHostExtensionPrivate;
00539 d->m_part = parent;
00540 }
00541
00542 BrowserHostExtension::~BrowserHostExtension()
00543 {
00544 delete d;
00545 }
00546
00547 QStringList BrowserHostExtension::frameNames() const
00548 {
00549 return QStringList();
00550 }
00551
00552 #if QT_VERSION < 300
00553 const QList<KParts::ReadOnlyPart> BrowserHostExtension::frames() const
00554 {
00555 return QList<KParts::ReadOnlyPart>();
00556 }
00557 #else
00558 const QPtrList<KParts::ReadOnlyPart> BrowserHostExtension::frames() const
00559 {
00560 return QPtrList<KParts::ReadOnlyPart>();
00561 }
00562 #endif
00563
00564 bool BrowserHostExtension::openURLInFrame( const KURL &, const KParts::URLArgs & )
00565 {
00566 return false;
00567 }
00568
00569 BrowserHostExtension *BrowserHostExtension::childObject( QObject *obj )
00570 {
00571 if ( !obj || !obj->children() )
00572 return 0L;
00573
00574
00575
00576 const QObjectList *children = obj->children();
00577 QObjectListIt it( *children );
00578 for (; it.current(); ++it )
00579 if ( it.current()->inherits( "KParts::BrowserHostExtension" ) )
00580 return static_cast<KParts::BrowserHostExtension *>( it.current() );
00581
00582 return 0L;
00583 }
00584
00585 void BrowserExtension::virtual_hook( int, void* )
00586 { }
00587
00588 void BrowserHostExtension::virtual_hook( int, void* )
00589 { }
00590
00591 LiveConnectExtension::LiveConnectExtension( KParts::ReadOnlyPart *parent, const char *name ) : QObject( parent, name) {}
00592
00593 bool LiveConnectExtension::get( const unsigned long, const QString &, Type &, unsigned long &, QString & ) {
00594 return false;
00595 }
00596
00597 bool LiveConnectExtension::put( const unsigned long, const QString &, const QString & ) {
00598 return false;
00599 }
00600
00601 bool LiveConnectExtension::call( const unsigned long, const QString &, const QStringList &, Type &, unsigned long &, QString & ) {
00602 return false;
00603 }
00604
00605 void LiveConnectExtension::unregister( const unsigned long ) {}
00606
00607 LiveConnectExtension *LiveConnectExtension::childObject( QObject *obj )
00608 {
00609 if ( !obj || !obj->children() )
00610 return 0L;
00611
00612
00613
00614 const QObjectList *children = obj->children();
00615 QObjectListIt it( *children );
00616 for (; it.current(); ++it )
00617 if ( it.current()->inherits( "KParts::LiveConnectExtension" ) )
00618 return static_cast<KParts::LiveConnectExtension *>( it.current() );
00619
00620 return 0L;
00621 }
00622
00623 #include "browserextension.moc"