File indexing completed on 2024-05-05 04:48:20

0001 /****************************************************************************************
0002  * Copyright (c) 2004 Mark Kretschmann <kretschmann@kde.org>                            *
0003  * Copyright (c) 2004 Stefan Bogner <bochi@online.ms>                                   *
0004  * Copyright (c) 2007 Dan Meltzer <parallelgrapefruit@gmail.com>                        *
0005  * Copyright (c) 2009 Martin Sandsmark <sandsmark@samfundet.no>                         *
0006  *                                                                                      *
0007  * This program is free software; you can redistribute it and/or modify it under        *
0008  * the terms of the GNU General Public License as published by the Free Software        *
0009  * Foundation; either version 2 of the License, or (at your option) any later           *
0010  * version.                                                                             *
0011  *                                                                                      *
0012  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0013  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0014  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0015  *                                                                                      *
0016  * You should have received a copy of the GNU General Public License along with         *
0017  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0018  ****************************************************************************************/
0019 
0020 #ifndef AMAROK_COVERFETCHER_H
0021 #define AMAROK_COVERFETCHER_H
0022 
0023 #include "core/meta/forward_declarations.h"
0024 #include "CoverFetchUnit.h"
0025 #include "network/NetworkAccessManagerProxy.h"
0026 
0027 #include <QHash>
0028 #include <QObject>      //baseclass
0029 #include <QPointer>
0030 #include <QStringList>  //stack allocated
0031 
0032 class CoverFetchQueue;
0033 class CoverFoundDialog;
0034 class QThread;
0035 
0036 namespace KIO { class Job; }
0037 
0038 class CoverFetcher : public QObject
0039 {
0040     Q_OBJECT
0041 
0042 public:
0043     AMAROK_EXPORT static CoverFetcher* instance();
0044     AMAROK_EXPORT static void destroy();
0045 
0046     AMAROK_EXPORT void manualFetch( Meta::AlbumPtr album );
0047     AMAROK_EXPORT void queueAlbum( Meta::AlbumPtr album );
0048     AMAROK_EXPORT void queueAlbums( Meta::AlbumList albums );
0049 
0050     QStringList errors() const { return m_errors; }
0051 
0052     enum FinishState { Success, Error, NotFound, Cancelled };
0053 
0054 public Q_SLOTS:
0055     AMAROK_EXPORT void queueQuery( const Meta::AlbumPtr &album, const QString &query, int page = 0 );
0056 
0057 Q_SIGNALS:
0058     void finishedSingle( int state );
0059 
0060 private Q_SLOTS:
0061 
0062     /// Fetch a cover
0063     void slotFetch( CoverFetchUnit::Ptr unit );
0064 
0065     /// Handle result of a fetch job
0066     void slotResult( const QUrl &url, const QByteArray &data, const NetworkAccessManagerProxy::Error &e );
0067 
0068     /// Cover found dialog is closed by the user
0069     void slotDialogFinished();
0070 
0071     /// The fetch request was redirected.
0072     void fetchRequestRedirected( QNetworkReply *oldReply, QNetworkReply *newReply );
0073 
0074 private:
0075     static CoverFetcher* s_instance;
0076     CoverFetcher();
0077     ~CoverFetcher() override;
0078 
0079     /// Remove a fetch unit from the queue, and clean up any running jobs
0080     void abortFetch( const CoverFetchUnit::Ptr &unit );
0081 
0082     void queueQueryForAlbum( Meta::AlbumPtr album );
0083 
0084     CoverFetchQueue *m_queue;     //!< current fetch queue
0085     QThread *m_queueThread;
0086 
0087     QHash< QUrl, CoverFetchUnit::Ptr > m_urls;
0088     QHash< const CoverFetchUnit::Ptr, QImage > m_selectedImages;
0089 
0090     QStringList m_errors;
0091 
0092     QPointer<CoverFoundDialog> m_dialog;
0093 
0094     /// cleanup depending on the fetch result
0095     void finish( const CoverFetchUnit::Ptr &unit,
0096                  FinishState state = Success,
0097                  const QString &message = QString() );
0098 
0099     /// Show the cover that has been found
0100     void showCover( const CoverFetchUnit::Ptr &unit,
0101                     const QImage &cover = QImage(),
0102                     const CoverFetch::Metadata &data = CoverFetch::Metadata() );
0103 
0104     void handleCoverPayload( const CoverFetchUnit::Ptr &unit,
0105                              const QByteArray &data,
0106                              const QUrl &url );
0107 
0108     CoverFetch::Source fetchSource() const;
0109 };
0110 
0111 namespace The
0112 {
0113     inline CoverFetcher *coverFetcher() { return CoverFetcher::instance(); }
0114 }
0115 
0116 #endif /* AMAROK_COVERFETCHER_H */