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

« back to all changes in this revision

Viewing changes to src/globalsearch/spotifysearchprovider.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 2010, 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 "spotifysearchprovider.h"
 
19
#include "core/logging.h"
 
20
#include "internet/internetmodel.h"
 
21
#include "internet/spotifyserver.h"
 
22
#include "internet/spotifyservice.h"
 
23
#include "playlist/songmimedata.h"
 
24
#include "spotifyblob/common/spotifymessagehandler.h"
 
25
 
 
26
SpotifySearchProvider::SpotifySearchProvider(QObject* parent)
 
27
  : SearchProvider(parent),
 
28
    server_(NULL),
 
29
    service_(NULL)
 
30
{
 
31
  Init("Spotify", "spotify", QIcon(":icons/32x32/spotify.png"),
 
32
       WantsDelayedQueries | WantsSerialisedArtQueries | ArtIsProbablyRemote |
 
33
       CanShowConfig);
 
34
}
 
35
 
 
36
SpotifyServer* SpotifySearchProvider::server() {
 
37
  if (server_)
 
38
    return server_;
 
39
 
 
40
  if (!service_)
 
41
    service_ = InternetModel::Service<SpotifyService>();
 
42
 
 
43
  if (service_->login_state() != SpotifyService::LoginState_LoggedIn)
 
44
    return NULL;
 
45
 
 
46
  server_ = service_->server();
 
47
  connect(server_, SIGNAL(SearchResults(spotify_pb::SearchResponse)),
 
48
          SLOT(SearchFinishedSlot(spotify_pb::SearchResponse)));
 
49
  connect(server_, SIGNAL(ImageLoaded(QString,QImage)),
 
50
          SLOT(ArtLoadedSlot(QString,QImage)));
 
51
  connect(server_, SIGNAL(AlbumBrowseResults(spotify_pb::BrowseAlbumResponse)),
 
52
          SLOT(AlbumBrowseResponse(spotify_pb::BrowseAlbumResponse)));
 
53
  connect(server_, SIGNAL(destroyed()), SLOT(ServerDestroyed()));
 
54
 
 
55
  return server_;
 
56
}
 
57
 
 
58
void SpotifySearchProvider::ServerDestroyed() {
 
59
  server_ = NULL;
 
60
}
 
61
 
 
62
void SpotifySearchProvider::SearchAsync(int id, const QString& query) {
 
63
  SpotifyServer* s = server();
 
64
  if (!s) {
 
65
    emit SearchFinished(id);
 
66
    return;
 
67
  }
 
68
 
 
69
  PendingState state;
 
70
  state.orig_id_ = id;
 
71
  state.tokens_ = TokenizeQuery(query);
 
72
 
 
73
  const QString query_string = state.tokens_.join(" ");
 
74
  s->Search(query_string, 5, 5);
 
75
  queries_[query_string] = state;
 
76
}
 
77
 
 
78
void SpotifySearchProvider::SearchFinishedSlot(const spotify_pb::SearchResponse& response) {
 
79
  QString query_string = QString::fromUtf8(response.request().query().c_str());
 
80
  QMap<QString, PendingState>::iterator it = queries_.find(query_string);
 
81
  if (it == queries_.end())
 
82
    return;
 
83
 
 
84
  PendingState state = it.value();
 
85
  queries_.erase(it);
 
86
 
 
87
  ResultList ret;
 
88
  for (int i=0; i < response.result_size() ; ++i) {
 
89
    const spotify_pb::Track& track = response.result(i);
 
90
 
 
91
    Result result(this);
 
92
    result.type_ = globalsearch::Type_Track;
 
93
    SpotifyService::SongFromProtobuf(track, &result.metadata_);
 
94
    result.match_quality_ = MatchQuality(state.tokens_, result.metadata_.title());
 
95
 
 
96
    ret << result;
 
97
  }
 
98
 
 
99
  for (int i=0 ; i<response.album_size() ; ++i) {
 
100
    const spotify_pb::Album& album = response.album(i);
 
101
 
 
102
    Result result(this);
 
103
    result.type_ = globalsearch::Type_Album;
 
104
    SpotifyService::SongFromProtobuf(album.metadata(), &result.metadata_);
 
105
    result.match_quality_ =
 
106
        qMin(MatchQuality(state.tokens_, result.metadata_.album()),
 
107
             MatchQuality(state.tokens_, result.metadata_.artist()));
 
108
    result.album_size_ = album.metadata().track();
 
109
 
 
110
    for (int j=0; j < album.track_size() ; ++j) {
 
111
      Song track_song;
 
112
      SpotifyService::SongFromProtobuf(album.track(j), &track_song);
 
113
      result.album_songs_ << track_song;
 
114
    }
 
115
 
 
116
    ret << result;
 
117
  }
 
118
 
 
119
  emit ResultsAvailable(state.orig_id_, ret);
 
120
  emit SearchFinished(state.orig_id_);
 
121
}
 
