1
/* This file is part of Clementine.
2
Copyright 2010, David Sansome <me@davidsansome.com>
4
Clementine is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation, either version 3 of the License, or
7
(at your option) any later version.
9
Clementine is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
18
#include "acoustidclient.h"
20
#include <QCoreApplication>
21
#include <QNetworkReply>
23
#include <qjson/parser.h>
25
#include "core/logging.h"
26
#include "core/network.h"
27
#include "core/timeconstants.h"
29
const char* AcoustidClient::kClientId = "qsZGpeLx";
30
const char* AcoustidClient::kUrl = "http://api.acoustid.org/v2/lookup";
31
const int AcoustidClient::kDefaultTimeout = 5000; // msec
33
AcoustidClient::AcoustidClient(QObject* parent)
35
network_(new NetworkAccessManager(this)),
36
timeouts_(new NetworkTimeouts(kDefaultTimeout, this))
40
void AcoustidClient::SetTimeout(int msec) {
41
timeouts_->SetTimeout(msec);
44
void AcoustidClient::Start(int id, const QString& fingerprint, int duration_msec) {
45
typedef QPair<QString, QString> Param;
47
QList<Param> parameters;
48
parameters << Param("format", "json")
49
<< Param("client", kClientId)
50
<< Param("duration", QString::number(duration_msec / kMsecPerSec))
51
<< Param("meta", "recordingids")
52
<< Param("fingerprint", fingerprint);
55
url.setQueryItems(parameters);
56
QNetworkRequest req(url);
58
QNetworkReply* reply = network_->get(req);
59
connect(reply, SIGNAL(finished()), SLOT(RequestFinished()));
60
requests_[reply] = id;
62
timeouts_->AddReply(reply);
65
void AcoustidClient::Cancel(int id) {
66
QNetworkReply* reply = requests_.key(id);
67
requests_.remove(reply);
71
void AcoustidClient::CancelAll() {
72
qDeleteAll(requests_.keys());
76
void AcoustidClient::RequestFinished() {
77
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
82
if (!requests_.contains(reply))
85
int id = requests_.take(reply);
87
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
88
emit Finished(id, QString());
94
QVariantMap result = parser.parse(reply, &ok).toMap();
96
emit Finished(id, QString());
100
QString status = result["status"].toString();
101
if (status != "ok") {
102
emit Finished(id, QString());
105
QVariantList results = result["results"].toList();
106
foreach (const QVariant& v, results) {
107
QVariantMap r = v.toMap();
108
if (r.contains("recordings")) {
109
QVariantList recordings = r["recordings"].toList();
110
foreach (const QVariant& recording, recordings) {
111
QVariantMap o = recording.toMap();
112
if (o.contains("id")) {
113
emit Finished(id, o["id"].toString());
120
emit Finished(id, QString());