File indexing completed on 2025-01-05 04:25:59

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 COPYTRACKSJOB_H
0018 #define COPYTRACKSJOB_H
0019 
0020 #include "IpodCollection.h"
0021 #include "core/meta/forward_declarations.h"
0022 #include "core/transcoding/TranscodingConfiguration.h"
0023 
0024 #include <ThreadWeaver/Job>
0025 
0026 #include <QMap>
0027 #include <QSemaphore>
0028 
0029 class KJob;
0030 
0031 class IpodCopyTracksJob : public QObject, public ThreadWeaver::Job
0032 {
0033     Q_OBJECT
0034 
0035     public:
0036         enum CopiedStatus {
0037             Duplicate, /// a track with same meta-data is already in iPod collection
0038             ExceededingSafeCapacity, /// would exceed "safe" capacity
0039             NotPlayable, /// track format would not be playable on connected iPod
0040             CopyingFailed, /// KIO failed to copy the file
0041             InternalError, /// all other reasons that have no nice user-tellable reason
0042             Success /// copied successfully
0043         };
0044 
0045         /**
0046          * @param sources list of sources
0047          * @param collection collection
0048          * @param configuration transcoding configuration
0049          * @param goingToRemoveSources whether this is in fact a move operation
0050          */
0051         IpodCopyTracksJob( const QMap<Meta::TrackPtr,QUrl> &sources,
0052                            const QPointer<IpodCollection> &collection,
0053                            const Transcoding::Configuration &configuration,
0054                            bool goingToRemoveSources );
0055         void run(ThreadWeaver::JobPointer self = QSharedPointer<ThreadWeaver::Job>(), ThreadWeaver::Thread *thread = nullptr) override;
0056 
0057     public Q_SLOTS:
0058         void abort();
0059 
0060     Q_SIGNALS:
0061         // a hack to create QueryMaken in a thread with event loop:
0062         void startDuplicateTrackSearch( const Meta::TrackPtr &track );
0063 
0064         // a hack to create copyjob in a thread with event loop:
0065         void startCopyOrTranscodeJob( const QUrl &src, const QUrl &dest, bool isJustCopy );
0066 
0067         // a hack to display KMessageBox in a gui thread:
0068         void displayErrorDialog();
0069 
0070         // signals for progress operation:
0071         void incrementProgress();
0072         void endProgressOperation( QObject *obj );
0073         void totalSteps( int steps ); // not used, defined to keep QObject::connect warning quiet
0074 
0075         /**
0076          * Signal various track copy statuses back to IpodCollectionLocation
0077          * @param srcTrack source track, always non-nul
0078          * @param destTrack destination track on iPod, copied one or existing if
0079          *                  status == Duplicate; may be null
0080          * @param status copying status
0081          */
0082         void signalTrackProcessed( Meta::TrackPtr srcTrack, Meta::TrackPtr destTrack, IpodCopyTracksJob::CopiedStatus status );
0083 
0084         /** This signal is emitted when this job is being processed by a thread. */
0085         void started(ThreadWeaver::JobPointer);
0086         /** This signal is emitted when the job has been finished (no matter if it succeeded or not). */
0087         void done(ThreadWeaver::JobPointer);
0088         /** This job has failed.
0089          * This signal is emitted when success() returns false after the job is executed. */
0090         void failed(ThreadWeaver::JobPointer);
0091 
0092     private Q_SLOTS:
0093         /// @see startDuplicateTrackSearch()
0094         void slotStartDuplicateTrackSearch( const Meta::TrackPtr &track );
0095         void slotDuplicateTrackSearchNewResult( const Meta::TrackList &tracks );
0096         void slotDuplicateTrackSearchQueryDone();
0097 
0098         /// @see startCopyJob()
0099         void slotStartCopyOrTranscodeJob( const QUrl &sourceUrl, const QUrl &destUrl,
0100                                           bool isJustCopy );
0101         void slotCopyOrTranscodeJobFinished( KJob *job );
0102 
0103         /// @see displayErrorDialog()
0104         void slotDisplayErrorDialog();
0105 
0106     private:
0107         void trackProcessed( CopiedStatus status, const Meta::TrackPtr &srcTrack, const Meta::TrackPtr &destTrack = Meta::TrackPtr() );
0108 
0109         QPointer<IpodCollection> m_coll;
0110         Transcoding::Configuration m_transcodingConfig;
0111         QMap<Meta::TrackPtr,QUrl> m_sources;
0112         QMultiHash<CopiedStatus, Meta::TrackPtr> m_sourceTrackStatus;
0113         QSemaphore m_copying;
0114         QSemaphore m_searchingForDuplicates;
0115         Meta::TrackPtr m_duplicateTrack;
0116         bool m_aborted;
0117         bool m_goingToRemoveSources;
0118         QSet<QString> m_notPlayableFormats;
0119         QSet<QString> m_copyErrors;
0120 
0121     protected:
0122         void defaultBegin(const ThreadWeaver::JobPointer& job, ThreadWeaver::Thread *thread) override;
0123         void defaultEnd(const ThreadWeaver::JobPointer& job, ThreadWeaver::Thread *thread) override;
0124 
0125 };
0126 
0127 #endif // COPYTRACKSJOB_H