summaryrefslogtreecommitdiffstats
path: root/amarok/src/magnatunebrowser/magnatunepurchasehandler.cpp
blob: fbfed0ba172ea08dfa115f4f5854bf1f090ce624 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
Copyright (c) 2006  Nikolaj Hald Nielsen <nhnFreespirit@gmail.com>

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB.  If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/

#include "amarok.h"
#include "debug.h"
#include "magnatunedatabasehandler.h"
#include "magnatunepurchasehandler.h"
#include "statusbar.h"

#include <ktempdir.h>

#include <tqdir.h>
#include <tqfile.h>
#include <tqmessagebox.h>

MagnatunePurchaseHandler::MagnatunePurchaseHandler()
        : TQObject()
{

    m_downloadDialog = 0;
    m_purchaseDialog = 0;
    m_albumDownloader = 0;
}


MagnatunePurchaseHandler::~MagnatunePurchaseHandler()
{
    delete m_downloadDialog;
    delete m_purchaseDialog;
    delete m_albumDownloader;
}


void MagnatunePurchaseHandler::purchaseAlbum( const MagnatuneAlbum &album )
{
    m_currentAlbum = album;

    //first lets get the album cover for the album we are about to purchase.
    //Then we can show it on the purchase dialog as well as put it in the
    //same directory as the album.

    TQString albumCoverUrlString = album.getCoverURL();

    if ( m_albumDownloader == 0 )
    {
        m_albumDownloader = new MagnatuneAlbumDownloader();
        connect( m_albumDownloader, TQT_SIGNAL( coverDownloadCompleted( TQString ) ), this, TQT_SLOT( showPurchaseDialog( TQString ) ) );
    }

    m_currentAlbumCoverName = album.getName() + " - cover.jpg";


    m_albumDownloader->downloadCover( albumCoverUrlString, m_currentAlbumCoverName );

}

void MagnatunePurchaseHandler::showPurchaseDialog(  TQString coverTempLocation )
{

    if ( m_albumDownloader != 0 )
    {
        delete m_albumDownloader;
        m_albumDownloader = 0;
    }

    if ( m_purchaseDialog == 0 )
    {
        m_purchaseDialog = new MagnatunePurchaseDialog( m_parent, "PurchaseDialog", true, 0 );

        connect( m_purchaseDialog, TQT_SIGNAL( makePurchase( TQString, TQString, TQString, TQString, TQString, TQString, int ) ), this, TQT_SLOT( processPayment( TQString, TQString, TQString, TQString, TQString, TQString, int ) ) );
        connect ( m_purchaseDialog, TQT_SIGNAL( cancelled() ), this, TQT_SLOT( albumPurchaseCancelled() ) );
    }





    if ( m_currentAlbum.getId() != 0 )
    {

        KTempDir tempDir;
        m_purchaseDialog->setAlbum( m_currentAlbum );
        m_purchaseDialog->setCover( coverTempLocation + m_currentAlbumCoverName );
        m_purchaseDialog->show();
    }
}

void MagnatunePurchaseHandler::processPayment( TQString ccNumber, TQString expYear, TQString expMonth, TQString name, TQString email, TQString albumCode, int amount )
{
    TQString amountString;
    amountString.setNum( amount, 10 );

    TQString 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;

    TQString debugPurchaseURL = "https://magnatune.com/buy/buy_dl_cc_xml?cc=**********&mm=**&yy=**&sku=" + albumCode + "&name=" + name + "&email=********&id=amarok&amount=" + amountString;
    debug() << "purchase url : " << debugPurchaseURL << endl;

    m_resultDownloadJob = KIO::storedGet( KURL( purchaseURL ), false, false );
    Amarok::StatusBar::instance() ->newProgressOperation( m_resultDownloadJob ).setDescription( i18n( "Processing Payment" ) );

    connect( m_resultDownloadJob, TQT_SIGNAL( result( KIO::Job* ) ), TQT_SLOT( xmlDownloadComplete( KIO::Job* ) ) );


}

