File: ccPluginManager.cpp

package info (click to toggle)
cloudcompare 2.11.3-7.1
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 58,224 kB
  • sloc: cpp: 229,982; ansic: 30,723; makefile: 84; sh: 20
file content (395 lines) | stat: -rw-r--r-- 10,052 bytes parent folder | download
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
//##########################################################################
//#                                                                        #
//#                              CLOUDCOMPARE                              #
//#                                                                        #
//#  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; version 2 or later of the License.      #
//#                                                                        #
//#  This program is distributed in the hope that it will be useful,       #
//#  but WITHOUT ANY WARRANTY; without even the implied warranty of        #
//#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the          #
//#  GNU General Public License for more details.                          #
//#                                                                        #
//#          COPYRIGHT: CloudCompare project                               #
//#                                                                        #
//##########################################################################

#include "ccApplicationBase.h"
#include "ccPluginManager.h"

//qCC_db
#include <ccExternalFactory.h>
#include <ccLog.h>

//plugins
#include "ccGLPluginInterface.h"
#include "ccIOPluginInterface.h"
#include "ccStdPluginInterface.h"

//Qt
#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QPluginLoader>
#include <QSet>
#include <QSettings>
#include <QStandardPaths>


namespace
{
	// This is used to avoid having to make the ccPluginManager constructor public
	class PrivatePluginManager : public ccPluginManager {};	
}

Q_GLOBAL_STATIC( PrivatePluginManager, sPluginManager );


// Get the plugin's IID from the meta data
static QString sPluginIID( QPluginLoader* loader )
{
	const QJsonObject metaObject = loader->metaData();
	
	if ( metaObject.isEmpty() )
	{
		const QString fileName = QFileInfo( loader->fileName() ).fileName();

		ccLog::Warning( QStringLiteral( "\t%1 does not supply meta data in the Q_PLUGIN_METADATA (see one of the example plugins)." ).arg( fileName ) );
		
		return {};
	}
	
	return metaObject["IID"].toString();
}

// Check for metadata and error if it's not there
// This indicates that a plugin hasn't been converted to the new JSON metadata.
static bool	sMetaDataValid( QPluginLoader* loader )
{
	const QJsonObject metaObject = loader->metaData();
	
	const QString fileName = QFileInfo( loader->fileName() ).fileName();
		
	if ( metaObject.isEmpty() || metaObject["MetaData"].toObject().isEmpty() )
	{
		ccLog::Error( QStringLiteral( "%1 does not supply meta data in the Q_PLUGIN_METADATA (see one of the example plugins)." ).arg( fileName ) );
		
		return false;
	}
	else
	{
		const QJsonObject	data = metaObject["MetaData"].toObject();
		
		// The plugin type is going to be required
		const QStringList validTypes{ "GL", "I/O", "Standard" };
		
		const QString pluginType = data["type"].toString();
		
		if ( !validTypes.contains( pluginType ) )
		{
			ccLog::Error( QStringLiteral( "%1 does not supply a valid plugin type in its info.json.\n\nFound: %2\n\nIt must be one of: %3" )
						  .arg( fileName, pluginType, validTypes.join( ", " ) ) );
			
			return false;
		}
	}
	
	return true;
}


ccPluginManager::ccPluginManager( QObject* parent ) :
	QObject( parent )
{
}

ccPluginManager& ccPluginManager::get()
{
	return *sPluginManager;
}

void ccPluginManager::setPaths( const QStringList& paths )
{
	m_pluginPaths = paths;
}

QStringList ccPluginManager::pluginPaths()
{
	return m_pluginPaths;
}

void ccPluginManager::loadPlugins()
{
	m_pluginList.clear();
	
	if ( m_pluginPaths.empty() )
	{
		qWarning() << "There are no plugin paths set. Maybe missing a call to ccPluginManager::setPaths()?";
	}
	
	// "static" plugins
	const QObjectList	pluginInstances = QPluginLoader::staticInstances();
	
	for ( QObject* plugin : pluginInstances )
	{
		ccPluginInterface* ccPlugin = dynamic_cast<ccPluginInterface*>(plugin);
		
		if (ccPlugin == nullptr)
		{
			continue;
		}
		
		ccLog::Print( tr( "[Plugin] Found: %1 (STATIC)" ).arg( ccPlugin->getName() ) );
		
		m_pluginList.push_back(ccPlugin);
	}
	
	// "dynamic" plugins
	loadFromPathsAndAddToList();
	
	// now iterate over plugins and automatically register what we can
	const auto pluginList = m_pluginList;
	
	const QStringList disabledList = disabledPluginIIDs();
	
	for ( ccPluginInterface* plugin : pluginList )
	{
		if ( plugin == nullptr )
		{
			Q_ASSERT(false);
			continue;
		}
		
		// Disable if we are not running on the command line and it's in the disabled list
		if ( !ccApp->isCommandLine() && disabledList.contains( plugin->IID() ) )
		{
			ccLog::Print( tr( "[Plugin][%1] Disabled" ).arg( plugin->getName() ) );
						
			continue;
		}
		
		switch ( plugin->getType() )
		{
			case CC_STD_PLUGIN:
			{
				ccStdPluginInterface* stdPlugin = static_cast<ccStdPluginInterface*>(plugin);
				
				//see if this plugin provides an additional factory for objects
				ccExternalFactory* factory = stdPlugin->getCustomObjectsFactory();
				
				if (factory != nullptr)
				{
					//if it's valid, add it to the factories set
					Q_ASSERT(ccExternalFactory::Container::GetUniqueInstance());
					ccExternalFactory::Container::GetUniqueInstance()->addFactory(factory);
				}
				
				break;
			}
				
			case CC_IO_FILTER_PLUGIN: //I/O filter
			{
				ccIOPluginInterface* ioPlugin = static_cast<ccIOPluginInterface*>(plugin);
				
				QStringList	ioExtensions;
				
				for ( auto &filter : ioPlugin->getFilters() )
				{
					if (filter)
					{
						FileIOFilter::Register(filter);
						
						ioExtensions += filter->getDefaultExtension().toUpper();
					}
				}
				
				if ( !ioExtensions.empty() )
				{
					ioExtensions.sort();
					
					ccLog::Print( tr( "[Plugin][%1] New file extensions registered: %2" )
								  .arg( ioPlugin->getName(), ioExtensions.join( ' ' ) ) );
				}
				
				break;
			}
				
			default:
				//nothing to do at this point
				break;
		}
	}
}

