1 /*
2  * PluginFactory.h
3  *
4  * Copyright (c) 2015 Lukas W <lukaswhl/at/gmail.com>
5  *
6  * This file is part of LMMS - https://lmms.io
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public
19  * License along with this program (see COPYING); if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301 USA.
22  *
23  */
24 
25 #ifndef PLUGINFACTORY_H
26 #define PLUGINFACTORY_H
27 
28 #include <memory>
29 
30 #include <QtCore/QFileInfo>
31 #include <QtCore/QHash>
32 #include <QtCore/QList>
33 
34 #include "export.h"
35 #include "Plugin.h"
36 
37 class QLibrary;
38 
39 class EXPORT PluginFactory
40 {
41 public:
42 	struct PluginInfo
43 	{
PluginInfoPluginInfo44 		PluginInfo() : library(nullptr), descriptor(nullptr) {}
45 
46 		const QString name() const;
47 		QFileInfo file;
48 		std::shared_ptr<QLibrary> library;
49 		Plugin::Descriptor* descriptor;
50 
isNullPluginInfo51 		bool isNull() const {return ! library;}
52 	};
53 	typedef QList<PluginInfo> PluginInfoList;
54 	typedef QMultiMap<Plugin::PluginTypes, Plugin::Descriptor*> DescriptorMap;
55 
56 	PluginFactory();
57 	~PluginFactory();
58 
59 	/// Returns the singleton instance of PluginFactory. You won't need to call
60 	/// this directly, use pluginFactory instead.
61 	static PluginFactory* instance();
62 
63 	/// Returns a list of all found plugins' descriptors.
64 	const Plugin::DescriptorList descriptors() const;
65 	const Plugin::DescriptorList descriptors(Plugin::PluginTypes type) const;
66 
67 	/// Returns a list of all found plugins' PluginFactory::PluginInfo objects.
68 	const PluginInfoList& pluginInfos() const;
69 	/// Returns a plugin that support the given file extension
70 	const PluginInfo pluginSupportingExtension(const QString& ext);
71 
72 	/// Returns the PluginInfo object of the plugin with the given name.
73 	/// If the plugin is not found, an empty PluginInfo is returned (use
74 	/// PluginInfo::isNull() to check this).
75 	const PluginInfo pluginInfo(const char* name) const;
76 
77 	/// When loading a library fails during discovery, the error string is saved.
78 	/// It can be retrieved by calling this function.
79 	QString errorString(QString pluginName) const;
80 
81 public slots:
82 	void discoverPlugins();
83 
84 private:
85 	DescriptorMap m_descriptors;
86 	PluginInfoList m_pluginInfos;
87 	QMap<QString, PluginInfo> m_pluginByExt;
88 
89 	QHash<QString, QString> m_errors;
90 
91 	static std::unique_ptr<PluginFactory> s_instance;
92 };
93 
94 #define pluginFactory PluginFactory::instance()
95 
96 #endif // PLUGINFACTORY_H
97