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

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 STATSYNCING_SYNCHRONIZETRACKSJOB_H
0018 #define STATSYNCING_SYNCHRONIZETRACKSJOB_H
0019 
0020 #include "core/meta/forward_declarations.h"
0021 #include "statsyncing/Options.h"
0022 #include "statsyncing/ScrobblingService.h"
0023 #include "statsyncing/Track.h"
0024 
0025 #include <ThreadWeaver/Job>
0026 
0027 #include <QMap>
0028 
0029 namespace StatSyncing
0030 {
0031     class TrackTuple;
0032 
0033     /**
0034      * A job to call TrackTuple::synchronize() in order not to make delays in the main
0035      * loop.
0036      */
0037     class SynchronizeTracksJob : public QObject, public ThreadWeaver::Job
0038     {
0039         Q_OBJECT
0040 
0041         public:
0042             explicit SynchronizeTracksJob( const QList<TrackTuple> &tuples,
0043                                            const TrackList &trackToScrobble,
0044                                            const Options &options, QObject *parent = nullptr );
0045 
0046             /**
0047              * Return count of tracks that were updated during synchronization
0048              */
0049             int updatedTracksCount() const;
0050 
0051             /**
0052              * Return scrobble counts per scrobbling service and their status.
0053              */
0054             QMap<ScrobblingServicePtr, QMap<ScrobblingService::ScrobbleError, int> > scrobbles();
0055 
0056         public Q_SLOTS:
0057             /**
0058              * Abort the job as soon as possible.
0059              */
0060             void abort();
0061 
0062         Q_SIGNALS:
0063             /**
0064              * Emitted when matcher gets to know total number of steps it will take to
0065              * match all tracks.
0066              */
0067             void totalSteps( int steps );
0068 
0069             /**
0070              * Emitted when one progress step has been finished.
0071              */
0072             void incrementProgress();
0073 
0074             /**
0075              * Emitted from worker thread when all time-consuming operations are done.
0076              */
0077             void endProgressOperation( QObject *owner );
0078 
0079             /**
0080              * Helper to cross thread boundary between this worker thread and main thread
0081              * where StatSyncing::Controller lives.
0082              */
0083             void scrobble( const Meta::TrackPtr &track, double playedFraction,
0084                            const QDateTime &time );
0085 
0086             /** This signal is emitted when this job is being processed by a thread. */
0087             void started(ThreadWeaver::JobPointer);
0088             /** This signal is emitted when the job has been finished (no matter if it succeeded or not). */
0089             void done(ThreadWeaver::JobPointer);
0090             /** This job has failed.
0091              * This signal is emitted when success() returns false after the job is executed. */
0092             void failed(ThreadWeaver::JobPointer);
0093 
0094         protected:
0095             void defaultBegin(const ThreadWeaver::JobPointer& job, ThreadWeaver::Thread *thread) override;
0096             void defaultEnd(const ThreadWeaver::JobPointer& job, ThreadWeaver::Thread *thread) override;
0097             void run(ThreadWeaver::JobPointer self = QSharedPointer<ThreadWeaver::Job>(), ThreadWeaver::Thread *thread = nullptr) override;
0098 
0099         private Q_SLOTS:
0100             void slotTrackScrobbled( const ScrobblingServicePtr &service, const Meta::TrackPtr &track );
0101             void slotScrobbleFailed( const ScrobblingServicePtr &service, const Meta::TrackPtr &track, int error );
0102 
0103         private:
0104             bool m_abort;
0105             QList<TrackTuple> m_tuples;
0106             TrackList m_tracksToScrobble;
0107             QSet<Meta::TrackPtr> m_scrobbledTracks;
0108             QMap<ScrobblingServicePtr, QMap<ScrobblingService::ScrobbleError, int> > m_scrobbles;
0109             int m_updatedTracksCount;
0110             const Options m_options;
0111     };
0112 
0113 } // namespace StatSyncing
0114 
0115 #endif // STATSYNCING_SYNCHRONIZETRACKSJOB_H