File indexing completed on 2024-05-05 04:48:30

0001 /****************************************************************************************
0002  * Copyright (c) 2010 Ralf Engels <ralf-engels@gmx.de>                                  *
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) version 3 or        *
0007  * any later version accepted by the membership of KDE e.V. (or its successor approved  *
0008  * by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of  *
0009  * version 3 of the license.                                                            *
0010  *                                                                                      *
0011  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0012  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0013  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0014  *                                                                                      *
0015  * You should have received a copy of the GNU General Public License along with         *
0016  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0017  ****************************************************************************************/
0018 
0019 #ifndef AMAROK_BIAS_FACTORY_H
0020 #define AMAROK_BIAS_FACTORY_H
0021 
0022 #include "amarok_export.h"
0023 #include "dynamic/Bias.h"
0024 
0025 #include <QObject>
0026 #include <KLocalizedString>
0027 
0028 class QXmlStreamReader;
0029 class QXmlStreamWriter;
0030 
0031 namespace Dynamic
0032 {
0033     /** A bias that will be used when a "real" bias could not be found.
0034         This bias will listen to the BiasFactory and present a "stand in" bias
0035         until a new factory with it's name becomes available.
0036         This will allow services with a bias to be switched off without their
0037         bias getting removed or otherwise corrupted.
0038     */
0039     class ReplacementBias : public RandomBias
0040     {
0041         Q_OBJECT
0042 
0043         public:
0044             explicit ReplacementBias( const QString &n );
0045             ReplacementBias( const QString &n, QXmlStreamReader *reader );
0046 
0047             void toXml( QXmlStreamWriter *writer ) const override;
0048 
0049             static QString sName();
0050             QString name() const override;
0051             QString toString() const override;
0052 
0053             QWidget* widget( QWidget* parent = nullptr ) override;
0054 
0055         protected Q_SLOTS:
0056                 void factoryChanged();
0057 
0058         private:
0059                 QString m_name;
0060                 QByteArray m_html;
0061 
0062                 Q_DISABLE_COPY(ReplacementBias)
0063     };
0064 
0065     /**
0066      * The factory which creates bias entries on demand. As the user can create any number
0067      * of biases from from the bias addition widget, new custom biass types need to be able to be
0068      * generated on command and at runtime.
0069      **/
0070     class AMAROK_EXPORT AbstractBiasFactory
0071     {
0072         public:
0073             AbstractBiasFactory() {}
0074             virtual ~AbstractBiasFactory() {}
0075 
0076             /** Returns the translated name of the type of bias.
0077                 This one is used in the combo boxes when selecting the bias.
0078                 It could be eg. "Last.fm Similar Artists"
0079              */
0080             virtual QString i18nName() const = 0;
0081 
0082             /** Returns an internal non-translatable name for this custom bias type.
0083                 This name must be unique over all biases and will also be used
0084                 when reading and writing a bias to xml.
0085              */
0086             virtual QString name() const = 0;
0087 
0088             /** Returns the translated description of the bias */
0089             virtual QString i18nDescription() const = 0;
0090 
0091             /** Create the custom bias. The caller takes owner of the pointer
0092              */
0093             virtual BiasPtr createBias() = 0;
0094 
0095             /** Creates a new custom bias from xml data
0096              */
0097             virtual BiasPtr createFromXml( QXmlStreamReader *reader );
0098     };
0099 
0100     class AMAROK_EXPORT BiasFactory : public QObject
0101     {
0102         Q_OBJECT
0103 
0104     public:
0105         static BiasFactory* instance();
0106 
0107         /** Add a new CustomBiasEntry to the registry.
0108              It will show up for users when then select the type of bias they want.
0109          */
0110         static void registerNewBiasFactory( AbstractBiasFactory* factory );
0111 
0112         /** Remove CustomBiasEntry from the list of bias types that the user can select.
0113          */
0114         static void removeBiasFactory( AbstractBiasFactory* factory );
0115 
0116         /** Helper function to get a bias from an xml tag */
0117         static BiasPtr fromXml( QXmlStreamReader *reader );
0118 
0119         /** Helper function to get a bias from an name */
0120         static BiasPtr fromName( const QString &name );
0121 
0122         /**
0123          * Returns all the current registered factories for this CustomBias
0124          */
0125         static QList<AbstractBiasFactory*> factories();
0126 
0127     Q_SIGNALS:
0128         /** Emitted when the list of bias factories was changed. */
0129         void changed();
0130 
0131     private:
0132         BiasFactory( QObject *parent = nullptr );
0133         ~BiasFactory() override;
0134 
0135         void emitChanged();
0136 
0137         static BiasFactory* s_instance;
0138         static QList<Dynamic::AbstractBiasFactory*> s_biasFactories;
0139     };
0140 }
0141 
0142 #endif