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

KParts

  • sources
  • kde-4.12
  • kdelibs
  • kparts
plugin.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE project
2  Copyright (C) 1999 Simon Hausmann <hausmann@kde.org>
3  (C) 1999 David Faure <faure@kde.org>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include <kparts/plugin.h>
22 #include <kparts/part.h>
23 #include <kparts/componentfactory.h>
24 
25 #include <assert.h>
26 
27 #include <QtCore/QFile>
28 #include <QtCore/QObject>
29 #include <QtCore/QFileInfo>
30 
31 #include <kcomponentdata.h>
32 #include <kstandarddirs.h>
33 #include <kdebug.h>
34 #include <kxmlguifactory.h>
35 #include <klocale.h>
36 #include <kdesktopfile.h>
37 #include <kconfiggroup.h>
38 
39 using namespace KParts;
40 
41 class Plugin::PluginPrivate
42 {
43 public:
44  KComponentData m_parentInstance;
45  QString m_library; // filename of the library
46 };
47 
48 Plugin::Plugin( QObject* parent )
49  : QObject( parent ),d(new PluginPrivate())
50 {
51  //kDebug() << className();
52 }
53 
54 Plugin::~Plugin()
55 {
56  delete d;
57 }
58 
59 QString Plugin::xmlFile() const
60 {
61  QString path = KXMLGUIClient::xmlFile();
62 
63  if ( !d->m_parentInstance.isValid() || ( path.length() > 0 && path[ 0 ] == '/' ) )
64  return path;
65 
66  QString absPath = KStandardDirs::locate( "data", d->m_parentInstance.componentName() + '/' + path );
67  assert( !absPath.isEmpty() );
68  return absPath;
69 }
70 
71 QString Plugin::localXMLFile() const
72 {
73  QString path = KXMLGUIClient::xmlFile();
74 
75  if ( !d->m_parentInstance.isValid() || ( path.length() > 0 && path[ 0 ] == '/' ) )
76  return path;
77 
78  QString absPath = KStandardDirs::locateLocal( "data", d->m_parentInstance.componentName() + '/' + path );
79  assert( !absPath.isEmpty() );
80  return absPath;
81 }
82 
83 //static
84 QList<Plugin::PluginInfo> Plugin::pluginInfos(const KComponentData &componentData)
85 {
86  if (!componentData.isValid())
87  kError(1000) << "No componentData ???" << endl;
88 
89  QList<PluginInfo> plugins;
90 
91  // TODO KDE5: change * into *.rc and remove test for .desktop from the for loop below.
92  const QStringList pluginDocs = componentData.dirs()->findAllResources(
93  "data", componentData.componentName()+"/kpartplugins/*", KStandardDirs::Recursive );
94 
95  QMap<QString,QStringList> sortedPlugins;
96 
97  QStringList::ConstIterator pIt = pluginDocs.begin();
98  QStringList::ConstIterator pEnd = pluginDocs.end();
99  for (; pIt != pEnd; ++pIt )
100  {
101  QFileInfo fInfo( *pIt );
102  if ( fInfo.completeSuffix() == QLatin1String( "desktop" ) )
103  continue;
104 
105  QMap<QString,QStringList>::Iterator mapIt = sortedPlugins.find( fInfo.fileName() );
106  if ( mapIt == sortedPlugins.end() )
107  mapIt = sortedPlugins.insert( fInfo.fileName(), QStringList() );
108 
109  mapIt.value().append( *pIt );
110  }
111 
112  QMap<QString,QStringList>::ConstIterator mapIt = sortedPlugins.constBegin();
113  QMap<QString,QStringList>::ConstIterator mapEnd = sortedPlugins.constEnd();
114  for (; mapIt != mapEnd; ++mapIt )
115  {
116  PluginInfo info;
117  QString doc;
118  info.m_absXMLFileName = KXMLGUIClient::findMostRecentXMLFile( mapIt.value(), doc );
119  if ( info.m_absXMLFileName.isEmpty() )
120  continue;
121 
122  kDebug( 1000 ) << "found KParts Plugin : " << info.m_absXMLFileName;
123  info.m_relXMLFileName = "kpartplugins/";
124  info.m_relXMLFileName += mapIt.key();
125 
126  info.m_document.setContent( doc );
127  if ( info.m_document.documentElement().isNull() )
128  continue;
129 
130  plugins.append( info );
131  }
132 
133  return plugins;
134 }
135 
136 void Plugin::loadPlugins(QObject *parent, const KComponentData &componentData)
137 {
138  loadPlugins( parent, pluginInfos( componentData ), componentData );
139 }
140 
141 void Plugin::loadPlugins(QObject *parent, const QList<PluginInfo> &pluginInfos, const KComponentData &componentData)
142 {
143  QList<PluginInfo>::ConstIterator pIt = pluginInfos.begin();
144  QList<PluginInfo>::ConstIterator pEnd = pluginInfos.end();
145  for (; pIt != pEnd; ++pIt )
146  {
147  QString library = (*pIt).m_document.documentElement().attribute( "library" );
148 
149  if ( library.isEmpty() || hasPlugin( parent, library ) )
150  continue;
151 
152  Plugin *plugin = loadPlugin( parent, library, (*pIt).m_document.documentElement().attribute( "X-KDE-PluginKeyword" ) );
153 
154  if ( plugin )
155  {
156  plugin->d->m_parentInstance = componentData;
157  plugin->setXMLFile( (*pIt).m_relXMLFileName, false, false );
158  plugin->setDOMDocument( (*pIt).m_document );
159 
160  }
161  }
162 
163 }
164 
165 void Plugin::loadPlugins( QObject *parent, const QList<PluginInfo> &pluginInfos )
166 {
167  loadPlugins(parent, pluginInfos, KComponentData());
168 }
169 
170 // static, deprecated
171 #ifndef KDE_NO_DEPRECATED
172 Plugin* Plugin::loadPlugin( QObject * parent, const char* libname )
173 {
174  Plugin* plugin = KLibLoader::createInstance<Plugin>( libname, parent );
175  if ( !plugin )
176  return 0;
177  plugin->d->m_library = libname;
178  return plugin;
179 }
180 #endif
181 
182 // static, deprecated
183 #ifndef KDE_NO_DEPRECATED
184 Plugin* Plugin::loadPlugin( QObject * parent, const QByteArray &libname )
185 {
186  return loadPlugin( parent, libname.data() );
187 }
188 #endif
189 
190 Plugin* Plugin::loadPlugin( QObject * parent, const QString &libname )
191 {
192  return loadPlugin( parent, libname, "" );
193 }
194 
195 // static
196 Plugin* Plugin::loadPlugin( QObject * parent, const QString &libname, const QString &keyword )
197 {
198  KPluginLoader loader( libname );
199  KPluginFactory* factory = loader.factory();
200 
201  if (!factory) {
202  return 0;
203  }
204 
205  Plugin* plugin = factory->create<Plugin>( keyword, parent );
206  if ( !plugin )
207  return 0;
208  plugin->d->m_library = libname;
209  return plugin;
210 }
211 
212 QList<KParts::Plugin *> Plugin::pluginObjects( QObject *parent )
213 {
214  QList<KParts::Plugin *> objects;
215 
216  if (!parent )
217  return objects;
218 
219  // TODO: move to a new method KGlobal::findDirectChildren, if there is more than one use of this?
220  const QObjectList plugins = parent->children();
221 
222  QObjectList::ConstIterator it = plugins.begin();
223  for ( ; it != plugins.end() ; ++it )
224  {
225  Plugin * plugin = qobject_cast<Plugin *>( *it );
226  if ( plugin )
227  objects.append( plugin );
228  }
229 
230  return objects;
231 }
232 
233 bool Plugin::hasPlugin( QObject* parent, const QString& library )
234 {
235  const QObjectList plugins = parent->children();
236 
237  QObjectList::ConstIterator it = plugins.begin();
238  for ( ; it != plugins.end() ; ++it )
239  {
240  Plugin * plugin = qobject_cast<Plugin *>( *it );
241  if ( plugin && plugin->d->m_library == library )
242  {
243  return true;
244  }
245  }
246  return false;
247 }
248 
249 void Plugin::setComponentData(const KComponentData &componentData)
250 {
251  KGlobal::locale()->insertCatalog(componentData.catalogName());
252  KXMLGUIClient::setComponentData(componentData);
253 }
254 
255 void Plugin::loadPlugins(QObject *parent, KXMLGUIClient* parentGUIClient,
256  const KComponentData &componentData, bool enableNewPluginsByDefault,
257  int interfaceVersionRequired)
258 {
259  KConfigGroup cfgGroup( componentData.config(), "KParts Plugins" );
260  const QList<PluginInfo> plugins = pluginInfos( componentData );
261  QList<PluginInfo>::ConstIterator pIt = plugins.begin();
262  const QList<PluginInfo>::ConstIterator pEnd = plugins.end();
263  for (; pIt != pEnd; ++pIt )
264  {
265  QDomElement docElem = (*pIt).m_document.documentElement();
266  QString library = docElem.attribute( "library" );
267  QString keyword;
268 
269  if ( library.isEmpty() )
270  continue;
271 
272  // Check configuration
273  const QString name = docElem.attribute( "name" );
274 
275  bool pluginEnabled = enableNewPluginsByDefault;
276  if ( cfgGroup.hasKey( name + "Enabled" ) )
277  {
278  pluginEnabled = cfgGroup.readEntry( name + "Enabled" , false );
279  }
280  else
281  { // no user-setting, load plugin default setting
282  QString relPath = QString( componentData.componentName() ) + '/' + (*pIt).m_relXMLFileName;
283  relPath.truncate( relPath.lastIndexOf( '.' ) ); // remove extension
284  relPath += ".desktop";
285  //kDebug(1000) << "looking for " << relPath;
286  const QString desktopfile = componentData.dirs()->findResource( "data", relPath );
287  if( !desktopfile.isEmpty() )
288  {
289  //kDebug(1000) << "loadPlugins found desktop file for " << name << ": " << desktopfile;
290  KDesktopFile _desktop( desktopfile );
291  const KConfigGroup desktop = _desktop.desktopGroup();
292  keyword = desktop.readEntry("X-KDE-PluginKeyword", "");
293  pluginEnabled = desktop.readEntry( "X-KDE-PluginInfo-EnabledByDefault",
294  enableNewPluginsByDefault );
295  if ( interfaceVersionRequired != 0 )
296  {
297  const int version = desktop.readEntry( "X-KDE-InterfaceVersion", 1 );
298  if ( version != interfaceVersionRequired )
299  {
300  kDebug(1000) << "Discarding plugin " << name << ", interface version " << version << ", expected " << interfaceVersionRequired;
301  pluginEnabled = false;
302  }
303  }
304  }
305  else
306  {
307  //kDebug(1000) << "loadPlugins no desktop file found in " << relPath;
308  }
309  }
310 
311  // search through already present plugins
312  const QObjectList pluginList = parent->children();
313 
314  bool pluginFound = false;
315  for ( QObjectList::ConstIterator it = pluginList.begin(); it != pluginList.end() ; ++it )
316  {
317  Plugin * plugin = qobject_cast<Plugin *>( *it );
318  if( plugin && plugin->d->m_library == library )
319  {
320  // delete and unload disabled plugins
321  if( !pluginEnabled )
322  {
323  kDebug( 1000 ) << "remove plugin " << name;
324  KXMLGUIFactory * factory = plugin->factory();
325  if( factory )
326  factory->removeClient( plugin );
327  delete plugin;
328  }
329 
330  pluginFound = true;
331  break;
332  }
333  }
334 
335  // if the plugin is already loaded or if it's disabled in the
336  // configuration do nothing
337  if( pluginFound || !pluginEnabled )
338  continue;
339 
340  kDebug( 1000 ) << "load plugin " << name << " " << library << " " << keyword;
341  Plugin *plugin = loadPlugin( parent, library, keyword );
342 
343  if ( plugin )
344  {
345  plugin->d->m_parentInstance = componentData;
346  plugin->setXMLFile( (*pIt).m_relXMLFileName, false, false );
347  plugin->setDOMDocument( (*pIt).m_document );
348  parentGUIClient->insertChildClient( plugin );
349  }
350  }
351 }
352 
353 // vim:sw=4:et:sts=4
354 
355 #include "plugin.moc"
KParts::Plugin::loadPlugin
static Plugin * loadPlugin(QObject *parent, const char *libname)
Definition: plugin.cpp:172
kdebug.h
KXMLGUIClient
KXMLGUIFactory::removeClient
void removeClient(KXMLGUIClient *client)
KStandardDirs::locate
static QString locate(const char *type, const QString &filename, const KComponentData &cData=KGlobal::mainComponent())
KParts::Plugin::setComponentData
virtual void setComponentData(const KComponentData &instance)
Definition: plugin.cpp:249
name
const char * name(StandardAction id)
plugin.h
kxmlguifactory.h
kError
static QDebug kError(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KXMLGUIClient::setXMLFile
virtual void setXMLFile(const QString &file, bool merge=false, bool setXMLDoc=true)
KXMLGUIClient::factory
KXMLGUIFactory * factory() const
KXMLGUIClient::xmlFile
virtual QString xmlFile() const
QString
KXMLGUIClient::setDOMDocument
virtual void setDOMDocument(const QDomDocument &document, bool merge=false)
kdesktopfile.h
QObject
kDebug
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
klocale.h
KComponentData::catalogName
QString catalogName() const
KParts::Plugin::pluginInfos
static QList< Plugin::PluginInfo > pluginInfos(const KComponentData &instance)
Look for plugins in the instance's "data" directory (+"/kpartplugins")
Definition: plugin.cpp:84
KComponentData::config
const KSharedConfig::Ptr & config() const
KPluginLoader
KStandardDirs::Recursive
KXMLGUIClient::componentData
virtual KComponentData componentData() const
KParts::Plugin::PluginInfo::m_relXMLFileName
QString m_relXMLFileName
Definition: plugin.h:60
QStringList
KParts::Plugin::localXMLFile
virtual QString localXMLFile() const
Reimplemented for internal reasons.
Definition: plugin.cpp:71
KParts::Plugin::~Plugin
virtual ~Plugin()
Destructor.
Definition: plugin.cpp:54
KParts::Plugin::PluginInfo::m_document
QDomDocument m_document
Definition: plugin.h:63
KComponentData::componentName
QString componentName() const
KParts::Plugin::PluginInfo
Definition: plugin.h:58
KXMLGUIClient::setComponentData
virtual void setComponentData(const KComponentData &componentData)
KLocale::insertCatalog
void insertCatalog(const QString &catalog)
KXMLGUIFactory
KDesktopFile
KGlobal::locale
KLocale * locale()
KPluginLoader::factory
KPluginFactory * factory()
KConfigGroup
KParts::Plugin::pluginObjects
static QList< Plugin * > pluginObjects(QObject *parent)
Returns a list of plugin objects loaded for parent.
Definition: plugin.cpp:212
KParts::Plugin::PluginInfo::m_absXMLFileName
QString m_absXMLFileName
Definition: plugin.h:61
KStandardDirs::locateLocal
static QString locateLocal(const char *type, const QString &filename, const KComponentData &cData=KGlobal::mainComponent())
kstandarddirs.h
version
unsigned int version()
KComponentData::isValid
bool isValid() const
KStandardDirs::findResource
QString findResource(const char *type, const QString &filename) const
componentfactory.h
KParts::Plugin::loadPlugins
static void loadPlugins(QObject *parent, const KComponentData &instance)
Load the plugin libraries from the directories appropriate to instance and make the Plugin objects ch...
Definition: plugin.cpp:136
KStandardDirs::findAllResources
QStringList findAllResources(const char *type, const QString &filter=QString(), SearchOptions options=NoSearchOptions) const
KXMLGUIClient::insertChildClient
void insertChildClient(KXMLGUIClient *child)
kcomponentdata.h
KXMLGUIClient::findMostRecentXMLFile
static QString findMostRecentXMLFile(const QStringList &files, QString &doc)
KPluginFactory
KParts::Plugin
A plugin is the way to add actions to an existing KParts application, or to a Part.
Definition: plugin.h:54
KConfigGroup::readEntry
T readEntry(const QString &key, const T &aDefault) const
KDesktopFile::desktopGroup
KConfigGroup desktopGroup() const
KComponentData
QMap< QString, QStringList >
part.h
kconfiggroup.h
QList
Definition: browserextension.h:34
KParts::Plugin::Plugin
Plugin(QObject *parent=0)
Construct a new KParts plugin.
Definition: plugin.cpp:48
KComponentData::dirs
KStandardDirs * dirs() const
KParts::Plugin::xmlFile
virtual QString xmlFile() const
Reimplemented for internal reasons.
Definition: plugin.cpp:59
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:50:42 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KParts

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

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal