2012-01-29 15:26:28 +01:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2012, Martin Björklund <mbj4668@gmail.com>
|
|
|
|
|
|
|
|
Clementine is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "discogscoverprovider.h"
|
|
|
|
#include "core/closure.h"
|
|
|
|
#include "core/logging.h"
|
|
|
|
#include "core/network.h"
|
|
|
|
#include "core/utilities.h"
|
|
|
|
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <qjson/parser.h>
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
const char* DiscogsCoverProvider::kSearchUrl =
|
|
|
|
"http://api.discogs.com/database/search";
|
2012-01-29 15:26:28 +01:00
|
|
|
|
|
|
|
DiscogsCoverProvider::DiscogsCoverProvider(QObject* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: CoverProvider("Discogs", parent),
|
|
|
|
network_(new NetworkAccessManager(this)) {}
|
2012-01-29 15:26:28 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
bool DiscogsCoverProvider::StartSearch(const QString& artist,
|
|
|
|
const QString& album, int id) {
|
2012-02-07 14:05:26 +01:00
|
|
|
DiscogsCoverSearchContext* ctx = new DiscogsCoverSearchContext;
|
|
|
|
ctx->id = id;
|
|
|
|
ctx->artist = artist;
|
|
|
|
ctx->album = album;
|
|
|
|
ctx->state = DiscogsCoverSearchContext::State_Init;
|
|
|
|
pending_requests_.insert(id, ctx);
|
|
|
|
SendSearchRequest(ctx);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiscogsCoverProvider::CancelSearch(int id) {
|
|
|
|
qLog(Debug) << "Discogs: cancel search id:" << id;
|
|
|
|
delete pending_requests_.take(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the search request using Discog's HTTP-based API.
|
|
|
|
void DiscogsCoverProvider::SendSearchRequest(DiscogsCoverSearchContext* ctx) {
|
2012-01-29 15:26:28 +01:00
|
|
|
typedef QPair<QString, QString> Arg;
|
|
|
|
typedef QList<Arg> ArgList;
|
|
|
|
|
|
|
|
typedef QPair<QByteArray, QByteArray> EncodedArg;
|
|
|
|
typedef QList<EncodedArg> EncodedArgList;
|
|
|
|
|
|
|
|
QUrl url(kSearchUrl);
|
|
|
|
|
2012-02-07 14:05:26 +01:00
|
|
|
QString type;
|
|
|
|
|
|
|
|
switch (ctx->state) {
|
2014-02-07 16:34:20 +01:00
|
|
|
case DiscogsCoverSearchContext::State_Init:
|
|
|
|
type = "master";
|
|
|
|
ctx->state = DiscogsCoverSearchContext::State_MastersRequested;
|
|
|
|
break;
|
|
|
|
case DiscogsCoverSearchContext::State_MastersRequested:
|
|
|
|
type = "release";
|
|
|
|
ctx->state = DiscogsCoverSearchContext::State_ReleasesRequested;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
EndSearch(ctx);
|
|
|
|
return;
|
2012-02-07 14:05:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ArgList args = ArgList();
|
|
|
|
if (!ctx->artist.isEmpty()) {
|
|
|
|
args.append(Arg("artist", ctx->artist.toLower()));
|
|
|
|
}
|
|
|
|
if (!ctx->album.isEmpty()) {
|
|
|
|
args.append(Arg("release_title", ctx->album.toLower()));
|
|
|
|
}
|
|
|
|
args.append(Arg("type", type));
|
2012-01-29 15:26:28 +01:00
|
|
|
|
|
|
|
EncodedArgList encoded_args;
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
foreach(const Arg & arg, args) {
|
2012-01-29 15:26:28 +01:00
|
|
|
EncodedArg encoded_arg(QUrl::toPercentEncoding(arg.first),
|
|
|
|
QUrl::toPercentEncoding(arg.second));
|
|
|
|
encoded_args << encoded_arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
url.setEncodedQueryItems(encoded_args);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
qLog(Debug) << "Discogs: send search request for id:" << ctx->id
|
|
|
|
<< "url: " << url;
|
2012-02-07 14:05:26 +01:00
|
|
|
|
2012-01-29 15:26:28 +01:00
|
|
|
QNetworkReply* reply = network_->get(QNetworkRequest(url));
|
2014-02-07 16:34:20 +01:00
|
|
|
NewClosure(reply, SIGNAL(finished()), this,
|
|
|
|
SLOT(HandleSearchReply(QNetworkReply*, int)), reply, ctx->id);
|
2012-01-29 15:26:28 +01:00
|
|
|
}
|
|
|
|
|
2012-02-07 14:05:26 +01:00
|
|
|
// Parse the reply from a search
|
|
|
|
void DiscogsCoverProvider::HandleSearchReply(QNetworkReply* reply, int id) {
|
2012-01-29 15:26:28 +01:00
|
|
|
reply->deleteLater();
|
|
|
|
|
2012-02-07 14:05:26 +01:00
|
|
|
DiscogsCoverSearchContext* ctx;
|
|
|
|
if (!pending_requests_.contains(id)) {
|
|
|
|
// the request was cancelled while we were waiting for the reply
|
|
|
|
qLog(Debug) << "Discogs: got reply for cancelled request" << id;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ctx = pending_requests_.value(id);
|
|
|
|
|
2012-01-29 15:26:28 +01:00
|
|
|
QJson::Parser parser;
|
|
|
|
bool ok;
|
2012-02-07 14:05:26 +01:00
|
|
|
bool found = false;
|
2012-01-29 15:26:28 +01:00
|
|
|
QVariantMap reply_map = parser.parse(reply, &ok).toMap();
|
|
|
|
|
|
|
|
if (!ok || !reply_map.contains("results")) {
|
2012-02-07 14:05:26 +01:00
|
|
|
// this is an error; either parse error or bad response from the server
|
|
|
|
EndSearch(ctx);
|
2012-01-29 15:26:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariantList results = reply_map["results"].toList();
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
foreach(const QVariant & result, results) {
|
2012-01-29 15:26:28 +01:00
|
|
|
QVariantMap result_map = result.toMap();
|
2012-02-07 14:05:26 +01:00
|
|
|
// In order to use less round-trips, we cheat here. Instead of
|
|
|
|
// following the "resource_url", and then scan all images in the
|
|
|
|
// resource, we go directly to the largest primary image by
|
|
|
|
// constructing the primary image's url from the thmub's url.
|
|
|
|
if (result_map.contains("thumb")) {
|
|
|
|
CoverSearchResult cover_result;
|
2014-02-07 16:34:20 +01:00
|
|
|
cover_result.image_url =
|
|
|
|
QUrl(result_map["thumb"].toString().replace("R-90-", "R-"));
|
2012-02-07 14:05:26 +01:00
|
|
|
if (result_map.contains("title")) {
|
|
|
|
cover_result.description = result_map["title"].toString();
|
|
|
|
}
|
|
|
|
ctx->results.append(cover_result);
|
|
|
|
found = true;
|
2012-01-29 15:26:28 +01:00
|
|
|
}
|
|
|
|
}
|
2012-02-07 14:05:26 +01:00
|
|
|
if (found) {
|
|
|
|
EndSearch(ctx);
|
2012-01-29 15:26:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-02-07 14:05:26 +01:00
|
|
|
// otherwise, no results
|
|
|
|
switch (ctx->state) {
|
|
|
|
case DiscogsCoverSearchContext::State_MastersRequested:
|
|
|
|
// search again, this time for releases
|
|
|
|
SendSearchRequest(ctx);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
EndSearch(ctx);
|
|
|
|
break;
|
2012-01-29 15:26:28 +01:00
|
|
|
}
|
2012-02-07 14:05:26 +01:00
|
|
|
}
|
2012-01-29 15:26:28 +01:00
|
|
|
|
2012-02-07 14:05:26 +01:00
|
|
|
void DiscogsCoverProvider::EndSearch(DiscogsCoverSearchContext* ctx) {
|
2014-02-07 16:34:20 +01:00
|
|
|
(void)pending_requests_.remove(ctx->id);
|
2012-02-07 14:05:26 +01:00
|
|
|
emit SearchFinished(ctx->id, ctx->results);
|
|
|
|
delete ctx;
|
2012-01-29 15:26:28 +01:00
|
|
|
}
|