Use liblastfm1

This commit is contained in:
Harald Sitter 2012-06-25 12:30:53 +02:00 committed by David Sansome
parent 4c55ed9e94
commit 769baa5c16
10 changed files with 79 additions and 126 deletions

View File

@ -26,7 +26,7 @@
#ifdef HAVE_LIBLASTFM
#include "internet/fixlastfm.h"
#include <lastfm/Track>
#include <lastfm/Track.h>
#endif
#include <QFile>

View File

@ -19,8 +19,8 @@
#include "coverprovider.h"
#include "lastfmcoverprovider.h"
#include <lastfm/Artist>
#include <lastfm/XmlQuery>
#include <lastfm/Artist.h>
#include <lastfm/XmlQuery.h>
#include <lastfm/ws.h>
#include <QNetworkReply>
@ -52,14 +52,8 @@ void LastFmCoverProvider::QueryFinished() {
CoverSearchResults results;
try {
lastfm::XmlQuery query(lastfm::ws::parse(reply));
#ifdef Q_OS_WIN32
if (lastfm::ws::last_parse_error != lastfm::ws::NoError) {
throw std::runtime_error("");
}
#endif
lastfm::XmlQuery query;
if (query.parse(reply->readAll())) {
// parse the list of search results
QList<lastfm::XmlQuery> elements = query["results"]["albummatches"].children("album");
@ -69,7 +63,7 @@ void LastFmCoverProvider::QueryFinished() {
result.image_url = element["image size=extralarge"].text();
results << result;
}
} catch(std::runtime_error&) {
} else {
// Drop through and emit an empty list of results.
}

View File

@ -49,20 +49,18 @@
#include <boost/scoped_ptr.hpp>
#include <lastfm/Audioscrobbler>
#include <lastfm/Audioscrobbler.h>
#include <lastfm/misc.h>
#include <lastfm/RadioStation>
#include <lastfm/Scrobble>
#include <lastfm/ScrobbleCache>
#include <lastfm/ScrobblePoint>
#include <lastfm/RadioStation.h>
#include <lastfm/ScrobbleCache.h>
#include <lastfm/ScrobblePoint.h>
#include <lastfm/ws.h>
#include <lastfm/XmlQuery>
#include <lastfm/XmlQuery.h>
#include <QMenu>
#include <QSettings>
using boost::scoped_ptr;
using lastfm::Scrobble;
using lastfm::XmlQuery;
uint qHash(const lastfm::Track& track) {
@ -318,13 +316,8 @@ void LastFMService::AuthenticateReplyFinished() {
reply->deleteLater();
// Parse the reply
try {
lastfm::XmlQuery const lfm = lastfm::ws::parse(reply);
#ifdef Q_OS_WIN32
if (lastfm::ws::last_parse_error != lastfm::ws::NoError)
throw std::runtime_error("");
#endif
lastfm::XmlQuery lfm;
if (lfm.parse(reply->readAll())) {
lastfm::ws::Username = lfm["session"]["name"].text();
lastfm::ws::SessionKey = lfm["session"]["key"].text();
QString subscribed = lfm["session"]["subscriber"].text();
@ -336,8 +329,8 @@ void LastFMService::AuthenticateReplyFinished() {
settings.setValue("Username", lastfm::ws::Username);
settings.setValue("Session", lastfm::ws::SessionKey);
settings.setValue("Subscriber", is_subscriber);
} catch (std::runtime_error& e) {
qLog(Error) << e.what();
} else {
qLog(Error) << lfm.parseError().message();
emit AuthenticationComplete(false);
return;
}
@ -365,13 +358,8 @@ void LastFMService::UpdateSubscriberStatusFinished() {
bool is_subscriber = false;
try {
const lastfm::XmlQuery lfm = lastfm::ws::parse(reply);
#ifdef Q_OS_WIN32
if (lastfm::ws::last_parse_error != lastfm::ws::NoError)
throw std::runtime_error("");
#endif
lastfm::XmlQuery lfm;
if (lfm.parse(reply->readAll())) {
connection_problems_ = false;
QString subscriber = lfm["user"]["subscriber"].text();
is_subscriber = (subscriber.toInt() == 1);
@ -380,11 +368,9 @@ void LastFMService::UpdateSubscriberStatusFinished() {
settings.beginGroup(kSettingsGroup);
settings.setValue("Subscriber", is_subscriber);
qLog(Info) << lastfm::ws::Username << "Subscriber status:" << is_subscriber;
} catch (lastfm::ws::ParseError e) {
qLog(Error) << "Last.fm parse error: " << e.enumValue();
connection_problems_ = e.enumValue() == lastfm::ws::MalformedResponse;
} catch (std::runtime_error& e) {
qLog(Error) << e.what();
} else {
qLog(Error) << "Last.fm parse error: " << lfm.parseError().message();
connection_problems_ = lfm.parseError().enumValue() == lastfm::ws::MalformedResponse;
}
emit UpdatedSubscriberStatus(is_subscriber);
@ -487,7 +473,8 @@ bool LastFMService::InitScrobbler() {
scrobbler_ = new lastfm::Audioscrobbler(kAudioscrobblerClientId);
//reemit the signal since the sender is private
connect(scrobbler_, SIGNAL(status(int)), SIGNAL(ScrobblerStatus(int)));
connect(scrobbler_, SIGNAL(scrobblesSubmitted(QList<lastfm::Track>)), SIGNAL(ScrobbleSubmitted()));
connect(scrobbler_, SIGNAL(nowPlayingError(int,QString)), SIGNAL(ScrobbleError(int)));
return true;
}
@ -515,7 +502,7 @@ void LastFMService::NowPlaying(const Song &song) {
if (!last_track_.isNull() &&
last_track_.source() == lastfm::Track::NonPersonalisedBroadcast) {
const int duration_secs = last_track_.timestamp().secsTo(QDateTime::currentDateTime());
if (duration_secs >= ScrobblePoint::kScrobbleMinLength) {
if (duration_secs >= lastfm::ScrobblePoint::scrobbleTimeMin()) {
lastfm::MutableTrack mtrack(last_track_);
mtrack.setDuration(duration_secs);
@ -532,14 +519,16 @@ void LastFMService::NowPlaying(const Song &song) {
already_scrobbled_ = false;
last_track_ = mtrack;
//check immediately if the song is valid
Scrobble::Invalidity invalidity;
if (!lastfm::Scrobble(last_track_).isValid( &invalidity )) {
//for now just notify this, we can also see the cause
emit ScrobblerStatus(-1);
return;
}
// TODO: validity was removed from liblastfm1 but might reappear, it should have
// no impact as we get a different error when actually trying to scrobble.
// //check immediately if the song is valid
// Scrobble::Invalidity invalidity;
//
// if (!lastfm::Scrobble(last_track_).isValid( &invalidity )) {
// //for now just notify this, we can also see the cause
// emit ScrobbleError(-1);
// return;
// }
scrobbler_->nowPlaying(mtrack);
}
@ -548,12 +537,12 @@ void LastFMService::Scrobble() {
if (!InitScrobbler())
return;
ScrobbleCache cache(lastfm::ws::Username);
lastfm::ScrobbleCache cache(lastfm::ws::Username);
qLog(Debug) << "There are" << cache.tracks().count() << "tracks in the last.fm cache.";
scrobbler_->cache(last_track_);
// Let's mark a track as cached, useful when the connection is down
emit ScrobblerStatus(30);
emit ScrobbleError(30);
scrobbler_->submit();
already_scrobbled_ = true;
@ -657,7 +646,7 @@ void LastFMService::RefreshFriends(bool force) {
return;
}
lastfm::AuthenticatedUser user;
lastfm::User user;
QNetworkReply* reply = user.getFriends();
connect(reply, SIGNAL(finished()), SLOT(RefreshFriendsFinished()));
}
@ -666,7 +655,7 @@ void LastFMService::RefreshNeighbours() {
if (!neighbours_list_ || !IsAuthenticated())
return;
lastfm::AuthenticatedUser user;
lastfm::User user;
QNetworkReply* reply = user.getNeighbours();
connect(reply, SIGNAL(finished()), SLOT(RefreshNeighboursFinished()));
}
@ -678,14 +667,11 @@ void LastFMService::RefreshFriendsFinished() {
QList<lastfm::User> friends;
try {
friends = lastfm::User::list(reply);
#ifdef Q_OS_WIN32
if (lastfm::ws::last_parse_error != lastfm::ws::NoError)
throw std::runtime_error("");
#endif
} catch (std::runtime_error& e) {
qLog(Error) << e.what();
lastfm::XmlQuery lfm;
if (lfm.parse(reply->readAll())) {
friends = lastfm::UserList(lfm).users();
} else {
qLog(Error) << lfm.parseError().message();
return;
}
@ -725,14 +711,11 @@ void LastFMService::RefreshNeighboursFinished() {
QList<lastfm::User> neighbours;
try {
neighbours = lastfm::User::list(reply);
#ifdef Q_OS_WIN32
if (lastfm::ws::last_parse_error != lastfm::ws::NoError)
throw std::runtime_error("");
#endif
} catch (std::runtime_error& e) {
qLog(Error) << e.what();
lastfm::XmlQuery lfm;
if (lfm.parse(reply->readAll())) {
neighbours = lastfm::UserList(lfm).users();
} else {
qLog(Error) << lfm.parseError().message();
return;
}
@ -883,13 +866,8 @@ void LastFMService::FetchMoreTracksFinished() {
app_->task_manager()->SetTaskFinished(tune_task_id_);
tune_task_id_ = 0;
try {
const XmlQuery& query = lastfm::ws::parse(reply);
#ifdef Q_OS_WIN32
if (lastfm::ws::last_parse_error != lastfm::ws::NoError)
throw std::runtime_error("");
#endif
XmlQuery query;
if (query.parse(reply->readAll())) {
const XmlQuery& playlist = query["playlist"];
foreach (const XmlQuery& q, playlist["trackList"].children("track")) {
lastfm::MutableTrack t;
@ -904,17 +882,9 @@ void LastFMService::FetchMoreTracksFinished() {
art_urls_[t] = q["image"].text();
playlist_ << t;
}
} catch (std::runtime_error& e) {
// For some reason a catch block that takes a lastfm::ws::ParseError&
// doesn't get called, even when a lastfm::ws::ParseError is thrown...
// Hacks like this remind me of Java...
if (QString(typeid(e).name()).contains("ParseError")) {
// dynamic_cast throws a std::bad_cast ... *boggle*
} else {
emit StreamError(tr("Couldn't load the last.fm radio station")
.arg(e.what()));
} else {
emit StreamError(tr("An unknown last.fm error occurred: %1").arg(e.what()));
}
.arg(query.parseError().message()));
return;
}
@ -927,7 +897,7 @@ void LastFMService::Tune(const QUrl& url) {
last_url_ = url;
initial_tune_ = true;
const lastfm::RadioStation station(FixupUrl(url));
const lastfm::RadioStation station(FixupUrl(url).toString());
playlist_.clear();

View File

@ -27,7 +27,8 @@ class Track;
uint qHash(const lastfm::Track& track);
#include "fixlastfm.h"
#include <lastfm/Track>
#include <lastfm/Audioscrobbler.h>
#include <lastfm/Track.h>
#include <lastfm/ws.h>
#include "internetmodel.h"
@ -134,7 +135,8 @@ class LastFMService : public InternetService {
void ButtonVisibilityChanged(bool value);
void ScrobbleButtonVisibilityChanged(bool value);
void PreferAlbumArtistChanged(bool value);
void ScrobblerStatus(int value);
void ScrobbleSubmitted();
void ScrobbleError(int value);
void UpdatedSubscriberStatus(bool is_subscriber);
void ScrobbledRadioStream();

View File

@ -22,7 +22,7 @@
#include "ui/iconloader.h"
#include <lastfm/ws.h>
#include <lastfm/XmlQuery>
#include <lastfm/XmlQuery.h>
void LastfmTrackInfoProvider::FetchInfo(int id, const Song& metadata) {
QMap<QString, QString> params;
@ -50,18 +50,12 @@ void LastfmTrackInfoProvider::RequestFinished() {
return;
}
try {
lastfm::XmlQuery query = lastfm::ws::parse(reply);
#ifdef Q_OS_WIN32
if (lastfm::ws::last_parse_error != lastfm::ws::NoError)
throw std::runtime_error("");
#endif
lastfm::XmlQuery query;
if (query.parse(reply->readAll())) {
GetPlayCounts(id, query);
GetWiki(id, query);
GetTags(id, query);
} catch (std::runtime_error&) {
}
emit Finished(id);
}

View File

@ -1,6 +1,6 @@
#include "lastfmsuggester.h"
#include <lastfm/XmlQuery>
#include <lastfm/XmlQuery.h>
#include "core/logging.h"
#include "core/timeconstants.h"
@ -39,13 +39,8 @@ void LastFMSuggester::RequestFinished() {
int id = it.value();
replies_.erase(it);
try {
lastfm::XmlQuery const lfm = lastfm::ws::parse(reply);
#ifdef Q_OS_WIN32
if (lastfm::ws::last_parse_error != lastfm::ws::NoError)
throw std::runtime_error("");
#endif
lastfm::XmlQuery lfm;
if (lfm.parse(reply->readAll())) {
const QList<XmlQuery> tracks = lfm["similartracks"].children("track");
SongList songs;
foreach (const XmlQuery& q, tracks) {
@ -59,8 +54,8 @@ void LastFMSuggester::RequestFinished() {
}
qLog(Debug) << songs.length() << "suggested songs from Last.fm";
emit SuggestSongsFinished(id, songs);
} catch (std::runtime_error& e) {
qLog(Error) << e.what();
} else {
qLog(Error) << lfm.parseError().message();
emit SuggestSongsFinished(id, SongList());
}
}

View File

@ -668,7 +668,8 @@ MainWindow::MainWindow(Application* app,
connect(app_->playlist_manager()->sequence(), SIGNAL(ShuffleModeChanged(PlaylistSequence::ShuffleMode)), osd_, SLOT(ShuffleModeChanged(PlaylistSequence::ShuffleMode)));
#ifdef HAVE_LIBLASTFM
connect(InternetModel::Service<LastFMService>(), SIGNAL(ScrobblerStatus(int)), SLOT(ScrobblerStatus(int)));
connect(InternetModel::Service<LastFMService>(), SIGNAL(ScrobbleSubmitted()), SLOT(ScrobbleSubmitted()));
connect(InternetModel::Service<LastFMService>(), SIGNAL(ScrobbleError(int)), SLOT(ScrobbleError(int)));
LastFMButtonVisibilityChanged(app_->internet_model()->InternetModel::Service<LastFMService>()->AreButtonsVisible());
ScrobbleButtonVisibilityChanged(app_->internet_model()->InternetModel::Service<LastFMService>()->IsScrobbleButtonVisible());
@ -2175,30 +2176,26 @@ void MainWindow::SetToggleScrobblingIcon(bool value) {
}
#ifdef HAVE_LIBLASTFM
void MainWindow::ScrobblerStatus(int value) {
void MainWindow::ScrobbleSubmitted() {
const LastFMService* lastfm_service = InternetModel::Service<LastFMService>();
const bool last_fm_enabled = ui_->action_toggle_scrobbling->isVisible() &&
lastfm_service->IsScrobblingEnabled() &&
lastfm_service->IsAuthenticated();
app_->playlist_manager()->active()->set_lastfm_status(Playlist::LastFM_Scrobbled);
// update the button icon
if (last_fm_enabled)
ui_->action_toggle_scrobbling->setIcon(QIcon(":/last.fm/as.png"));
}
void MainWindow::ScrobbleError(int value) {
switch (value) {
case -1:
// custom error value got from initial validity check
app_->playlist_manager()->active()->set_lastfm_status(Playlist::LastFM_Invalid);
break;
case 2:
case 3:
// we should get 3 for a correct scrobbling, but I just get 2 for
// mysterious reasons
// seems to scrobble fine though, so for now we accept it as correct
app_->playlist_manager()->active()->set_lastfm_status(Playlist::LastFM_Scrobbled);
// update the button icon
if (last_fm_enabled)
ui_->action_toggle_scrobbling->setIcon(QIcon(":/last.fm/as.png"));
break;
case 30:
// Hack: when offline, liblastfm doesn't inform us, so set the status
// as queued; in this way we won't try to scrobble again, it will be done automatically

View File

@ -222,7 +222,8 @@ class MainWindow : public QMainWindow, public PlatformInterface {
void ShowCoverManager();
#ifdef HAVE_LIBLASTFM
void ScrobblerStatus(int value);
void ScrobbleSubmitted();
void ScrobbleError(int value);
#endif
void ShowAboutDialog();
void ShowTranscodeDialog();

View File

@ -22,7 +22,7 @@
#include "playlist/playlistsequence.h"
#ifdef HAVE_LIBLASTFM
#include "internet/fixlastfm.h"
#include <lastfm/Track>
#include <lastfm/Track.h>
#endif
#include "gmock/gmock.h"

View File

@ -20,7 +20,7 @@
#include "core/song.h"
#ifdef HAVE_LIBLASTFM
#include "internet/fixlastfm.h"
#include <lastfm/Track>
#include <lastfm/Track.h>
#endif
#include "gmock/gmock.h"