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

125 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 ALBUMCOVERFETCHERSEARCH_H
#define ALBUMCOVERFETCHERSEARCH_H
#include "config.h"
#include <QtGlobal>
2018-02-27 18:06:05 +01:00
#include <QObject>
#include <QPair>
2020-02-09 02:29:35 +01:00
#include <QMap>
#include <QMultiMap>
2021-07-12 08:28:52 +02:00
#include <QHash>
#include <QByteArray>
#include <QString>
2020-02-09 02:29:35 +01:00
#include <QUrl>
#include <QImage>
#include "core/shared_ptr.h"
#include "albumcoverfetcher.h"
#include "coversearchstatistics.h"
#include "albumcoverimageresult.h"
2018-02-27 18:06:05 +01: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;
// 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:
explicit AlbumCoverFetcherSearch(const CoverSearchRequest &request, SharedPtr<NetworkAccessManager> network, QObject *parent);
2020-06-15 21:55:05 +02:00
~AlbumCoverFetcherSearch() override;
2018-02-27 18:06:05 +01:00
void Start(SharedPtr<CoverProviders> cover_providers);
2018-02-27 18:06:05 +01: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_; }
static bool CoverProviderSearchResultCompareNumber(const CoverProviderSearchResult &a, const CoverProviderSearchResult &b);
signals:
2018-02-27 18:06:05 +01:00
// It's the end of search (when there was no fetch-me-a-cover request).
void SearchFinished(quint64, const CoverProviderSearchResults &results);
2018-02-27 18:06:05 +01:00
// It's the end of search and we've fetched a cover.
void AlbumCoverFetched(const quint64 id, const AlbumCoverImageResult &result);
2018-02-27 18:06:05 +01:00
private slots:
void ProviderSearchResults(const int id, const CoverProviderSearchResults &results);
void ProviderSearchFinished(const int id, const CoverProviderSearchResults &results);
void ProviderCoverFetchFinished(QNetworkReply *reply);
2018-02-27 18:06:05 +01:00
void TerminateSearch();
private:
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);
static bool CoverProviderSearchResultCompareScore(const CoverProviderSearchResult &a, const CoverProviderSearchResult &b);
2020-05-09 01:48:08 +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).
CoverProviderSearchResults results_;
2018-02-27 18:06:05 +01:00
QMap<int, CoverProvider*> pending_requests_;
2021-07-12 08:28:52 +02:00
QHash<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
// 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;
};
QMultiMap<float, CandidateImage> candidate_images_;
2018-02-27 18:06:05 +01:00
SharedPtr<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