• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

kresources

factory.cpp

Go to the documentation of this file.
00001 /*
00002     This file is part of libkresources.
00003 
00004     Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
00005     Copyright (c) 2002 Jan-Pascal van Best <janpascal@vanbest.org>
00006     Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
00007 
00008     This library is free software; you can redistribute it and/or
00009     modify it under the terms of the GNU Library General Public
00010     License as published by the Free Software Foundation; either
00011     version 2 of the License, or (at your option) any later version.
00012 
00013     This library is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016     Library General Public License for more details.
00017 
00018     You should have received a copy of the GNU Library General Public License
00019     along with this library; see the file COPYING.LIB.  If not, write to
00020     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00021     Boston, MA 02110-1301, USA.
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();
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() << "Factory creation failed: " << loader.errorString();
00117     return 0;
00118   }
00119 
00120   PluginFactoryBase *pluginFactory = static_cast<PluginFactoryBase *>( factory );
00121 
00122   if ( !pluginFactory ) {
00123     kDebug() << "no plugin factory.";
00124     return 0;
00125   }
00126 
00127   ConfigWidget *wdg = pluginFactory->configWidget( parent );
00128   if ( !wdg ) {
00129     kDebug() << "'" << ptr->library() << "' doesn't provide a ConfigWidget";
00130     return 0;
00131   }
00132 
00133   return wdg;
00134 }
00135 
00136 QString Factory::typeName( const QString &type ) const
00137 {
00138   if ( type.isEmpty() || !d->mTypeMap.contains( type ) ) {
00139     return QString();
00140   }
00141 
00142   KService::Ptr ptr = d->mTypeMap[ type ];
00143   return ptr->name();
00144 }
00145 
00146 QString Factory::typeDescription( const QString &type ) const
00147 {
00148   if ( type.isEmpty() || !d->mTypeMap.contains( type ) ) {
00149     return QString();
00150   }
00151 
00152   KService::Ptr ptr = d->mTypeMap[ type ];
00153   return ptr->comment();
00154 }
00155 
00156 Resource *Factory::Private::resourceInternal( const QString &type, const KConfigGroup *group )
00157 {
00158   kDebug() << "(" << type << ", config )";
00159 
00160   if ( type.isEmpty() || !mTypeMap.contains( type ) ) {
00161     kDebug() << "no such type" << type;
00162     return 0;
00163   }
00164 
00165   KService::Ptr ptr = mTypeMap[ type ];
00166   KPluginLoader loader( ptr->library() );
00167   KPluginFactory *factory = loader.factory();
00168   if ( !factory ) {
00169     kDebug() << "Factory creation failed" << loader.errorString();
00170     return 0;
00171   }
00172 
00173   PluginFactoryBase *pluginFactory = static_cast<PluginFactoryBase *>( factory );
00174 
00175   if ( !pluginFactory ) {
00176     kDebug() << "no plugin factory.";
00177     return 0;
00178   }
00179 
00180   Resource *resource;
00181   if ( group ) {
00182     resource = pluginFactory->resource( *group );
00183   } else {
00184     resource = pluginFactory->resource();
00185   }
00186 
00187   if ( !resource ) {
00188     kDebug() << "'" << ptr->library()
00189              << "' is not a" << mResourceFamily << "plugin.";
00190     return 0;
00191   }
00192 
00193   resource->setType( type );
00194 
00195   return resource;
00196 }
00197 
00198 Resource *Factory::resource( const QString &type, const KConfigGroup &group )
00199 {
00200   return d->resourceInternal( type, &group );
00201 }
00202 
00203 Resource *Factory::resource( const QString &type )
00204 {
00205   return d->resourceInternal( type, 0 );
00206 }

kresources

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

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  • kabc
  • kblog
  • kcal
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.4
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