File indexing completed on 2024-05-19 04:50:19
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 "MagnatuneRedownloadHandler.h" 0018 0019 #include "MagnatuneConfig.h" 0020 #include "core/support/Amarok.h" 0021 #include "core/support/Debug.h" 0022 #include "core/support/Components.h" 0023 #include "core/logger/Logger.h" 0024 0025 #include <KLocalizedString> 0026 #include <KMessageBox> 0027 0028 #include "QDir" 0029 0030 0031 MagnatuneRedownloadHandler::MagnatuneRedownloadHandler(QWidget * parent) 0032 { 0033 m_parent = parent; 0034 m_redownloadDialog = nullptr; 0035 m_downloadDialog = nullptr; 0036 m_albumDownloader = nullptr; 0037 } 0038 0039 0040 MagnatuneRedownloadHandler::~MagnatuneRedownloadHandler() 0041 { 0042 } 0043 0044 void 0045 MagnatuneRedownloadHandler::showRedownloadDialog( ) 0046 { 0047 fetchServerSideRedownloadList(); 0048 return; 0049 } 0050 0051 QStringList 0052 MagnatuneRedownloadHandler::GetPurchaseList( ) 0053 { 0054 0055 debug() << "MagnatuneRedownloadHandler::GetPurchaseList( )"; 0056 0057 QStringList returnList; 0058 QDir purchaseInfoDir( Amarok::saveLocation( "magnatune.com/purchases/" ) ); 0059 0060 if ( !purchaseInfoDir.exists () ) { 0061 return returnList; 0062 } 0063 0064 purchaseInfoDir.setFilter( QDir::Files); 0065 purchaseInfoDir.setSorting( QDir::Name ); 0066 0067 const QFileInfoList list = purchaseInfoDir.entryInfoList(); 0068 QFileInfoList::const_iterator it( list.begin() ); 0069 QFileInfo fi; 0070 0071 while ( it != list.end() ) { 0072 fi = *it; 0073 returnList.append( fi.fileName() ); 0074 ++it; 0075 } 0076 debug() << "Done parsing previous purchases!"; 0077 return returnList; 0078 0079 } 0080 0081 void MagnatuneRedownloadHandler::redownload( const MagnatuneDownloadInfo &info ) 0082 { 0083 0084 if ( m_albumDownloader == nullptr ) 0085 { 0086 m_albumDownloader = new MagnatuneAlbumDownloader(); 0087 connect( m_albumDownloader, &MagnatuneAlbumDownloader::downloadComplete, this, &MagnatuneRedownloadHandler::albumDownloadComplete ); 0088 } 0089 0090 0091 if ( m_downloadDialog == nullptr ) 0092 { 0093 m_downloadDialog = new MagnatuneDownloadDialog(m_parent); 0094 connect( m_downloadDialog, &MagnatuneDownloadDialog::downloadAlbum, m_albumDownloader, &MagnatuneAlbumDownloader::downloadAlbum ); 0095 } 0096 0097 debug() << "Showing download dialog"; 0098 m_downloadDialog->setDownloadInfo( info ); 0099 m_downloadDialog->show(); 0100 } 0101 0102 void 0103 MagnatuneRedownloadHandler::selectionDialogCancelled( ) 0104 { 0105 if (m_redownloadDialog != nullptr) { 0106 m_redownloadDialog->hide(); 0107 delete m_redownloadDialog; 0108 m_redownloadDialog = nullptr; 0109 } 0110 } 0111 0112 void 0113 MagnatuneRedownloadHandler::albumDownloadComplete( bool success ) 0114 { 0115 Q_UNUSED( success ); 0116 //cleanup time! 0117 0118 if (m_downloadDialog != nullptr) { 0119 delete m_downloadDialog; 0120 m_downloadDialog = nullptr; 0121 } 0122 if (m_albumDownloader != nullptr) { 0123 delete m_albumDownloader; 0124 m_albumDownloader = nullptr; 0125 } 0126 0127 } 0128 0129 void 0130 MagnatuneRedownloadHandler::fetchServerSideRedownloadList() 0131 { 0132 0133 DEBUG_BLOCK 0134 0135 //do we have an email set, if not, ask the user for one. 0136 MagnatuneConfig config; 0137 0138 QString email = config.email(); 0139 0140 if ( email.isEmpty() ) 0141 { 0142 //TODO ask for the email 0143 return; 0144 } 0145 0146 QUrl redownloadApiUrl = QUrl::fromUserInput( "http://magnatune.com/buy/redownload_xml?email=" + email ); 0147 0148 m_redownloadApiJob = KIO::storedGet( redownloadApiUrl, KIO::NoReload, KIO::HideProgressInfo ); 0149 Amarok::Logger::newProgressOperation( m_redownloadApiJob, i18n( "Getting list of previous Magnatune.com purchases" ) ); 0150 connect( m_redownloadApiJob, &KIO::TransferJob::result, this, &MagnatuneRedownloadHandler::redownloadApiResult ); 0151 0152 } 0153 0154 void MagnatuneRedownloadHandler::redownloadApiResult( KJob* job ) 0155 { 0156 0157 DEBUG_BLOCK 0158 0159 if ( job->error() != 0 ) 0160 { 0161 //TODO: error handling here 0162 debug() << "Job error... " << job->error(); 0163 return ; 0164 } 0165 if ( job != m_redownloadApiJob ) { 0166 debug() << "Wrong job..."; 0167 return ; //not the right job, so let's ignore it 0168 } 0169 0170 KIO::StoredTransferJob* const storedJob = static_cast<KIO::StoredTransferJob*>( job ); 0171 QString resultXml = QString( storedJob->data() ); 0172 0173 debug() << Qt::endl << Qt::endl << "result: " << resultXml; 0174 0175 0176 QList<MagnatuneDownloadInfo> previousPurchasesInfoList; 0177 0178 QDomDocument doc; 0179 doc.setContent( resultXml ); 0180 0181 QDomNodeList downloads = doc.elementsByTagName( "download" ); 0182 for( int i = 0; i < downloads.size(); i++ ) 0183 { 0184 QDomElement downloadElement = downloads.item( i ).toElement(); 0185 MagnatuneDownloadInfo info; 0186 if ( info.initFromRedownloadXml( downloadElement ) ) 0187 previousPurchasesInfoList << info; 0188 } 0189 0190 0191 if (m_redownloadDialog == nullptr) 0192 { 0193 m_redownloadDialog = new MagnatuneRedownloadDialog( m_parent ); 0194 connect( m_redownloadDialog, &MagnatuneRedownloadDialog::redownload, this, &MagnatuneRedownloadHandler::redownload ); 0195 connect( m_redownloadDialog, &MagnatuneRedownloadDialog::cancelled, this, &MagnatuneRedownloadHandler::selectionDialogCancelled ); 0196 } 0197 0198 m_redownloadDialog->setRedownloadItems( previousPurchasesInfoList ); 0199 0200 m_redownloadDialog->show(); 0201 0202 } 0203 0204