~ubuntu-branches/ubuntu/gutsy/amarok/gutsy-updates

« back to all changes in this revision

Viewing changes to amarok/src/lastfm.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Mercatante
  • Date: 2006-11-03 23:57:33 UTC
  • mfrom: (1.31.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061103235733-a41oyfz4mzienqin
Tags: 2:1.4.4-0ubuntu2
Add debian/kubuntu-media-amarok and
debian amarok_play_audiocd.desktop for good KDE integration

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
using namespace LastFm;
47
47
 
48
48
///////////////////////////////////////////////////////////////////////////////
 
49
// CLASS AmarokHttp
 
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,
 
54
                         QObject* parent )
 
55
    : QObject( parent ),
 
56
      m_hostname( hostname ),
 
57
      m_port( port )
 
58
{}
 
59
 
 
60
int
 
61
AmarokHttp::get ( const QString & path )
 
62
{
 
63
    QString uri = QString( "http://%1:%2/%3" )
 
64
                  .arg( m_hostname )
 
65
                  .arg( m_port )
 
66
                  .arg( path );
 
67
 
 
68
    m_done = false;
 
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*)));
 
76
 
 
77
    return 0;
 
78
}
 
79
 
 
80
QHttp::State
 
81
AmarokHttp::state() const
 
82
{
 
83
    return m_state;
 
84
}
 
85
 
 
86
QByteArray
 
87
AmarokHttp::readAll ()
 
88
{
 
89
    return m_result;
 
90
}
 
91
 
 
92
QHttp::Error
 
93
AmarokHttp::error()
 
94
{
 
95
    return m_error;
 
96
}
 
97
 
 
98
void
 
99
AmarokHttp::slotData(KIO::Job*, const QByteArray& data)
 
100
{
 
101
    if( data.size() == 0 ) {
 
102
        return;
 
103
    }
 
104
    else if ( m_result.size() == 0 ) {
 
105
        m_result = data;
 
106
    }
 
107
    else if ( m_result.resize( m_result.size() + data.size() ) ) {
 
108
        memcpy( m_result.end(), data.data(),  data.size() );
 
109
    }
 
110
}
 
111
 
 
112
void
 
113
AmarokHttp::slotResult(KIO::Job* job)
 
114
{
 
115
    bool err = job->error();
 
116
    if( err || m_error != QHttp::NoError ) {
 
117
        m_error = QHttp::UnknownError;
 
118
    }
 
119
    else {
 
120
        m_error = QHttp::NoError;
 
121
    }
 
122
    m_done = true;
 
123
    m_state = QHttp::Unconnected;
 
124
    emit( requestFinished( 0, err ) );
 
125
}
 
126
 
 
127
 
 
128
 
 
129
///////////////////////////////////////////////////////////////////////////////
49
130
// CLASS Controller
50
131
////////////////////////////////////////////////////////////////////////////////
51
132
 
55
136
    : QObject( EngineController::instance(), "lastfmController" )
56
137
    , m_service( 0 )
57
138
{
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" ) );
61
142
 
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" ) );
64
145
 
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 );
68
149
}
178
259
void
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 );
183
264
 
184
265
    KAction* action;
185
266
    for( action = m_actionList.first(); action; action = m_actionList.next() )
252
333
    }
253
334
 
254
335
    /// GROUP RADIOS
255
 
    //eg: lastfm://group/amaroK%20users
 
336
    //eg: lastfm://group/Amarok%20users
256
337
    else if ( elements[1] == "group" )
257
338
        return i18n( "Group Radio: %1" ).arg( elements[2] );
258
339
 
312
393
    m_username = username;
313
394
    m_password = password;
314
395
 
315
 
    QHttp http( "ws.audioscrobbler.com", 80 );
 
396
    AmarokHttp http( "ws.audioscrobbler.com", 80 );
316
397
 
317
398
    const QString path =
318
399
            QString( "/radio/handshake.php?version=%1&platform=%2&username=%3&passwordmd5=%4&debug=%5" )
345
426
    if ( m_session.lower() == "failed" )
346
427
        return false;
347
428
 
348
 
    amaroK::config( "Scrobbler" )->writeEntry( "Subscriber", m_subscriber );
 
429
    Amarok::config( "Scrobbler" )->writeEntry( "Subscriber", m_subscriber );
