summaryrefslogtreecommitdiff
path: root/PluginLoader.cpp
blob: 2a59cb984515dcbb261638fa3cace0c15459e675 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/** \file PluginLoader.h
\brief Define the plugin loader
\author alpha_one_x86
\version 0.3
\date 2010
\licence GPL3, see the file COPYING */

#include "PluginLoader.h"

PluginLoader::PluginLoader(QObject *parent) :
	QObject(parent)
{
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"start");
	//load the overall instance
	plugins=PluginsManager::getInstance();
	//load the plugin
	plugins->lockPluginListEdition();
	QList<PluginsAvailable> list=plugins->getPluginsByCategory(PluginType_PluginLoader);
	foreach(PluginsAvailable currentPlugin,list)
		onePluginAdded(currentPlugin);
	qRegisterMetaType<PluginsAvailable>("PluginsAvailable");
	qRegisterMetaType<CatchState>("CatchState");
	connect(plugins,SIGNAL(onePluginAdded(PluginsAvailable)),		this,SLOT(onePluginAdded(PluginsAvailable)));
	connect(plugins,SIGNAL(onePluginWillBeRemoved(PluginsAvailable)),	this,SLOT(onePluginWillBeRemoved(PluginsAvailable)),Qt::DirectConnection);
	connect(plugins,SIGNAL(pluginListingIsfinish()),			this,SLOT(allPluginIsloaded()));
	plugins->unlockPluginListEdition();
	needEnable=false;
	last_state=Uncaught;
	last_have_plugin=false;
	last_inWaitOfReply=false;
}

PluginLoader::~PluginLoader()
{
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"start");
	QList<PluginsAvailable> list=plugins->getPluginsByCategory(PluginType_PluginLoader);
	foreach(PluginsAvailable currentPlugin,list)
		onePluginWillBeRemoved(currentPlugin);
	PluginsManager::destroyInstanceAtTheLastCall();
}

void PluginLoader::resendState()
{
	sendState(true);
}

void PluginLoader::onePluginAdded(const PluginsAvailable &plugin)
{
	if(plugin.category!=PluginType_PluginLoader)
		return;
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"start");
	QString pluginPath=plugin.path+PluginsManager::getResolvedPluginName("pluginLoader");
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"try load: "+pluginPath);
	QPluginLoader *pluginLoader= new QPluginLoader(pluginPath);
	QObject *pluginInstance = pluginLoader->instance();
	if(pluginInstance)
	{
		PluginInterface_PluginLoader *PluginLoader = qobject_cast<PluginInterface_PluginLoader *>(pluginInstance);
		//check if found
		index=0;
		loop_size=pluginList.size();
		while(index<loop_size)
		{
			if(pluginList.at(index).PluginLoaderInterface==PluginLoader)
			{
				ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Warning,QString("Plugin already found"));
				pluginLoader->unload();
				return;
			}
			index++;
		}
		if(PluginLoader)
		{
			#ifdef ULTRACOPIER_DEBUG
			connect(PluginLoader,SIGNAL(debugInformation(DebugLevel,QString,QString,QString,int)),this,SLOT(debugInformation(DebugLevel,QString,QString,QString,int)));
			#endif // ULTRACOPIER_DEBUG
			LocalPlugin newEntry;
			newEntry.options=new LocalPluginOptions("PluginLoader-"+plugin.name);
			newEntry.pluginLoader			= pluginLoader;
			newEntry.PluginLoaderInterface		= PluginLoader;
			newEntry.path				= plugin.path;
			newEntry.state				= Uncaught;
			newEntry.inWaitOfReply			= false;
			pluginList << newEntry;
			PluginLoader->setResources(newEntry.options,plugin.writablePath,plugin.path,ULTRACOPIER_VERSION_PORTABLE_BOOL);
			connect(pluginList.last().PluginLoaderInterface,SIGNAL(newState(CatchState)),this,SLOT(newState(CatchState)));
			if(needEnable)
			{
				pluginList.last().inWaitOfReply=true;
				newEntry.PluginLoaderInterface->setEnabled(needEnable);
			}

		}
		else
			ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Warning,"unable to cast the plugin: "+pluginLoader->errorString());
	}
	else
		ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Warning,"unable to load the plugin: "+pluginLoader->errorString());
}

