1 /****************************************************************************************
2  * Copyright (c) 2007 Nikolaj Hald Nielsen <nhn@kde.org>                                *
3  *                                                                                      *
4  * This program is free software; you can redistribute it and/or modify it under        *
5  * the terms of the GNU General Public License as published by the Free Software        *
6  * Foundation; either version 2 of the License, or (at your option) any later           *
7  * version.                                                                             *
8  *                                                                                      *
9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
11  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
12  *                                                                                      *
13  * You should have received a copy of the GNU General Public License along with         *
14  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
15  ****************************************************************************************/
16 
17 #ifndef SERVICEALBUMCOVERDOWNLOADER_H
18 #define SERVICEALBUMCOVERDOWNLOADER_H
19 
20 #include "ServiceMetaBase.h"
21 #include "amarok_export.h"
22 
23 #include <kio/job.h>
24 #include <kio/jobclasses.h>
25 #include <QTemporaryDir>
26 
27 #include <QObject>
28 
29 namespace Meta
30 {
31 
32 //forward declaration
33 class ServiceAlbumWithCover;
34 
35 typedef AmarokSharedPointer<ServiceAlbumWithCover> ServiceAlbumWithCoverPtr;
36 
37 /**
38 A specialized ServiceAlbum that supports fetching its cover from a known url.
39 Takes care of caching covers.
40 
41     @author  Nikolaj Hald Nielsen <nhn@kde.org>
42 */
43 class AMAROK_EXPORT ServiceAlbumWithCover : public ServiceAlbum
44 {
45 
46 public:
47 
48     /**
49      * Constructor, reimplemented from ServiceAlbum.
50      * @param name The name of the album.
51      */
52     explicit ServiceAlbumWithCover( const QString &name );
53 
54     /**
55      * Constructor, reimplemented from ServiceAlbum.
56      * @param resultRow The result raw used to initialize the album.
57      */
58     explicit ServiceAlbumWithCover( const QStringList &resultRow );
59 
60     /**
61      * Destructor.
62      */
63     ~ServiceAlbumWithCover() override;
64 
65     /**
66      * Get the download prefix used for caching the cover.
67      * @return The prefix, most often the name of the parent service.
68      */
69     virtual QString downloadPrefix() const = 0;
70 
71     /**
72      * Set the url from where to fetch the cover.
73      * @param coverUrl The cover url.
74      */
75     virtual void setCoverUrl( const QString &coverUrl ) = 0;
76 
77     /**
78      * Ger the cover url
79      * @return The url of the cover.
80      */
81     virtual QString coverUrl() const = 0;
82 
83     /**
84      * Set the cover image of the album.
85      * @param image The cover image.
86      */
87     void setImage( const QImage &image ) override;
88 
89     /**
90      * Notify album that the download of the cover has been cancelled.
91      */
92     void imageDownloadCanceled() const;
93 
94     /**
95      * Reimplemented from Meta::Album. Get whether an album has a cover. This function always returns true to avoid the standard cover fetcher kicking in.
96      * @param size Unused.
97      * @return True.
98      */
99     bool hasImage( int size = 1 ) const override { Q_UNUSED( size ); return true; }
100 
101     /**
102      * Get the image of this album. If the image has not yet been fetched, this call will return a standard
103      * "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.
104      * @param size The required size of the album.
105      * @return The cover image or a default cover.
106      */
107     QImage image( int size = 0 ) const override; //overridden from Meta::Album
108 
109 protected:
110 
111     mutable QImage m_cover;
112     mutable bool m_hasFetchedCover;
113     mutable bool m_isFetchingCover;
114     QString m_coverDownloadPath;
115 };
116 
117 
118 
119 /**
120 A helper class for fetching covers from online services, used by ServiceAlbumWithCover
121 
122     @author  Nikolaj Hald Nielsen <nhn@kde.org>
123 */
124 class ServiceAlbumCoverDownloader : public QObject
125 {
126     Q_OBJECT
127 
128     public:
129 
130         /**
131          * Constructor.
132          */
133         ServiceAlbumCoverDownloader();
134 
135         /**
136          * Destructor.
137          */
138         ~ServiceAlbumCoverDownloader() override;
139 
140         /**
141          * Start the download of the cover of a ServiceAlbumWithCover.
142          * @param album The albumwhose cover should be downloaded.
143          */
144         void downloadCover( Meta::ServiceAlbumWithCoverPtr album );
145 
146     private Q_SLOTS:
147 
148         /**
149          * Slot called when the download job is complete.
150          * @param downloadJob The job responsible for the cover download.
151          */
152         void coverDownloadComplete( KJob * downloadJob );
153 
154         /**
155          * Slot called if the download job is cancelled.
156          * @param downloadJob The job responsible for the cover download.
157          */
158         void coverDownloadCanceled( KJob * downloadJob );
159     private:
160         Meta::ServiceAlbumWithCoverPtr m_album;
161         QString m_coverDownloadPath;
162         KIO::FileCopyJob * m_albumDownloadJob;
163         QTemporaryDir * m_tempDir;
164 };
165 
166 }
167 
168 #endif
169