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

144 lines
4.2 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 2018-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 ALBUMCOVERFETCHER_H
#define ALBUMCOVERFETCHER_H
#include "config.h"
#include <QtGlobal>
2018-02-27 18:06:05 +01:00
#include <QObject>
#include <QMetaType>
#include <QSet>
2020-02-08 15:03:11 +01:00
#include <QList>
#include <QHash>
#include <QQueue>
#include <QByteArray>
#include <QString>
2018-02-27 18:06:05 +01:00
#include <QUrl>
#include <QImage>
2018-02-27 18:06:05 +01:00
2021-01-26 16:48:04 +01:00
#include "coversearchstatistics.h"
#include "albumcoverimageresult.h"
2021-01-26 16:48:04 +01:00
2020-02-08 15:03:11 +01:00
class QTimer;
2020-12-09 18:39:37 +01:00
class NetworkAccessManager;
2018-02-27 18:06:05 +01:00
class CoverProviders;
class AlbumCoverFetcherSearch;
2018-02-27 18:06:05 +01:00
// This class represents a single search-for-cover request. It identifies and describes the request.
2018-02-27 18:06:05 +01:00
struct CoverSearchRequest {
2021-10-30 17:10:05 +02:00
explicit CoverSearchRequest() : id(0), search(false), batch(false) {}
// An unique (for one AlbumCoverFetcher) request identifier
2018-02-27 18:06:05 +01:00
quint64 id;
// A search query
2018-02-27 18:06:05 +01:00
QString artist;
QString album;
QString title;
2018-02-27 18:06:05 +01:00
// Is this only a search request or should we also fetch the first cover that's found?
2018-02-27 18:06:05 +01:00
bool search;
2018-03-17 14:28:45 +01:00
// Is the request part of a batch (fetching all missing covers)
bool batch;
2018-02-27 18:06:05 +01:00
};
// This structure represents a single result of some album's cover search request.
struct CoverProviderSearchResult {
explicit CoverProviderSearchResult() : score_provider(0.0), score_match(0.0), score_quality(0.0), number(0) {}
2019-04-17 22:18:03 +02:00
// Used for grouping in the user interface.
2018-02-27 18:06:05 +01:00
QString provider;
2019-04-17 22:18:03 +02:00
// Artist and album returned by the provider
QString artist;
QString album;
2018-02-27 18:06:05 +01:00
2019-04-17 22:18:03 +02:00
// An URL of a cover image
2018-02-27 18:06:05 +01:00
QUrl image_url;
2019-04-17 22:18:03 +02:00
// Image size
QSize image_size;
// Score for this provider
float score_provider;
// Score for match
float score_match;
// Score for image quality
float score_quality;
// The result number
int number;
2019-04-17 22:18:03 +02:00
// Total score for this result
float score() const { return score_provider + score_match + score_quality; }
2019-04-17 22:18:03 +02:00
2018-02-27 18:06:05 +01:00
};
Q_DECLARE_METATYPE(CoverProviderSearchResult)
2018-02-27 18:06:05 +01:00
// This is a complete result of a single search request (a list of results, each describing one image, actually).
typedef QList<CoverProviderSearchResult> CoverProviderSearchResults;
Q_DECLARE_METATYPE(QList<CoverProviderSearchResult>)
2018-02-27 18:06:05 +01:00
// This class searches for album covers for a given query or artist/album and returns URLs. It's NOT thread-safe.
2018-02-27 18:06:05 +01:00
class AlbumCoverFetcher : public QObject {
Q_OBJECT
public:
2020-12-09 18:39:37 +01:00
explicit AlbumCoverFetcher(CoverProviders *cover_providers, QObject *parent = nullptr, NetworkAccessManager *network = nullptr);
2020-06-15 21:55:05 +02:00
~AlbumCoverFetcher() override;
2018-02-27 18:06:05 +01:00
static const int kMaxConcurrentRequests;
quint64 SearchForCovers(const QString &artist, const QString &album, const QString &title = QString());
quint64 FetchAlbumCover(const QString &artist, const QString &album, const QString &title, const bool batch);
2018-02-27 18:06:05 +01:00
void Clear();
signals:
void AlbumCoverFetched(quint64 request_id, AlbumCoverImageResult result, CoverSearchStatistics statistics);
void SearchFinished(quint64 request_id, CoverProviderSearchResults results, CoverSearchStatistics statistics);
2018-02-27 18:06:05 +01:00
private slots:
void SingleSearchFinished(const quint64, const CoverProviderSearchResults &results);
void SingleCoverFetched(const quint64, const AlbumCoverImageResult &result);
2018-02-27 18:06:05 +01:00
void StartRequests();
private:
void AddRequest(const CoverSearchRequest &req);
CoverProviders *cover_providers_;
2020-12-09 18:39:37 +01:00
NetworkAccessManager *network_;
2018-02-27 18:06:05 +01:00
quint64 next_id_;
QQueue<CoverSearchRequest> queued_requests_;
QHash<quint64, AlbumCoverFetcherSearch*> active_requests_;
QTimer *request_starter_;
2018-10-02 00:38:52 +02:00
2018-02-27 18:06:05 +01:00
};
#endif // ALBUMCOVERFETCHER_H