componentfactory.h
#ifndef __kparts_componentfactory_h__
#define __kparts_componentfactory_h__
#include <kparts/factory.h>
#include <kparts/part.h>
#include <ktrader.h>
#include <qmetaobject.h>
namespace KParts
{
// this is a namespace and not a class because stupid egcs 1.1.2 doesn't grok
// static template methods in classes. !@%@#$!
namespace ComponentFactory
{
enum ComponentLoadingError { ErrNoServiceFound = 1,
ErrServiceProvidesNoLibrary,
ErrNoLibrary,
ErrNoFactory,
ErrNoComponent };
template <class T>
static T *createInstanceFromFactory( KLibFactory *factory, QObject *parent = 0,
const char *name = 0,
const QStringList &args = QStringList() )
{
QObject *object = factory->create( parent, name,
T::staticMetaObject()->className(),
args );
T *result = dynamic_cast<T *>( object );
if ( !result )
delete object;
return result;
}
template <class T>
static T *createPartInstanceFromFactory( KParts::Factory *factory,
QWidget *parentWidget = 0,
const char *widgetName = 0,
QObject *parent = 0,
const char *name = 0,
const QStringList &args = QStringList() )
{
KParts::Part *object = factory->createPart( parentWidget, widgetName,
parent, name,
T::staticMetaObject()->className(),
args );
T *result = dynamic_cast<T *>( object );
if ( !result )
delete object;
return result;
}
template <class T>
static T *createInstanceFromLibrary( const char *libraryName, QObject *parent = 0,
const char *name = 0,
const QStringList &args = QStringList(),
int *error = 0 )
{
KLibrary *library = KLibLoader::self()->library( libraryName );
if ( !library )
{
if ( error )
*error = ErrNoLibrary;
return 0;
}
KLibFactory *factory = library->factory();
if ( !factory )
{
library->unload();
if ( error )
*error = ErrNoFactory;
return 0;
}
T *res = createInstanceFromFactory<T>( factory, parent, name, args );
if ( !res )
{
library->unload();
if ( error )
*error = ErrNoComponent;
}
return res;
}
template <class T>
static T *createPartInstanceFromLibrary( const char *libraryName,
QWidget *parentWidget = 0,
const char *widgetName = 0,
QObject *parent = 0,
const char *name = 0,
const QStringList &args = QStringList(),
int *error = 0 )
{
KLibrary *library = KLibLoader::self()->library( libraryName );
if ( !library )
{
if ( error )
*error = ErrNoLibrary;
return 0;
}
KLibFactory *factory = library->factory();
if ( !factory )
{
library->unload();
if ( error )
*error = ErrNoFactory;
return 0;
}
KParts::Factory *partFactory = dynamic_cast<KParts::Factory *>( factory );
if ( !partFactory )
{
library->unload();
if ( error )
*error = ErrNoFactory;
return 0;
}
T *res = createPartInstanceFromFactory<T>( partFactory, parentWidget,
widgetName, parent, name, args );
if ( !res )
{
library->unload();
if ( error )
*error = ErrNoComponent;
}
return res;
}
template <class T>
static T *createInstanceFromService( const KService::Ptr &service,
QObject *parent = 0,
const char *name = 0,
const QStringList &args = QStringList(),
int *error = 0 )
{
QString library = service->library();
if ( library.isEmpty() )
{
if ( error )
*error = ErrServiceProvidesNoLibrary;
return 0;
}
return createInstanceFromLibrary<T>( library.local8Bit().data(), parent,
name, args, error );
}
template <class T>
static T *createPartInstanceFromService( const KService::Ptr &service,
QWidget *parentWidget = 0,
const char *widgetName = 0,
QObject *parent = 0,
const char *name = 0,
const QStringList &args = QStringList(),
int *error = 0 )
{
QString library = service->library();
if ( library.isEmpty() )
{
if ( error )
*error = ErrServiceProvidesNoLibrary;
return 0;
}
return createPartInstanceFromLibrary<T>( library.local8Bit().data(), parentWidget,
widgetName, parent, name, args, error );
}
template <class T, class ServiceIterator>
static T *createInstanceFromServices( ServiceIterator begin, ServiceIterator end,
QObject *parent = 0,
const char *name = 0,
const QStringList &args = QStringList(),
int *error = 0 )
{
for (; begin != end; ++begin )
{
KService::Ptr service = *begin;
if ( error )
*error = 0;
T *component = createInstanceFromService<T>( service, parent, name,
args, error );
if ( component )
return component;
}
if ( error )
*error = ErrNoServiceFound;
return 0;
}
template <class T, class ServiceIterator>
static T *createPartInstanceFromServices( ServiceIterator begin,
ServiceIterator end,
QWidget *parentWidget = 0,
const char *widgetName = 0,
QObject *parent = 0,
const char *name = 0,
const QStringList &args = QStringList(),
int *error = 0 )
{
for (; begin != end; ++begin )
{
KService::Ptr service = *begin;
if ( error )
*error = 0;
T *component = createPartInstanceFromService<T>( service, parentWidget,
widgetName, parent,
name, args, error );
if ( component )
return component;
}
if ( error )
*error = ErrNoServiceFound;
return 0;
}
template <class T>
static T *createInstanceFromQuery( const QString &serviceType,
const QString &constraint = QString::null,
QObject *parent = 0,
const char *name = 0,
const QStringList &args = QStringList(),
int *error = 0 )
{
KTrader::OfferList offers = KTrader::self()->query( serviceType, constraint );
if ( offers.isEmpty() )
{
if ( error )
*error = ErrNoServiceFound;
return 0;
}
return createInstanceFromServices<T>( offers.begin(),
offers.end(),
parent, name, args, error );
}
template <class T>
static T *createPartInstanceFromQuery( const QString &serviceType,
const QString &constraint,
QWidget *parentWidget = 0,
const char *widgetName = 0,
QObject *parent = 0,
const char *name = 0,
const QStringList &args = QStringList(),
int *error = 0 )
{
KTrader::OfferList offers = KTrader::self()->query( serviceType, QString::fromLatin1("KParts/ReadOnlyPart"), constraint, QString::null );
if ( offers.isEmpty() )
{
if ( error )
*error = ErrNoServiceFound;
return 0;
}
return createPartInstanceFromServices<T>( offers.begin(), offers.end(),
parentWidget, widgetName,
parent, name, args, error );
}
}
}
/*
* vim: et sw=4
*/
#endif
KLibFactory
KParts::Part
Base class for parts.
Definition: part.h:181
KLibrary::unload
void unload() const
KLibrary
KParts::Factory::createPart
Part * createPart(QWidget *parentWidget=0, const char *widgetName=0, QObject *parent=0, const char *name=0, const char *classname="KParts::Part", const QStringList &args=QStringList())
Creates a part.
Definition: factory.cpp:42
KParts::ComponentFactory::createPartInstanceFromFactory
static T * createPartInstanceFromFactory(KParts::Factory *factory, QWidget *parentWidget=0, const char *widgetName=0, QObject *parent=0, const char *name=0, const QStringList &args=QStringList())
This template function allows to ask the given kparts factory to create an instance of the given temp...
Definition: componentfactory.h:95
KLibrary::factory
KLibFactory * factory()
KParts::Factory
A generic factory object to create a Part.
Definition: factory.h:40
KParts::ComponentFactory::createInstanceFromFactory
static T * createInstanceFromFactory(KLibFactory *factory, QObject *parent=0, const char *name=0, const QStringList &args=QStringList())
This template function allows to ask the given factory to create an instance of the given template ty...
Definition: componentfactory.h:61
KLibLoader::library
virtual KLibrary * library(const char *libname)
KLibLoader::self
static KLibLoader * self()
KParts::ComponentFactory::createInstanceFromQuery
static T * createInstanceFromQuery(const QString &serviceType, const QString &constraint=QString::null, QObject *parent=0, const char *name=0, const QStringList &args=QStringList(), int *error=0)
This method creates and returns a plugin, from the trader query for a given serviceType.
Definition: componentfactory.h:321
KParts::ComponentFactory::ComponentLoadingError
ComponentLoadingError
This enum type defines the possible error cases that can happen when loading a component.
Definition: componentfactory.h:37
KParts::ComponentFactory::createInstanceFromLibrary
static T * createInstanceFromLibrary(const char *libraryName, QObject *parent=0, const char *name=0, const QStringList &args=QStringList(), int *error=0)
This template allows to load the specified library and ask the factory to create an instance of the g...
Definition: componentfactory.h:127
KStdAccel::name
QString name(StdAccel id)
KParts::ComponentFactory::createPartInstanceFromQuery
static T * createPartInstanceFromQuery(const QString &serviceType, const QString &constraint, QWidget *parentWidget=0, const char *widgetName=0, QObject *parent=0, const char *name=0, const QStringList &args=QStringList(), int *error=0)
This method creates and returns a KParts part from a serviceType (e.g.
Definition: componentfactory.h:370

KDE Home | KDE Accessibility Home | Description of Access Keys