kparts Library API Documentation

plugin.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 00021 #include <config.h> 00022 #include <kparts/plugin.h> 00023 #include <kparts/part.h> 00024 #include <kparts/componentfactory.h> 00025 00026 #include <assert.h> 00027 00028 #include <qfile.h> 00029 #include <qobjectlist.h> 00030 #include <qfileinfo.h> 00031 00032 #include <klibloader.h> 00033 #include <kinstance.h> 00034 #include <kstandarddirs.h> 00035 #include <kdebug.h> 00036 #include <kxmlguifactory.h> 00037 #include <klocale.h> 00038 #include <kconfig.h> 00039 #include <ksimpleconfig.h> 00040 00041 using namespace KParts; 00042 00043 class Plugin::PluginPrivate 00044 { 00045 public: 00046 PluginPrivate() : m_parentInstance( 0 ) {} 00047 00048 const KInstance *m_parentInstance; 00049 QString m_library; // filename of the library 00050 }; 00051 00052 Plugin::Plugin( QObject* parent, const char* name ) 00053 : QObject( parent, name ) 00054 { 00055 //kdDebug() << className() << endl; 00056 d = new PluginPrivate(); 00057 } 00058 00059 Plugin::~Plugin() 00060 { 00061 delete d; 00062 } 00063 00064 QString Plugin::xmlFile() const 00065 { 00066 QString path = KXMLGUIClient::xmlFile(); 00067 00068 if ( !d->m_parentInstance || ( path.length() > 0 && path[ 0 ] == '/' ) ) 00069 return path; 00070 00071 QString absPath = locate( "data", QString::fromLatin1( d->m_parentInstance->instanceName() ) + '/' + path ); 00072 assert( !absPath.isEmpty() ); 00073 return absPath; 00074 } 00075 00076 QString Plugin::localXMLFile() const 00077 { 00078 QString path = KXMLGUIClient::xmlFile(); 00079 00080 if ( !d->m_parentInstance || ( path.length() > 0 && path[ 0 ] == '/' ) ) 00081 return path; 00082 00083 QString absPath = locateLocal( "data", QString::fromLatin1( d->m_parentInstance->instanceName() ) + '/' + path ); 00084 assert( !absPath.isEmpty() ); 00085 return absPath; 00086 } 00087 00088 //static 00089 QValueList<Plugin::PluginInfo> Plugin::pluginInfos( const KInstance * instance ) 00090 { 00091 if ( !instance ) 00092 kdError(1000) << "No instance ???" << endl; 00093 00094 QValueList<PluginInfo> plugins; 00095 00096 QStringList pluginDocs = instance->dirs()->findAllResources( 00097 "data", instance->instanceName()+"/kpartplugins/*", true, false ); 00098 00099 QMap<QString,QStringList> sortedPlugins; 00100 00101 QStringList::ConstIterator pIt = pluginDocs.begin(); 00102 QStringList::ConstIterator pEnd = pluginDocs.end(); 00103 for (; pIt != pEnd; ++pIt ) 00104 { 00105 QFileInfo fInfo( *pIt ); 00106 if ( fInfo.extension() == QString::fromLatin1( ".desktop" ) ) 00107 continue; 00108 00109 QMap<QString,QStringList>::Iterator mapIt = sortedPlugins.find( fInfo.fileName() ); 00110 if ( mapIt == sortedPlugins.end() ) 00111 mapIt = sortedPlugins.insert( fInfo.fileName(), QStringList() ); 00112 00113 mapIt.data().append( *pIt ); 00114 } 00115 00116 QMap<QString,QStringList>::ConstIterator mapIt = sortedPlugins.begin(); 00117 QMap<QString,QStringList>::ConstIterator mapEnd = sortedPlugins.end(); 00118 for (; mapIt != mapEnd; ++mapIt ) 00119 { 00120 PluginInfo info; 00121 QString doc; 00122 info.m_absXMLFileName = KXMLGUIClient::findMostRecentXMLFile( mapIt.data(), doc ); 00123 if ( info.m_absXMLFileName.isEmpty() ) 00124 continue; 00125 00126 kdDebug( 1000 ) << "found Plugin : " << info.m_absXMLFileName << " !" << endl; 00127 info.m_relXMLFileName = "kpartplugins/"; 00128 info.m_relXMLFileName += mapIt.key(); 00129 00130 info.m_document.setContent( doc ); 00131 if ( info.m_document.documentElement().isNull() ) 00132 continue; 00133 00134 plugins.append( info ); 00135 } 00136 00137 return plugins; 00138 } 00139 00140 void Plugin::loadPlugins( QObject *parent, const KInstance *instance ) 00141 { 00142 loadPlugins( parent, pluginInfos( instance ), instance ); 00143 } 00144 00145 void Plugin::loadPlugins( QObject *parent, const QValueList<PluginInfo> &pluginInfos, const KInstance *instance ) 00146 { 00147 QValueList<PluginInfo>::ConstIterator pIt = pluginInfos.begin(); 00148 QValueList<PluginInfo>::ConstIterator pEnd = pluginInfos.end(); 00149 for (; pIt != pEnd; ++pIt ) 00150 { 00151 QString library = (*pIt).m_document.documentElement().attribute( "library" ); 00152 00153 if ( library.isEmpty() || hasPlugin( parent, library ) ) 00154 continue; 00155 00156 Plugin *plugin = loadPlugin( parent, QFile::encodeName(library) ); 00157 00158 if ( plugin ) 00159 { 00160 plugin->d->m_parentInstance = instance; 00161 plugin->setXMLFile( (*pIt).m_relXMLFileName, false, false ); 00162 plugin->setDOMDocument( (*pIt).m_document ); 00163 00164 } 00165 } 00166 00167 } 00168 00169 void Plugin::loadPlugins( QObject *parent, const QValueList<PluginInfo> &pluginInfos ) 00170 { 00171 loadPlugins(parent, pluginInfos, 0); 00172 } 00173 00174 // static 00175 Plugin* Plugin::loadPlugin( QObject * parent, const char* libname ) 00176 { 00177 Plugin* plugin = ComponentFactory::createInstanceFromLibrary<Plugin>( libname, parent, libname ); 00178 if ( !plugin ) 00179 return 0L; 00180 plugin->d->m_library = libname; 00181 return plugin; 00182 } 00183 00184 QPtrList<KParts::Plugin> Plugin::pluginObjects( QObject *parent ) 00185 { 00186 QPtrList<KParts::Plugin> objects; 00187 00188 if (!parent ) 00189 return objects; 00190 00191 QObjectList *plugins = parent->queryList( "KParts::Plugin", 0, false, false ); 00192 00193 QObjectListIt it( *plugins ); 00194 for ( ; it.current() ; ++it ) 00195 { 00196 objects.append( static_cast<Plugin *>( it.current() ) ); 00197 } 00198 00199 delete plugins; 00200 00201 return objects; 00202 } 00203 00204 bool Plugin::hasPlugin( QObject* parent, const QString& library ) 00205 { 00206 QObjectList *plugins = parent->queryList( "KParts::Plugin", 0, false, false ); 00207 QObjectListIt it( *plugins ); 00208 for ( ; it.current() ; ++it ) 00209 { 00210 if ( static_cast<Plugin *>( it.current() )->d->m_library == library ) 00211 { 00212 delete plugins; 00213 return true; 00214 } 00215 } 00216 delete plugins; 00217 return false; 00218 } 00219 00220 void Plugin::setInstance( KInstance *instance ) 00221 { 00222 KGlobal::locale()->insertCatalogue( instance->instanceName() ); 00223 KXMLGUIClient::setInstance( instance ); 00224 } 00225 00226 void Plugin::loadPlugins( QObject *parent, KXMLGUIClient* parentGUIClient, KInstance* instance, bool enableNewPluginsByDefault ) 00227 { 00228 KConfigGroup cfgGroup( instance->config(), "KParts Plugins" ); 00229 QValueList<PluginInfo> plugins = pluginInfos( instance ); 00230 QValueList<PluginInfo>::ConstIterator pIt = plugins.begin(); 00231 QValueList<PluginInfo>::ConstIterator pEnd = plugins.end(); 00232 for (; pIt != pEnd; ++pIt ) 00233 { 00234 QDomElement docElem = (*pIt).m_document.documentElement(); 00235 QString library = docElem.attribute( "library" ); 00236 00237 if ( library.isEmpty() ) 00238 continue; 00239 00240 // Check configuration 00241 QString name = docElem.attribute( "name" ); 00242 QString desktopfile = instance->dirs()->findResource( "data", 00243 QString( instance->instanceName() ) + "/kpartplugins/" + name + 00244 ".desktop" ); 00245 kdDebug( 1000 ) << "loadPlugins found desktopfile for " << instance->instanceName() << ": " << desktopfile << endl; 00246 if( ! desktopfile.isNull() ) 00247 { 00248 KSimpleConfig desktop( desktopfile ); 00249 desktop.setGroup( "X-KDE Plugin Info" ); 00250 enableNewPluginsByDefault = desktop.readBoolEntry( 00251 "EnabledByDefault", enableNewPluginsByDefault ); 00252 } 00253 bool pluginEnabled = cfgGroup.readBoolEntry( name + "Enabled", enableNewPluginsByDefault ); 00254 00255 // search through already present plugins 00256 QObjectList *pluginList = parent->queryList( "KParts::Plugin", 0, false, false ); 00257 QObjectListIt it( *pluginList ); 00258 bool pluginFound = false; 00259 for ( ; it.current() ; ++it ) 00260 { 00261 Plugin * plugin = static_cast<Plugin *>( it.current() ); 00262 if( plugin->d->m_library == library ) 00263 { 00264 // delete and unload disabled plugins 00265 if( !pluginEnabled ) 00266 { 00267 kdDebug( 1000 ) << "remove plugin " << name << endl; 00268 KXMLGUIFactory * factory = plugin->factory(); 00269 if( factory ) 00270 factory->removeClient( plugin ); 00271 delete plugin; 00272 } 00273 00274 pluginFound = true; 00275 break; 00276 } 00277 } 00278 delete pluginList; 00279 00280 // if the plugin is already loaded or if it's disabled in the 00281 // configuration do nothing 00282 if( pluginFound || !pluginEnabled ) 00283 continue; 00284 00285 kdDebug( 1000 ) << "load plugin " << name << endl; 00286 Plugin *plugin = loadPlugin( parent, QFile::encodeName(library) ); 00287 00288 if ( plugin ) 00289 { 00290 plugin->d->m_parentInstance = instance; 00291 plugin->setXMLFile( (*pIt).m_relXMLFileName, false, false ); 00292 plugin->setDOMDocument( (*pIt).m_document ); 00293 parentGUIClient->insertChildClient( plugin ); 00294 } 00295 } 00296 } 00297 00298 // vim:sw=4:et:sts=4 00299 00300 #include "plugin.moc"
KDE Logo
This file is part of the documentation for kparts Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Sep 29 09:41:28 2004 by doxygen 1.3.8 written by Dimitri van Heesch, © 1997-2003