void PluginLoader::onePluginWillBeRemoved(const PluginsAvailable &plugin)
{
	if(plugin.category!=PluginType_PluginLoader)
		return;
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"start");
	index=0;
	loop_size=pluginList.size();
	while(index<loop_size)
	{
		if(plugin.path==pluginList.at(index).path)
		{
			pluginList.at(index).PluginLoaderInterface->setEnabled(false);
			if(!pluginList.at(index).pluginLoader->isLoaded() || pluginList.at(index).pluginLoader->unload())
			{
				delete pluginList.at(index).options;
				pluginList.removeAt(index);
			}
			sendState();
			return;
		}
		index++;
	}
}

void PluginLoader::load()
{
	needEnable=true;
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"start");
	int index=0;
	while(index<pluginList.size())
	{
		pluginList[index].inWaitOfReply=true;
		pluginList.at(index).PluginLoaderInterface->setEnabled(true);
		index++;
	}
}

void PluginLoader::unload()
{
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"start");
	needEnable=false;
	int index=0;
	while(index<pluginList.size())
	{
		pluginList[index].inWaitOfReply=true;
		pluginList.at(index).PluginLoaderInterface->setEnabled(false);
		index++;
	}
}

#ifdef ULTRACOPIER_DEBUG
void PluginLoader::debugInformation(DebugLevel level,const QString& fonction,const QString& text,const QString& file,const int& ligne)
{
	DebugEngine::addDebugInformationStatic(level,fonction,text,file,ligne,"Plugin loader plugin");
}
#endif // ULTRACOPIER_DEBUG

void PluginLoader::allPluginIsloaded()
{
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"with value: "+QString::number(pluginList.size()>0));
	sendState(true);
}

void PluginLoader::sendState(bool force)
{
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,QString("start, pluginList.size(): %1, force: %2").arg(pluginList.size()).arg(force));
	CatchState current_state=Uncaught;
	bool found_not_listen=false,found_listen=false,found_inWaitOfReply=false;
	int index=0;
	while(index<pluginList.size())
	{
		if(current_state==Uncaught)
		{
			if(pluginList.at(index).state==Semiuncaught)
				current_state=Semiuncaught;
			else if(pluginList.at(index).state==Uncaught)
				found_not_listen=true;
			else if(pluginList.at(index).state==Caught)
				found_listen=true;
		}
		if(pluginList.at(index).inWaitOfReply)
			found_inWaitOfReply=true;
		index++;
	}
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,QString("current_state: %1").arg(current_state));
	if(current_state==Uncaught)
	{
		if(found_not_listen && !found_listen)
			current_state=Uncaught;
		else if(!found_not_listen && found_listen)
			current_state=Caught;
		else
			current_state=Semiuncaught;
	}
	bool have_plugin=pluginList.size()>0;
	if(force || current_state!=last_state || have_plugin!=last_have_plugin || found_inWaitOfReply!=last_inWaitOfReply)
	{
		ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,QString("send pluginLoaderReady(%1,%2,%3)").arg(current_state).arg(have_plugin).arg(found_inWaitOfReply));
		emit pluginLoaderReady(current_state,have_plugin,found_inWaitOfReply);
	}
	else
		ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,QString("Skip the signal sending"));
	last_state=Uncaught;
	last_have_plugin=have_plugin;
	last_inWaitOfReply=found_inWaitOfReply;
}

void PluginLoader::newState(const CatchState &state)
{
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,QString("start, state: %1").arg(state));
	PluginInterface_PluginLoader *temp=qobject_cast<PluginInterface_PluginLoader *>(QObject::sender());
	if(temp==NULL)
	{
		ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Critical,QString("listener not located!"));
		return;
	}
	int index=0;
	while(index<pluginList.size())
	{
		if(temp==pluginList.at(index).PluginLoaderInterface)
		{
			pluginList[index].state=state;
			pluginList[index].inWaitOfReply=false;
			sendState(true);
			return;
		}
		index++;
	}
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Critical,QString("listener not found!"));
}