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

KDECore

  • sources
  • kde-4.14
  • kdelibs
  • kdecore
  • util
kpluginfactory.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE project
2  Copyright (C) 2007 Matthias Kretz <kretz@kde.org>
3  Copyright (C) 2007 Bernhard Loos <nhuh.put@web.de>
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 
22 #include "kpluginfactory.h"
23 #include "kpluginfactory_p.h"
24 #include <kdebug.h>
25 #include <kglobal.h>
26 
27 #include <QtCore/QObjectCleanupHandler>
28 
29 K_GLOBAL_STATIC(QObjectCleanupHandler, factorycleanup)
30 
31 extern int kLibraryDebugArea();
32 
33 KPluginFactory::KPluginFactory(const char *componentName, const char *catalogName, QObject *parent)
34  : QObject(parent), d_ptr(new KPluginFactoryPrivate)
35 {
36  Q_D(KPluginFactory);
37  d->q_ptr = this;
38 
39  if (componentName)
40  d->componentData = KComponentData(componentName, catalogName);
41 
42  factorycleanup->add(this);
43 }
44 
45 #ifndef KDE_NO_DEPRECATED
46 KPluginFactory::KPluginFactory(const KAboutData *aboutData, QObject *parent)
47  : QObject(parent), d_ptr(new KPluginFactoryPrivate)
48 {
49  Q_D(KPluginFactory);
50  d->q_ptr = this;
51  d->componentData = KComponentData(*aboutData);
52 
53  factorycleanup->add(this);
54 }
55 #endif
56 
57 KPluginFactory::KPluginFactory(const KAboutData &aboutData, QObject *parent)
58  : QObject(parent), d_ptr(new KPluginFactoryPrivate)
59 {
60  Q_D(KPluginFactory);
61  d->q_ptr = this;
62  d->componentData = KComponentData(aboutData);
63 
64  factorycleanup->add(this);
65 }
66 
67 #ifndef KDE_NO_DEPRECATED
68 KPluginFactory::KPluginFactory(QObject *parent)
69  : QObject(parent), d_ptr(new KPluginFactoryPrivate())
70 {
71  Q_D(KPluginFactory);
72  d->q_ptr = this;
73  factorycleanup->add(this);
74 }
75 #endif
76 
77 KPluginFactory::KPluginFactory(KPluginFactoryPrivate &d, QObject *parent)
78  : QObject(parent), d_ptr(&d)
79 {
80  factorycleanup->add(this);
81 }
82 
83 KPluginFactory::~KPluginFactory()
84 {
85  Q_D(KPluginFactory);
86 
87  if (d->catalogInitialized && d->componentData.isValid()) {
88  KGlobal::locale()->removeCatalog(d->componentData.catalogName());
89  }
90 
91  delete d_ptr;
92 }
93 
94 KComponentData KPluginFactory::componentData() const
95 {
96  Q_D(const KPluginFactory);
97  return d->componentData;
98 }
99 
100 void KPluginFactory::registerPlugin(const QString &keyword, const QMetaObject *metaObject, CreateInstanceFunction instanceFunction)
101 {
102  Q_D(KPluginFactory);
103 
104  Q_ASSERT(metaObject);
105 
106  // we allow different interfaces to be registered without keyword
107  if (!keyword.isEmpty()) {
108  if (d->createInstanceHash.contains(keyword)) {
109  kFatal(kLibraryDebugArea()) << "A plugin with the keyword" << keyword << "was already registered. A keyword must be unique!";
110  }
111  d->createInstanceHash.insert(keyword, KPluginFactoryPrivate::Plugin(metaObject, instanceFunction));
112  } else {
113  QList<KPluginFactoryPrivate::Plugin> clashes(d->createInstanceHash.values(keyword));
114  const QMetaObject *superClass = metaObject->superClass();
115  if (superClass) {
116  foreach (const KPluginFactoryPrivate::Plugin &plugin, clashes) {
117  for (const QMetaObject *otherSuper = plugin.first->superClass(); otherSuper;
118  otherSuper = otherSuper->superClass()) {
119  if (superClass == otherSuper) {
120  kFatal(kLibraryDebugArea()) << "Two plugins with the same interface(" << superClass->className() << ") were registered. Use keywords to identify the plugins.";
121  }
122  }
123  }
124  }
125  foreach (const KPluginFactoryPrivate::Plugin &plugin, clashes) {
126  superClass = plugin.first->superClass();
127  if (superClass) {
128  for (const QMetaObject *otherSuper = metaObject->superClass(); otherSuper;
129  otherSuper = otherSuper->superClass()) {
130  if (superClass == otherSuper) {
131  kFatal(kLibraryDebugArea()) << "Two plugins with the same interface(" << superClass->className() << ") were registered. Use keywords to identify the plugins.";
132  }
133  }
134  }
135  }
136  d->createInstanceHash.insertMulti(keyword, KPluginFactoryPrivate::Plugin(metaObject, instanceFunction));
137  }
138 }
139 
140 #ifndef KDE_NO_DEPRECATED
141 QObject *KPluginFactory::createObject(QObject *parent, const char *className, const QStringList &args)
142 {
143  Q_UNUSED(parent);
144  Q_UNUSED(className);
145  Q_UNUSED(args);
146  return 0;
147 }
148 #endif
149 
150 #ifndef KDE_NO_DEPRECATED
151 KParts::Part *KPluginFactory::createPartObject(QWidget *parentWidget, QObject *parent, const char *classname, const QStringList &args)
152 {
153  Q_UNUSED(parent);
154  Q_UNUSED(parentWidget);
155  Q_UNUSED(classname);
156  Q_UNUSED(args);
157  return 0;
158 }
159 #endif
160 
161 QObject *KPluginFactory::create(const char *iface, QWidget *parentWidget, QObject *parent, const QVariantList &args, const QString &keyword)
162 {
163  Q_D(KPluginFactory);
164 
165  QObject *obj = 0;
166 
167  if (!d->catalogInitialized) {
168  d->catalogInitialized = true;
169  setupTranslations();
170  }
171 
172 #ifndef KDE_NO_DEPRECATED
173  if (keyword.isEmpty()) {
174 
175  // kde3-kparts compatibility, remove in kde5
176  const char* kpartsIface = iface;
177  if (args.contains(QVariant(QString::fromLatin1("Browser/View"))))
178  kpartsIface = "Browser/View";
179 
180  const QStringList argsStringList = variantListToStringList(args);
181 
182  if ((obj = reinterpret_cast<QObject *>(createPartObject(parentWidget, parent, kpartsIface, argsStringList)))) {
183  objectCreated(obj);
184  return obj;
185  }
186 
187  if ((obj = createObject(parent, iface, argsStringList))) {
188  objectCreated(obj);
189  return obj;
190  }
191  }
192 #endif
193 
194  const QList<KPluginFactoryPrivate::Plugin> candidates(d->createInstanceHash.values(keyword));
195  // for !keyword.isEmpty() candidates.count() is 0 or 1
196 
197  foreach (const KPluginFactoryPrivate::Plugin &plugin, candidates) {
198  for (const QMetaObject *current = plugin.first; current; current = current->superClass()) {
199  if (0 == qstrcmp(iface, current->className())) {
200  if (obj) {
201  kFatal(kLibraryDebugArea()) << "ambiguous interface requested from a DSO containing more than one plugin";
202  }
203  obj = plugin.second(parentWidget, parent, args);
204  break;
205  }
206  }
207  }
208 
209  if (obj) {
210  emit objectCreated(obj);
211  }
212  return obj;
213 }
214 
215 void KPluginFactory::setupTranslations()
216 {
217  Q_D(KPluginFactory);
218 
219  if (!d->componentData.isValid())
220  return;
221 
222  KGlobal::locale()->insertCatalog(d->componentData.catalogName());
223 }
224 
225 void KPluginFactory::setComponentData(const KComponentData &kcd)
226 {
227  Q_D(KPluginFactory);
228  d->componentData = kcd;
229 }
230 
231 // KDE5 TODO: should be static, and possibly public (for apps to use)
232 QStringList KPluginFactory::variantListToStringList(const QVariantList &list)
233 {
234  QStringList stringlist;
235  Q_FOREACH(const QVariant& var, list)
236  stringlist << var.toString();
237  return stringlist;
238 }
239 
240 // KDE5 TODO: should be static, and possibly public (for apps to use)
241 QVariantList KPluginFactory::stringListToVariantList(const QStringList &list)
242 {
243  QVariantList variantlist;
244  Q_FOREACH(const QString& str, list)
245  variantlist << QVariant(str);
246  return variantlist;
247 }
248 
249 #include "kpluginfactory.moc"
QWidget
KPluginFactory::stringListToVariantList
QVariantList stringListToVariantList(const QStringList &list)
Definition: kpluginfactory.cpp:241
kdebug.h
KPluginFactory::create
T * create(QObject *parent=0, const QVariantList &args=QVariantList())
Use this method to create an object.
Definition: kpluginfactory.h:505
QMetaObject::superClass
const QMetaObject * superClass() const
K_GLOBAL_STATIC
#define K_GLOBAL_STATIC(TYPE, NAME)
This macro makes it easy to use non-POD types as global statics.
Definition: kglobal.h:221
KLocale::removeCatalog
void removeCatalog(const QString &catalog)
Removes a catalog for translation lookup.
Definition: klocale.cpp:141
KPluginFactory::componentData
KComponentData componentData() const
You can use this method to get the component data of the plugin.
Definition: kpluginfactory.cpp:94
QObjectCleanupHandler
kglobal.h
KPluginFactory::setupTranslations
virtual void setupTranslations()
Definition: kpluginfactory.cpp:215
QMetaObject
QString::insert
QString & insert(int position, QChar ch)
KPluginFactory::objectCreated
void objectCreated(QObject *object)
kLibraryDebugArea
int kLibraryDebugArea()
Definition: klibrary.cpp:33
KPluginFactory::variantListToStringList
QStringList variantListToStringList(const QVariantList &list)
Definition: kpluginfactory.cpp:232
QObject
QString::isEmpty
bool isEmpty() const
KPluginFactory::setComponentData
void setComponentData(const KComponentData &componentData)
This method sets the component data of the plugin.
Definition: kpluginfactory.cpp:225
KAboutData
This class is used to store information about a program.
Definition: kaboutdata.h:192
QString
QList
Definition: kaboutdata.h:33
QStringList
kpluginfactory_p.h
KPluginFactory::registerPlugin
void registerPlugin(const QString &keyword=QString(), CreateInstanceFunction instanceFunction=InheritanceChecker< T >().createInstanceFunction(reinterpret_cast< T * >(0)))
Registers a plugin with the factory.
Definition: kpluginfactory.h:402
QPair
KLocale::insertCatalog
void insertCatalog(const QString &catalog)
Adds another catalog to search for translation lookup.
Definition: klocale.cpp:136
KPluginFactory::KPluginFactory
KPluginFactory(const char *componentName=0, const char *catalogName=0, QObject *parent=0)
This constructor creates a factory for a plugin with the given componentName and catalogName.
Definition: kpluginfactory.cpp:33
KPluginFactoryPrivate
Definition: kpluginfactory_p.h:33
KGlobal::locale
KLocale * locale()
Returns the global locale object.
Definition: kglobal.cpp:170
KPluginFactory::createPartObject
virtual KParts::Part * createPartObject(QWidget *parentWidget, QObject *parent, const char *classname, const QStringList &args)
Definition: kpluginfactory.cpp:151
kpluginfactory.h
KPluginFactory::d_ptr
KPluginFactoryPrivate *const d_ptr
Definition: kpluginfactory.h:422
QString::fromLatin1
QString fromLatin1(const char *str, int size)
KPluginFactory::createObject
virtual QObject * createObject(QObject *parent, const char *className, const QStringList &args)
Definition: kpluginfactory.cpp:141
QVariant::toString
QString toString() const
KPluginFactory
If you develop a library that is to be loaded dynamically at runtime, then you should return a pointe...
Definition: kpluginfactory.h:232
kFatal
static QDebug kFatal(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
Definition: kdebug.h:198
KPluginFactory::~KPluginFactory
virtual ~KPluginFactory()
This destroys the PluginFactory.
Definition: kpluginfactory.cpp:83
KComponentData
Per component data.
Definition: kcomponentdata.h:46
QVariant
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:22:11 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

Skip menu "KDECore"
  • 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
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • 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