2011-04-02 15:34:06 +02:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
|
|
|
|
|
|
|
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 "albumcoverfetchersearch.h"
|
|
|
|
#include "coverprovider.h"
|
|
|
|
#include "coverproviders.h"
|
2011-04-22 19:07:57 +02:00
|
|
|
#include "core/logging.h"
|
2011-04-02 15:34:06 +02:00
|
|
|
|
|
|
|
#include <QMutexLocker>
|
|
|
|
#include <QNetworkReply>
|
2011-05-28 18:00:46 +02:00
|
|
|
#include <QTimer>
|
2011-04-17 17:25:32 +02:00
|
|
|
#include <QtDebug>
|
2011-04-02 15:34:06 +02:00
|
|
|
|
|
|
|
const int AlbumCoverFetcherSearch::kSearchTimeout = 10000;
|
|
|
|
|
|
|
|
AlbumCoverFetcherSearch::AlbumCoverFetcherSearch(const CoverSearchRequest& request,
|
|
|
|
QNetworkAccessManager* network,
|
|
|
|
QObject* parent)
|
|
|
|
: QObject(parent),
|
|
|
|
request_(request),
|
|
|
|
network_(network)
|
|
|
|
{
|
|
|
|
// we will terminate the search after kSearchTimeout miliseconds if we are not
|
2011-04-02 17:31:28 +02:00
|
|
|
// able to find all of the results before that point in time
|
2011-05-28 18:00:46 +02:00
|
|
|
QTimer::singleShot(kSearchTimeout, this, SLOT(Timeout()));
|
2011-04-02 15:34:06 +02:00
|
|
|
}
|
|
|
|
|
2011-05-28 18:00:46 +02:00
|
|
|
void AlbumCoverFetcherSearch::Timeout() {
|
2011-04-02 17:31:28 +02:00
|
|
|
TerminateSearch();
|
|
|
|
}
|
2011-04-02 15:34:06 +02:00
|
|
|
|
2011-04-02 17:31:28 +02:00
|
|
|
void AlbumCoverFetcherSearch::TerminateSearch() {
|
2011-05-28 18:00:46 +02:00
|
|
|
foreach (QNetworkReply* reply, pending_requests_.keys()) {
|
|
|
|
disconnect(reply, SIGNAL(finished()), this, SLOT(ProviderSearchFinished()));
|
|
|
|
reply->abort();
|
|
|
|
}
|
|
|
|
|
2011-04-02 15:34:06 +02:00
|
|
|
if(request_.search) {
|
2011-04-02 17:31:28 +02:00
|
|
|
// send everything we've managed to find
|
|
|
|
emit SearchFinished(request_.id, results_);
|
2011-04-02 15:34:06 +02:00
|
|
|
} else {
|
|
|
|
emit AlbumCoverFetched(request_.id, QImage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumCoverFetcherSearch::Start() {
|
2011-04-02 17:31:28 +02:00
|
|
|
// end this search before it even began if there are no providers...
|
2011-05-28 18:00:46 +02:00
|
|
|
foreach(CoverProvider* provider, CoverProviders::instance().List(this)) {
|
|
|
|
QNetworkReply* reply = provider->SendRequest(request_.query);
|
2011-04-02 15:34:06 +02:00
|
|
|
|
2011-05-28 18:00:46 +02:00
|
|
|
if (reply) {
|
2011-04-02 17:31:28 +02:00
|
|
|
connect(reply, SIGNAL(finished()), SLOT(ProviderSearchFinished()));
|
2011-05-28 18:00:46 +02:00
|
|
|
pending_requests_.insert(reply, provider);
|
2011-04-02 17:31:28 +02:00
|
|
|
}
|
2011-04-02 15:34:06 +02:00
|
|
|
}
|
2011-05-28 18:00:46 +02:00
|
|
|
|
|
|
|
if(pending_requests_.isEmpty()) {
|
|
|
|
TerminateSearch();
|
|
|
|
}
|
2011-04-02 15:34:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumCoverFetcherSearch::ProviderSearchFinished() {
|
2011-05-28 18:00:46 +02:00
|
|
|
// Note: we don't delete the reply here. It's parented to the provider's
|
|
|
|
// network access manager which is deleted when it is deleted.
|
|
|
|
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
|
2011-04-02 15:34:06 +02:00
|
|
|
|
2011-05-28 18:00:46 +02:00
|
|
|
CoverProvider* provider = pending_requests_.take(reply);
|
|
|
|
|
|
|
|
if(reply->error() == QNetworkReply::NoError) {
|
|
|
|
CoverSearchResults partial_results = provider->ParseReply(reply);
|
2011-06-20 01:15:29 +02:00
|
|
|
|
|
|
|
// Add categories to the results if the provider didn't specify them
|
|
|
|
for (int i=0 ; i<partial_results.count() ; ++i) {
|
|
|
|
if (partial_results[i].category.isEmpty()) {
|
|
|
|
partial_results[i].category = provider->name();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-28 18:00:46 +02:00
|
|
|
// add results from the current provider to our pool
|
|
|
|
results_.append(partial_results);
|
|
|
|
} else {
|
|
|
|
QString contents(reply->readAll());
|
|
|
|
qLog(Debug) << "CoverProvider's request error - summary:";
|
|
|
|
qLog(Debug) << reply->errorString();
|
|
|
|
qLog(Debug) << "CoverProvider's request error - contents:";
|
|
|
|
qLog(Debug) << contents;
|
|
|
|
}
|
|
|
|
|
|
|
|
// do we have more providers left?
|
|
|
|
if(!pending_requests_.isEmpty()) {
|
|
|
|
return;
|
2011-04-02 15:34:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// if we only wanted to do the search then we're done
|
|
|
|
if (request_.search) {
|
|
|
|
emit SearchFinished(request_.id, results_);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// no results?
|
|
|
|
if (results_.isEmpty()) {
|
|
|
|
emit AlbumCoverFetched(request_.id, QImage());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now we need to fetch the first result's image
|
|
|
|
QNetworkReply* image_reply = network_->get(QNetworkRequest(results_[0].image_url));
|
|
|
|
connect(image_reply, SIGNAL(finished()), SLOT(ProviderCoverFetchFinished()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumCoverFetcherSearch::ProviderCoverFetchFinished() {
|
|
|
|
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
|
|
|
|
reply->deleteLater();
|
|
|
|
|
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
|
|
|
// TODO: retry request.
|
|
|
|
emit AlbumCoverFetched(request_.id, QImage());
|
|
|
|
|
|
|
|
} else {
|
|
|
|
QImage image;
|
|
|
|
image.loadFromData(reply->readAll());
|
|
|
|
|
|
|
|
emit AlbumCoverFetched(request_.id, image);
|
|
|
|
}
|
|
|
|
}
|