From 139e148912d820707db649c6fef32c9c1cc87a5b Mon Sep 17 00:00:00 2001 From: Jonas Kvinge Date: Sun, 10 May 2020 14:59:04 +0200 Subject: [PATCH] Use shared_ptr for scrobbler cache items --- src/scrobbler/listenbrainzscrobbler.cpp | 2 +- src/scrobbler/scrobblercache.cpp | 45 +++++++++++++++---------- src/scrobbler/scrobblercache.h | 19 ++++++----- src/scrobbler/scrobblercacheitem.cpp | 2 -- src/scrobbler/scrobblercacheitem.h | 9 ++++- src/scrobbler/scrobblingapi20.cpp | 6 ++-- src/scrobbler/scrobblingapi20.h | 4 +-- 7 files changed, 53 insertions(+), 34 deletions(-) diff --git a/src/scrobbler/listenbrainzscrobbler.cpp b/src/scrobbler/listenbrainzscrobbler.cpp index 35124e547..8f3bb46de 100644 --- a/src/scrobbler/listenbrainzscrobbler.cpp +++ b/src/scrobbler/listenbrainzscrobbler.cpp @@ -481,7 +481,7 @@ void ListenBrainzScrobbler::Submit() { QJsonArray array; int i(0); QList list; - for (ScrobblerCacheItem *item : cache_->List()) { + for (ScrobblerCacheItemPtr item : cache_->List()) { if (item->sent_) continue; item->sent_ = true; ++i; diff --git a/src/scrobbler/scrobblercache.cpp b/src/scrobbler/scrobblercache.cpp index fc2a2fe87..5a1c611bf 100644 --- a/src/scrobbler/scrobblercache.cpp +++ b/src/scrobbler/scrobblercache.cpp @@ -19,6 +19,8 @@ #include "config.h" +#include "memory" + #include #include #include @@ -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(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 ::iterator i; + QHash > ::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 ×tamp) { +ScrobblerCacheItemPtr ScrobblerCache::Add(const Song &song, const quint64 ×tamp) { if (scrobbler_cache_.contains(timestamp)) return nullptr; @@ -187,7 +196,7 @@ ScrobblerCacheItem *ScrobblerCache::Add(const Song &song, const quint64 ×ta 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(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 ×ta } -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 list) { - for (quint64 timestamp : list) { +void ScrobblerCache::ClearSent(const QList &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 list) { +void ScrobblerCache::Flush(const QList &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())); diff --git a/src/scrobbler/scrobblercache.h b/src/scrobbler/scrobblercache.h index 4ce54413e..3a9b790fe 100644 --- a/src/scrobbler/scrobblercache.h +++ b/src/scrobbler/scrobblercache.h @@ -22,14 +22,17 @@ #include "config.h" +#include + #include #include #include #include #include +#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 ×tamp); - ScrobblerCacheItem *Get(const quint64 hash); + ScrobblerCacheItemPtr Add(const Song &song, const quint64 ×tamp); + 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 List() const { return scrobbler_cache_.values(); } - void ClearSent(const QList list); - void Flush(const QList list); + QList List() const { return scrobbler_cache_.values(); } + void ClearSent(const QList &list); + void Flush(const QList &list); public slots: void WriteCache(); @@ -55,7 +58,7 @@ class ScrobblerCache : public QObject { private: QString filename_; bool loaded_; - QHash scrobbler_cache_; + QHash scrobbler_cache_; }; diff --git a/src/scrobbler/scrobblercacheitem.cpp b/src/scrobbler/scrobblercacheitem.cpp index bbf0cb07e..7039cca14 100644 --- a/src/scrobbler/scrobblercacheitem.cpp +++ b/src/scrobbler/scrobblercacheitem.cpp @@ -33,5 +33,3 @@ ScrobblerCacheItem::ScrobblerCacheItem(const QString &artist, const QString &alb duration_(duration), timestamp_(timestamp), sent_(false) {} - -ScrobblerCacheItem::~ScrobblerCacheItem() {} diff --git a/src/scrobbler/scrobblercacheitem.h b/src/scrobbler/scrobblercacheitem.h index bff62d765..783e822e9 100644 --- a/src/scrobbler/scrobblercacheitem.h +++ b/src/scrobbler/scrobblercacheitem.h @@ -22,6 +22,8 @@ #include "config.h" +#include + #include #include #include @@ -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 ×tamp); - ~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 ScrobblerCacheItemPtr; +typedef QList ScrobblerCacheItemList; + +Q_DECLARE_METATYPE(ScrobblerCacheItemPtr) +Q_DECLARE_METATYPE(ScrobblerCacheItemList) + #endif // SCROBBLERCACHEITEM_H diff --git a/src/scrobbler/scrobblingapi20.cpp b/src/scrobbler/scrobblingapi20.cpp index aefebab09..c86516432 100644 --- a/src/scrobbler/scrobblingapi20.cpp +++ b/src/scrobbler/scrobblingapi20.cpp @@ -542,7 +542,7 @@ void ScrobblingAPI20::Submit() { int i(0); QList 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, QListdeleteLater(); - 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; diff --git a/src/scrobbler/scrobblingapi20.h b/src/scrobbler/scrobblingapi20.h index 97924b2cb..3950ff991 100644 --- a/src/scrobbler/scrobblingapi20.h +++ b/src/scrobbler/scrobblingapi20.h @@ -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();