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