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
#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
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
QStringList list =
KGlobal::dirs()->
findAllResources(
"data" ,
"kabc/formats/*.desktop",
true,
true );
00060
for ( QStringList::iterator 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
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 }