~ubuntu-branches/ubuntu/saucy/clementine/saucy

« back to all changes in this revision

Viewing changes to src/globalsearch/groovesharksearchprovider.cpp

  • Committer: Package Import Robot
  • Author(s): Thomas PIERSON
  • Date: 2012-01-01 20:43:39 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120101204339-lsb6nndwhfy05sde
Tags: 1.0.1+dfsg-1
New upstream release. (Closes: #653926, #651611, #657391)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of Clementine.
 
2
   Copyright 2011, David Sansome <me@davidsansome.com>
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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/>.
 
16
*/
 
17
 
 
18
#include "groovesharksearchprovider.h"
 
19
 
 
20
#include <QIcon>
 
21
 
 
22
#include "core/logging.h"
 
23
#include "covers/albumcoverloader.h"
 
24
#include "internet/groovesharkservice.h"
 
25
#include "internet/internetsongmimedata.h"
 
26
 
 
27
GroovesharkSearchProvider::GroovesharkSearchProvider(QObject* parent)
 
28
    : service_(NULL) {
 
29
}
 
30
 
 
31
void GroovesharkSearchProvider::Init(GroovesharkService* service) {
 
32
  service_ = service;
 
33
  SearchProvider::Init("Grooveshark", "grooveshark",
 
34
                       QIcon(":providers/grooveshark.png"),
 
35
                       WantsDelayedQueries | ArtIsProbablyRemote | CanShowConfig);
 
36
 
 
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)));
 
43
 
 
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);
 
49
 
 
50
  connect(cover_loader_->Worker().get(),
 
51
          SIGNAL(ImageLoaded(quint64, QImage)),
 
52
          SLOT(AlbumArtLoaded(quint64, QImage)));
 
53
}
 
54
 
 
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));;
 
58
 
 
59
  const int album_id = service_->SearchAlbums(query);
 
60
  pending_searches_[album_id] = PendingState(id, TokenizeQuery(query));
 
61
}
 
62
 
 
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_;
 
67
 
 
68
  SongList songs_copy(songs);
 
69
  SortSongs(&songs_copy);
 
70
 
 
71
  ResultList ret;
 
72
  foreach (const Song& song, songs_copy) {
 
73
    Result result(this);
 
74
    result.type_ = globalsearch::Type_Track;
 
75
    result.metadata_ = song;
 
76
    result.match_quality_ = MatchQuality(state.tokens_, song.title());
 
77
 
 
78
    ret << result;
 
79
  }
 
80
 
 
81
  emit ResultsAvailable(global_search_id, ret);
 
82
  MaybeSearchFinished(global_search_id);
 
83
}
 
84
 
 
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_;
 
89
 
 
90
  ResultList ret;
 
91
  foreach (const Song& s, songs) {
 
92
    Result result(this);
 
93
    result.type_ = globalsearch::Type_Album;
 
94
    result.metadata_ = s;
 
95
    result.match_quality_ =
 
96
        qMin(MatchQuality(state.tokens_, s.album()),
 
97
             MatchQuality(state.tokens_, s.artist()));
 
98
 
 
99
    ret << result;
 
100
  }
 
101
 
 
102
  emit ResultsAvailable(global_search_id, ret);
 
103
  MaybeSearchFinished(global_search_id);
 
104
}
 
105
 
 
106
void GroovesharkSearchProvider::MaybeSearchFinished(int id) {
 
107
  if (pending_searches_.keys(PendingState(id, QStringList())).isEmpty()) {
 
108
    emit SearchFinished(id);
 
109
  }
 
110
}
 
111
 
 
112
 
 
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;
 
116
}
 
117
 
 
118
void GroovesharkSearchProvider::AlbumArtLoaded(quint64 id, const QImage& image) {
 
119
  if (!cover_loader_tasks_.contains(id)) {
 
120
    return;
 
121
  }
 
122
  int original_id = cover_loader_tasks_.take(id);
 
123
  emit ArtLoaded(original_id, image);
 
124
}
 
125
 
 
126
void GroovesharkSearchProvider::LoadTracksAsync(int id, const Result& result) {
 
127
  SongList ret;
 
128
 
 
129
  switch (result.type_) {
 
130
    case globalsearch::Type_Track: {
 
131
      ret << result.metadata_;
 
132
      SortSongs(&ret);
 
133
 
 
134
      InternetSongMimeData* mime_data = new InternetSongMimeData(service_);
 
135
      mime_data->songs = ret;
 
136
 
 
137
      emit TracksLoaded(id, mime_data);
 
138
      break;
 
139
    }
 
140
 
 
141
    case globalsearch::Type_Album:
 
142
      FetchAlbum(id, result);
 
143
      break;
 
144
 
 
145
    default:
 
146
      Q_ASSERT(0);
 
147
  }
 
148
 
 
149
}
 
150
 
 
151
bool GroovesharkSearchProvider::IsLoggedIn() {
 
152
  return (service_ && service_->IsLoggedIn());
 
153
}
 
154
 
 
155
void GroovesharkSearchProvider::ShowConfig() {
 
156
  service_->ShowConfig();
 
157
}
 
158
 
 
159
void GroovesharkSearchProvider::FetchAlbum(int id, const Result& result) {
 
160
  service_->FetchSongsForAlbum(id, result.metadata_.url());
 
161
}
 
162
 
 
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);
 
167
 
 
168
  emit TracksLoaded(id, mime_data);
 
169
}