2011-09-30 16:01:07 +02:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2011, 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/>.
|
|
|
|
*/
|
|
|
|
|
2011-09-30 15:35:09 +02:00
|
|
|
#include "groovesharksearchprovider.h"
|
|
|
|
|
2011-09-30 15:58:24 +02:00
|
|
|
#include <QIcon>
|
|
|
|
|
2012-02-13 21:44:04 +01:00
|
|
|
#include "core/application.h"
|
2011-09-30 15:35:09 +02:00
|
|
|
#include "core/logging.h"
|
2011-09-30 15:58:24 +02:00
|
|
|
#include "covers/albumcoverloader.h"
|
2014-12-18 23:35:21 +01:00
|
|
|
#include "internet/grooveshark/groovesharkservice.h"
|
2011-09-30 15:35:09 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
GroovesharkSearchProvider::GroovesharkSearchProvider(Application* app,
|
|
|
|
QObject* parent)
|
|
|
|
: SearchProvider(app, parent), service_(nullptr) {}
|
2011-09-30 15:35:09 +02:00
|
|
|
|
2011-10-05 21:59:15 +02:00
|
|
|
void GroovesharkSearchProvider::Init(GroovesharkService* service) {
|
2011-09-30 15:35:09 +02:00
|
|
|
service_ = service;
|
2014-02-07 16:34:20 +01:00
|
|
|
SearchProvider::Init(
|
|
|
|
"Grooveshark", "grooveshark", QIcon(":providers/grooveshark.png"),
|
|
|
|
WantsDelayedQueries | ArtIsProbablyRemote | CanShowConfig);
|
2011-10-20 00:01:10 +02:00
|
|
|
|
2011-09-30 15:35:09 +02:00
|
|
|
connect(service_, SIGNAL(SimpleSearchResults(int, SongList)),
|
|
|
|
SLOT(SearchDone(int, SongList)));
|
2012-03-05 00:54:24 +01:00
|
|
|
connect(service_, SIGNAL(AlbumSearchResult(int, QList<quint64>)),
|
|
|
|
SLOT(AlbumSearchResult(int, QList<quint64>)));
|
|
|
|
connect(service_, SIGNAL(AlbumSongsLoaded(quint64, SongList)),
|
|
|
|
SLOT(AlbumSongsLoaded(quint64, SongList)));
|
2011-09-30 15:58:24 +02:00
|
|
|
|
2012-02-13 21:44:04 +01:00
|
|
|
cover_loader_options_.desired_height_ = kArtHeight;
|
|
|
|
cover_loader_options_.pad_output_image_ = true;
|
|
|
|
cover_loader_options_.scale_output_image_ = true;
|
2011-09-30 15:58:24 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(app_->album_cover_loader(), SIGNAL(ImageLoaded(quint64, QImage)),
|
2011-09-30 15:58:24 +02:00
|
|
|
SLOT(AlbumArtLoaded(quint64, QImage)));
|
2011-09-30 15:35:09 +02:00
|
|
|
}
|
|
|
|
|
2011-10-05 21:59:15 +02:00
|
|
|
void GroovesharkSearchProvider::SearchAsync(int id, const QString& query) {
|
2011-09-30 15:35:09 +02:00
|
|
|
const int service_id = service_->SimpleSearch(query);
|
2014-02-07 16:34:20 +01:00
|
|
|
pending_searches_[service_id] = PendingState(id, TokenizeQuery(query));
|
|
|
|
;
|
2011-10-04 18:26:40 +02:00
|
|
|
|
2011-10-05 14:08:33 +02:00
|
|
|
const int album_id = service_->SearchAlbums(query);
|
2011-11-01 13:00:27 +01:00
|
|
|
pending_searches_[album_id] = PendingState(id, TokenizeQuery(query));
|
2011-09-30 15:35:09 +02:00
|
|
|
}
|
|
|
|
|
2011-10-05 21:59:15 +02:00
|
|
|
void GroovesharkSearchProvider::SearchDone(int id, const SongList& songs) {
|
2011-09-30 15:35:09 +02:00
|
|
|
// Map back to the original id.
|
2011-11-01 13:00:27 +01:00
|
|
|
const PendingState state = pending_searches_.take(id);
|
|
|
|
const int global_search_id = state.orig_id_;
|
|
|
|
|
2011-09-30 15:35:09 +02:00
|
|
|
ResultList ret;
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const Song& song : songs) {
|
2011-09-30 15:35:09 +02:00
|
|
|
Result result(this);
|
|
|
|
result.metadata_ = song;
|
|
|
|
|
|
|
|
ret << result;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit ResultsAvailable(global_search_id, ret);
|
2011-10-16 22:57:28 +02:00
|
|
|
MaybeSearchFinished(global_search_id);
|
2011-09-30 15:35:09 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void GroovesharkSearchProvider::AlbumSearchResult(
|
|
|
|
int id, const QList<quint64>& albums_ids) {
|
2011-11-01 13:00:27 +01:00
|
|
|
// Map back to the original id.
|
|
|
|
const PendingState state = pending_searches_.take(id);
|
|
|
|
const int global_search_id = state.orig_id_;
|
2012-03-05 00:54:24 +01:00
|
|
|
if (albums_ids.isEmpty()) {
|
|
|
|
MaybeSearchFinished(global_search_id);
|
|
|
|
return;
|
|
|
|
}
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const quint64 album_id : albums_ids) {
|
2012-03-05 00:54:24 +01:00
|
|
|
pending_searches_[album_id] = PendingState(global_search_id, QStringList());
|
2011-10-05 14:08:33 +02:00
|
|
|
}
|
2011-10-16 22:57:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GroovesharkSearchProvider::MaybeSearchFinished(int id) {
|
2011-11-01 13:00:27 +01:00
|
|
|
if (pending_searches_.keys(PendingState(id, QStringList())).isEmpty()) {
|
2011-10-16 22:57:28 +02:00
|
|
|
emit SearchFinished(id);
|
|
|
|
}
|
2011-10-05 14:08:33 +02:00
|
|
|
}
|
|
|
|
|
2011-10-05 21:59:15 +02:00
|
|
|
void GroovesharkSearchProvider::LoadArtAsync(int id, const Result& result) {
|
2012-02-13 21:44:04 +01:00
|
|
|
quint64 loader_id = app_->album_cover_loader()->LoadImageAsync(
|
2014-02-07 16:34:20 +01:00
|
|
|
cover_loader_options_, result.metadata_);
|
2011-09-30 15:58:24 +02:00
|
|
|
cover_loader_tasks_[loader_id] = id;
|
|
|
|
}
|
2011-09-30 15:35:09 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void GroovesharkSearchProvider::AlbumArtLoaded(quint64 id,
|
|
|
|
const QImage& image) {
|
2011-09-30 15:58:24 +02:00
|
|
|
if (!cover_loader_tasks_.contains(id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int original_id = cover_loader_tasks_.take(id);
|
|
|
|
emit ArtLoaded(original_id, image);
|
2011-09-30 15:35:09 +02:00
|
|
|
}
|
|
|
|
|
2011-10-20 15:03:47 +02:00
|
|
|
bool GroovesharkSearchProvider::IsLoggedIn() {
|
|
|
|
return (service_ && service_->IsLoggedIn());
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void GroovesharkSearchProvider::ShowConfig() { service_->ShowConfig(); }
|
2011-10-20 15:03:47 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void GroovesharkSearchProvider::AlbumSongsLoaded(quint64 id,
|
|
|
|
const SongList& songs) {
|
2012-03-05 00:54:24 +01:00
|
|
|
const PendingState state = pending_searches_.take(id);
|
|
|
|
const int global_search_id = state.orig_id_;
|
|
|
|
ResultList ret;
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const Song& s : songs) {
|
2012-03-05 00:54:24 +01:00
|
|
|
Result result(this);
|
2012-06-04 19:18:37 +02:00
|
|
|
result.metadata_ = s;
|
2012-03-05 00:54:24 +01:00
|
|
|
ret << result;
|
|
|
|
}
|
2012-06-04 19:18:37 +02:00
|
|
|
|
2012-03-05 00:54:24 +01:00
|
|
|
emit ResultsAvailable(global_search_id, ret);
|
|
|
|
MaybeSearchFinished(global_search_id);
|
2011-09-30 15:35:09 +02:00
|
|
|
}
|