~ubuntu-branches/ubuntu/saucy/clementine/saucy

« back to all changes in this revision

Viewing changes to src/musicbrainz/acoustidclient.cpp

  • Committer: Package Import Robot
  • Author(s): Thomas PIERSON
  • Date: 2012-01-01 20:43:39 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120101204339-lsb6nndwhfy05sde
Tags: 1.0.1+dfsg-1
New upstream release. (Closes: #653926, #651611, #657391)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of Clementine.
 
2
   Copyright 2010, David Sansome <me@davidsansome.com>
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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/>.
 
16
*/
 
17
 
 
18
#include "acoustidclient.h"
 
19
 
 
20
#include <QCoreApplication>
 
21
#include <QNetworkReply>
 
22
 
 
23
#include <qjson/parser.h>
 
24
 
 
25
#include "core/logging.h"
 
26
#include "core/network.h"
 
27
#include "core/timeconstants.h"
 
28
 
 
29
const char* AcoustidClient::kClientId = "qsZGpeLx";
 
30
const char* AcoustidClient::kUrl = "http://api.acoustid.org/v2/lookup";
 
31
const int AcoustidClient::kDefaultTimeout = 5000; // msec
 
32
 
 
33
AcoustidClient::AcoustidClient(QObject* parent)
 
34
  : QObject(parent),
 
35
    network_(new NetworkAccessManager(this)),
 
36
    timeouts_(new NetworkTimeouts(kDefaultTimeout, this))
 
37
{
 
38
}
 
39
 
 
40
void AcoustidClient::SetTimeout(int msec) {
 
41
  timeouts_->SetTimeout(msec);
 
42
}
 
43
 
 
44
void AcoustidClient::Start(int id, const QString& fingerprint, int duration_msec) {
 
45
  typedef QPair<QString, QString> Param;
 
46
 
 
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);
 
53
 
 
54
  QUrl url(kUrl);
 
55
  url.setQueryItems(parameters);
 
56
  QNetworkRequest req(url);
 
57
 
 
58
  QNetworkReply* reply = network_->get(req);
 
59
  connect(reply, SIGNAL(finished()), SLOT(RequestFinished()));
 
60
  requests_[reply] = id;
 
61
 
 
62
  timeouts_->AddReply(reply);
 
63
}
 
64
 
 
65
void AcoustidClient::Cancel(int id) {
 
66
  QNetworkReply* reply = requests_.key(id);
 
67
  requests_.remove(reply);
 
68
  delete reply;
 
69
}
 
70
 
 
71
void AcoustidClient::CancelAll() {
 
72
  qDeleteAll(requests_.keys());
 
73
  requests_.clear();
 
74
}
 
75
 
 
76
void AcoustidClient::RequestFinished() {
 
77
  QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
 
78
  if (!reply)
 
79
    return;
 
80
 
 
81
  reply->deleteLater();
 
82
  if (!requests_.contains(reply))
 
83
    return;
 
84
 
 
85
  int id = requests_.take(reply);
 
86
 
 
87
  if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
 
88
    emit Finished(id, QString());
 
89
    return;
 
90
  }
 
91
 
 
92
  QJson::Parser parser;
 
93
  bool ok = false;
 
94
  QVariantMap result = parser.parse(reply, &ok).toMap();
 
95
  if (!ok) {
 
96
    emit Finished(id, QString());
 
97
    return;
 
98
  }
 
99
 
 
100
  QString status = result["status"].toString();
 
101
  if (status != "ok") {
 
102
    emit Finished(id, QString());
 
103
    return;
 
104
  }
 
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());
 
114
          return;
 
115
        }
 
116
      }
 
117
    }
 
118
  }
 
119
 
 
120
  emit Finished(id, QString());
 
121
}