349
430
 
350
431
    // Find free port
351
432
    MyServerSocket* socket = new MyServerSocket();
355
436
 
356
437
    m_proxyUrl = QString( "http://localhost:%1/lastfm.mp3" ).arg( port );
357
438
 
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() );
365
447
 
366
448
    if( !m_server->start( KProcIO::NotifyOnExit, true ) ) {
367
449
        error() << "Failed to start amarok_proxy.rb" << endl;
387
469
{
388
470
    debug() << "Changing station:" << url << endl;
389
471
 
390
 
    QHttp http( m_baseHost, 80 );
 
472
    AmarokHttp http( m_baseHost, 80 );
391
473
 
392
474
    http.get( QString( m_basePath + "/adjust.php?session=%1&url=%2&debug=0" )
393
475
             .arg( m_session )
427
509
void
428
510
WebService::requestMetaData() //SLOT
429
511
{
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 ) ) );
432
514
 
433
515
    http->get( QString( m_basePath + "/np.php?session=%1&debug=%2" )
441
523
{
442
524
    DEBUG_BLOCK
443
525
 
444
 
    QHttp* http = (QHttp*) sender();
 
526
    AmarokHttp* http = (AmarokHttp*) sender();
445
527
    http->deleteLater();
446
528
    if( error ) return;
447
529
 
494
576
    DEBUG_BLOCK
495
577
 
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();
499
581
 
500
582
        QImage img( static_cast<KIO::StoredTransferJob*>( job )->data() );
514
596
    else
515
597
        debug() << "Disabling Scrobbling!" << endl;
516
598
 
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 ) ) );
519
601
 
520
602
    http->get( QString( m_basePath + "/control.php?session=%1&command=%2&debug=%3" )
527
609
void
528
610
WebService::enableScrobblingFinished( int /*id*/, bool error ) //SLOT
529
611
{
530
 
    QHttp* http = (QHttp*) sender();
 
612
    AmarokHttp* http = (AmarokHttp*) sender();
531
613
    http->deleteLater();
532
614
    if ( error ) return;
533
615
 
538
620
void
539
621
WebService::love() //SLOT
540
622
{
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 ) ) );
543
625
 
544
626
    http->get( QString( m_basePath + "/control.php?session=%1&command=love&debug=%2" )
545
627
                  .arg( m_session )
546
628
                  .arg( "0" ) );
547
 
    amaroK::StatusBar::instance()->shortMessage( i18n("love, as in affection", "Loving song...") );
 
629
    Amarok::StatusBar::instance()->shortMessage( i18n("love, as in affection", "Loving song...") );
548
630
}
549
631
 
550
632
 
551
633
void
552
634
WebService::skip() //SLOT
553
635
{
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 ) ) );
556
638
 
557
639
    http->get( QString( m_basePath + "/control.php?session=%1&command=skip&debug=%2" )
558
640
                  .arg( m_session )
559
641
                  .arg( "0" ) );
560
 
    amaroK::StatusBar::instance()->shortMessage( i18n("Skipping song...") );
 
642
    Amarok::StatusBar::instance()->shortMessage( i18n("Skipping song...") );
561
643
}
562
644
 
563
645
 
564
646
void
565
647
WebService::ban() //SLOT
566
648
{
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 ) ) );
569
651
 
570
652
    http->get( QString( m_basePath + "/control.php?session=%1&command=ban&debug=%2" )
571
653
                  .arg( m_session )
572
654
                  .arg( "0" ) );
573
 
    amaroK::StatusBar::instance()->shortMessage( i18n("Ban, as in dislike", "Banning song...") );
 
655
    Amarok::StatusBar::instance()->shortMessage( i18n("Ban, as in dislike", "Banning song...") );
574
656
}
575
657
 
576
658
 
