Add Resolver interface and Spotify resolver.

This commit is contained in:
John Maguire 2011-05-31 15:33:46 +00:00
parent 41e79bea0e
commit e5d991ddaf
6 changed files with 123 additions and 4 deletions

View File

@ -186,6 +186,7 @@ set(SOURCES
radio/somafmurlhandler.cpp
resolvers/libraryresolver.cpp
resolvers/spotifyresolver.cpp
scripting/installscriptdialog.cpp
scripting/languageengine.cpp
@ -402,6 +403,8 @@ set(HEADERS
radio/somafmurlhandler.h
resolvers/libraryresolver.h
resolvers/resolver.h
resolvers/spotifyresolver.h
scripting/installscriptdialog.h
scripting/languageengine.h

View File

@ -13,7 +13,7 @@
using boost::scoped_ptr;
LibraryResolver::LibraryResolver(LibraryBackendInterface* backend, QObject* parent)
: QObject(parent),
: Resolver(parent),
backend_(backend) {
}

View File

@ -2,16 +2,15 @@
#define LIBRARYRESOLVER_H
#include <QMap>
#include <QObject>
#include "core/song.h"
#include "resolver.h"
class QNetworkReply;
class LibraryBackendInterface;
class LibraryQuery;
class LibraryResolver : public QObject {
class LibraryResolver : public Resolver {
Q_OBJECT
public:
LibraryResolver(LibraryBackendInterface* backend, QObject* parent = 0);

20
src/resolvers/resolver.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef RESOLVER_H
#define RESOLVER_H
#include <QObject>
#include "core/song.h"
class Resolver : public QObject {
Q_OBJECT
public:
Resolver(QObject* parent = 0) : QObject(parent) {}
virtual ~Resolver() {}
virtual int ResolveSong(const Song& song) = 0;
signals:
void ResolveFinished(int id, SongList songs);
};
#endif // RESOLVER_H

View File

@ -0,0 +1,65 @@
#include "spotifyresolver.h"
#include "core/logging.h"
#include "core/timeconstants.h"
#include "radio/spotifyserver.h"
#include "spotifyblob/common/spotifymessages.pb.h"
SpotifyResolver::SpotifyResolver(SpotifyServer* spotify, QObject* parent)
: Resolver(parent),
spotify_(spotify),
next_id_(0) {
connect(spotify_, SIGNAL(SearchResults(const protobuf::SearchResponse&)),
SLOT(SearchFinished(const protobuf::SearchResponse&)));
}
int SpotifyResolver::ResolveSong(const Song& song) {
QString query_string;
query_string += "artist:\"" + song.artist() + "\"";
query_string += " title:\"" + song.title() + "\"";
query_string += " album:\"" + song.album() + "\"";
qLog(Debug) << query_string;
spotify_->Search(query_string, 25);
int id = next_id_++;
queries_[query_string] = id;
return id;
}
void SpotifyResolver::SearchFinished(const protobuf::SearchResponse& response) {
QString query_string = QString::fromUtf8(response.request().query().c_str());
qLog(Debug) << query_string;
QMap<QString, int>::iterator it = queries_.find(query_string);
if (it == queries_.end()) {
return;
}
int id = it.value();
queries_.erase(it);
SongList songs;
for (int i = 0; i < response.result_size(); ++i) {
const protobuf::Track& track = response.result(i);
Song song;
song.set_title(QString::fromUtf8(track.title().c_str()));
if (track.artist_size() > 0) {
song.set_artist(QString::fromUtf8(track.artist(0).c_str()));
}
song.set_album(QString::fromUtf8(track.album().c_str()));
song.set_length_nanosec(track.duration_msec() * kNsecPerMsec);
song.set_disc(track.disc());
song.set_track(track.track());
song.set_year(track.year());
song.set_url(QUrl(QString::fromUtf8(track.uri().c_str())));
song.set_valid(true);
songs << song;
}
qLog(Debug) << "Resolved from spotify:" << songs.length();
if (!songs.isEmpty()) {
qLog(Debug) << songs[0].title() << songs[0].artist();
}
emit ResolveFinished(id, songs);
}

View File

@ -0,0 +1,32 @@
#ifndef SPOTIFYRESOLVER_H
#define SPOTIFYRESOLVER_H
#include <QMap>
#include "resolver.h"
namespace protobuf {
class SearchResponse;
}
class SpotifyServer;
class SpotifyResolver : public Resolver {
Q_OBJECT
public:
SpotifyResolver(SpotifyServer* service, QObject* parent = 0);
int ResolveSong(const Song& song);
signals:
void ResolveFinished(int id, SongList songs);
private slots:
void SearchFinished(const protobuf::SearchResponse& response);
private:
SpotifyServer* spotify_;
QMap<QString, int> queries_;
int next_id_;
};
#endif // SPOTIFYRESOLVER_H