File indexing completed on 2024-05-19 04:50:18

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 "MagnatuneDownloadHandler.h"
0018 
0019 #include "core/logger/Logger.h"
0020 #include "core/support/Amarok.h"
0021 #include "core/support/Components.h"
0022 #include "core/support/Debug.h"
0023 #include "MagnatuneDatabaseHandler.h"
0024 #include "MagnatuneConfig.h"
0025 
0026 #include <KMessageBox>
0027 
0028 #include <QDir>
0029 #include <QFile>
0030 #include <QTemporaryDir>
0031 #include <QTextStream>
0032 
0033 using namespace Meta;
0034 
0035 MagnatuneDownloadHandler::MagnatuneDownloadHandler()
0036         : QObject()
0037         , m_downloadDialog( nullptr )
0038         , m_albumDownloader( nullptr )
0039         , m_currentAlbum( nullptr )
0040         , m_membershipDownload( false )
0041 {
0042 }
0043 
0044 
0045 MagnatuneDownloadHandler::~MagnatuneDownloadHandler()
0046 {
0047     delete m_downloadDialog;
0048     delete m_albumDownloader;
0049 }
0050 
0051 
0052 void MagnatuneDownloadHandler::downloadAlbum( MagnatuneAlbum * album )
0053 {
0054     DEBUG_BLOCK
0055     m_currentAlbum = album;
0056 
0057     //do we have a membership that allows free downloads?
0058 
0059     MagnatuneConfig config;
0060 
0061     if ( config.isMember() && config.membershipType() == MagnatuneConfig::DOWNLOAD ) {
0062         debug() << "membership download...";
0063         membershipDownload( config.membershipType(), config.username(), config.password() );
0064     }
0065 }
0066 
0067 
0068 
0069 void MagnatuneDownloadHandler::membershipDownload( int membershipType, const QString &username, const QString &password )
0070 {
0071     QString type;
0072     if( membershipType == MagnatuneConfig::STREAM )
0073         type = "stream";
0074     else
0075          type = "download";
0076     
0077     QUrl purchaseURL = QUrl::fromUserInput( "http://" + username + ":" + password + "@" + type + ".magnatune.com/buy/membership_free_dl_xml?sku=" + m_currentAlbum->albumCode() + "&id=amarok" );
0078 
0079     m_membershipDownload = true;
0080 
0081     m_resultDownloadJob = KIO::storedGet( purchaseURL, KIO::NoReload, KIO::HideProgressInfo );
0082     Amarok::Logger::newProgressOperation( m_resultDownloadJob,
0083                                                         i18n( "Processing download" ) );
0084     connect( m_resultDownloadJob, &KJob::result, this, &MagnatuneDownloadHandler::xmlDownloadComplete );
0085 }
0086 
0087 void MagnatuneDownloadHandler::xmlDownloadComplete( KJob * downloadJob )
0088 {
0089 
0090     debug() << "xml download complete";
0091 
0092     if ( downloadJob->error() )
0093     {
0094         //TODO: error handling here
0095         debug() << "Job error... " << downloadJob->error();
0096         return ;
0097     }
0098     if ( downloadJob != m_resultDownloadJob ) {
0099         debug() << "Wrong job...";
0100         return ; //not the right job, so let's ignore it
0101     }
0102 
0103     KIO::StoredTransferJob* const storedJob = static_cast<KIO::StoredTransferJob*>( downloadJob );
0104     QString resultXml = QString( storedJob->data() );
0105 
0106     debug() << Qt::endl << Qt::endl << "result: " << resultXml;
0107 
0108 
0109     if ( !m_albumDownloader )
0110     {
0111         m_albumDownloader = new MagnatuneAlbumDownloader();
0112         connect( m_albumDownloader, &MagnatuneAlbumDownloader::downloadComplete, this, &MagnatuneDownloadHandler::albumDownloadComplete );
0113     }
0114 
0115     if ( !m_downloadDialog )
0116     {
0117         m_downloadDialog = new MagnatuneDownloadDialog( m_parent );
0118         m_downloadDialog->setModal( true );
0119         connect( m_downloadDialog, &MagnatuneDownloadDialog::downloadAlbum, m_albumDownloader, &MagnatuneAlbumDownloader::downloadAlbum );
0120         //connect( m_downloadDialog, SIGNAL(rejected()), this, SLOT(albumPurchaseCancelled()) );
0121 
0122     }
0123 
0124 
0125     MagnatuneDownloadInfo downloadInfo;
0126     if ( downloadInfo.initFromString( resultXml, m_membershipDownload ) )
0127     {
0128 
0129         downloadInfo.setAlbumCode( m_currentAlbum->albumCode() );
0130         downloadInfo.setCoverUrl( m_currentAlbum->coverUrl() );
0131         downloadInfo.setAlbumName( m_currentAlbum->prettyName() );
0132         downloadInfo.setArtistName( m_currentAlbum->albumArtist()->prettyName() );
0133         
0134         if ( m_membershipDownload ) {
0135             MagnatuneConfig config;
0136             downloadInfo.setMembershipInfo( config.username(), config.password() );
0137         } else {
0138             saveDownloadInfo( resultXml );
0139         }
0140         
0141         m_downloadDialog->setDownloadInfo( downloadInfo );
0142         //m_purchaseDialog->close();
0143 
0144         
0145         m_downloadDialog->show();
0146     } else {
0147         
0148         KMessageBox::information( m_parent, i18n( "There seems to be an error in the supplied membership information. Please correct this and try again."),i18n("Could not process download") );
0149     }
0150 }
0151 
0152 
0153 void MagnatuneDownloadHandler::setParent( QWidget * parent )
0154 {
0155     m_parent = parent;
0156 
0157 }
0158 
0159 void MagnatuneDownloadHandler::saveDownloadInfo( const QString &infoXml )
0160 {
0161 
0162     MagnatuneDatabaseHandler dbHandler;
0163 
0164     QDir purchaseDir( Amarok::saveLocation( "magnatune.com/purchases/" ) );
0165 
0166     debug() << "magnatune save location" << purchaseDir.absolutePath();
0167 
0168     //if directory does not exist, create it
0169     if ( ! purchaseDir.exists () )
0170     {
0171         purchaseDir.mkdir( "." );
0172     }
0173 
0174     QString fileName = m_currentAlbum->albumArtist()->name() + " - " + m_currentAlbum->name();
0175 
0176     QFile file( purchaseDir.absolutePath() + QLatin1Char('/') + fileName );
0177 
0178     //Skip if file already exists
0179     if ( file.exists () )
0180         return ;
0181 
0182     //write info
0183     if ( file.open( QIODevice::WriteOnly | QIODevice::Text ) )
0184     {
0185         QTextStream stream( &file );
0186         stream << infoXml << "\n";
0187         file.close();
0188     }
0189 }
0190 
0191 void MagnatuneDownloadHandler::albumDownloadComplete( bool success )
0192 {
0193     //cleanup time!
0194 
0195     debug() << "MagnatuneDownloadHandler::albumDownloadComplete";
0196 
0197     delete m_downloadDialog;
0198     m_downloadDialog = nullptr;
0199 
0200     Q_EMIT( downloadCompleted( success ) );
0201 
0202 }
0203 
0204 
0205 
0206 
0207