libkdepim

pluginloaderbase.cpp

00001 /*  -*- c++ -*-
00002     This file is part of libkdepim.
00003 
00004     Copyright (c) 2002,2004 Marc Mutz <mutz@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019     Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include <pluginloaderbase.h>
00023 
00024 #include <ksimpleconfig.h>
00025 #include <klocale.h>
00026 #include <kstandarddirs.h>
00027 #include <klibloader.h>
00028 #include <kglobal.h>
00029 #include <kdebug.h>
00030 
00031 #include <qfile.h>
00032 #include <qstringlist.h>
00033 
00034 static kdbgstream warning() {
00035   return kdWarning( 5300 ) << "PluginLoaderBase: ";
00036 }
00037 #ifndef NDEBUG
00038 static kdbgstream debug( bool cond )
00039 #else
00040 static kndbgstream debug( bool cond )
00041 #endif
00042 {
00043   return kdDebug( cond, 5300 ) << "PluginLoaderBase: ";
00044 }
00045 
00046 namespace KPIM {
00047 
00048   PluginLoaderBase::PluginLoaderBase() : d(0) {}
00049   PluginLoaderBase::~PluginLoaderBase() {}
00050 
00051 
00052   QStringList PluginLoaderBase::types() const {
00053     QStringList result;
00054     for ( QMap< QString, PluginMetaData >::const_iterator it = mPluginMap.begin();
00055       it != mPluginMap.end() ; ++it )
00056       result.push_back( it.key() );
00057     return result;
00058   }
00059 
00060   const PluginMetaData * PluginLoaderBase::infoForName( const QString & type ) const {
00061     return mPluginMap.contains( type ) ? &(mPluginMap[type]) : 0 ;
00062   }
00063 
00064 
00065   void PluginLoaderBase::doScan( const char * path ) {
00066     mPluginMap.clear();
00067 
00068     const QStringList list =
00069       KGlobal::dirs()->findAllResources( "data", path, true, true );
00070     for ( QStringList::const_iterator it = list.begin() ;
00071       it != list.end() ; ++it ) {
00072       KSimpleConfig config( *it, true );
00073       if ( config.hasGroup( "Misc" ) && config.hasGroup( "Plugin" ) ) {
00074     config.setGroup( "Plugin" );
00075 
00076     const QString type = config.readEntry( "Type" ).lower();
00077     if ( type.isEmpty() ) {
00078       warning() << "missing or empty [Plugin]Type value in \""
00079             << *it << "\" - skipping" << endl;
00080       continue;
00081     }
00082 
00083     const QString library = config.readEntry( "X-KDE-Library" );
00084     if ( library.isEmpty() ) {
00085       warning() << "missing or empty [Plugin]X-KDE-Library value in \""
00086             << *it << "\" - skipping" << endl;
00087       continue;
00088     }
00089 
00090     config.setGroup( "Misc" );
00091 
00092     QString name = config.readEntry( "Name" );
00093     if ( name.isEmpty() ) {
00094       warning() << "missing or empty [Misc]Name value in \""
00095             << *it << "\" - inserting default name" << endl;
00096       name = i18n("Unnamed plugin");
00097     }
00098 
00099     QString comment = config.readEntry( "Comment" );
00100     if ( comment.isEmpty() ) {
00101       warning() << "missing or empty [Misc]Comment value in \""
00102             << *it << "\" - inserting default name" << endl;
00103       comment = i18n("No description available");
00104     }
00105 
00106     mPluginMap.insert( type, PluginMetaData( library, name, comment ) );
00107       } else {
00108     warning() << "Desktop file \"" << *it
00109           << "\" doesn't seem to describe a plugin "
00110           << "(misses Misc and/or Plugin group)" << endl;
00111       }
00112     }
00113   }
00114 
00115   void * PluginLoaderBase::mainFunc( const QString & type,
00116                      const char * mf_name ) const {
00117     if ( type.isEmpty() || !mPluginMap.contains( type ) )
00118       return 0;
00119 
00120     const QString libName = mPluginMap[ type ].library;
00121     if ( libName.isEmpty() )
00122       return 0;
00123 
00124     const KLibrary * lib = openLibrary( libName );
00125     if ( !lib )
00126       return 0;
00127 
00128     mPluginMap[ type ].loaded = true;
00129 
00130     const QString factory_name = libName + '_' + mf_name;
00131     if ( !lib->hasSymbol( factory_name.latin1() ) ) {
00132       warning() << "No symbol named \"" << factory_name.latin1() << "\" ("
00133         << factory_name << ") was found in library \"" << libName
00134         << "\"" << endl;
00135       return 0;
00136     }
00137 
00138     return lib->symbol( factory_name.latin1() );
00139   }
00140 
00141   const KLibrary * PluginLoaderBase::openLibrary( const QString & libName ) const {
00142 
00143     const QString path = KLibLoader::findLibrary( QFile::encodeName( libName ) );
00144 
00145     if ( path.isEmpty() ) {
00146       warning() << "No plugin library named \"" << libName
00147         << "\" was found!" << endl;
00148       return 0;
00149     }
00150 
00151     const KLibrary * library = KLibLoader::self()->library( QFile::encodeName( path ) );
00152 
00153     debug( !library ) << "Could not load library '" << libName << "'" << endl;
00154 
00155     return library;
00156   }
00157 
00158 
00159 } // namespace KMime
KDE Home | KDE Accessibility Home | Description of Access Keys