PluginFactory.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #ifndef PLUGINFACTORY_H
  25. #define PLUGINFACTORY_H
  26. #include <memory>
  27. #include <string>
  28. #include <QtCore/QFileInfo>
  29. #include <QtCore/QHash>
  30. #include <QtCore/QList>
  31. #include <QtCore/QString>
  32. #include <QtCore/QVector>
  33. #include "lmms_export.h"
  34. #include "Plugin.h"
  35. class QLibrary;
  36. class LMMS_EXPORT PluginFactory
  37. {
  38. public:
  39. struct PluginInfo
  40. {
  41. const QString name() const;
  42. QFileInfo file;
  43. std::shared_ptr<QLibrary> library = nullptr;
  44. Plugin::Descriptor* descriptor = nullptr;
  45. bool isNull() const {return ! library;}
  46. };
  47. typedef QList<PluginInfo> PluginInfoList;
  48. typedef QMultiMap<Plugin::PluginTypes, Plugin::Descriptor*> DescriptorMap;
  49. PluginFactory();
  50. ~PluginFactory();
  51. static void setupSearchPaths();
  52. /// Returns the singleton instance of PluginFactory. You won't need to call
  53. /// this directly, use pluginFactory instead.
  54. static PluginFactory* instance();
  55. /// Returns a list of all found plugins' descriptors.
  56. const Plugin::DescriptorList descriptors() const;
  57. const Plugin::DescriptorList descriptors(Plugin::PluginTypes type) const;
  58. struct PluginInfoAndKey
  59. {
  60. PluginInfo info;
  61. Plugin::Descriptor::SubPluginFeatures::Key key;
  62. bool isNull() const { return info.isNull(); }
  63. };
  64. /// Returns a list of all found plugins' PluginFactory::PluginInfo objects.
  65. const PluginInfoList& pluginInfos() const;
  66. /// Returns a plugin that support the given file extension
  67. const PluginInfoAndKey pluginSupportingExtension(const QString& ext);
  68. /// Returns the PluginInfo object of the plugin with the given name.
  69. /// If the plugin is not found, an empty PluginInfo is returned (use
  70. /// PluginInfo::isNull() to check this).
  71. const PluginInfo pluginInfo(const char* name) const;
  72. /// When loading a library fails during discovery, the error string is saved.
  73. /// It can be retrieved by calling this function.
  74. QString errorString(QString pluginName) const;
  75. public slots:
  76. void discoverPlugins();
  77. private:
  78. DescriptorMap m_descriptors;
  79. PluginInfoList m_pluginInfos;
  80. QMap<QString, PluginInfoAndKey> m_pluginByExt;
  81. QVector<std::string> m_garbage; //!< cleaned up at destruction
  82. QHash<QString, QString> m_errors;
  83. static std::unique_ptr<PluginFactory> s_instance;
  84. };
  85. //Short-hand function
  86. LMMS_EXPORT PluginFactory* getPluginFactory();
  87. #endif // PLUGINFACTORY_H