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 02110-1301, USA. *
19 ***************************************************************************/
21 #include "magnatunepurchasehandler.h"
25 #include "magnatunedatabasehandler.h"
26 #include "ContextStatusBar.h"
28 #include <KMessageBox>
31 #include <Q3TextStream>
37 MagnatunePurchaseHandler::MagnatunePurchaseHandler()
39 , m_purchaseDialog( 0 )
40 , m_downloadDialog( 0 )
41 , m_albumDownloader( 0 )
43 , m_giftCardPurchase ( false )
48 MagnatunePurchaseHandler::~MagnatunePurchaseHandler()
50 delete m_downloadDialog
;
51 delete m_purchaseDialog
;
52 delete m_albumDownloader
;
56 void MagnatunePurchaseHandler:: purchaseAlbum( MagnatuneAlbum
* album
)
59 m_currentAlbum
= album
;
61 showPurchaseDialog( QString() );
65 void MagnatunePurchaseHandler::showPurchaseDialog( const QString
&coverTempLocation
)
68 if ( m_purchaseDialog
== 0 )
70 m_purchaseDialog
= new MagnatunePurchaseDialog( m_parent
, "PurchaseDialog", true, 0 );
72 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( makeGiftCardPurchase( QString
, QString
, QString
, QString
, int ) ), this, SLOT( processGiftCardPayment( QString
, QString
, QString
, QString
, int ) ) );
76 connect ( m_purchaseDialog
, SIGNAL( cancelled() ), this, SLOT( albumPurchaseCancelled() ) );
83 debug() << "showing purchase dialog with image: " << coverTempLocation
+ m_currentAlbumCoverName
;
86 m_purchaseDialog
->setAlbum( m_currentAlbum
);
87 //m_purchaseDialog->setCover( coverTempLocation + m_currentAlbumCoverName );
88 m_purchaseDialog
->show();
92 void MagnatunePurchaseHandler::processPayment( const QString
&ccNumber
, const QString
&expYear
, const QString
&expMonth
, const QString
&name
, const QString
&email
, const QString
&albumCode
, int amount
)
96 amountString
.setNum( amount
, 10 );
98 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
;
100 QString debugPurchaseURL
= "https://magnatune.com/buy/buy_dl_cc_xml?cc=**********&mm=**&yy=**&sku=" + albumCode
+ "&name=" + name
+ "&email=********&id=amarok&amount=" + amountString
;
101 debug() << "purchase url : " << debugPurchaseURL
;
103 m_giftCardPurchase
= false;
105 m_resultDownloadJob
= KIO::storedGet( KUrl( purchaseURL
), KIO::NoReload
, KIO::HideProgressInfo
);
107 Amarok::ContextStatusBar::instance() ->newProgressOperation( m_resultDownloadJob
).setDescription( i18n( "Processing Payment" ) );
109 connect( m_resultDownloadJob
, SIGNAL( result( KJob
* ) ), SLOT( xmlDownloadComplete( KJob
* ) ) );
114 void MagnatunePurchaseHandler::processGiftCardPayment(const QString
& giftCardCode
, const QString
& name
, const QString
& email
, const QString
& albumCode
, int amount
)
117 QString amountString
;
118 amountString
.setNum( amount
, 10 );
120 QString purchaseURL
= "https://magnatune.com/buy/buy_dl_cc_xml?gc=" + giftCardCode
+ "&sku=" + albumCode
+ "&name=" + name
+ "&email=" + email
+ "&id=amarok&amount=" + amountString
;
122 QString debugPurchaseURL
= "https://magnatune.com/buy/buy_dl_cc_xml?gc=**********&sku=" + albumCode
+ "&name=" + name
+ "&email=********&id=amarok&amount=" + amountString
;
123 debug() << "purchase url : " << debugPurchaseURL
;
125 m_giftCardPurchase
= true;
127 m_resultDownloadJob
= KIO::storedGet( KUrl( purchaseURL
), KIO::NoReload
, KIO::HideProgressInfo
);
129 Amarok::ContextStatusBar::instance() ->newProgressOperation( m_resultDownloadJob
).setDescription( i18n( "Processing Payment" ) );
131 connect( m_resultDownloadJob
, SIGNAL( result( KJob
* ) ), SLOT( xmlDownloadComplete( KJob
* ) ) );
136 void MagnatunePurchaseHandler::xmlDownloadComplete( KJob
* downloadJob
)
139 debug() << "xml download complete";
141 if ( !downloadJob
->error() == 0 )
143 //TODO: error handling here
146 if ( downloadJob
!= m_resultDownloadJob
)
147 return ; //not the right job, so let's ignore it
149 KIO::StoredTransferJob
* const storedJob
= static_cast<KIO::StoredTransferJob
*>( downloadJob
);
150 QString resultXml
= QString( storedJob
->data() );
152 debug() << endl
<< endl
<< "result: " << resultXml
;
155 if ( m_albumDownloader
== 0 )
157 m_albumDownloader
= new MagnatuneAlbumDownloader();
158 connect( m_albumDownloader
, SIGNAL( downloadComplete( bool ) ), this, SLOT( albumDownloadComplete( bool ) ) );
161 if ( m_downloadDialog
== 0 )
163 m_downloadDialog
= new MagnatuneDownloadDialog( m_parent
, "downloaddialog", true, 0 );
164 connect( m_downloadDialog
, SIGNAL( downloadAlbum( MagnatuneDownloadInfo
* ) ), m_albumDownloader
, SLOT( downloadAlbum( MagnatuneDownloadInfo
* ) ) );
171 MagnatuneDownloadInfo
* downloadInfo
= new MagnatuneDownloadInfo();
172 if ( downloadInfo
->initFromString( resultXml
) )
176 downloadInfo
->setAlbum( m_currentAlbum
);
178 saveDownloadInfo( resultXml
);
179 m_downloadDialog
->setDownloadInfo( downloadInfo
);
180 //m_purchaseDialog->close();
181 delete m_purchaseDialog
;
182 m_purchaseDialog
= 0;
183 m_downloadDialog
->show();
187 QString checkInfoMessage
;
189 if ( m_giftCardPurchase
)
190 checkInfoMessage
= i18n( "check the gift card code" );
192 checkInfoMessage
= i18n( "check the credit card information" );
195 KMessageBox::information( m_parent
, "Could not process payment",
196 "There seems to be an error in the information entered (" + checkInfoMessage
+ "), please try again\n" );
199 m_purchaseDialog
->setEnabled( true );
204 void MagnatunePurchaseHandler::setParent( QWidget
* parent
)
210 void MagnatunePurchaseHandler::saveDownloadInfo( const QString
&infoXml
)
213 MagnatuneDatabaseHandler dbHandler
;
215 QDir
purchaseDir( Amarok::saveLocation( "magnatune.com/purchases/" ) );
217 debug() << "magnatune save location" << purchaseDir
.absolutePath();
219 //if directory does not exist, create it
220 if ( ! purchaseDir
.exists () )
222 purchaseDir
.mkdir( "." );
225 QString fileName
= m_currentAlbum
->albumArtist()->name() + " - " + m_currentAlbum
->name();
227 QFile
file( purchaseDir
.absolutePath() + '/' + fileName
);
229 //Skip if file already exists
230 if ( file
.exists () )
234 if ( file
.open( QIODevice::WriteOnly
) )
236 Q3TextStream
stream( &file
);
237 stream
<< infoXml
<< "\n";
242 void MagnatunePurchaseHandler::albumDownloadComplete( bool success
)
246 debug() << "MagnatunePurchaseHandler::albumDownloadComplete";
248 delete m_downloadDialog
;
249 m_downloadDialog
= 0;
251 emit( purchaseCompleted( success
) );
255 void MagnatunePurchaseHandler::albumPurchaseCancelled( )
257 debug() << "Purchased dialog cancelled, deleting...";
259 delete m_purchaseDialog
;
260 m_purchaseDialog
= 0;
263 emit( purchaseCompleted( false ) );
274 #include "magnatunepurchasehandler.moc"