kabc
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., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 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 #include <klibrary.h> 00030 00031 #include <QtCore/QCoreApplication> 00032 #include <QtCore/QFile> 00033 00034 using namespace KABC; 00035 00036 class FormatFactory::Private 00037 { 00038 public: 00039 ~Private() { 00040 mFormatList.clear(); 00041 qRemovePostRoutine( cleanupFormatFactory ); 00042 } 00043 00044 KLibrary *openLibrary( const QString &libName ); 00045 00046 QHash<QString, FormatInfo> mFormatList; 00047 00048 static FormatFactory *sSelf; 00049 static void cleanupFormatFactory() 00050 { 00051 delete sSelf; 00052 sSelf = 0; 00053 } 00054 }; 00055 FormatFactory *FormatFactory::Private::sSelf = 0; 00056 00057 KLibrary *FormatFactory::Private::openLibrary( const QString &libName ) 00058 { 00059 KLibrary *library = new KLibrary( libName ); 00060 if ( library->load() ) { 00061 return library; 00062 } 00063 kDebug() << library->errorString(); 00064 delete library; 00065 return 0; 00066 } 00067 00068 FormatFactory *FormatFactory::self() 00069 { 00070 kDebug(); 00071 00072 static Private p; 00073 if ( !p.sSelf ) { 00074 p.sSelf = new FormatFactory; 00075 qAddPostRoutine( Private::cleanupFormatFactory ); 00076 } 00077 return p.sSelf; 00078 } 00079 00080 FormatFactory::FormatFactory() 00081 : d( new Private ) 00082 { 00083 // dummy entry for default format 00084 FormatInfo info; 00085 info.library = QLatin1String( "<NoLibrary>" ); 00086 info.nameLabel = i18n( "vCard" ); 00087 info.descriptionLabel = i18n( "vCard Format" ); 00088 d->mFormatList.insert( QLatin1String( "vcard" ), info ); 00089 00090 const QStringList list = 00091 KGlobal::dirs()->findAllResources( "data", QLatin1String( "kabc/formats/*.desktop" ), 00092 KStandardDirs::Recursive | 00093 KStandardDirs::NoDuplicates ); 00094 for ( QStringList::ConstIterator it = list.begin(); it != list.end(); ++it ) { 00095 KConfig config( *it, KConfig::SimpleConfig ); 00096 00097 if ( !config.hasGroup( "Misc" ) || !config.hasGroup( "Plugin" ) ) { 00098 continue; 00099 } 00100 00101 KConfigGroup group = config.group( "Plugin" ); 00102 QString type = group.readEntry( "Type" ); 00103 info.library = group.readEntry( "X-KDE-Library" ); 00104 00105 group = config.group( "Misc" ); 00106 info.nameLabel = group.readEntry( "Name" ); 00107 info.descriptionLabel = group.readEntry( "Comment", i18n( "No description available." ) ); 00108 00109 d->mFormatList.insert( type, info ); 00110 } 00111 } 00112 00113 FormatFactory::~FormatFactory() 00114 { 00115 delete d; 00116 } 00117 00118 QStringList FormatFactory::formats() 00119 { 00120 QStringList retval; 00121 00122 // make sure 'vcard' is the first entry 00123 retval << QLatin1String( "vcard" ); 00124 00125 QHashIterator<QString, FormatInfo> it( d->mFormatList ); 00126 while ( it.hasNext() ) { 00127 it.next(); 00128 if ( it.key() != QLatin1String( "vcard" ) ) { 00129 retval << it.key(); 00130 } 00131 } 00132 00133 return retval; 00134 } 00135 00136 FormatInfo FormatFactory::info( const QString &type ) const 00137 { 00138 if ( type.isEmpty() || !d->mFormatList.contains( type ) ) { 00139 return FormatInfo(); 00140 } else { 00141 return d->mFormatList[ type ]; 00142 } 00143 } 00144 00145 Format *FormatFactory::format( const QString &type ) 00146 { 00147 Format *format = 0; 00148 00149 if ( type.isEmpty() ) { 00150 return 0; 00151 } 00152 00153 if ( type == QLatin1String( "vcard" ) ) { 00154 format = new VCardFormat; 00155 format->setType( type ); 00156 format->setNameLabel( i18n( "vCard" ) ); 00157 format->setDescriptionLabel( i18n( "vCard Format" ) ); 00158 return format; 00159 } 00160 00161 if ( !d->mFormatList.contains( type ) ) { 00162 return 0; 00163 } 00164 00165 FormatInfo fi = d->mFormatList[ type ]; 00166 QString libName = fi.library; 00167 00168 KLibrary *library = d->openLibrary( libName ); 00169 if ( !library ) { 00170 return 0; 00171 } 00172 00173 KLibrary::void_function_ptr format_func = library->resolveFunction( "format" ); 00174 00175 if ( format_func ) { 00176 format = ( (Format *(*)())format_func )(); 00177 format->setType( type ); 00178 format->setNameLabel( fi.nameLabel ); 00179 format->setDescriptionLabel( fi.descriptionLabel ); 00180 } else { 00181 kDebug() << "'" << libName << "' is not a format plugin."; 00182 return 0; 00183 } 00184 00185 return format; 00186 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 14 2012 05:09:41 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 14 2012 05:09:41 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.