File indexing completed on 2024-05-05 04:49:15

0001 /****************************************************************************************
0002  * Copyright (c) 2007 Nikolaj Hald Nielsen <nhn@kde.org>                                *
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 SERVICEALBUMCOVERDOWNLOADER_H
0018 #define SERVICEALBUMCOVERDOWNLOADER_H
0019 
0020 #include "ServiceMetaBase.h"
0021 #include "amarok_export.h"
0022 
0023 #include <kio/job.h>
0024 #include <kio/jobclasses.h>
0025 #include <QTemporaryDir>
0026 
0027 #include <QObject>
0028 
0029 namespace Meta
0030 {
0031 
0032 //forward declaration
0033 class ServiceAlbumWithCover;
0034 
0035 typedef AmarokSharedPointer<ServiceAlbumWithCover> ServiceAlbumWithCoverPtr;
0036 
0037 /**
0038 A specialized ServiceAlbum that supports fetching its cover from a known url.
0039 Takes care of caching covers.
0040 
0041     @author  Nikolaj Hald Nielsen <nhn@kde.org>
0042 */
0043 class AMAROK_EXPORT ServiceAlbumWithCover : public ServiceAlbum
0044 {
0045 
0046 public:
0047 
0048     /**
0049      * Constructor, reimplemented from ServiceAlbum.
0050      * @param name The name of the album.
0051      */
0052     explicit ServiceAlbumWithCover( const QString &name );
0053 
0054     /**
0055      * Constructor, reimplemented from ServiceAlbum.
0056      * @param resultRow The result raw used to initialize the album.
0057      */
0058     explicit ServiceAlbumWithCover( const QStringList &resultRow );
0059 
0060     /**
0061      * Destructor.
0062      */
0063     ~ServiceAlbumWithCover() override;
0064 
0065     /**
0066      * Get the download prefix used for caching the cover.
0067      * @return The prefix, most often the name of the parent service.
0068      */
0069     virtual QString downloadPrefix() const = 0;
0070 
0071     /**
0072      * Set the url from where to fetch the cover.
0073      * @param coverUrl The cover url.
0074      */
0075     virtual void setCoverUrl( const QString &coverUrl ) = 0;
0076 
0077     /**
0078      * Ger the cover url
0079      * @return The url of the cover.
0080      */
0081     virtual QString coverUrl() const = 0;
0082 
0083     /**
0084      * Set the cover image of the album.
0085      * @param image The cover image.
0086      */
0087     void setImage( const QImage &image ) override;
0088 
0089     /**
0090      * Notify album that the download of the cover has been cancelled.
0091      */
0092     void imageDownloadCanceled() const;
0093 
0094     /**
0095      * Reimplemented from Meta::Album. Get whether an album has a cover. This function always returns true to avoid the standard cover fetcher kicking in.
0096      * @param size Unused.
0097      * @return True.
0098      */
0099     bool hasImage( int size = 1 ) const override { Q_UNUSED( size ); return true; }
0100 
0101     /**
0102      * Get the image of this album. If the image has not yet been fetched, this call will return a standard
0103      * "no-cover" cover and then start the download of the real cover. When the download is complete, any oberserves will be notified that the metadata of this album has been changed.
0104      * @param size The required size of the album.
0105      * @return The cover image or a default cover.
0106      */
0107     QImage image( int size = 0 ) const override; //overridden from Meta::Album
0108 
0109 protected:
0110 
0111     mutable QImage m_cover;
0112     mutable bool m_hasFetchedCover;
0113     mutable bool m_isFetchingCover;
0114     QString m_coverDownloadPath;
0115 };
0116 
0117 
0118 
0119 /**
0120 A helper class for fetching covers from online services, used by ServiceAlbumWithCover
0121 
0122     @author  Nikolaj Hald Nielsen <nhn@kde.org>
0123 */
0124 class ServiceAlbumCoverDownloader : public QObject
0125 {
0126     Q_OBJECT
0127 
0128     public:
0129 
0130         /**
0131          * Constructor.
0132          */
0133         ServiceAlbumCoverDownloader();
0134 
0135         /**
0136          * Destructor.
0137          */
0138         ~ServiceAlbumCoverDownloader() override;
0139 
0140         /**
0141          * Start the download of the cover of a ServiceAlbumWithCover.
0142          * @param album The albumwhose cover should be downloaded.
0143          */
0144         void downloadCover( Meta::ServiceAlbumWithCoverPtr album );
0145 
0146     private Q_SLOTS:
0147 
0148         /**
0149          * Slot called when the download job is complete.
0150          * @param downloadJob The job responsible for the cover download.
0151          */
0152         void coverDownloadComplete( KJob * downloadJob );
0153 
0154         /**
0155          * Slot called if the download job is cancelled.
0156          * @param downloadJob The job responsible for the cover download.
0157          */
0158         void coverDownloadCanceled( KJob * downloadJob );
0159     private:
0160         Meta::ServiceAlbumWithCoverPtr m_album;
0161         QString m_coverDownloadPath;
0162         KIO::FileCopyJob * m_albumDownloadJob;
0163         QTemporaryDir * m_tempDir;
0164 };
0165 
0166 }
0167 
0168 #endif