1 /****************************************************************************************
2  * Copyright (c) 2009 Leo Franchi <lfranchi@kde.org>                                    *
3  * Copyright (c) 2011 Ralf Engels <ralf-engels@gmx.de>                                  *
4  *                                                                                      *
5  * This program is free software; you can redistribute it and/or modify it under        *
6  * the terms of the GNU General Public License as published by the Free Software        *
7  * Foundation; either version 2 of the License, or (at your option) any later           *
8  * version.                                                                             *
9  *                                                                                      *
10  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
11  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
12  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
13  *                                                                                      *
14  * You should have received a copy of the GNU General Public License along with         *
15  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
16  ****************************************************************************************/
17 
18 #ifndef LASTFM_BIAS_H
19 #define LASTFM_BIAS_H
20 
21 #include "dynamic/biases/TagMatchBias.h"
22 
23 #include <QMutex>
24 
25 namespace Dynamic
26 {
27 
28     /** This is a bias which adds the suggested songs to the playlist. */
29     class LastFmBias : public SimpleMatchBias
30     {
31         Q_OBJECT
32 
33         public:
34 
35             /** The tracks that are used for the matching */
36             enum MatchType
37             {
38                 SimilarArtist,
39                 SimilarTrack
40             };
41 
42             LastFmBias();
43             ~LastFmBias() override;
44 
45             void fromXml( QXmlStreamReader *reader ) override;
46             void toXml( QXmlStreamWriter *writer ) const override;
47 
48             static QString sName();
49             QString name() const override;
50             QString toString() const override;
51 
52             QWidget* widget( QWidget* parent = nullptr ) override;
53 
54             virtual Dynamic::TrackSet matchingTracks( const Meta::TrackList& playlist,
55                                                       int contextCount, int finalCount,
56                                                       Dynamic::TrackCollectionPtr universe ) const;
57 
58             bool trackMatches( int position,
59                                const Meta::TrackList& playlist,
60                                int contextCount ) const override;
61 
62 
63             MatchType match() const;
64             void setMatch( MatchType value );
65 
66         public Q_SLOTS:
67             void invalidate() override;
68 
69         private Q_SLOTS:
70             void newQuery() override;
71             virtual void newSimilarQuery();
72 
73             void similarArtistQueryDone();
74             void similarTrackQueryDone();
75             void queryFailed( const char *message );
76 
77             void setMatchTypeArtist( bool matchArtist );
78 
79         private:
80             /** The pair is used for the tracks */
81             typedef QPair<QString, QString> TitleArtistPair;
82 
83             static QString nameForMatch( MatchType match );
84             static MatchType matchForName( const QString &name );
85 
86             void saveDataToFile() const;
87 
88             void readSimilarArtists( QXmlStreamReader *reader );
89             TitleArtistPair readTrack( QXmlStreamReader *reader );
90             void readSimilarTracks( QXmlStreamReader *reader );
91             void loadDataFromFile();
92 
93             mutable QString m_currentArtist;
94             mutable QString m_currentTrack;
95 
96             MatchType m_match;
97 
98             mutable QMutex m_mutex; // mutex protecting all of the below structures
99             mutable QMap< QString, QStringList> m_similarArtistMap;
100             mutable QMap< TitleArtistPair, QList<TitleArtistPair> > m_similarTrackMap;
101             mutable QMap< QString, TrackSet> m_tracksMap; // for artist AND album
102 
103         private:
104             Q_DISABLE_COPY(LastFmBias)
105     };
106 
107 
108     class LastFmBiasFactory : public Dynamic::AbstractBiasFactory
109     {
110         public:
111             QString i18nName() const override;
112             QString name() const override;
113             QString i18nDescription() const override;
114             BiasPtr createBias() override;
115     };
116 
117 }
118 
119 #endif
120