From dadc34655c3ab961b0b0b94a10eaaba710f0b5e8 Mon Sep 17 00:00:00 2001 From: tpearson Date: Mon, 4 Jul 2011 22:38:03 +0000 Subject: Added kmymoney git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/applications/kmymoney@1239792 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- kmymoney2/plugins/pluginloader.cpp | 163 +++++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 kmymoney2/plugins/pluginloader.cpp (limited to 'kmymoney2/plugins/pluginloader.cpp') diff --git a/kmymoney2/plugins/pluginloader.cpp b/kmymoney2/plugins/pluginloader.cpp new file mode 100644 index 0000000..0201337 --- /dev/null +++ b/kmymoney2/plugins/pluginloader.cpp @@ -0,0 +1,163 @@ +/*************************************************************************** + pluginloader.cpp + ------------------- + begin : Thu Feb 12 2009 + copyright : (C) 2009 Cristian Onet + email : onet.cristian@gmail.com + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +// ---------------------------------------------------------------------------- +// QT Includes + +#include +#include +#include + +// ---------------------------------------------------------------------------- +// KDE Includes + +#include +#include +#include +#include +#include +#include +#include + +// ---------------------------------------------------------------------------- +// Project Includes + +#include "kmymoneyplugin.h" +#include "pluginloader.h" + +namespace KMyMoneyPlugin { + +//--------------------------------------------------------------------- +// +// PluginLoader +// +//--------------------------------------------------------------------- +static PluginLoader* s_instance = 0; + +typedef QMap PluginsMap; + +struct PluginLoader::Private +{ + QObject* m_parent; + KPluginInfo::List m_pluginList; + KPluginSelector* m_pluginSelector; + PluginsMap m_loadedPlugins; +}; + +PluginLoader::PluginLoader(QObject* parent) +{ + Q_ASSERT( s_instance == 0 ); + s_instance = this; + + d = new Private; + + d->m_parent = parent; + + KTrader::OfferList offers = KTrader::self()->query("KMyMoneyPlugin"); + d->m_pluginList = KPluginInfo::fromServices(offers); + + d->m_pluginSelector = new KPluginSelector(NULL); + d->m_pluginSelector->setShowEmptyConfigPage(false); + d->m_pluginSelector->addPlugins(d->m_pluginList); + d->m_pluginSelector->load(); + + connect(d->m_pluginSelector, SIGNAL(changed(bool)), this, SLOT(changed())); + connect(d->m_pluginSelector, SIGNAL(configCommitted(const QCString &)), this, SLOT(changedConfigOfPlugin(const QCString &))); +} + +PluginLoader::~PluginLoader() +{ + delete d; +} + +void PluginLoader::loadPlugins() +{ + for( KPluginInfo::List::Iterator it = d->m_pluginList.begin(); it != d->m_pluginList.end(); ++it ) + loadPlugin( *it ); +} + +void PluginLoader::loadPlugin(KPluginInfo* info) +{ + if (info->isPluginEnabled()) { + Plugin* plugin = getPluginFromInfo(info); + + if (!plugin) { + // the plugin is enabled but it is not loaded + KService::Ptr service = info->service(); + int error = 0; + Plugin* plugin = KParts::ComponentFactory + ::createInstanceFromService(service, d->m_parent, info->name().utf8(), QStringList(), &error); + if (plugin) { + kdDebug() << "KMyMoneyPlugin::PluginLoader: Loaded plugin " << plugin->name() << endl; + d->m_loadedPlugins.insert(info->name(), plugin); + emit PluginLoader::instance()->plug(info); + } + else { + kdWarning() << "KMyMoneyPlugin::PluginLoader:: createInstanceFromService returned 0 for " + << info->name() + << " with error number " + << error << endl; + if (error == KParts::ComponentFactory::ErrNoLibrary) + kdWarning() << "KLibLoader says: " + << KLibLoader::self()->lastErrorMessage() << endl; + } + } + } + else { + if (getPluginFromInfo(info) != NULL) { + // everybody interested should say goodbye to the plugin + emit PluginLoader::instance()->unplug(info); + d->m_loadedPlugins.erase(info->name()); + } + } +} + +void PluginLoader::changed() +{ + loadPlugins(); +} + +void PluginLoader::changedConfigOfPlugin(const QCString & name) +{ + PluginsMap::iterator itPlugin = d->m_loadedPlugins.find(QString(name)); + if (itPlugin != d->m_loadedPlugins.end()) + configChanged(*itPlugin); +} + +Plugin* PluginLoader::getPluginFromInfo(KPluginInfo* info) +{ + PluginsMap::iterator itPlugin = d->m_loadedPlugins.find(info->name()); + if (itPlugin != d->m_loadedPlugins.end()) + return *itPlugin; + else + return NULL; +} + +PluginLoader* PluginLoader::instance() +{ + Q_ASSERT( s_instance != 0); + return s_instance; +} + +KPluginSelector* PluginLoader::pluginSelectorWidget() +{ + return d->m_pluginSelector; +} + +} // namespace + +#include "pluginloader.moc" -- cgit v1.2.1