kabc Library: formatfactory.cpp Source File (kabc Library)

| # kabc Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# formatfactory.cpp

    00001 /*
    00002     This file is part of libkabc.
    00003     Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
    00004
    00005     This library is free software; you can redistribute it and/or
    00006     modify it under the terms of the GNU Library General Public
    00007     License as published by the Free Software Foundation; either
    00008     version 2 of the License, or (at your option) any later version.
    00009
    00010     This library is distributed in the hope that it will be useful,
    00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013     Library General Public License for more details.
    00014
    00015     You should have received a copy of the GNU Library General Public License
    00016     along with this library; see the file COPYING.LIB.  If not, write to
    00017     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00018     Boston, MA 02111-1307, USA.
    00019 */
    00020
    00021 #include <kdebug.h>
    00022 #include <klocale.h>
    00023 #include <ksimpleconfig.h>
    00024 #include <kstandarddirs.h>
    00025 #include <kstaticdeleter.h>
    00026
    00027 #include <qfile.h>
    00028
    00029 #include "vcardformatplugin.h"
    00030
    00031 #include "formatfactory.h"
    00032
    00033 using namespace KABC;
    00034
    00035 FormatFactory *FormatFactory::mSelf = 0;
    00036 static KStaticDeleter<FormatFactory> factoryDeleter;
    00037
    00038 FormatFactory *FormatFactory::self()
    00039 {
    00040   kdDebug(5700) << "FormatFactory::self()" << endl;
    00041
    00042   if ( !mSelf )
    00043     factoryDeleter.setObject( mSelf, new FormatFactory );
    00044
    00045   return mSelf;
    00046 }
    00047
    00048 FormatFactory::FormatFactory()
    00049 {
    00050   mFormatList.setAutoDelete( true );
    00051
    00052   // dummy entry for default format
    00053   FormatInfo *info = new FormatInfo;
    00054   info->library = "<NoLibrary>";
    00055   info->nameLabel = i18n( "vCard" );
    00056   info->descriptionLabel = i18n( "vCard Format" );
    00057   mFormatList.insert( "vcard", info );
    00058
    00059   const QStringList list = KGlobal::dirs()->findAllResources( "data" ,"kabc/formats/*.desktop", true, true );
    00060   for ( QStringList::ConstIterator it = list.begin(); it != list.end(); ++it )
    00061   {
    00062     KSimpleConfig config( *it, true );
    00063
    00064     if ( !config.hasGroup( "Misc" ) || !config.hasGroup( "Plugin" ) )
    00065         continue;
    00066
    00067     info = new FormatInfo;
    00068
    00069     config.setGroup( "Plugin" );
    00070     QString type = config.readEntry( "Type" );
    00071     info->library = config.readEntry( "X-KDE-Library" );
    00072
    00073     config.setGroup( "Misc" );
    00074     info->nameLabel = config.readEntry( "Name" );
    00075     info->descriptionLabel = config.readEntry( "Comment", i18n( "No description available." ) );
    00076
    00077     mFormatList.insert( type, info );
    00078   }
    00079 }
    00080
    00081 FormatFactory::~FormatFactory()
    00082 {
    00083   mFormatList.clear();
    00084 }
    00085
    00086 QStringList FormatFactory::formats()
    00087 {
    00088   QStringList retval;
    00089
    00090   // make sure 'vcard' is the first entry
    00091   retval << "vcard";
    00092
    00093   QDictIterator<FormatInfo> it( mFormatList );
    00094   for ( ; it.current(); ++it )
    00095     if ( it.currentKey() != "vcard" )
    00096       retval << it.currentKey();
    00097
    00098   return retval;
    00099 }
    00100
    00101 FormatInfo *FormatFactory::info( const QString &type )
    00102 {
    00103   if ( type.isEmpty() )
    00104     return 0;
    00105   else
    00106     return mFormatList[ type ];
    00107 }
    00108
    00109 FormatPlugin *FormatFactory::format( const QString& type )
    00110 {
    00111   FormatPlugin *format = 0;
    00112
    00113   if ( type.isEmpty() )
    00114     return 0;
    00115
    00116   if ( type == "vcard" ) {
    00117     format = new VCardFormatPlugin;
    00118     format->setType( type );
    00119     format->setNameLabel( i18n( "vCard" ) );
    00120     format->setDescriptionLabel( i18n( "vCard Format" ) );
    00121     return format;
    00122   }
    00123
    00124   FormatInfo *fi = mFormatList[ type ];
    00125   if (!fi)
    00126       return 0;
    00127   QString libName = fi->library;
    00128
    00129   KLibrary *library = openLibrary( libName );
    00130   if ( !library )
    00131     return 0;
    00132
    00133   void *format_func = library->symbol( "format" );
    00134
    00135   if ( format_func ) {
    00136     format = ((FormatPlugin* (*)())format_func)();
    00137     format->setType( type );
    00138     format->setNameLabel( fi->nameLabel );
    00139     format->setDescriptionLabel( fi->descriptionLabel );
    00140   } else {
    00141     kdDebug( 5700 ) << "'" << libName << "' is not a format plugin." << endl;
    00142     return 0;
    00143   }
    00144
    00145   return format;
    00146 }
    00147
    00148
    00149 KLibrary *FormatFactory::openLibrary( const QString& libName )
    00150 {
    00151   KLibrary *library = 0;
    00152
    00153   QString path = KLibLoader::findLibrary( QFile::encodeName( libName ) );
    00154
    00155   if ( path.isEmpty() ) {
    00156     kdDebug( 5700 ) << "No format plugin library was found!" << endl;
    00157     return 0;
    00158   }
    00159
    00160   library = KLibLoader::self()->library( QFile::encodeName( path ) );
    00161
    00162   if ( !library ) {
    00163     kdDebug( 5700 ) << "Could not load library '" << libName << "'" << endl;
    00164     return 0;
    00165   }
    00166
    00167   return library;
    00168 }

This file is part of the documentation for kabc Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:31:29 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003