1
0
mirror of https://github.com/clementine-player/Clementine synced 2025-02-01 11:56:45 +01:00

Remember the user's list of last.fm friends, refresh it every day.

This commit is contained in:
David Sansome 2011-09-24 13:48:14 +01:00
parent 5d59c19bbe
commit 62165a6feb
2 changed files with 66 additions and 12 deletions

View File

@ -67,6 +67,8 @@ const char* LastFMService::kTitleArtist = QT_TR_NOOP("Last.fm Similar Artists to
const char* LastFMService::kTitleTag = QT_TR_NOOP("Last.fm Tag Radio: %1");
const char* LastFMService::kTitleCustom = QT_TR_NOOP("Last.fm Custom Radio: %1");
const int LastFMService::kFriendsCacheDurationSecs = 60 * 60 * 24; // 1 day
LastFMService::LastFMService(InternetModel* parent)
: InternetService(kServiceName, parent, parent),
url_handler_(new LastFMUrlHandler(this, this)),
@ -97,6 +99,8 @@ LastFMService::LastFMService(InternetModel* parent)
QIcon(":last.fm/icon_tag.png"), tr("Play tag radio..."), this, SLOT(AddTagRadio()));
add_custom_action_ = context_menu_->addAction(
QIcon(":last.fm/icon_radio.png"), tr("Play custom radio..."), this, SLOT(AddCustomRadio()));
refresh_friends_action_ = context_menu_->addAction(
IconLoader::Load("view-refresh"), tr("Refresh friends list"), this, SLOT(ForceRefreshFriends()));
context_menu_->addAction(
IconLoader::Load("configure"), tr("Configure Last.fm..."), this, SLOT(ShowConfig()));
@ -122,6 +126,9 @@ void LastFMService::ReloadSettings() {
buttons_visible_ = settings.value("ShowLoveBanButtons", true).toBool();
scrobble_button_visible_ = settings.value("ShowScrobbleButton", true).toBool();
friend_names_ = settings.value("FriendNames").toStringList();
last_refreshed_friends_ = settings.value("LastRefreshedFriends").toDateTime();
//avoid emitting signal if it's not changed
if(scrobbling_enabled_old != scrobbling_enabled_)
emit ScrobblingEnabledChanged(scrobbling_enabled_);
@ -210,7 +217,7 @@ void LastFMService::LazyPopulate(QStandardItem* parent) {
break;
case Type_Friends:
RefreshFriends();
RefreshFriends(false);
break;
case Type_Neighbours:
@ -269,10 +276,15 @@ void LastFMService::SignOut() {
lastfm::ws::Username.clear();
lastfm::ws::SessionKey.clear();
friend_names_ = QStringList();
last_refreshed_friends_ = QDateTime();
QSettings settings;
settings.beginGroup(kSettingsGroup);
settings.setValue("Username", QString());
settings.setValue("Session", QString());
settings.setValue("FriendNames", friend_names_);
settings.setValue("LastRefreshedFriends", last_refreshed_friends_);
}
void LastFMService::AuthenticateReplyFinished() {
@ -571,12 +583,24 @@ void LastFMService::ShowContextMenu(const QModelIndex& index, const QPoint &glob
context_menu_->popup(global_pos);
}
void LastFMService::RefreshFriends() {
bool LastFMService::IsFriendsListStale() const {
return last_refreshed_friends_.isNull() ||
last_refreshed_friends_.secsTo(QDateTime::currentDateTime()) >
kFriendsCacheDurationSecs;
}
void LastFMService::ForceRefreshFriends() {
RefreshFriends(true);
}
void LastFMService::RefreshFriends(bool force) {
if (!friends_list_ || !IsAuthenticated())
return;
if (friends_list_->hasChildren())
friends_list_->removeRows(0, friends_list_->rowCount());
if (!force && !IsFriendsListStale()) {
PopulateFriendsList();
return;
}
lastfm::AuthenticatedUser user;
QNetworkReply* reply = user.getFriends();
@ -587,9 +611,6 @@ void LastFMService::RefreshNeighbours() {
if (!neighbours_list_ || !IsAuthenticated())
return;
if (neighbours_list_->hasChildren())
neighbours_list_->removeRows(0, neighbours_list_->rowCount());
lastfm::AuthenticatedUser user;
QNetworkReply* reply = user.getNeighbours();
connect(reply, SIGNAL(finished()), SLOT(RefreshNeighboursFinished()));
@ -613,12 +634,30 @@ void LastFMService::RefreshFriendsFinished() {
return;
}
last_refreshed_friends_ = QDateTime::currentDateTime();
friend_names_ = QStringList();
foreach (const lastfm::User& f, friends) {
Song song;
song.set_url(QUrl("lastfm://user/" + f.name() + "/library"));
song.set_title(tr("Last.fm Library - %1").arg(f.name()));
friend_names_ << f.name();
}
QStandardItem* item = new QStandardItem(QIcon(":last.fm/icon_user.png"), f.name());
QSettings s;
s.beginGroup(kSettingsGroup);
s.setValue("FriendNames", friend_names_);
s.setValue("LastRefreshedFriends", last_refreshed_friends_);
PopulateFriendsList();
}
void LastFMService::PopulateFriendsList() {
if (friends_list_->hasChildren())
friends_list_->removeRows(0, friends_list_->rowCount());
foreach (const QString& name, friend_names_) {
Song song;
song.set_url(QUrl("lastfm://user/" + name + "/library"));
song.set_title(tr("Last.fm Library - %1").arg(name));
QStandardItem* item = new QStandardItem(QIcon(":last.fm/icon_user.png"), name);
item->setData(QVariant::fromValue(song), InternetModel::Role_SongMetadata);
item->setData(true, InternetModel::Role_CanLazyLoad);
item->setData(Type_OtherUser, InternetModel::Role_Type);
@ -645,6 +684,9 @@ void LastFMService::RefreshNeighboursFinished() {
return;
}
if (neighbours_list_->hasChildren())
neighbours_list_->removeRows(0, neighbours_list_->rowCount());
foreach (const lastfm::User& n, neighbours) {
Song song;
song.set_url(QUrl("lastfm://user/" + n.name() + "/library"));

View File

@ -36,6 +36,7 @@ uint qHash(const lastfm::Track& track);
#include "core/song.h"
#include "playlist/playlistitem.h"
#include <QDateTime>
#include <QMap>
#include <QMenu>
#include <QQueue>
@ -69,6 +70,8 @@ class LastFMService : public InternetService {
static const char* kTitleTag;
static const char* kTitleCustom;
static const int kFriendsCacheDurationSecs;
enum ItemType {
Type_Root = InternetModel::TypeCount,
Type_Artists,
@ -108,6 +111,8 @@ class LastFMService : public InternetService {
PlaylistItemPtr PlaylistItemForUrl(const QUrl& url);
bool IsFriendsListStale() const;
public slots:
void NowPlaying(const Song& song);
void Scrobble();
@ -140,6 +145,7 @@ class LastFMService : public InternetService {
void AddArtistRadio();
void AddTagRadio();
void AddCustomRadio();
void ForceRefreshFriends();
void Remove();
// Radio tuner.
@ -155,7 +161,7 @@ class LastFMService : public InternetService {
QString ErrorString(lastfm::ws::Error error) const;
bool InitScrobbler();
lastfm::Track TrackFromSong(const Song& song) const;
void RefreshFriends();
void RefreshFriends(bool force);
void RefreshNeighbours();
void AddArtistOrTag(const QString& name,
LastFMStationDialog::Type dialog_type,
@ -171,6 +177,8 @@ class LastFMService : public InternetService {
static QUrl FixupUrl(const QUrl& url);
void Tune(const QUrl& station);
void PopulateFriendsList();
void AddSelectedToPlaylist(bool clear_first);
private:
@ -189,6 +197,7 @@ class LastFMService : public InternetService {
QAction* add_artist_action_;
QAction* add_tag_action_;
QAction* add_custom_action_;
QAction* refresh_friends_action_;
QStandardItem* context_item_;
QUrl last_url_;
@ -207,6 +216,9 @@ class LastFMService : public InternetService {
QHash<lastfm::Track, QString> art_urls_;
QStringList friend_names_;
QDateTime last_refreshed_friends_;
// Useful to inform the user that we can't scrobble right now
bool connection_problems_;
};