• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KParts

plugin.cpp

Go to the documentation of this file.
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., 51 Franklin Street, Fifth Floor,
00018    Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include <kparts/plugin.h>
00022 #include <kparts/part.h>
00023 #include <kparts/componentfactory.h>
00024 
00025 #include <assert.h>
00026 
00027 #include <QtCore/QFile>
00028 #include <QtCore/QObject>
00029 #include <QtCore/QFileInfo>
00030 
00031 #include <klibloader.h>
00032 #include <kcomponentdata.h>
00033 #include <kstandarddirs.h>
00034 #include <kdebug.h>
00035 #include <kxmlguifactory.h>
00036 #include <klocale.h>
00037 #include <kdesktopfile.h>
00038 #include <kconfiggroup.h>
00039 
00040 using namespace KParts;
00041 
00042 class Plugin::PluginPrivate
00043 {
00044 public:
00045     KComponentData m_parentInstance;
00046     QString m_library; // filename of the library
00047 };
00048 
00049 Plugin::Plugin( QObject* parent )
00050     : QObject( parent ),d(new PluginPrivate())
00051 {
00052   //kDebug() << className();
00053 }
00054 
00055 Plugin::~Plugin()
00056 {
00057     delete d;
00058 }
00059 
00060 QString Plugin::xmlFile() const
00061 {
00062     QString path = KXMLGUIClient::xmlFile();
00063 
00064     if ( !d->m_parentInstance.isValid() || ( path.length() > 0 && path[ 0 ] == '/' ) )
00065         return path;
00066 
00067     QString absPath = KStandardDirs::locate( "data", d->m_parentInstance.componentName() + '/' + path );
00068     assert( !absPath.isEmpty() );
00069     return absPath;
00070 }
00071 
00072 QString Plugin::localXMLFile() const
00073 {
00074     QString path = KXMLGUIClient::xmlFile();
00075 
00076     if ( !d->m_parentInstance.isValid() || ( path.length() > 0 && path[ 0 ] == '/' ) )
00077         return path;
00078 
00079     QString absPath = KStandardDirs::locateLocal( "data", d->m_parentInstance.componentName() + '/' + path );
00080     assert( !absPath.isEmpty() );
00081     return absPath;
00082 }
00083 
00084 //static
00085 QList<Plugin::PluginInfo> Plugin::pluginInfos(const KComponentData &componentData)
00086 {
00087   if (!componentData.isValid())
00088     kError(1000) << "No componentData ???" << endl;
00089 
00090   QList<PluginInfo> plugins;
00091 
00092   // KDE4: change * into *.rc and remove test for .desktop from the for loop below.
00093   const QStringList pluginDocs = componentData.dirs()->findAllResources(
00094     "data", componentData.componentName()+"/kpartplugins/*", KStandardDirs::Recursive );
00095 
00096   QMap<QString,QStringList> sortedPlugins;
00097 
00098   QStringList::ConstIterator pIt = pluginDocs.begin();
00099   QStringList::ConstIterator pEnd = pluginDocs.end();
00100   for (; pIt != pEnd; ++pIt )
00101   {
00102       QFileInfo fInfo( *pIt );
00103       if ( fInfo.completeSuffix() == QLatin1String( "desktop" ) )
00104           continue;
00105 
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.value().append( *pIt );
00111   }
00112 
00113   QMap<QString,QStringList>::ConstIterator mapIt = sortedPlugins.constBegin();
00114   QMap<QString,QStringList>::ConstIterator mapEnd = sortedPlugins.constEnd();
00115   for (; mapIt != mapEnd; ++mapIt )
00116   {
00117       PluginInfo info;
00118       QString doc;
00119       info.m_absXMLFileName = KXMLGUIClient::findMostRecentXMLFile( mapIt.value(), doc );
00120       if ( info.m_absXMLFileName.isEmpty() )
00121           continue;
00122 
00123       kDebug( 1000 ) << "found KParts Plugin : " << info.m_absXMLFileName;
00124       info.m_relXMLFileName = "kpartplugins/";
00125       info.m_relXMLFileName += mapIt.key();
00126 
00127       info.m_document.setContent( doc );
00128       if ( info.m_document.documentElement().isNull() )
00129           continue;
00130 
00131       plugins.append( info );
00132   }
00133 
00134   return plugins;
00135 }
00136 
00137 void Plugin::loadPlugins(QObject *parent, const KComponentData &componentData)
00138 {
00139   loadPlugins( parent, pluginInfos( componentData ), componentData );
00140 }
00141 
00142 void Plugin::loadPlugins(QObject *parent, const QList<PluginInfo> &pluginInfos, const KComponentData &componentData)
00143 {
00144    QList<PluginInfo>::ConstIterator pIt = pluginInfos.begin();
00145    QList<PluginInfo>::ConstIterator pEnd = pluginInfos.end();
00146    for (; pIt != pEnd; ++pIt )
00147    {
00148      QString library = (*pIt).m_document.documentElement().attribute( "library" );
00149 
00150      if ( library.isEmpty() || hasPlugin( parent, library ) )
00151        continue;
00152 
00153      Plugin *plugin = loadPlugin( parent, QFile::encodeName(library) );
00154 
00155      if ( plugin )
00156      {
00157        plugin->d->m_parentInstance = componentData;
00158        plugin->setXMLFile( (*pIt).m_relXMLFileName, false, false );
00159        plugin->setDOMDocument( (*pIt).m_document );
00160 
00161      }
00162    }
00163 
00164 }
00165 
00166 void Plugin::loadPlugins( QObject *parent, const QList<PluginInfo> &pluginInfos )
00167 {
00168    loadPlugins(parent, pluginInfos, KComponentData());
00169 }
00170 
00171 // static
00172 Plugin* Plugin::loadPlugin( QObject * parent, const char* libname )
00173 {
00174     Plugin* plugin = KLibLoader::createInstance<Plugin>( libname, parent );
00175     if ( !plugin )
00176         return 0;
00177     plugin->d->m_library = libname;
00178     return plugin;
00179 }
00180 
00181 QList<KParts::Plugin *> Plugin::pluginObjects( QObject *parent )
00182 {
00183   QList<KParts::Plugin *> objects;
00184 
00185   if (!parent )
00186     return objects;
00187 
00188   const QObjectList plugins = parent->children();
00189 
00190   QObjectList::ConstIterator it = plugins.begin();
00191   for ( ; it != plugins.end() ; ++it )
00192   {
00193     Plugin * plugin = qobject_cast<Plugin *>( *it );
00194     if ( plugin )
00195         objects.append( plugin );
00196   }
00197 
00198   return objects;
00199 }
00200 
00201 bool Plugin::hasPlugin( QObject* parent, const QString& library )
00202 {
00203   const QObjectList plugins = parent->children();
00204 
00205   QObjectList::ConstIterator it = plugins.begin();
00206   for ( ; it != plugins.end() ; ++it )
00207   {
00208       Plugin * plugin = qobject_cast<Plugin *>( *it );
00209       if ( plugin && plugin->d->m_library == library )
00210       {
00211           return true;
00212       }
00213   }
00214   return false;
00215 }
00216 
00217 void Plugin::setComponentData(const KComponentData &componentData)
00218 {
00219     KGlobal::locale()->insertCatalog(componentData.catalogName());
00220     KXMLGUIClient::setComponentData(componentData);
00221 }
00222 
00223 void Plugin::loadPlugins(QObject *parent, KXMLGUIClient* parentGUIClient,
00224         const KComponentData &componentData, bool enableNewPluginsByDefault,
00225         int interfaceVersionRequired)
00226 {
00227     KConfigGroup cfgGroup( componentData.config(), "KParts Plugins" );
00228     const QList<PluginInfo> plugins = pluginInfos( componentData );
00229     QList<PluginInfo>::ConstIterator pIt = plugins.begin();
00230     const QList<PluginInfo>::ConstIterator pEnd = plugins.end();
00231     for (; pIt != pEnd; ++pIt )
00232     {
00233         QDomElement docElem = (*pIt).m_document.documentElement();
00234         QString library = docElem.attribute( "library" );
00235 
00236         if ( library.isEmpty() )
00237             continue;
00238 
00239         // Check configuration
00240         const QString name = docElem.attribute( "name" );
00241 
00242         bool pluginEnabled = enableNewPluginsByDefault;
00243         if ( cfgGroup.hasKey( name + "Enabled" ) )
00244         {
00245             pluginEnabled = cfgGroup.readEntry( name + "Enabled" , false );
00246         }
00247         else
00248         { // no user-setting, load plugin default setting
00249             QString relPath = QString( componentData.componentName() ) + '/' + (*pIt).m_relXMLFileName;
00250             relPath.truncate( relPath.lastIndexOf( '.' ) ); // remove extension
00251             relPath += ".desktop";
00252             //kDebug(1000) << "looking for " << relPath;
00253             const QString desktopfile = componentData.dirs()->findResource( "data", relPath );
00254             if( !desktopfile.isEmpty() )
00255             {
00256                 //kDebug(1000) << "loadPlugins found desktop file for " << name << ": " << desktopfile;
00257                 KDesktopFile _desktop( desktopfile );
00258                 const KConfigGroup desktop = _desktop.desktopGroup();
00259                 pluginEnabled = desktop.readEntry( "X-KDE-PluginInfo-EnabledByDefault",
00260                                                    enableNewPluginsByDefault );
00261                 if ( interfaceVersionRequired != 0 )
00262                 {
00263                     const int version = desktop.readEntry( "X-KDE-InterfaceVersion", 1 );
00264                     if ( version != interfaceVersionRequired )
00265                     {
00266                         kDebug(1000) << "Discarding plugin " << name << ", interface version " << version << ", expected " << interfaceVersionRequired;
00267                         pluginEnabled = false;
00268                     }
00269                 }
00270             }
00271             else
00272             {
00273                 //kDebug(1000) << "loadPlugins no desktop file found in " << relPath;
00274             }
00275         }
00276 
00277         // search through already present plugins
00278         const QObjectList pluginList = parent->children();
00279 
00280         bool pluginFound = false;
00281         for ( QObjectList::ConstIterator it = pluginList.begin(); it != pluginList.end() ; ++it )
00282         {
00283             Plugin * plugin = qobject_cast<Plugin *>( *it );
00284             if( plugin && plugin->d->m_library == library )
00285             {
00286                 // delete and unload disabled plugins
00287                 if( !pluginEnabled )
00288                 {
00289                     kDebug( 1000 ) << "remove plugin " << name;
00290                     KXMLGUIFactory * factory = plugin->factory();
00291                     if( factory )
00292                         factory->removeClient( plugin );
00293                     delete plugin;
00294                 }
00295 
00296                 pluginFound = true;
00297                 break;
00298             }
00299         }
00300 
00301         // if the plugin is already loaded or if it's disabled in the
00302         // configuration do nothing
00303         if( pluginFound || !pluginEnabled )
00304             continue;
00305 
00306         kDebug( 1000 ) << "load plugin " << name;
00307         Plugin *plugin = loadPlugin( parent, QFile::encodeName(library) );
00308 
00309         if ( plugin )
00310         {
00311             plugin->d->m_parentInstance = componentData;
00312             plugin->setXMLFile( (*pIt).m_relXMLFileName, false, false );
00313             plugin->setDOMDocument( (*pIt).m_document );
00314             parentGUIClient->insertChildClient( plugin );
00315         }
00316     }
00317 }
00318 
00319 // vim:sw=4:et:sts=4
00320 
00321 #include "plugin.moc"

KParts

Skip menu "KParts"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal