kresources Library API Documentation

factory.cpp

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., 59 Temple Place - Suite 330,
00021     Boston, MA 02111-1307, USA.
00022 */
00023 
00024 #include <kdebug.h>
00025 #include <klocale.h>
00026 #include <ksimpleconfig.h>
00027 #include <kstandarddirs.h>
00028 #include <kstaticdeleter.h>
00029 
00030 #include <qfile.h>
00031 
00032 #include "resource.h"
00033 #include "factory.h"
00034 
00035 using namespace KRES;
00036 
00037 QDict<Factory> *Factory::mSelves = 0;
00038 static KStaticDeleter< QDict<Factory> > staticDeleter;
00039 
00040 Factory *Factory::self( const QString& resourceFamily )
00041 {
00042   kdDebug(5650) << "Factory::self()" << endl;
00043 
00044   Factory *factory = 0;
00045   if ( !mSelves )
00046     staticDeleter.setObject( mSelves, new QDict<Factory> );
00047 
00048   factory = mSelves->find( resourceFamily );
00049 
00050   if ( !factory ) {
00051     factory = new Factory( resourceFamily );
00052     mSelves->insert( resourceFamily, factory );
00053   }
00054 
00055   return factory;
00056 }
00057 
00058 Factory::Factory( const QString& resourceFamily ) :
00059   mResourceFamily( resourceFamily )
00060 {
00061   KTrader::OfferList plugins = KTrader::self()->query( "KResources/Plugin", QString( "[X-KDE-ResourceFamily] == '%1'" )
00062                                                 .arg( resourceFamily ) );
00063   KTrader::OfferList::ConstIterator it;
00064   for ( it = plugins.begin(); it != plugins.end(); ++it ) {
00065     QVariant type = (*it)->property( "X-KDE-ResourceType" );
00066     if ( !type.toString().isEmpty() )
00067       mTypeMap.insert( type.toString(), *it );
00068   }
00069 }
00070 
00071 Factory::~Factory()
00072 {
00073 }
00074 
00075 QStringList Factory::typeNames() const
00076 {
00077   return mTypeMap.keys();
00078 }
00079 
00080 ConfigWidget *Factory::configWidget( const QString& type, QWidget *parent )
00081 {
00082   if ( type.isEmpty() || !mTypeMap.contains( type ) )
00083     return 0;
00084 
00085   KService::Ptr ptr = mTypeMap[ type ];
00086   KLibFactory *factory = KLibLoader::self()->factory( ptr->library().latin1() );
00087   if ( !factory ) {
00088     kdDebug(5650) << "KRES::Factory::configWidget(): Factory creation failed"
00089                   << endl;
00090     return 0;
00091   }
00092 
00093   PluginFactoryBase *pluginFactory = static_cast<PluginFactoryBase *>( factory );
00094 
00095   if ( !pluginFactory ) {
00096     kdDebug(5650) << "KRES::Factory::configWidget(): no plugin factory."
00097                   << endl;
00098     return 0;
00099   }
00100 
00101   ConfigWidget *wdg = pluginFactory->configWidget( parent );
00102   if ( !wdg ) {
00103     kdDebug(5650) << "'" << ptr->library() << "' doesn't provide a ConfigWidget" << endl;
00104     return 0;
00105   }
00106 
00107   return wdg;
00108 }
00109 
00110 QString Factory::typeName( const QString &type ) const
00111 {
00112   if ( type.isEmpty() || !mTypeMap.contains( type ) )
00113     return QString();
00114 
00115   KService::Ptr ptr = mTypeMap[ type ];
00116   return ptr->name();
00117 }
00118 
00119 QString Factory::typeDescription( const QString &type ) const
00120 {
00121   if ( type.isEmpty() || !mTypeMap.contains( type ) )
00122     return QString();
00123 
00124   KService::Ptr ptr = mTypeMap[ type ];
00125   return ptr->comment();
00126 }
00127 
00128 Resource *Factory::resource( const QString& type, const KConfig *config )
00129 {
00130   kdDebug(5650) << "Factory::resource( " << type << ", config )" << endl;
00131 
00132   if ( type.isEmpty() || !mTypeMap.contains( type ) )
00133     return 0;
00134 
00135   KService::Ptr ptr = mTypeMap[ type ];
00136   KLibFactory *factory = KLibLoader::self()->factory( ptr->library().latin1() );
00137   if ( !factory ) {
00138     kdDebug(5650) << "KRES::Factory::resource(): Factory creation failed"
00139                   << endl;
00140     return 0;
00141   }
00142 
00143   PluginFactoryBase *pluginFactory = static_cast<PluginFactoryBase *>( factory );
00144 
00145   if ( !pluginFactory ) {
00146     kdDebug(5650) << "KRES::Factory::resource(): no plugin factory." << endl;
00147     return 0;
00148   }
00149 
00150   Resource *resource = pluginFactory->resource( config );
00151   if ( !resource ) {
00152     kdDebug(5650) << "'" << ptr->library() << "' is not a " + mResourceFamily +
00153                      " plugin." << endl;
00154     return 0;
00155   }
00156 
00157   resource->setType( type );
00158 
00159   return resource;
00160 }
KDE Logo
This file is part of the documentation for kresources Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Nov 27 13:47:15 2004 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003