formatfactory.cpp Source File (kabc Library)

| # kabc Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | 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
    00026 #include <qfile.h>
    00027
    00028 #include "vcardformatplugin.h"
    00029
    00030 #include "formatfactory.h"
    00031
    00032 using namespace KABC;
    00033
    00034 FormatFactory *FormatFactory::mSelf = 0;
    00035
    00036 FormatFactory *FormatFactory::self()
    00037 {
    00038   kdDebug(5700) << "FormatFactory::self()" << endl;
    00039
    00040   if ( !mSelf ) {
    00041     mSelf = new FormatFactory;
    00042   }
    00043
    00044   return mSelf;
    00045 }
    00046
    00047 FormatFactory::FormatFactory()
    00048 {
    00049   mFormatList.setAutoDelete( true );
    00050
    00051   // dummy entry for default format
    00052   FormatInfo *info = new FormatInfo;
    00053   info->library = "<NoLibrary>";
    00054   info->nameLabel = i18n( "vCard" );
    00055   info->descriptionLabel = i18n( "vCard Format" );
    00056   mFormatList.insert( "vcard", info );
    00057
    00058   QStringList list = KGlobal::dirs()->findAllResources( "data" ,"kabc/formats/*.desktop", true, true );
    00059   for ( QStringList::iterator it = list.begin(); it != list.end(); ++it )
    00060   {
    00061     KSimpleConfig config( *it, true );
    00062
    00063     if ( !config.hasGroup( "Misc" ) || !config.hasGroup( "Plugin" ) )
    00064             continue;
    00065
    00066     info = new FormatInfo;
    00067
    00068     config.setGroup( "Plugin" );
    00069     QString type = config.readEntry( "Type" );
    00070     info->library = config.readEntry( "X-KDE-Library" );
    00071
    00072     config.setGroup( "Misc" );
    00073     info->nameLabel = config.readEntry( "Name" );
    00074     info->descriptionLabel = config.readEntry( "Comment", i18n( "No description available." ) );
    00075
    00076     mFormatList.insert( type, info );
    00077   }
    00078 }
    00079
    00080 FormatFactory::~FormatFactory()
    00081 {
    00082   mFormatList.clear();
    00083 }
    00084
    00085 QStringList FormatFactory::formats()
    00086 {
    00087   QStringList retval;
    00088
    00089   // make sure 'vcard' is the first entry
    00090   retval << "vcard";
    00091
    00092   QDictIterator<FormatInfo> it( mFormatList );
    00093   for ( ; it.current(); ++it )
    00094     if ( it.currentKey() != "vcard" )
    00095       retval << it.currentKey();
    00096
    00097   return retval;
    00098 }
    00099
    00100 FormatInfo *FormatFactory::info( const QString &type )
    00101 {
    00102   if ( type.isEmpty() )
    00103     return 0;
    00104   else
    00105     return mFormatList[ type ];
    00106 }
    00107
    00108 FormatPlugin *FormatFactory::format( const QString& type )
    00109 {
    00110   FormatPlugin *format = 0;
    00111
    00112   if ( type.isEmpty() )
    00113     return 0;
    00114
    00115   if ( type == "vcard" ) {
    00116     format = new VCardFormatPlugin;
    00117     format->setType( type );
    00118     format->setNameLabel( i18n( "vCard" ) );
    00119     format->setDescriptionLabel( i18n( "vCard Format" ) );
    00120     return format;
    00121   }
    00122
    00123   FormatInfo *fi = mFormatList[ type ];
    00124   if (!fi)
    00125           return 0;
    00126   QString libName = fi->library;
    00127
    00128   KLibrary *library = openLibrary( libName );
    00129   if ( !library )
    00130     return 0;
    00131
    00132   void *format_func = library->symbol( "format" );
    00133
    00134   if ( format_func ) {
    00135     format = ((FormatPlugin* (*)())format_func)();
    00136     format->setType( type );
    00137     format->setNameLabel( fi->nameLabel );
    00138     format->setDescriptionLabel( fi->descriptionLabel );
    00139   } else {
    00140     kdDebug( 5700 ) << "'" << libName << "' is not a format plugin." << endl;
    00141     return 0;
    00142   }
    00143
    00144   return format;
    00145 }
    00146
    00147
    00148 KLibrary *FormatFactory::openLibrary( const QString& libName )
    00149 {
    00150   KLibrary *library = 0;
    00151
    00152   QString path = KLibLoader::findLibrary( QFile::encodeName( libName ) );
    00153
    00154   if ( path.isEmpty() ) {
    00155     kdDebug( 5700 ) << "No format plugin library was found!" << endl;
    00156     return 0;
    00157   }
    00158
    00159   library = KLibLoader::self()->library( QFile::encodeName( path ) );
    00160
    00161   if ( !library ) {
    00162     kdDebug( 5700 ) << "Could not load library '" << libName << "'" << endl;
    00163     return 0;
    00164   }
    00165
    00166   return library;
    00167 }

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:29:20 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001