From 91b9c34d34d724f09f06195ea960f4205dbed54e Mon Sep 17 00:00:00 2001 From: dfighter1985 Date: Sun, 8 Jun 2014 18:17:24 +0200 Subject: [PATCH] Implemented plugin unload. --- .../src/extension_system/iplugin_manager.h | 3 + .../src/extension_system/plugin_manager.cpp | 62 +++++++++++++++++++ .../src/extension_system/plugin_manager.h | 4 ++ .../src/plugins/core/plugin_view_dialog.cpp | 17 ++++- .../src/plugins/core/plugin_view_dialog.h | 1 + 5 files changed, 86 insertions(+), 1 deletion(-) diff --git a/code/studio/src/extension_system/iplugin_manager.h b/code/studio/src/extension_system/iplugin_manager.h index 15fa4a336..efbf5ee2f 100644 --- a/code/studio/src/extension_system/iplugin_manager.h +++ b/code/studio/src/extension_system/iplugin_manager.h @@ -55,6 +55,9 @@ public: virtual void setPluginPaths(const QStringList &paths) = 0; virtual QList plugins() const = 0; + virtual bool loadPlugin( const char *plugin ) = 0; + virtual bool unloadPlugin( ExtensionSystem::IPluginSpec *plugin ) = 0; + // Settings virtual void setSettings(QSettings *settings) = 0; virtual QSettings *settings() const = 0; diff --git a/code/studio/src/extension_system/plugin_manager.cpp b/code/studio/src/extension_system/plugin_manager.cpp index 265268896..c9f40e7bd 100644 --- a/code/studio/src/extension_system/plugin_manager.cpp +++ b/code/studio/src/extension_system/plugin_manager.cpp @@ -114,6 +114,68 @@ void PluginManager::loadPlugins() Q_EMIT pluginsChanged(); } +bool PluginManager::loadPlugin( const char *plugin ) +{ + return true; +} + +bool PluginManager::unloadPlugin( ExtensionSystem::IPluginSpec *plugin ) +{ + ExtensionSystem::PluginSpec *spec = static_cast< ExtensionSystem::PluginSpec* >( plugin ); + + // Let's see if anything depends on this one + QListIterator< ExtensionSystem::PluginSpec* > itr( m_pluginSpecs ); + while( itr.hasNext() ) + { + ExtensionSystem::PluginSpec *sp = itr.next(); + QList< ExtensionSystem::PluginSpec* > l = sp->dependencySpecs(); + if( l.contains( spec ) ) + return false; + } + + // Stop, delete then remove plugin + spec->stop(); + spec->kill(); + removePlugin( plugin ); + + return true; +} + +void PluginManager::removePlugin( ExtensionSystem::IPluginSpec *plugin ) +{ + QList< ExtensionSystem::IPluginSpec* >::iterator itr1; + QList< ExtensionSystem::PluginSpec* >::iterator itr2; + + QList< ExtensionSystem::IPluginSpec* >::iterator i1 = m_ipluginSpecs.begin(); + while( i1 != m_ipluginSpecs.end() ) + { + if( *i1 == plugin ) + { + itr1 = i1; + break; + } + i1++; + } + + QList< ExtensionSystem::PluginSpec* >::iterator i2 = m_pluginSpecs.begin(); + while( i2 != m_pluginSpecs.end() ) + { + if( *i2 == static_cast< ExtensionSystem::PluginSpec* >( plugin ) ) + { + itr2 = i2; + break; + } + i2++; + } + + m_ipluginSpecs.erase( itr1 ); + m_pluginSpecs.erase( itr2 ); + delete plugin; + + Q_EMIT pluginsChanged(); + +} + QStringList PluginManager::getPluginPaths() const { return m_pluginPaths; diff --git a/code/studio/src/extension_system/plugin_manager.h b/code/studio/src/extension_system/plugin_manager.h index 9ab1ae135..10130ffc4 100644 --- a/code/studio/src/extension_system/plugin_manager.h +++ b/code/studio/src/extension_system/plugin_manager.h @@ -51,6 +51,10 @@ public: virtual QList plugins() const; QList loadQueue(); + bool loadPlugin( const char *plugin ); + bool unloadPlugin( ExtensionSystem::IPluginSpec *plugin ); + void removePlugin( ExtensionSystem::IPluginSpec *plugin ); + // Settings virtual void setSettings(QSettings *settings); virtual QSettings *settings() const; diff --git a/code/studio/src/plugins/core/plugin_view_dialog.cpp b/code/studio/src/plugins/core/plugin_view_dialog.cpp index 316bb0097..bf62d4632 100644 --- a/code/studio/src/plugins/core/plugin_view_dialog.cpp +++ b/code/studio/src/plugins/core/plugin_view_dialog.cpp @@ -65,6 +65,7 @@ void PluginView::updateList() static QIcon notLoadedIcon = QApplication::style()->standardIcon(QStyle::SP_DialogResetButton); m_specToItem.clear(); + m_itemToSpec.clear(); QList items; Q_FOREACH (ExtensionSystem::IPluginSpec *spec, m_pluginManager->plugins()) @@ -87,6 +88,7 @@ void PluginView::updateList() items.append(item); m_specToItem.insert(spec, item); + m_itemToSpec.insert(item, spec); } m_ui.pluginTreeWidget->clear(); @@ -127,7 +129,18 @@ void PluginView::onUnloadClicked() return; } - + QMap< QTreeWidgetItem*, ExtensionSystem::IPluginSpec* >::const_iterator itr + = m_itemToSpec.find( item ); + if( itr == m_itemToSpec.end() ) + return; + + bool success = m_pluginManager->unloadPlugin( itr.value() ); + if( !success ) + { + QMessageBox::warning( this, + tr( "Plugin unload" ), + tr( "Failed to unload plugin." ) ); + } } void PluginView::onLoadClicked() @@ -141,6 +154,8 @@ void PluginView::onLoadClicked() { return; } + + this->m_pluginManager->loadPlugin( f.toAscii().data() ); } } /* namespace Core */ \ No newline at end of file diff --git a/code/studio/src/plugins/core/plugin_view_dialog.h b/code/studio/src/plugins/core/plugin_view_dialog.h index e11449fdd..e7c1123c1 100644 --- a/code/studio/src/plugins/core/plugin_view_dialog.h +++ b/code/studio/src/plugins/core/plugin_view_dialog.h @@ -52,6 +52,7 @@ private: const int m_checkStateColumn; QMap m_specToItem; + QMap m_itemToSpec; QStringList m_whiteList; ExtensionSystem::IPluginManager *m_pluginManager; Ui::PluginView m_ui;