• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.8.3 API Reference
  • KDE Home
  • 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_p.h"
00021 
00022 #include <kconfiggroup.h>
00023 #include <kdebug.h>
00024 #include <kglobal.h>
00025 #include <klocale.h>
00026 #include <kstandarddirs.h>
00027 #include <KPluginLoader>
00028 
00029 #include <QtCore/QDebug>
00030 
00031 #ifdef Q_OS_WINCE
00032 #include <KMessageBox>
00033 #endif
00034 
00035 using namespace Akonadi;
00036 
00037 PluginMetaData::PluginMetaData()
00038 {
00039 }
00040 
00041 PluginMetaData::PluginMetaData( const QString & lib, const QString & name, const QString & comment, const QString & cname )
00042   : library( lib ), nameLabel( name ),
00043     descriptionLabel( comment ),
00044     className(cname), loaded( false )
00045 {
00046 }
00047 
00048 
00049 PluginLoader* PluginLoader::mSelf = 0;
00050 
00051 PluginLoader::PluginLoader()
00052 {
00053   scan();
00054 }
00055 
00056 PluginLoader::~PluginLoader()
00057 {
00058   qDeleteAll( mPluginLoaders );
00059   mPluginLoaders.clear();
00060 }
00061 
00062 PluginLoader* PluginLoader::self()
00063 {
00064   if ( !mSelf )
00065     mSelf = new PluginLoader();
00066 
00067   return mSelf;
00068 }
00069 
00070 QStringList PluginLoader::names() const
00071 {
00072   return mPluginInfos.keys();
00073 }
00074 
00075 QObject* PluginLoader::createForName( const QString & name )
00076 {
00077   if ( !mPluginInfos.contains( name ) ) {
00078     kWarning( 5300 ) << "plugin name \"" << name << "\" is unknown to the plugin loader." << endl;
00079     return 0;
00080   }
00081 
00082   PluginMetaData &info = mPluginInfos[ name ];
00083 
00084   //First try to load it staticly
00085   foreach (QObject *plugin, QPluginLoader::staticInstances()) {
00086     if(QLatin1String(plugin->metaObject()->className()) == info.className) {
00087       info.loaded = true;
00088       return plugin;
00089       break;
00090     }
00091   }
00092 
00093   if ( !info.loaded ) {
00094     KPluginLoader* loader = new KPluginLoader( info.library );
00095     if ( loader->fileName().isEmpty() ) {
00096       kWarning( 5300 ) << loader->errorString();
00097       delete loader;
00098       return 0;
00099     }
00100 
00101     mPluginLoaders.insert( name, loader );
00102     info.loaded = true;
00103   }
00104 
00105   QPluginLoader *loader = mPluginLoaders.value( name );
00106   Q_ASSERT(loader);
00107 
00108   QObject *object = loader->instance();
00109   if ( !object ) {
00110 #ifdef Q_OS_WINCE
00111     //Maybe filter out the default plugins, they should be found but...
00112     //if ( !name.endsWith( QLatin1String( "@default" ) ) ) {
00113       QString errMessage =
00114         i18n( "Plugin \"%1\" is not builtin static, "
00115               "please specify this information in the bug report.", info.className );
00116       KMessageBox::critical( 0, i18n( "Plugin Not Built Statically" ), errMessage );
00117     //}
00118 #endif
00119     kWarning( 5300 ) << "unable to load plugin for plugin name \"" << name << "\"." << endl;
00120     kWarning( 5300 ) << "Error was:\"" << loader->errorString() << "\"." << endl;
00121     return 0;
00122   }
00123 
00124   return object;
00125 }
00126 
00127 PluginMetaData PluginLoader::infoForName( const QString & name ) const
00128 {
00129   if ( !mPluginInfos.contains( name ) )
00130     return PluginMetaData();
00131 
00132   return mPluginInfos.value( name );
00133 }
00134 
00135 void PluginLoader::scan()
00136 {
00137   const QStringList list = KGlobal::dirs()->findAllResources( "data", QLatin1String( "akonadi/plugins/serializer/*.desktop" ),
00138                                                               KStandardDirs::Recursive | KStandardDirs::NoDuplicates );
00139   for ( int i = 0; i < list.count(); ++i ) {
00140     const QString entry = list.at( i );
00141 
00142     KConfig config( entry, KConfig::SimpleConfig );
00143     if ( config.hasGroup( "Misc" ) && config.hasGroup( "Plugin" ) ) {
00144       KConfigGroup group( &config, "Plugin" );
00145 
00146       const QString type = group.readEntry( "Type" ).toLower();
00147       if ( type.isEmpty() ) {
00148         kWarning( 5300 ) << "missing or empty [Plugin]Type value in \"" << entry << "\" - skipping" << endl;
00149         continue;
00150       }
00151 
00152       // read Class entry as a list so that types like QPair<A,B> are
00153       // properly escaped and don't end up being split into QPair<A
00154       // and B>.
00155       const QStringList classes = group.readXdgListEntry( "X-Akonadi-Class" );
00156       if ( classes.isEmpty() ) {
00157         kWarning( 5300 ) << "missing or empty [Plugin]X-Akonadi-Class value in \"" << entry << "\" - skipping" << endl;
00158         continue;
00159       }
00160 
00161       const QString library = group.readEntry( "X-KDE-Library" );
00162       if ( library.isEmpty() ) {
00163         kWarning( 5300 ) << "missing or empty [Plugin]X-KDE-Library value in \"" << entry << "\" - skipping" << endl;
00164         continue;
00165       }
00166 
00167       KConfigGroup group2( &config, "Misc" );
00168 
00169       QString name = group2.readEntry( "Name" );
00170       if ( name.isEmpty() ) {
00171         kWarning( 5300 ) << "missing or empty [Misc]Name value in \"" << entry << "\" - inserting default name" << endl;
00172         name = i18n( "Unnamed plugin" );
00173       }
00174 
00175       QString comment = group2.readEntry( "Comment" );
00176       if ( comment.isEmpty() ) {
00177         kWarning( 5300 ) << "missing or empty [Misc]Comment value in \"" << entry << "\" - inserting default name" << endl;
00178         comment = i18n( "No description available" );
00179       }
00180 
00181       QString cname      = group.readEntry( "X-KDE-ClassName" );
00182       if ( cname.isEmpty() ) {
00183         kWarning( 5300 ) << "missing or empty X-KDE-ClassName value in \"" << entry << "\"" << endl;
00184       }
00185 
00186       const QStringList mimeTypes = type.split( QLatin1Char( ',' ), QString::SkipEmptyParts );
00187 
00188       kDebug( 5300 ) << "registering Desktop file" << entry << "for" << mimeTypes << '@' << classes;
00189       Q_FOREACH( const QString & mimeType, mimeTypes )
00190         Q_FOREACH( const QString & classType, classes )
00191           mPluginInfos.insert( mimeType + QLatin1Char('@') + classType, PluginMetaData( library, name, comment, cname ) );
00192 
00193     } else {
00194       kWarning( 5300 ) << "Desktop file \"" << entry << "\" doesn't seem to describe a plugin " << "(misses Misc and/or Plugin group)" << endl;
00195     }
00196   }
00197 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Tue May 8 2012 00:00:45 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

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

kdepimlibs-4.8.3 API Reference

Skip menu "kdepimlibs-4.8.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal