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 QMap<QString,QStringList>::Iterator mapIt = sortedPlugins.find( fInfo.fileName() ); 00107 if ( mapIt == sortedPlugins.end() ) 00108 mapIt = sortedPlugins.insert( fInfo.fileName(), QStringList() ); 00109 00110 mapIt.data().append( *pIt ); 00111 } 00112 00113 QMap<QString,QStringList>::ConstIterator mapIt = sortedPlugins.begin(); 00114 QMap<QString,QStringList>::ConstIterator mapEnd = sortedPlugins.end(); 00115 for (; mapIt != mapEnd; ++mapIt ) 00116 { 00117 PluginInfo info; 00118 QString doc; 00119 info.m_absXMLFileName = KXMLGUIClient::findMostRecentXMLFile( mapIt.data(), doc ); 00120 if ( !info.m_absXMLFileName.isEmpty() ) 00121 { 00122 kdDebug( 1000 ) << "found Plugin : " << info.m_absXMLFileName << " !" << endl; 00123 info.m_relXMLFileName = "kpartplugins/"; 00124 info.m_relXMLFileName += mapIt.key(); 00125 00126 info.m_document.setContent( doc ); 00127 if ( !info.m_document.documentElement().isNull() ) 00128 plugins.append( info ); 00129 } 00130 } 00131 00132 return plugins; 00133 } 00134 00135 void Plugin::loadPlugins( QObject *parent, const KInstance *instance ) 00136 { 00137 loadPlugins( parent, pluginInfos( instance ), instance ); 00138 } 00139 00140 void Plugin::loadPlugins( QObject *parent, const QValueList<PluginInfo> &pluginInfos, const KInstance *instance ) 00141 { 00142 QValueList<PluginInfo>::ConstIterator pIt = pluginInfos.begin(); 00143 QValueList<PluginInfo>::ConstIterator pEnd = pluginInfos.end(); 00144 for (; pIt != pEnd; ++pIt ) 00145 { 00146 QString library = (*pIt).m_document.documentElement().attribute( "library" ); 00147 00148 if ( library.isEmpty() || hasPlugin( parent, library ) ) 00149 continue; 00150 00151 Plugin *plugin = loadPlugin( parent, QFile::encodeName(library) ); 00152 00153 if ( plugin ) 00154 { 00155 plugin->d->m_parentInstance = instance; 00156 plugin->setXMLFile( (*pIt).m_relXMLFileName, false, false ); 00157 plugin->setDOMDocument( (*pIt).m_document ); 00158 00159 } 00160 } 00161 00162 } 00163 00164 void Plugin::loadPlugins( QObject *parent, const QValueList<PluginInfo> &pluginInfos ) 00165 { 00166 loadPlugins(parent, pluginInfos, 0); 00167 } 00168 00169 // static 00170 Plugin* Plugin::loadPlugin( QObject * parent, const char* libname ) 00171 { 00172 Plugin* plugin = ComponentFactory::createInstanceFromLibrary<Plugin>( libname, parent, libname ); 00173 if ( !plugin ) 00174 return 0L; 00175 plugin->d->m_library = libname; 00176 return plugin; 00177 } 00178 00179 QPtrList<KParts::Plugin> Plugin::pluginObjects( QObject *parent ) 00180 { 00181 QPtrList<KParts::Plugin> objects; 00182 00183 if (!parent ) 00184 return objects; 00185 00186 QObjectList *plugins = parent->queryList( "KParts::Plugin", 0, false, false ); 00187 00188 QObjectListIt it( *plugins ); 00189 for ( ; it.current() ; ++it ) 00190 { 00191 objects.append( static_cast<Plugin *>( it.current() ) ); 00192 } 00193 00194 delete plugins; 00195 00196 return objects; 00197 } 00198 00199 bool Plugin::hasPlugin( QObject* parent, const QString& library ) 00200 { 00201 QObjectList *plugins = parent->queryList( "KParts::Plugin", 0, false, false ); 00202 QObjectListIt it( *plugins ); 00203 for ( ; it.current() ; ++it ) 00204 { 00205 if ( static_cast<Plugin *>( it.current() )->d->m_library == library ) 00206 { 00207 delete plugins; 00208 return true; 00209 } 00210 } 00211 delete plugins; 00212 return false; 00213 } 00214 00215 void Plugin::setInstance( KInstance *instance ) 00216 { 00217 KGlobal::locale()->insertCatalogue( instance->instanceName() ); 00218 KXMLGUIClient::setInstance( instance ); 00219 } 00220 00221 void Plugin::loadPlugins( QObject *parent, KXMLGUIClient* parentGUIClient, KInstance* instance, bool enableNewPluginsByDefault ) 00222 { 00223 KConfigGroup cfgGroup( instance->config(), "KParts Plugins" ); 00224 QValueList<PluginInfo> plugins = pluginInfos( instance ); 00225 QValueList<PluginInfo>::ConstIterator pIt = plugins.begin(); 00226 QValueList<PluginInfo>::ConstIterator pEnd = plugins.end(); 00227 for (; pIt != pEnd; ++pIt ) 00228 { 00229 QDomElement docElem = (*pIt).m_document.documentElement(); 00230 QString library = docElem.attribute( "library" ); 00231 00232 if ( library.isEmpty() ) 00233 continue; 00234 00235 // Check configuration 00236 QString name = docElem.attribute( "name" ); 00237 QString desktopfile = instance->dirs()->findResource( "data", 00238 QString( instance->instanceName() ) + "/kpartplugins/" + name + 00239 ".desktop" ); 00240 kdDebug( 1000 ) << "loadPlugins found desktopfile for " << instance->instanceName() << ": " << desktopfile << endl; 00241 if( ! desktopfile.isNull() ) 00242 { 00243 KSimpleConfig desktop( desktopfile ); 00244 desktop.setGroup( "X-KDE Plugin Info" ); 00245 enableNewPluginsByDefault = desktop.readBoolEntry( 00246 "EnabledByDefault", enableNewPluginsByDefault ); 00247 } 00248 bool pluginEnabled = cfgGroup.readBoolEntry( name + "Enabled", enableNewPluginsByDefault ); 00249 00250 // search through already present plugins 00251 QObjectList *pluginList = parent->queryList( "KParts::Plugin", 0, false, false ); 00252 QObjectListIt it( *pluginList ); 00253 bool pluginFound = false; 00254 for ( ; it.current() ; ++it ) 00255 { 00256 Plugin * plugin = static_cast<Plugin *>( it.current() ); 00257 if( plugin->d->m_library == library ) 00258 { 00259 // delete and unload disabled plugins 00260 if( !pluginEnabled ) 00261 { 00262 kdDebug( 1000 ) << "remove plugin " << name << endl; 00263 KXMLGUIFactory * factory = plugin->factory(); 00264 if( factory ) 00265 factory->removeClient( plugin ); 00266 delete plugin; 00267 } 00268 00269 pluginFound = true; 00270 break; 00271 } 00272 } 00273 delete pluginList; 00274 00275 // if the plugin is already loaded or if it's disabled in the 00276 // configuration do nothing 00277 if( pluginFound || !pluginEnabled ) 00278 continue; 00279 00280 kdDebug( 1000 ) << "load plugin " << name << endl; 00281 Plugin *plugin = loadPlugin( parent, QFile::encodeName(library) ); 00282 00283 if ( plugin ) 00284 { 00285 plugin->d->m_parentInstance = instance; 00286 plugin->setXMLFile( (*pIt).m_relXMLFileName, false, false ); 00287 plugin->setDOMDocument( (*pIt).m_document ); 00288 parentGUIClient->insertChildClient( plugin ); 00289 } 00290 } 00291 } 00292 00293 // vim:sw=4:et:sts=4 00294 00295 #include "plugin.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 Sat Jun 12 15:09:07 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003