2011-04-02 15:34:06 +02:00
|
|
|
/* This file is part of Clementine.
|
2014-12-26 13:34:53 +01:00
|
|
|
Copyright 2011, Paweł Bara <keirangtp@gmail.com>
|
|
|
|
Copyright 2011-2012, David Sansome <me@davidsansome.com>
|
|
|
|
Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
|
|
|
|
Copyright 2014, John Maguire <john.maguire@gmail.com>
|
2011-04-02 15:34:06 +02:00
|
|
|
|
|
|
|
Clementine 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.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "albumcoverfetcher.h"
|
|
|
|
|
|
|
|
#include <QTimer>
|
|
|
|
|
2014-12-26 14:57:02 +01:00
|
|
|
#include "albumcoverfetchersearch.h"
|
|
|
|
#include "core/network.h"
|
|
|
|
|
2011-04-02 15:34:06 +02:00
|
|
|
const int AlbumCoverFetcher::kMaxConcurrentRequests = 5;
|
|
|
|
|
2011-07-23 20:34:41 +02:00
|
|
|
AlbumCoverFetcher::AlbumCoverFetcher(CoverProviders* cover_providers,
|
2014-02-07 16:34:20 +01:00
|
|
|
QObject* parent,
|
|
|
|
QNetworkAccessManager* network)
|
2011-04-02 15:34:06 +02:00
|
|
|
: QObject(parent),
|
2011-07-23 20:34:41 +02:00
|
|
|
cover_providers_(cover_providers),
|
2011-04-02 15:34:06 +02:00
|
|
|
network_(network ? network : new NetworkAccessManager(this)),
|
|
|
|
next_id_(0),
|
2014-02-07 16:34:20 +01:00
|
|
|
request_starter_(new QTimer(this)) {
|
2011-04-02 15:34:06 +02:00
|
|
|
request_starter_->setInterval(1000);
|
|
|
|
connect(request_starter_, SIGNAL(timeout()), SLOT(StartRequests()));
|
|
|
|
}
|
|
|
|
|
2011-06-22 21:07:15 +02:00
|
|
|
quint64 AlbumCoverFetcher::FetchAlbumCover(const QString& artist,
|
|
|
|
const QString& album) {
|
2011-04-02 15:34:06 +02:00
|
|
|
CoverSearchRequest request;
|
2011-06-22 21:07:15 +02:00
|
|
|
request.artist = artist;
|
|
|
|
request.album = album;
|
2011-04-02 15:34:06 +02:00
|
|
|
request.search = false;
|
2014-02-07 16:34:20 +01:00
|
|
|
request.id = next_id_++;
|
2011-04-02 15:34:06 +02:00
|
|
|
|
|
|
|
AddRequest(request);
|
|
|
|
return request.id;
|
|
|
|
}
|
|
|
|
|
2011-06-22 21:07:15 +02:00
|
|
|
quint64 AlbumCoverFetcher::SearchForCovers(const QString& artist,
|
|
|
|
const QString& album) {
|
2011-04-02 15:34:06 +02:00
|
|
|
CoverSearchRequest request;
|
2011-06-22 21:07:15 +02:00
|
|
|
request.artist = artist;
|
|
|
|
request.album = album;
|
2011-04-02 15:34:06 +02:00
|
|
|
request.search = true;
|
2014-02-07 16:34:20 +01:00
|
|
|
request.id = next_id_++;
|
2011-04-02 15:34:06 +02:00
|
|
|
|
|
|
|
AddRequest(request);
|
|
|
|
return request.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumCoverFetcher::AddRequest(const CoverSearchRequest& req) {
|
|
|
|
queued_requests_.enqueue(req);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!request_starter_->isActive()) request_starter_->start();
|
2011-04-02 15:34:06 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (active_requests_.size() < kMaxConcurrentRequests) StartRequests();
|
2011-04-02 15:34:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumCoverFetcher::Clear() {
|
|
|
|
queued_requests_.clear();
|
2011-06-26 17:07:29 +02:00
|
|
|
|
2014-02-10 14:29:07 +01:00
|
|
|
for (AlbumCoverFetcherSearch* search : active_requests_.values()) {
|
2011-06-26 17:07:29 +02:00
|
|
|
search->Cancel();
|
|
|
|
search->deleteLater();
|
|
|
|
}
|
|
|
|
active_requests_.clear();
|
2011-04-02 15:34:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumCoverFetcher::StartRequests() {
|
|
|
|
if (queued_requests_.isEmpty()) {
|
|
|
|
request_starter_->stop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-26 13:34:53 +01:00
|
|
|
while (!queued_requests_.isEmpty() && active_requests_.size() < kMaxConcurrentRequests) {
|
2011-04-02 15:34:06 +02:00
|
|
|
CoverSearchRequest request = queued_requests_.dequeue();
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
// search objects are this fetcher's children so worst case scenario - they
|
|
|
|
// get
|
2011-04-02 15:34:06 +02:00
|
|
|
// deleted with it
|
2014-02-07 16:34:20 +01:00
|
|
|
AlbumCoverFetcherSearch* search =
|
|
|
|
new AlbumCoverFetcherSearch(request, network_, this);
|
2011-04-02 15:34:06 +02:00
|
|
|
active_requests_.insert(request.id, search);
|
|
|
|
|
|
|
|
connect(search, SIGNAL(SearchFinished(quint64, CoverSearchResults)),
|
2014-02-07 16:34:20 +01:00
|
|
|
SLOT(SingleSearchFinished(quint64, CoverSearchResults)));
|
2011-04-02 15:34:06 +02:00
|
|
|
connect(search, SIGNAL(AlbumCoverFetched(quint64, const QImage&)),
|
2014-02-07 16:34:20 +01:00
|
|
|
SLOT(SingleCoverFetched(quint64, const QImage&)));
|
2011-04-02 15:34:06 +02:00
|
|
|
|
2011-07-23 20:34:41 +02:00
|
|
|
search->Start(cover_providers_);
|
2011-04-02 15:34:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void AlbumCoverFetcher::SingleSearchFinished(quint64 request_id,
|
|
|
|
CoverSearchResults results) {
|
2011-06-26 17:07:48 +02:00
|
|
|
AlbumCoverFetcherSearch* search = active_requests_.take(request_id);
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!search) return;
|
2012-02-26 16:34:18 +01:00
|
|
|
|
2011-06-26 17:07:48 +02:00
|
|
|
search->deleteLater();
|
|
|
|
emit SearchFinished(request_id, results, search->statistics());
|
2011-04-02 15:34:06 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void AlbumCoverFetcher::SingleCoverFetched(quint64 request_id,
|
|
|
|
const QImage& image) {
|
2011-06-26 17:07:48 +02:00
|
|
|
AlbumCoverFetcherSearch* search = active_requests_.take(request_id);
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!search) return;
|
2012-02-26 16:34:18 +01:00
|
|
|
|
2011-06-26 17:07:48 +02:00
|
|
|
search->deleteLater();
|
|
|
|
emit AlbumCoverFetched(request_id, image, search->statistics());
|
2011-04-02 15:34:06 +02:00
|
|
|
}
|