• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.11.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 #ifdef Q_OS_WINCE
33 #include <KMessageBox>
34 #endif
35 
36 using namespace Akonadi;
37 
38 PluginMetaData::PluginMetaData()
39 {
40 }
41 
42 PluginMetaData::PluginMetaData( const QString & lib, const QString & name, const QString & comment, const QString & cname )
43  : library( lib ), nameLabel( name ),
44  descriptionLabel( comment ),
45  className(cname), loaded( false )
46 {
47 }
48 
49 
50 PluginLoader* PluginLoader::mSelf = 0;
51 
52 PluginLoader::PluginLoader()
53 {
54  scan();
55 }
56 
57 PluginLoader::~PluginLoader()
58 {
59  qDeleteAll( mPluginLoaders );
60  mPluginLoaders.clear();
61 }
62 
63 PluginLoader* PluginLoader::self()
64 {
65  if ( !mSelf )
66  mSelf = new PluginLoader();
67 
68  return mSelf;
69 }
70 
71 QStringList PluginLoader::names() const
72 {
73  return mPluginInfos.keys();
74 }
75 
76 QObject* PluginLoader::createForName( const QString & name )
77 {
78  if ( !mPluginInfos.contains( name ) ) {
79  kWarning( 5300 ) << "plugin name \"" << name << "\" is unknown to the plugin loader." << endl;
80  return 0;
81  }
82 
83  PluginMetaData &info = mPluginInfos[ name ];
84 
85  //First try to load it staticly
86  foreach (QObject *plugin, QPluginLoader::staticInstances()) {
87  if(QLatin1String(plugin->metaObject()->className()) == info.className) {
88  info.loaded = true;
89  return plugin;
90  break;
91  }
92  }
93 
94  if ( !info.loaded ) {
95  KPluginLoader* loader = new KPluginLoader( info.library );
96  if ( loader->fileName().isEmpty() ) {
97  kWarning( 5300 ) << loader->errorString();
98  delete loader;
99  return 0;
100  }
101 
102  mPluginLoaders.insert( name, loader );
103  info.loaded = true;
104  }
105 
106  QPluginLoader *loader = mPluginLoaders.value( name );
107  Q_ASSERT(loader);
108 
109  QObject *object = loader->instance();
110  if ( !object ) {
111 #ifdef Q_OS_WINCE
112  //Maybe filter out the default plugins, they should be found but...
113  //if ( !name.endsWith( QLatin1String( "@default" ) ) ) {
114  QString errMessage =
115  i18n( "Plugin \"%1\" is not builtin static, "
116  "please specify this information in the bug report.", info.className );
117  KMessageBox::critical( 0, i18n( "Plugin Not Built Statically" ), errMessage );
118  //}
119 #endif
120  kWarning( 5300 ) << "unable to load plugin for plugin name \"" << name << "\"." << endl;
121  kWarning( 5300 ) << "Error was:\"" << loader->errorString() << "\"." << endl;
122  return 0;
123  }
124 
125  return object;
126 }
127 
128 PluginMetaData PluginLoader::infoForName( const QString & name ) const
129 {
130  if ( !mPluginInfos.contains( name ) )
131  return PluginMetaData();
132 
133  return mPluginInfos.value( name );
134 }
135 
136 void PluginLoader::scan()
137 {
138  const QStringList list = KGlobal::dirs()->findAllResources( "data", QLatin1String( "akonadi/plugins/serializer/*.desktop" ),
139  KStandardDirs::Recursive | KStandardDirs::NoDuplicates );
140  for ( int i = 0; i < list.count(); ++i ) {
141  const QString entry = list.at( i );
142 
143  KConfig config( entry, KConfig::SimpleConfig );
144  if ( config.hasGroup( "Misc" ) && config.hasGroup( "Plugin" ) ) {
145  KConfigGroup group( &config, "Plugin" );
146 
147  const QString type = group.readEntry( "Type" ).toLower();
148  if ( type.isEmpty() ) {
149  kWarning( 5300 ) << "missing or empty [Plugin]Type value in \"" << entry << "\" - skipping" << endl;
150  continue;
151  }
152 
153  // read Class entry as a list so that types like QPair<A,B> are
154  // properly escaped and don't end up being split into QPair<A
155  // and B>.
156  const QStringList classes = group.readXdgListEntry( "X-Akonadi-Class" );
157  if ( classes.isEmpty() ) {
158  kWarning( 5300 ) << "missing or empty [Plugin]X-Akonadi-Class value in \"" << entry << "\" - skipping" << endl;
159  continue;
160  }
161 
162  const QString library = group.readEntry( "X-KDE-Library" );
163  if ( library.isEmpty() ) {
164  kWarning( 5300 ) << "missing or empty [Plugin]X-KDE-Library value in \"" << entry << "\" - skipping" << endl;
165  continue;
166  }
167 
168  KConfigGroup group2( &config, "Misc" );
169 
170  QString name = group2.readEntry( "Name" );
171  if ( name.isEmpty() ) {
172  kWarning( 5300 ) << "missing or empty [Misc]Name value in \"" << entry << "\" - inserting default name" << endl;
173  name = i18n( "Unnamed plugin" );
174  }
175 
176  QString comment = group2.readEntry( "Comment" );
177  if ( comment.isEmpty() ) {
178  kWarning( 5300 ) << "missing or empty [Misc]Comment value in \"" << entry << "\" - inserting default name" << endl;
179  comment = i18n( "No description available" );
180  }
181 
182  QString cname = group.readEntry( "X-KDE-ClassName" );
183  if ( cname.isEmpty() ) {
184  kWarning( 5300 ) << "missing or empty X-KDE-ClassName value in \"" << entry << "\"" << endl;
185  }
186 
187  const QStringList mimeTypes = type.split( QLatin1Char( ',' ), QString::SkipEmptyParts );
188 
189  kDebug( 5300 ) << "registering Desktop file" << entry << "for" << mimeTypes << '@' << classes;
190  Q_FOREACH( const QString & mimeType, mimeTypes )
191  Q_FOREACH( const QString & classType, classes )
192  mPluginInfos.insert( mimeType + QLatin1Char('@') + classType, PluginMetaData( library, name, comment, cname ) );
193 
194  } else {
195  kWarning( 5300 ) << "Desktop file \"" << entry << "\" doesn't seem to describe a plugin " << "(misses Misc and/or Plugin group)" << endl;
196  }
197  }
198 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Tue Nov 26 2013 09:03:18 by doxygen 1.8.5 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.11.3 API Reference

Skip menu "kdepimlibs-4.11.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