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

0001 /****************************************************************************************
0002  * Copyright (c) 2009 Rick W. Chen <stuffcorpse@archlinux.us>                           *
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_COVERFETCHQUEUE_H
0018 #define AMAROK_COVERFETCHQUEUE_H
0019 
0020 #include "core/meta/forward_declarations.h"
0021 #include "CoverFetchUnit.h"
0022 
0023 #include <KIO/Job>
0024 
0025 #include <QByteArray>
0026 #include <QList>
0027 #include <QObject>
0028 #include <QUrl>
0029 
0030 
0031 typedef QList< CoverFetchUnit::Ptr > CoverFetchUnitList;
0032 
0033 /**
0034  * A queue that creates and keeps track of cover fetching units.
0035  * This queue creates cover fetch units with suitable payloads as requested by
0036  * the cover fetcher. It does not manage the state of those units, as in, what
0037  * to do next after each of those units is completed. The cover fetcher is
0038  * responsible for coordinating that.
0039  */
0040 
0041 class CoverFetchQueue : public QObject
0042 {
0043     Q_OBJECT
0044 
0045 public:
0046     explicit CoverFetchQueue( QObject *parent = nullptr );
0047     ~CoverFetchQueue() override;
0048 
0049     /**
0050      * Add an album-associated work unit to the queue.
0051      * @param album album to fetch cover for.
0052      * @param opt cover fetch option.
0053      * @param src cover image source.
0054      * @param xml xml document from the cover provider. Can be empty on first
0055      * pass of the fetching process.
0056      */
0057     void add( const Meta::AlbumPtr &album,
0058               const CoverFetch::Option opt = CoverFetch::Automatic,
0059               const CoverFetch::Source src = CoverFetch::LastFm,
0060               const QByteArray &xml = QByteArray() );
0061 
0062     /**
0063      * Add a work unit to the queue that does not need to associate with any album.
0064      * @param opt cover fetch option.
0065      * @param src cover image source.
0066      * @param xml xml document from the cover provider. Can be empty on first
0067      * pass of the fetching process.
0068      */
0069     void add( const CoverFetch::Option opt = CoverFetch::WildInteractive,
0070               const CoverFetch::Source src = CoverFetch::LastFm,
0071               const QByteArray &xml = QByteArray() );
0072 
0073     /**
0074      * Add a string query to the queue.
0075      * @param query text to be used for image search.
0076      * @param src the image provider to search.
0077      * @param page the page number to jump to.
0078      * @param album optional album this query is associated with
0079      */
0080     void addQuery( const QString &query,
0081                    const CoverFetch::Source src = CoverFetch::LastFm,
0082                    unsigned int page = 0,
0083                    const Meta::AlbumPtr &album = Meta::AlbumPtr(nullptr) );
0084 
0085     void clear();
0086 
0087 public Q_SLOTS:
0088     void remove( const CoverFetchUnit::Ptr &unit );
0089     void remove( const Meta::AlbumPtr &album );
0090 
0091 Q_SIGNALS:
0092     void fetchUnitAdded( CoverFetchUnit::Ptr );
0093 
0094 private:
0095     void add( const CoverFetchUnit::Ptr &unit );
0096     bool contains( const Meta::AlbumPtr &album ) const;
0097     int index( const Meta::AlbumPtr &album ) const;
0098     const CoverFetchUnit::Ptr take( const Meta::AlbumPtr &album );
0099 
0100     CoverFetchUnitList m_queue;
0101     Q_DISABLE_COPY( CoverFetchQueue )
0102 };
0103 
0104 #endif /* AMAROK_COVERFETCHQUEUE_H */