File indexing completed on 2024-05-19 04:49:40

0001 /****************************************************************************************
0002  * Copyright (c) 2009 Leo Franchi <lfranchi@kde.org>                                    *
0003  * Copyright (c) 2010, 2011, 2013 Ralf Engels <ralf-engels@gmx.de>                      *
0004  *                                                                                      *
0005  * This program is free software; you can redistribute it and/or modify it under        *
0006  * the terms of the GNU General Public License as published by the Free Software        *
0007  * Foundation; either version 2 of the License, or (at your option) any later           *
0008  * version.                                                                             *
0009  *                                                                                      *
0010  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0012  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0013  *                                                                                      *
0014  * You should have received a copy of the GNU General Public License along with         *
0015  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0016  ****************************************************************************************/
0017 
0018 #ifndef ECHO_NEST_BIAS_H
0019 #define ECHO_NEST_BIAS_H
0020 
0021 #include "dynamic/biases/TagMatchBias.h"
0022 
0023 #include <QMutex>
0024 
0025 namespace KIO {
0026     class StoredTransferJob;
0027 }
0028 class KJob;
0029 class QUrl;
0030 
0031 namespace Dynamic
0032 {
0033     /**
0034      * This is a bias which adds the suggested songs to the playlist.
0035      */
0036     class EchoNestBias : public SimpleMatchBias
0037     {
0038         Q_OBJECT
0039 
0040         public:
0041 
0042             /** The tracks that are used for the matching */
0043             enum MatchType
0044             {
0045                 PreviousTrack,
0046                 Playlist
0047             };
0048 
0049             EchoNestBias();
0050             ~EchoNestBias() override;
0051 
0052             void fromXml( QXmlStreamReader *reader ) override;
0053             void toXml( QXmlStreamWriter *writer ) const override;
0054 
0055             static QString sName();
0056             QString name() const override;
0057             QString toString() const override;
0058 
0059             QWidget* widget( QWidget* parent = nullptr ) override;
0060 
0061             Dynamic::TrackSet matchingTracks( const Meta::TrackList& playlist,
0062                                                       int contextCount, int finalCount,
0063                                                       const Dynamic::TrackCollectionPtr &universe ) const override;
0064 
0065             bool trackMatches( int position,
0066                                        const Meta::TrackList& playlist,
0067                                        int contextCount ) const override;
0068 
0069 
0070             MatchType match() const;
0071             void setMatch( MatchType value );
0072 
0073         public Q_SLOTS:
0074             void invalidate() override;
0075 
0076         private Q_SLOTS:
0077             void newQuery() override;
0078             virtual void newSimilarArtistQuery();
0079 
0080             void similarArtistQueryDone( KJob* );
0081             void updateFinished() override;
0082 
0083             void setMatchTypePlaylist( bool );
0084 
0085         private:
0086             /** Returns the artists we should lookup */
0087             QStringList currentArtists( int position, const Meta::TrackList& playlist ) const;
0088             static QUrl createUrl( const QString &method, QMultiMap< QString, QString > params );
0089             static QString nameForMatch( MatchType match );
0090             static MatchType matchForName( const QString &name );
0091 
0092             /** Returns the key used for m_tracksMap */
0093             static QString tracksMapKey( const QStringList &artists );
0094 
0095             void saveDataToFile() const;
0096 
0097             void readSimilarArtists( QXmlStreamReader *reader );
0098             void loadDataFromFile();
0099 
0100             /** The artist we are currently querying. */
0101             mutable QStringList m_currentArtists;
0102             mutable QMap< KIO::StoredTransferJob*, QString> m_artistNameQueries;
0103             mutable KIO::StoredTransferJob* m_artistSuggestedQuery;
0104 
0105             MatchType m_match;
0106 
0107             mutable QMutex m_mutex; // mutex protecting all of the below structures
0108             mutable QMap< QString, QStringList> m_similarArtistMap;
0109             mutable QMap< QString, TrackSet> m_tracksMap;
0110 
0111         private:
0112             Q_DISABLE_COPY(EchoNestBias)
0113     };
0114 
0115 
0116     class AMAROK_EXPORT EchoNestBiasFactory : public Dynamic::AbstractBiasFactory
0117     {
0118         public:
0119             QString i18nName() const override;
0120             QString name() const override;
0121             QString i18nDescription() const override;
0122             BiasPtr createBias() override;
0123     };
0124 }
0125 
0126 #endif