Use shared_ptr for scrobbler cache items
This commit is contained in:
@@ -481,7 +481,7 @@ void ListenBrainzScrobbler::Submit() {
|
|||||||
QJsonArray array;
|
QJsonArray array;
|
||||||
int i(0);
|
int i(0);
|
||||||
QList<quint64> list;
|
QList<quint64> list;
|
||||||
for (ScrobblerCacheItem *item : cache_->List()) {
|
for (ScrobblerCacheItemPtr item : cache_->List()) {
|
||||||
if (item->sent_) continue;
|
if (item->sent_) continue;
|
||||||
item->sent_ = true;
|
item->sent_ = true;
|
||||||
++i;
|
++i;
|
||||||
|
|||||||
@@ -19,6 +19,8 @@
|
|||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
|
#include "memory"
|
||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QStandardPaths>
|
#include <QStandardPaths>
|
||||||
#include <QHash>
|
#include <QHash>
|
||||||
@@ -48,7 +50,9 @@ ScrobblerCache::ScrobblerCache(const QString &filename, QObject *parent) :
|
|||||||
loaded_ = true;
|
loaded_ = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
ScrobblerCache::~ScrobblerCache() {}
|
ScrobblerCache::~ScrobblerCache() {
|
||||||
|
scrobbler_cache_.clear();
|
||||||
|
}
|
||||||
|
|
||||||
void ScrobblerCache::ReadCache() {
|
void ScrobblerCache::ReadCache() {
|
||||||
|
|
||||||
@@ -130,8 +134,7 @@ void ScrobblerCache::ReadCache() {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (scrobbler_cache_.contains(timestamp)) continue;
|
if (scrobbler_cache_.contains(timestamp)) continue;
|
||||||
ScrobblerCacheItem *item = new ScrobblerCacheItem(artist, album, song, albumartist, track, duration, timestamp);
|
scrobbler_cache_.insert(timestamp, std::make_shared<ScrobblerCacheItem>(artist, album, song, albumartist, track, duration, timestamp));
|
||||||
scrobbler_cache_.insert(timestamp, item);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,11 +146,17 @@ void ScrobblerCache::WriteCache() {
|
|||||||
|
|
||||||
qLog(Debug) << "Writing scrobbler cache file" << filename_;
|
qLog(Debug) << "Writing scrobbler cache file" << filename_;
|
||||||
|
|
||||||
|
if (scrobbler_cache_.isEmpty()) {
|
||||||
|
QFile file(filename_);
|
||||||
|
if (file.exists()) file.remove();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
QJsonArray array;
|
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) {
|
for (i = scrobbler_cache_.begin() ; i != scrobbler_cache_.end() ; ++i) {
|
||||||
ScrobblerCacheItem *item = i.value();
|
ScrobblerCacheItemPtr item = i.value();
|
||||||
QJsonObject object;
|
QJsonObject object;
|
||||||
object.insert("timestamp", QJsonValue::fromVariant(item->timestamp_));
|
object.insert("timestamp", QJsonValue::fromVariant(item->timestamp_));
|
||||||
object.insert("artist", QJsonValue::fromVariant(item->artist_));
|
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;
|
if (scrobbler_cache_.contains(timestamp)) return nullptr;
|
||||||
|
|
||||||
@@ -187,7 +196,7 @@ ScrobblerCacheItem *ScrobblerCache::Add(const Song &song, const quint64 ×ta
|
|||||||
album.remove(Song::kAlbumRemoveMisc);
|
album.remove(Song::kAlbumRemoveMisc);
|
||||||
title.remove(Song::kTitleRemoveMisc);
|
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);
|
scrobbler_cache_.insert(timestamp, item);
|
||||||
|
|
||||||
if (loaded_) DoInAMinuteOrSo(this, SLOT(WriteCache()));
|
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); }
|
if (scrobbler_cache_.contains(hash)) { return scrobbler_cache_.value(hash); }
|
||||||
else return nullptr;
|
else return nullptr;
|
||||||
@@ -210,27 +219,29 @@ void ScrobblerCache::Remove(const quint64 hash) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
delete scrobbler_cache_.take(hash);
|
scrobbler_cache_.remove(hash);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ScrobblerCache::Remove(ScrobblerCacheItem &item) {
|
void ScrobblerCache::Remove(ScrobblerCacheItemPtr item) {
|
||||||
delete scrobbler_cache_.take(item.timestamp_);
|
scrobbler_cache_.remove(item->timestamp_);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ScrobblerCache::ClearSent(const QList<quint64> list) {
|
void ScrobblerCache::ClearSent(const QList<quint64> &list) {
|
||||||
for (quint64 timestamp : list) {
|
|
||||||
|
for (const quint64 timestamp : list) {
|
||||||
if (!scrobbler_cache_.contains(timestamp)) continue;
|
if (!scrobbler_cache_.contains(timestamp)) continue;
|
||||||
ScrobblerCacheItem *item = scrobbler_cache_.take(timestamp);
|
ScrobblerCacheItemPtr item = scrobbler_cache_.take(timestamp);
|
||||||
item->sent_ = false;
|
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;
|
if (!scrobbler_cache_.contains(timestamp)) continue;
|
||||||
delete scrobbler_cache_.take(timestamp);
|
scrobbler_cache_.remove(timestamp);
|
||||||
}
|
}
|
||||||
DoInAMinuteOrSo(this, SLOT(WriteCache()));
|
DoInAMinuteOrSo(this, SLOT(WriteCache()));
|
||||||
|
|
||||||
|
|||||||
@@ -22,14 +22,17 @@
|
|||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
#include <QtGlobal>
|
#include <QtGlobal>
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QList>
|
#include <QList>
|
||||||
#include <QHash>
|
#include <QHash>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
|
||||||
|
#include "scrobblercacheitem.h"
|
||||||
|
|
||||||
class Song;
|
class Song;
|
||||||
class ScrobblerCacheItem;
|
|
||||||
|
|
||||||
class ScrobblerCache : public QObject {
|
class ScrobblerCache : public QObject {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
@@ -40,14 +43,14 @@ class ScrobblerCache : public QObject {
|
|||||||
|
|
||||||
void ReadCache();
|
void ReadCache();
|
||||||
|
|
||||||
ScrobblerCacheItem *Add(const Song &song, const quint64 ×tamp);
|
ScrobblerCacheItemPtr Add(const Song &song, const quint64 ×tamp);
|
||||||
ScrobblerCacheItem *Get(const quint64 hash);
|
ScrobblerCacheItemPtr Get(const quint64 hash);
|
||||||
void Remove(const quint64 hash);
|
void Remove(const quint64 hash);
|
||||||
void Remove(ScrobblerCacheItem &item);
|
void Remove(ScrobblerCacheItemPtr item);
|
||||||
int Count() const { return scrobbler_cache_.size(); };
|
int Count() const { return scrobbler_cache_.size(); };
|
||||||
QList<ScrobblerCacheItem*> List() const { return scrobbler_cache_.values(); }
|
QList<ScrobblerCacheItemPtr> List() const { return scrobbler_cache_.values(); }
|
||||||
void ClearSent(const QList<quint64> list);
|
void ClearSent(const QList<quint64> &list);
|
||||||
void Flush(const QList<quint64> list);
|
void Flush(const QList<quint64> &list);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void WriteCache();
|
void WriteCache();
|
||||||
@@ -55,7 +58,7 @@ class ScrobblerCache : public QObject {
|
|||||||
private:
|
private:
|
||||||
QString filename_;
|
QString filename_;
|
||||||
bool loaded_;
|
bool loaded_;
|
||||||
QHash <quint64, ScrobblerCacheItem*> scrobbler_cache_;
|
QHash<quint64, ScrobblerCacheItemPtr> scrobbler_cache_;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -33,5 +33,3 @@ ScrobblerCacheItem::ScrobblerCacheItem(const QString &artist, const QString &alb
|
|||||||
duration_(duration),
|
duration_(duration),
|
||||||
timestamp_(timestamp),
|
timestamp_(timestamp),
|
||||||
sent_(false) {}
|
sent_(false) {}
|
||||||
|
|
||||||
ScrobblerCacheItem::~ScrobblerCacheItem() {}
|
|
||||||
|
|||||||
@@ -22,6 +22,8 @@
|
|||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
#include <QtGlobal>
|
#include <QtGlobal>
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
@@ -33,7 +35,6 @@ class ScrobblerCacheItem : public QObject {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
explicit ScrobblerCacheItem(const QString &artist, const QString &album, const QString &song, const QString &albumartist, const int track, const qint64 duration, const quint64 ×tamp);
|
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_; }
|
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
|
#endif // SCROBBLERCACHEITEM_H
|
||||||
|
|||||||
@@ -542,7 +542,7 @@ void ScrobblingAPI20::Submit() {
|
|||||||
|
|
||||||
int i(0);
|
int i(0);
|
||||||
QList<quint64> list;
|
QList<quint64> list;
|
||||||
for (ScrobblerCacheItem *item : cache()->List()) {
|
for (ScrobblerCacheItemPtr item : cache()->List()) {
|
||||||
if (item->sent_) continue;
|
if (item->sent_) continue;
|
||||||
item->sent_ = true;
|
item->sent_ = true;
|
||||||
if (!batch_) {
|
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()
|
ParamList params = ParamList()
|
||||||
<< Param("method", "track.scrobble")
|
<< Param("method", "track.scrobble")
|
||||||
@@ -752,7 +752,7 @@ void ScrobblingAPI20::SingleScrobbleRequestFinished(QNetworkReply *reply, quint6
|
|||||||
|
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
|
|
||||||
ScrobblerCacheItem *item = cache()->Get(timestamp);
|
ScrobblerCacheItemPtr item = cache()->Get(timestamp);
|
||||||
if (!item) {
|
if (!item) {
|
||||||
Error(QString("Received reply for non-existing cache entry %1.").arg(timestamp));
|
Error(QString("Received reply for non-existing cache entry %1.").arg(timestamp));
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -32,13 +32,13 @@
|
|||||||
#include "core/song.h"
|
#include "core/song.h"
|
||||||
#include "scrobblerservice.h"
|
#include "scrobblerservice.h"
|
||||||
#include "scrobblercache.h"
|
#include "scrobblercache.h"
|
||||||
|
#include "scrobblercacheitem.h"
|
||||||
|
|
||||||
class QNetworkReply;
|
class QNetworkReply;
|
||||||
|
|
||||||
class Application;
|
class Application;
|
||||||
class NetworkAccessManager;
|
class NetworkAccessManager;
|
||||||
class LocalRedirectServer;
|
class LocalRedirectServer;
|
||||||
class ScrobblerCacheItem;
|
|
||||||
|
|
||||||
class ScrobblingAPI20 : public ScrobblerService {
|
class ScrobblingAPI20 : public ScrobblerService {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
@@ -129,7 +129,7 @@ class ScrobblingAPI20 : public ScrobblerService {
|
|||||||
|
|
||||||
void RequestSession(const QString &token);
|
void RequestSession(const QString &token);
|
||||||
void AuthError(const QString &error);
|
void AuthError(const QString &error);
|
||||||
void SendSingleScrobble(ScrobblerCacheItem *item);
|
void SendSingleScrobble(ScrobblerCacheItemPtr item);
|
||||||
void Error(const QString &error, const QVariant &debug = QVariant());
|
void Error(const QString &error, const QVariant &debug = QVariant());
|
||||||
QString ErrorString(const ScrobbleErrorCode error) const;
|
QString ErrorString(const ScrobbleErrorCode error) const;
|
||||||
void DoSubmit();
|
void DoSubmit();
|
||||||
|
|||||||
Reference in New Issue
Block a user