• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

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 
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   // dummy entry for default format
00083   FormatInfo info;
00084   info.library = "<NoLibrary>";
00085   info.nameLabel = i18n( "vCard" );
00086   info.descriptionLabel = i18n( "vCard Format" );
00087   d->mFormatList.insert( "vcard", info );
00088 
00089   const QStringList list =
00090     KGlobal::dirs()->findAllResources( "data","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   // make sure 'vcard' is the first entry
00122   retval << "vcard";
00123 
00124   QHashIterator<QString, FormatInfo> it( d->mFormatList );
00125   while ( it.hasNext() ) {
00126     it.next();
00127     if ( it.key() != "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 == "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 }

kabc

Skip menu "kabc"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  • kabc
  • kblog
  • kcal
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal