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., 51 Franklin Street, Fifth Floor,
00021     Boston, MA 02110-1301, 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                   << KLibLoader::self()->lastErrorMessage() << 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     kdDebug(5650) << "Factory::resource() no such type " << type << endl;
00134     return 0;
00135   }
00136 
00137   KService::Ptr ptr = mTypeMap[ type ];
00138   KLibFactory *factory = KLibLoader::self()->factory( ptr->library().latin1() );
00139   if ( !factory ) {
00140     kdDebug(5650) << "KRES::Factory::resource(): Factory creation failed "
00141                   << KLibLoader::self()->lastErrorMessage() << endl;
00142     return 0;
00143   }
00144 
00145   PluginFactoryBase *pluginFactory = static_cast<PluginFactoryBase *>( factory );
00146 
00147   if ( !pluginFactory ) {
00148     kdDebug(5650) << "KRES::Factory::resource(): no plugin factory." << endl;
00149     return 0;
00150   }
00151 
00152   Resource *resource = pluginFactory->resource( config );
00153   if ( !resource ) {
00154     kdDebug(5650) << "'" << ptr->library() << "' is not a " + mResourceFamily +
00155                      " plugin." << endl;
00156     return 0;
00157   }
00158 
00159   resource->setType( type );
00160 
00161   return resource;
00162 }
KDE Home | KDE Accessibility Home | Description of Access Keys