authentication against the Mp3tunes server now works, but nothing else does
[amarok.git] / src / servicebrowser / mp3tunes / mp3tunesservice.cpp
blob51b3246fb501c960f1c2e0067d7eb30edd4aab80
1 /***************************************************************************
2 * Copyright (c) 2007 Nikolaj Hald Nielsen <nhnFreespirit@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
20 #include "mp3tunesservice.h"
22 #include "amarok.h"
23 #include "debug.h"
24 #include "statusbar.h"
27 #include <KMessageBox>
28 #include <kpassworddialog.h>
31 Mp3tunesService::Mp3tunesService(const QString & name)
32 : ServiceBase( name )
33 , m_partnerToken( "7359149936" )
34 , m_apiOutputFormat( "xml")
35 , m_authenticated( false )
36 , m_sessionId ( QString() )
39 setShortDescription("The Mp3Tunes Locker service. Access your stored music!");
40 setIcon( KIcon( Amarok::icon( "download" ) ) );
45 Mp3tunesService::~Mp3tunesService()
49 void Mp3tunesService::polish()
51 authenticate();
55 void Mp3tunesService::authenticate( const QString & uname, const QString & passwd )
58 QString username, password;
60 if ( uname.isEmpty() || passwd.isEmpty() ) {
61 KPasswordDialog dlg( 0 , KPasswordDialog::ShowUsernameLine ); //FIXME 0x02 = KPasswordDialog::showUsername according to api, but that does not work
62 dlg.setPrompt( i18n( "Enter a login and a password" ) );
63 if( !dlg.exec() )
64 return; //the user canceled
66 username = dlg.username();
67 password = dlg.password();
69 } else {
70 username = uname;
71 password = passwd;
74 QString authenticationString = "https://shop.mp3tunes.com/api/v0/login?username=<username>&password=<password>&partner_token=<partner token>&output=<output format>";
76 authenticationString.replace(QString("<username>"), username);
77 authenticationString.replace(QString("<password>"), password);
78 authenticationString.replace(QString("<partner token>"), m_partnerToken);
79 authenticationString.replace(QString("<output format>"), m_apiOutputFormat);
81 debug() << "Authenticating with string: " << authenticationString;
85 m_xmlDownloadJob = KIO::storedGet( authenticationString, false, false );
86 connect( m_xmlDownloadJob, SIGNAL(result(KJob *)), this, SLOT( authenticationComplete( KJob*) ) );
87 Amarok::StatusBar::instance() ->newProgressOperation( m_xmlDownloadJob )
88 .setDescription( i18n( "Authenticating" ) );
92 void Mp3tunesService::authenticationComplete(KJob * job)
95 if ( !job->error() == 0 )
97 //TODO: error handling here
98 return ;
100 if ( job != m_xmlDownloadJob )
101 return ; //not the right job, so let's ignore it
104 QString xmlReply = ((KIO::StoredTransferJob* )job)->data();
105 debug() << "Authentication reply: " << xmlReply;
108 //so lets figure out what we got here:
109 QDomDocument doc( "reply" );
111 doc.setContent( m_xmlDownloadJob->data() );
114 QDomElement root = doc.firstChildElement("mp3tunes");
116 //find status code:
117 QDomElement element = root.firstChildElement("status");
119 if ( element.text() == "1" ) {
121 element = root.firstChildElement("session_id");
122 m_sessionId = element.text();
123 m_authenticated = true;
125 } else {
127 element = root.firstChildElement("errorMessage");
128 KMessageBox::error ( this, element.text(), "Authentication Error!" );
132 m_xmlDownloadJob->deleteLater();
135 #include "mp3tunesservice.moc"