46
46
using namespace LastFm;
48
48
///////////////////////////////////////////////////////////////////////////////
50
// AmarokHttp is a hack written so that lastfm code could easily use something proxy aware.
51
// DO NOT use this class for anything else, use KIO directly instead.
52
////////////////////////////////////////////////////////////////////////////////
53
AmarokHttp::AmarokHttp ( const QString& hostname, Q_UINT16 port,
56
m_hostname( hostname ),
61
AmarokHttp::get ( const QString & path )
63
QString uri = QString( "http://%1:%2/%3" )
69
m_error = QHttp::NoError;
70
m_state = QHttp::Connecting;
71
KIO::TransferJob *job = KIO::get(uri, true, false);
72
connect(job, SIGNAL(data(KIO::Job*, const QByteArray&)),
73
this, SLOT(slotData(KIO::Job*, const QByteArray&)));
74
connect(job, SIGNAL(result(KIO::Job*)),
75
this, SLOT(slotResult(KIO::Job*)));
81
AmarokHttp::state() const
87
AmarokHttp::readAll ()
99
AmarokHttp::slotData(KIO::Job*, const QByteArray& data)
101
if( data.size() == 0 ) {
104
else if ( m_result.size() == 0 ) {
107
else if ( m_result.resize( m_result.size() + data.size() ) ) {
108
memcpy( m_result.end(), data.data(), data.size() );
113
AmarokHttp::slotResult(KIO::Job* job)
115
bool err = job->error();
116
if( err || m_error != QHttp::NoError ) {
117
m_error = QHttp::UnknownError;
120
m_error = QHttp::NoError;
123
m_state = QHttp::Unconnected;
124
emit( requestFinished( 0, err ) );
129
///////////////////////////////////////////////////////////////////////////////
49
130
// CLASS Controller
50
131
////////////////////////////////////////////////////////////////////////////////
55
136
: QObject( EngineController::instance(), "lastfmController" )
58
KActionCollection* ac = amaroK::actionCollection();
59
m_actionList.append( new KAction( i18n( "Ban" ), amaroK::icon( "remove" ),
139
KActionCollection* ac = Amarok::actionCollection();
140
m_actionList.append( new KAction( i18n( "Ban" ), Amarok::icon( "remove" ),
60
141
KKey( Qt::CTRL | Qt::Key_B ), this, SLOT( ban() ), ac, "ban" ) );
62
m_actionList.append( new KAction( i18n( "Love" ), amaroK::icon( "love" ),
143
m_actionList.append( new KAction( i18n( "Love" ), Amarok::icon( "love" ),
63
144
KKey( Qt::CTRL | Qt::Key_L ), this, SLOT( love() ), ac, "love" ) );
65
m_actionList.append( new KAction( i18n( "Skip" ), amaroK::icon( "next" ),
146
m_actionList.append( new KAction( i18n( "Skip" ), Amarok::icon( "next" ),
66
147
KKey( Qt::CTRL | Qt::Key_K ), this, SLOT( skip() ), ac, "skip" ) );
67
148
setActionsEnabled( false );
179
260
Controller::setActionsEnabled( bool enable )
180
261
{ //pausing last.fm streams doesn't do anything good
181
amaroK::actionCollection()->action( "play_pause" )->setEnabled( !enable );
182
amaroK::actionCollection()->action( "pause" )->setEnabled( !enable );
262
Amarok::actionCollection()->action( "play_pause" )->setEnabled( !enable );
263
Amarok::actionCollection()->action( "pause" )->setEnabled( !enable );
185
266
for( action = m_actionList.first(); action; action = m_actionList.next() )
345
426
if ( m_session.lower() == "failed" )
348
amaroK::config( "Scrobbler" )->writeEntry( "Subscriber", m_subscriber );
429
Amarok::config( "Scrobbler" )->writeEntry( "Subscriber", m_subscriber );
350
431
// Find free port
351
432
MyServerSocket* socket = new MyServerSocket();
356
437
m_proxyUrl = QString( "http://localhost:%1/lastfm.mp3" ).arg( port );
358
m_server = new amaroK::ProcIO();
439
m_server = new Amarok::ProcIO();
359
440
m_server->setComm( KProcess::Communication( KProcess::AllOutput ) );
360
441
*m_server << "amarok_proxy.rb";
361
442
*m_server << "--lastfm";
362
443
*m_server << QString::number( port );
363
444
*m_server << m_streamUrl.toString();
364
445
*m_server << AmarokConfig::soundSystem();
446
*m_server << Amarok::proxyForUrl( m_streamUrl.toString() );
366
448
if( !m_server->start( KProcIO::NotifyOnExit, true ) ) {
367
449
error() << "Failed to start amarok_proxy.rb" << endl;
388
470
debug() << "Changing station:" << url << endl;
390
QHttp http( m_baseHost, 80 );
472
AmarokHttp http( m_baseHost, 80 );
392
474
http.get( QString( m_basePath + "/adjust.php?session=%1&url=%2&debug=0" )
393
475
.arg( m_session )
428
510
WebService::requestMetaData() //SLOT
430
QHttp *http = new QHttp( m_baseHost, 80, this );
512
AmarokHttp *http = new AmarokHttp( m_baseHost, 80, this );
431
513
connect( http, SIGNAL( requestFinished( int, bool ) ), this, SLOT( metaDataFinished( int, bool ) ) );
433
515
http->get( QString( m_basePath + "/np.php?session=%1&debug=%2" )
496
578
if( job->error() == 0 ) {
497
const QString path = amaroK::saveLocation() + "lastfm_image.png";
579
const QString path = Amarok::saveLocation() + "lastfm_image.png";
498
580
const int size = AmarokConfig::coverPreviewSize();
500
582
QImage img( static_cast<KIO::StoredTransferJob*>( job )->data() );
515
597
debug() << "Disabling Scrobbling!" << endl;
517
QHttp *http = new QHttp( m_baseHost, 80, this );
599
AmarokHttp *http = new AmarokHttp( m_baseHost, 80, this );
518
600
connect( http, SIGNAL( requestFinished( int, bool ) ), this, SLOT( enableScrobblingFinished( int, bool ) ) );
520
602
http->get( QString( m_basePath + "/control.php?session=%1&command=%2&debug=%3" )
539
621
WebService::love() //SLOT
541
QHttp *http = new QHttp( m_baseHost, 80, this );
623
AmarokHttp *http = new AmarokHttp( m_baseHost, 80, this );
542
624
connect( http, SIGNAL( requestFinished( int, bool ) ), this, SLOT( loveFinished( int, bool ) ) );
544
626
http->get( QString( m_basePath + "/control.php?session=%1&command=love&debug=%2" )
545
627
.arg( m_session )
547
amaroK::StatusBar::instance()->shortMessage( i18n("love, as in affection", "Loving song...") );
629
Amarok::StatusBar::instance()->shortMessage( i18n("love, as in affection", "Loving song...") );
552
634
WebService::skip() //SLOT
554
QHttp *http = new QHttp( m_baseHost, 80, this );
636
AmarokHttp *http = new AmarokHttp( m_baseHost, 80, this );
555
637
connect( http, SIGNAL( requestFinished( int, bool ) ), this, SLOT( skipFinished( int, bool ) ) );
557
639
http->get( QString( m_basePath + "/control.php?session=%1&command=skip&debug=%2" )
558
640
.arg( m_session )
560
amaroK::StatusBar::instance()->shortMessage( i18n("Skipping song...") );
642
Amarok::StatusBar::instance()->shortMessage( i18n("Skipping song...") );
565
647
WebService::ban() //SLOT
567
QHttp *http = new QHttp( m_baseHost, 80, this );
649
AmarokHttp *http = new AmarokHttp( m_baseHost, 80, this );
568
650
connect( http, SIGNAL( requestFinished( int, bool ) ), this, SLOT( banFinished( int, bool ) ) );
570
652
http->get( QString( m_basePath + "/control.php?session=%1&command=ban&debug=%2" )
571
653
.arg( m_session )
573
amaroK::StatusBar::instance()->shortMessage( i18n("Ban, as in dislike", "Banning song...") );
655
Amarok::StatusBar::instance()->shortMessage( i18n("Ban, as in dislike", "Banning song...") );
622
704
if ( username.isEmpty() )
623
705
username = m_username;
625
QHttp *http = new QHttp( m_baseHost, 80, this );
707
AmarokHttp *http = new AmarokHttp( m_baseHost, 80, this );
626
708
connect( http, SIGNAL( requestFinished( bool ) ), this, SLOT( friendsFinished( bool ) ) );
628
710
http->get( QString( "/1.0/user/%1/friends.xml" )
664
746
if ( username.isEmpty() )
665
747
username = m_username;
667
QHttp *http = new QHttp( m_baseHost, 80, this );
749
AmarokHttp *http = new AmarokHttp( m_baseHost, 80, this );
668
750
connect( http, SIGNAL( requestFinished( bool ) ), this, SLOT( neighboursFinished( bool ) ) );
670
752
http->get( QString( "/1.0/user/%1/neighbours.xml" )
706
788
if ( username.isEmpty() )
707
789
username = m_username;
709
QHttp *http = new QHttp( m_baseHost, 80, this );
791
AmarokHttp *http = new AmarokHttp( m_baseHost, 80, this );
710
792
connect( http, SIGNAL( requestFinished( bool ) ), this, SLOT( userTagsFinished( bool ) ) );
712
794
http->get( QString( "/1.0/user/%1/tags.xml?debug=%2" )
748
830
if ( username.isEmpty() )
749
831
username = m_username;
751
QHttp *http = new QHttp( m_baseHost, 80, this );
833
AmarokHttp *http = new AmarokHttp( m_baseHost, 80, this );
752
834
connect( http, SIGNAL( requestFinished( bool ) ), this, SLOT( recentTracksFinished( bool ) ) );
754
836
http->get( QString( "/1.0/user/%1/recenttracks.xml" )
835
917
WebService::recommendFinished( int /*id*/, bool /*error*/ ) //SLOT
837
QHttp* http = (QHttp*) sender();
919
AmarokHttp* http = (AmarokHttp*) sender();
838
920
http->deleteLater();
840
922
debug() << "Recommendation:" << http->readAll() << endl;
845
WebService::parameter( QString keyName, QString data ) const
927
WebService::parameter( const QString keyName, const QString data ) const
847
929
QStringList list = QStringList::split( '\n', data );
864
WebService::parameterArray( QString keyName, QString data ) const
946
WebService::parameterArray( const QString keyName, const QString data ) const
866
948
QStringList result;
867
949
QStringList list = QStringList::split( '\n', data );
884
WebService::parameterKeys( QString keyName, QString data ) const
966
WebService::parameterKeys( const QString keyName, const QString data ) const
886
968
QStringList result;
887
969
QStringList list = QStringList::split( '\n', data );
932
1014
message = i18n( "Failed to play this last.fm stream." );
935
amaroK::StatusBar::instance()->longMessage( message, KDE::StatusBar::Sorry );
1017
Amarok::StatusBar::instance()->longMessage( message, KDE::StatusBar::Sorry );
938
1020
////////////////////////////////////////////////////////////////////////////////