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

akonadi

pluginloader.cpp

00001 /*  -*- c++ -*-
00002     Copyright (c) 2008 Tobias Koenig <tokoe@kde.org>
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017     Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "pluginloader.h"
00021 
00022 #include <kconfiggroup.h>
00023 #include <kdebug.h>
00024 #include <kglobal.h>
00025 #include <klibloader.h>
00026 #include <klocale.h>
00027 #include <kstandarddirs.h>
00028 
00029 #include <QtCore/QDebug>
00030 #include <QtCore/QPluginLoader>
00031 
00032 using namespace Akonadi;
00033 
00034 PluginMetaData::PluginMetaData()
00035 {
00036 }
00037 
00038 PluginMetaData::PluginMetaData( const QString & lib, const QString & name, const QString & comment )
00039   : library( lib ), nameLabel( name ),
00040     descriptionLabel( comment ), loaded( false )
00041 {
00042 }
00043 
00044 
00045 PluginLoader* PluginLoader::mSelf = 0;
00046 
00047 PluginLoader::PluginLoader()
00048 {
00049   scan();
00050 }
00051 
00052 PluginLoader::~PluginLoader()
00053 {
00054   qDeleteAll( mPluginLoaders );
00055   mPluginLoaders.clear();
00056 }
00057 
00058 PluginLoader* PluginLoader::self()
00059 {
00060   if ( !mSelf )
00061     mSelf = new PluginLoader();
00062 
00063   return mSelf;
00064 }
00065 
00066 QStringList PluginLoader::types() const
00067 {
00068   return mPluginInfos.keys();
00069 }
00070 
00071 QObject* PluginLoader::createForName( const QString &type )
00072 {
00073   if ( !mPluginInfos.contains( type ) ) {
00074     kWarning( 5300 ) << "plugin type \"" << type << "\" is unknown to the plugin loader." << endl;
00075     return 0;
00076   }
00077 
00078   PluginMetaData &info = mPluginInfos[ type ];
00079   if ( !info.loaded ) {
00080     const QString path = KLibLoader::findLibrary( info.library );
00081     if ( path.isEmpty() ) {
00082       kWarning( 5300 ) << "unable to find library for plugin type \"" << type << "\"." << endl;
00083       return 0;
00084     }
00085 
00086     mPluginLoaders.insert( type, new QPluginLoader( path ) );
00087     info.loaded = true;
00088   }
00089 
00090   QPluginLoader *loader = mPluginLoaders[ type ];
00091 
00092   QObject *object = loader->instance();
00093   if ( !object ) {
00094     kWarning( 5300 ) << "unable to load plugin for plugin type \"" << type << "\"." << endl;
00095     return 0;
00096   }
00097 
00098   return object;
00099 }
00100 
00101 PluginMetaData PluginLoader::infoForName( const QString & type ) const
00102 {
00103   if ( !mPluginInfos.contains( type ) )
00104     return PluginMetaData();
00105 
00106   return mPluginInfos.value( type );
00107 }
00108 
00109 void PluginLoader::scan()
00110 {
00111   const QStringList list = KGlobal::dirs()->findAllResources( "data", QLatin1String( "akonadi/plugins/serializer/*.desktop" ),
00112                                                               KStandardDirs::Recursive | KStandardDirs::NoDuplicates );
00113   for ( int i = 0; i < list.count(); ++i ) {
00114     const QString entry = list.at( i );
00115 
00116     KConfig config( entry, KConfig::SimpleConfig );
00117     if ( config.hasGroup( "Misc" ) && config.hasGroup( "Plugin" ) ) {
00118       KConfigGroup group( &config, "Plugin" );
00119 
00120       const QString type = group.readEntry( "Type" ).toLower();
00121       if ( type.isEmpty() ) {
00122         kWarning( 5300 ) << "missing or empty [Plugin]Type value in \"" << entry << "\" - skipping" << endl;
00123         continue;
00124       }
00125 
00126       const QString library = group.readEntry( "X-KDE-Library" );
00127       if ( library.isEmpty() ) {
00128         kWarning( 5300 ) << "missing or empty [Plugin]X-KDE-Library value in \"" << entry << "\" - skipping" << endl;
00129         continue;
00130       }
00131 
00132       KConfigGroup group2( &config, "Misc" );
00133 
00134       QString name = group2.readEntry( "Name" );
00135       if ( name.isEmpty() ) {
00136         kWarning( 5300 ) << "missing or empty [Misc]Name value in \"" << entry << "\" - inserting default name" << endl;
00137         name = i18n("Unnamed plugin");
00138       }
00139 
00140       QString comment = group2.readEntry( "Comment" );
00141       if ( comment.isEmpty() ) {
00142         kWarning( 5300 ) << "missing or empty [Misc]Comment value in \"" << entry << "\" - inserting default name" << endl;
00143         comment = i18n("No description available");
00144       }
00145 
00146       if ( type.contains( QLatin1Char( ',' ) ) ) {
00147         const QStringList subTypes = type.split( QLatin1Char( ',' ), QString::SkipEmptyParts );
00148         for ( int j = 0; j < subTypes.count(); ++j )
00149           mPluginInfos.insert( subTypes.at( j ), PluginMetaData( library, name, comment ) );
00150       } else {
00151         mPluginInfos.insert( type, PluginMetaData( library, name, comment ) );
00152       }
00153     } else {
00154       kWarning( 5300 ) << "Desktop file \"" << entry << "\" doesn't seem to describe a plugin " << "(misses Misc and/or Plugin group)" << endl;
00155     }
00156   }
00157 }

akonadi

Skip menu "akonadi"
  • Main Page
  • Modules
  • 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
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.4
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