1
0
mirror of https://github.com/strawberrymusicplayer/strawberry synced 2024-12-17 02:59:03 +01:00

Use shared_ptr for scrobbler cache items

This commit is contained in:
Jonas Kvinge 2020-05-10 14:59:04 +02:00
parent 1b8dedb4ed
commit 139e148912
7 changed files with 53 additions and 34 deletions

View File

@ -481,7 +481,7 @@ void ListenBrainzScrobbler::Submit() {
QJsonArray array;
int i(0);
QList<quint64> list;
for (ScrobblerCacheItem *item : cache_->List()) {
for (ScrobblerCacheItemPtr item : cache_->List()) {
if (item->sent_) continue;
item->sent_ = true;
++i;

View File

@ -19,6 +19,8 @@
#include "config.h"
#include "memory"
#include <QObject>
#include <QStandardPaths>
#include <QHash>
@ -48,7 +50,9 @@ ScrobblerCache::ScrobblerCache(const QString &filename, QObject *parent) :
loaded_ = true;
}
ScrobblerCache::~ScrobblerCache() {}
ScrobblerCache::~ScrobblerCache() {
scrobbler_cache_.clear();
}
void ScrobblerCache::ReadCache() {
@ -130,8 +134,7 @@ void ScrobblerCache::ReadCache() {
continue;
}
if (scrobbler_cache_.contains(timestamp)) continue;
ScrobblerCacheItem *item = new ScrobblerCacheItem(artist, album, song, albumartist, track, duration, timestamp);
scrobbler_cache_.insert(timestamp, item);
scrobbler_cache_.insert(timestamp, std::make_shared<ScrobblerCacheItem>(artist, album, song, albumartist, track, duration, timestamp));
}
@ -143,11 +146,17 @@ void ScrobblerCache::WriteCache() {
qLog(Debug) << "Writing scrobbler cache file" << filename_;
if (scrobbler_cache_.isEmpty()) {
QFile file(filename_);
if (file.exists()) file.remove();
return;
}
QJsonArray array;
QHash <quint64, ScrobblerCacheItem*> ::iterator i;
QHash <quint64, std::shared_ptr<ScrobblerCacheItem>> ::iterator i;
for (i = scrobbler_cache_.begin() ; i != scrobbler_cache_.end() ; ++i) {
ScrobblerCacheItem *item = i.value();
ScrobblerCacheItemPtr item = i.value();
QJsonObject object;
object.insert("timestamp", QJsonValue::fromVariant(item->timestamp_));
object.insert("artist", QJsonValue::fromVariant(item->artist_));
@ -176,7 +185,7 @@ void ScrobblerCache::WriteCache() {
}
ScrobblerCacheItem *ScrobblerCache::Add(const Song &song, const quint64 &timestamp) {
ScrobblerCacheItemPtr ScrobblerCache::Add(const Song &song, const quint64 &timestamp) {
if (scrobbler_cache_.contains(timestamp)) return nullptr;
@ -187,7 +196,7 @@ ScrobblerCacheItem *ScrobblerCache::Add(const Song &song, const quint64 &timesta
album.remove(Song::kAlbumRemoveMisc);
title.remove(Song::kTitleRemoveMisc);
ScrobblerCacheItem *item = new ScrobblerCacheItem(song.artist(), album, title, song.albumartist(), song.track(), song.length_nanosec(), timestamp);
ScrobblerCacheItemPtr item = std::make_shared<ScrobblerCacheItem>(song.artist(), album, title, song.albumartist(), song.track(), song.length_nanosec(), timestamp);
scrobbler_cache_.insert(timestamp, item);
if (loaded_) DoInAMinuteOrSo(this, SLOT(WriteCache()));
@ -196,7 +205,7 @@ ScrobblerCacheItem *ScrobblerCache::Add(const Song &song, const quint64 &timesta
}
ScrobblerCacheItem *ScrobblerCache::Get(const quint64 hash) {
ScrobblerCacheItemPtr ScrobblerCache::Get(const quint64 hash) {
if (scrobbler_cache_.contains(hash)) { return scrobbler_cache_.value(hash); }
else return nullptr;
@ -210,27 +219,29 @@ void ScrobblerCache::Remove(const quint64 hash) {
return;
}
delete scrobbler_cache_.take(hash);
scrobbler_cache_.remove(hash);
}
void ScrobblerCache::Remove(ScrobblerCacheItem &item) {
delete scrobbler_cache_.take(item.timestamp_);
void ScrobblerCache::Remove(ScrobblerCacheItemPtr item) {
scrobbler_cache_.remove(item->timestamp_);
}
void ScrobblerCache::ClearSent(const QList<quint64> list) {
for (quint64 timestamp : list) {
void ScrobblerCache::ClearSent(const QList<quint64> &list) {
for (const quint64 timestamp : list) {
if (!scrobbler_cache_.contains(timestamp)) continue;
ScrobblerCacheItem *item = scrobbler_cache_.take(timestamp);
ScrobblerCacheItemPtr item = scrobbler_cache_.take(timestamp);
item->sent_ = false;
}
}
void ScrobblerCache::Flush(const QList<quint64> list) {
void ScrobblerCache::Flush(const QList<quint64> &list) {
for (quint64 timestamp : list) {
for (const quint64 timestamp : list) {
if (!scrobbler_cache_.contains(timestamp)) continue;
delete scrobbler_cache_.take(timestamp);
scrobbler_cache_.remove(timestamp);
}
DoInAMinuteOrSo(this, SLOT(WriteCache()));

View File

@ -22,14 +22,17 @@
#include "config.h"
#include <memory>
#include <QtGlobal>
#include <QObject>
#include <QList>
#include <QHash>
#include <QString>
#include "scrobblercacheitem.h"
class Song;
class ScrobblerCacheItem;
class ScrobblerCache : public QObject {
Q_OBJECT
@ -40,14 +43,14 @@ class ScrobblerCache : public QObject {
void ReadCache();
ScrobblerCacheItem *Add(const Song &song, const quint64 &timestamp);
ScrobblerCacheItem *Get(const quint64 hash);
ScrobblerCacheItemPtr Add(const Song &song, const quint64 &timestamp);
ScrobblerCacheItemPtr Get(const quint64 hash);
void Remove(const quint64 hash);
void Remove(ScrobblerCacheItem &item);
void Remove(ScrobblerCacheItemPtr item);
int Count() const { return scrobbler_cache_.size(); };
QList<ScrobblerCacheItem*> List() const { return scrobbler_cache_.values(); }
void ClearSent(const QList<quint64> list);
void Flush(const QList<quint64> list);
QList<ScrobblerCacheItemPtr> List() const { return scrobbler_cache_.values(); }
void ClearSent(const QList<quint64> &list);
void Flush(const QList<quint64> &list);
public slots:
void WriteCache();
@ -55,7 +58,7 @@ class ScrobblerCache : public QObject {
private:
QString filename_;
bool loaded_;
QHash <quint64, ScrobblerCacheItem*> scrobbler_cache_;
QHash<quint64, ScrobblerCacheItemPtr> scrobbler_cache_;
};

View File

@ -33,5 +33,3 @@ ScrobblerCacheItem::ScrobblerCacheItem(const QString &artist, const QString &alb
duration_(duration),
timestamp_(timestamp),
sent_(false) {}
ScrobblerCacheItem::~ScrobblerCacheItem() {}

View File

@ -22,6 +22,8 @@
#include "config.h"
#include <memory>
#include <QtGlobal>
#include <QObject>
#include <QString>
@ -33,7 +35,6 @@ class ScrobblerCacheItem : public QObject {
public:
explicit ScrobblerCacheItem(const QString &artist, const QString &album, const QString &song, const QString &albumartist, const int track, const qint64 duration, const quint64 &timestamp);
~ScrobblerCacheItem();
QString effective_albumartist() const { return albumartist_.isEmpty() || albumartist_.toLower() == Song::kVariousArtists ? artist_ : albumartist_; }
@ -49,4 +50,10 @@ class ScrobblerCacheItem : public QObject {
};
typedef std::shared_ptr<ScrobblerCacheItem> ScrobblerCacheItemPtr;
typedef QList<ScrobblerCacheItemPtr> ScrobblerCacheItemList;
Q_DECLARE_METATYPE(ScrobblerCacheItemPtr)
Q_DECLARE_METATYPE(ScrobblerCacheItemList)
#endif // SCROBBLERCACHEITEM_H

View File

@ -542,7 +542,7 @@ void ScrobblingAPI20::Submit() {
int i(0);
QList<quint64> list;
for (ScrobblerCacheItem *item : cache()->List()) {
for (ScrobblerCacheItemPtr item : cache()->List()) {
if (item->sent_) continue;
item->sent_ = true;
if (!batch_) {
@ -727,7 +727,7 @@ void ScrobblingAPI20::ScrobbleRequestFinished(QNetworkReply *reply, QList<quint6
}
void ScrobblingAPI20::SendSingleScrobble(ScrobblerCacheItem *item) {
void ScrobblingAPI20::SendSingleScrobble(ScrobblerCacheItemPtr item) {
ParamList params = ParamList()
<< Param("method", "track.scrobble")
@ -752,7 +752,7 @@ void ScrobblingAPI20::SingleScrobbleRequestFinished(QNetworkReply *reply, quint6
reply->deleteLater();
ScrobblerCacheItem *item = cache()->Get(timestamp);
ScrobblerCacheItemPtr item = cache()->Get(timestamp);
if (!item) {
Error(QString("Received reply for non-existing cache entry %1.").arg(timestamp));
return;

View File

@ -32,13 +32,13 @@
#include "core/song.h"
#include "scrobblerservice.h"
#include "scrobblercache.h"
#include "scrobblercacheitem.h"
class QNetworkReply;
class Application;
class NetworkAccessManager;
class LocalRedirectServer;
class ScrobblerCacheItem;
class ScrobblingAPI20 : public ScrobblerService {
Q_OBJECT
@ -129,7 +129,7 @@ class ScrobblingAPI20 : public ScrobblerService {
void RequestSession(const QString &token);
void AuthError(const QString &error);
void SendSingleScrobble(ScrobblerCacheItem *item);
void SendSingleScrobble(ScrobblerCacheItemPtr item);
void Error(const QString &error, const QVariant &debug = QVariant());
QString ErrorString(const ScrobbleErrorCode error) const;
void DoSubmit();