d/control: Recommend gdb
[gammaray-debian.git] / pluginmanager.cpp
blob949305df91a99d93f6b85b25d42d2c211ed3f219
1 /*
2 pluginmanager.cpp
4 This file is part of GammaRay, the Qt application inspection and
5 manipulation tool.
7 Copyright (C) 2010-2011 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
8 Author: Kevin Funk <kevin.funk@kdab.com>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "pluginmanager.h"
26 #include "config-gammaray.h"
28 #include <QCoreApplication>
29 #include <QStringList>
30 #include <QDebug>
31 #include <QDir>
32 #include <QLibrary>
34 #include <QPluginLoader>
35 #include "proxytoolfactory.h"
37 #define IF_DEBUG(x)
39 using namespace GammaRay;
40 using namespace std;
42 static const QLatin1String GAMMARAY_PLUGIN_SUFFIX("gammaray");
43 PluginManager *PluginManager::s_instance = 0;
45 PluginManager *PluginManager::instance()
47 if (!s_instance) {
48 s_instance = new PluginManager();
49 s_instance->scan();
51 return s_instance;
54 PluginManager::PluginManager()
56 QCoreApplication::addLibraryPath(QLatin1String(GAMMARAY_PLUGIN_INSTALL_DIR));
59 QStringList PluginManager::pluginPaths() const
61 QStringList pluginPaths;
63 // add plugins from gammaray's build directory
64 pluginPaths << QLatin1String(GAMMARAY_BUILD_DIR) +
65 QDir::separator() + "lib" +
66 QDir::separator() + "plugins" +
67 QDir::separator() + GAMMARAY_PLUGIN_SUFFIX;
69 QStringList libraryPaths = QCoreApplication::libraryPaths();
70 foreach (const QString &libraryPath, libraryPaths) {
71 pluginPaths << libraryPath + QDir::separator() + GAMMARAY_PLUGIN_SUFFIX;
74 return pluginPaths;
77 void PluginManager::scan()
79 // TODO: temporary work-around to avoid loading the same plugin two times
80 // should be fixed when providing valid plugin spec files
81 QStringList loadedPluginNames;
83 foreach (const QString &pluginPath, pluginPaths()) {
84 const QDir dir(pluginPath);
85 IF_DEBUG(cout << "checking plugin path: " << qPrintable(dir.absolutePath()) << endl);
86 foreach (const QString &plugin, dir.entryList(QDir::Files)) {
87 const QString pluginFile = dir.absoluteFilePath(plugin);
88 const QFileInfo pluginInfo(pluginFile);
89 const QString pluginName = pluginInfo.baseName();
91 if (loadedPluginNames.contains(pluginName)) {
92 cout << "not loading plugin, already loaded: " << qPrintable(pluginFile) << endl;
93 continue;
96 if (pluginInfo.suffix() == QLatin1String("desktop")) {
97 ProxyToolFactory *proxy = new ProxyToolFactory(pluginFile);
98 if (!proxy->isValid()) {
99 std::cerr << "invalid plugin " << qPrintable(pluginFile) << std::endl;
100 delete proxy;
101 } else {
102 IF_DEBUG(cout << "plugin looks valid " << qPrintable(pluginFile) << endl;)
103 m_plugins.push_back(proxy);
104 loadedPluginNames << pluginName;
111 QVector<ToolFactory *> PluginManager::plugins()
113 return m_plugins;