<commit_before>/***********************************************************************************************************************
 * PluginManager.cpp
 *
 *  Created on: Oct 29, 2010
 *      Author: <NAME>
 **********************************************************************************************************************/

#include "EnvisionPlugin.h"
#include "PluginManager.h"
#include "EnvisionException.h"

#include <QtXml/QDomDocument>
#include <QtXml/QDomElement>
#include <QtCore/QTextStream>
#include <QtCore/QStringList>

namespace Envision {

PluginManager::PluginManager(QString path) :
	pluginsDir(path)
{
	scanAllPluginsMetaData();
}

void PluginManager::loadAllPlugins(EnvisionManager& envisionManager)
{
	int lastCountLoaded = -1;

	// Holds the ids of plugins which are not yet loaded.
	QStringList plugins;
	for (QList<PluginInfo>::iterator p = pluginMetaData.begin(); p != pluginMetaData.end(); p++)
		plugins.append(p->id);

	while ( lastCountLoaded != loadedPlugins.length() && loadedPlugins.length() < pluginMetaData.length() )
	{
		lastCountLoaded = loadedPlugins.length();

		for (int i = plugins.length() - 1; i >= 0; i--)
		{
			QList<PluginDependency> depList = idToMetaDataMap.value(plugins.at(i))->dependencies;

			bool allDependenciesLoaded = true;
			for (QList<PluginDependency>::iterator dep = depList.begin(); dep != depList.end(); dep++)
			{
				// Check if this dependency is already loaded
				if ( idToPluginLoaderMap.contains(dep->id) == false )
				{
					allDependenciesLoaded = false;
					break;
				}

				// Check if the version of the non-dependent plugin matches the version the current plugin depends on
				if ( idToMetaDataMap.value(dep->id)->version.startsWith(dep->majorVersion + ".", Qt::CaseSensitive) == false )
				{
					throw EnvisionException("Plugin " + plugins.at(i) + " depends on version " + dep->majorVersion + " of " + dep->id + " but a different version is installed.");
				}
			}

			if ( allDependenciesLoaded )
			{
				QTextStream out(stdout);
				out << "Loading plug-in " << plugins.at(i) << "... ";

				QPluginLoader* plugin = new QPluginLoader(pluginsDir.absoluteFilePath(getLibraryFileName(plugins.at(i))));
				plugin->setLoadHints(QLibrary::ExportExternalSymbolsHint);

				plugin->load();

				if ( plugin->isLoaded() == false ) throw EnvisionException("Could not load plugin: " + plugin->errorString());

				EnvisionPlugin* p = qobject_cast<EnvisionPlugin*> (plugin->instance());
				p->initialize(envisionManager);

				loadedPlugins.append(plugin);
				idToPluginLoaderMap.insert(plugins.at(i), plugin);
				plugins.removeAt(i);

				out << "OK" << endl;
			}
		}
	}
}

bool PluginManager::isPluginLoaded(QString pluginId)
{
	return idToPluginLoaderMap.contains(pluginId);
}

EnvisionPlugin* PluginManager::getLoadedPluginInterface(QString pluginId)
{
	if ( isPluginLoaded(pluginId) )
	{
		return qobject_cast<EnvisionPlugin*> (idToPluginLoaderMap.value(pluginId)->instance());
	}<commit_msg>Added a warning message when not all plug-ins are loaded due to dependency problem.<commit_after>/***********************************************************************************************************************
 * PluginManager.cpp
 *
 *  Created on: Oct 29, 2010
 *      Author: <NAME>
 **********************************************************************************************************************/

#include "EnvisionPlugin.h"
#include "PluginManager.h"
#include "EnvisionException.h"

#include <QtXml/QDomDocument>
#include <QtXml/QDomElement>
#include <QtCore/QTextStream>
#include <QtCore/QStringList>

namespace Envision {

PluginManager::PluginManager(QString path) :
	pluginsDir(path)
{
	scanAllPluginsMetaData();
}

void PluginManager::loadAllPlugins(EnvisionManager& envisionManager)
{
	int lastCountLoaded = -1;

	// Holds the ids of plugins which are not yet loaded.
	QStringList plugins;
	for (QList<PluginInfo>::iterator p = pluginMetaData.begin(); p != pluginMetaData.end(); p++)
		plugins.append(p->id);

	while ( lastCountLoaded != loadedPlugins.length() && loadedPlugins.length() < pluginMetaData.length() )
	{
		lastCountLoaded = loadedPlugins.length();

		for (int i = plugins.length() - 1; i >= 0; i--)
		{
			QList<PluginDependency> depList = idToMetaDataMap.value(plugins.at(i))->dependencies;

			bool allDependenciesLoaded = true;
			for (QList<PluginDependency>::iterator dep = depList.begin(); dep != depList.end(); dep++)
			{
				// Check if this dependency is already loaded
				if ( idToPluginLoaderMap.contains(dep->id) == false )
				{
					allDependenciesLoaded = false;
					break;
				}

				// Check if the version of the non-dependent plugin matches the version the current plugin depends on
				if ( idToMetaDataMap.value(dep->id)->version.startsWith(dep->majorVersion + ".", Qt::CaseSensitive) == false )
				{
					throw EnvisionException("Plugin " + plugins.at(i) + " depends on version " + dep->majorVersion + " of " + dep->id + " but a different version is installed.");
				}
			}

			if ( allDependenciesLoaded )
			{
				QTextStream out(stdout);
				out << "Loading plug-in " << plugins.at(i) << "... ";

				QPluginLoader* plugin = new QPluginLoader(pluginsDir.absoluteFilePath(getLibraryFileName(plugins.at(i))));
				plugin->setLoadHints(QLibrary::ExportExternalSymbolsHint);

				plugin->load();

				if ( plugin->isLoaded() == false ) throw EnvisionException("Could not load plugin: " + plugin->errorString());

				EnvisionPlugin* p = qobject_cast<EnvisionPlugin*> (plugin->instance());
				p->initialize(envisionManager);

				loadedPlugins.append(plugin);
				idToPluginLoaderMap.insert(plugins.at(i), plugin);
				plugins.removeAt(i);

				out << "OK" << endl;
			}
		}
	}

	// Check if there are any plug-ins with unmet dependencies
	if (plugins.size() > 0)
	{
		QTextStream out(stdout);
		out<< "Warning: The following plug-ins have not been loaded because their dependencies are not satisfied" << endl;
		for (int i = 0; i< plugins.size(); ++i) out << "  " << plugins.at(i) << endl;
	}
}

bool PluginManager::isPluginLoaded(QString pluginId)
{
	return idToPluginLoaderMap.contains(pluginId);
}

EnvisionPlugin* PluginManager::getLoadedPluginInterface(QString pluginId)
{
	if ( isPluginLoaded(pluginId) )
	{
		return qobject_cast<EnvisionPlugin*> (idToPluginLoaderMap.value(pluginId)->instance());
	}
{'fraction_non_alphanumeric': 0.2018867924528302, 'fraction_numerical': 0.0031446540880503146, 'mean_word_length': 4.782727272727273, 'pattern_counts': {'":': 0, '<': 56, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4480623', 'n_tokens_mistral': 2080, 'n_tokens_neox': 1826, 'n_words': 411}