00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "formatfactory.h"
00022 #include "vcardformat.h"
00023
00024 #include <kdebug.h>
00025 #include <klocale.h>
00026 #include <kconfig.h>
00027 #include <kstandarddirs.h>
00028 #include <kconfiggroup.h>
00029
00030 #include <QtCore/QCoreApplication>
00031 #include <QtCore/QFile>
00032
00033 using namespace KABC;
00034
00035 class FormatFactory::Private
00036 {
00037 public:
00038 ~Private() {
00039 mFormatList.clear();
00040 qRemovePostRoutine( cleanupFormatFactory );
00041 }
00042
00043 KLibrary *openLibrary( const QString &libName );
00044
00045 QHash<QString, FormatInfo> mFormatList;
00046
00047 static FormatFactory *sSelf;
00048 static void cleanupFormatFactory()
00049 {
00050 delete sSelf;
00051 sSelf = 0;
00052 }
00053 };
00054 FormatFactory *FormatFactory::Private::sSelf = 0;
00055
00056 KLibrary *FormatFactory::Private::openLibrary( const QString &libName )
00057 {
00058 KLibrary *library = new KLibrary( libName );
00059 if ( library->load() ) {
00060 return library;
00061 }
00062 kDebug() << library->errorString();
00063 delete library;
00064 return 0;
00065 }
00066
00067 FormatFactory *FormatFactory::self()
00068 {
00069 kDebug();
00070
00071 static Private p;
00072 if ( !p.sSelf ) {
00073 p.sSelf = new FormatFactory;
00074 qAddPostRoutine( Private::cleanupFormatFactory );
00075 }
00076 return p.sSelf;
00077 }
00078
00079 FormatFactory::FormatFactory()
00080 : d( new Private )
00081 {
00082
00083 FormatInfo info;
00084 info.library = QLatin1String( "<NoLibrary>" );
00085 info.nameLabel = i18n( "vCard" );
00086 info.descriptionLabel = i18n( "vCard Format" );
00087 d->mFormatList.insert( QLatin1String( "vcard" ), info );
00088
00089 const QStringList list =
00090 KGlobal::dirs()->findAllResources( "data", QLatin1String( "kabc/formats/*.desktop" ),
00091 KStandardDirs::Recursive |
00092 KStandardDirs::NoDuplicates );
00093 for ( QStringList::ConstIterator it = list.begin(); it != list.end(); ++it ) {
00094 KConfig config( *it, KConfig::SimpleConfig );
00095
00096 if ( !config.hasGroup( "Misc" ) || !config.hasGroup( "Plugin" ) ) {
00097 continue;
00098 }
00099
00100 KConfigGroup group = config.group( "Plugin" );
00101 QString type = group.readEntry( "Type" );
00102 info.library = group.readEntry( "X-KDE-Library" );
00103
00104 group = config.group( "Misc" );
00105 info.nameLabel = group.readEntry( "Name" );
00106 info.descriptionLabel = group.readEntry( "Comment", i18n( "No description available." ) );
00107
00108 d->mFormatList.insert( type, info );
00109 }
00110 }
00111
00112 FormatFactory::~FormatFactory()
00113 {
00114 delete d;
00115 }
00116
00117 QStringList FormatFactory::formats()
00118 {
00119 QStringList retval;
00120
00121
00122 retval << QLatin1String( "vcard" );
00123
00124 QHashIterator<QString, FormatInfo> it( d->mFormatList );
00125 while ( it.hasNext() ) {
00126 it.next();
00127 if ( it.key() != QLatin1String( "vcard" ) ) {
00128 retval << it.key();
00129 }
00130 }
00131
00132 return retval;
00133 }
00134
00135 FormatInfo FormatFactory::info( const QString &type ) const
00136 {
00137 if ( type.isEmpty() || !d->mFormatList.contains( type ) ) {
00138 return FormatInfo();
00139 } else {
00140 return d->mFormatList[ type ];
00141 }
00142 }
00143
00144 Format *FormatFactory::format( const QString &type )
00145 {
00146 Format *format = 0;
00147
00148 if ( type.isEmpty() ) {
00149 return 0;
00150 }
00151
00152 if ( type == QLatin1String( "vcard" ) ) {
00153 format = new VCardFormat;
00154 format->setType( type );
00155 format->setNameLabel( i18n( "vCard" ) );
00156 format->setDescriptionLabel( i18n( "vCard Format" ) );
00157 return format;
00158 }
00159
00160 if ( !d->mFormatList.contains( type ) ) {
00161 return 0;
00162 }
00163
00164 FormatInfo fi = d->mFormatList[ type ];
00165 QString libName = fi.library;
00166
00167 KLibrary *library = d->openLibrary( libName );
00168 if ( !library ) {
00169 return 0;
00170 }
00171
00172 KLibrary::void_function_ptr format_func = library->resolveFunction( "format" );
00173
00174 if ( format_func ) {
00175 format = ( (Format *(*)())format_func )();
00176 format->setType( type );
00177 format->setNameLabel( fi.nameLabel );
00178 format->setDescriptionLabel( fi.descriptionLabel );
00179 } else {
00180 kDebug() << "'" << libName << "' is not a format plugin.";
00181 return 0;
00182 }
00183
00184 return format;
00185 }