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

akonadi

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

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



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