ccPluginInterfaceList &ccPluginManager::pluginList()
{
#ifdef QT_DEBUG
	if ( m_pluginList.empty() )
	{
		qWarning() << "Plugin list is empty - did you call loadPlugins()?";
	}
#endif
	
	return m_pluginList;
}

void ccPluginManager::setPluginEnabled( const ccPluginInterface* plugin, bool enabled )
{	
	QStringList list = disabledPluginIIDs();
	
	const QString &iid = plugin->IID();
	
	if ( enabled )
	{
		list.removeAll( iid );
	}
	else
	{
		if ( !list.contains( iid ) )
		{
			list.append( iid );
		}
	}
	
	QSettings settings;
	
	settings.beginGroup( "Plugins" );
	
	settings.setValue( "Disabled", list );
}

bool ccPluginManager::isEnabled( const ccPluginInterface *plugin ) const
{
	const QString &iid = plugin->IID();
	
	return !disabledPluginIIDs().contains( iid );
}

void ccPluginManager::loadFromPathsAndAddToList()
{
	const QStringList nameFilters{
#if defined(Q_OS_MAC)
		"*.dylib"
#elif defined(Q_OS_WIN)
		"*.dll"
#elif defined(Q_OS_LINUX)
		"*.so"
#else
#error Need to specify the dynamic library extension for this OS.
#endif
	};
	
	// Map the plugin's IID to its loader so we can unload it if necessary.
	//	This lets us override plugins by path.
	QMap<QString, QPluginLoader *> pluginIIDToLoaderMap;
	
	const auto paths = pluginPaths();
	
	for ( const QString& path : paths )
	{
		ccLog::Print( tr( "[Plugin] Searching: %1" ).arg( path ) );
		
		QDir pluginsDir( path );
		
		pluginsDir.setNameFilters( nameFilters );
		
		const QStringList	fileNames = pluginsDir.entryList();
		
		for ( const QString& fileName : fileNames )
		{
			const QString pluginPath = pluginsDir.absoluteFilePath( fileName );
			
			QPluginLoader *loader = new QPluginLoader( pluginPath );
			
			const QString pluginIID = sPluginIID( loader );
			
			bool metaDataValid = sMetaDataValid( loader );
			
			if ( !metaDataValid || pluginIID.isNull() )
			{
				ccLog::Warning( tr( "\t%1 has invalid meta data\t" ).arg( fileName ) );
				
				delete loader;
				
				continue;
			}
			
			QObject* plugin = loader->instance();
			ccPluginInterface* ccPlugin = dynamic_cast<ccPluginInterface*>(plugin);
			
			if ( (plugin == nullptr) || (ccPlugin == nullptr) )
			{				
				if ( plugin == nullptr )
				{
					ccLog::Warning( tr( "\t%1 does not seem to be a valid plugin\t(%2)" ).arg( fileName, loader->errorString() ) );
				}
				else
				{
					ccLog::Warning( tr( "\t%1 does not seem to be a valid plugin or it is not supported by this version" ).arg( fileName ) );
				}
				
				loader->unload();
				
				delete loader;
				
				continue;
			}
			
			if ( ccPlugin->getName().isEmpty() )
			{
				ccLog::Error( tr( "Plugin %1 has a blank name" ).arg( fileName ) );
				
				loader->unload();
				
				delete loader;
				
				continue;
			}
			
			ccPlugin->setIID( pluginIID );
			
			QPluginLoader* previousLoader = pluginIIDToLoaderMap.value( pluginIID );
			
			// If we have already loaded a plugin with this IID, unload it and replace the interface in the plugin list
			if ( previousLoader != nullptr )
			{
				ccPluginInterface* pluginInterface = dynamic_cast<ccPluginInterface *>( previousLoader->instance() );
				
				// maintain the order of the plugin list
				const int index = m_pluginList.indexOf( pluginInterface );
				m_pluginList.replace( index, ccPlugin );
				
				previousLoader->unload();
				
				delete previousLoader;
				
				ccLog::Warning( tr( "\t%1 overridden" ).arg( fileName ) );
			}
			else
			{
				m_pluginList.push_back( ccPlugin );
			}
			
			pluginIIDToLoaderMap[pluginIID] = loader;
			
			ccLog::Print( tr( "\tPlugin found: %1 (%2)" ).arg( ccPlugin->getName(), fileName ) );
		}
	}
	
	const auto loaders = pluginIIDToLoaderMap.values();
	
	for ( QPluginLoader* loader : loaders )
	{
		delete loader;
	}
}

QStringList ccPluginManager::disabledPluginIIDs() const
{	
	QSettings settings;
	
	settings.beginGroup( "Plugins" );
	
	return settings.value( "Disabled" ).toStringList();
}