| 1 |
/**************************************************************************************** |
| 2 |
* Copyright (c) 2009 Bart Cerneels <[email protected]> * |
| 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 |
#include "core/podcasts/PodcastImageFetcher.h" |
| 18 |
|
| 19 |
#include "core/support/Debug.h" |
| 20 |
|
| 21 |
#include <QCryptographicHash> |
| 22 |
#include <QNetworkInformation> |
| 23 |
|
| 24 |
#include <KIO/MkdirJob> |
| 25 |
#include <KIO/FileCopyJob> |
| 26 |
|
| 27 |
PodcastImageFetcher::PodcastImageFetcher() |
| 28 |
{ |
| 29 |
} |
| 30 |
|
| 31 |
void |
| 32 |
PodcastImageFetcher::addChannel( Podcasts::PodcastChannelPtr channel ) |
| 33 |
{ |
| 34 |
DEBUG_BLOCK |
| 35 |
if( channel->imageUrl().isEmpty() ) |
| 36 |
{ |
| 37 |
debug() << channel->title() << " does not have an imageUrl"; |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
if( hasCachedImage( channel ) ) |
| 42 |
{ |
| 43 |
debug() << "using cached image for " << channel->title(); |
| 44 |
QString imagePath = cachedImagePath( channel ).toLocalFile(); |
| 45 |
QImage image( imagePath ); |
| 46 |
if( image.isNull() ) |
| 47 |
error() << "could not load pixmap from " << imagePath; |
| 48 |
else |
| 49 |
channel->setImage( image ); |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
if( m_channels.contains( channel ) ) |
| 54 |
{ |
| 55 |
debug() << "Channel already queued:" << channel->title(); |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
if( m_jobChannelMap.values().contains( channel ) ) |
| 60 |
{ |
| 61 |
debug() << "Copy job already running for channel:" << channel->title(); |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
debug() << "Adding " << channel->title() << " to fetch queue"; |
| 66 |
m_channels.append( channel ); |
| 67 |
} |
| 68 |
|
| 69 |
void |
| 70 |
PodcastImageFetcher::addEpisode( const Podcasts::PodcastEpisodePtr &episode ) |
| 71 |
{ |
| 72 |
Q_UNUSED( episode ); |
| 73 |
} |
| 74 |
|
| 75 |
QUrl |
| 76 |
PodcastImageFetcher::cachedImagePath( const Podcasts::PodcastChannelPtr &channel ) |
| 77 |
{ |
| 78 |
return cachedImagePath( channel.data() ); |
| 79 |
} |
| 80 |
|
| 81 |
QUrl |
| 82 |
PodcastImageFetcher::cachedImagePath( Podcasts::PodcastChannel *channel ) |
| 83 |
{ |
| 84 |
QUrl imagePath = channel->saveLocation(); |
| 85 |
if( imagePath.isEmpty() || !imagePath.isLocalFile() ) |
| 86 |
imagePath = QUrl::fromLocalFile( Amarok::saveLocation( QStringLiteral("podcasts") ) ); |
| 87 |
QCryptographicHash md5( QCryptographicHash::Md5 ); |
| 88 |
md5.addData( channel->url().url().toLocal8Bit() ); |
| 89 |
QString extension = Amarok::extension( channel->imageUrl().fileName() ); |
| 90 |
imagePath = imagePath.adjusted( QUrl::StripTrailingSlash ); |
| 91 |
imagePath.setPath( imagePath.path() + QLatin1Char('/') + QLatin1String( md5.result().toHex() ) + QLatin1Char('.') + extension ); |
| 92 |
return imagePath; |
| 93 |
} |
| 94 |
|
| 95 |
bool |
| 96 |
PodcastImageFetcher::hasCachedImage( const Podcasts::PodcastChannelPtr &channel ) |
| 97 |
{ |
| 98 |
DEBUG_BLOCK |
| 99 |
return QFile( PodcastImageFetcher::cachedImagePath( |
| 100 |
Podcasts::PodcastChannelPtr::dynamicCast( channel ) ).toLocalFile() ).exists(); |
| 101 |
} |
| 102 |
|
| 103 |
void |
| 104 |
PodcastImageFetcher::run() |
| 105 |
{ |
| 106 |
if( m_channels.isEmpty() && m_episodes.isEmpty() |
| 107 |
&& m_jobChannelMap.isEmpty() && m_jobEpisodeMap.isEmpty() ) |
| 108 |
{ |
| 109 |
//nothing to do |
| 110 |
Q_EMIT( done( this ) ); |
| 111 |
return; |
| 112 |
} |
| 113 |
if( QNetworkInformation::instance()->reachability() == QNetworkInformation::Reachability::Disconnected ) |
| 114 |
{ |
| 115 |
debug() << "QNetworkInformation reports we are not online, canceling podcast image download"; |
| 116 |
Q_EMIT( done( this ) ); |
| 117 |
//TODO: schedule another run after Solid reports we are online again |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
for( Podcasts::PodcastChannelPtr channel : m_channels ) |
| 122 |
{ |
| 123 |
QUrl channelImageUrl = channel->imageUrl(); |
| 124 |
// KIO::file_copy in KF5 needs scheme |
| 125 |
if (channelImageUrl.isRelative() && channelImageUrl.host().isEmpty()) { |
| 126 |
channelImageUrl.setScheme( QStringLiteral("file") ); |
| 127 |
} |
| 128 |
|
| 129 |
QUrl cachedPath = cachedImagePath( channel ); |
| 130 |
KIO::mkdir( cachedPath.adjusted(QUrl::RemoveFilename|QUrl::StripTrailingSlash) ); |
| 131 |
KIO::FileCopyJob *job = KIO::file_copy(channelImageUrl, cachedPath, |
| 132 |
-1, KIO::HideProgressInfo | KIO::Overwrite ); |
| 133 |
m_jobChannelMap.insert( job, channel ); |
| 134 |
connect( job, &KIO::FileCopyJob::finished, this, &PodcastImageFetcher::slotDownloadFinished ); |
| 135 |
} |
| 136 |
m_channels.clear(); |
| 137 |
|
| 138 |
//TODO: episodes |
| 139 |
} |
| 140 |
|
| 141 |
void |
| 142 |
PodcastImageFetcher::slotDownloadFinished( KJob *job ) |
| 143 |
{ |
| 144 |
DEBUG_BLOCK |
| 145 |
|
| 146 |
//QMap::take() also removes the entry from the map. |
| 147 |
Podcasts::PodcastChannelPtr channel = m_jobChannelMap.take( job ); |
| 148 |
if( channel.isNull() ) |
| 149 |
{ |
| 150 |
error() << "got null PodcastChannelPtr " << __FILE__ << ":" << __LINE__; |
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
if( job->error() ) |
| 155 |
{ |
| 156 |
error() << "downloading podcast image " << job->errorString(); |
| 157 |
} |
| 158 |
else |
| 159 |
{ |
| 160 |
QString imagePath = cachedImagePath( channel ).toLocalFile(); |
| 161 |
QImage image( imagePath ); |
| 162 |
if( image.isNull() ) |
| 163 |
error() << "could not load pixmap from " << imagePath; |
| 164 |
else |
| 165 |
channel->setImage( image ); |
| 166 |
} |
| 167 |
|
| 168 |
//call run again to start the next batch of transfers. |
| 169 |
run(); |
| 170 |
} |