Merge branch 'plugin_ownership'

This commit is contained in:
alemuntoni 2021-03-10 14:38:38 +01:00
commit f15a348316
4 changed files with 49 additions and 14 deletions

View File

@ -22,13 +22,14 @@
****************************************************************************/
#include "plugin_manager.h"
#include "meshlab_plugin_type.h"
#include <QObject>
#include <qapplication.h>
#include <QPluginLoader>
#include <QDebug>
#include <QDir>
#include <QApplication>
#include <vcg/complex/algorithms/create/platonic.h>
#include "meshlab_plugin_type.h"
#include "../mlexception.h"
#include "../globals.h"
@ -53,8 +54,10 @@ PluginManager::PluginManager()
PluginManager::~PluginManager()
{
for (auto& plugin : allPlugins)
for (auto& plugin : allPluginLoaders){
plugin->unload();
delete plugin;
}
}
/**
@ -121,6 +124,8 @@ void PluginManager::checkPlugin(const QString& filename)
if (type.isFilterPlugin()){
checkFilterPlugin(qobject_cast<FilterPlugin *>(plugin));
}
loader.unload();
}
/**
@ -188,8 +193,8 @@ void PluginManager::loadPlugin(const QString& fileName)
checkPlugin(fileName);
//load the plugin depending on the type (can be more than one type!)
QPluginLoader loader(fin.absoluteFilePath());
QObject *plugin = loader.instance();
QPluginLoader* loader = new QPluginLoader(fin.absoluteFilePath());
QObject *plugin = loader->instance();
MeshLabPluginFile* ifp = dynamic_cast<MeshLabPluginFile *>(plugin);
MeshLabPluginType type(ifp);
@ -216,6 +221,7 @@ void PluginManager::loadPlugin(const QString& fileName)
//of all plugins
ifp->plugFileInfo = fin;
allPlugins.push_back(ifp);
allPluginLoaders.push_back(loader);
pluginFiles.insert(fin.absoluteFilePath());
}
@ -223,6 +229,7 @@ void PluginManager::unloadPlugin(MeshLabPluginFile* ifp)
{
auto it = std::find(allPlugins.begin(), allPlugins.end(), ifp);
if (it != allPlugins.end()){
unsigned int index = it - allPlugins.begin();
MeshLabPluginType type(ifp);
if (type.isDecoratePlugin()){
decoratePlugins.eraseDecoratePlugin(dynamic_cast<DecoratePlugin *>(ifp));
@ -242,8 +249,11 @@ void PluginManager::unloadPlugin(MeshLabPluginFile* ifp)
if (type.isRenderPlugin()){
renderPlugins.eraseRenderPlugin(dynamic_cast<RenderPlugin *>(ifp));
}
QPluginLoader* l = allPluginLoaders[index];
allPluginLoaders.erase(allPluginLoaders.begin() + index);
allPlugins.erase(it);
delete ifp;
l->unload();
delete l;
}
}

View File

@ -31,9 +31,8 @@
#include "containers/ioraster_plugin_container.h"
#include "containers/render_plugin_container.h"
#include<QMap>
#include<QObject>
#include <QDir>
#include <QPluginLoader>
#include <QObject>
/**
* @brief The PluginManager class provides the basic tools for managing all the plugins.
@ -91,6 +90,7 @@ public:
private:
//all plugins
std::vector<MeshLabPluginFile*> allPlugins;
std::vector<QPluginLoader*> allPluginLoaders;
std::set<QString> pluginFiles; //used to check if a plugin file has been already loaded
//Plugin containers: used for better organization of each type of plugin

View File

@ -28,6 +28,7 @@
#include <QPushButton>
#include <QFileDialog>
#include <QMessageBox>
#include <QSettings>
#include <common/plugins/interfaces/filter_plugin.h>
#include <common/plugins/interfaces/iomesh_plugin.h>
@ -89,7 +90,18 @@ void PluginInfoDialog::uninstallPluginPushButtonClicked()
MeshLabPluginFile* fpi = pm[nPlug];
QFileInfo fdel = fpi->pluginFileInfo();
pm.unloadPlugin(fpi);
QFile::remove(fdel.absoluteFilePath());
bool res = QFile::remove(fdel.absoluteFilePath());
if (!res){
QSettings settings;
QStringList toDeletePlugins = settings.value("ToDeletePlugins").value<QStringList>();
toDeletePlugins.append(fdel.absoluteFilePath());
settings.setValue("ToDeletePlugins", toDeletePlugins);
//pm.loadPlugin(fdel.absoluteFilePath());
//QMessageBox::warning(
// this, "Error while deleting plugin.",
// "Impossible to delete the plugin. Please delete manually the following file (or disable the plugin):\n"
// + fdel.absoluteFilePath());
}
ui->treeWidget->clear();
populateTreeWidget();
ui->treeWidget->update();

View File

@ -54,6 +54,21 @@ MainWindow::MainWindow():
PM(meshlab::pluginManagerInstance()),
_currviewcontainer(NULL)
{
QSettings settings;
//toDelete plugins, flagged in the last session
//this is needed on windows, since it refuses to delete dll plugins while meshlab is
//running. Therefore, in these cases plugins that are going to be deleted are
//saved in this list, and the files are removed in the next meshlab session,
//BEFORE they will be loaded. Therefore, these lines need to be executed
//before the PM.loadPlugins call.
QStringList toDeletePlugins = settings.value("ToDeletePlugins").value<QStringList>();
if (!toDeletePlugins.isEmpty()){
for (const QString& file : toDeletePlugins){
QFile::remove(file);
}
}
settings.remove("ToDeletePlugins");
setContextMenuPolicy(Qt::NoContextMenu);
//workspace = new QWorkspace(this);
@ -70,7 +85,6 @@ MainWindow::MainWindow():
catch (const MLException& e) {
QMessageBox::warning(this, "Error while loading plugins.", e.what());
}
QSettings settings;
//disable previously disabled plugins
QStringList disabledPlugins = settings.value("DisabledPlugins").value<QStringList>();
@ -78,7 +92,6 @@ MainWindow::MainWindow():
if (disabledPlugins.contains(fp->pluginName()))
PM.disablePlugin(fp);
}
//setCentralWidget(workspace);
setCentralWidget(mdiarea);