Fix discogs album cover fetcher

This commit is contained in:
Jonas Kvinge
2018-03-17 14:28:45 +01:00
parent c3f0d27165
commit dadd5e05a3
8 changed files with 75 additions and 140 deletions

View File

@@ -242,7 +242,7 @@ void AlbumCoverChoiceController::ShowCover(const Song &song) {
void AlbumCoverChoiceController::SearchCoverAutomatically(const Song &song) { void AlbumCoverChoiceController::SearchCoverAutomatically(const Song &song) {
qint64 id = cover_fetcher_->FetchAlbumCover(song.effective_albumartist(), song.effective_album()); qint64 id = cover_fetcher_->FetchAlbumCover(song.effective_albumartist(), song.effective_album(), false);
cover_fetching_tasks_[id] = song; cover_fetching_tasks_[id] = song;

View File

@@ -41,7 +41,7 @@ AlbumCoverFetcher::AlbumCoverFetcher(CoverProviders *cover_providers, QObject *p
connect(request_starter_, SIGNAL(timeout()), SLOT(StartRequests())); connect(request_starter_, SIGNAL(timeout()), SLOT(StartRequests()));
} }
quint64 AlbumCoverFetcher::FetchAlbumCover(const QString &artist, const QString &album) { quint64 AlbumCoverFetcher::FetchAlbumCover(const QString &artist, const QString &album, bool fetchall) {
QString album2(album); QString album2(album);
album2 = album2.remove(QRegExp(" ?-? ((\\(|\\[)?)(Disc|CD) ?([0-9]{1,2})((\\)|\\])?)$")); album2 = album2.remove(QRegExp(" ?-? ((\\(|\\[)?)(Disc|CD) ?([0-9]{1,2})((\\)|\\])?)$"));
@@ -51,6 +51,7 @@ quint64 AlbumCoverFetcher::FetchAlbumCover(const QString &artist, const QString
request.album = album2; request.album = album2;
request.search = false; request.search = false;
request.id = next_id_++; request.id = next_id_++;
request.fetchall = fetchall;
AddRequest(request); AddRequest(request);
return request.id; return request.id;
@@ -59,8 +60,6 @@ quint64 AlbumCoverFetcher::FetchAlbumCover(const QString &artist, const QString
quint64 AlbumCoverFetcher::SearchForCovers(const QString &artist, const QString &album) { quint64 AlbumCoverFetcher::SearchForCovers(const QString &artist, const QString &album) {
fetchall_ = false;
QString album2(album); QString album2(album);
album2 = album2.remove(QRegExp(" ?-? ((\\(|\\[)?)(Disc|CD) ?([0-9]{1,2})((\\)|\\])?)$")); album2 = album2.remove(QRegExp(" ?-? ((\\(|\\[)?)(Disc|CD) ?([0-9]{1,2})((\\)|\\])?)$"));
@@ -69,6 +68,7 @@ quint64 AlbumCoverFetcher::SearchForCovers(const QString &artist, const QString
request.album = album2; request.album = album2;
request.search = true; request.search = true;
request.id = next_id_++; request.id = next_id_++;
request.fetchall = false;
AddRequest(request); AddRequest(request);
return request.id; return request.id;
@@ -110,7 +110,6 @@ void AlbumCoverFetcher::StartRequests() {
// search objects are this fetcher's children so worst case scenario - they get deleted with it // search objects are this fetcher's children so worst case scenario - they get deleted with it
AlbumCoverFetcherSearch *search = new AlbumCoverFetcherSearch(request, network_, this); AlbumCoverFetcherSearch *search = new AlbumCoverFetcherSearch(request, network_, this);
search->fetchall_ = fetchall_;
active_requests_.insert(request.id, search); active_requests_.insert(request.id, search);
connect(search, SIGNAL(SearchFinished(quint64, CoverSearchResults)), SLOT(SingleSearchFinished(quint64, CoverSearchResults))); connect(search, SIGNAL(SearchFinished(quint64, CoverSearchResults)), SLOT(SingleSearchFinished(quint64, CoverSearchResults)));

View File

@@ -51,6 +51,9 @@ struct CoverSearchRequest {
// is this only a search request or should we also fetch the first cover that's found? // is this only a search request or should we also fetch the first cover that's found?
bool search; bool search;
// is the request part of fetchall (fetching all missing covers)
bool fetchall;
}; };
// This structure represents a single result of some album's cover search request. // This structure represents a single result of some album's cover search request.
@@ -84,12 +87,10 @@ class AlbumCoverFetcher : public QObject {
static const int kMaxConcurrentRequests; static const int kMaxConcurrentRequests;
quint64 SearchForCovers(const QString &artist, const QString &album); quint64 SearchForCovers(const QString &artist, const QString &album);
quint64 FetchAlbumCover(const QString &artist, const QString &album); quint64 FetchAlbumCover(const QString &artist, const QString &album, bool fetchall);
void Clear(); void Clear();
bool fetchall_ = false;
signals: signals:
void AlbumCoverFetched(quint64, const QImage &cover, const CoverSearchStatistics &statistics); void AlbumCoverFetched(quint64, const QImage &cover, const CoverSearchStatistics &statistics);
void SearchFinished(quint64, const CoverSearchResults &results, const CoverSearchStatistics &statistics); void SearchFinished(quint64, const CoverSearchResults &results, const CoverSearchStatistics &statistics);

View File

@@ -69,7 +69,7 @@ void AlbumCoverFetcherSearch::Start(CoverProviders *cover_providers) {
for (CoverProvider *provider : cover_providers->List()) { for (CoverProvider *provider : cover_providers->List()) {
// Skip provider if it does not have fetchall set, and we are doing fetchall - "Fetch Missing Covers". // Skip provider if it does not have fetchall set, and we are doing fetchall - "Fetch Missing Covers".
if ((provider->fetchall() == false) && (fetchall_ == true)) { if (!provider->fetchall() && request_.fetchall) {
//qLog(Debug) << "Skipping provider" << provider->name(); //qLog(Debug) << "Skipping provider" << provider->name();
continue; continue;
} }

View File

@@ -52,8 +52,6 @@ class AlbumCoverFetcherSearch : public QObject {
CoverSearchStatistics statistics() const { return statistics_; } CoverSearchStatistics statistics() const { return statistics_; }
bool fetchall_ = false;
signals: signals:
// It's the end of search (when there was no fetch-me-a-cover request). // It's the end of search (when there was no fetch-me-a-cover request).
void SearchFinished(quint64, const CoverSearchResults& results); void SearchFinished(quint64, const CoverSearchResults& results);

View File

@@ -416,14 +416,12 @@ bool AlbumCoverManager::ShouldHide(const QListWidgetItem &item, const QString &f
void AlbumCoverManager::FetchAlbumCovers() { void AlbumCoverManager::FetchAlbumCovers() {
cover_fetcher_->fetchall_ = true;
for (int i = 0; i < ui_->albums->count(); ++i) { for (int i = 0; i < ui_->albums->count(); ++i) {
QListWidgetItem *item = ui_->albums->item(i); QListWidgetItem *item = ui_->albums->item(i);
if (item->isHidden()) continue; if (item->isHidden()) continue;
if (ItemHasCover(*item)) continue; if (ItemHasCover(*item)) continue;
quint64 id = cover_fetcher_->FetchAlbumCover(EffectiveAlbumArtistName(*item), item->data(Role_AlbumName).toString()); quint64 id = cover_fetcher_->FetchAlbumCover(EffectiveAlbumArtistName(*item), item->data(Role_AlbumName).toString(), true);
cover_fetching_tasks_[id] = item; cover_fetching_tasks_[id] = item;
jobs_++; jobs_++;
} }
@@ -557,10 +555,8 @@ void AlbumCoverManager::ShowCover() {
void AlbumCoverManager::FetchSingleCover() { void AlbumCoverManager::FetchSingleCover() {
cover_fetcher_->fetchall_ = false;
for (QListWidgetItem *item : context_menu_items_) { for (QListWidgetItem *item : context_menu_items_) {
quint64 id = cover_fetcher_->FetchAlbumCover(EffectiveAlbumArtistName(*item), item->data(Role_AlbumName).toString()); quint64 id = cover_fetcher_->FetchAlbumCover(EffectiveAlbumArtistName(*item), item->data(Role_AlbumName).toString(), false);
cover_fetching_tasks_[id] = item; cover_fetching_tasks_[id] = item;
jobs_++; jobs_++;
} }

View File

@@ -1,7 +1,6 @@
/* /*
* Strawberry Music Player * Strawberry Music Player
* This file was part of Clementine. * This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
* Copyright 2012, Martin Björklund <mbj4668@gmail.com> * Copyright 2012, Martin Björklund <mbj4668@gmail.com>
* Copyright 2016, Jonas Kvinge <jonas@jkvinge.net> * Copyright 2016, Jonas Kvinge <jonas@jkvinge.net>
* *
@@ -22,13 +21,17 @@
#include "config.h" #include "config.h"
#include <QByteArray>
#include <QVariant> #include <QVariant>
#include <QNetworkReply> #include <QPair>
#include <QXmlStreamReader> #include <QList>
#include <QString>
#include <QStringList> #include <QStringList>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QUrlQuery> #include <QUrlQuery>
#include <QJsonObject>
#include <QJsonDocument> #include <QJsonDocument>
#include <QJsonObject>
#include "discogscoverprovider.h" #include "discogscoverprovider.h"
@@ -41,14 +44,14 @@ const char *DiscogsCoverProvider::kUrlSearch = "https://api.discogs.com/database
const char *DiscogsCoverProvider::kUrlReleases = "https://api.discogs.com/releases"; const char *DiscogsCoverProvider::kUrlReleases = "https://api.discogs.com/releases";
const char *DiscogsCoverProvider::kAccessKeyB64 = "dGh6ZnljUGJlZ1NEeXBuSFFxSVk="; const char *DiscogsCoverProvider::kAccessKeyB64 = "dGh6ZnljUGJlZ1NEeXBuSFFxSVk=";
const char *DiscogsCoverProvider::kSecretAccessKeyB64 = "ZkFIcmlaSER4aHhRSlF2U3d0bm5ZVmdxeXFLWUl0UXI="; const char *DiscogsCoverProvider::kSecretKeyB64 = "ZkFIcmlaSER4aHhRSlF2U3d0bm5ZVmdxeXFLWUl0UXI=";
DiscogsCoverProvider::DiscogsCoverProvider(QObject *parent) : CoverProvider("Discogs", false, parent), network_(new NetworkAccessManager(this)) {} DiscogsCoverProvider::DiscogsCoverProvider(QObject *parent) : CoverProvider("Discogs", false, parent), network_(new NetworkAccessManager(this)) {}
bool DiscogsCoverProvider::StartSearch(const QString &artist, const QString &album, int s_id) { bool DiscogsCoverProvider::StartSearch(const QString &artist, const QString &album, int s_id) {
DiscogsCoverSearchContext *s_ctx = new DiscogsCoverSearchContext; DiscogsCoverSearchContext *s_ctx = new DiscogsCoverSearchContext;
if (s_ctx == nullptr) return false;
s_ctx->id = s_id; s_ctx->id = s_id;
s_ctx->artist = artist; s_ctx->artist = artist;
s_ctx->album = album; s_ctx->album = album;
@@ -60,10 +63,14 @@ bool DiscogsCoverProvider::StartSearch(const QString &artist, const QString &alb
} }
void DiscogsCoverProvider::CancelSearch(int id) {
delete requests_search_.take(id);
}
bool DiscogsCoverProvider::StartRelease(DiscogsCoverSearchContext *s_ctx, int r_id, QString resource_url) { bool DiscogsCoverProvider::StartRelease(DiscogsCoverSearchContext *s_ctx, int r_id, QString resource_url) {
DiscogsCoverReleaseContext *r_ctx = new DiscogsCoverReleaseContext; DiscogsCoverReleaseContext *r_ctx = new DiscogsCoverReleaseContext;
if (r_ctx == nullptr) return false;
s_ctx->r_count++; s_ctx->r_count++;
@@ -89,7 +96,7 @@ void DiscogsCoverProvider::SendSearchRequest(DiscogsCoverSearchContext *s_ctx) {
ArgList args = ArgList() ArgList args = ArgList()
<< Arg("key", QByteArray::fromBase64(kAccessKeyB64)) << Arg("key", QByteArray::fromBase64(kAccessKeyB64))
<< Arg("secret", QByteArray::fromBase64(kSecretAccessKeyB64)); << Arg("secret", QByteArray::fromBase64(kSecretKeyB64));
args.append(Arg("type", "release")); args.append(Arg("type", "release"));
if (!s_ctx->artist.isEmpty()) { if (!s_ctx->artist.isEmpty()) {
@@ -112,7 +119,7 @@ void DiscogsCoverProvider::SendSearchRequest(DiscogsCoverSearchContext *s_ctx) {
// Sign the request // Sign the request
const QByteArray data_to_sign = QString("GET\n%1\n%2\n%3").arg(url.host(), url.path(), query_items.join("&")).toLatin1(); const QByteArray data_to_sign = QString("GET\n%1\n%2\n%3").arg(url.host(), url.path(), query_items.join("&")).toLatin1();
const QByteArray signature(Utilities::HmacSha256(QByteArray::fromBase64(kSecretAccessKeyB64), data_to_sign)); const QByteArray signature(Utilities::HmacSha256(QByteArray::fromBase64(kSecretKeyB64), data_to_sign));
// Add the signature to the request // Add the signature to the request
url_query.addQueryItem("Signature", QUrl::toPercentEncoding(signature.toBase64())); url_query.addQueryItem("Signature", QUrl::toPercentEncoding(signature.toBase64()));
@@ -123,8 +130,6 @@ void DiscogsCoverProvider::SendSearchRequest(DiscogsCoverSearchContext *s_ctx) {
NewClosure(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(SearchRequestError(QNetworkReply::NetworkError, QNetworkReply*, int)), reply, s_ctx->id); NewClosure(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(SearchRequestError(QNetworkReply::NetworkError, QNetworkReply*, int)), reply, s_ctx->id);
NewClosure(reply, SIGNAL(finished()), this, SLOT(HandleSearchReply(QNetworkReply*, int)), reply, s_ctx->id); NewClosure(reply, SIGNAL(finished()), this, SLOT(HandleSearchReply(QNetworkReply*, int)), reply, s_ctx->id);
return true;
} }
void DiscogsCoverProvider::SendReleaseRequest(DiscogsCoverSearchContext *s_ctx, DiscogsCoverReleaseContext *r_ctx) { void DiscogsCoverProvider::SendReleaseRequest(DiscogsCoverSearchContext *s_ctx, DiscogsCoverReleaseContext *r_ctx) {
@@ -140,7 +145,7 @@ void DiscogsCoverProvider::SendReleaseRequest(DiscogsCoverSearchContext *s_ctx,
ArgList args = ArgList() ArgList args = ArgList()
<< Arg("key", QByteArray::fromBase64(kAccessKeyB64)) << Arg("key", QByteArray::fromBase64(kAccessKeyB64))
<< Arg("secret", QByteArray::fromBase64(kSecretAccessKeyB64)); << Arg("secret", QByteArray::fromBase64(kSecretKeyB64));
// Encode the arguments // Encode the arguments
for (const Arg &arg : args) { for (const Arg &arg : args) {
EncodedArg encoded_arg(QUrl::toPercentEncoding(arg.first), QUrl::toPercentEncoding(arg.second)); EncodedArg encoded_arg(QUrl::toPercentEncoding(arg.first), QUrl::toPercentEncoding(arg.second));
@@ -148,14 +153,11 @@ void DiscogsCoverProvider::SendReleaseRequest(DiscogsCoverSearchContext *s_ctx,
url_query.addQueryItem(encoded_arg.first, encoded_arg.second); url_query.addQueryItem(encoded_arg.first, encoded_arg.second);
} }
//QString urlstr = QString("%1/%2").arg(kUrlReleases).arg(r_ctx->id);
QUrl url(r_ctx->resource_url); QUrl url(r_ctx->resource_url);
//qLog(Debug) << "Send: " << url;
// Sign the request // Sign the request
const QByteArray data_to_sign = QString("GET\n%1\n%2\n%3").arg(url.host(), url.path(), query_items.join("&")).toLatin1(); const QByteArray data_to_sign = QString("GET\n%1\n%2\n%3").arg(url.host(), url.path(), query_items.join("&")).toUtf8();
const QByteArray signature(Utilities::HmacSha256(QByteArray::fromBase64(kSecretAccessKeyB64), data_to_sign)); const QByteArray signature(Utilities::HmacSha256(QByteArray::fromBase64(kSecretKeyB64), data_to_sign));
// Add the signature to the request // Add the signature to the request
url_query.addQueryItem("Signature", QUrl::toPercentEncoding(signature.toBase64())); url_query.addQueryItem("Signature", QUrl::toPercentEncoding(signature.toBase64()));
@@ -166,39 +168,24 @@ void DiscogsCoverProvider::SendReleaseRequest(DiscogsCoverSearchContext *s_ctx,
NewClosure(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(ReleaseRequestError(QNetworkReply::NetworkError, QNetworkReply*, int, int)), reply, s_ctx->id, r_ctx->id); NewClosure(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(ReleaseRequestError(QNetworkReply::NetworkError, QNetworkReply*, int, int)), reply, s_ctx->id, r_ctx->id);
NewClosure(reply, SIGNAL(finished()), this, SLOT(HandleReleaseReply(QNetworkReply*, int, int)), reply, s_ctx->id, r_ctx->id); NewClosure(reply, SIGNAL(finished()), this, SLOT(HandleReleaseReply(QNetworkReply*, int, int)), reply, s_ctx->id, r_ctx->id);
return true;
} }
void DiscogsCoverProvider::HandleSearchReply(QNetworkReply *reply, int s_id) { void DiscogsCoverProvider::HandleSearchReply(QNetworkReply *reply, int s_id) {
//QString text(reply->readAll());
//qLog(Debug) << "text: " << text << "\n";
reply->deleteLater(); reply->deleteLater();
DiscogsCoverSearchContext *s_ctx;
if (!requests_search_.contains(s_id)) { if (!requests_search_.contains(s_id)) {
qLog(Error) << "Discogs: Got reply for cancelled request: " << s_id; //qLog(Error) << "Discogs: Got reply for cancelled request: " << s_id;
return; return;
} }
s_ctx = requests_search_.value(s_id); DiscogsCoverSearchContext *s_ctx = requests_search_.value(s_id);
if (s_ctx == nullptr) return;
QString json_string; QJsonDocument json_doc = QJsonDocument::fromJson(reply->readAll());
json_string = reply->readAll(); if ((json_doc.isNull()) || (!json_doc.isObject())) {
QByteArray json_bytes = json_string.toLocal8Bit();
auto json_doc = QJsonDocument::fromJson(json_bytes);
if (json_doc.isNull()) {
qLog(Error) << "Discogs: Failed to create JSON doc."; qLog(Error) << "Discogs: Failed to create JSON doc.";
EndSearch(s_ctx); EndSearch(s_ctx);
return; return;
} }
if (!json_doc.isObject()) {
qLog(Error) << "Discogs: JSON is not an object.";
EndSearch(s_ctx);
return;
}
QJsonObject json_obj = json_doc.object(); QJsonObject json_obj = json_doc.object();
if (json_obj.isEmpty()) { if (json_obj.isEmpty()) {
@@ -209,7 +196,7 @@ void DiscogsCoverProvider::HandleSearchReply(QNetworkReply *reply, int s_id) {
QVariantMap reply_map = json_obj.toVariantMap(); QVariantMap reply_map = json_obj.toVariantMap();
if (!reply_map.contains("results")) { if (!reply_map.contains("results")) {
qLog(Error) << "Discogs: Search reply from server is missing JSON results."; //qLog(Error) << "Discogs: Search reply from server is missing JSON results.";
//qLog(Error) << "Discogs: Map dump:"; //qLog(Error) << "Discogs: Map dump:";
//qLog(Error) << reply_map; //qLog(Error) << reply_map;
EndSearch(s_ctx); EndSearch(s_ctx);
@@ -221,24 +208,14 @@ void DiscogsCoverProvider::HandleSearchReply(QNetworkReply *reply, int s_id) {
for (const QVariant &result : results) { for (const QVariant &result : results) {
QVariantMap result_map = result.toMap(); QVariantMap result_map = result.toMap();
int r_id = 0; if ((result_map.contains("id")) && (result_map.contains("resource_url"))) {
QString title; int r_id = result_map["id"].toInt();
QString resource_url; QString title = result_map["title"].toString();
if ((!result_map.contains("id")) || (!result_map.contains("resource_url"))) continue; QString resource_url = result_map["resource_url"].toString();
if (resource_url.isEmpty()) continue;
if (result_map.contains("id")) { StartRelease(s_ctx, r_id, resource_url);
r_id = result_map["id"].toInt(); i++;
//qLog(Debug) << "id: " << r_id;
} }
if (result_map.contains("title")) {
title = result_map["title"].toString();
}
if (result_map.contains("resource_url")) {
resource_url = result_map["resource_url"].toString();
//qLog(Debug) << "resource_url: " << resource_url;
}
StartRelease(s_ctx, r_id, resource_url);
i++;
} }
if (i <= 0) EndSearch(s_ctx); if (i <= 0) EndSearch(s_ctx);
@@ -246,42 +223,27 @@ void DiscogsCoverProvider::HandleSearchReply(QNetworkReply *reply, int s_id) {
void DiscogsCoverProvider::HandleReleaseReply(QNetworkReply *reply, int s_id, int r_id) { void DiscogsCoverProvider::HandleReleaseReply(QNetworkReply *reply, int s_id, int r_id) {
//QString text(reply->readAll());
//qLog(Debug) << "text: " << text << "\n";
reply->deleteLater(); reply->deleteLater();
DiscogsCoverReleaseContext *r_ctx;
if (!requests_release_.contains(r_id)) { if (!requests_release_.contains(r_id)) {
//qLog(Error) << "Discogs: Got reply for cancelled request: " << r_id; //qLog(Error) << "Discogs: Got reply for cancelled request: " << r_id;
return; return;
} }
r_ctx = requests_release_.value(r_id); DiscogsCoverReleaseContext *r_ctx = requests_release_.value(r_id);
if (r_ctx == nullptr) return;
DiscogsCoverSearchContext *s_ctx;
if (!requests_search_.contains(s_id)) { if (!requests_search_.contains(s_id)) {
qLog(Error) << "Discogs: Got reply for cancelled request: " << s_id << " " << r_id; //qLog(Error) << "Discogs: Got reply for cancelled request: " << s_id << " " << r_id;
EndSearch(nullptr, r_ctx); EndSearch(r_ctx);
return; return;
} }
s_ctx = requests_search_.value(s_id); DiscogsCoverSearchContext *s_ctx = requests_search_.value(s_id);
if (s_ctx == nullptr) return;
QString json_string; QJsonDocument json_doc = QJsonDocument::fromJson(reply->readAll());
json_string = reply->readAll(); if ((json_doc.isNull()) || (!json_doc.isObject())) {
QByteArray json_bytes = json_string.toLocal8Bit();
auto json_doc = QJsonDocument::fromJson(json_bytes);
if (json_doc.isNull()) {
qLog(Error) << "Discogs: Failed to create JSON doc."; qLog(Error) << "Discogs: Failed to create JSON doc.";
EndSearch(s_ctx, r_ctx); EndSearch(s_ctx, r_ctx);
return; return;
} }
if (!json_doc.isObject()) {
qLog(Error) << "Discogs: JSON is not an object.";
EndSearch(s_ctx, r_ctx);
return;
}
QJsonObject json_obj = json_doc.object(); QJsonObject json_obj = json_doc.object();
if (json_obj.isEmpty()) { if (json_obj.isEmpty()) {
@@ -310,13 +272,10 @@ void DiscogsCoverProvider::HandleReleaseReply(QNetworkReply *reply, int s_id, in
QString type = result_map["type"].toString(); QString type = result_map["type"].toString();
if (type != "primary") continue; if (type != "primary") continue;
} }
if (result_map.contains("height")) {
}
if (result_map.contains("width")) {
}
if (result_map.contains("resource_url")) { if (result_map.contains("resource_url")) {
cover_result.image_url = QUrl(result_map["resource_url"].toString()); cover_result.image_url = QUrl(result_map["resource_url"].toString());
} }
if (cover_result.image_url.isEmpty()) continue;
s_ctx->results.append(cover_result); s_ctx->results.append(cover_result);
} }
@@ -324,43 +283,31 @@ void DiscogsCoverProvider::HandleReleaseReply(QNetworkReply *reply, int s_id, in
} }
void DiscogsCoverProvider::CancelSearch(int id) {
delete requests_search_.take(id);
}
void DiscogsCoverProvider::SearchRequestError(QNetworkReply::NetworkError error, QNetworkReply *reply, int s_id) { void DiscogsCoverProvider::SearchRequestError(QNetworkReply::NetworkError error, QNetworkReply *reply, int s_id) {
DiscogsCoverSearchContext *s_ctx;
if (!requests_search_.contains(s_id)) { if (!requests_search_.contains(s_id)) {
qLog(Error) << "Discogs: got reply for cancelled request: " << s_id; //qLog(Error) << "Discogs: got reply for cancelled request: " << s_id;
return; return;
} }
s_ctx = requests_search_.value(s_id); DiscogsCoverSearchContext *s_ctx = requests_search_.value(s_id);
if (s_ctx == nullptr) return;
EndSearch(s_ctx); EndSearch(s_ctx);
} }
void DiscogsCoverProvider::ReleaseRequestError(QNetworkReply::NetworkError error, QNetworkReply *reply, int s_id, int r_id) { void DiscogsCoverProvider::ReleaseRequestError(QNetworkReply::NetworkError error, QNetworkReply *reply, int s_id, int r_id) {
DiscogsCoverSearchContext *s_ctx;
if (!requests_search_.contains(s_id)) {
qLog(Error) << "Discogs: got reply for cancelled request: " << s_id << r_id;
return;
}
s_ctx = requests_search_.value(s_id);
if (s_ctx == nullptr) return;
DiscogsCoverReleaseContext *r_ctx;
if (!requests_release_.contains(r_id)) { if (!requests_release_.contains(r_id)) {
qLog(Error) << "Discogs: got reply for cancelled request: " << s_id << r_id; //qLog(Error) << "Discogs: got reply for cancelled request: " << s_id << r_id;
return; return;
} }
r_ctx = requests_release_.value(r_id); DiscogsCoverReleaseContext *r_ctx = requests_release_.value(r_id);
if (r_ctx == nullptr) return;
if (!requests_search_.contains(s_id)) {
EndSearch(r_ctx);
//qLog(Error) << "Discogs: got reply for cancelled request: " << s_id << r_id;
return;
}
DiscogsCoverSearchContext *s_ctx = requests_search_.value(s_id);
EndSearch(s_ctx, r_ctx); EndSearch(s_ctx, r_ctx);
@@ -368,10 +315,7 @@ void DiscogsCoverProvider::ReleaseRequestError(QNetworkReply::NetworkError error
void DiscogsCoverProvider::EndSearch(DiscogsCoverSearchContext *s_ctx, DiscogsCoverReleaseContext *r_ctx) { void DiscogsCoverProvider::EndSearch(DiscogsCoverSearchContext *s_ctx, DiscogsCoverReleaseContext *r_ctx) {
(void)requests_release_.remove(r_ctx->id); delete requests_release_.take(r_ctx->id);
delete r_ctx;
if (s_ctx == nullptr) return;
s_ctx->r_count--; s_ctx->r_count--;
@@ -385,8 +329,12 @@ void DiscogsCoverProvider::EndSearch(DiscogsCoverSearchContext *s_ctx) {
//qLog(Debug) << "Discogs: Finished." << s_ctx->id; //qLog(Debug) << "Discogs: Finished." << s_ctx->id;
(void)requests_search_.remove(s_ctx->id); requests_search_.remove(s_ctx->id);
emit SearchFinished(s_ctx->id, s_ctx->results); emit SearchFinished(s_ctx->id, s_ctx->results);
delete s_ctx; delete s_ctx;
} }
void DiscogsCoverProvider::EndSearch(DiscogsCoverReleaseContext* r_ctx) {
delete requests_release_.take(r_ctx->id);
}

View File

@@ -1,7 +1,6 @@
/* /*
* Strawberry Music Player * Strawberry Music Player
* This file was part of Clementine. * This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
* Copyright 2012, Martin Björklund <mbj4668@gmail.com> * Copyright 2012, Martin Björklund <mbj4668@gmail.com>
* Copyright 2016, Jonas Kvinge <jonas@jkvinge.net> * Copyright 2016, Jonas Kvinge <jonas@jkvinge.net>
* *
@@ -34,7 +33,6 @@ class QNetworkAccessManager;
// This struct represents a single search-for-cover request. It identifies and describes the request. // This struct represents a single search-for-cover request. It identifies and describes the request.
struct DiscogsCoverSearchContext { struct DiscogsCoverSearchContext {
enum State { State_Init, State_Release };
// the unique request identifier // the unique request identifier
int id; int id;
@@ -49,6 +47,8 @@ struct DiscogsCoverSearchContext {
}; };
Q_DECLARE_METATYPE(DiscogsCoverSearchContext) Q_DECLARE_METATYPE(DiscogsCoverSearchContext)
// This struct represents a single release request. It identifies and describes
// the request.
struct DiscogsCoverReleaseContext { struct DiscogsCoverReleaseContext {
int id; // the unique request identifier int id; // the unique request identifier
@@ -64,19 +64,6 @@ class DiscogsCoverProvider : public CoverProvider {
public: public:
explicit DiscogsCoverProvider(QObject *parent = nullptr); explicit DiscogsCoverProvider(QObject *parent = nullptr);
static const char *kUrlSearch;
static const char *kUrlReleases;
static const char *kRequestTokenURL;
static const char *kAuthorizeURL;
static const char *kAccessTokenURL;
static const char *kAccessKey;
static const char *kSecretAccessKey;
static const char *kAccessKeyB64;
static const char *kSecretAccessKeyB64;
bool StartSearch(const QString &artist, const QString &album, int s_id); bool StartSearch(const QString &artist, const QString &album, int s_id);
void CancelSearch(int id); void CancelSearch(int id);
@@ -85,9 +72,14 @@ class DiscogsCoverProvider : public CoverProvider {
void SearchRequestError(QNetworkReply::NetworkError error, QNetworkReply *reply, int s_id); void SearchRequestError(QNetworkReply::NetworkError error, QNetworkReply *reply, int s_id);
void ReleaseRequestError(QNetworkReply::NetworkError error, QNetworkReply *reply, int s_id, int r_id); void ReleaseRequestError(QNetworkReply::NetworkError error, QNetworkReply *reply, int s_id, int r_id);
void HandleSearchReply(QNetworkReply *reply, int s_id); void HandleSearchReply(QNetworkReply *reply, int s_id);
void HandleReleaseReply(QNetworkReply *reply, int sa_id, int si_id); void HandleReleaseReply(QNetworkReply *reply, int s_id, int r_id);
private: private:
static const char *kUrlSearch;
static const char *kUrlReleases;
static const char *kAccessKeyB64;
static const char *kSecretKeyB64;
QNetworkAccessManager *network_; QNetworkAccessManager *network_;
QHash<int, DiscogsCoverSearchContext*> requests_search_; QHash<int, DiscogsCoverSearchContext*> requests_search_;
QHash<int, DiscogsCoverReleaseContext*> requests_release_; QHash<int, DiscogsCoverReleaseContext*> requests_release_;
@@ -98,6 +90,7 @@ class DiscogsCoverProvider : public CoverProvider {
void SendReleaseRequest(DiscogsCoverSearchContext *s_ctx, DiscogsCoverReleaseContext *r_ctx); void SendReleaseRequest(DiscogsCoverSearchContext *s_ctx, DiscogsCoverReleaseContext *r_ctx);
void EndSearch(DiscogsCoverSearchContext *s_ctx, DiscogsCoverReleaseContext *r_ctx); void EndSearch(DiscogsCoverSearchContext *s_ctx, DiscogsCoverReleaseContext *r_ctx);
void EndSearch(DiscogsCoverSearchContext *s_ctx); void EndSearch(DiscogsCoverSearchContext *s_ctx);
void EndSearch(DiscogsCoverReleaseContext *r_ctx);
}; };