• Skip to content
  • Skip to link menu
KDE 4.0 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(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 }

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"
  • 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.5
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