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"
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QtGlobal>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QObject>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QMetaType>
|
|
|
|
#include <QSet>
|
2020-02-08 15:03:11 +01:00
|
|
|
#include <QList>
|
|
|
|
#include <QHash>
|
|
|
|
#include <QQueue>
|
2021-02-26 21:03:51 +01:00
|
|
|
#include <QByteArray>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QString>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QUrl>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QImage>
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
#include "coversearchstatistics.h"
|
2021-02-26 21:03:51 +01:00
|
|
|
#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;
|
2018-05-01 00:41:33 +02:00
|
|
|
class AlbumCoverFetcherSearch;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-03-05 21:05:30 +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) {}
|
2019-07-07 21:14:24 +02:00
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
// An unique (for one AlbumCoverFetcher) request identifier
|
2018-02-27 18:06:05 +01:00
|
|
|
quint64 id;
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
// A search query
|
2018-02-27 18:06:05 +01:00
|
|
|
QString artist;
|
|
|
|
QString album;
|
2020-04-20 18:03:18 +02:00
|
|
|
QString title;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-05-01 00:41:33 +02: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
|
|
|
|
2021-02-26 21:03:51 +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.
|
2021-02-26 21:03:51 +01:00
|
|
|
struct CoverProviderSearchResult {
|
|
|
|
explicit CoverProviderSearchResult() : score_provider(0.0), score_match(0.0), score_quality(0.0), number(0) {}
|
2019-07-07 21:14:24 +02:00
|
|
|
|
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
|
|
|
|
2020-08-09 20:10:53 +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
|
2020-08-09 20:10:53 +02:00
|
|
|
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
|
|
|
};
|
2021-02-26 21:03:51 +01:00
|
|
|
Q_DECLARE_METATYPE(CoverProviderSearchResult)
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
// This is a complete result of a single search request (a list of results, each describing one image, actually).
|
2022-10-13 22:39:31 +02:00
|
|
|
using CoverProviderSearchResults = QList<CoverProviderSearchResult>;
|
2023-02-18 14:09:27 +01:00
|
|
|
Q_DECLARE_METATYPE(CoverProviderSearchResults)
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-05-01 00:41:33 +02: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:
|
2023-04-21 20:20:53 +02:00
|
|
|
explicit AlbumCoverFetcher(CoverProviders *cover_providers, NetworkAccessManager *network, QObject *parent = nullptr);
|
2020-06-15 21:55:05 +02:00
|
|
|
~AlbumCoverFetcher() override;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
static const int kMaxConcurrentRequests;
|
|
|
|
|
2020-04-20 18:03:18 +02:00
|
|
|
quint64 SearchForCovers(const QString &artist, const QString &album, const QString &title = QString());
|
2021-02-26 21:03:51 +01:00
|
|
|
quint64 FetchAlbumCover(const QString &artist, const QString &album, const QString &title, const bool batch);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
void Clear();
|
|
|
|
|
2020-04-20 18:03:18 +02:00
|
|
|
signals:
|
2023-04-09 22:26:17 +02:00
|
|
|
void AlbumCoverFetched(const quint64 request_id, const AlbumCoverImageResult &result, const CoverSearchStatistics &statistics);
|
2023-04-09 20:23:42 +02:00
|
|
|
void SearchFinished(const quint64 request_id, const CoverProviderSearchResults &results, const CoverSearchStatistics &statistics);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
private slots:
|
2023-04-09 20:23:42 +02:00
|
|
|
void SingleSearchFinished(const quint64 id, const CoverProviderSearchResults &results);
|
2023-04-09 22:26:17 +02:00
|
|
|
void SingleCoverFetched(const quint64 id, 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_;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // ALBUMCOVERFETCHER_H
|