From 324c79580e119ea8e3c5f9beed732458a1ceb5b3 Mon Sep 17 00:00:00 2001 From: trialuser02 Date: Sun, 17 Nov 2013 10:01:52 +0000 Subject: moved libre.fm scrobbler to 2.0 api git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@3913 90c681e8-e032-0410-971d-27865f9a5e38 --- src/plugins/General/scrobbler/defines.h | 30 + src/plugins/General/scrobbler/lastfmscrobbler.cpp | 595 -------------------- src/plugins/General/scrobbler/lastfmscrobbler.h | 122 ----- src/plugins/General/scrobbler/librefmscrobbler.cpp | 374 ------------- src/plugins/General/scrobbler/librefmscrobbler.h | 81 --- src/plugins/General/scrobbler/scrobbler.cpp | 602 +++++++++++++++++++++ src/plugins/General/scrobbler/scrobbler.h | 125 +++++ src/plugins/General/scrobbler/scrobbler.pro | 11 +- src/plugins/General/scrobbler/scrobblerhandler.cpp | 16 +- src/plugins/General/scrobbler/settingsdialog.cpp | 101 +++- src/plugins/General/scrobbler/settingsdialog.h | 7 +- src/plugins/General/scrobbler/settingsdialog.ui | 88 +-- 12 files changed, 890 insertions(+), 1262 deletions(-) create mode 100644 src/plugins/General/scrobbler/defines.h delete mode 100644 src/plugins/General/scrobbler/lastfmscrobbler.cpp delete mode 100644 src/plugins/General/scrobbler/lastfmscrobbler.h delete mode 100644 src/plugins/General/scrobbler/librefmscrobbler.cpp delete mode 100644 src/plugins/General/scrobbler/librefmscrobbler.h create mode 100644 src/plugins/General/scrobbler/scrobbler.cpp create mode 100644 src/plugins/General/scrobbler/scrobbler.h diff --git a/src/plugins/General/scrobbler/defines.h b/src/plugins/General/scrobbler/defines.h new file mode 100644 index 000000000..36648fe4f --- /dev/null +++ b/src/plugins/General/scrobbler/defines.h @@ -0,0 +1,30 @@ +/*************************************************************************** + * Copyright (C) 2013 by Ilya Kotov * + * forkotov02@hotmail.ru * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef DEFINES_H +#define DEFINES_H + +#define SCROBBLER_LASTFM_URL "http://ws.audioscrobbler.com/2.0/" +#define SCROBBLER_LIBREFM_URL "http://libre.fm/2.0/" + +#define LASTFM_AUTH_URL "http://www.last.fm/api/auth/" +#define LIBREFM_AUTH_URL "http://libre.fm/api/auth/" + +#endif // DEFINES_H diff --git a/src/plugins/General/scrobbler/lastfmscrobbler.cpp b/src/plugins/General/scrobbler/lastfmscrobbler.cpp deleted file mode 100644 index c3f25cd5e..000000000 --- a/src/plugins/General/scrobbler/lastfmscrobbler.cpp +++ /dev/null @@ -1,595 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2010-2013 by Ilya Kotov * - * forkotov02@hotmail.ru * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * - ***************************************************************************/ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "lastfmscrobbler.h" - -#define SCROBBLER_LASTFM_URL "http://ws.audioscrobbler.com/2.0/" -#define API_KEY "d71c6f01b2ea562d7042bd5f5970041f" -#define SECRET "32d47bc0010473d40e1d38bdcff20968" - - -void LastfmResponse::parse(QIODevice *device) -{ - QXmlStreamReader reader(device); - QStringList tags; - while(!reader.atEnd()) - { - reader.readNext(); - if(reader.isStartElement()) - { - tags << reader.name().toString(); - if(tags.last() == "lfm") - status = reader.attributes().value("status").toString(); - else if(tags.last() == "error") - code = reader.attributes().value("code").toString(); - } - else if(reader.isCharacters() && !reader.isWhitespace()) - { - if(tags.last() == "token") - token = reader.text().toString(); - else if(tags.last() == "error") - error = reader.text().toString(); - if(tags.count() >= 2 && tags.at(tags.count() - 2) == "session") - { - if(tags.last() == "key") - key = reader.text().toString(); - else if(tags.last() == "name") - name = reader.text().toString(); - else if(tags.last() == "subscriber") - subscriber = reader.text().toString(); - } - } - else if(reader.isEndElement()) - { - tags.takeLast(); - } - } -} - -LastfmScrobbler::LastfmScrobbler(QObject *parent) : QObject(parent) -{ - m_notificationReply = 0; - m_submitedSongs = 0; - m_submitReply = 0; - m_state = Qmmp::Stopped; - m_time = new QTime(); - m_cache = new ScrobblerCache(QDir::homePath() +"/.qmmp/scrobbler_lastfm.cache"); - 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(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))); - - setupProxy(); - m_cachedSongs = m_cache->load(); - - if(!m_session.isEmpty()) - { - submit(); - if(m_core->state() == Qmmp::Playing) - { - setState(Qmmp::Playing); - updateMetaData(); - } - } -} - -LastfmScrobbler::~LastfmScrobbler() -{ - m_cache->save(m_cachedSongs); - delete m_time; - delete m_cache; - -} - -void LastfmScrobbler::setState(Qmmp::State state) -{ - static Qmmp::State previousState = state; - static int elapsed = 0; - m_state = state; - switch ((uint) state) - { - case Qmmp::Playing: - if (previousState != Qmmp::Paused) - { - qDebug("LastfmScrobbler: new song started"); - m_start_ts = QDateTime::currentDateTime().toTime_t(); - elapsed = 0; - } - else - { - qDebug("LastfmScrobbler: resuming from %d seconds played", elapsed / 1000); - } - m_time->restart(); - break; - case Qmmp::Stopped: - if (previousState != Qmmp::Paused) - elapsed += m_time->elapsed(); - if (!m_song.metaData().isEmpty() - && ((elapsed / 1000 > 240) || (elapsed / 1000 > int(m_song.length()/2))) - && (m_song.length() > MIN_SONG_LENGTH)) - { - m_song.setTimeStamp(m_start_ts); - m_cachedSongs << m_song; - m_cache->save(m_cachedSongs); - } - - m_song.clear(); - if (m_cachedSongs.isEmpty()) - break; - - if (!m_session.isEmpty() && !m_submitReply) - submit(); - break; - case Qmmp::Paused: - elapsed += m_time->elapsed(); - qDebug("LastfmScrobbler: pausing after %d seconds played", elapsed / 1000); - break; - default: - ; - } - previousState = state; -} - -void LastfmScrobbler::updateMetaData() -{ - QMap metadata = m_core->metaData(); - 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 LastfmScrobbler::processResponse(QNetworkReply *reply) -{ - if (reply->error() != QNetworkReply::NoError) - { - qWarning("LastfmScrobbler: http error: %s", qPrintable(reply->errorString())); - } - - LastfmResponse response; - response.parse(reply); - - QString error_code; - if(response.status != "ok" && !response.status.isEmpty()) - { - if(!response.error.isEmpty()) - { - qWarning("LastfmScrobbler: status=%s, %s-%s", qPrintable(response.status), - qPrintable(response.code), qPrintable(response.error)); - error_code = response.code; - } - else - qWarning("LastfmScrobbler: invalid content"); - } - - if (reply == m_submitReply) - { - m_submitReply = 0; - if (response.status == "ok") - { - qDebug("LastfmScrobbler: submited %d song(s)", m_submitedSongs); - while (m_submitedSongs) - { - m_submitedSongs--; - m_cachedSongs.removeFirst (); - } - if (!m_cachedSongs.isEmpty()) //submit remaining songs - { - submit(); - } - else - { - m_cache->save(m_cachedSongs); // update the cache file to reflect the empty cache - updateMetaData(); - } - } - else if(error_code == "9") //invalid session key - { - m_session.clear(); - qWarning("LastfmScrobbler: invalid session key, scrobbling disabled"); - } - else if(error_code == "11" || error_code == "16" || error_code.isEmpty()) //unavailable - { - QTimer::singleShot(120000, this, SLOT(submit())); - } - else - { - m_session.clear(); - qWarning("LastfmScrobbler: service returned unrecoverable error, scrobbling disabled"); - } - } - else if (reply == m_notificationReply) - { - m_notificationReply = 0; - if(response.status == "ok") - { - qDebug("LastfmScrobbler: Now-Playing notification done"); - } - else if(error_code == "9") //invalid session key - { - m_session.clear(); - qWarning("LastfmScrobbler: invalid session key, scrobbling disabled"); - } - } - reply->deleteLater(); -} - -void LastfmScrobbler::setupProxy() -{ - QmmpSettings *gs = QmmpSettings::instance(); - if (gs->isProxyEnabled()) - { - QNetworkProxy proxy(QNetworkProxy::HttpProxy, gs->proxy().host(), gs->proxy().port()); - if(gs->useProxyAuth()) - { - proxy.setUser(gs->proxy().userName()); - proxy.setPassword(gs->proxy().password()); - } - m_http->setProxy(proxy); - } - else - m_http->setProxy(QNetworkProxy::NoProxy); -} - -void LastfmScrobbler::submit() -{ - if (m_cachedSongs.isEmpty() || m_session.isEmpty() || m_submitReply) - return; - - qDebug("LastfmScrobbler: submit request"); - m_submitedSongs = qMin(m_cachedSongs.size(),25); - - QMap params; - for (int i = 0; i < m_submitedSongs; ++i) - { - SongInfo info = m_cachedSongs[i]; - params.insert(QString("track[%1]").arg(i),info.metaData(Qmmp::TITLE)); - params.insert(QString("timestamp[%1]").arg(i),QString("%1").arg(info.timeStamp())); - params.insert(QString("artist[%1]").arg(i),info.metaData(Qmmp::ARTIST)); - params.insert(QString("album[%1]").arg(i),info.metaData(Qmmp::ALBUM)); - params.insert(QString("trackNumber[%1]").arg(i),info.metaData(Qmmp::TRACK)); - params.insert(QString("duration[%1]").arg(i),QString("%1").arg(info.length())); - } - params.insert("api_key", API_KEY); - params.insert("method", "track.scrobble"); - params.insert("sk", m_session); - - QStringList keys = params.keys(); - foreach (QString key, keys) //removes empty keys - { - if(params.value(key).isEmpty() || params.value(key) == "0") - params.remove(key); - } - - QUrl url(SCROBBLER_LASTFM_URL); - url.setPort(80); - - QUrl body(""); - QByteArray data; - foreach (QString key, params.keys()) - { - body.addQueryItem(key, params.value(key)); - data.append(key.toUtf8() + params.value(key).toUtf8()); - } - data.append(SECRET); - body.addQueryItem("api_sig", QCryptographicHash::hash(data, QCryptographicHash::Md5).toHex()); - QByteArray bodyData = body.toEncoded().remove(0,1); - bodyData.replace("+", QUrl::toPercentEncoding("+")); - - QNetworkRequest request(url); - request.setRawHeader("User-Agent", m_ua); - request.setRawHeader("Host", url.host().toAscii()); - request.setRawHeader("Accept", "*/*"); - request.setRawHeader("Content-Type", "application/x-www-form-urlencoded"); - request.setHeader(QNetworkRequest::ContentLengthHeader, bodyData.size()); - m_submitReply = m_http->post(request, bodyData); -} - -void LastfmScrobbler::sendNotification(const SongInfo &info) -{ - if(m_session.isEmpty()) - return; - qDebug("LastfmScrobbler: sending notification"); - - QMap params; - params.insert("track", info.metaData(Qmmp::TITLE)); - params.insert("artist", info.metaData(Qmmp::ARTIST)); - if(!info.metaData(Qmmp::ALBUM).isEmpty()) - params.insert("album", info.metaData(Qmmp::ALBUM)); - if(!info.metaData(Qmmp::TRACK).isEmpty()) - params.insert("trackNumber", info.metaData(Qmmp::TRACK)); - params.insert("duration", QString("%1").arg(info.length())); - params.insert("api_key", API_KEY); - params.insert("method", "track.updateNowPlaying"); - params.insert("sk", m_session); - - foreach (QString key, params) //removes empty keys - { - if(params.value(key).isEmpty()) - params.remove(key); - } - - QUrl url(SCROBBLER_LASTFM_URL); - url.setPort(80); - - QUrl body(""); - QByteArray data; - foreach (QString key, params.keys()) - { - body.addQueryItem(key, params.value(key)); - data.append(key.toUtf8() + params.value(key).toUtf8()); - } - data.append(SECRET); - body.addQueryItem("api_sig", QCryptographicHash::hash(data, QCryptographicHash::Md5).toHex()); - QByteArray bodyData = body.toEncoded().remove(0,1); - bodyData.replace("+", QUrl::toPercentEncoding("+")); - - QNetworkRequest request(url); - request.setRawHeader("User-Agent", m_ua); - request.setRawHeader("Host", url.host().toAscii()); - request.setRawHeader("Accept", "*/*"); - request.setRawHeader("Content-Type", "application/x-www-form-urlencoded"); - request.setHeader(QNetworkRequest::ContentLengthHeader, bodyData.size()); - m_notificationReply = m_http->post(request, bodyData); -} - -LastfmAuth::LastfmAuth(QObject *parent) : QObject(parent) -{ - m_getTokenReply = 0; - m_getSessionReply = 0; - m_ua = QString("qmmp-plugins/%1").arg(Qmmp::strVersion().toLower()).toAscii(); - m_http = new QNetworkAccessManager(this); - connect(m_http, SIGNAL(finished (QNetworkReply *)), SLOT(processResponse(QNetworkReply *))); - - QmmpSettings *gs = QmmpSettings::instance(); - if (gs->isProxyEnabled()) - { - QNetworkProxy proxy(QNetworkProxy::HttpProxy, gs->proxy().host(), gs->proxy().port()); - if(gs->useProxyAuth()) - { - proxy.setUser(gs->proxy().userName()); - proxy.setPassword(gs->proxy().password()); - } - m_http->setProxy(proxy); - } - else - m_http->setProxy(QNetworkProxy::NoProxy); -} - -void LastfmAuth::getToken() -{ - qDebug("LastfmAuth: new token request"); - m_session.clear(); - QUrl url(QString(SCROBBLER_LASTFM_URL) + "?"); - url.setPort(80); - url.addQueryItem("method", "auth.getToken"); - url.addQueryItem("api_key", API_KEY); - - QByteArray data; - data.append("api_key"API_KEY); - data.append("methodauth.getToken"); - data.append(SECRET); - url.addQueryItem("api_sig", QCryptographicHash::hash(data,QCryptographicHash::Md5).toHex()); - - QNetworkRequest request(url); - request.setRawHeader("User-Agent", m_ua); - request.setRawHeader("Host",url.host().toAscii()); - request.setRawHeader("Accept", "*/*"); - m_getTokenReply = m_http->get(request); -} - -void LastfmAuth::getSession() -{ - qDebug("LastfmAuth: new session request"); - QUrl url(QString(SCROBBLER_LASTFM_URL) + "?"); - url.setPort(80); - url.addQueryItem("api_key", API_KEY); - url.addQueryItem("method", "auth.getSession"); - url.addQueryItem("token", m_token); - - QByteArray data; - data.append("api_key"API_KEY); - data.append("methodauth.getSession"); - data.append("token" + m_token.toUtf8()); - data.append(SECRET); - url.addQueryItem("api_sig", QCryptographicHash::hash(data, QCryptographicHash::Md5).toHex()); - - QNetworkRequest request(url); - request.setRawHeader("User-Agent", m_ua); - request.setRawHeader("Host",url.host().toAscii()); - request.setRawHeader("Accept", "*/*"); - m_getSessionReply = m_http->get(request); -} - -void LastfmAuth::checkSession(const QString &session) -{ - qDebug("LastfmAuth: checking session..."); - m_session = session; - QMap params; - params.insert("api_key", API_KEY); - params.insert("sk", session); - params.insert("method", "user.getRecommendedArtists"); - params.insert("limit", "1"); - - QUrl url(SCROBBLER_LASTFM_URL); - url.setPort(80); - - QUrl body(""); - QByteArray data; - foreach (QString key, params.keys()) - { - body.addQueryItem(key, params.value(key)); - data.append(key.toUtf8() + params.value(key).toUtf8()); - } - data.append(SECRET); - body.addQueryItem("api_sig", QCryptographicHash::hash(data, QCryptographicHash::Md5).toHex()); - QByteArray bodyData = body.toEncoded().remove(0,1); - bodyData.replace("+", QUrl::toPercentEncoding("+")); - - QNetworkRequest request(url); - request.setRawHeader("User-Agent", m_ua); - request.setRawHeader("Host", url.host().toAscii()); - request.setRawHeader("Accept", "*/*"); - request.setRawHeader("Content-Type", "application/x-www-form-urlencoded"); - request.setHeader(QNetworkRequest::ContentLengthHeader, bodyData.size()); - m_checkSessionReply = m_http->post(request, bodyData); -} - -QString LastfmAuth::session() const -{ - return m_session; -} - -void LastfmAuth::processResponse(QNetworkReply *reply) -{ - if (reply->error() != QNetworkReply::NoError) - { - qWarning("LastfmAuth: http error: %s", qPrintable(reply->errorString())); - } - - LastfmResponse response; - response.parse(reply); - - QString error_code; - if(response.status != "ok" && !response.status.isEmpty()) - { - if(!response.error.isEmpty()) - { - qWarning("LastfmAuth: status=%s, %s-%s", qPrintable(response.status), - qPrintable(response.code), qPrintable(response.error)); - error_code = response.code; - } - else - qWarning("LastfmAuth: invalid content"); - } - - if (reply == m_getTokenReply) - { - m_getTokenReply = 0; - if(response.status == "ok") - { - m_token = response.token; - qDebug("LastfmAuth: token: %s", qPrintable(m_token)); - QDesktopServices::openUrl("http://www.last.fm/api/auth/?api_key="API_KEY"&token="+m_token); - emit(tokenRequestFinished(NO_ERROR)); - } - else if(error_code.isEmpty()) - { - m_token.clear(); - emit tokenRequestFinished(NETWORK_ERROR); - } - else if(error_code == "8" || error_code == "7" || error_code == "11" || error_code.isEmpty()) - { - m_token.clear(); - emit tokenRequestFinished(LASTFM_ERROR); - } - else - { - m_token.clear(); - emit tokenRequestFinished(LASTFM_ERROR); - } - } - else if(reply == m_getSessionReply) - { - m_getSessionReply = 0; - m_session.clear(); - if(response.status == "ok") - { - m_session = response.key; - qDebug("LastfmAuth: name: %s", qPrintable(response.name)); - qDebug("LastfmAuth: key: %s", qPrintable(m_session)); - qDebug("LastfmAuth: subscriber: %s", qPrintable(response.subscriber)); - emit sessionRequestFinished(NO_ERROR); - } - else if(error_code == "4" || error_code == "15") //invalid token - { - m_token.clear(); - emit sessionRequestFinished(LASTFM_ERROR); - } - else if(error_code == "11") //service offline - { - m_token.clear(); - emit sessionRequestFinished(LASTFM_ERROR); - } - else if(error_code == "14") // unauthorized token - { - m_token.clear(); - emit sessionRequestFinished(LASTFM_ERROR); - } - else if (error_code.isEmpty()) //network error - { - m_token.clear(); - emit sessionRequestFinished(NETWORK_ERROR); - } - else - { - m_token.clear(); - emit sessionRequestFinished(LASTFM_ERROR); - } - } - else if(reply == m_checkSessionReply) - { - m_checkSessionReply = 0; - if(response.status == "ok") - { - qDebug("LastfmAuth: session ok"); - emit checkSessionFinished(NO_ERROR); - } - else if(error_code.isEmpty()) - { - qWarning("LastfmAuth: network error"); - emit checkSessionFinished(NETWORK_ERROR); - } - else - { - qWarning("LastfmAuth: received last.fm error (code=%s)", qPrintable(error_code)); - emit checkSessionFinished(LASTFM_ERROR); - } - } - reply->deleteLater(); -} diff --git a/src/plugins/General/scrobbler/lastfmscrobbler.h b/src/plugins/General/scrobbler/lastfmscrobbler.h deleted file mode 100644 index c9385670e..000000000 --- a/src/plugins/General/scrobbler/lastfmscrobbler.h +++ /dev/null @@ -1,122 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2010-2013 by Ilya Kotov * - * forkotov02@hotmail.ru * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * - ***************************************************************************/ -#ifndef LASTFMSCROBBLER_H -#define LASTFMSCROBBLER_H - -#include -#include -#include -#include "scrobblercache.h" - -class QNetworkAccessManager; -class QNetworkReply; -class QIODevice; -class QTime; -class SoundCore; - - -/** - @author Ilya Kotov -*/ -class LastfmResponse -{ -public: - QString status; - QString token; - QString code; - QString error; - QString key; - QString name; - QString subscriber; - - void parse(QIODevice *device); -}; - -/** - @author Ilya Kotov -*/ -class LastfmScrobbler : public QObject -{ - Q_OBJECT -public: - LastfmScrobbler(QObject *parent = 0); - ~LastfmScrobbler(); - -private slots: - void setState(Qmmp::State state); - void updateMetaData(); - void processResponse(QNetworkReply *reply); - void setupProxy(); - void submit(); - -private: - enum { MIN_SONG_LENGTH = 30 }; - - void sendNotification(const SongInfo &info); - uint m_start_ts; - SongInfo m_song; - Qmmp::State m_state; - QList m_cachedSongs; - QByteArray m_ua; - int m_submitedSongs; - QString m_session; - QNetworkAccessManager *m_http; - SoundCore *m_core; - QNetworkReply *m_submitReply, *m_notificationReply; - QTime *m_time; - ScrobblerCache *m_cache; -}; - -/** - @author Ilya Kotov -*/ -class LastfmAuth : public QObject -{ - Q_OBJECT -public: - explicit LastfmAuth(QObject *parent = 0); - void getToken(); - void getSession(); - void checkSession(const QString &session); - QString session() const; - - enum ErrorType - { - NO_ERROR = 0, - NETWORK_ERROR, - LASTFM_ERROR - }; - -signals: - void tokenRequestFinished(int error); - void sessionRequestFinished(int error); - void checkSessionFinished(int error); - -private slots: - void processResponse(QNetworkReply *reply); - -private: - QString m_token, m_session; - QByteArray m_ua; - QNetworkAccessManager *m_http; - QNetworkReply *m_getTokenReply, *m_getSessionReply, *m_checkSessionReply; -}; - -#endif diff --git a/src/plugins/General/scrobbler/librefmscrobbler.cpp b/src/plugins/General/scrobbler/librefmscrobbler.cpp deleted file mode 100644 index 97bdf7d69..000000000 --- a/src/plugins/General/scrobbler/librefmscrobbler.cpp +++ /dev/null @@ -1,374 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2008-2013 by Ilya Kotov * - * forkotov02@hotmail.ru * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * - ***************************************************************************/ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "librefmscrobbler.h" - -#define PROTOCOL_VER "1.2.1" -#define CLIENT_ID "qmm" -#define CLIENT_VER "0.7" -#define SCROBBLER_LIBREFM_URL "http://turtle.libre.fm/" - - -LibrefmScrobbler::LibrefmScrobbler(const QString &login, const QString &passw, QObject *parent) - : QObject(parent) -{ - m_failure_count = 0; - m_handshake_count = 0; - m_submitedSongs = 0; - m_handshakeReply = 0; - m_submitReply = 0; - m_notificationReply = 0; - 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_cache = new ScrobblerCache(QDir::homePath() +"/.qmmp/scrobbler_librefm.cache"); - m_http = new QNetworkAccessManager(this); - m_time = new QTime(); - - 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))); - - setupProxy(); - m_cachedSongs = m_cache->load(); - - m_start_ts = QDateTime::currentDateTime().toTime_t(); - handshake(); -} - - -LibrefmScrobbler::~LibrefmScrobbler() -{ - m_cache->save(m_cachedSongs); - delete m_time; - delete m_cache; -} - -void LibrefmScrobbler::setState(Qmmp::State state) -{ - static Qmmp::State previousState = state; - static int elapsed = 0; - switch ((uint) state) - { - case Qmmp::Playing: - if (previousState != Qmmp::Paused) - { - m_start_ts = QDateTime::currentDateTime().toTime_t(); - elapsed = 0; - } - else - { - qDebug("LibrefmScrobbler: resuming from %d seconds played", elapsed / 1000); - } - m_time->restart(); - if (!isReady() && !m_handshakeReply) - handshake(); - break; - case Qmmp::Stopped: - if (previousState != Qmmp::Paused) - elapsed += m_time->elapsed(); - if (!m_song.metaData().isEmpty() - && ((elapsed/1000 > 240) || (elapsed/1000 > int(m_song.length()/2))) - && (m_song.length() > MIN_SONG_LENGTH)) - { - m_song.setTimeStamp(m_start_ts); - m_cachedSongs << m_song; - m_cache->save(m_cachedSongs); - } - m_song.clear(); - if (m_cachedSongs.isEmpty()) - break; - - if (isReady() && !m_submitReply) - submit(); - break; - case Qmmp::Paused: - elapsed += m_time->elapsed(); - qDebug("LibrefmScrobbler: pausing after %d seconds played", elapsed / 1000); - break; - default: - ; - } - previousState = state; -} - -void LibrefmScrobbler::updateMetaData() -{ - QMap metadata = m_core->metaData(); - if (m_core->state() == Qmmp::Playing - && !metadata.value(Qmmp::TITLE).isEmpty() //skip empty tags - && !metadata.value(Qmmp::ARTIST).isEmpty() - && m_core->totalTime() >= 0) //skip stream - { - metadata[Qmmp::ARTIST].replace("%", QUrl::toPercentEncoding("%")); //replace special symbols - metadata[Qmmp::ALBUM].replace("%", QUrl::toPercentEncoding("%")); - metadata[Qmmp::TITLE].replace("%", QUrl::toPercentEncoding("%")); - metadata[Qmmp::ARTIST].replace("&", QUrl::toPercentEncoding("&")); - metadata[Qmmp::ALBUM].replace("&", QUrl::toPercentEncoding("&")); - metadata[Qmmp::TITLE].replace("&", QUrl::toPercentEncoding("&")); - metadata[Qmmp::ARTIST].replace("=", QUrl::toPercentEncoding("=")); - metadata[Qmmp::ALBUM].replace("=", QUrl::toPercentEncoding("=")); - metadata[Qmmp::TITLE].replace("=", QUrl::toPercentEncoding("=")); - m_song = SongInfo(metadata, m_core->totalTime()/1000); - if (isReady() && !m_notificationReply && !m_submitReply) - sendNotification(m_song); - } -} - -void LibrefmScrobbler::processResponse(QNetworkReply *reply) -{ - QString data; - if (reply->error() == QNetworkReply::NoError) - data = reply->readAll(); - else - data = reply->errorString (); - - data = data.trimmed(); - if (data.startsWith("OK")) - { - m_failure_count = 0; - m_handshake_count = 0; - } - - if (reply == m_handshakeReply) - { - m_handshakeReply = 0; - m_submitUrl.clear(); - m_session.clear(); - m_nowPlayingUrl.clear(); - m_failure_count = 0; - QStringList strlist = data.split("\n"); - if (!strlist[0].contains("OK") || strlist.size() < 4) - { - qWarning("LibrefmScrobbler: handshake phase error"); - m_disabled = true; - if(strlist[0].contains("BANNED")) - qWarning("LibrefmScrobbler: client has been banned"); - else if(strlist[0].contains("BADAUTH")) - qWarning("LibrefmScrobbler: incorrect user/password"); - else if(strlist[0].contains("BADTIME")) - qWarning("LibrefmScrobbler: incorrect system time"); - else - { - qWarning("LibrefmScrobbler: service error: %s", qPrintable(strlist[0])); - m_disabled = false; - m_handshake_count++; - QTimer::singleShot (60000 * qMin(m_handshake_count^2, 120) , this, SLOT(handshake())); - qWarning("LibrefmScrobbler: waiting %d minutes...", qMin(m_handshake_count^2, 120)); - } - } - else if (strlist.size() > 3) //process handshake response - { - qDebug("LibrefmScrobbler: reading handshake response"); - qDebug("LibrefmScrobbler: Session ID: %s", qPrintable(strlist[1])); - qDebug("LibrefmScrobbler: Now-Playing URL: %s", qPrintable(strlist[2])); - qDebug("LibrefmScrobbler: Submission URL: %s", qPrintable(strlist[3])); - m_submitUrl = strlist[3]; - m_nowPlayingUrl = strlist[2]; - m_session = strlist[1]; - updateMetaData(); //send now-playing notification for already playing song - if (!m_cachedSongs.isEmpty()) //submit recent songs - submit(); - } - } - else if (reply == m_submitReply) - { - m_submitReply = 0; - if (!data.startsWith("OK")) - { - qWarning("LibrefmScrobbler: submit error"); - if(data.contains("BADSESSION")) - { - qWarning("LibrefmScrobbler: invalid session ID"); - qWarning("LibrefmScrobbler: performing re-handshake"); - handshake(); - } - else - { - qWarning("LibrefmScrobbler: %s", qPrintable(data)); - m_failure_count ++; - } - } - else - { - qDebug("LibrefmScrobbler: submited %d song(s)", m_submitedSongs); - while (m_submitedSongs) - { - m_submitedSongs--; - m_cachedSongs.removeFirst (); - } - if (!m_cachedSongs.isEmpty()) //submit remaining songs - submit(); - else - updateMetaData(); - } - } - else if (reply == m_notificationReply) - { - m_notificationReply = 0; - if (!data.startsWith("OK")) - { - qWarning("LibrefmScrobbler: notification error"); - if(data.contains("BADSESSION")) - { - qWarning("LibrefmScrobbler: invalid session ID"); - qWarning("LibrefmScrobbler: performing re-handshake"); - handshake(); - } - else - { - qWarning("LibrefmScrobbler: %s",qPrintable(data)); - m_failure_count ++; - } - } - else - qDebug("LibrefmScrobbler: Now-Playing notification done"); - } - if(m_failure_count >= 3) - { - qWarning("LibrefmScrobbler: performing re-handshake"); - handshake(); - } - reply->deleteLater(); -} - -void LibrefmScrobbler::setupProxy() -{ - QmmpSettings *gs = QmmpSettings::instance(); - if (gs->isProxyEnabled()) - { - QNetworkProxy proxy(QNetworkProxy::HttpProxy, gs->proxy().host(), gs->proxy().port()); - if(gs->useProxyAuth()) - { - proxy.setUser(gs->proxy().userName()); - proxy.setPassword(gs->proxy().password()); - } - m_http->setProxy(proxy); - } - else - m_http->setProxy(QNetworkProxy::NoProxy); -} - -void LibrefmScrobbler::handshake() -{ - if (m_disabled) - return; - qDebug("LibrefmScrobbler: handshake request"); - uint ts = QDateTime::currentDateTime().toTime_t(); - qDebug("LibrefmScrobbler: current time stamp %d",ts); - QString auth_tmp = QString("%1%2").arg(m_passw).arg(ts); - QByteArray auth = QCryptographicHash::hash(auth_tmp.toAscii (), QCryptographicHash::Md5); - auth = auth.toHex(); - QUrl url(QString(SCROBBLER_LIBREFM_URL) + "?"); - url.addQueryItem("hs", "true"); - url.addQueryItem("p", PROTOCOL_VER); - url.addQueryItem("c", CLIENT_ID); - url.addQueryItem("v", CLIENT_VER); - url.addQueryItem("u", m_login); - url.addQueryItem("t", QString::number(ts)); - url.addQueryItem("a", QString(auth)); - url.setPort(80); - qDebug("LibrefmScrobbler: request url: %s",qPrintable(url.toString())); - QNetworkRequest request(url); - request.setRawHeader("User-Agent", m_ua); - request.setRawHeader("Host",url.host().toAscii()); - request.setRawHeader("Accept", "*/*"); - m_handshakeReply = m_http->get(request); -} - -void LibrefmScrobbler::submit() -{ - qDebug("LibrefmScrobbler: submit request"); - if (m_cachedSongs.isEmpty()) - return; - m_submitedSongs = qMin(m_cachedSongs.size(),25); - QString body = QString("s=%1").arg(m_session); - for (int i = 0; i < m_submitedSongs; ++i) - { - SongInfo info = m_cachedSongs[i]; - body += QString("&a[%9]=%1&t[%9]=%2&i[%9]=%3&o[%9]=%4&r[%9]=%5&l[%9]=%6&b[%9]=%7&n[%9]=%8&m[%9]=") - .arg(info.metaData(Qmmp::ARTIST)) - .arg(info.metaData(Qmmp::TITLE)) - .arg(info.timeStamp()) - .arg("P") - .arg("") - .arg(info.length()) - .arg(info.metaData(Qmmp::ALBUM)) - .arg(info.metaData(Qmmp::TRACK)) - .arg(i); - } - //qDebug("%s",qPrintable(body)); - QUrl url(m_submitUrl); - url.setPort(80); - QNetworkRequest request(url); - request.setRawHeader("User-Agent", m_ua); - request.setRawHeader("Host",url.host().toAscii()); - request.setRawHeader("Accept", "*/*"); - request.setRawHeader("Content-Type", "application/x-www-form-urlencoded"); - request.setHeader(QNetworkRequest::ContentLengthHeader, - QUrl::toPercentEncoding(body,":/[]&=%").size()); - m_submitReply = m_http->post(request, QUrl::toPercentEncoding(body,":/[]&=%")); -} - -void LibrefmScrobbler::sendNotification(const SongInfo &info) -{ - qDebug("LibrefmScrobbler: sending notification"); - QString body = QString("s=%1").arg(m_session); - body += QString("&a=%1&t=%2&b=%3&l=%4&n=%5&m=") - .arg(info.metaData(Qmmp::ARTIST)) - .arg(info.metaData(Qmmp::TITLE)) - .arg(info.metaData(Qmmp::ALBUM)) - .arg(info.length()) - .arg(info.metaData(Qmmp::TRACK)); - QUrl url(m_nowPlayingUrl); - url.setPort(80); - QNetworkRequest request(url); - request.setRawHeader("User-Agent", m_ua); - request.setRawHeader("Host", url.host().toAscii()); - request.setRawHeader("Accept", "*/*"); - request.setRawHeader("Content-Type", "application/x-www-form-urlencoded"); - request.setHeader(QNetworkRequest::ContentLengthHeader, - QUrl::toPercentEncoding(body,":/[]&=%").size()); - m_notificationReply = m_http->post(request, QUrl::toPercentEncoding(body,":/[]&=%")); -} - -bool LibrefmScrobbler::isReady() -{ - return !m_submitUrl.isEmpty() && !m_session.isEmpty(); -} diff --git a/src/plugins/General/scrobbler/librefmscrobbler.h b/src/plugins/General/scrobbler/librefmscrobbler.h deleted file mode 100644 index ece5ecec1..000000000 --- a/src/plugins/General/scrobbler/librefmscrobbler.h +++ /dev/null @@ -1,81 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2008-2013 by Ilya Kotov * - * forkotov02@hotmail.ru * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * - ***************************************************************************/ -#ifndef LIBREFMSCROBBLER_H -#define LIBREFMSCROBBLER_H - -#include -#include -#include -#include "scrobblercache.h" - -class QNetworkAccessManager; -class QNetworkReply; -class QTime; -class SoundCore; - -/** - @author Ilya Kotov -*/ -class LibrefmScrobbler : public QObject -{ - Q_OBJECT -public: - LibrefmScrobbler(const QString &login, const QString &passw, QObject *parent = 0); - - ~LibrefmScrobbler(); - -private slots: - void setState(Qmmp::State state); - void updateMetaData(); - void processResponse(QNetworkReply *reply); - void setupProxy(); - void handshake(); - -private: - enum { MIN_SONG_LENGTH = 30 }; - - void submit(); - void sendNotification(const SongInfo &info); - bool isReady(); - - uint m_start_ts; - SongInfo m_song; - QString m_login; - QString m_passw; - QString m_submitUrl; - QString m_nowPlayingUrl; - QString m_session; - QList m_cachedSongs; - QByteArray m_ua; - int m_submitedSongs; - int m_failure_count; - int m_handshake_count; - bool m_disabled; - QNetworkAccessManager *m_http; - SoundCore *m_core; - QNetworkReply *m_handshakeReply; - QNetworkReply *m_submitReply; - QNetworkReply *m_notificationReply; - QTime* m_time; - ScrobblerCache *m_cache; - -}; - -#endif diff --git a/src/plugins/General/scrobbler/scrobbler.cpp b/src/plugins/General/scrobbler/scrobbler.cpp new file mode 100644 index 000000000..526912593 --- /dev/null +++ b/src/plugins/General/scrobbler/scrobbler.cpp @@ -0,0 +1,602 @@ +/*************************************************************************** + * Copyright (C) 2010-2013 by Ilya Kotov * + * forkotov02@hotmail.ru * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "scrobbler.h" + +#define API_KEY "d71c6f01b2ea562d7042bd5f5970041f" +#define SECRET "32d47bc0010473d40e1d38bdcff20968" + +void ScrobblerResponse::parse(QIODevice *device) +{ + QXmlStreamReader reader(device); + QStringList tags; + while(!reader.atEnd()) + { + reader.readNext(); + if(reader.isStartElement()) + { + tags << reader.name().toString(); + if(tags.last() == "lfm") + status = reader.attributes().value("status").toString(); + else if(tags.last() == "error") + code = reader.attributes().value("code").toString(); + } + else if(reader.isCharacters() && !reader.isWhitespace()) + { + if(tags.last() == "token") + token = reader.text().toString(); + else if(tags.last() == "error") + error = reader.text().toString(); + if(tags.count() >= 2 && tags.at(tags.count() - 2) == "session") + { + if(tags.last() == "key") + key = reader.text().toString(); + else if(tags.last() == "name") + name = reader.text().toString(); + else if(tags.last() == "subscriber") + subscriber = reader.text().toString(); + } + } + else if(reader.isEndElement()) + { + tags.takeLast(); + } + } +} + +Scrobbler::Scrobbler(const QString &scrobblerUrl, const QString &name, QObject *parent) + : QObject(parent) +{ + m_notificationReply = 0; + m_submitedSongs = 0; + m_submitReply = 0; + m_scrobblerUrl = scrobblerUrl; + m_name = name; + m_state = Qmmp::Stopped; + m_time = new QTime(); + m_cache = new ScrobblerCache(QDir::homePath() +"/.qmmp/scrobbler_"+name+".cache"); + 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/"+name+"_session").toString(); + + 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))); + + setupProxy(); + m_cachedSongs = m_cache->load(); + + if(!m_session.isEmpty()) + { + submit(); + if(m_core->state() == Qmmp::Playing) + { + setState(Qmmp::Playing); + updateMetaData(); + } + } +} + +Scrobbler::~Scrobbler() +{ + m_cache->save(m_cachedSongs); + delete m_time; + delete m_cache; + +} + +void Scrobbler::setState(Qmmp::State state) +{ + static Qmmp::State previousState = state; + static int elapsed = 0; + m_state = state; + switch ((uint) state) + { + case Qmmp::Playing: + if (previousState != Qmmp::Paused) + { + qDebug("Scrobbler[%s]: new song started", qPrintable(m_name)); + m_start_ts = QDateTime::currentDateTime().toTime_t(); + elapsed = 0; + } + else + { + qDebug("Scrobbler[%s]: resuming from %d seconds played", qPrintable(m_name), elapsed / 1000); + } + m_time->restart(); + break; + case Qmmp::Stopped: + if (previousState != Qmmp::Paused) + elapsed += m_time->elapsed(); + if (!m_song.metaData().isEmpty() + && ((elapsed / 1000 > 240) || (elapsed / 1000 > int(m_song.length()/2))) + && (m_song.length() > MIN_SONG_LENGTH)) + { + m_song.setTimeStamp(m_start_ts); + m_cachedSongs << m_song; + m_cache->save(m_cachedSongs); + } + + m_song.clear(); + if (m_cachedSongs.isEmpty()) + break; + + if (!m_session.isEmpty() && !m_submitReply) + submit(); + break; + case Qmmp::Paused: + elapsed += m_time->elapsed(); + qDebug("Scrobbler[%s]: pausing after %d seconds played", qPrintable(m_name), elapsed / 1000); + break; + default: + ; + } + previousState = state; +} + +void Scrobbler::updateMetaData() +{ + QMap metadata = m_core->metaData(); + 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 Scrobbler::processResponse(QNetworkReply *reply) +{ + if (reply->error() != QNetworkReply::NoError) + { + qWarning("Scrobbler[%s]: http error: %s", qPrintable(m_name), qPrintable(reply->errorString())); + } + + ScrobblerResponse response; + response.parse(reply); + + QString error_code; + if(response.status != "ok" && !response.status.isEmpty()) + { + if(!response.error.isEmpty()) + { + qWarning("Scrobbler[%s]: status=%s, %s-%s", qPrintable(m_name), qPrintable(response.status), + qPrintable(response.code), qPrintable(response.error)); + error_code = response.code; + } + else + qWarning("Scrobbler[%s]: invalid content", qPrintable(m_name)); + } + + if (reply == m_submitReply) + { + m_submitReply = 0; + if (response.status == "ok") + { + qDebug("Scrobbler[%s]: submited %d song(s)", qPrintable(m_name), m_submitedSongs); + while (m_submitedSongs) + { + m_submitedSongs--; + m_cachedSongs.removeFirst (); + } + if (!m_cachedSongs.isEmpty()) //submit remaining songs + { + submit(); + } + else + { + m_cache->save(m_cachedSongs); // update the cache file to reflect the empty cache + updateMetaData(); + } + } + else if(error_code == "9") //invalid session key + { + m_session.clear(); + qWarning("Scrobbler[%s]: invalid session key, scrobbling disabled", qPrintable(m_name)); + } + else if(error_code == "11" || error_code == "16" || error_code.isEmpty()) //unavailable + { + QTimer::singleShot(120000, this, SLOT(submit())); + } + else + { + m_session.clear(); + qWarning("Scrobbler[%s]: service returned unrecoverable error, scrobbling disabled", + qPrintable(m_name)); + } + } + else if (reply == m_notificationReply) + { + m_notificationReply = 0; + if(response.status == "ok") + { + qDebug("Scrobbler[%s]: Now-Playing notification done", qPrintable(m_name)); + } + else if(error_code == "9") //invalid session key + { + m_session.clear(); + qWarning("Scrobbler[%s]: invalid session key, scrobbling disabled", qPrintable(m_name)); + } + } + reply->deleteLater(); +} + +void Scrobbler::setupProxy() +{ + QmmpSettings *gs = QmmpSettings::instance(); + if (gs->isProxyEnabled()) + { + QNetworkProxy proxy(QNetworkProxy::HttpProxy, gs->proxy().host(), gs->proxy().port()); + if(gs->useProxyAuth()) + { + proxy.setUser(gs->proxy().userName()); + proxy.setPassword(gs->proxy().password()); + } + m_http->setProxy(proxy); + } + else + m_http->setProxy(QNetworkProxy::NoProxy); +} + +void Scrobbler::submit() +{ + if (m_cachedSongs.isEmpty() || m_session.isEmpty() || m_submitReply) + return; + + qDebug("Scrobbler[%s]: submit request", qPrintable(m_name)); + m_submitedSongs = qMin(m_cachedSongs.size(),25); + + QMap params; + for (int i = 0; i < m_submitedSongs; ++i) + { + SongInfo info = m_cachedSongs[i]; + params.insert(QString("track[%1]").arg(i),info.metaData(Qmmp::TITLE)); + params.insert(QString("timestamp[%1]").arg(i),QString("%1").arg(info.timeStamp())); + params.insert(QString("artist[%1]").arg(i),info.metaData(Qmmp::ARTIST)); + params.insert(QString("album[%1]").arg(i),info.metaData(Qmmp::ALBUM)); + params.insert(QString("trackNumber[%1]").arg(i),info.metaData(Qmmp::TRACK)); + params.insert(QString("duration[%1]").arg(i),QString("%1").arg(info.length())); + } + params.insert("api_key", API_KEY); + params.insert("method", "track.scrobble"); + params.insert("sk", m_session); + + QStringList keys = params.keys(); + foreach (QString key, keys) //removes empty keys + { + if(params.value(key).isEmpty() || params.value(key) == "0") + params.remove(key); + } + + QUrl url(m_scrobblerUrl); + url.setPort(80); + + QUrl body(""); + QByteArray data; + foreach (QString key, params.keys()) + { + body.addQueryItem(key, params.value(key)); + data.append(key.toUtf8() + params.value(key).toUtf8()); + } + data.append(SECRET); + body.addQueryItem("api_sig", QCryptographicHash::hash(data, QCryptographicHash::Md5).toHex()); + QByteArray bodyData = body.toEncoded().remove(0,1); + bodyData.replace("+", QUrl::toPercentEncoding("+")); + + QNetworkRequest request(url); + request.setRawHeader("User-Agent", m_ua); + request.setRawHeader("Host", url.host().toAscii()); + request.setRawHeader("Accept", "*/*"); + request.setRawHeader("Content-Type", "application/x-www-form-urlencoded"); + request.setHeader(QNetworkRequest::ContentLengthHeader, bodyData.size()); + m_submitReply = m_http->post(request, bodyData); +} + +void Scrobbler::sendNotification(const SongInfo &info) +{ + if(m_session.isEmpty()) + return; + qDebug("Scrobbler[%s]: sending notification", qPrintable(m_name)); + + QMap params; + params.insert("track", info.metaData(Qmmp::TITLE)); + params.insert("artist", info.metaData(Qmmp::ARTIST)); + if(!info.metaData(Qmmp::ALBUM).isEmpty()) + params.insert("album", info.metaData(Qmmp::ALBUM)); + if(!info.metaData(Qmmp::TRACK).isEmpty()) + params.insert("trackNumber", info.metaData(Qmmp::TRACK)); + params.insert("duration", QString("%1").arg(info.length())); + params.insert("api_key", API_KEY); + params.insert("method", "track.updateNowPlaying"); + params.insert("sk", m_session); + + foreach (QString key, params) //removes empty keys + { + if(params.value(key).isEmpty()) + params.remove(key); + } + + QUrl url(m_scrobblerUrl); + url.setPort(80); + + QUrl body(""); + QByteArray data; + foreach (QString key, params.keys()) + { + body.addQueryItem(key, params.value(key)); + data.append(key.toUtf8() + params.value(key).toUtf8()); + } + data.append(SECRET); + body.addQueryItem("api_sig", QCryptographicHash::hash(data, QCryptographicHash::Md5).toHex()); + QByteArray bodyData = body.toEncoded().remove(0,1); + bodyData.replace("+", QUrl::toPercentEncoding("+")); + + QNetworkRequest request(url); + request.setRawHeader("User-Agent", m_ua); + request.setRawHeader("Host", url.host().toAscii()); + request.setRawHeader("Accept", "*/*"); + request.setRawHeader("Content-Type", "application/x-www-form-urlencoded"); + request.setHeader(QNetworkRequest::ContentLengthHeader, bodyData.size()); + m_notificationReply = m_http->post(request, bodyData); +} + +ScrobblerAuth::ScrobblerAuth(const QString &scrobblerUrl, const QString &authUrl, + const QString &name, QObject *parent) : QObject(parent) +{ + m_getTokenReply = 0; + m_getSessionReply = 0; + m_scrobblerUrl = scrobblerUrl; + m_authUrl = authUrl; + m_name = name; + m_ua = QString("qmmp-plugins/%1").arg(Qmmp::strVersion().toLower()).toAscii(); + m_http = new QNetworkAccessManager(this); + connect(m_http, SIGNAL(finished (QNetworkReply *)), SLOT(processResponse(QNetworkReply *))); + + QmmpSettings *gs = QmmpSettings::instance(); + if (gs->isProxyEnabled()) + { + QNetworkProxy proxy(QNetworkProxy::HttpProxy, gs->proxy().host(), gs->proxy().port()); + if(gs->useProxyAuth()) + { + proxy.setUser(gs->proxy().userName()); + proxy.setPassword(gs->proxy().password()); + } + m_http->setProxy(proxy); + } + else + m_http->setProxy(QNetworkProxy::NoProxy); +} + +void ScrobblerAuth::getToken() +{ + qDebug("ScrobblerAuth[%s]: new token request", qPrintable(m_name)); + m_session.clear(); + QUrl url(m_scrobblerUrl + "?"); + url.setPort(80); + url.addQueryItem("method", "auth.getToken"); + url.addQueryItem("api_key", API_KEY); + + QByteArray data; + data.append("api_key"API_KEY); + data.append("methodauth.getToken"); + data.append(SECRET); + url.addQueryItem("api_sig", QCryptographicHash::hash(data,QCryptographicHash::Md5).toHex()); + + QNetworkRequest request(url); + request.setRawHeader("User-Agent", m_ua); + request.setRawHeader("Host",url.host().toAscii()); + request.setRawHeader("Accept", "*/*"); + m_getTokenReply = m_http->get(request); +} + +void ScrobblerAuth::getSession() +{ + qDebug("ScrobblerAuth[%s]: new session request", qPrintable(m_name)); + QUrl url(m_scrobblerUrl + "?"); + url.setPort(80); + url.addQueryItem("api_key", API_KEY); + url.addQueryItem("method", "auth.getSession"); + url.addQueryItem("token", m_token); + + QByteArray data; + data.append("api_key"API_KEY); + data.append("methodauth.getSession"); + data.append("token" + m_token.toUtf8()); + data.append(SECRET); + url.addQueryItem("api_sig", QCryptographicHash::hash(data, QCryptographicHash::Md5).toHex()); + + QNetworkRequest request(url); + request.setRawHeader("User-Agent", m_ua); + request.setRawHeader("Host",url.host().toAscii()); + request.setRawHeader("Accept", "*/*"); + m_getSessionReply = m_http->get(request); +} + +void ScrobblerAuth::checkSession(const QString &session) +{ + qDebug("ScrobblerAuth[%s]: checking session...", qPrintable(m_name)); + m_session = session; + QMap params; + params.insert("api_key", API_KEY); + params.insert("sk", session); + params.insert("method", "user.getRecommendedArtists"); + params.insert("limit", "1"); + + QUrl url(m_scrobblerUrl); + url.setPort(80); + + QUrl body(""); + QByteArray data; + foreach (QString key, params.keys()) + { + body.addQueryItem(key, params.value(key)); + data.append(key.toUtf8() + params.value(key).toUtf8()); + } + data.append(SECRET); + body.addQueryItem("api_sig", QCryptographicHash::hash(data, QCryptographicHash::Md5).toHex()); + QByteArray bodyData = body.toEncoded().remove(0,1); + bodyData.replace("+", QUrl::toPercentEncoding("+")); + + QNetworkRequest request(url); + request.setRawHeader("User-Agent", m_ua); + request.setRawHeader("Host", url.host().toAscii()); + request.setRawHeader("Accept", "*/*"); + request.setRawHeader("Content-Type", "application/x-www-form-urlencoded"); + request.setHeader(QNetworkRequest::ContentLengthHeader, bodyData.size()); + m_checkSessionReply = m_http->post(request, bodyData); +} + +QString ScrobblerAuth::session() const +{ + return m_session; +} + +void ScrobblerAuth::processResponse(QNetworkReply *reply) +{ + if (reply->error() != QNetworkReply::NoError) + { + qWarning("ScrobblerAuth[%s]: http error: %s", qPrintable(m_name), qPrintable(reply->errorString())); + } + + ScrobblerResponse response; + response.parse(reply); + + QString error_code; + if(response.status != "ok" && !response.status.isEmpty()) + { + if(!response.error.isEmpty()) + { + qWarning("ScrobblerAuth[%s]: status=%s, %s-%s", qPrintable(m_name), qPrintable(response.status), + qPrintable(response.code), qPrintable(response.error)); + error_code = response.code; + } + else + qWarning("ScrobblerAuth[%s]: invalid content", qPrintable(m_name)); + } + + if (reply == m_getTokenReply) + { + m_getTokenReply = 0; + if(response.status == "ok") + { + m_token = response.token; + qDebug("ScrobblerAuth[%s]: token: %s", qPrintable(m_name), qPrintable(m_token)); + QDesktopServices::openUrl(m_authUrl + "?api_key="API_KEY"&token="+m_token); + emit(tokenRequestFinished(NO_ERROR)); + } + else if(error_code.isEmpty()) + { + m_token.clear(); + emit tokenRequestFinished(NETWORK_ERROR); + } + else if(error_code == "8" || error_code == "7" || error_code == "11" || error_code.isEmpty()) + { + m_token.clear(); + emit tokenRequestFinished(LASTFM_ERROR); + } + else + { + m_token.clear(); + emit tokenRequestFinished(LASTFM_ERROR); + } + } + else if(reply == m_getSessionReply) + { + m_getSessionReply = 0; + m_session.clear(); + if(response.status == "ok") + { + m_session = response.key; + qDebug("ScrobblerAuth[%s]: name: %s", qPrintable(m_name), qPrintable(response.name)); + qDebug("ScrobblerAuth[%s]: key: %s", qPrintable(m_name), qPrintable(m_session)); + qDebug("ScrobblerAuth[%s]: subscriber: %s", qPrintable(m_name), qPrintable(response.subscriber)); + emit sessionRequestFinished(NO_ERROR); + } + else if(error_code == "4" || error_code == "15") //invalid token + { + m_token.clear(); + emit sessionRequestFinished(LASTFM_ERROR); + } + else if(error_code == "11") //service offline + { + m_token.clear(); + emit sessionRequestFinished(LASTFM_ERROR); + } + else if(error_code == "14") // unauthorized token + { + m_token.clear(); + emit sessionRequestFinished(LASTFM_ERROR); + } + else if (error_code.isEmpty()) //network error + { + m_token.clear(); + emit sessionRequestFinished(NETWORK_ERROR); + } + else + { + m_token.clear(); + emit sessionRequestFinished(LASTFM_ERROR); + } + } + else if(reply == m_checkSessionReply) + { + m_checkSessionReply = 0; + if(response.status == "ok") + { + qDebug("ScrobblerAuth[%s]: session ok", qPrintable(m_name)); + emit checkSessionFinished(NO_ERROR); + } + else if(error_code.isEmpty()) + { + qWarning("ScrobblerAuth[%s]: network error", qPrintable(m_name)); + emit checkSessionFinished(NETWORK_ERROR); + } + else + { + qWarning("ScrobblerAuth[%s]: received last.fm error (code=%s)", + qPrintable(m_name), qPrintable(error_code)); + emit checkSessionFinished(LASTFM_ERROR); + } + } + reply->deleteLater(); +} diff --git a/src/plugins/General/scrobbler/scrobbler.h b/src/plugins/General/scrobbler/scrobbler.h new file mode 100644 index 000000000..db0e086ad --- /dev/null +++ b/src/plugins/General/scrobbler/scrobbler.h @@ -0,0 +1,125 @@ +/*************************************************************************** + * Copyright (C) 2010-2013 by Ilya Kotov * + * forkotov02@hotmail.ru * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef SCROBBLER_H +#define SCROBBLER_H + +#include +#include +#include +#include "scrobblercache.h" + +class QNetworkAccessManager; +class QNetworkReply; +class QIODevice; +class QTime; +class SoundCore; + + +/** + @author Ilya Kotov +*/ +class ScrobblerResponse +{ +public: + QString status; + QString token; + QString code; + QString error; + QString key; + QString name; + QString subscriber; + + void parse(QIODevice *device); +}; + +/** + @author Ilya Kotov +*/ +class Scrobbler : public QObject +{ + Q_OBJECT +public: + Scrobbler(const QString &scrobblerUrl, const QString &name,QObject *parent = 0); + ~Scrobbler(); + +private slots: + void setState(Qmmp::State state); + void updateMetaData(); + void processResponse(QNetworkReply *reply); + void setupProxy(); + void submit(); + +private: + enum { MIN_SONG_LENGTH = 30 }; + + void sendNotification(const SongInfo &info); + uint m_start_ts; + SongInfo m_song; + Qmmp::State m_state; + QList m_cachedSongs; + QByteArray m_ua; + int m_submitedSongs; + QString m_session; + QNetworkAccessManager *m_http; + SoundCore *m_core; + QNetworkReply *m_submitReply, *m_notificationReply; + QTime *m_time; + ScrobblerCache *m_cache; + QString m_scrobblerUrl, m_name; +}; + +/** + @author Ilya Kotov +*/ +class ScrobblerAuth : public QObject +{ + Q_OBJECT +public: + explicit ScrobblerAuth(const QString &scrobblerUrl, const QString &authUrl, + const QString &name, QObject *parent = 0); + void getToken(); + void getSession(); + void checkSession(const QString &session); + QString session() const; + + enum ErrorType + { + NO_ERROR = 0, + NETWORK_ERROR, + LASTFM_ERROR + }; + +signals: + void tokenRequestFinished(int error); + void sessionRequestFinished(int error); + void checkSessionFinished(int error); + +private slots: + void processResponse(QNetworkReply *reply); + +private: + QString m_token, m_session; + QByteArray m_ua; + QNetworkAccessManager *m_http; + QNetworkReply *m_getTokenReply, *m_getSessionReply, *m_checkSessionReply; + QString m_scrobblerUrl, m_authUrl, m_name; +}; + +#endif //SCROBBLER_H diff --git a/src/plugins/General/scrobbler/scrobbler.pro b/src/plugins/General/scrobbler/scrobbler.pro index f40b90333..a73954b06 100644 --- a/src/plugins/General/scrobbler/scrobbler.pro +++ b/src/plugins/General/scrobbler/scrobbler.pro @@ -29,16 +29,15 @@ unix { HEADERS += scrobblerfactory.h \ settingsdialog.h \ scrobblerhandler.h \ - lastfmscrobbler.h \ - librefmscrobbler.h \ - scrobblercache.h + scrobblercache.h \ + scrobbler.h \ + defines.h win32:HEADERS += ../../../../src/qmmpui/general.h SOURCES += scrobblerfactory.cpp \ settingsdialog.cpp \ scrobblerhandler.cpp \ - lastfmscrobbler.cpp \ - librefmscrobbler.cpp \ - scrobblercache.cpp + scrobblercache.cpp \ + scrobbler.cpp QT += network INCLUDEPATH += ../../../ unix:LIBS += -lqmmpui \ diff --git a/src/plugins/General/scrobbler/scrobblerhandler.cpp b/src/plugins/General/scrobbler/scrobblerhandler.cpp index 17f375f01..176f8f26e 100644 --- a/src/plugins/General/scrobbler/scrobblerhandler.cpp +++ b/src/plugins/General/scrobbler/scrobblerhandler.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2009-2012 by Ilya Kotov * + * Copyright (C) 2009-2013 by Ilya Kotov * * forkotov02@hotmail.ru * * * * This program is free software; you can redistribute it and/or modify * @@ -19,8 +19,8 @@ ***************************************************************************/ #include -#include "lastfmscrobbler.h" -#include "librefmscrobbler.h" +#include "scrobbler.h" +#include "defines.h" #include "scrobblerhandler.h" ScrobblerHandler::ScrobblerHandler(QObject *parent) : QObject(parent) @@ -28,15 +28,9 @@ ScrobblerHandler::ScrobblerHandler(QObject *parent) : QObject(parent) QSettings settings(Qmmp::configFile(), QSettings::IniFormat); settings.beginGroup("Scrobbler"); if(settings.value("use_lastfm", false).toBool()) - { - new LastfmScrobbler(this); - } + new Scrobbler(SCROBBLER_LASTFM_URL, "lastfm", this); if(settings.value("use_librefm", false).toBool()) - { - new LibrefmScrobbler(settings.value("librefm_login").toString(), - settings.value("librefm_password").toString(), this); - - } + new Scrobbler(SCROBBLER_LIBREFM_URL, "librefm", this); settings.endGroup(); } diff --git a/src/plugins/General/scrobbler/settingsdialog.cpp b/src/plugins/General/scrobbler/settingsdialog.cpp index dad403c5f..6e3db8e50 100644 --- a/src/plugins/General/scrobbler/settingsdialog.cpp +++ b/src/plugins/General/scrobbler/settingsdialog.cpp @@ -21,23 +21,28 @@ #include #include #include -#include "lastfmscrobbler.h" +#include "scrobbler.h" +#include "defines.h" #include "settingsdialog.h" SettingsDialog::SettingsDialog(QWidget *parent) : QDialog(parent) { m_ui.setupUi(this); - m_lastfmAuth = new LastfmAuth(this); + m_lastfmAuth = new ScrobblerAuth(SCROBBLER_LASTFM_URL, LASTFM_AUTH_URL, "lastfm", this); + m_librefmAuth = new ScrobblerAuth(SCROBBLER_LIBREFM_URL, LIBREFM_AUTH_URL, "librefm", this); connect(m_lastfmAuth, SIGNAL(tokenRequestFinished(int)), SLOT(processTokenResponse(int))); connect(m_lastfmAuth, SIGNAL(sessionRequestFinished(int)), SLOT(processSessionResponse(int))); connect(m_lastfmAuth, SIGNAL(checkSessionFinished(int)), SLOT(processCheckResponse(int))); + connect(m_librefmAuth, SIGNAL(tokenRequestFinished(int)), SLOT(processTokenResponse(int))); + connect(m_librefmAuth, SIGNAL(sessionRequestFinished(int)), SLOT(processSessionResponse(int))); + connect(m_librefmAuth, SIGNAL(checkSessionFinished(int)), SLOT(processCheckResponse(int))); + QSettings settings(Qmmp::configFile(), QSettings::IniFormat); settings.beginGroup("Scrobbler"); m_ui.lastfmGroupBox->setChecked(settings.value("use_lastfm", false).toBool()); - m_ui.sessionLineEdit_lastfm->setText(settings.value("lastfm_session").toString()); m_ui.librefmGroupBox->setChecked(settings.value("use_librefm", false).toBool()); - m_ui.userLineEdit_libre->setText(settings.value("librefm_login").toString()); - m_ui.passwordLineEdit_libre->setText(settings.value("librefm_password").toString()); + m_ui.sessionLineEdit_lastfm->setText(settings.value("lastfm_session").toString()); + m_ui.sessionLineEdit_librefm->setText(settings.value("librefm_session").toString()); settings.endGroup(); } @@ -49,10 +54,9 @@ void SettingsDialog::accept() QSettings settings(Qmmp::configFile(), QSettings::IniFormat); settings.beginGroup("Scrobbler"); settings.setValue("use_lastfm", m_ui.lastfmGroupBox->isChecked()); - settings.setValue("lastfm_session",m_ui.sessionLineEdit_lastfm->text()); settings.setValue("use_librefm", m_ui.librefmGroupBox->isChecked()); - settings.setValue("librefm_login",m_ui.userLineEdit_libre->text()); - settings.setValue("librefm_password", m_ui.passwordLineEdit_libre->text()); + settings.setValue("lastfm_session",m_ui.sessionLineEdit_lastfm->text()); + settings.setValue("librefm_session",m_ui.sessionLineEdit_librefm->text()); settings.endGroup(); QDialog::accept(); } @@ -63,24 +67,42 @@ void SettingsDialog::on_newSessionButton_lastfm_clicked() m_lastfmAuth->getToken(); } +void SettingsDialog::on_newSessionButton_librefm_clicked() +{ + m_ui.newSessionButton_librefm->setEnabled(false); + m_librefmAuth->getToken(); +} + void SettingsDialog::processTokenResponse(int error) { - m_ui.newSessionButton_lastfm->setEnabled(true); + if(sender() == m_lastfmAuth) + m_ui.newSessionButton_lastfm->setEnabled(true); + else if(sender() == m_librefmAuth) + m_ui.newSessionButton_librefm->setEnabled(true); switch(error) { - case LastfmAuth::NO_ERROR: + case ScrobblerAuth::NO_ERROR: QMessageBox::information(this, tr("Message"), tr("1. Wait for browser startup") + "\n" + - tr("2. Allow Qmmp to scrobble tracks to your Last.fm account") + "\n" + + tr("2. Allow Qmmp to scrobble tracks to your %1 account") + .arg((sender() == m_lastfmAuth) ? "Last.fm" : "Libre.fm") + "\n" + tr("3. Press \"OK\"")); - m_ui.newSessionButton_lastfm->setEnabled(false); - m_lastfmAuth->getSession(); + if(sender() == m_lastfmAuth) + { + m_ui.newSessionButton_lastfm->setEnabled(false); + m_lastfmAuth->getSession(); + } + else if(sender() == m_librefmAuth) + { + m_ui.newSessionButton_librefm->setEnabled(false); + m_librefmAuth->getSession(); + } break; - case LastfmAuth::NETWORK_ERROR: + case ScrobblerAuth::NETWORK_ERROR: QMessageBox::warning(this, tr("Error"), tr("Network error")); break; - case LastfmAuth::LASTFM_ERROR: + case ScrobblerAuth::LASTFM_ERROR: default: QMessageBox::warning(this, tr("Error"), tr("Unable to register new session")); } @@ -88,22 +110,32 @@ void SettingsDialog::processTokenResponse(int error) void SettingsDialog::processSessionResponse(int error) { - m_ui.newSessionButton_lastfm->setEnabled(true); + if(sender() == m_lastfmAuth) + m_ui.newSessionButton_lastfm->setEnabled(true); + else if(sender() == m_librefmAuth) + m_ui.newSessionButton_librefm->setEnabled(true); switch(error) { - case LastfmAuth::NO_ERROR: + case ScrobblerAuth::NO_ERROR: { QMessageBox::information(this, tr("Message"), tr("New session has been received successfully")); - m_ui.sessionLineEdit_lastfm->setText(m_lastfmAuth->session()); - QSettings settings(Qmmp::configFile(), QSettings::IniFormat); - settings.setValue("Scrobbler/lastfm_session",m_ui.sessionLineEdit_lastfm->text()); + if(sender() == m_lastfmAuth) + { + m_ui.sessionLineEdit_lastfm->setText(m_lastfmAuth->session()); + settings.setValue("Scrobbler/lastfm_session",m_ui.sessionLineEdit_lastfm->text()); + } + else if(sender() == m_librefmAuth) + { + m_ui.sessionLineEdit_librefm->setText(m_librefmAuth->session()); + settings.setValue("Scrobbler/librefm_session",m_ui.sessionLineEdit_librefm->text()); + } break; } - case LastfmAuth::NETWORK_ERROR: + case ScrobblerAuth::NETWORK_ERROR: QMessageBox::warning(this, tr("Error"), tr("Network error")); break; - case LastfmAuth::LASTFM_ERROR: + case ScrobblerAuth::LASTFM_ERROR: default: QMessageBox::warning(this, tr("Error"), tr("Unable to register new session")); } @@ -118,21 +150,36 @@ void SettingsDialog::on_checkButton_lastfm_clicked() } } +void SettingsDialog::on_checkButton_librefm_clicked() +{ + if(!m_ui.sessionLineEdit_librefm->text().isEmpty()) + { + m_ui.checkButton_librefm->setEnabled(false); + m_librefmAuth->checkSession(m_ui.sessionLineEdit_librefm->text()); + } +} + void SettingsDialog::processCheckResponse(int error) { - m_ui.checkButton_lastfm->setEnabled(true); + if(sender() == m_lastfmAuth) + m_ui.checkButton_lastfm->setEnabled(true); + else if(sender() == m_librefmAuth) + m_ui.checkButton_librefm->setEnabled(true); switch(error) { - case LastfmAuth::NO_ERROR: + case ScrobblerAuth::NO_ERROR: { QMessageBox::information(this, tr("Message"), tr("Permission granted")); - m_ui.sessionLineEdit_lastfm->setText(m_lastfmAuth->session()); + if(sender() == m_lastfmAuth) + m_ui.sessionLineEdit_lastfm->setText(m_lastfmAuth->session()); + else if(sender() == m_librefmAuth) + m_ui.sessionLineEdit_librefm->setText(m_librefmAuth->session()); break; } - case LastfmAuth::NETWORK_ERROR: + case ScrobblerAuth::NETWORK_ERROR: QMessageBox::warning(this, tr("Error"), tr("Network error")); break; - case LastfmAuth::LASTFM_ERROR: + case ScrobblerAuth::LASTFM_ERROR: default: QMessageBox::warning(this, tr("Error"), tr("Permission denied")); } diff --git a/src/plugins/General/scrobbler/settingsdialog.h b/src/plugins/General/scrobbler/settingsdialog.h index 41f81c73a..999efaaf0 100644 --- a/src/plugins/General/scrobbler/settingsdialog.h +++ b/src/plugins/General/scrobbler/settingsdialog.h @@ -23,7 +23,7 @@ #include #include "ui_settingsdialog.h" -class LastfmAuth; +class ScrobblerAuth; /** @author Ilya Kotov @@ -41,14 +41,17 @@ public slots: private slots: void on_newSessionButton_lastfm_clicked(); + void on_newSessionButton_librefm_clicked(); void processTokenResponse(int error); void processSessionResponse(int error); void on_checkButton_lastfm_clicked(); + void on_checkButton_librefm_clicked(); void processCheckResponse(int error); private: Ui::SettingsDialog m_ui; - LastfmAuth *m_lastfmAuth; + ScrobblerAuth *m_lastfmAuth; + ScrobblerAuth *m_librefmAuth; }; diff --git a/src/plugins/General/scrobbler/settingsdialog.ui b/src/plugins/General/scrobbler/settingsdialog.ui index dd111a948..a4798f07b 100644 --- a/src/plugins/General/scrobbler/settingsdialog.ui +++ b/src/plugins/General/scrobbler/settingsdialog.ui @@ -6,8 +6,8 @@ 0 0 - 337 - 227 + 376 + 255 @@ -17,9 +17,6 @@ 6 - - 9 - 6 @@ -88,56 +85,59 @@ true - - - - User name: - - - - - - - - - Password: + + + Qt::Horizontal - + + + 189 + 20 + + + - - - QLineEdit::Password + + + Register new session + + + + + + Session: + + + + + + + + + + false + + + Check + + + + + - - - - - Qt::Horizontal - - - - 136 - 18 - - - - - - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - - - + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + -- cgit v1.2.3-13-gbd6f