File indexing completed on 2024-04-21 04:47:53

0001 /****************************************************************************************
0002  * Copyright (c) 2004-2013 Mark Kretschmann <kretschmann@kde.org>                       *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #ifndef AMAROK_PLUGINMANAGER_H
0018 #define AMAROK_PLUGINMANAGER_H
0019 
0020 #include "amarok_export.h"
0021 
0022 #include <KPluginInfo>
0023 #include <KPluginMetaData>
0024 
0025 #include <QVector>
0026 
0027 namespace Plugins {
0028 
0029 class PluginFactory;
0030 
0031 class AMAROK_EXPORT PluginManager : public QObject
0032 {
0033     Q_OBJECT
0034     Q_PROPERTY( int pluginFrameworkVersion READ pluginFrameworkVersion )
0035 
0036     public:
0037         /** Type of the plugin.
0038          *
0039          *  Will be determined by the KPluginInfo::category
0040          */
0041         enum Type
0042         {
0043             Collection = 1, ///< the plugin implements a CollectionFactory
0044             Service = 2,    ///< this is a service plugin
0045             Importer = 3,   ///< this plugin implements importer functionality
0046             Storage = 4,    ///< the plugin implements a StorageFactory
0047         };
0048         Q_ENUM( Type )
0049 
0050         ~PluginManager() override;
0051 
0052         static PluginManager *instance();
0053 
0054         /** Destroys the instance of the PluginManager.
0055          *
0056          *  The order of the destruction is somewhat important.
0057          *  The PluginManager needs to be destroyed after all collections
0058          *  have been removed and before the CollectionManager,
0059          *  the ServicePluginManager and the StatSyncing::Controller are destroyed.
0060          */
0061         static void destroy();
0062         static int pluginFrameworkVersion();
0063 
0064         /**
0065          * Load any services that are configured to be loaded
0066          */
0067         void init();
0068 
0069         /** Returns enabled plugin factories for the given plugin type.
0070          *
0071          *  This function will only return factories for enabled plugins.
0072          */
0073         QList<QSharedPointer<PluginFactory> > factories( Type type ) const;
0074 
0075         KPluginInfo::List plugins( Type type ) const;
0076 
0077         QVector<KPluginMetaData> enabledPlugins(Type type ) const;
0078 
0079         /** Check if any services were disabled and needs to be removed, or any
0080          *  that are hidden needs to be enabled
0081          *
0082          * This function will call the sub plugin managers (like CollectionManager)
0083          * setFactories function.
0084          */
0085         void checkPluginEnabledStates();
0086 
0087     private:
0088         /** Tries finding Amarok plugins */
0089         QVector<KPluginMetaData> findPlugins();
0090 
0091         /** Returns true if the plugin is enabled.
0092          *  This function will check the default enabled state,
0093          *  the Amarok configuration state and the primary collection.
0094          *
0095          *  @returns true if the plugin is enabled.
0096          */
0097         bool isPluginEnabled( const KPluginMetaData &plugin ) const;
0098 
0099         /** Creates a factories for a plugin */
0100         QSharedPointer<PluginFactory> createFactory( const KPluginMetaData &pluginInfo );
0101 
0102         /// contains the names of all KPluginInfos that have factories created
0103         QVector<KPluginMetaData> m_plugins;
0104         QHash<Type, QList<KPluginMetaData> > m_pluginsByType;
0105         QHash<Type, QList<QSharedPointer<PluginFactory> > > m_factoriesByType;
0106         QHash<QString, QSharedPointer<PluginFactory>> m_factoryCreated;
0107 
0108         static const int s_pluginFrameworkVersion;
0109         static PluginManager *s_instance;
0110 
0111         explicit PluginManager( QObject *parent = nullptr );
0112 };
0113 
0114 } // namespace Plugins
0115 
0116 namespace The
0117 {
0118     inline Plugins::PluginManager *pluginManager() { return Plugins::PluginManager::instance(); }
0119 }
0120 
0121 #endif /* AMAROK_PLUGINMANAGER_H */