File indexing completed on 2024-05-19 04:50:17

0001 /****************************************************************************************
0002  * Copyright (c) 2012 Matěj Laitl <matej@laitl.cz>                                      *
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 SYNCHRONIZATIONADAPTER_H
0018 #define SYNCHRONIZATIONADAPTER_H
0019 
0020 #include "services/lastfm/LastFmServiceConfig.h"
0021 #include "statsyncing/Provider.h"
0022 
0023 #include <QSemaphore>
0024 
0025 class LastFmServiceConfig;
0026 
0027 class SynchronizationAdapter : public StatSyncing::Provider
0028 {
0029     Q_OBJECT
0030 
0031     public:
0032         /**
0033          * @param config a pointer to Last.fm config
0034          */
0035         explicit SynchronizationAdapter( const LastFmServiceConfigPtr &config );
0036         ~SynchronizationAdapter() override;
0037 
0038         QString id() const override;
0039         QString prettyName() const override;
0040         QString description() const override;
0041         QIcon icon() const override;
0042         qint64 reliableTrackMetaData() const override;
0043         qint64 writableTrackStatsData() const override;
0044         Preference defaultPreference() override;
0045         QSet<QString> artists() override;
0046         StatSyncing::TrackList artistTracks( const QString &artistName ) override;
0047 
0048     Q_SIGNALS:
0049         /// hacks to create and start Last.fm queries in main eventloop
0050         // Last.fm indexes from 1!
0051         void startArtistSearch( int page );
0052         void startTrackSearch( QString artistName, int page );
0053         void startTagSearch( QString artistName, QString trackName );
0054 
0055     private Q_SLOTS:
0056         /// @see startArtistSearch
0057         void slotStartArtistSearch( int page );
0058         void slotStartTrackSearch( QString artistName, int page );
0059         void slotStartTagSearch( QString artistName, QString trackName );
0060 
0061         void slotArtistsReceived();
0062         void slotTracksReceived();
0063         void slotTagsReceived();
0064 
0065     private:
0066         LastFmServiceConfigPtr m_config;
0067 
0068         /**
0069          * number of artist or track entries to request from Last.fm in earch webservice
0070          * query. Last.fm default is 50; the greater the number, the faster the fetching
0071          * (of long lists) is. On the other hand, Last.fm has own limit, 200 works well.
0072          */
0073         static const int s_entriesPerQuery;
0074 
0075         QSet<QString> m_artists;
0076         StatSyncing::TrackList m_tracks;
0077         StatSyncing::TrackList m_tagQueue; // tracks waiting to be assigned tags
0078         /**
0079          * Semaphore for the simplified producer-consumer pattern, where
0080          * slotArtistsReceived() is producer and artist() is consumer, or
0081          * slotTracksReceived() is producer and artistTracks() is consumer, or
0082          * slotTagsReceived() is producer and artistTracks() is consumer.
0083          */
0084         QSemaphore m_semaphore;
0085 };
0086 
0087 #endif // SYNCHRONIZATIONADAPTER_H