1
/* This file is part of Clementine.
2
Copyright 2011, David Sansome <me@davidsansome.com>
4
Clementine is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation, either version 3 of the License, or
7
(at your option) any later version.
9
Clementine is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
18
#include "groovesharksearchprovider.h"
22
#include "core/logging.h"
23
#include "covers/albumcoverloader.h"
24
#include "internet/groovesharkservice.h"
25
#include "internet/internetsongmimedata.h"
27
GroovesharkSearchProvider::GroovesharkSearchProvider(QObject* parent)
31
void GroovesharkSearchProvider::Init(GroovesharkService* service) {
33
SearchProvider::Init("Grooveshark", "grooveshark",
34
QIcon(":providers/grooveshark.png"),
35
WantsDelayedQueries | ArtIsProbablyRemote | CanShowConfig);
37
connect(service_, SIGNAL(SimpleSearchResults(int, SongList)),
38
SLOT(SearchDone(int, SongList)));
39
connect(service_, SIGNAL(AlbumSearchResult(int, SongList)),
40
SLOT(AlbumSearchResult(int, SongList)));
41
connect(service_, SIGNAL(AlbumSongsLoaded(int, SongList)),
42
SLOT(AlbumSongsLoaded(int, SongList)));
44
cover_loader_ = new BackgroundThreadImplementation<AlbumCoverLoader, AlbumCoverLoader>(this);
45
cover_loader_->Start(true);
46
cover_loader_->Worker()->SetDesiredHeight(kArtHeight);
47
cover_loader_->Worker()->SetPadOutputImage(true);
48
cover_loader_->Worker()->SetScaleOutputImage(true);
50
connect(cover_loader_->Worker().get(),
51
SIGNAL(ImageLoaded(quint64, QImage)),
52
SLOT(AlbumArtLoaded(quint64, QImage)));
55
void GroovesharkSearchProvider::SearchAsync(int id, const QString& query) {
56
const int service_id = service_->SimpleSearch(query);
57
pending_searches_[service_id] = PendingState(id, TokenizeQuery(query));;
59
const int album_id = service_->SearchAlbums(query);
60
pending_searches_[album_id] = PendingState(id, TokenizeQuery(query));
63
void GroovesharkSearchProvider::SearchDone(int id, const SongList& songs) {
64
// Map back to the original id.
65
const PendingState state = pending_searches_.take(id);
66
const int global_search_id = state.orig_id_;
68
SongList songs_copy(songs);
69
SortSongs(&songs_copy);
72
foreach (const Song& song, songs_copy) {
74
result.type_ = globalsearch::Type_Track;
75
result.metadata_ = song;
76
result.match_quality_ = MatchQuality(state.tokens_, song.title());
81
emit ResultsAvailable(global_search_id, ret);
82
MaybeSearchFinished(global_search_id);
85
void GroovesharkSearchProvider::AlbumSearchResult(int id, const SongList& songs) {
86
// Map back to the original id.
87
const PendingState state = pending_searches_.take(id);
88
const int global_search_id = state.orig_id_;
91
foreach (const Song& s, songs) {
93
result.type_ = globalsearch::Type_Album;
95
result.match_quality_ =
96
qMin(MatchQuality(state.tokens_, s.album()),
97
MatchQuality(state.tokens_, s.artist()));
102
emit ResultsAvailable(global_search_id, ret);
103
MaybeSearchFinished(global_search_id);
106
void GroovesharkSearchProvider::MaybeSearchFinished(int id) {
107
if (pending_searches_.keys(PendingState(id, QStringList())).isEmpty()) {
108
emit SearchFinished(id);
113
void GroovesharkSearchProvider::LoadArtAsync(int id, const Result& result) {
114
quint64 loader_id = cover_loader_->Worker()->LoadImageAsync(result.metadata_);
115
cover_loader_tasks_[loader_id] = id;
118
void GroovesharkSearchProvider::AlbumArtLoaded(quint64 id, const QImage& image) {
119
if (!cover_loader_tasks_.contains(id)) {
122
int original_id = cover_loader_tasks_.take(id);
123
emit ArtLoaded(original_id, image);
126
void GroovesharkSearchProvider::LoadTracksAsync(int id, const Result& result) {
129
switch (result.type_) {
130
case globalsearch::Type_Track: {
131
ret << result.metadata_;
134
InternetSongMimeData* mime_data = new InternetSongMimeData(service_);
135
mime_data->songs = ret;
137
emit TracksLoaded(id, mime_data);
141
case globalsearch::Type_Album:
142
FetchAlbum(id, result);
151
bool GroovesharkSearchProvider::IsLoggedIn() {
152
return (service_ && service_->IsLoggedIn());
155
void GroovesharkSearchProvider::ShowConfig() {
156
service_->ShowConfig();
159
void GroovesharkSearchProvider::FetchAlbum(int id, const Result& result) {
160
service_->FetchSongsForAlbum(id, result.metadata_.url());
163
void GroovesharkSearchProvider::AlbumSongsLoaded(int id, const SongList& songs) {
164
InternetSongMimeData* mime_data = new InternetSongMimeData(service_);
165
mime_data->songs = songs;
166
SortSongs(&mime_data->songs);
168
emit TracksLoaded(id, mime_data);