2011-03-12 22:19:41 +01:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2010, David Sansome <me@davidsansome.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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MUSICBRAINZCLIENT_H
|
|
|
|
#define MUSICBRAINZCLIENT_H
|
|
|
|
|
2012-01-07 22:51:02 +01:00
|
|
|
#include <QHash>
|
2014-08-09 01:48:35 +02:00
|
|
|
#include <QMultiMap>
|
2011-03-12 22:19:41 +01:00
|
|
|
#include <QObject>
|
2011-03-12 23:29:08 +01:00
|
|
|
#include <QXmlStreamReader>
|
2011-03-12 22:19:41 +01:00
|
|
|
|
2011-03-13 14:17:35 +01:00
|
|
|
class NetworkTimeouts;
|
|
|
|
|
2011-03-12 22:19:41 +01:00
|
|
|
class QNetworkAccessManager;
|
|
|
|
class QNetworkReply;
|
|
|
|
|
|
|
|
class MusicBrainzClient : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
2012-01-06 17:31:29 +01:00
|
|
|
// Gets metadata for a particular MBID.
|
|
|
|
// An MBID is created from a fingerprint using MusicDnsClient.
|
2011-03-12 23:29:03 +01:00
|
|
|
// You can create one MusicBrainzClient and make multiple requests using it.
|
|
|
|
// IDs are provided by the caller when a request is started and included in
|
|
|
|
// the Finished signal - they have no meaning to MusicBrainzClient.
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
public:
|
2014-05-24 13:33:32 +02:00
|
|
|
// The second argument allows for specifying a custom network access
|
2014-06-02 16:34:03 +02:00
|
|
|
// manager. It is used in tests. The ownership of network
|
|
|
|
// is not transferred.
|
2014-05-24 13:33:32 +02:00
|
|
|
MusicBrainzClient(QObject* parent = nullptr,
|
|
|
|
QNetworkAccessManager* network = nullptr);
|
2011-03-12 22:19:41 +01:00
|
|
|
|
|
|
|
struct Result {
|
2012-01-07 22:51:02 +01:00
|
|
|
Result() : duration_msec_(0), track_(0), year_(-1) {}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
bool operator<(const Result& other) const {
|
|
|
|
#define cmp(field) \
|
|
|
|
if (field < other.field) return true; \
|
|
|
|
if (field > other.field) return false;
|
2012-01-07 22:51:02 +01:00
|
|
|
|
|
|
|
cmp(track_);
|
|
|
|
cmp(year_);
|
|
|
|
cmp(title_);
|
|
|
|
cmp(artist_);
|
|
|
|
return false;
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
#undef cmp
|
2012-01-07 22:51:02 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
bool operator==(const Result& other) const {
|
|
|
|
return title_ == other.title_ && artist_ == other.artist_ &&
|
|
|
|
album_ == other.album_ && duration_msec_ == other.duration_msec_ &&
|
|
|
|
track_ == other.track_ && year_ == other.year_;
|
2012-01-07 22:51:02 +01:00
|
|
|
}
|
2011-03-12 22:19:41 +01:00
|
|
|
|
|
|
|
QString title_;
|
|
|
|
QString artist_;
|
|
|
|
QString album_;
|
|
|
|
int duration_msec_;
|
|
|
|
int track_;
|
2012-01-07 22:51:02 +01:00
|
|
|
int year_;
|
2011-03-12 22:19:41 +01:00
|
|
|
};
|
|
|
|
typedef QList<Result> ResultList;
|
|
|
|
|
2011-03-12 23:29:03 +01:00
|
|
|
// Starts a request and returns immediately. Finished() will be emitted
|
|
|
|
// later with the same ID.
|
2014-08-09 01:48:35 +02:00
|
|
|
void Start(int id, const QStringList& mbid);
|
2012-01-06 17:31:29 +01:00
|
|
|
void StartDiscIdRequest(const QString& discid);
|
2011-03-12 23:29:03 +01:00
|
|
|
|
|
|
|
// Cancels the request with the given ID. Finished() will never be emitted
|
|
|
|
// for that ID. Does nothing if there is no request with the given ID.
|
2011-03-12 22:19:41 +01:00
|
|
|
void Cancel(int id);
|
2011-03-12 23:29:03 +01:00
|
|
|
|
|
|
|
// Cancels all requests. Finished() will never be emitted for any pending
|
|
|
|
// requests.
|
2011-03-12 22:19:41 +01:00
|
|
|
void CancelAll();
|
|
|
|
|
2020-09-18 16:15:19 +02:00
|
|
|
signals:
|
2011-06-10 01:08:43 +02:00
|
|
|
// Finished signal emitted when fechting songs tags
|
2011-03-12 22:19:41 +01:00
|
|
|
void Finished(int id, const MusicBrainzClient::ResultList& result);
|
2011-06-10 01:08:43 +02:00
|
|
|
// Finished signal emitted when fechting album's songs tags using DiscId
|
|
|
|
void Finished(const QString& artist, const QString album,
|
|
|
|
const MusicBrainzClient::ResultList& result);
|
2011-03-12 22:19:41 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
private slots:
|
2014-08-09 01:48:35 +02:00
|
|
|
// id identifies the track, and request_number means it's the
|
|
|
|
// 'request_number'th request for this track
|
|
|
|
void RequestFinished(QNetworkReply* reply, int id, int request_number);
|
2014-05-24 13:33:32 +02:00
|
|
|
void DiscIdRequestFinished(const QString& discid, QNetworkReply* reply);
|
2011-03-12 22:19:41 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
private:
|
2014-08-10 14:22:42 +02:00
|
|
|
// Used as parameter for UniqueResults
|
2020-09-18 16:15:19 +02:00
|
|
|
enum UniqueResultsSortOption { SortResults = 0, KeepOriginalOrder };
|
2014-08-10 14:22:42 +02:00
|
|
|
|
2012-01-07 22:51:02 +01:00
|
|
|
struct Release {
|
2014-12-06 17:54:13 +01:00
|
|
|
enum Status {
|
|
|
|
Status_Unknown = 0,
|
|
|
|
Status_PseudoRelease,
|
|
|
|
Status_Bootleg,
|
|
|
|
Status_Promotional,
|
|
|
|
Status_Official
|
|
|
|
};
|
|
|
|
|
|
|
|
Release() : track_(0), year_(0), status_(Status_Unknown) {}
|
2012-01-07 22:51:02 +01:00
|
|
|
|
|
|
|
Result CopyAndMergeInto(const Result& orig) const {
|
|
|
|
Result ret(orig);
|
|
|
|
ret.album_ = album_;
|
|
|
|
ret.track_ = track_;
|
|
|
|
ret.year_ = year_;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-12-06 17:54:13 +01:00
|
|
|
void SetStatusFromString(const QString& s) {
|
|
|
|
if (s.compare("Official", Qt::CaseInsensitive) == 0) {
|
|
|
|
status_ = Status_Official;
|
|
|
|
} else if (s.compare("Promotion", Qt::CaseInsensitive) == 0) {
|
|
|
|
status_ = Status_Promotional;
|
|
|
|
} else if (s.compare("Bootleg", Qt::CaseInsensitive) == 0) {
|
|
|
|
status_ = Status_Bootleg;
|
|
|
|
} else if (s.compare("Pseudo-release", Qt::CaseInsensitive) == 0) {
|
|
|
|
status_ = Status_PseudoRelease;
|
|
|
|
} else {
|
|
|
|
status_ = Status_Unknown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const Release& other) const {
|
|
|
|
// Compare status so that "best" status (e.g. Official) will be first
|
|
|
|
// when sorting a list of releases.
|
|
|
|
return status_ > other.status_;
|
|
|
|
}
|
|
|
|
|
2012-01-07 22:51:02 +01:00
|
|
|
QString album_;
|
|
|
|
int track_;
|
|
|
|
int year_;
|
2014-12-06 17:54:13 +01:00
|
|
|
Status status_;
|
2012-01-07 22:51:02 +01:00
|
|
|
};
|
|
|
|
|
2014-08-09 01:48:35 +02:00
|
|
|
struct PendingResults {
|
|
|
|
PendingResults(int sort_id, const ResultList& results)
|
2020-09-18 16:15:19 +02:00
|
|
|
: sort_id_(sort_id), results_(results) {}
|
2014-08-09 01:48:35 +02:00
|
|
|
|
|
|
|
bool operator<(const PendingResults& other) const {
|
|
|
|
return sort_id_ < other.sort_id_;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sort_id_;
|
|
|
|
ResultList results_;
|
|
|
|
};
|
|
|
|
|
2014-05-24 13:33:32 +02:00
|
|
|
static bool MediumHasDiscid(const QString& discid, QXmlStreamReader* reader);
|
|
|
|
static ResultList ParseMedium(QXmlStreamReader* reader);
|
|
|
|
static Result ParseTrackFromDisc(QXmlStreamReader* reader);
|
2012-01-07 22:51:02 +01:00
|
|
|
static ResultList ParseTrack(QXmlStreamReader* reader);
|
2011-03-12 22:19:41 +01:00
|
|
|
static void ParseArtist(QXmlStreamReader* reader, QString* artist);
|
2012-01-07 22:51:02 +01:00
|
|
|
static Release ParseRelease(QXmlStreamReader* reader);
|
2014-08-10 14:22:42 +02:00
|
|
|
static ResultList UniqueResults(const ResultList& results,
|
2020-09-18 16:15:19 +02:00
|
|
|
UniqueResultsSortOption opt = SortResults);
|
2011-03-12 22:19:41 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
private:
|
2011-06-10 01:08:43 +02:00
|
|
|
static const char* kTrackUrl;
|
|
|
|
static const char* kDiscUrl;
|
2012-01-07 22:51:02 +01:00
|
|
|
static const char* kDateRegex;
|
2011-03-13 14:17:35 +01:00
|
|
|
static const int kDefaultTimeout;
|
2014-08-11 00:37:02 +02:00
|
|
|
static const int kMaxRequestPerTrack;
|
2011-03-12 22:19:41 +01:00
|
|
|
|
|
|
|
QNetworkAccessManager* network_;
|
2011-03-13 14:17:35 +01:00
|
|
|
NetworkTimeouts* timeouts_;
|
2014-08-09 01:48:35 +02:00
|
|
|
QMultiMap<int, QNetworkReply*> requests_;
|
|
|
|
// Results we received so far, kept here until all the replies are finished
|
|
|
|
QMap<int, QList<PendingResults>> pending_results_;
|
2011-03-12 22:19:41 +01:00
|
|
|
};
|
|
|
|
|
2012-01-07 22:51:02 +01:00
|
|
|
inline uint qHash(const MusicBrainzClient::Result& result) {
|
2014-02-07 16:34:20 +01:00
|
|
|
return qHash(result.album_) ^ qHash(result.artist_) ^ result.duration_msec_ ^
|
|
|
|
qHash(result.title_) ^ result.track_ ^ result.year_;
|
2012-01-07 22:51:02 +01:00
|
|
|
}
|
2014-02-07 16:34:20 +01:00
|
|
|
#endif // MUSICBRAINZCLIENT_H
|