diff options
| author | trialuser02 <trialuser02@90c681e8-e032-0410-971d-27865f9a5e38> | 2013-01-22 18:10:49 +0000 |
|---|---|---|
| committer | trialuser02 <trialuser02@90c681e8-e032-0410-971d-27865f9a5e38> | 2013-01-22 18:10:49 +0000 |
| commit | 1988a82eb1f120af57a9d2d77ca448a7bc459615 (patch) | |
| tree | c417fc3a7f257ae8bb908f1ad896beb500838d52 /src/plugins | |
| parent | ae72f1771205127d3faa30fc52fd8f77f3f16a65 (diff) | |
| download | qmmp-1988a82eb1f120af57a9d2d77ca448a7bc459615.tar.gz qmmp-1988a82eb1f120af57a9d2d77ca448a7bc459615.tar.bz2 qmmp-1988a82eb1f120af57a9d2d77ca448a7bc459615.zip | |
scrobbler: refactoring
git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@3186 90c681e8-e032-0410-971d-27865f9a5e38
Diffstat (limited to 'src/plugins')
| -rw-r--r-- | src/plugins/General/scrobbler/scrobbler.cpp | 121 | ||||
| -rw-r--r-- | src/plugins/General/scrobbler/scrobbler.h | 14 | ||||
| -rw-r--r-- | src/plugins/General/scrobbler/scrobbler2.cpp | 151 | ||||
| -rw-r--r-- | src/plugins/General/scrobbler/scrobbler2.h | 5 |
4 files changed, 156 insertions, 135 deletions
diff --git a/src/plugins/General/scrobbler/scrobbler.cpp b/src/plugins/General/scrobbler/scrobbler.cpp index 82eed0cc7..d57b38783 100644 --- a/src/plugins/General/scrobbler/scrobbler.cpp +++ b/src/plugins/General/scrobbler/scrobbler.cpp @@ -48,77 +48,38 @@ Scrobbler::Scrobbler(const QString &url, { m_failure_count = 0; m_handshake_count = 0; - m_http = new QNetworkAccessManager(this); - m_login = login; - m_passw = passw; - m_server = url; - m_name = name; - connect(QmmpSettings::instance(), SIGNAL(networkSettingsChanged()), SLOT(setupProxy())); - setupProxy(); - m_disabled = m_login.isEmpty() || m_passw.isEmpty(); - m_passw = QString(QCryptographicHash::hash(m_passw.toAscii(), QCryptographicHash::Md5).toHex()); - connect(m_http, SIGNAL(finished (QNetworkReply *)), SLOT(processResponse(QNetworkReply *))); - m_core = SoundCore::instance(); - connect (m_core, SIGNAL(metaDataChanged()), SLOT(updateMetaData())); - connect (m_core, SIGNAL(stateChanged (Qmmp::State)), SLOT(setState(Qmmp::State))); - m_time = new QTime(); m_submitedSongs = 0; m_handshakeReply = 0; m_submitReply = 0; m_notificationReply = 0; + m_server = url; + m_name = name; m_ua = QString("iScrobbler/1.5.1qmmp-plugins/%1").arg(Qmmp::strVersion()).toAscii(); + m_login = login; + m_passw = passw; + m_passw = QString(QCryptographicHash::hash(m_passw.toAscii(), QCryptographicHash::Md5).toHex()); + m_disabled = login.isEmpty() || passw.isEmpty(); + m_core = SoundCore::instance(); + m_http = new QNetworkAccessManager(this); + m_time = new QTime(); - QFile file(QDir::homePath() +"/.qmmp/scrobbler_" + m_name + ".cache"); - - if (!m_disabled && file.open(QIODevice::ReadOnly)) - { - int s; - QString line, param, value; - while (!file.atEnd()) - { - line = QString::fromUtf8(file.readLine()).trimmed(); - if ((s = line.indexOf("=")) < 0) - continue; + connect(m_http, SIGNAL(finished (QNetworkReply *)), SLOT(processResponse(QNetworkReply *))); + connect(QmmpSettings::instance(), SIGNAL(networkSettingsChanged()), SLOT(setupProxy())); + connect (m_core, SIGNAL(metaDataChanged()), SLOT(updateMetaData())); + connect (m_core, SIGNAL(stateChanged (Qmmp::State)), SLOT(setState(Qmmp::State))); - param = line.left(s); - value = line.right(line.size() - s - 1); + setupProxy(); + readCache(); - if (param == "title") - { - m_songCache << SongInfo(); - m_songCache.last().setMetaData(Qmmp::TITLE, value); - } - else if (m_songCache.isEmpty()) - continue; - else if (param == "artist") - m_songCache.last().setMetaData(Qmmp::ARTIST, value); - else if (param == "album") - m_songCache.last().setMetaData(Qmmp::ALBUM, value); - else if (param == "comment") - m_songCache.last().setMetaData(Qmmp::COMMENT, value); - else if (param == "genre") - m_songCache.last().setMetaData(Qmmp::GENRE, value); - else if (param == "year") - m_songCache.last().setMetaData(Qmmp::YEAR, value); - else if (param == "track") - m_songCache.last().setMetaData(Qmmp::TRACK, value); - else if (param == "length") - m_songCache.last().setLength(value.toInt()); - else if (param == "time") - m_songCache.last().setTimeStamp(value.toUInt()); - } - file.close(); - } m_start_ts = QDateTime::currentDateTime().toTime_t(); - if (!m_disabled) - handshake(); + handshake(); } Scrobbler::~Scrobbler() { delete m_time; - syncCache(); + writeCache(); } void Scrobbler::setState(Qmmp::State state) @@ -138,7 +99,7 @@ void Scrobbler::setState(Qmmp::State state) { m_song.setTimeStamp(m_start_ts); m_songCache << m_song; - syncCache(); + writeCache(); } m_song.clear(); if (m_songCache.isEmpty()) @@ -398,7 +359,7 @@ bool Scrobbler::isReady() return !m_submitUrl.isEmpty() && !m_session.isEmpty(); } -void Scrobbler::syncCache() +void Scrobbler::writeCache() { QFile file(QDir::homePath() +"/.qmmp/scrobbler_" + m_name + ".cache"); if (m_songCache.isEmpty()) @@ -422,6 +383,50 @@ void Scrobbler::syncCache() file.close(); } +void Scrobbler::readCache() +{ + int s = 0; + QString line, param, value; + QFile file(QDir::homePath() +"/.qmmp/scrobbler_" + m_name + ".cache"); + if(m_disabled || !file.open(QIODevice::ReadOnly)) + return; + + while (!file.atEnd()) + { + line = QString::fromUtf8(file.readLine()).trimmed(); + if ((s = line.indexOf("=")) < 0) + continue; + + param = line.left(s); + value = line.right(line.size() - s - 1); + + if (param == "title") + { + m_songCache << SongInfo(); + m_songCache.last().setMetaData(Qmmp::TITLE, value); + } + else if (m_songCache.isEmpty()) + continue; + else if (param == "artist") + m_songCache.last().setMetaData(Qmmp::ARTIST, value); + else if (param == "album") + m_songCache.last().setMetaData(Qmmp::ALBUM, value); + else if (param == "comment") + m_songCache.last().setMetaData(Qmmp::COMMENT, value); + else if (param == "genre") + m_songCache.last().setMetaData(Qmmp::GENRE, value); + else if (param == "year") + m_songCache.last().setMetaData(Qmmp::YEAR, value); + else if (param == "track") + m_songCache.last().setMetaData(Qmmp::TRACK, value); + else if (param == "length") + m_songCache.last().setLength(value.toInt()); + else if (param == "time") + m_songCache.last().setTimeStamp(value.toUInt()); + } + file.close(); +} + SongInfo::SongInfo() { m_length = 0; diff --git a/src/plugins/General/scrobbler/scrobbler.h b/src/plugins/General/scrobbler/scrobbler.h index c78d8dbad..34024c3c2 100644 --- a/src/plugins/General/scrobbler/scrobbler.h +++ b/src/plugins/General/scrobbler/scrobbler.h @@ -90,27 +90,29 @@ private: void submit(); void sendNotification(const SongInfo &info); bool isReady(); - void syncCache(); + void writeCache(); + void readCache(); uint m_start_ts; SongInfo m_song; QNetworkAccessManager *m_http; SoundCore *m_core; + QNetworkReply *m_handshakeReply; + QNetworkReply *m_submitReply; + QNetworkReply *m_notificationReply; + QTime* m_time; QString m_login; QString m_passw; QString m_submitUrl; QString m_nowPlayingUrl; QString m_session; + QString m_server, m_name; QList <SongInfo> m_songCache; QByteArray m_ua; - QTime* m_time; int m_submitedSongs; int m_failure_count; int m_handshake_count; - QNetworkReply *m_handshakeReply; - QNetworkReply *m_submitReply; - QNetworkReply *m_notificationReply; bool m_disabled; - QString m_server, m_name; + }; #endif diff --git a/src/plugins/General/scrobbler/scrobbler2.cpp b/src/plugins/General/scrobbler/scrobbler2.cpp index 278bc8e19..c240f3790 100644 --- a/src/plugins/General/scrobbler/scrobbler2.cpp +++ b/src/plugins/General/scrobbler/scrobbler2.cpp @@ -44,65 +44,27 @@ Scrobbler2::Scrobbler2(const QString &url, const QString &name, QObject *parent) { m_getTokenReply = 0; m_getSessionReply = 0; - m_http = new QNetworkAccessManager(this); + m_notificationReply = 0; + m_submitedSongs = 0; + m_submitReply = 0; m_state = Qmmp::Stopped; m_server = url; m_name = name; + m_time = new QTime(); + m_ua = QString("qmmp-plugins/%1").arg(Qmmp::strVersion().toLower()).toAscii(); + m_http = new QNetworkAccessManager(this); + m_core = SoundCore::instance(); QSettings settings(Qmmp::configFile(), QSettings::IniFormat); m_session = settings.value("Scrobbler/lastfm_session").toString(); - connect(QmmpSettings::instance(), SIGNAL(networkSettingsChanged()), SLOT(setupProxy())); - setupProxy(); + connect(m_http, SIGNAL(finished (QNetworkReply *)), SLOT(processResponse(QNetworkReply *))); - m_core = SoundCore::instance(); + connect(QmmpSettings::instance(), SIGNAL(networkSettingsChanged()), SLOT(setupProxy())); connect (m_core, SIGNAL(metaDataChanged()), SLOT(updateMetaData())); connect (m_core, SIGNAL(stateChanged (Qmmp::State)), SLOT(setState(Qmmp::State))); - m_time = new QTime(); - m_submitedSongs = 0; - m_submitReply = 0; - m_notificationReply = 0; - m_ua = QString("qmmp-plugins/%1").arg(Qmmp::strVersion().toLower()).toAscii(); - - QFile file(QDir::homePath() +"/.qmmp/scrobbler_" + m_name + ".cache"); - if (file.open(QIODevice::ReadOnly)) - { - int s; - QString line, param, value; - while (!file.atEnd()) - { - line = QString::fromUtf8(file.readLine()).trimmed(); - if ((s = line.indexOf("=")) < 0) - continue; - - param = line.left(s); - value = line.right(line.size() - s - 1); + setupProxy(); + readCache(); - if (param == "title") - { - m_songCache << SongInfo(); - m_songCache.last().setMetaData(Qmmp::TITLE, value); - } - else if (m_songCache.isEmpty()) - continue; - else if (param == "artist") - m_songCache.last().setMetaData(Qmmp::ARTIST, value); - else if (param == "album") - m_songCache.last().setMetaData(Qmmp::ALBUM, value); - else if (param == "comment") - m_songCache.last().setMetaData(Qmmp::COMMENT, value); - else if (param == "genre") - m_songCache.last().setMetaData(Qmmp::GENRE, value); - else if (param == "year") - m_songCache.last().setMetaData(Qmmp::YEAR, value); - else if (param == "track") - m_songCache.last().setMetaData(Qmmp::TRACK, value); - else if (param == "length") - m_songCache.last().setLength(value.toInt()); - else if (param == "time") - m_songCache.last().setTimeStamp(value.toUInt()); - } - file.close(); - } if(m_session.isEmpty()) getToken(); else @@ -120,7 +82,7 @@ Scrobbler2::Scrobbler2(const QString &url, const QString &name, QObject *parent) Scrobbler2::~Scrobbler2() { delete m_time; - syncCache(); + writeCache(); } void Scrobbler2::setState(Qmmp::State state) @@ -134,12 +96,12 @@ void Scrobbler2::setState(Qmmp::State state) break; case Qmmp::Stopped: if (!m_song.metaData().isEmpty() - && ((m_time->elapsed ()/1000 > 240) || (m_time->elapsed ()/1000 > int(m_song.length()/2))) - && (m_song.length() > MIN_SONG_LENGTH)) + && ((m_time->elapsed ()/1000 > 240) || (m_time->elapsed ()/1000 > int(m_song.length()/2))) + && (m_song.length() > MIN_SONG_LENGTH)) { m_song.setTimeStamp(m_start_ts); m_songCache << m_song; - syncCache(); + writeCache(); } m_song.clear(); @@ -157,15 +119,15 @@ void Scrobbler2::setState(Qmmp::State state) void Scrobbler2::updateMetaData() { QMap <Qmmp::MetaData, QString> metadata = m_core->metaData(); - if (m_state == Qmmp::Playing - && !metadata.value(Qmmp::TITLE).isEmpty() //skip empty tags - && !metadata.value(Qmmp::ARTIST).isEmpty() - && m_core->totalTime()) //skip stream - { - m_song = SongInfo(metadata, m_core->totalTime()/1000); - if (!m_session.isEmpty() && !m_notificationReply && !m_submitReply) - sendNotification(m_song); - } + if(m_state != Qmmp::Playing || m_core->totalTime() <= 0) //skip stream + return; + if(metadata.value(Qmmp::TITLE).isEmpty() || metadata.value(Qmmp::ARTIST).isEmpty()) //skip empty tags + return; + if(m_notificationReply && m_submitReply) //used + return; + + m_song = SongInfo(metadata, m_core->totalTime()/1000); + sendNotification(m_song); } void Scrobbler2::processResponse(QNetworkReply *reply) @@ -175,7 +137,7 @@ void Scrobbler2::processResponse(QNetworkReply *reply) qWarning("Scrobbler2[%s]: http error: %s", qPrintable(m_name), qPrintable(reply->errorString())); } - ScrobblerResponse response; + Scrobbler2Response response; QStringList tags; QXmlStreamReader reader(reply); while(!reader.atEnd()) @@ -304,7 +266,7 @@ void Scrobbler2::processResponse(QNetworkReply *reply) } else { - syncCache(); // update the cache file to reflect the empty cache + writeCache(); // update the cache file to reflect the empty cache updateMetaData(); } } @@ -359,7 +321,8 @@ void Scrobbler2::setupProxy() void Scrobbler2::getToken() { - qDebug("%s", Q_FUNC_INFO); + qDebug("Scrobbler2[%s]: new token request", qPrintable(m_name)); + m_session.clear(); QUrl url(QString("http://") + m_server + "/?"); url.setPort(80); url.addQueryItem("method", "auth.getToken"); @@ -380,7 +343,7 @@ void Scrobbler2::getToken() void Scrobbler2::getSession() { - qDebug("%s", Q_FUNC_INFO); + qDebug("Scrobbler2[%s]: new session request", qPrintable(m_name)); QUrl url(QString("http://") + m_server + "/?"); url.setPort(80); url.addQueryItem("api_key", API_KEY); @@ -459,7 +422,7 @@ void Scrobbler2::sendNotification(const SongInfo &info) { if(m_session.isEmpty()) return; - qDebug("Scrobbler2[%s] sending notification", qPrintable(m_name)); + qDebug("Scrobbler2[%s]: sending notification", qPrintable(m_name)); QMap <QString, QString> params; params.insert("track", info.metaData(Qmmp::TITLE)); @@ -503,7 +466,7 @@ void Scrobbler2::sendNotification(const SongInfo &info) m_notificationReply = m_http->post(request, bodyData); } -void Scrobbler2::syncCache() +void Scrobbler2::writeCache() { QFile file(QDir::homePath() +"/.qmmp/scrobbler_" + m_name + ".cache"); if (m_songCache.isEmpty()) @@ -511,7 +474,12 @@ void Scrobbler2::syncCache() file.remove(); return; } - file.open(QIODevice::WriteOnly); + if(!file.open(QIODevice::WriteOnly)) + { + qWarning("Scrobbler2[%s]: error %d: %s", qPrintable(m_name), + file.error(), qPrintable(file.errorString())); + return; + } foreach(SongInfo m, m_songCache) { file.write(QString("title=%1").arg(m.metaData(Qmmp::TITLE)).toUtf8() +"\n"); @@ -526,3 +494,48 @@ void Scrobbler2::syncCache() } file.close(); } + +void Scrobbler2::readCache() +{ + int s = 0; + QString line, param, value; + QFile file(QDir::homePath() +"/.qmmp/scrobbler_" + m_name + ".cache"); + + if(!file.open(QIODevice::ReadOnly)) + return; + + while (!file.atEnd()) + { + line = QString::fromUtf8(file.readLine()).trimmed(); + if ((s = line.indexOf("=")) < 0) + continue; + + param = line.left(s); + value = line.right(line.size() - s - 1); + + if (param == "title") + { + m_songCache << SongInfo(); + m_songCache.last().setMetaData(Qmmp::TITLE, value); + } + else if (m_songCache.isEmpty()) + continue; + else if (param == "artist") + m_songCache.last().setMetaData(Qmmp::ARTIST, value); + else if (param == "album") + m_songCache.last().setMetaData(Qmmp::ALBUM, value); + else if (param == "comment") + m_songCache.last().setMetaData(Qmmp::COMMENT, value); + else if (param == "genre") + m_songCache.last().setMetaData(Qmmp::GENRE, value); + else if (param == "year") + m_songCache.last().setMetaData(Qmmp::YEAR, value); + else if (param == "track") + m_songCache.last().setMetaData(Qmmp::TRACK, value); + else if (param == "length") + m_songCache.last().setLength(value.toInt()); + else if (param == "time") + m_songCache.last().setTimeStamp(value.toUInt()); + } + file.close(); +} diff --git a/src/plugins/General/scrobbler/scrobbler2.h b/src/plugins/General/scrobbler/scrobbler2.h index ebc804cb0..9bd0aeef4 100644 --- a/src/plugins/General/scrobbler/scrobbler2.h +++ b/src/plugins/General/scrobbler/scrobbler2.h @@ -32,7 +32,7 @@ class SoundCore; /** @author Ilya Kotov <forkotov02@hotmail.ru> */ -struct ScrobblerResponse +struct Scrobbler2Response { QString status; QString token; @@ -66,7 +66,8 @@ private: enum { MIN_SONG_LENGTH = 30 }; void sendNotification(const SongInfo &info); - void syncCache(); + void writeCache(); + void readCache(); uint m_start_ts; SongInfo m_song; QNetworkAccessManager *m_http; |
