• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.13.3 API Reference
  • KDE Home
  • Contact Us
 

akonadi

  • akonadi
pluginloader.cpp
1 /* -*- c++ -*-
2  Copyright (c) 2008 Tobias Koenig <tokoe@kde.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "pluginloader_p.h"
21 
22 #include <kconfiggroup.h>
23 #include <kdebug.h>
24 #include <kglobal.h>
25 #include <klocale.h>
26 #include <klocalizedstring.h>
27 #include <kstandarddirs.h>
28 #include <KPluginLoader>
29 
30 #include <QtCore/QDebug>
31 
32 using namespace Akonadi;
33 
34 PluginMetaData::PluginMetaData()
35 {
36 }
37 
38 PluginMetaData::PluginMetaData( const QString & lib, const QString & name, const QString & comment, const QString & cname )
39  : library( lib ), nameLabel( name ),
40  descriptionLabel( comment ),
41  className(cname), loaded( false )
42 {
43 }
44 
45 PluginLoader* PluginLoader::mSelf = 0;
46 
47 PluginLoader::PluginLoader()
48 {
49  scan();
50 }
51 
52 PluginLoader::~PluginLoader()
53 {
54  qDeleteAll( mPluginLoaders );
55  mPluginLoaders.clear();
56 }
57 
58 PluginLoader* PluginLoader::self()
59 {
60  if ( !mSelf )
61  mSelf = new PluginLoader();
62 
63  return mSelf;
64 }
65 
66 QStringList PluginLoader::names() const
67 {
68  return mPluginInfos.keys();
69 }
70 
71 QObject* PluginLoader::createForName( const QString & name )
72 {
73  if ( !mPluginInfos.contains( name ) ) {
74  kWarning( 5300 ) << "plugin name \"" << name << "\" is unknown to the plugin loader." << endl;
75  return 0;
76  }
77 
78  PluginMetaData &info = mPluginInfos[ name ];
79 
80  //First try to load it staticly
81  foreach (QObject *plugin, QPluginLoader::staticInstances()) {
82  if (QLatin1String(plugin->metaObject()->className()) == info.className) {
83  info.loaded = true;
84  return plugin;
85  break;
86  }
87  }
88 
89  if ( !info.loaded ) {
90  KPluginLoader* loader = new KPluginLoader( info.library );
91  if ( loader->fileName().isEmpty() ) {
92  kWarning( 5300 ) << loader->errorString();
93  delete loader;
94  return 0;
95  }
96 
97  mPluginLoaders.insert( name, loader );
98  info.loaded = true;
99  }
100 
101  QPluginLoader *loader = mPluginLoaders.value( name );
102  Q_ASSERT(loader);
103 
104  QObject *object = loader->instance();
105  if ( !object ) {
106  kWarning( 5300 ) << "unable to load plugin for plugin name \"" << name << "\"." << endl;
107  kWarning( 5300 ) << "Error was:\"" << loader->errorString() << "\"." << endl;
108  return 0;
109  }
110 
111  return object;
112 }
113 
114 PluginMetaData PluginLoader::infoForName( const QString & name ) const
115 {
116  if ( !mPluginInfos.contains( name ) )
117  return PluginMetaData();
118 
119  return mPluginInfos.value( name );
120 }
121 
122 void PluginLoader::scan()
123 {
124  const QStringList list = KGlobal::dirs()->findAllResources( "data", QLatin1String( "akonadi/plugins/serializer/*.desktop" ),
125  KStandardDirs::Recursive | KStandardDirs::NoDuplicates );
126  for ( int i = 0; i < list.count(); ++i ) {
127  const QString entry = list.at( i );
128 
129  KConfig config( entry, KConfig::SimpleConfig );
130  if ( config.hasGroup( "Misc" ) && config.hasGroup( "Plugin" ) ) {
131  KConfigGroup group( &config, "Plugin" );
132 
133  const QString type = group.readEntry( "Type" ).toLower();
134  if ( type.isEmpty() ) {
135  kWarning( 5300 ) << "missing or empty [Plugin]Type value in \"" << entry << "\" - skipping" << endl;
136  continue;
137  }
138 
139  // read Class entry as a list so that types like QPair<A,B> are
140  // properly escaped and don't end up being split into QPair<A
141  // and B>.
142  const QStringList classes = group.readXdgListEntry( "X-Akonadi-Class" );
143  if ( classes.isEmpty() ) {
144  kWarning( 5300 ) << "missing or empty [Plugin]X-Akonadi-Class value in \"" << entry << "\" - skipping" << endl;
145  continue;
146  }
147 
148  const QString library = group.readEntry( "X-KDE-Library" );
149  if ( library.isEmpty() ) {
150  kWarning( 5300 ) << "missing or empty [Plugin]X-KDE-Library value in \"" << entry << "\" - skipping" << endl;
151  continue;
152  }
153 
154  KConfigGroup group2( &config, "Misc" );
155 
156  QString name = group2.readEntry( "Name" );
157  if ( name.isEmpty() ) {
158  kWarning( 5300 ) << "missing or empty [Misc]Name value in \"" << entry << "\" - inserting default name" << endl;
159  name = i18n( "Unnamed plugin" );
160  }
161 
162  QString comment = group2.readEntry( "Comment" );
163  if ( comment.isEmpty() ) {
164  kWarning( 5300 ) << "missing or empty [Misc]Comment value in \"" << entry << "\" - inserting default name" << endl;
165  comment = i18n( "No description available" );
166  }
167 
168  QString cname = group.readEntry( "X-KDE-ClassName" );
169  if ( cname.isEmpty() ) {
170  kWarning( 5300 ) << "missing or empty X-KDE-ClassName value in \"" << entry << "\"" << endl;
171  }
172 
173  const QStringList mimeTypes = type.split( QLatin1Char( ',' ), QString::SkipEmptyParts );
174 
175  kDebug( 5300 ) << "registering Desktop file" << entry << "for" << mimeTypes << '@' << classes;
176  Q_FOREACH( const QString & mimeType, mimeTypes )
177  Q_FOREACH( const QString & classType, classes )
178  mPluginInfos.insert( mimeType + QLatin1Char('@') + classType, PluginMetaData( library, name, comment, cname ) );
179 
180  } else {
181  kWarning( 5300 ) << "Desktop file \"" << entry << "\" doesn't seem to describe a plugin " << "(misses Misc and/or Plugin group)" << endl;
182  }
183  }
184 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Mon Jul 21 2014 08:03:54 by doxygen 1.8.6 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.13.3 API Reference

Skip menu "kdepimlibs-4.13.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • 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