| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "miscellaneous/pluginfactory.h" |
| 4 | |
| 5 | #include "definitions/definitions.h" |
| 6 | #include "services/abstract/serviceentrypoint.h" |
| 7 | |
| 8 | #include <QCoreApplication> |
| 9 | #include <QDir> |
| 10 | #include <QDirIterator> |
| 11 | #include <QPluginLoader> |
| 12 | |
| 13 | PluginFactory::PluginFactory() {} |
| 14 | |
| 15 | QList<ServiceEntryPoint*> PluginFactory::loadPlugins() const { |
| 16 | QList<ServiceEntryPoint*> plugins; |
| 17 | QStringList plugin_ids; |
| 18 | |
| 19 | const QString plugin_name_wildcard = pluginNameWildCard(); |
| 20 | const auto plugins_paths = pluginPaths(); |
| 21 | const auto backup_current_dir = QDir::currentPath(); |
| 22 | |
| 23 | for (const QString& plugin_folder : plugins_paths) { |
| 24 | QDirIterator dir_iter(plugin_folder, |
| 25 | {plugin_name_wildcard}, |
| 26 | QDir::Filter::Files, |
| 27 | #if !defined(NDEBUG) || defined(LOAD_PLUGINS_FROM_PARENT) |
| 28 | QDirIterator::IteratorFlag::Subdirectories); |
| 29 | #else |
| 30 | QDirIterator::IteratorFlag::NoIteratorFlags); |
| 31 | #endif |
| 32 | |
| 33 | qDebugNN << LOGSEC_CORE << "Checking for plugins in" << QUOTE_W_SPACE_DOT(plugin_folder); |
| 34 | |
| 35 | while (dir_iter.hasNext()) { |
| 36 | dir_iter.next(); |
| 37 | |
| 38 | const QFileInfo& plugin_file = dir_iter.fileInfo(); |
| 39 | |
| 40 | qApp->addLibraryPath(plugin_file.absolutePath()); |
| 41 | QDir::setCurrent(plugin_file.absolutePath()); |
| 42 | |
| 43 | QPluginLoader loader(plugin_file.absoluteFilePath()); |
| 44 | ServiceEntryPoint* plugin_instance = qobject_cast<ServiceEntryPoint*>(loader.instance()); |
| 45 | |
| 46 | QDir::setCurrent(backup_current_dir); |
| 47 | |
| 48 | if (plugin_instance == nullptr) { |
| 49 | qCriticalNN << LOGSEC_CORE << "The plugin" << QUOTE_W_SPACE(plugin_file.absoluteFilePath()) |
| 50 | << "was not loaded successfully:" << QUOTE_W_SPACE_DOT(loader.errorString()); |
| 51 | } |
| 52 | else { |
| 53 | if (plugin_ids.contains(plugin_instance->code())) { |
| 54 | qCriticalNN << LOGSEC_CORE << "Plugin" << QUOTE_W_SPACE(plugin_instance->code()) |
| 55 | << "was already loaded before. Skipping now."; |
| 56 | } |
| 57 | else { |
| 58 | qDebugNN << LOGSEC_CORE << "Plugin" << QUOTE_W_SPACE(plugin_file.absoluteFilePath()) << "loaded."; |
| 59 | |
| 60 | plugin_instance->setIsDynamicallyLoaded(true); |
| 61 | plugin_ids.append(plugin_instance->code()); |
| 62 | plugins.append(plugin_instance); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | QDir::setCurrent(backup_current_dir); |
| 69 | |
| 70 | return plugins; |
| 71 | } |
| 72 | |
| 73 | QStringList PluginFactory::pluginPaths() const { |
| 74 | QStringList paths; |
| 75 | |
| 76 | #if defined(Q_OS_LINUX) || defined(Q_OS_FREEBSD) || defined(BUILD_MSYS2) |
| 77 | paths << QCoreApplication::applicationDirPath() + QDir::separator() + QL1S("..") + QDir::separator() + |
| 78 | QL1S(RSSGUARD_LIBDIR) + QDir::separator() + QL1S(APP_LOW_NAME); |
| 79 | #elif defined(Q_OS_WIN) |
| 80 | paths << QCoreApplication::applicationDirPath() + QDir::separator() + QL1S("plugins"); |
| 81 | #else |
| 82 | paths << QCoreApplication::applicationDirPath(); |
| 83 | #endif |
| 84 | |
| 85 | #if !defined(NDEBUG) || defined(LOAD_PLUGINS_FROM_PARENT) |
| 86 | paths << QCoreApplication::applicationDirPath() + QDir::separator() + QL1S("..") + QDir::separator(); |
| 87 | #endif |
| 88 | |
| 89 | return paths; |
| 90 | } |
| 91 | |
| 92 | QString PluginFactory::pluginNameWildCard() const { |
| 93 | #if defined(Q_OS_WIN) |
| 94 | return QSL("*rssguard-*.dll"); |
| 95 | #elif defined(Q_OS_LINUX) |
| 96 | return QSL("*rssguard-*.so"); |
| 97 | #elif defined(Q_OS_MACOS) |
| 98 | return QSL("*rssguard-*.dylib"); |
| 99 | #else |
| 100 | return QSL("*rssguard-*.*"); |
| 101 | #endif |
| 102 | } |