20 #include "pluginloader_p.h"
22 #include <kconfiggroup.h>
26 #include <klocalizedstring.h>
27 #include <kstandarddirs.h>
28 #include <KPluginLoader>
30 #include <QtCore/QDebug>
33 #include <KMessageBox>
36 using namespace Akonadi;
38 PluginMetaData::PluginMetaData()
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 )
49 PluginLoader* PluginLoader::mSelf = 0;
51 PluginLoader::PluginLoader()
56 PluginLoader::~PluginLoader()
58 qDeleteAll( mPluginLoaders );
59 mPluginLoaders.clear();
62 PluginLoader* PluginLoader::self()
65 mSelf =
new PluginLoader();
70 QStringList PluginLoader::names()
const
72 return mPluginInfos.keys();
75 QObject* PluginLoader::createForName(
const QString & name )
77 if ( !mPluginInfos.contains( name ) ) {
78 kWarning( 5300 ) <<
"plugin name \"" << name <<
"\" is unknown to the plugin loader." << endl;
82 PluginMetaData &info = mPluginInfos[ name ];
85 foreach (QObject *plugin, QPluginLoader::staticInstances()) {
86 if (QLatin1String(plugin->metaObject()->className()) == info.className) {
94 KPluginLoader* loader =
new KPluginLoader( info.library );
95 if ( loader->fileName().isEmpty() ) {
96 kWarning( 5300 ) << loader->errorString();
101 mPluginLoaders.insert( name, loader );
105 QPluginLoader *loader = mPluginLoaders.value( name );
108 QObject *
object = loader->instance();
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 );
119 kWarning( 5300 ) <<
"unable to load plugin for plugin name \"" << name <<
"\"." << endl;
120 kWarning( 5300 ) <<
"Error was:\"" << loader->errorString() <<
"\"." << endl;
127 PluginMetaData PluginLoader::infoForName(
const QString & name )
const
129 if ( !mPluginInfos.contains( name ) )
130 return PluginMetaData();
132 return mPluginInfos.value( name );
135 void PluginLoader::scan()
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 );
142 KConfig config( entry, KConfig::SimpleConfig );
143 if ( config.hasGroup(
"Misc" ) && config.hasGroup(
"Plugin" ) ) {
144 KConfigGroup group( &config,
"Plugin" );
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;
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;
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;
167 KConfigGroup group2( &config,
"Misc" );
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" );
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" );
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;
186 const QStringList mimeTypes = type.split( QLatin1Char(
',' ), QString::SkipEmptyParts );
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 ) );
194 kWarning( 5300 ) <<
"Desktop file \"" << entry <<
"\" doesn't seem to describe a plugin " <<
"(misses Misc and/or Plugin group)" << endl;