formatfactory.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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
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.