kparts Library API Documentation

browserextension.cpp

00001  /* This file is part of the KDE project
00002    Copyright (C) 1999 Simon Hausmann <hausmann@kde.org>
00003              (C) 1999 David Faure <faure@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018    Boston, MA 02111-1307, USA.
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 //  d = new OpenURLEventPrivate();
00049 }
00050 
00051 OpenURLEvent::~OpenURLEvent()
00052 {
00053 //  delete d;
00054 }
00055 
00056 namespace KParts
00057 {
00058 
00059 struct URLArgsPrivate
00060 {
00061     URLArgsPrivate() {
00062       doPost = false;
00063       redirectedRequest = false;
00064       lockHistory = false;
00065       newTab = false;
00066     }
00067     QString contentType; // for POST
00068     QMap<QString, QString> metaData;
00069     bool doPost;
00070     bool redirectedRequest;
00071     bool lockHistory;
00072     bool newTab;
00073 };
00074 
00075 }
00076 
00077 URLArgs::URLArgs()
00078 {
00079   reload = false;
00080   xOffset = 0;
00081   yOffset = 0;
00082   trustedSource = false;
00083   d = 0L; // Let's build it on demand for now
00084 }
00085 
00086 
00087 URLArgs::URLArgs( bool _reload, int _xOffset, int _yOffset, const QString &_serviceType )
00088 {
00089   reload = _reload;
00090   xOffset = _xOffset;
00091   yOffset = _yOffset;
00092   serviceType = _serviceType;
00093   d = 0L; // Let's build it on demand for now
00094 }
00095 
00096 URLArgs::URLArgs( const URLArgs &args )
00097 {
00098   d = 0L;
00099   (*this) = args;
00100 }
00101 
00102 URLArgs &URLArgs::operator=(const URLArgs &args)
00103 {
00104   if (this == &args) return *this;
00105 
00106   delete d; d= 0;
00107 
00108   reload = args.reload;
00109   xOffset = args.xOffset;
00110   yOffset = args.yOffset;
00111   serviceType = args.serviceType;
00112   postData = args.postData;
00113   frameName = args.frameName;
00114   docState = args.docState;
00115   trustedSource = args.trustedSource;
00116 
00117   if ( args.d )
00118      d = new URLArgsPrivate( * args.d );
00119 
00120   return *this;
00121 }
00122 
00123 URLArgs::~URLArgs()
00124 {
00125   delete d; d = 0;
00126 }
00127 
00128 void URLArgs::setContentType( const QString & contentType )
00129 {
00130   if (!d)
00131     d = new URLArgsPrivate;
00132   d->contentType = contentType;
00133 }
00134 
00135 void URLArgs::setRedirectedRequest( bool redirected )
00136 {
00137   if (!d)
00138      d = new URLArgsPrivate;
00139   d->redirectedRequest = redirected;
00140 }
00141 
00142 bool URLArgs::redirectedRequest () const
00143 {
00144   return d ? d->redirectedRequest : false;
00145 }
00146 
00147 QString URLArgs::contentType() const
00148 {
00149   return d ? d->contentType : QString::null;
00150 }
00151 
00152 QMap<QString, QString> &URLArgs::metaData()
00153 {
00154   if (!d)
00155      d = new URLArgsPrivate;
00156   return d->metaData;
00157 }
00158 
00159 void URLArgs::setDoPost( bool enable )
00160 {
00161     if ( !d )
00162         d = new URLArgsPrivate;
00163     d->doPost = enable;
00164 }
00165 
00166 bool URLArgs::doPost() const
00167 {
00168     return d ? d->doPost : false;
00169 }
00170 
00171 void URLArgs::setLockHistory( bool lock )
00172 {
00173   if (!d)
00174      d = new URLArgsPrivate;
00175   d->lockHistory = lock;
00176 }
00177 
00178 bool URLArgs::lockHistory() const
00179 {
00180     return d ? d->lockHistory : false;
00181 }
00182 
00183 void URLArgs::setNewTab( bool newTab )
00184 {
00185   if (!d)
00186      d = new URLArgsPrivate;
00187   d->newTab = newTab;
00188 }
00189 
00190 bool URLArgs::newTab() const
00191 {
00192     return d ? d->newTab : false;
00193 }
00194 
00195 namespace KParts
00196 {
00197 
00198 struct WindowArgsPrivate
00199 {
00200 };
00201 
00202 }
00203 
00204 WindowArgs::WindowArgs()
00205 {
00206     x = y = width = height = -1;
00207     fullscreen = false;
00208     menuBarVisible = true;
00209     toolBarsVisible = true;
00210     statusBarVisible = true;
00211     resizable = true;
00212     lowerWindow = false;
00213     d = 0;
00214 }
00215 
00216 WindowArgs::WindowArgs( const WindowArgs &args )
00217 {
00218     d = 0;
00219     (*this) = args;
00220 }
00221 
00222 WindowArgs &WindowArgs::operator=( const WindowArgs &args )
00223 {
00224     if ( this == &args ) return *this;
00225 
00226     delete d; d = 0;
00227 
00228     x = args.x;
00229     y = args.y;
00230     width = args.width;
00231     height = args.height;
00232     fullscreen = args.fullscreen;
00233     menuBarVisible = args.menuBarVisible;
00234     toolBarsVisible = args.toolBarsVisible;
00235     statusBarVisible = args.statusBarVisible;
00236     resizable = args.resizable;
00237     lowerWindow = args.lowerWindow;
00238 
00239     /*
00240     if ( args.d )
00241     {
00242       [ ... ]
00243     }
00244     */
00245 
00246     return *this;
00247 }
00248 
00249 WindowArgs::WindowArgs( const QRect &_geometry, bool _fullscreen, bool _menuBarVisible,
00250                         bool _toolBarsVisible, bool _statusBarVisible, bool _resizable )
00251 {
00252     d = 0;
00253     x = _geometry.x();
00254     y = _geometry.y();
00255     width = _geometry.width();
00256     height = _geometry.height();
00257     fullscreen = _fullscreen;
00258     menuBarVisible = _menuBarVisible;
00259     toolBarsVisible = _toolBarsVisible;
00260     statusBarVisible = _statusBarVisible;
00261     resizable = _resizable;
00262     lowerWindow = false;
00263 }
00264 
00265 WindowArgs::WindowArgs( int _x, int _y, int _width, int _height, bool _fullscreen,
00266                         bool _menuBarVisible, bool _toolBarsVisible,
00267                         bool _statusBarVisible, bool _resizable )
00268 {
00269     d = 0;
00270     x = _x;
00271     y = _y;
00272     width = _width;
00273     height = _height;
00274     fullscreen = _fullscreen;
00275     menuBarVisible = _menuBarVisible;
00276     toolBarsVisible = _toolBarsVisible;
00277     statusBarVisible = _statusBarVisible;
00278     resizable = _resizable;
00279     lowerWindow = false;
00280 }
00281 
00282 namespace KParts
00283 {
00284 
00285 // Internal class, use to store the status of the actions
00286 class KBitArray
00287 {
00288 public:
00289     int val;
00290     KBitArray() { val = 0; }
00291     bool operator [](int index) { return (val & (1 << index)) ? true : false; }
00292     void setBit(int index, bool value) {
00293         if (value) val = val | (1 << index);
00294         else val = val & ~(1 << index);
00295     }
00296 };
00297 
00298 class BrowserExtensionPrivate
00299 {
00300 public:
00301   BrowserExtensionPrivate()
00302   {
00303       m_browserInterface = 0;
00304   }
00305   ~BrowserExtensionPrivate()
00306   {
00307   }
00308 
00309   struct DelayedRequest {
00310     KURL m_delayedURL;
00311     KParts::URLArgs m_delayedArgs;
00312   };
00313   QValueList<DelayedRequest> m_requests;
00314   bool m_urlDropHandlingEnabled;
00315   KBitArray m_actionStatus;
00316   BrowserInterface *m_browserInterface;
00317 };
00318 
00319 }
00320 
00321 BrowserExtension::ActionSlotMap * BrowserExtension::s_actionSlotMap = 0L;
00322 KStaticDeleter<BrowserExtension::ActionSlotMap> actionSlotMapsd;
00323 BrowserExtension::ActionNumberMap * BrowserExtension::s_actionNumberMap = 0L;
00324 KStaticDeleter<BrowserExtension::ActionNumberMap> actionNumberMapsd;
00325 
00326 BrowserExtension::BrowserExtension( KParts::ReadOnlyPart *parent,
00327                                     const char *name )
00328 : QObject( parent, name), m_part( parent )
00329 {
00330   //kdDebug() << "BrowserExtension::BrowserExtension() " << this << endl;
00331   d = new BrowserExtensionPrivate;
00332   d->m_urlDropHandlingEnabled = false;
00333 
00334   if ( !s_actionSlotMap )
00335       // Create the action-slot map
00336       createActionSlotMap();
00337 
00338   // Set the initial status of the actions depending on whether
00339   // they're supported or not
00340   ActionSlotMap::ConstIterator it = s_actionSlotMap->begin();
00341   ActionSlotMap::ConstIterator itEnd = s_actionSlotMap->end();
00342   QStrList slotNames = metaObject()->slotNames();
00343   for ( int i=0 ; it != itEnd ; ++it, ++i )
00344   {
00345       // Does the extension have a slot with the name of this action ?
00346       d->m_actionStatus.setBit( i, slotNames.contains( it.key()+"()" ) );
00347   }
00348 
00349   connect( m_part, SIGNAL( completed() ),
00350            this, SLOT( slotCompleted() ) );
00351   connect( this, SIGNAL( openURLRequest( const KURL &, const KParts::URLArgs & ) ),
00352            this, SLOT( slotOpenURLRequest( const KURL &, const KParts::URLArgs & ) ) );
00353   connect( this, SIGNAL( enableAction( const char *, bool ) ),
00354            this, SLOT( slotEnableAction( const char *, bool ) ) );
00355 }
00356 
00357 BrowserExtension::~BrowserExtension()
00358 {
00359   //kdDebug() << "BrowserExtension::~BrowserExtension() " << this << endl;
00360   delete d;
00361 }
00362 
00363 void BrowserExtension::setURLArgs( const URLArgs &args )
00364 {
00365   m_args = args;
00366 }
00367 
00368 URLArgs BrowserExtension::urlArgs() const
00369 {
00370   return m_args;
00371 }
00372 
00373 int BrowserExtension::xOffset()
00374 {
00375   return 0;
00376 }
00377 
00378 int BrowserExtension::yOffset()
00379 {
00380   return 0;
00381 }
00382 
00383 void BrowserExtension::saveState( QDataStream &stream )
00384 {
00385   stream << m_part->url() << (Q_INT32)xOffset() << (Q_INT32)yOffset();
00386 }
00387 
00388 void BrowserExtension::restoreState( QDataStream &stream )
00389 {
00390   KURL u;
00391   Q_INT32 xOfs, yOfs;
00392   stream >> u >> xOfs >> yOfs;
00393 
00394   URLArgs args( urlArgs() );
00395   args.xOffset = xOfs;
00396   args.yOffset = yOfs;
00397 
00398   setURLArgs( args );
00399 
00400   m_part->openURL( u );
00401 }
00402 
00403 bool BrowserExtension::isURLDropHandlingEnabled() const
00404 {
00405     return d->m_urlDropHandlingEnabled;
00406 }
00407 
00408 void BrowserExtension::setURLDropHandlingEnabled( bool enable )
00409 {
00410     d->m_urlDropHandlingEnabled = enable;
00411 }
00412 
00413 void BrowserExtension::slotCompleted()
00414 {
00415   //empty the argument stuff, to avoid bogus/invalid values when opening a new url
00416   setURLArgs( URLArgs() );
00417 }
00418 
00419 void BrowserExtension::slotOpenURLRequest( const KURL &url, const KParts::URLArgs &args )
00420 {
00421     //kdDebug() << this << " BrowserExtension::slotOpenURLRequest(): url=" << url.url() << endl;
00422     BrowserExtensionPrivate::DelayedRequest req;
00423     req.m_delayedURL = url;
00424     req.m_delayedArgs = args;
00425     d->m_requests.append( req );
00426     QTimer::singleShot( 0, this, SLOT( slotEmitOpenURLRequestDelayed() ) );
00427 }
00428 
00429 void BrowserExtension::slotEmitOpenURLRequestDelayed()
00430 {
00431     if (d->m_requests.isEmpty()) return;
00432     BrowserExtensionPrivate::DelayedRequest req = d->m_requests.front();
00433     d->m_requests.pop_front();
00434     emit openURLRequestDelayed( req.m_delayedURL, req.m_delayedArgs );
00435     // tricky: do not do anything here! (no access to member variables, etc.)
00436 }
00437 
00438 void BrowserExtension::setBrowserInterface( BrowserInterface *impl )
00439 {
00440     d->m_browserInterface = impl;
00441 }
00442 
00443 BrowserInterface *BrowserExtension::browserInterface() const
00444 {
00445     return d->m_browserInterface;
00446 }
00447 
00448 void BrowserExtension::slotEnableAction( const char * name, bool enabled )
00449 {
00450     //kdDebug() << "BrowserExtension::slotEnableAction " << name << " " << enabled << endl;
00451     ActionNumberMap::ConstIterator it = s_actionNumberMap->find( name );
00452     if ( it != s_actionNumberMap->end() )
00453     {
00454         d->m_actionStatus.setBit( it.data(), enabled );
00455         //kdDebug() << "BrowserExtension::slotEnableAction setting bit " << it.data() << " to " << enabled << endl;
00456     }
00457     else
00458         kdWarning() << "BrowserExtension::slotEnableAction unknown action " << name << endl;
00459 }
00460 
00461 bool BrowserExtension::isActionEnabled( const char * name ) const
00462 {
00463     int actionNumber = (*s_actionNumberMap)[ name ];
00464     return d->m_actionStatus[ actionNumber ];
00465 }
00466 
00467 // for compatibility
00468 BrowserExtension::ActionSlotMap BrowserExtension::actionSlotMap()
00469 {
00470     return *actionSlotMapPtr();
00471 }
00472 
00473 BrowserExtension::ActionSlotMap * BrowserExtension::actionSlotMapPtr()
00474 {
00475     if (!s_actionSlotMap)
00476         createActionSlotMap();
00477     return s_actionSlotMap;
00478 }
00479 
00480 void BrowserExtension::createActionSlotMap()
00481 {
00482     assert(!s_actionSlotMap);
00483     s_actionSlotMap = new ActionSlotMap;
00484     actionSlotMapsd.setObject( s_actionSlotMap );
00485 
00486     s_actionSlotMap->insert( "cut", SLOT( cut() ) );
00487     s_actionSlotMap->insert( "copy", SLOT( copy() ) );
00488     s_actionSlotMap->insert( "paste", SLOT( paste() ) );
00489     s_actionSlotMap->insert( "rename", SLOT( rename() ) );
00490     s_actionSlotMap->insert( "trash", SLOT( trash() ) );
00491     s_actionSlotMap->insert( "del", SLOT( del() ) );
00492     s_actionSlotMap->insert( "shred", SLOT( shred() ) );
00493     s_actionSlotMap->insert( "properties", SLOT( properties() ) );
00494     s_actionSlotMap->insert( "editMimeType", SLOT( editMimeType() ) );
00495     s_actionSlotMap->insert( "print", SLOT( print() ) );
00496     // Tricky. Those aren't actions in fact, but simply methods that a browserextension
00497     // can have or not. No need to return them here.
00498     //s_actionSlotMap->insert( "reparseConfiguration", SLOT( reparseConfiguration() ) );
00499     //s_actionSlotMap->insert( "refreshMimeTypes", SLOT( refreshMimeTypes() ) );
00500     // nothing for setSaveViewPropertiesLocally either
00501 
00502     // Create the action-number map
00503     assert(!s_actionNumberMap);
00504     s_actionNumberMap = new ActionNumberMap;
00505     actionNumberMapsd.setObject( s_actionNumberMap );
00506     ActionSlotMap::ConstIterator it = s_actionSlotMap->begin();
00507     ActionSlotMap::ConstIterator itEnd = s_actionSlotMap->end();
00508     for ( int i=0 ; it != itEnd ; ++it, ++i )
00509     {
00510         //kdDebug(1202) << " action " << it.key() << " number " << i << endl;
00511         s_actionNumberMap->insert( it.key(), i );
00512     }
00513 }
00514 
00515 BrowserExtension *BrowserExtension::childObject( QObject *obj )
00516 {
00517     if ( !obj || !obj->children() )
00518         return 0L;
00519 
00520     // we try to do it on our own, in hope that we are faster than
00521     // queryList, which looks kind of big :-)
00522     const QObjectList *children = obj->children();
00523     QObjectListIt it( *children );
00524     for (; it.current(); ++it )
00525         if ( it.current()->inherits( "KParts::BrowserExtension" ) )
00526             return static_cast<KParts::BrowserExtension *>( it.current() );
00527 
00528     return 0L;
00529 }
00530 
00531 namespace KParts
00532 {
00533 
00534 class BrowserHostExtension::BrowserHostExtensionPrivate
00535 {
00536 public:
00537   BrowserHostExtensionPrivate()
00538   {
00539   }
00540   ~BrowserHostExtensionPrivate()
00541   {
00542   }
00543 
00544   KParts::ReadOnlyPart *m_part;
00545 };
00546 
00547 }
00548 
00549 BrowserHostExtension::BrowserHostExtension( KParts::ReadOnlyPart *parent, const char *name )
00550  : QObject( parent, name )
00551 {
00552   d = new BrowserHostExtensionPrivate;
00553   d->m_part = parent;
00554 }
00555 
00556 BrowserHostExtension::~BrowserHostExtension()
00557 {
00558   delete d;
00559 }
00560 
00561 QStringList BrowserHostExtension::frameNames() const
00562 {
00563   return QStringList();
00564 }
00565 
00566 #if QT_VERSION < 300
00567 const QList<KParts::ReadOnlyPart> BrowserHostExtension::frames() const
00568 {
00569   return QList<KParts::ReadOnlyPart>();
00570 }
00571 #else
00572 const QPtrList<KParts::ReadOnlyPart> BrowserHostExtension::frames() const
00573 {
00574   return QPtrList<KParts::ReadOnlyPart>();
00575 }
00576 #endif
00577 
00578 bool BrowserHostExtension::openURLInFrame( const KURL &, const KParts::URLArgs & )
00579 {
00580   return false;
00581 }
00582 
00583 BrowserHostExtension *BrowserHostExtension::childObject( QObject *obj )
00584 {
00585     if ( !obj || !obj->children() )
00586         return 0L;
00587 
00588     // we try to do it on our own, in hope that we are faster than
00589     // queryList, which looks kind of big :-)
00590     const QObjectList *children = obj->children();
00591     QObjectListIt it( *children );
00592     for (; it.current(); ++it )
00593         if ( it.current()->inherits( "KParts::BrowserHostExtension" ) )
00594             return static_cast<KParts::BrowserHostExtension *>( it.current() );
00595 
00596     return 0L;
00597 }
00598 
00599 void BrowserExtension::virtual_hook( int, void* )
00600 { /*BASE::virtual_hook( id, data );*/ }
00601 
00602 void BrowserHostExtension::virtual_hook( int, void* )
00603 { /*BASE::virtual_hook( id, data );*/ }
00604 
00605 LiveConnectExtension::LiveConnectExtension( KParts::ReadOnlyPart *parent, const char *name ) : QObject( parent, name) {}
00606 
00607 bool LiveConnectExtension::get( const unsigned long, const QString &, Type &, unsigned long &, QString & ) {
00608     return false;
00609 }
00610 
00611 bool LiveConnectExtension::put( const unsigned long, const QString &, const QString & ) {
00612       return false;
00613 }
00614 
00615 bool LiveConnectExtension::call( const unsigned long, const QString &, const QStringList &, Type &, unsigned long &, QString & ) { 
00616       return false; 
00617 }
00618 
00619 void LiveConnectExtension::unregister( const unsigned long ) {}
00620 
00621 LiveConnectExtension *LiveConnectExtension::childObject( QObject *obj )
00622 {
00623     if ( !obj || !obj->children() )
00624         return 0L;
00625 
00626     // we try to do it on our own, in hope that we are faster than
00627     // queryList, which looks kind of big :-)
00628     const QObjectList *children = obj->children();
00629     QObjectListIt it( *children );
00630     for (; it.current(); ++it )
00631         if ( it.current()->inherits( "KParts::LiveConnectExtension" ) )
00632             return static_cast<KParts::LiveConnectExtension *>( it.current() );
00633 
00634     return 0L;
00635 }
00636 
00637 #include "browserextension.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:26:50 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001