122
 
 
123
void SpotifySearchProvider::LoadArtAsync(int id, const Result& result) {
 
124
  SpotifyServer* s = server();
 
125
  if (!s) {
 
126
    emit ArtLoaded(id, QImage());
 
127
    return;
 
128
  }
 
129
 
 
130
  QString image_id = QUrl(result.metadata_.art_automatic()).path();
 
131
  if (image_id.startsWith('/'))
 
132
    image_id.remove(0, 1);
 
133
 
 
134
  pending_art_[image_id] = id;
 
135
  s->LoadImage(image_id);
 
136
}
 
137
 
 
138
void SpotifySearchProvider::ArtLoadedSlot(const QString& id, const QImage& image) {
 
139
  QMap<QString, int>::iterator it = pending_art_.find(id);
 
140
  if (it == pending_art_.end())
 
141
    return;
 
142
 
 
143
  const int orig_id = it.value();
 
144
  pending_art_.erase(it);
 
145
 
 
146
  emit ArtLoaded(orig_id, ScaleAndPad(image));
 
147
}
 
148
 
 
149
void SpotifySearchProvider::LoadTracksAsync(int id, const Result& result) {
 
150
  switch (result.type_) {
 
151
  case globalsearch::Type_Track: {
 
152
    SongMimeData* mime_data = new SongMimeData;
 
153
    mime_data->songs = SongList() << result.metadata_;
 
154
    emit TracksLoaded(id, mime_data);
 
155
    break;
 
156
  }
 
157
 
 
158
  case globalsearch::Type_Album: {
 
159
    SpotifyServer* s = server();
 
160
    if (!s) {
 
161
      emit TracksLoaded(id, NULL);
 
162
      return;
 
163
    }
 
164
 
 
165
    QString uri = result.metadata_.url().toString();
 
166
 
 
167
    pending_tracks_[uri] = id;
 
168
    s->AlbumBrowse(uri);
 
169
    break;
 
170
  }
 
171
 
 
172
  default:
 
173
    break;
 
174
  }
 
175
}
 
176
 
 
177
void SpotifySearchProvider::AlbumBrowseResponse(const spotify_pb::BrowseAlbumResponse& response) {
 
178
  QString uri = QStringFromStdString(response.uri());
 
179
  QMap<QString, int>::iterator it = pending_tracks_.find(uri);
 
180
  if (it == pending_tracks_.end())
 
181
    return;
 
182
 
 
183
  const int orig_id = it.value();
 
184
  pending_tracks_.erase(it);
 
185
 
 
186
  SongMimeData* mime_data = new SongMimeData;
 
187
 
 
188
  for (int i=0 ; i<response.track_size() ; ++i) {
 
189
    Song song;
 
190
    SpotifyService::SongFromProtobuf(response.track(i), &song);
 
191
    mime_data->songs << song;
 
192
  }
 
193
 
 
194
  emit TracksLoaded(orig_id, mime_data);
 
195
}
 
196
 
 
197
bool SpotifySearchProvider::IsLoggedIn() {
 
198
  if (server()) {
 
199
    return service_->IsLoggedIn();
 
200
  }
 
201
  return false;
 
202
}
 
203
 
 
204
void SpotifySearchProvider::ShowConfig() {
 
205
  if (service_) {
 
206
    return service_->ShowConfig();
 
207
  }
 
208
}