strawberry-audio-player-win.../src/covermanager/discogscoverprovider.h

114 lines
3.9 KiB
C
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2012, Martin Björklund <mbj4668@gmail.com>
2021-03-20 21:14:47 +01:00
* Copyright 2016-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 DISCOGSCOVERPROVIDER_H
#define DISCOGSCOVERPROVIDER_H
#include "config.h"
2020-04-23 21:02:48 +02:00
#include <memory>
#include <QObject>
#include <QMetaType>
#include <QPair>
#include <QList>
#include <QQueue>
2020-04-23 21:02:48 +02:00
#include <QMap>
2020-02-09 02:29:35 +01:00
#include <QVariant>
#include <QByteArray>
#include <QString>
2020-02-09 02:29:35 +01:00
#include <QJsonObject>
2018-02-27 18:06:05 +01:00
2020-05-10 12:49:11 +02:00
#include "jsoncoverprovider.h"
#include "albumcoverfetcher.h"
2018-02-27 18:06:05 +01:00
2020-12-09 18:39:37 +01:00
class NetworkAccessManager;
2020-02-09 02:29:35 +01:00
class QNetworkReply;
class QTimer;
2019-04-14 16:40:05 +02:00
class Application;
2020-05-10 12:49:11 +02:00
class DiscogsCoverProvider : public JsonCoverProvider {
2018-02-27 18:06:05 +01:00
Q_OBJECT
public:
2021-08-12 23:00:10 +02:00
explicit DiscogsCoverProvider(Application *app, NetworkAccessManager *network, QObject *parent = nullptr);
2020-06-15 21:55:05 +02:00
~DiscogsCoverProvider() override;
2018-02-27 18:06:05 +01:00
2020-06-15 21:55:05 +02:00
bool StartSearch(const QString &artist, const QString &album, const QString &title, const int id) override;
void CancelSearch(const int id) override;
2018-02-27 18:06:05 +01:00
enum DiscogsCoverType {
DiscogsCoverType_Master,
DiscogsCoverType_Release,
};
2020-04-23 21:02:48 +02:00
struct DiscogsCoverReleaseContext {
explicit DiscogsCoverReleaseContext(const quint64 _search_id = 0, const quint64 _id = 0, const QUrl &_url = QUrl()) : search_id(_search_id), id(_id), url(_url) {}
quint64 search_id;
2020-04-23 21:02:48 +02:00
quint64 id;
QUrl url;
};
struct DiscogsCoverSearchContext {
explicit DiscogsCoverSearchContext(const int _id = 0, const QString &_artist = QString(), const QString &_album = QString(), const DiscogsCoverType _type = DiscogsCoverType_Master) : id(_id), artist(_artist), album(_album), type(_type) {}
2020-04-23 21:02:48 +02:00
int id;
QString artist;
QString album;
DiscogsCoverType type;
2020-04-23 21:02:48 +02:00
QMap<quint64, DiscogsCoverReleaseContext> requests_release_;
CoverProviderSearchResults results;
2020-04-23 21:02:48 +02:00
};
private:
typedef QPair<QString, QString> Param;
typedef QList<Param> ParamList;
void SendSearchRequest(std::shared_ptr<DiscogsCoverSearchContext> search);
2021-06-20 19:04:08 +02:00
void SendReleaseRequest(const DiscogsCoverReleaseContext &release);
2020-04-23 21:02:48 +02:00
QNetworkReply *CreateRequest(QUrl url, const ParamList &params_provided = ParamList());
QByteArray GetReplyData(QNetworkReply *reply);
void StartReleaseRequest(std::shared_ptr<DiscogsCoverSearchContext> search, const quint64 release_id, const QUrl &url);
void EndSearch(std::shared_ptr<DiscogsCoverSearchContext> search, const quint64 release_id = 0);
2020-06-15 21:55:05 +02:00
void Error(const QString &error, const QVariant &debug = QVariant()) override;
2018-02-27 18:06:05 +01:00
private slots:
void FlushRequests();
void HandleSearchReply(QNetworkReply *reply, const quint64 id);
void HandleReleaseReply(QNetworkReply *reply, const quint64 id, const quint64 release_id);
2018-02-27 18:06:05 +01:00
private:
2018-03-17 14:28:45 +01:00
static const char *kUrlSearch;
static const char *kAccessKeyB64;
static const char *kSecretKeyB64;
static const int kRequestsDelay;
2018-03-17 14:28:45 +01:00
QTimer *timer_flush_requests_;
QQueue<std::shared_ptr<DiscogsCoverSearchContext>> queue_search_requests_;
QQueue<DiscogsCoverReleaseContext> queue_release_requests_;
2020-04-23 21:02:48 +02:00
QMap<int, std::shared_ptr<DiscogsCoverSearchContext>> requests_search_;
QList<QNetworkReply*> replies_;
2018-02-27 18:06:05 +01:00
};
2020-04-23 21:02:48 +02:00
Q_DECLARE_METATYPE(DiscogsCoverProvider::DiscogsCoverSearchContext)
Q_DECLARE_METATYPE(DiscogsCoverProvider::DiscogsCoverReleaseContext)
2018-02-27 18:06:05 +01:00
#endif // DISCOGSCOVERPROVIDER_H