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"
|
2011-09-30 15:35:09 +02:00
|
|
|
#include "internet/groovesharkservice.h"
|
2011-11-24 00:30:11 +01:00
|
|
|
#include "internet/internetsongmimedata.h"
|
2011-09-30 15:35:09 +02:00
|
|
|
|
2012-02-13 21:44:04 +01:00
|
|
|
GroovesharkSearchProvider::GroovesharkSearchProvider(Application* app, QObject* parent)
|
|
|
|
: SearchProvider(app, parent),
|
|
|
|
service_(NULL)
|
|
|
|
{
|
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;
|
2011-10-05 21:59:15 +02:00
|
|
|
SearchProvider::Init("Grooveshark", "grooveshark",
|
2011-10-20 00:01:10 +02:00
|
|
|
QIcon(":providers/grooveshark.png"),
|
2011-11-05 18:11:02 +01:00
|
|
|
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
|
|
|
|
2012-02-13 21:44:04 +01:00
|
|
|
connect(app_->album_cover_loader(),
|
2011-09-30 15:58:24 +02:00
|
|
|
SIGNAL(ImageLoaded(quint64, QImage)),
|
|
|
|
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);
|
2011-11-01 13:00:27 +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_;
|
|
|
|
|
|
|
|
SongList songs_copy(songs);
|
|
|
|
SortSongs(&songs_copy);
|
2011-09-30 15:35:09 +02:00
|
|
|
|
|
|
|
ResultList ret;
|
2011-11-01 13:00:27 +01:00
|
|
|
foreach (const Song& song, songs_copy) {
|
2011-09-30 15:35:09 +02:00
|
|
|
Result result(this);
|
2011-10-16 22:57:53 +02:00
|
|
|
result.type_ = globalsearch::Type_Track;
|
2011-09-30 15:35:09 +02:00
|
|
|
result.metadata_ = song;
|
2011-11-01 13:00:27 +01:00
|
|
|
result.match_quality_ = MatchQuality(state.tokens_, song.title());
|
2011-09-30 15:35:09 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-03-05 00:54:24 +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;
|
|
|
|
}
|
|
|
|
foreach (const quint64 album_id, albums_ids) {
|
|
|
|
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(
|
|
|
|
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
|
|
|
|
2011-10-05 21:59:15 +02: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-05 21:59:15 +02:00
|
|
|
void GroovesharkSearchProvider::LoadTracksAsync(int id, const Result& result) {
|
2011-09-30 15:58:24 +02:00
|
|
|
SongList ret;
|
|
|
|
|
|
|
|
switch (result.type_) {
|
2011-10-16 22:57:53 +02:00
|
|
|
case globalsearch::Type_Track: {
|
2011-09-30 15:58:24 +02:00
|
|
|
ret << result.metadata_;
|
2011-10-05 14:08:33 +02:00
|
|
|
SortSongs(&ret);
|
|
|
|
|
2011-11-24 00:30:11 +01:00
|
|
|
InternetSongMimeData* mime_data = new InternetSongMimeData(service_);
|
2011-10-05 14:08:33 +02:00
|
|
|
mime_data->songs = ret;
|
|
|
|
|
|
|
|
emit TracksLoaded(id, mime_data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-02-26 16:51:11 +01:00
|
|
|
case globalsearch::Type_Album: {
|
|
|
|
InternetSongMimeData* mime_data = new InternetSongMimeData(service_);
|
|
|
|
mime_data->songs = result.album_songs_;
|
|
|
|
emit TracksLoaded(id, mime_data);
|
2011-09-30 15:58:24 +02:00
|
|
|
break;
|
2012-02-26 16:51:11 +01:00
|
|
|
}
|
2011-09-30 15:58:24 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
Q_ASSERT(0);
|
|
|
|
}
|
|
|
|
|
2011-10-05 14:08:33 +02:00
|
|
|
}
|
|
|
|
|
2011-10-20 15:03:47 +02:00
|
|
|
bool GroovesharkSearchProvider::IsLoggedIn() {
|
|
|
|
return (service_ && service_->IsLoggedIn());
|
|
|
|
}
|
|
|
|
|
|
|
|
void GroovesharkSearchProvider::ShowConfig() {
|
|
|
|
service_->ShowConfig();
|
|
|
|
}
|
|
|
|
|
2012-03-05 00:54:24 +01:00
|
|
|
void GroovesharkSearchProvider::AlbumSongsLoaded(quint64 id, const SongList& songs) {
|
|
|
|
const PendingState state = pending_searches_.take(id);
|
|
|
|
const int global_search_id = state.orig_id_;
|
|
|
|
ResultList ret;
|
|
|
|
if (!songs.isEmpty()) {
|
|
|
|
Result result(this);
|
|
|
|
result.type_ = globalsearch::Type_Album;
|
|
|
|
const QString& artist = songs.last().artist();
|
|
|
|
const QString& album = songs.last().album();
|
|
|
|
result.metadata_.set_album(album);
|
|
|
|
result.metadata_.set_artist(artist);
|
|
|
|
result.metadata_.set_art_automatic(songs.last().art_automatic());
|
|
|
|
result.match_quality_ =
|
|
|
|
qMin(MatchQuality(state.tokens_, album),
|
|
|
|
MatchQuality(state.tokens_, artist));
|
|
|
|
foreach (const Song& s, songs) {
|
|
|
|
result.album_songs_ << s;
|
|
|
|
}
|
|
|
|
result.album_size_ = result.album_songs_.size();
|
2011-09-30 15:35:09 +02:00
|
|
|
|
2012-03-05 00:54:24 +01:00
|
|
|
ret << result;
|
|
|
|
}
|
|
|
|
emit ResultsAvailable(global_search_id, ret);
|
|
|
|
MaybeSearchFinished(global_search_id);
|
2011-09-30 15:35:09 +02:00
|
|
|
}
|