Cancel album cover loads properly

This commit is contained in:
David Sansome 2011-06-26 15:07:29 +00:00
parent f8045af720
commit 42801a967b
3 changed files with 35 additions and 3 deletions

View File

@ -69,6 +69,12 @@ void AlbumCoverFetcher::AddRequest(const CoverSearchRequest& req) {
void AlbumCoverFetcher::Clear() {
queued_requests_.clear();
foreach (AlbumCoverFetcherSearch* search, active_requests_.values()) {
search->Cancel();
search->deleteLater();
}
active_requests_.clear();
}
void AlbumCoverFetcher::StartRequests() {

View File

@ -40,7 +40,8 @@ AlbumCoverFetcherSearch::AlbumCoverFetcherSearch(const CoverSearchRequest& reque
: QObject(parent),
request_(request),
image_load_timeout_(new NetworkTimeouts(kImageLoadTimeoutMs, this)),
network_(network)
network_(network),
cancel_requested_(false)
{
// we will terminate the search after kSearchTimeoutMs miliseconds if we are not
// able to find all of the results before that point in time
@ -108,6 +109,10 @@ void AlbumCoverFetcherSearch::ProviderSearchFinished(
}
void AlbumCoverFetcherSearch::AllProvidersFinished() {
if (cancel_requested_) {
return;
}
// if we only wanted to do the search then we're done
if (request_.search) {
emit SearchFinished(request_.id, results_);
@ -159,6 +164,10 @@ void AlbumCoverFetcherSearch::ProviderCoverFetchFinished() {
reply->deleteLater();
pending_image_loads_.removeAll(reply);
if (cancel_requested_) {
return;
}
if (reply->error() != QNetworkReply::NoError) {
qLog(Info) << "Error requesting" << reply->url() << reply->errorString();
} else {
@ -216,3 +225,16 @@ void AlbumCoverFetcherSearch::SendBestImage() {
emit AlbumCoverFetched(request_.id, image);
}
void AlbumCoverFetcherSearch::Cancel() {
cancel_requested_ = true;
if (!pending_requests_.isEmpty()) {
TerminateSearch();
} else if (!pending_image_loads_.isEmpty()) {
foreach (QNetworkReply* reply, pending_image_loads_) {
reply->abort();
}
pending_image_loads_.clear();
}
}

View File

@ -39,10 +39,12 @@ class AlbumCoverFetcherSearch : public QObject {
AlbumCoverFetcherSearch(const CoverSearchRequest& request,
QNetworkAccessManager* network, QObject* parent);
// Starts the search. This is the moment when we count cover providers available
// in the application.
void Start();
// Cancels all pending requests. No Finished signals will be emitted, and it
// is the caller's responsibility to delete the AlbumCoverFetcherSearch.
void Cancel();
signals:
// It's the end of search (when there was no fetch-me-a-cover request).
void SearchFinished(quint64, const CoverSearchResults& results);
@ -81,6 +83,8 @@ private:
QMap<float, QImage> candidate_images_;
QNetworkAccessManager* network_;
bool cancel_requested_;
};
#endif // ALBUMCOVERFETCHERSEARCH_H