Permalink
Browse files

Changes required for libspotify 0.0.6

  • Loading branch information...
1 parent 7faaf43 commit 9d5ec12c3a33cd71936c3161737007324c637984 @liesen committed Dec 4, 2010
Showing with 72 additions and 64 deletions.
  1. +3 −2 src/callback_queue.cc
  2. +1 −1 src/callback_queue.h
  3. +13 −12 src/index.cc
  4. +3 −10 src/index.h
  5. +7 −6 src/playlist.cc
  6. +3 −2 src/playlist.h
  7. +16 −10 src/playlistcontainer.cc
  8. +3 −2 src/playlistcontainer.h
  9. +7 −4 src/search.cc
  10. +3 −2 src/search.h
  11. +5 −7 src/session.cc
  12. +5 −4 src/track.cc
  13. +3 −2 src/track.h
@@ -27,7 +27,8 @@ void CallbackQueue::remove(CallbackQueueEntry *entry) {
delete entry;
}
-int CallbackQueue::process(const Handle<Object> &context,
+int CallbackQueue::process(sp_session* session,
+ const Handle<Object> &context,
bool once /*= false*/) {
int hits = 0;
CallbackQueueEntry *entry;
@@ -42,7 +43,7 @@ int CallbackQueue::process(const Handle<Object> &context,
Handle<Value> err = Undefined();
if (status == SP_ERROR_OK) {
// loaded
- Handle<Value> argv[] = { Undefined(), Track::New(t) };
+ Handle<Value> argv[] = { Undefined(), Track::New(session, t) };
(*entry->callback)->Call(context, 2, argv);
} else {
// an error occured
@@ -25,7 +25,7 @@ class CallbackQueue {
void push(const Local<Value> &callback, int type = 0, void *obj = NULL);
void push(const Local<Value> &callback, sp_track *track);
void remove(CallbackQueueEntry *entry);
- int process(const Handle<Object> &context, bool once = false);
+ int process(sp_session* session, const Handle<Object> &context, bool once = false);
};
#endif // SPOTIFY_CALLBACK_QUEUE_H_
View
@@ -7,16 +7,17 @@
#include "album.h"
#include "artist.h"
-extern "C" void init(Handle<Object> target) {
- HandleScope scope;
- Session::Initialize(target);
- PlaylistContainer::Initialize(target);
- Playlist::Initialize(target);
- SearchResult::Initialize(target);
- Track::Initialize(target);
- Album::Initialize(target);
- Artist::Initialize(target);
- target->Set(String::NewSymbol("version"), String::New("0.1"));
- target->Set(String::NewSymbol("spotifyAPIVersion"),
- Integer::New(SPOTIFY_API_VERSION));
+extern "C" {
+ void init(Handle<Object> target) {
+ HandleScope scope;
+ Session::Initialize(target);
+ PlaylistContainer::Initialize(target);
+ Playlist::Initialize(target);
+ SearchResult::Initialize(target);
+ Track::Initialize(target);
+ Album::Initialize(target);
+ Artist::Initialize(target);
+ target->Set(String::NewSymbol("version"),
+ Integer::New(SPOTIFY_API_VERSION));
+ }
}
View
@@ -3,27 +3,20 @@
#ifndef SPOTIFY_INDEX_H_
#define SPOTIFY_INDEX_H_
-// We almost always need access to these
-#include <node.h> // includes v8.h, ev.h, eio.h, sys/types.h, etc
-#include <node_events.h> // EventEmitter
-#include <libspotify/api.h> // Spotify API
+#include <node.h> // includes v8.h, ev.h, eio.h, sys/types.h, etc
+#include <node_events.h> // EventEmitter
+#include <libspotify/api.h> // Spotify API
-// Since we are not building a C++ API, we dont' care about namespaces in .h's
using namespace v8;
using namespace node;
-// -----------------------------------------------------------------------------
-// Constants
enum MetadataUpdateType {
kMetadataUpdateTypeTrack = 0,
kMetadataUpdateTypeAlbum = 1,
kMetadataUpdateTypeArtist = 2,
};
-// -----------------------------------------------------------------------------
-// Aiding construction of types
-
// Property getter interface boilerplate
#define GETTER_H(name)\
static Handle<Value> name(Local<String> property, const AccessorInfo& info)
View
@@ -25,7 +25,7 @@ static void TracksAdded(sp_playlist *playlist, sp_track *const *tracks,
Local<Array> tracks_array = Array::New(count);
for (int i = 0; i < count; i++) {
- tracks_array->Set(i, Track::New(tracks[i]));
+ tracks_array->Set(i, Track::New(pl->session_, tracks[i]));
}
Handle<Value> argv[] = { Integer::New(position), tracks_array };
@@ -104,8 +104,9 @@ static sp_playlist_callbacks callbacks = {
// -----------------------------------------------------------------------------
// Playlist implementation
-Playlist::Playlist(sp_playlist* playlist)
+Playlist::Playlist(sp_session* session, sp_playlist* playlist)
: EventEmitter()
+ , session_(session)
, playlist_(playlist) {
if (playlist_) {
sp_playlist_add_ref(playlist_);
@@ -129,7 +130,7 @@ Playlist::~Playlist() {
/**
* Creates a new JavaScript playlist object wrapper, or a cached if one exists.
*/
-Handle<Value> Playlist::New(sp_playlist *playlist) {
+Handle<Value> Playlist::New(sp_session* session, sp_playlist *playlist) {
// Try to find playlist in cache
PlaylistMap::iterator it = playlist_cache_.find(playlist);
@@ -140,7 +141,7 @@ Handle<Value> Playlist::New(sp_playlist *playlist) {
HandleScope scope;
Persistent<Object> instance = Persistent<Object>::New(
constructor_template->GetFunction()->NewInstance(0, NULL));
- Playlist* pl = new Playlist(playlist);
+ Playlist* pl = new Playlist(session, playlist);
pl->Wrap(instance);
playlist_cache_[playlist] = instance;
return scope.Close(instance);
@@ -175,7 +176,7 @@ Handle<Value> Playlist::TrackGetter(uint32_t index,
if (track == NULL)
return Undefined();
- return scope.Close(Track::New(track));
+ return scope.Close(Track::New(p->session_, track));
}
Handle<Value> Playlist::TrackSetter(uint32_t index,
@@ -220,7 +221,7 @@ Handle<Array> Playlist::TrackEnumerator(const AccessorInfo& info) {
for (int i = 0; i < num_tracks; i++) {
sp_track* track = sp_playlist_track(p->playlist_, i);
- tracks->Set(i, Track::New(track));
+ tracks->Set(i, Track::New(p->session_, track));
}
return scope.Close(tracks);
View
@@ -5,13 +5,13 @@
class Playlist : public node::EventEmitter {
public:
- explicit Playlist(sp_playlist* playlist);
+ explicit Playlist(sp_session* session, sp_playlist* playlist);
~Playlist();
static void Initialize(v8::Handle<v8::Object> target);
- static v8::Handle<v8::Value> New(sp_playlist *playlist);
+ static v8::Handle<v8::Value> New(sp_session* session, sp_playlist *playlist);
static v8::Handle<v8::Value> LengthGetter(v8::Local<v8::String> property,
const v8::AccessorInfo& info);
@@ -54,6 +54,7 @@ class Playlist : public node::EventEmitter {
int NumTracks() { return sp_playlist_num_tracks(playlist_); }
+ sp_session* session_;
sp_playlist* playlist_;
static Persistent<FunctionTemplate> constructor_template;
@@ -25,15 +25,18 @@ static void PlaylistAdded(sp_playlistcontainer *playlist_container,
it != pc->create_callback_queue_.end();
++it) {
if (it->first == playlist) {
- Handle<Value> argv[] = { Playlist::New(playlist) };
+ Handle<Value> argv[] = { Playlist::New(pc->session_, playlist) };
*(*it->second)->Call(pc->handle_, 1, argv);
sp_playlist_release(playlist);
cb_destroy(it->second);
pc->create_callback_queue_.erase(it);
}
}
- Handle<Value> argv[] = { Playlist::New(playlist), Integer::New(position) };
+ Handle<Value> argv[] = {
+ Playlist::New(pc->session_, playlist),
+ Integer::New(position)
+ };
pc->Emit(String::New("playlistAdded"), 2, argv);
}
@@ -44,7 +47,7 @@ static void PlaylistRemoved(sp_playlistcontainer *playlist_container,
// this is called on the main thread
PlaylistContainer* pc = static_cast<PlaylistContainer*>(userdata);
Handle<Value> argv[] = {
- Playlist::New(playlist),
+ Playlist::New(pc->session_, playlist),
Integer::New(old_position)
};
pc->Emit(String::New("playlistRemoved"), 2, argv);
@@ -58,7 +61,7 @@ static void PlaylistMoved(sp_playlistcontainer *playlist_container,
// this is called on the main thread
PlaylistContainer* pc = static_cast<PlaylistContainer*>(userdata);
Handle<Value> argv[] = {
- Playlist::New(playlist),
+ Playlist::New(pc->session_, playlist),
Integer::New(old_position),
Integer::New(new_position)
};
@@ -82,8 +85,10 @@ static sp_playlistcontainer_callbacks callbacks = {
PlaylistContainerLoaded
};
-PlaylistContainer::PlaylistContainer(sp_playlistcontainer* playlist_container)
+PlaylistContainer::PlaylistContainer(sp_session* session,
+ sp_playlistcontainer* playlist_container)
: EventEmitter(),
+ session_(session),
playlist_container_(playlist_container) {
if (playlist_container_)
sp_playlistcontainer_add_callbacks(playlist_container_, &callbacks, this);
@@ -105,11 +110,12 @@ PlaylistContainer::~PlaylistContainer() {
create_callback_queue_.clear();
}
-Handle<Value> PlaylistContainer::New(sp_playlistcontainer *playlist_container) {
+Handle<Value> PlaylistContainer::New(sp_session* session,
+ sp_playlistcontainer *playlist_container) {
HandleScope scope;
Local<Object> instance =
constructor_template->GetFunction()->NewInstance(0, NULL);
- PlaylistContainer* pc = new PlaylistContainer(playlist_container);
+ PlaylistContainer* pc = new PlaylistContainer(session, playlist_container);
pc->Wrap(instance);
return scope.Close(instance);
}
@@ -129,7 +135,7 @@ Handle<Value> PlaylistContainer::PlaylistGetter(uint32_t index,
sp_playlist* playlist = sp_playlistcontainer_playlist(
pc->playlist_container_, index);
if (!playlist) return Undefined();
- return scope.Close(Playlist::New(playlist));
+ return scope.Close(Playlist::New(pc->session_, playlist));
}
Handle<Value> PlaylistContainer::PlaylistSetter(uint32_t index,
@@ -165,7 +171,7 @@ Handle<Array> PlaylistContainer::PlaylistEnumerator(const AccessorInfo& info) {
for (int i = 0; i < num_playlists; i++) {
sp_playlist* playlist = sp_playlistcontainer_playlist(
pc->playlist_container_, i);
- playlists->Set(i, Playlist::New(playlist));
+ playlists->Set(i, Playlist::New(pc->session_, playlist));
}
return scope.Close(playlists);
@@ -197,7 +203,7 @@ Handle<Value> PlaylistContainer::Create(const Arguments& args) {
pc->create_callback_queue_.push_back(callback_entry);
}
- return scope.Close(Playlist::New(playlist));
+ return scope.Close(Playlist::New(pc->session_, playlist));
}
Handle<Value> PlaylistContainer::Remove(const Arguments& args) {
@@ -8,12 +8,12 @@
class PlaylistContainer : public node::EventEmitter {
public:
- explicit PlaylistContainer(sp_playlistcontainer* playlist_container);
+ explicit PlaylistContainer(sp_session* session, sp_playlistcontainer* playlist_container);
~PlaylistContainer();
static void Initialize(v8::Handle<v8::Object> target);
- static v8::Handle<v8::Value> New(sp_playlistcontainer* playlist_container);
+ static v8::Handle<v8::Value> New(sp_session* session, sp_playlistcontainer* playlist_container);
static v8::Handle<v8::Value> LengthGetter(v8::Local<v8::String> property,
const v8::AccessorInfo& info);
@@ -35,6 +35,7 @@ class PlaylistContainer : public node::EventEmitter {
// remove(playlist) Removes a playlist
static v8::Handle<v8::Value> Remove(const v8::Arguments& args);
+ sp_session* session_;
sp_playlistcontainer* playlist_container_;
std::list<std::pair<sp_playlist*, Persistent<Function>*> > create_callback_queue_;
View
@@ -10,15 +10,17 @@ Persistent<FunctionTemplate> SearchResult::constructor_template;
// -----------------------------------------------------------------------------
// SearchResult implementation
-SearchResult::SearchResult(sp_search *search)
+SearchResult::SearchResult(sp_session* session, sp_search *search)
: node::EventEmitter()
+ , session_(session)
, search_(search) {
}
-Local<Object> SearchResult::New(sp_search *search) {
+Local<Object> SearchResult::New(sp_session* session, sp_search *search) {
Local<Object> instance =
constructor_template->GetFunction()->NewInstance(0, NULL);
SearchResult *sr = ObjectWrap::Unwrap<SearchResult>(instance);
+ sr->session_ = session;
sr->search_ = search;
// call member "onsetup" (if function) to allow custom setup
Handle<Value> setupFun = instance->Get(String::New("onsetup"));
@@ -28,7 +30,7 @@ Local<Object> SearchResult::New(sp_search *search) {
}
Handle<Value> SearchResult::New(const Arguments& args) {
- (new SearchResult(NULL))->Wrap(args.This());
+ (new SearchResult(NULL, NULL))->Wrap(args.This());
return args.This();
}
@@ -39,7 +41,8 @@ Handle<Value> SearchResult::TracksGetter(Local<String> property,
int count = sp_search_num_tracks(s->search_);
Local<Array> array = Array::New(count);
for (int i = 0; i < count; i++) {
- array->Set(Integer::New(i), Track::New(sp_search_track(s->search_, i)));
+ sp_track* track = sp_search_track(s->search_, i);
+ array->Set(Integer::New(i), Track::New(s->session_, track));
}
return scope.Close(array);
}
View
@@ -8,9 +8,9 @@ class SearchResult : public EventEmitter {
static Persistent<FunctionTemplate> constructor_template;
static void Initialize(Handle<Object> target);
- explicit SearchResult(sp_search* search);
+ explicit SearchResult(sp_session* session, sp_search* search);
static Handle<Value> New(const Arguments& args);
- static Local<Object> New(sp_search *search);
+ static Local<Object> New(sp_session* session, sp_search *search);
static Handle<Value> LoadedGetter(Local<String> property,
const AccessorInfo& info);
@@ -29,6 +29,7 @@ class SearchResult : public EventEmitter {
static Handle<Value> URIGetter(Local<String> property,
const AccessorInfo& info);
protected:
+ sp_session* session_;
sp_search* search_;
};
View
@@ -130,7 +130,7 @@ static void MetadataUpdated(sp_session *session) {
Session* s = reinterpret_cast<Session*>(sp_session_userdata(session));
assert(s->main_thread_id_ == pthread_self() /* or we will crash */);
s->Emit(String::New("metadataUpdated"), 0, NULL);
- s->metadata_update_queue_.process(s->handle_);
+ s->metadata_update_queue_.process(s->session_, s->handle_);
}
static void ConnectionError(sp_session* session, sp_error error) {
@@ -154,7 +154,7 @@ static void SearchComplete(sp_search *search, void *userdata) {
} else {
Handle<Value> argv[] = {
Undefined(),
- SearchResult::New(search)
+ SearchResult::New(s->session_, search)
};
(*sdata->callback)->Call(s->handle_, 2, argv);
}
@@ -296,7 +296,7 @@ Handle<Value> Session::New(const Arguments& args) {
ev_unref(EV_DEFAULT_UC); // don't let a lingering async ev keep the main loop
sp_session* session;
- sp_error error = sp_session_init(&config, &session);
+ sp_error error = sp_session_create(&config, &session);
if (error != SP_ERROR_OK)
return JS_THROW(Error, sp_error_message(error));
@@ -330,9 +330,7 @@ Handle<Value> Session::Login(const Arguments& args) {
// save login callback
if (s->login_callback_) cb_destroy(s->login_callback_);
s->login_callback_ = cb_persist(args[2]);
-
sp_session_login(s->session_, *username, *password);
-
return Undefined();
}
@@ -452,7 +450,7 @@ Handle<Value> Session::GetTrackByLink(const Arguments& args) {
}
// create Track object
- Handle<Value> track = Track::New(t);
+ Handle<Value> track = Track::New(s->session_, t);
// "loaded" callback
if (args.Length() > 1) {
@@ -488,7 +486,7 @@ Handle<Value> Session::PlaylistContainerGetter(Local<String> property,
if (!s->playlist_container_) {
sp_playlistcontainer *pc = sp_session_playlistcontainer(s->session_);
- Handle<Value> playlist_container = PlaylistContainer::New(pc);
+ Handle<Value> playlist_container = PlaylistContainer::New(s->session_, pc);
s->playlist_container_ = new Persistent<Object>();
*s->playlist_container_ = Persistent<Object>::New(
Handle<Object>::Cast((*playlist_container)->ToObject()));
Oops, something went wrong.

0 comments on commit 9d5ec12

Please sign in to comment.