kresources
factory.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00036 #include "factory.h"
00037
00038 #include <QtCore/QFile>
00039
00040 #include <kdebug.h>
00041 #include <klocale.h>
00042 #include <kconfig.h>
00043 #include <kstandarddirs.h>
00044 #include <kservicetypetrader.h>
00045 #include <kpluginloader.h>
00046
00047 #include "resource.h"
00048
00049 using namespace KRES;
00050
00051 class Factory::Private
00052 {
00053 public:
00054 Resource *resourceInternal ( const QString &type, const KConfigGroup *group );
00055 QString mResourceFamily;
00056 QMap<QString, KService::Ptr> mTypeMap;
00057 };
00058
00059 typedef QMap<QString, Factory*> factoryMap;
00060 K_GLOBAL_STATIC( factoryMap, mSelves )
00061
00062 Factory *Factory::self( const QString &resourceFamily )
00063 {
00064 kDebug(5650) << "Factory::self()";
00065
00066 Factory *factory = 0;
00067
00068 factory = mSelves->value( resourceFamily, 0 );
00069
00070 if ( !factory ) {
00071 factory = new Factory( resourceFamily );
00072 mSelves->insert( resourceFamily, factory );
00073 }
00074
00075 return factory;
00076 }
00077
00078 Factory::Factory( const QString &resourceFamily ) :
00079 d( new KRES::Factory::Private )
00080 {
00081 d->mResourceFamily = resourceFamily;
00082 const KService::List plugins =
00083 KServiceTypeTrader::self()->query(
00084 "KResources/Plugin",
00085 QString( "[X-KDE-ResourceFamily] == '%1'" ).arg( d->mResourceFamily ) );
00086
00087 KService::List::ConstIterator it;
00088 for ( it = plugins.begin(); it != plugins.end(); ++it ) {
00089 const QVariant type = (*it)->property( "X-KDE-ResourceType" );
00090 if ( !type.toString().isEmpty() ) {
00091 d->mTypeMap.insert( type.toString(), *it );
00092 }
00093 }
00094 }
00095
00096 Factory::~Factory()
00097 {
00098 delete d;
00099 }
00100
00101 QStringList Factory::typeNames() const
00102 {
00103 return d->mTypeMap.keys();
00104 }
00105
00106 ConfigWidget *Factory::configWidget( const QString &type, QWidget *parent )
00107 {
00108 if ( type.isEmpty() || !d->mTypeMap.contains( type ) ) {
00109 return 0;
00110 }
00111
00112 KService::Ptr ptr = d->mTypeMap[ type ];
00113 KPluginLoader loader( ptr->library() );
00114 KPluginFactory *factory = loader.factory();
00115 if ( !factory ) {
00116 kDebug(5650) << "KRES::Factory::configWidget(): Factory creation failed"
00117 << loader.errorString();
00118 return 0;
00119 }
00120
00121 PluginFactoryBase *pluginFactory = static_cast<PluginFactoryBase *>( factory );
00122
00123 if ( !pluginFactory ) {
00124 kDebug(5650) << "KRES::Factory::configWidget(): no plugin factory.";
00125 return 0;
00126 }
00127
00128 ConfigWidget *wdg = pluginFactory->configWidget( parent );
00129 if ( !wdg ) {
00130 kDebug(5650) << "'" << ptr->library() << "' doesn't provide a ConfigWidget";
00131 return 0;
00132 }
00133
00134 return wdg;
00135 }
00136
00137 QString Factory::typeName( const QString &type ) const
00138 {
00139 if ( type.isEmpty() || !d->mTypeMap.contains( type ) ) {
00140 return QString();
00141 }
00142
00143 KService::Ptr ptr = d->mTypeMap[ type ];
00144 return ptr->name();
00145 }
00146
00147 QString Factory::typeDescription( const QString &type ) const
00148 {
00149 if ( type.isEmpty() || !d->mTypeMap.contains( type ) ) {
00150 return QString();
00151 }
00152
00153 KService::Ptr ptr = d->mTypeMap[ type ];
00154 return ptr->comment();
00155 }
00156
00157 Resource *Factory::Private::resourceInternal( const QString &type, const KConfigGroup *group )
00158 {
00159 kDebug(5650) << "Factory::resource(" << type << ", config )";
00160
00161 if ( type.isEmpty() || !mTypeMap.contains( type ) ) {
00162 kDebug(5650) << "Factory::resource() no such type" << type;
00163 return 0;
00164 }
00165
00166 KService::Ptr ptr = mTypeMap[ type ];
00167 KPluginLoader loader( ptr->library() );
00168 KPluginFactory *factory = loader.factory();
00169 if ( !factory ) {
00170 kDebug(5650) << "KRES::Factory::resource(): Factory creation failed"
00171 << loader.errorString();
00172 return 0;
00173 }
00174
00175 PluginFactoryBase *pluginFactory = static_cast<PluginFactoryBase *>( factory );
00176
00177 if ( !pluginFactory ) {
00178 kDebug(5650) << "KRES::Factory::resource(): no plugin factory.";
00179 return 0;
00180 }
00181
00182 Resource *resource;
00183 if ( group ) {
00184 resource = pluginFactory->resource( *group );
00185 } else {
00186 resource = pluginFactory->resource();
00187 }
00188
00189 if ( !resource ) {
00190 kDebug(5650) << "'" << ptr->library()
00191 << "' is not a" << mResourceFamily << "plugin.";
00192 return 0;
00193 }
00194
00195 resource->setType( type );
00196
00197 return resource;
00198 }
00199
00200 Resource *Factory::resource( const QString &type, const KConfigGroup &group )
00201 {
00202 return d->resourceInternal( type, &group );
00203 }
00204
00205 Resource *Factory::resource( const QString &type )
00206 {
00207 return d->resourceInternal( type, 0 );
00208 }