plugin.cpp Source File (kparts Library)

| # kparts Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

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

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:51 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001