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

0001 /****************************************************************************************
0002  * Copyright (c) 2013 Tatjana Gornak <t.gornak@gmail.com>                               *
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 AMAROK_PLAYLISTFILELOADERJOB_H
0018 #define AMAROK_PLAYLISTFILELOADERJOB_H
0019 
0020 #include "core-impl/playlists/types/file/PlaylistFile.h"
0021 
0022 #include <QTemporaryFile>
0023 
0024 #include <ThreadWeaver/Job>
0025 
0026 class KJob;
0027 
0028 namespace Playlists
0029 {
0030     /**
0031      * Allows threading during playlist file loading. Auto-deletes when its work is done.
0032      */
0033     class PlaylistFileLoaderJob :public QObject,  public ThreadWeaver::Job
0034     {
0035         Q_OBJECT
0036 
0037         public:
0038             explicit PlaylistFileLoaderJob( const PlaylistFilePtr &playlist );
0039 
0040         protected:
0041             void run(ThreadWeaver::JobPointer self = QSharedPointer<ThreadWeaver::Job>(), ThreadWeaver::Thread *thread = nullptr) override;
0042             void defaultBegin(const ThreadWeaver::JobPointer& job, ThreadWeaver::Thread *thread) override;
0043             void defaultEnd(const ThreadWeaver::JobPointer& job, ThreadWeaver::Thread *thread) override;
0044 
0045         Q_SIGNALS:
0046             /** This signal is emitted when this job is being processed by a thread. */
0047             void started(ThreadWeaver::JobPointer);
0048             /** This signal is emitted when the job has been finished (no matter if it succeeded or not). */
0049             void done(ThreadWeaver::JobPointer);
0050             /** This job has failed.
0051              * This signal is emitted when success() returns false after the job is executed. */
0052             void failed(ThreadWeaver::JobPointer);
0053 
0054         private Q_SLOTS:
0055             void slotDonwloadFinished( KJob *job );
0056 
0057             /**
0058              * Responsible for notification of finished loading
0059              */
0060             void slotDone();
0061 
0062         private:
0063             PlaylistFilePtr m_playlist;
0064             QTemporaryFile m_tempFile;
0065             QString m_actualPlaylistFile; // path to local playlist file to actually load
0066             QSemaphore m_downloadSemaphore;
0067     };
0068 }
0069 
0070 #endif