kitchensync

filtermanager.cpp

00001 /*
00002     This file is part of KitchenSync.
00003 
00004     Copyright (c) 2005 Tobias Koenig <tokoe@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019     Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include <qfile.h>
00023 
00024 #include <kdebug.h>
00025 #include <kgenericfactory.h>
00026 #include <klibloader.h>
00027 #include <kstaticdeleter.h>
00028 #include <ktrader.h>
00029 
00030 #include "filtermanager.h"
00031 
00032 using namespace KSync;
00033 
00034 FilterManager *FilterManager::mSelf = 0;
00035 static KStaticDeleter<FilterManager> filterFactoryDeleter;
00036 
00037 FilterManager::FilterManager()
00038 {
00039   loadFactories();
00040 }
00041 
00042 FilterManager::~FilterManager()
00043 {
00044 }
00045 
00046 FilterManager *FilterManager::self()
00047 {
00048   if ( !mSelf )
00049     filterFactoryDeleter.setObject( mSelf, new FilterManager );
00050 
00051   return mSelf;
00052 }
00053 
00054 Filter *FilterManager::create( const QString &type )
00055 {
00056   FactoryMap::Iterator it = mFactoryMap.find( type );
00057   if (  it == mFactoryMap.end() ) {
00058     kdError() << "Asked for undefined filter type '" << type << "'" << endl;
00059     return 0;
00060   }
00061 
00062   return it.data()->createFilter( 0 );
00063 }
00064 
00065 void FilterManager::loadFactories()
00066 {
00067   const KTrader::OfferList offers = KTrader::self()->query( "KitchenSync/Filter" );
00068 
00069   KLibFactory *factory = 0;
00070   KTrader::OfferList::ConstIterator it( offers.begin() );
00071   for ( ; it != offers.end(); ++it ) {
00072     KService::Ptr ptr = (*it);
00073 
00074     factory = KLibLoader::self()->factory( QFile::encodeName( ptr->library() ) );
00075     if ( !factory )
00076       continue;
00077 
00078     FilterFactory *filterFactory = static_cast<FilterFactory*>( factory );
00079 
00080     if ( filterFactory )
00081       mFactoryMap.insert( ptr->property( "X-KDE-KitchenSyncFilterType" ).toString(), filterFactory );
00082   }
00083 }
KDE Home | KDE Accessibility Home | Description of Access Keys