1
0
mirror of https://github.com/clementine-player/Clementine synced 2024-12-17 03:45:56 +01:00

Port musicbrainz client to closures.

This commit is contained in:
John Maguire 2012-10-12 14:36:14 +02:00
parent aa7b22c103
commit f292a3bf98
2 changed files with 18 additions and 28 deletions

View File

@ -16,15 +16,16 @@
*/ */
#include "musicbrainzclient.h" #include "musicbrainzclient.h"
#include "core/logging.h"
#include "core/network.h"
#include "core/utilities.h"
#include <QCoreApplication> #include <QCoreApplication>
#include <QNetworkReply> #include <QNetworkReply>
#include <QSet> #include <QSet>
#include <QXmlStreamReader> #include <QXmlStreamReader>
#include <QtDebug>
#include "core/closure.h"
#include "core/logging.h"
#include "core/network.h"
#include "core/utilities.h"
const char* MusicBrainzClient::kTrackUrl = "http://musicbrainz.org/ws/2/recording/"; const char* MusicBrainzClient::kTrackUrl = "http://musicbrainz.org/ws/2/recording/";
const char* MusicBrainzClient::kDiscUrl = "http://musicbrainz.org/ws/1/release/"; const char* MusicBrainzClient::kDiscUrl = "http://musicbrainz.org/ws/1/release/";
@ -49,8 +50,9 @@ void MusicBrainzClient::Start(int id, const QString& mbid) {
QNetworkRequest req(url); QNetworkRequest req(url);
QNetworkReply* reply = network_->get(req); QNetworkReply* reply = network_->get(req);
connect(reply, SIGNAL(finished()), SLOT(RequestFinished())); NewClosure(reply, SIGNAL(finished()), this,
requests_[reply] = id; SLOT(RequestFinished(QNetworkReply*, int)), reply, id);
requests_[id] = reply;
timeouts_->AddReply(reply); timeouts_->AddReply(reply);
} }
@ -67,27 +69,22 @@ void MusicBrainzClient::StartDiscIdRequest(const QString& discid) {
QNetworkRequest req(url); QNetworkRequest req(url);
QNetworkReply* reply = network_->get(req); QNetworkReply* reply = network_->get(req);
connect(reply, SIGNAL(finished()), SLOT(DiscIdRequestFinished())); NewClosure(reply, SIGNAL(finished()), this,
//requests_[reply] = id; SLOT(DiscIdRequestFinished(QNetworkReply*)), reply);
timeouts_->AddReply(reply); timeouts_->AddReply(reply);
} }
void MusicBrainzClient::Cancel(int id) { void MusicBrainzClient::Cancel(int id) {
QNetworkReply* reply = requests_.key(id); delete requests_.take(id);
requests_.remove(reply);
delete reply;
} }
void MusicBrainzClient::CancelAll() { void MusicBrainzClient::CancelAll() {
qDeleteAll(requests_.keys()); qDeleteAll(requests_.values());
requests_.clear(); requests_.clear();
} }
void MusicBrainzClient::DiscIdRequestFinished() { void MusicBrainzClient::DiscIdRequestFinished(QNetworkReply* reply) {
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
if (!reply)
return;
reply->deleteLater(); reply->deleteLater();
ResultList ret; ResultList ret;
@ -136,16 +133,9 @@ void MusicBrainzClient::DiscIdRequestFinished() {
} }
void MusicBrainzClient::RequestFinished() { void MusicBrainzClient::RequestFinished(QNetworkReply* reply, int id) {
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
if (!reply)
return;
reply->deleteLater(); reply->deleteLater();
if (!requests_.contains(reply)) requests_.remove(id);
return;
int id = requests_.take(reply);
ResultList ret; ResultList ret;
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) { if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {

View File

@ -97,8 +97,8 @@ signals:
const MusicBrainzClient::ResultList& result); const MusicBrainzClient::ResultList& result);
private slots: private slots:
void RequestFinished(); void RequestFinished(QNetworkReply* reply, int id);
void DiscIdRequestFinished(); void DiscIdRequestFinished(QNetworkReply* reply);
private: private:
struct Release { struct Release {
@ -130,7 +130,7 @@ private:
QNetworkAccessManager* network_; QNetworkAccessManager* network_;
NetworkTimeouts* timeouts_; NetworkTimeouts* timeouts_;
QMap<QNetworkReply*, int> requests_; QMap<int, QNetworkReply*> requests_;
}; };
inline uint qHash(const MusicBrainzClient::Result& result) { inline uint qHash(const MusicBrainzClient::Result& result) {