1 /***************************************************************************
2 * Copyright (c) 2006, 2007 *
3 * Nikolaj Hald Nielsen <nhnFreespirit@gmail.com> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "magnatunepurchasehandler.h"
25 #include "magnatunedatabasehandler.h"
26 #include "statusbar.h"
28 #include <KMessageBox>
31 #include <Q3TextStream>
37 MagnatunePurchaseHandler::MagnatunePurchaseHandler()
39 //, m_currentAlbum( 0 )
44 m_albumDownloader
= 0;
49 MagnatunePurchaseHandler::~MagnatunePurchaseHandler()
51 delete m_downloadDialog
;
52 delete m_purchaseDialog
;
53 delete m_albumDownloader
;
57 void MagnatunePurchaseHandler:: purchaseAlbum( MagnatuneAlbum
* album
)
60 m_currentAlbum
= album
;
62 showPurchaseDialog( QString() );
66 void MagnatunePurchaseHandler::showPurchaseDialog( const QString
&coverTempLocation
)
69 if ( m_purchaseDialog
== 0 )
71 m_purchaseDialog
= new MagnatunePurchaseDialog( m_parent
, "PurchaseDialog", true, 0 );
73 connect( m_purchaseDialog
, SIGNAL( makePurchase( QString
, QString
, QString
, QString
, QString
, QString
, int ) ), this, SLOT( processPayment( QString
, QString
, QString
, QString
, QString
, QString
, int ) ) );
74 connect ( m_purchaseDialog
, SIGNAL( cancelled() ), this, SLOT( albumPurchaseCancelled() ) );
81 debug() << "showing purchase dialog with image: " << coverTempLocation
+ m_currentAlbumCoverName
<< endl
;
84 m_purchaseDialog
->setAlbum( m_currentAlbum
);
85 //m_purchaseDialog->setCover( coverTempLocation + m_currentAlbumCoverName );
86 m_purchaseDialog
->show();
90 void MagnatunePurchaseHandler::processPayment( const QString
&ccNumber
, const QString
&expYear
, const QString
&expMonth
, const QString
&name
, const QString
&email
, const QString
&albumCode
, int amount
)
93 amountString
.setNum( amount
, 10 );
95 QString purchaseURL
= "https://magnatune.com/buy/buy_dl_cc_xml?cc=" + ccNumber
+ "&mm=" + expMonth
+ "&yy=" + expYear
+ "&sku=" + albumCode
+ "&name=" + name
+ "&email=" + email
+ "&id=amarok&amount=" + amountString
;
97 QString debugPurchaseURL
= "https://magnatune.com/buy/buy_dl_cc_xml?cc=**********&mm=**&yy=**&sku=" + albumCode
+ "&name=" + name
+ "&email=********&id=amarok&amount=" + amountString
;
98 debug() << "purchase url : " << debugPurchaseURL
<< endl
;
100 m_resultDownloadJob
= KIO::storedGet( KUrl( purchaseURL
), false, false );
102 Amarok::StatusBar::instance() ->newProgressOperation( m_resultDownloadJob
).setDescription( i18n( "Processing Payment" ) );
104 connect( m_resultDownloadJob
, SIGNAL( result( KJob
* ) ), SLOT( xmlDownloadComplete( KJob
* ) ) );
109 void MagnatunePurchaseHandler::xmlDownloadComplete( KJob
* downloadJob
)
112 debug() << "xml download complete" << endl
;
114 if ( !downloadJob
->error() == 0 )
116 //TODO: error handling here
119 if ( downloadJob
!= m_resultDownloadJob
)
120 return ; //not the right job, so let's ignore it
122 KIO::StoredTransferJob
* const storedJob
= static_cast<KIO::StoredTransferJob
*>( downloadJob
);
123 QString resultXml
= QString( storedJob
->data() );
125 debug() << endl
<< endl
<< "result: " << resultXml
<< endl
<< endl
;
128 if ( m_albumDownloader
== 0 )
130 m_albumDownloader
= new MagnatuneAlbumDownloader();
131 connect( m_albumDownloader
, SIGNAL( downloadComplete( bool ) ), this, SLOT( albumDownloadComplete( bool ) ) );
134 if ( m_downloadDialog
== 0 )
136 m_downloadDialog
= new MagnatuneDownloadDialog( m_parent
, "downloaddialog", true, 0 );
137 connect( m_downloadDialog
, SIGNAL( downloadAlbum( MagnatuneDownloadInfo
* ) ), m_albumDownloader
, SLOT( downloadAlbum( MagnatuneDownloadInfo
* ) ) );
144 MagnatuneDownloadInfo
* downloadInfo
= new MagnatuneDownloadInfo();
145 if ( downloadInfo
->initFromString( resultXml
) )
149 downloadInfo
->setAlbum( m_currentAlbum
);
151 saveDownloadInfo( resultXml
);
152 m_downloadDialog
->setDownloadInfo( downloadInfo
);
153 //m_purchaseDialog->close();
154 delete m_purchaseDialog
;
155 m_purchaseDialog
= 0;
156 m_downloadDialog
->show();
161 KMessageBox::information( m_parent
, "Could not process payment",
162 "There seems to be an error in the information entered (check the credit card number), please try again\n" );
163 m_purchaseDialog
->setEnabled( true );
168 void MagnatunePurchaseHandler::setParent( QWidget
* parent
)
174 void MagnatunePurchaseHandler::saveDownloadInfo( const QString
&infoXml
)
177 MagnatuneDatabaseHandler dbHandler
;
179 QDir
purchaseDir( Amarok::saveLocation( "magnatune.com/purchases/" ) );
181 debug() << "magnatune save location" << purchaseDir
.absolutePath() << endl
;
183 //if directory does not exist, create it
184 if ( ! purchaseDir
.exists () )
186 purchaseDir
.mkdir( "." );
189 QString fileName
= m_currentAlbum
->albumArtist()->name() + " - " + m_currentAlbum
->name();
191 QFile
file( purchaseDir
.absolutePath() + '/' + fileName
);
193 //Skip if file already exists
194 if ( file
.exists () )
198 if ( file
.open( QIODevice::WriteOnly
) )
200 Q3TextStream
stream( &file
);
201 stream
<< infoXml
<< "\n";
206 void MagnatunePurchaseHandler::albumDownloadComplete( bool success
)
210 debug() << "MagnatunePurchaseHandler::albumDownloadComplete" << endl
;
212 delete m_downloadDialog
;
213 m_downloadDialog
= 0;
215 emit( purchaseCompleted( success
) );
219 void MagnatunePurchaseHandler::albumPurchaseCancelled( )
221 debug() << "Purchased dialog cancelled, deleting..." << endl
;
223 delete m_purchaseDialog
;
224 m_purchaseDialog
= 0;
227 emit( purchaseCompleted( false ) );
236 #include "magnatunepurchasehandler.moc"