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 ALBUMCOVERFETCHERSEARCH_H
|
|
|
|
#define ALBUMCOVERFETCHERSEARCH_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 <QPair>
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QMap>
|
2020-05-29 17:46:41 +02:00
|
|
|
#include <QMultiMap>
|
2021-02-26 21:03:51 +01:00
|
|
|
#include <QByteArray>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QString>
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QUrl>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QImage>
|
|
|
|
|
|
|
|
#include "albumcoverfetcher.h"
|
|
|
|
#include "coversearchstatistics.h"
|
2021-02-26 21:03:51 +01:00
|
|
|
#include "albumcoverimageresult.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-08-22 19:28:54 +02:00
|
|
|
class QNetworkReply;
|
2018-02-27 18:06:05 +01:00
|
|
|
class CoverProvider;
|
|
|
|
class CoverProviders;
|
2020-12-09 18:39:37 +01:00
|
|
|
class NetworkAccessManager;
|
2018-02-27 18:06:05 +01:00
|
|
|
class NetworkTimeouts;
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
// This class encapsulates a single search for covers initiated by an AlbumCoverFetcher.
|
|
|
|
// The search engages all of the known cover providers.
|
|
|
|
// AlbumCoverFetcherSearch signals search results to an interested AlbumCoverFetcher when all of the providers have done their part.
|
2018-02-27 18:06:05 +01:00
|
|
|
class AlbumCoverFetcherSearch : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2020-12-09 18:39:37 +01:00
|
|
|
explicit AlbumCoverFetcherSearch(const CoverSearchRequest &request, NetworkAccessManager *network, QObject *parent);
|
2020-06-15 21:55:05 +02:00
|
|
|
~AlbumCoverFetcherSearch() override;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-03-05 21:05:30 +01:00
|
|
|
void Start(CoverProviders *cover_providers);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
// Cancels all pending requests. No Finished signals will be emitted, and it is the caller's responsibility to delete the AlbumCoverFetcherSearch.
|
2018-02-27 18:06:05 +01:00
|
|
|
void Cancel();
|
|
|
|
|
|
|
|
CoverSearchStatistics statistics() const { return statistics_; }
|
|
|
|
|
2021-02-26 21:03:51 +01:00
|
|
|
static bool CoverProviderSearchResultCompareNumber(const CoverProviderSearchResult &a, const CoverProviderSearchResult &b);
|
2020-08-09 20:10:53 +02:00
|
|
|
|
2018-08-29 21:42:24 +02:00
|
|
|
signals:
|
2018-02-27 18:06:05 +01:00
|
|
|
// It's the end of search (when there was no fetch-me-a-cover request).
|
2021-02-26 21:03:51 +01:00
|
|
|
void SearchFinished(quint64, CoverProviderSearchResults results);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
// It's the end of search and we've fetched a cover.
|
2021-02-26 21:03:51 +01:00
|
|
|
void AlbumCoverFetched(const quint64, AlbumCoverImageResult result);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-08-29 21:42:24 +02:00
|
|
|
private slots:
|
2021-02-26 21:03:51 +01:00
|
|
|
void ProviderSearchResults(const int id, const CoverProviderSearchResults &results);
|
|
|
|
void ProviderSearchFinished(const int id, const CoverProviderSearchResults &results);
|
2019-08-22 19:28:54 +02:00
|
|
|
void ProviderCoverFetchFinished(QNetworkReply *reply);
|
2018-02-27 18:06:05 +01:00
|
|
|
void TerminateSearch();
|
|
|
|
|
2018-08-29 21:42:24 +02:00
|
|
|
private:
|
2021-02-26 21:03:51 +01:00
|
|
|
void ProviderSearchResults(CoverProvider *provider, const CoverProviderSearchResults &results);
|
2018-02-27 18:06:05 +01:00
|
|
|
void AllProvidersFinished();
|
|
|
|
|
|
|
|
void FetchMoreImages();
|
2021-07-11 09:49:38 +02:00
|
|
|
static float ScoreImage(const QSize size);
|
2018-02-27 18:06:05 +01:00
|
|
|
void SendBestImage();
|
|
|
|
|
2020-05-09 01:48:08 +02:00
|
|
|
static bool ProviderCompareOrder(CoverProvider *a, CoverProvider *b);
|
2021-02-26 21:03:51 +01:00
|
|
|
static bool CoverProviderSearchResultCompareScore(const CoverProviderSearchResult &a, const CoverProviderSearchResult &b);
|
2020-05-09 01:48:08 +02:00
|
|
|
|
2018-08-29 21:42:24 +02:00
|
|
|
private:
|
2018-02-27 18:06:05 +01:00
|
|
|
static const int kSearchTimeoutMs;
|
|
|
|
static const int kImageLoadTimeoutMs;
|
|
|
|
static const int kTargetSize;
|
|
|
|
static const float kGoodScore;
|
|
|
|
|
|
|
|
CoverSearchStatistics statistics_;
|
|
|
|
|
|
|
|
// Search request encapsulated by this AlbumCoverFetcherSearch.
|
|
|
|
CoverSearchRequest request_;
|
|
|
|
|
|
|
|
// Complete results (from all of the available providers).
|
2021-02-26 21:03:51 +01:00
|
|
|
CoverProviderSearchResults results_;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
QMap<int, CoverProvider*> pending_requests_;
|
2021-02-26 21:03:51 +01:00
|
|
|
QMap<QNetworkReply*, CoverProviderSearchResult> pending_image_loads_;
|
2021-06-12 20:53:23 +02:00
|
|
|
NetworkTimeouts *image_load_timeout_;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2021-02-26 21:03:51 +01:00
|
|
|
// QMap is sorted by key (score).
|
|
|
|
struct CandidateImage {
|
|
|
|
CandidateImage(const CoverProviderSearchResult &_result, const AlbumCoverImageResult &_album_cover) : result(_result), album_cover(_album_cover) {}
|
|
|
|
CoverProviderSearchResult result;
|
|
|
|
AlbumCoverImageResult album_cover;
|
|
|
|
};
|
2020-05-29 17:46:41 +02:00
|
|
|
QMultiMap<float, CandidateImage> candidate_images_;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2020-12-09 18:39:37 +01:00
|
|
|
NetworkAccessManager *network_;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
bool cancel_requested_;
|
2018-10-02 00:38:52 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // ALBUMCOVERFETCHERSEARCH_H
|