krop's commit fixes my problem in a better way, reverting
[kdepim.git] / libkdepim / pluginloaderbase.cpp
blobfb8f21ebdf5a157100207bef48e5c427eef11fa6
1 /* -*- c++ -*-
2 This file is part of libkdepim.
4 Copyright (c) 2002,2004 Marc Mutz <mutz@kde.org>
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
22 #include "pluginloaderbase.h"
24 #include <KConfigGroup>
25 #include <KDebug>
26 #include <KGlobal>
27 #include <KLibLoader>
28 #include <KLocale>
29 #include <KStandardDirs>
31 #include <QFile>
32 #include <QStringList>
34 namespace KPIM {
36 PluginLoaderBase::PluginLoaderBase() : d(0) {}
37 PluginLoaderBase::~PluginLoaderBase() {}
40 QStringList PluginLoaderBase::types() const {
41 QStringList result;
42 for ( QMap< QString, PluginMetaData >::const_iterator it = mPluginMap.constBegin(); it != mPluginMap.constEnd() ; ++it )
43 result.push_back( it.key() );
44 return result;
47 const PluginMetaData * PluginLoaderBase::infoForName( const QString & type ) const {
48 return mPluginMap.contains( type ) ? &(const_cast<PluginLoaderBase*>(this)->mPluginMap[type]) : 0 ;
52 void PluginLoaderBase::doScan( const char * path ) {
53 mPluginMap.clear();
55 const QStringList list =
56 KGlobal::dirs()->findAllResources( "data", path,
57 KStandardDirs::Recursive |
58 KStandardDirs::NoDuplicates );
59 for ( QStringList::const_iterator it = list.constBegin(); it != list.constEnd(); ++it ) {
60 KConfig config( *it, KConfig::SimpleConfig);
61 if ( config.hasGroup( "Misc" ) && config.hasGroup( "Plugin" ) ) {
62 KConfigGroup group( &config, "Plugin" );
64 const QString type = group.readEntry( "Type" ).toLower();
65 if ( type.isEmpty() ) {
66 kWarning( 5300 ) << "missing or empty [Plugin]Type value in \"" << *it << "\" - skipping" << endl;
67 continue;
70 const QString library = group.readEntry( "X-KDE-Library" );
71 if ( library.isEmpty() ) {
72 kWarning( 5300 ) << "missing or empty [Plugin]X-KDE-Library value in \"" << *it << "\" - skipping" << endl;
73 continue;
76 KConfigGroup group2( &config, "Misc" );
78 QString name = group2.readEntry( "Name" );
79 if ( name.isEmpty() ) {
80 kWarning( 5300 ) << "missing or empty [Misc]Name value in \"" << *it << "\" - inserting default name" << endl;
81 name = i18n("Unnamed plugin");
84 QString comment = group2.readEntry( "Comment" );
85 if ( comment.isEmpty() ) {
86 kWarning( 5300 ) << "missing or empty [Misc]Comment value in \"" << *it << "\" - inserting default name" << endl;
87 comment = i18n("No description available");
90 mPluginMap.insert( type, PluginMetaData( library, name, comment ) );
91 } else {
92 kWarning( 5300 ) << "Desktop file \"" << *it << "\" doesn't seem to describe a plugin " << "(misses Misc and/or Plugin group)" << endl;
97 KLibrary::void_function_ptr PluginLoaderBase::mainFunc( const QString & type, const char * mf_name ) const {
98 if ( type.isEmpty() || !mPluginMap.contains( type ) )
99 return 0;
101 const QString libName = mPluginMap[ type ].library;
102 if ( libName.isEmpty() )
103 return 0;
105 const KLibrary * lib = openLibrary( libName );
106 if ( !lib )
107 return 0;
109 PluginMetaData pmd = mPluginMap.value( type );
110 pmd.loaded = true;
111 mPluginMap[ type ] = pmd;
113 const QString factory_name = libName + '_' + mf_name;
114 KLibrary::void_function_ptr sym = const_cast<KLibrary*>( lib )->resolveFunction( factory_name.toLatin1() );
115 if ( !sym ) {
116 kWarning( 5300 ) << "No symbol named \"" << factory_name.toLatin1() << "\" (" << factory_name << ") was found in library \"" << libName << "\"" << endl;
117 return 0;
120 return sym;
123 const KLibrary * PluginLoaderBase::openLibrary( const QString & libName ) const {
125 const QString path = KLibLoader::findLibrary( libName );
127 if ( path.isEmpty() ) {
128 kWarning( 5300 ) << "No plugin library named \"" << libName << "\" was found!" << endl;
129 return 0;
132 const KLibrary * library = KLibLoader::self()->library( path );
134 kDebug( !library, 5300 ) << "Could not load library '" << libName << "'" << endl;
136 return library;
140 } // namespace KMime