579
661
{
580
662
    DEBUG_BLOCK
581
663
 
582
 
    QHttp* http = (QHttp*) sender();
 
664
    AmarokHttp* http = (AmarokHttp*) sender();
583
665
    http->deleteLater();
584
666
    if( error ) return;
585
667
 
592
674
{
593
675
    DEBUG_BLOCK
594
676
 
595
 
    QHttp* http = (QHttp*) sender();
 
677
    AmarokHttp* http = (AmarokHttp*) sender();
596
678
    http->deleteLater();
597
679
    if( error ) return;
598
680
 
606
688
{
607
689
    DEBUG_BLOCK
608
690
 
609
 
    QHttp* http = (QHttp*) sender();
 
691
    AmarokHttp* http = (AmarokHttp*) sender();
610
692
    http->deleteLater();
611
693
    if( error ) return;
612
694
 
622
704
    if ( username.isEmpty() )
623
705
        username = m_username;
624
706
 
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 ) ) );
627
709
 
628
710
    http->get( QString( "/1.0/user/%1/friends.xml" )
633
715
void
634
716
WebService::friendsFinished( int /*id*/, bool error ) //SLOT
635
717
{
636
 
    QHttp* http = (QHttp*) sender();
 
718
    AmarokHttp* http = (AmarokHttp*) sender();
637
719
    http->deleteLater();
638
720
    if( error ) return;
639
721
 
664
746
    if ( username.isEmpty() )
665
747
        username = m_username;
666
748
 
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 ) ) );
669
751
 
670
752
    http->get( QString( "/1.0/user/%1/neighbours.xml" )
675
757
void
676
758
WebService::neighboursFinished( int /*id*/, bool error ) //SLOT
677
759
{
678
 
    QHttp* http = (QHttp*) sender();
 
760
    AmarokHttp* http = (AmarokHttp*) sender();
679
761
    http->deleteLater();
680
762
    if( error )  return;
681
763
 
706
788
    if ( username.isEmpty() )
707
789
        username = m_username;
708
790
 
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 ) ) );
711
793
 
712
794
    http->get( QString( "/1.0/user/%1/tags.xml?debug=%2" )
717
799
void
718
800
WebService::userTagsFinished( int /*id*/, bool error ) //SLOT
719
801
{
720
 
    QHttp* http = (QHttp*) sender();
 
802
    AmarokHttp* http = (AmarokHttp*) sender();
721
803
    http->deleteLater();
722
804
    if( error ) return;
723
805
 
748
830
    if ( username.isEmpty() )
749
831
        username = m_username;
750
832
 
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 ) ) );
753
835
 
754
836
    http->get( QString( "/1.0/user/%1/recenttracks.xml" )
759
841
void
760
842
WebService::recentTracksFinished( int /*id*/, bool error ) //SLOT
761
843
{
762
 
    QHttp* http = (QHttp*) sender();
 
844
    AmarokHttp* http = (AmarokHttp*) sender();
763
845
    http->deleteLater();
764
846
    if( error ) return;
765
847
 
834
916
void
835
917
WebService::recommendFinished( int /*id*/, bool /*error*/ ) //SLOT
836
918
{
837
 
    QHttp* http = (QHttp*) sender();
 
919
    AmarokHttp* http = (AmarokHttp*) sender();
838
920
    http->deleteLater();
839
921
 
840
922
    debug() << "Recommendation:" << http->readAll() << endl;
842
924
 
843
925
 
844
926
QString
845
 
WebService::parameter( QString keyName, QString data ) const
 
927
WebService::parameter( const QString keyName, const QString data ) const
846
928
{
847
929
    QStringList list = QStringList::split( '\n', data );
848
930
 
861
943
 
862
944
 
863
945
QStringList
864
 
WebService::parameterArray( QString keyName, QString data ) const
 
946
WebService::parameterArray( const QString keyName, const QString data ) const
865
947
{
866
948
    QStringList result;
867
949
    QStringList list = QStringList::split( '\n', data );
881
963
 
882
964
 
883
965
QStringList
884
 
WebService::parameterKeys( QString keyName, QString data ) const
 
966
WebService::parameterKeys( const QString keyName, const QString data ) const
885
967
{
886
968
    QStringList result;
887
969
    QStringList list = QStringList::split( '\n', data );
932
1014
                message = i18n( "Failed to play this last.fm stream." );
933
1015
    }
934
1016
 
935
 
    amaroK::StatusBar::instance()->longMessage( message, KDE::StatusBar::Sorry );
 
1017
    Amarok::StatusBar::instance()->longMessage( message, KDE::StatusBar::Sorry );
936
1018
}
937
1019
 
938
1020
////////////////////////////////////////////////////////////////////////////////