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() << "' is not a " + mResourceFamily +
00104                      " plugin." << endl;
00105     return 0;
00106   }
00107 
00108   return wdg;
00109 }
00110 
00111 QString Factory::typeName( const QString &type ) const
00112 {
00113   if ( type.isEmpty() || !mTypeMap.contains( type ) )
00114     return QString();
00115 
00116   KService::Ptr ptr = mTypeMap[ type ];
00117   return ptr->name();
00118 }
00119 
00120 QString Factory::typeDescription( const QString &type ) const
00121 {
00122   if ( type.isEmpty() || !mTypeMap.contains( type ) )
00123     return QString();
00124 
00125   KService::Ptr ptr = mTypeMap[ type ];
00126   return ptr->comment();
00127 }
00128 
00129 Resource *Factory::resource( const QString& type, const KConfig *config )
00130 {
00131   kdDebug(5650) << "Factory::resource( " << type << ", config )" << endl;
00132 
00133   if ( type.isEmpty() || !mTypeMap.contains( type ) )
00134     return 0;
00135 
00136   KService::Ptr ptr = mTypeMap[ type ];
00137   KLibFactory *factory = KLibLoader::self()->factory( ptr->library().latin1() );
00138   if ( !factory ) {
00139     kdDebug(5650) << "KRES::Factory::resource(): Factory creation failed"
00140                   << endl;
00141     return 0;
00142   }
00143 
00144   PluginFactoryBase *pluginFactory = static_cast<PluginFactoryBase *>( factory );
00145 
00146   if ( !pluginFactory ) {
00147     kdDebug(5650) << "KRES::Factory::resource(): no plugin factory." << endl;
00148     return 0;
00149   }
00150 
00151   Resource *resource = pluginFactory->resource( config );
00152   if ( !resource ) {  
00153     kdDebug(5650) << "'" << ptr->library() << "' is not a " + mResourceFamily +
00154                      " plugin." << endl;
00155     return 0;
00156   }
00157 
00158   resource->setType( type );
00159 
00160   return resource;
00161 }
KDE Logo
This file is part of the documentation for kresources Library Version 3.2.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Mar 4 22:44:50 2004 by doxygen 1.3.6-20040222 written by Dimitri van Heesch, © 1997-2003