File indexing completed on 2024-05-19 04:50:17
0001 /**************************************************************************************** 0002 * Copyright (c) 2006,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 #include "MagnatuneAlbumDownloader.h" 0018 0019 #include "core/support/Amarok.h" 0020 #include "core/support/Components.h" 0021 #include "core/support/Debug.h" 0022 #include "core/logger/Logger.h" 0023 #include "MagnatuneMeta.h" 0024 0025 #include <QTemporaryDir> 0026 0027 #include <KLocalizedString> 0028 #include <KZip> 0029 0030 MagnatuneAlbumDownloader::MagnatuneAlbumDownloader() 0031 : QObject() 0032 , m_albumDownloadJob( nullptr ) 0033 , m_coverDownloadJob( nullptr ) 0034 , m_currentAlbumFileName() 0035 { 0036 m_tempDir = new QTemporaryDir(); 0037 } 0038 0039 MagnatuneAlbumDownloader::~MagnatuneAlbumDownloader() 0040 { 0041 delete m_tempDir; 0042 } 0043 0044 void 0045 MagnatuneAlbumDownloader::downloadAlbum( MagnatuneDownloadInfo info ) 0046 { 0047 DEBUG_BLOCK 0048 0049 m_currentAlbumInfo = info; 0050 0051 0052 QUrl downloadUrl = info.completeDownloadUrl(); 0053 m_currentAlbumUnpackLocation = info.unpackLocation(); 0054 debug() << "Download: " << downloadUrl.url() << " to: " << m_currentAlbumUnpackLocation; 0055 0056 m_currentAlbumFileName = info.albumCode() + ".zip"; 0057 0058 debug() << "Using temporary location: " << m_tempDir->path() + QLatin1Char('/') + m_currentAlbumFileName; 0059 0060 m_albumDownloadJob = KIO::file_copy( downloadUrl, QUrl::fromLocalFile( m_tempDir->path() + QLatin1Char('/') + m_currentAlbumFileName ), -1, KIO::Overwrite | KIO::HideProgressInfo ); 0061 0062 connect( m_albumDownloadJob, &KJob::result, this, &MagnatuneAlbumDownloader::albumDownloadComplete ); 0063 0064 QString msgText; 0065 if( !info.albumName().isEmpty() && !info.artistName().isEmpty() ) 0066 { 0067 msgText = i18n( "Downloading '%1' by %2 from Magnatune.com", info.albumName(), info.artistName() ); 0068 } 0069 else 0070 { 0071 msgText = i18n( "Downloading album from Magnatune.com" ); 0072 } 0073 0074 Amarok::Logger::newProgressOperation( m_albumDownloadJob, msgText, this, &MagnatuneAlbumDownloader::albumDownloadAborted ); 0075 } 0076 0077 void 0078 MagnatuneAlbumDownloader::albumDownloadComplete( KJob * downloadJob ) 0079 { 0080 DEBUG_BLOCK 0081 0082 debug() << "album download complete"; 0083 0084 if ( downloadJob->error() ) 0085 { 0086 //TODO: error handling here 0087 return ; 0088 } 0089 if ( downloadJob != m_albumDownloadJob ) 0090 return ; //not the right job, so let's ignore it 0091 0092 const QString finalAlbumPath = m_currentAlbumUnpackLocation + QLatin1Char('/') + m_currentAlbumInfo.artistName() + QLatin1Char('/') + m_currentAlbumInfo.albumName(); 0093 0094 //ok, now we have the .zip file downloaded. All we need is to unpack it to the desired location and add it to the collection. 0095 0096 KZip kzip( m_tempDir->path() + QLatin1Char('/') + m_currentAlbumFileName ); 0097 0098 if ( !kzip.open( QIODevice::ReadOnly ) ) 0099 { 0100 Amarok::Logger::shortMessage( i18n( "Magnatune download seems to have failed. Cannot read zip file" ) ); 0101 Q_EMIT( downloadComplete( false ) ); 0102 return; 0103 } 0104 0105 debug() << m_tempDir->path() + QLatin1Char('/') + m_currentAlbumFileName << " opened for decompression"; 0106 0107 const KArchiveDirectory * directory = kzip.directory(); 0108 0109 Amarok::Logger::shortMessage( i18n( "Uncompressing Magnatune.com download..." ) ); 0110 0111 //Is this really blocking with no progress status!? Why is it not a KJob? 0112 0113 debug() << "decompressing to " << finalAlbumPath; 0114 directory->copyTo( m_currentAlbumUnpackLocation ); 0115 0116 debug() << "done!"; 0117 0118 0119 0120 //now I really want to add the album cover to the same folder where I just unzipped the album... The 0121 //only way of getting the actual location where the album was unpacked is using the artist and album names 0122 0123 QString coverUrlString = m_currentAlbumInfo.coverUrl(); 0124 0125 QUrl downloadUrl( coverUrlString.replace( "_200.jpg", ".jpg") ); 0126 0127 debug() << "Adding cover " << downloadUrl.url() << " to collection at " << finalAlbumPath; 0128 0129 m_coverDownloadJob = KIO::file_copy( downloadUrl, QUrl::fromLocalFile( finalAlbumPath + "/cover.jpg" ), -1, KIO::Overwrite | KIO::HideProgressInfo ); 0130 0131 connect( m_coverDownloadJob, &KJob::result, this, &MagnatuneAlbumDownloader::coverDownloadComplete ); 0132 0133 Amarok::Logger::newProgressOperation( m_coverDownloadJob, i18n( "Adding album cover to collection" ), this, &MagnatuneAlbumDownloader::coverAddAborted ); 0134 0135 Q_EMIT( downloadComplete( true ) ); 0136 } 0137 0138 void 0139 MagnatuneAlbumDownloader::coverDownloadComplete(KJob* downloadJob) 0140 { 0141 DEBUG_BLOCK 0142 0143 debug() << "cover download complete"; 0144 0145 if ( downloadJob->error() ) 0146 { 0147 //TODO: error handling here 0148 return ; 0149 } 0150 if ( downloadJob != m_coverDownloadJob ) 0151 return ; //not the right job, so let's ignore it 0152 0153 //TODO: storing of cover here 0154 } 0155 0156 void 0157 MagnatuneAlbumDownloader::albumDownloadAborted( ) 0158 { 0159 DEBUG_BLOCK 0160 0161 m_albumDownloadJob->kill(); 0162 m_albumDownloadJob = nullptr; 0163 debug() << "Aborted album download"; 0164 0165 Q_EMIT( downloadComplete( false ) ); 0166 } 0167 0168 void 0169 MagnatuneAlbumDownloader::coverAddAborted() 0170 { 0171 DEBUG_BLOCK 0172 0173 m_coverDownloadJob->kill(); 0174 m_coverDownloadJob = nullptr; 0175 debug() << "Aborted cover download"; 0176 0177 Q_EMIT( downloadComplete( false ) ); 0178 } 0179