void MagnatunePurchaseHandler::xmlDownloadComplete( KIO::Job * downloadJob )
{

    debug() << "xml download complete" << endl;

    if ( !downloadJob->error() == 0 )
    {
        //TODO: error handling here
        return ;
    }
    if ( downloadJob != m_resultDownloadJob )
        return ; //not the right job, so let's ignore it

    KIO::StoredTransferJob* const storedJob = static_cast<KIO::StoredTransferJob*>( downloadJob );
    TQString resultXml = TQString( storedJob->data() );

    debug() << endl << endl << "result: " << resultXml << endl << endl;


    if ( m_albumDownloader == 0 )
    {
        m_albumDownloader = new MagnatuneAlbumDownloader();
        connect( m_albumDownloader, TQT_SIGNAL( downloadComplete( bool ) ), this, TQT_SLOT( albumDownloadComplete( bool ) ) );
    }

    if ( m_downloadDialog == 0 )
    {
        m_downloadDialog = new MagnatuneDownloadDialog( m_parent, "downloaddialog", true, 0 );
        connect( m_downloadDialog, TQT_SIGNAL( downloadAlbum( MagnatuneDownloadInfo * ) ), m_albumDownloader, TQT_SLOT( downloadAlbum( MagnatuneDownloadInfo * ) ) );

    }




    MagnatuneDownloadInfo * downloadInfo = new MagnatuneDownloadInfo();
    if ( downloadInfo->initFromString( resultXml ) )
    {


        downloadInfo->setAlbumId( m_currentAlbum.getId() );

        saveDownloadInfo( resultXml );
        m_downloadDialog->setDownloadInfo( downloadInfo );
        //m_purchaseDialog->close();
        delete m_purchaseDialog;
        m_purchaseDialog = 0;
        m_downloadDialog->show();
    }
    else
    {

        TQMessageBox::information( m_parent, "Could not process payment",
                                  "There seems to be an error in the information entered (check the credit card number), please try again\n" );
        m_purchaseDialog->setEnabled( true );
    }
}


void MagnatunePurchaseHandler::setParent( TQWidget * parent )
{
    m_parent = parent;

}

void MagnatunePurchaseHandler::saveDownloadInfo( TQString infoXml )
{

    TQDir purchaseDir( Amarok::saveLocation( "magnatune.com/purchases/" ) );

    debug() << "magnatune save location" << purchaseDir.absPath() << endl;

    //if directory does not exist, create it
    if ( ! purchaseDir.exists () )
    {
        purchaseDir.mkdir( ".", false );
    }

    //Create file name
    MagnatuneArtist artist = MagnatuneDatabaseHandler::instance() ->getArtistById( m_currentAlbum.getArtistId() );
    TQString artistName = artist.getName();
    TQString fileName = artistName + " - " + m_currentAlbum.getName();

    TQFile file( purchaseDir.absPath() + "/" + fileName );

    //Skip if file already exists
    if ( file.exists () )
        return ;

    //write info
    if ( file.open( IO_WriteOnly ) )
    {
        TQTextStream stream( &file );
        stream << infoXml << "\n";
        file.close();
    }
}

void MagnatunePurchaseHandler::albumDownloadComplete( bool success )
{
    //cleanup time!

    debug() << "MagnatunePurchaseHandler::albumDownloadComplete" << endl;

    delete m_downloadDialog;
    m_downloadDialog = 0;

    emit( purchaseCompleted( success ) );

}

void MagnatunePurchaseHandler::albumPurchaseCancelled( )
{
    debug() << "Purchased dialog cancelled, deleting..." << endl;

    delete m_purchaseDialog;
    m_purchaseDialog = 0;


    emit( purchaseCompleted( false ) );
}







#include "magnatunepurchasehandler.moc"