strawberry-audio-player-win.../src/musicbrainz/musicbrainzclient.h

224 lines
7.1 KiB
C
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
2021-03-20 21:14:47 +01:00
* Copyright 2019-2021, Jonas Kvinge <jonas@jkvinge.net>
2018-02-27 18:06:05 +01:00
*
* Strawberry 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.
*
* Strawberry 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 Strawberry. If not, see <http://www.gnu.org/licenses/>.
2018-08-09 18:39:44 +02:00
*
2018-02-27 18:06:05 +01:00
*/
#ifndef MUSICBRAINZCLIENT_H
#define MUSICBRAINZCLIENT_H
#include "config.h"
#include <QtGlobal>
#include <QObject>
#include <QList>
#include <QMap>
2018-02-27 18:06:05 +01:00
#include <QMultiMap>
2020-02-09 02:29:35 +01:00
#include <QVariant>
#include <QString>
#include <QStringList>
2018-02-27 18:06:05 +01:00
#include "core/shared_ptr.h"
2020-02-09 02:29:35 +01:00
class QNetworkReply;
class QTimer;
2020-02-09 02:29:35 +01:00
class QXmlStreamReader;
class NetworkAccessManager;
2018-02-27 18:06:05 +01:00
class NetworkTimeouts;
class MusicBrainzClient : public QObject {
Q_OBJECT
// Gets metadata for a particular MBID.
// An MBID is created from a fingerprint using MusicDnsClient.
// 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.
2018-02-27 18:06:05 +01:00
public:
// The second argument allows for specifying a custom network access manager.
// It is used in tests. The ownership of network is not transferred.
explicit MusicBrainzClient(SharedPtr<NetworkAccessManager> network, QObject *parent = nullptr);
2020-06-15 21:55:05 +02:00
~MusicBrainzClient() override;
2018-02-27 18:06:05 +01:00
struct Result {
Result() : duration_msec_(0), track_(0), year_(-1) {}
2021-06-12 20:53:23 +02:00
bool operator<(const Result &other) const {
2018-02-27 18:06:05 +01:00
#define cmp(field) \
2020-06-15 00:33:47 +02:00
if ((field) < other.field) return true; \
if ((field) > other.field) return false;
2018-02-27 18:06:05 +01:00
cmp(track_);
cmp(year_);
cmp(title_);
cmp(artist_);
return false;
#undef cmp
}
2021-06-12 20:53:23 +02:00
bool operator==(const Result &other) const {
2018-02-27 18:06:05 +01:00
return
title_ == other.title_ &&
artist_ == other.artist_ &&
album_ == other.album_ &&
duration_msec_ == other.duration_msec_ &&
track_ == other.track_ &&
year_ == other.year_;
}
QString title_;
QString artist_;
QString album_;
int duration_msec_;
int track_;
int year_;
};
2022-10-13 22:39:31 +02:00
using ResultList = QList<Result>;
2018-02-27 18:06:05 +01:00
// Starts a request and returns immediately. Finished() will be emitted later with the same ID.
void Start(const int id, const QStringList &mbid);
void StartDiscIdRequest(const QString &discid);
2018-02-27 18:06:05 +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.
2018-02-27 18:06:05 +01:00
void Cancel(int id);
// Cancels all requests. Finished() will never be emitted for any pending requests.
2018-02-27 18:06:05 +01:00
void CancelAll();
signals:
2018-02-27 18:06:05 +01:00
// Finished signal emitted when fechting songs tags
void Finished(const int id, const MusicBrainzClient::ResultList &result, const QString &error = QString());
2018-02-27 18:06:05 +01:00
// Finished signal emitted when fechting album's songs tags using DiscId
void DiscIdFinished(const QString &artist, const QString &album, const MusicBrainzClient::ResultList &result, const QString &error = QString());
2018-02-27 18:06:05 +01:00
private slots:
void FlushRequests();
// id identifies the track, and request_number means it's the 'request_number'th request for this track
2021-06-12 20:53:23 +02:00
void RequestFinished(QNetworkReply *reply, const int id, const int request_number);
void DiscIdRequestFinished(const QString &discid, QNetworkReply *reply);
2018-02-27 18:06:05 +01:00
private:
2022-10-13 22:39:31 +02:00
using Param = QPair<QString, QString>;
using ParamList = QList<Param>;
struct Request {
Request() : id(0), number(0) {}
Request(const int _id, const QString &_mbid, const int _number) : id(_id), mbid(_mbid), number(_number) {}
int id;
QString mbid;
int number;
};
2018-02-27 18:06:05 +01:00
// Used as parameter for UniqueResults
2023-02-18 14:09:27 +01:00
enum class UniqueResultsSortOption {
2018-02-27 18:06:05 +01:00
SortResults = 0,
KeepOriginalOrder
};
struct Release {
2023-02-18 14:09:27 +01:00
enum class Status {
Unknown = 0,
PseudoRelease,
Bootleg,
Promotional,
Official
2018-02-27 18:06:05 +01:00
};
2023-02-18 14:09:27 +01:00
Release() : track_(0), year_(0), status_(Status::Unknown) {}
2018-02-27 18:06:05 +01:00
2021-06-12 20:53:23 +02:00
Result CopyAndMergeInto(const Result &orig) const {
2018-02-27 18:06:05 +01:00
Result ret(orig);
ret.album_ = album_;
ret.track_ = track_;
ret.year_ = year_;
return ret;
}
2021-06-12 20:53:23 +02:00
void SetStatusFromString(const QString &s) {
2024-04-09 23:20:26 +02:00
if (s.compare(QLatin1String("Official"), Qt::CaseInsensitive) == 0) {
2023-02-18 14:09:27 +01:00
status_ = Status::Official;
2018-02-27 18:06:05 +01:00
}
2024-04-09 23:20:26 +02:00
else if (s.compare(QLatin1String("Promotion"), Qt::CaseInsensitive) == 0) {
2023-02-18 14:09:27 +01:00
status_ = Status::Promotional;
2018-02-27 18:06:05 +01:00
}
2024-04-09 23:20:26 +02:00
else if (s.compare(QLatin1String("Bootleg"), Qt::CaseInsensitive) == 0) {
2023-02-18 14:09:27 +01:00
status_ = Status::Bootleg;
2018-02-27 18:06:05 +01:00
}
2024-04-09 23:20:26 +02:00
else if (s.compare(QLatin1String("Pseudo-release"), Qt::CaseInsensitive) == 0) {
2023-02-18 14:09:27 +01:00
status_ = Status::PseudoRelease;
2018-02-27 18:06:05 +01:00
}
else {
2023-02-18 14:09:27 +01:00
status_ = Status::Unknown;
2018-02-27 18:06:05 +01:00
}
}
2021-06-12 20:53:23 +02:00
bool operator<(const Release &other) const {
// Compare status so that "best" status (e.g. Official) will be first when sorting a list of releases.
2018-02-27 18:06:05 +01:00
return status_ > other.status_;
}
QString album_;
int track_;
int year_;
Status status_;
};
struct PendingResults {
2021-06-12 20:53:23 +02:00
PendingResults(int sort_id, const ResultList &results) : sort_id_(sort_id), results_(results) {}
2018-02-27 18:06:05 +01:00
2021-06-12 20:53:23 +02:00
bool operator<(const PendingResults &other) const {
2018-02-27 18:06:05 +01:00
return sort_id_ < other.sort_id_;
}
int sort_id_;
ResultList results_;
};
2021-06-22 13:41:38 +02:00
static QByteArray GetReplyData(QNetworkReply *reply, QString &error);
2021-06-12 20:53:23 +02:00
static bool MediumHasDiscid(const QString &discid, QXmlStreamReader *reader);
static ResultList ParseMedium(QXmlStreamReader *reader);
static Result ParseTrackFromDisc(QXmlStreamReader *reader);
static ResultList ParseTrack(QXmlStreamReader *reader);
static void ParseArtist(QXmlStreamReader *reader, QString *artist);
static Release ParseRelease(QXmlStreamReader *reader);
2023-02-18 14:09:27 +01:00
static ResultList UniqueResults(const ResultList &results, UniqueResultsSortOption opt = UniqueResultsSortOption::SortResults);
2021-06-22 13:41:38 +02:00
static void Error(const QString &error, const QVariant &debug = QVariant());
2018-02-27 18:06:05 +01:00
private:
SharedPtr<NetworkAccessManager> network_;
2021-06-12 20:53:23 +02:00
NetworkTimeouts *timeouts_;
2019-08-04 16:02:22 +02:00
QMultiMap<int, Request> requests_pending_;
2018-02-27 18:06:05 +01:00
QMultiMap<int, QNetworkReply*> requests_;
// Results we received so far, kept here until all the replies are finished
QMap<int, QList<PendingResults>> pending_results_;
QTimer *timer_flush_requests_;
2018-02-27 18:06:05 +01:00
};
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
2021-06-12 20:53:23 +02:00
inline size_t qHash(const MusicBrainzClient::Result &result) {
#else
2021-06-12 20:53:23 +02:00
inline uint qHash(const MusicBrainzClient::Result &result) {
#endif
return qHash(result.album_) ^ qHash(result.artist_) ^ result.duration_msec_ ^ qHash(result.title_) ^ result.track_ ^ result.year_;
2018-02-27 18:06:05 +01:00
}
#endif // MUSICBRAINZCLIENT_H