lastfm.cpp
来自「Amarok是一款在LINUX或其他类UNIX操作系统中运行的音频播放器软件。 」· C++ 代码 · 共 1,099 行 · 第 1/3 页
CPP
1,099 行
}voidWebService::readProxy() //SLOT{ QString line; while( m_server->readln( line ) != -1 ) { debug() << line << endl; if( line == "AMAROK_PROXY: SYNC" ) requestMetaData(); }}boolWebService::handshake( const QString& username, const QString& password ){ DEBUG_BLOCK m_username = username; m_password = password; AmarokHttp http( "ws.audioscrobbler.com", 80 ); const QString path = QString( "/radio/handshake.php?version=%1&platform=%2&username=%3&passwordmd5=%4&debug=%5" ) .arg( APP_VERSION ) //Muesli-approved: Amarok version, and Amarok-as-platform .arg( QString("Amarok") ) .arg( QString( QUrl( username ).encodedPathAndQuery() ) ) .arg( KMD5( m_password.utf8() ).hexDigest() ) .arg( "0" ); http.get( path ); do kapp->processEvents(); while( http.state() != QHttp::Unconnected ); if ( http.error() != QHttp::NoError ) return false; const QString result( QDeepCopy<QString>( http.readAll() ) ); debug() << "result: " << result << endl; m_session = parameter( "session", result ); m_baseHost = parameter( "base_url", result ); m_basePath = parameter( "base_path", result ); m_subscriber = parameter( "subscriber", result ) == "1"; m_streamUrl = QUrl( parameter( "stream_url", result ) );// bool banned = parameter( "banned", result ) == "1"; if ( m_session.lower() == "failed" ) { Amarok::StatusBar::instance()->longMessage( i18n( "Amarok failed to establish a session with last.fm. <br>" "Check if your last.fm user and password are correctly set." ) ); return false; } Amarok::config( "Scrobbler" )->writeEntry( "Subscriber", m_subscriber ); if( m_useProxy ) { // Find free port MyServerSocket* socket = new MyServerSocket(); const int port = socket->port(); debug() << "Proxy server using port: " << port << endl; delete socket; m_proxyUrl = QString( "http://localhost:%1/lastfm.mp3" ).arg( port ); m_server = new Amarok::ProcIO(); m_server->setComm( KProcess::Communication( KProcess::AllOutput ) ); *m_server << "amarok_proxy.rb"; *m_server << "--lastfm"; *m_server << QString::number( port ); *m_server << m_streamUrl.toString(); *m_server << AmarokConfig::soundSystem(); *m_server << Amarok::proxyForUrl( m_streamUrl.toString() ); if( !m_server->start( KProcIO::NotifyOnExit, true ) ) { error() << "Failed to start amarok_proxy.rb" << endl; return false; } QString line; while( true ) { kapp->processEvents(); m_server->readln( line ); if( line == "AMAROK_PROXY: startup" ) break; } connect( m_server, SIGNAL( readReady( KProcIO* ) ), this, SLOT( readProxy() ) ); connect( m_server, SIGNAL( processExited( KProcess* ) ), Controller::instance(), SLOT( playbackStopped() ) ); } else m_proxyUrl = m_streamUrl.toString(); return true;}boolWebService::changeStation( QString url ){ debug() << "Changing station:" << url << endl; AmarokHttp http( m_baseHost, 80 ); http.get( QString( m_basePath + "/adjust.php?session=%1&url=%2&debug=0" ) .arg( m_session ) .arg( url ) ); do kapp->processEvents(); while( http.state() != QHttp::Unconnected ); if ( http.error() != QHttp::NoError ) { showError( E_OTHER ); // default error return false; } const QString result( QDeepCopy<QString>( http.readAll() ) ); const int errCode = parameter( "error", result ).toInt(); if ( errCode ) { showError( errCode ); return false; } const QString _url = parameter( "url", result ); if ( _url.startsWith( "lastfm://" ) ) { m_station = _url; // parse it in stationDescription emit stationChanged( _url, m_station ); } else emit stationChanged( _url, QString::null ); return true;}voidWebService::requestMetaData() //SLOT{ AmarokHttp *http = new AmarokHttp( m_baseHost, 80, this ); connect( http, SIGNAL( requestFinished( int, bool ) ), this, SLOT( metaDataFinished( int, bool ) ) ); http->get( QString( m_basePath + "/np.php?session=%1&debug=%2" ) .arg( m_session ) .arg( "0" ) );}voidWebService::metaDataFinished( int /*id*/, bool error ) //SLOT{ DEBUG_BLOCK AmarokHttp* http = (AmarokHttp*) sender(); http->deleteLater(); if( error ) return; const QString result( http->readAll() ); debug() << result << endl; int errCode = parameter( "error", result ).toInt(); if ( errCode > 0 ) { debug() << "Metadata failed with error code: " << errCode << endl; showError( errCode ); return; } m_metaBundle.setArtist( parameter( "artist", result ) ); m_metaBundle.setAlbum ( parameter( "album", result ) ); m_metaBundle.setTitle ( parameter( "track", result ) ); m_metaBundle.setUrl ( KURL( Controller::instance()->getGenreUrl() ) ); m_metaBundle.setLength( parameter( "trackduration", result ).toInt() ); Bundle lastFmStuff; QString imageUrl = parameter( "albumcover_medium", result ); if( imageUrl == "http://static.last.fm/coverart/" || imageUrl == "http://static.last.fm/depth/catalogue/no_album_large.gif" ) imageUrl = QString::null; lastFmStuff.setImageUrl ( CollectionDB::instance()->notAvailCover( true ) ); lastFmStuff.setArtistUrl( parameter( "artist_url", result ) ); lastFmStuff.setAlbumUrl ( parameter( "album_url", result ) ); lastFmStuff.setTitleUrl ( parameter( "track_url", result ) );// bool discovery = parameter( "discovery", result ) != "-1"; m_metaBundle.setLastFmBundle( lastFmStuff ); const KURL u( imageUrl ); if( !u.isValid() ) { debug() << "imageUrl empty or invalid." << endl; emit metaDataResult( m_metaBundle ); return; } KIO::Job* job = KIO::storedGet( u, true, false ); connect( job, SIGNAL( result( KIO::Job* ) ), this, SLOT( fetchImageFinished( KIO::Job* ) ) );}voidWebService::fetchImageFinished( KIO::Job* job ) //SLOT{ DEBUG_BLOCK if( job->error() == 0 ) { const QString path = Amarok::saveLocation() + "lastfm_image.png"; const int size = AmarokConfig::coverPreviewSize(); QImage img( static_cast<KIO::StoredTransferJob*>( job )->data() ); img.smoothScale( size, size ).save( path, "PNG" ); m_metaBundle.lastFmBundle()->setImageUrl( CollectionDB::makeShadowedImage( path, false ) ); } emit metaDataResult( m_metaBundle );}voidWebService::enableScrobbling( bool enabled ) //SLOT{ if ( enabled ) debug() << "Enabling Scrobbling!" << endl; else debug() << "Disabling Scrobbling!" << endl; AmarokHttp *http = new AmarokHttp( m_baseHost, 80, this ); connect( http, SIGNAL( requestFinished( int, bool ) ), this, SLOT( enableScrobblingFinished( int, bool ) ) ); http->get( QString( m_basePath + "/control.php?session=%1&command=%2&debug=%3" ) .arg( m_session ) .arg( enabled ? QString( "rtp" ) : QString( "nortp" ) ) .arg( "0" ) );}voidWebService::enableScrobblingFinished( int /*id*/, bool error ) //SLOT{ AmarokHttp* http = (AmarokHttp*) sender(); http->deleteLater(); if ( error ) return; emit enableScrobblingDone();}voidWebService::love() //SLOT{ AmarokHttp *http = new AmarokHttp( m_baseHost, 80, this ); connect( http, SIGNAL( requestFinished( int, bool ) ), this, SLOT( loveFinished( int, bool ) ) ); http->get( QString( m_basePath + "/control.php?session=%1&command=love&debug=%2" ) .arg( m_session ) .arg( "0" ) ); Amarok::StatusBar::instance()->shortMessage( i18n("love, as in affection", "Loving song...") );}voidWebService::skip() //SLOT{ AmarokHttp *http = new AmarokHttp( m_baseHost, 80, this ); connect( http, SIGNAL( requestFinished( int, bool ) ), this, SLOT( skipFinished( int, bool ) ) ); http->get( QString( m_basePath + "/control.php?session=%1&command=skip&debug=%2" ) .arg( m_session ) .arg( "0" ) ); Amarok::StatusBar::instance()->shortMessage( i18n("Skipping song...") );}voidWebService::ban() //SLOT{ AmarokHttp *http = new AmarokHttp( m_baseHost, 80, this ); connect( http, SIGNAL( requestFinished( int, bool ) ), this, SLOT( banFinished( int, bool ) ) ); http->get( QString( m_basePath + "/control.php?session=%1&command=ban&debug=%2" ) .arg( m_session ) .arg( "0" ) ); Amarok::StatusBar::instance()->shortMessage( i18n("Ban, as in dislike", "Banning song...") );}voidWebService::loveFinished( int /*id*/, bool error ) //SLOT{ DEBUG_BLOCK AmarokHttp* http = (AmarokHttp*) sender(); http->deleteLater(); if( error ) return; emit loveDone();}voidWebService::skipFinished( int /*id*/, bool error ) //SLOT{ DEBUG_BLOCK AmarokHttp* http = (AmarokHttp*) sender(); http->deleteLater(); if( error ) return; EngineController::engine()->flushBuffer(); emit skipDone();}voidWebService::banFinished( int /*id*/, bool error ) //SLOT{ DEBUG_BLOCK AmarokHttp* http = (AmarokHttp*) sender(); http->deleteLater(); if( error ) return; EngineController::engine()->flushBuffer(); emit banDone(); emit skipDone();}voidWebService::friends( QString username ){ if ( username.isEmpty() ) username = m_username; AmarokHttp *http = new AmarokHttp( m_baseHost, 80, this ); connect( http, SIGNAL( requestFinished( bool ) ), this, SLOT( friendsFinished( bool ) ) ); http->get( QString( "/1.0/user/%1/friends.xml" ) .arg( QString( QUrl( username ).encodedPathAndQuery() ) ) );}voidWebService::friendsFinished( int /*id*/, bool error ) //SLOT{ AmarokHttp* http = (AmarokHttp*) sender(); http->deleteLater(); if( error ) return; QDomDocument document; document.setContent( http->readAll() ); if ( document.elementsByTagName( "friends" ).length() == 0 ) { emit friendsResult( QString( "" ), QStringList() ); return;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?