Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 44 additions & 4 deletions lib/PlaylistContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ PlaylistContainer.prototype.__defineGetter__('_object_type', playlistcontainer_o

module.exports = PlaylistContainer;

PlaylistContainer.prototype.PLAYLIST_TYPE_PLAYLIST = b.SP_PLAYLIST_TYPE_PLAYLIST;
PlaylistContainer.prototype.PLAYLIST_TYPE_START_FOLDER = b.SP_PLAYLIST_TYPE_START_FOLDER;
PlaylistContainer.prototype.PLAYLIST_TYPE_END_FOLDER = b.SP_PLAYLIST_TYPE_END_FOLDER;
PlaylistContainer.prototype.PLAYLIST_TYPE_PLACEHOLDER = b.SP_PLAYLIST_TYPE_PLACEHOLDER;

PlaylistContainer.prototype._populateAttributes = function _populateAttributes() {
return this.constructor.super_.prototype._populateAttributes();
};
Expand All @@ -33,6 +38,37 @@ PlaylistContainer.prototype.getNumPlaylists = function getNumPlaylists() {
return b.playlistcontainer_num_playlists(this._sp_object);
};

/**
* gets the type of the playlist at index in the playlist container
*/
PlaylistContainer.prototype.getPlaylistType = function getPlaylistType(idx) {
this._readyOrThrow();
return b.playlistcontainer_playlist_type(this._sp_object, idx);
};

/**
* gets the id of the playlist folder at index in the playlist container
*/
PlaylistContainer.prototype.getPlaylistFolderID = function getPlaylistFolderID(idx) {
this._readyOrThrow();
return b.playlistcontainer_playlist_folder_id(this._sp_object, idx);
};

/**
* gets the name of the playlist folder at index in the playlist container
*/
PlaylistContainer.prototype.getPlaylistFolderName = function getPlaylistFolderName(idx) {
this._readyOrThrow();
return b.playlistcontainer_playlist_folder_name(this._sp_object, idx);
};

/**
* gets a playlist from the specified index
*/
PlaylistContainer.prototype.getPlaylistAtIndex = function getPlaylistAtIndex(idx) {
return new sp.Playlist(b.playlistcontainer_playlist(this._sp_object, sp.Session.currentSession._sp_session, idx))
}

/**
* gets a list of all the playlists in the playlist container
*/
Expand All @@ -41,11 +77,15 @@ PlaylistContainer.prototype.getPlaylists = function getPlaylists(callback) {
var i, numReady = 0;

var numPlaylists = this.getNumPlaylists();
var playlists = new Array(numPlaylists);
var playlists = new Array();

for (i = 0; i < playlists.length; i++) {
var playlist = new sp.Playlist(b.playlistcontainer_playlist(this._sp_object, sp.Session.currentSession._sp_session, i));
playlists[i] = playlist;
for (i = 0; i < numPlaylists; i++) {
var type = this.getPlaylistType(i);
switch (type) {
case b.SP_PLAYLIST_TYPE_PLAYLIST:
playlists.push(this.getPlaylistAtIndex(i));
break;
}
}

for (i = 0; i < playlists.length; i++) {
Expand Down
97 changes: 86 additions & 11 deletions src/playlist.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,76 @@ static Handle<Value> PlaylistContainer_Num_Playlists(const Arguments& args) {

// actually call sp_playlistcontainer_num_playlists
int numPlaylists = sp_playlistcontainer_num_playlists(playlistcontainer->pointer);

return scope.Close(Number::New(numPlaylists));

return scope.Close(Number::New(numPlaylists));
}

/**
* JS playlist type implementation.
*/
static Handle<Value> PlaylistContainer_Playlist_Type(const Arguments& args) {
HandleScope scope;

// test arguments sanity
assert(args.Length() == 2);
assert(args[0]->IsObject());
assert(args[1]->IsNumber());

// gets sp_playlistcontainer pointer from given object
ObjectHandle<sp_playlistcontainer>* playlistcontainer = ObjectHandle<sp_playlistcontainer>::Unwrap(args[0]);

int index = args[1]->ToNumber()->Int32Value();

// actually call sp_playlistcontainer_playlist_type
sp_playlist_type playlistType = sp_playlistcontainer_playlist_type(playlistcontainer->pointer, index);

return scope.Close(Number::New(static_cast<int>(playlistType)));
}

/**
* JS playlist folder id implementation.
*/
static Handle<Value> PlaylistContainer_Playlist_Folder_ID(const Arguments& args) {
HandleScope scope;

// test arguments sanity
assert(args.Length() == 2);
assert(args[0]->IsObject());
assert(args[1]->IsNumber());

// gets sp_playlistcontainer pointer from given object
ObjectHandle<sp_playlistcontainer>* playlistcontainer = ObjectHandle<sp_playlistcontainer>::Unwrap(args[0]);

int index = args[1]->ToNumber()->Int32Value();

// actually call sp_playlistcontainer_playlist_folder_id
sp_uint64 playlistFolderId = sp_playlistcontainer_playlist_folder_id(playlistcontainer->pointer, index);

return scope.Close(Number::New(playlistFolderId));
}

/**
* JS playlist folder name implementation.
*/
static Handle<Value> PlaylistContainer_Playlist_Folder_Name(const Arguments& args) {
HandleScope scope;

// test arguments sanity
assert(args.Length() == 2);
assert(args[0]->IsObject());
assert(args[1]->IsNumber());

// gets sp_playlistcontainer pointer from given object
ObjectHandle<sp_playlistcontainer>* playlistcontainer = ObjectHandle<sp_playlistcontainer>::Unwrap(args[0]);

int index = args[1]->ToNumber()->Int32Value();
char nameChars[256];

// actually call sp_playlistcontainer_playlist_folder_name
sp_error error = sp_playlistcontainer_playlist_folder_name(playlistcontainer->pointer, index, nameChars, sizeof(nameChars));
NSP_THROW_IF_ERROR(error);

return scope.Close(String::New(nameChars));
}

/**
Expand Down Expand Up @@ -90,23 +158,30 @@ static Handle<Value> PlaylistContainer_Playlist(const Arguments& args) {

// actually call sp_playlistcontainer_playlist
sp_playlist* spplaylist = sp_playlistcontainer_playlist(playlistcontainer->pointer, index);

// Set the playlist in RAM
sp_playlist_set_in_ram(session->pointer, spplaylist, true);

ObjectHandle<sp_playlist>* playlist = new ObjectHandle<sp_playlist>("sp_playlist");
playlist->pointer = spplaylist;

sp_error error = sp_playlist_add_callbacks(spplaylist, &nsp_playlist_callbacks, playlist);
NSP_THROW_IF_ERROR(error);

return scope.Close(playlist->object);
}

void nsp::init_playlistcontainer(Handle<Object> target) {
NODE_SET_METHOD(target, "playlistcontainer_is_loaded", PlaylistContainer_Is_Loaded);
NODE_SET_METHOD(target, "playlistcontainer_is_loaded", PlaylistContainer_Is_Loaded);
NODE_SET_METHOD(target, "playlistcontainer_num_playlists", PlaylistContainer_Num_Playlists);
NODE_SET_METHOD(target, "playlistcontainer_playlist_type", PlaylistContainer_Playlist_Type);
NODE_SET_METHOD(target, "playlistcontainer_playlist_folder_id", PlaylistContainer_Playlist_Folder_ID);
NODE_SET_METHOD(target, "playlistcontainer_playlist_folder_name", PlaylistContainer_Playlist_Folder_Name);
NODE_SET_METHOD(target, "playlistcontainer_playlist", PlaylistContainer_Playlist);
target->Set(v8::String::NewSymbol("SP_PLAYLIST_TYPE_PLAYLIST"), v8::Int32::New(static_cast<int>(SP_PLAYLIST_TYPE_PLAYLIST)), ReadOnly);
target->Set(v8::String::NewSymbol("SP_PLAYLIST_TYPE_START_FOLDER"), v8::Int32::New(static_cast<int>(SP_PLAYLIST_TYPE_START_FOLDER)), ReadOnly);
target->Set(v8::String::NewSymbol("SP_PLAYLIST_TYPE_END_FOLDER"), v8::Int32::New(static_cast<int>(SP_PLAYLIST_TYPE_END_FOLDER)), ReadOnly);
target->Set(v8::String::NewSymbol("SP_PLAYLIST_TYPE_PLACEHOLDER"), v8::Int32::New(static_cast<int>(SP_PLAYLIST_TYPE_PLACEHOLDER)), ReadOnly);
}

/*
Expand Down Expand Up @@ -166,7 +241,7 @@ static Handle<Value> Playlist_Num_Tracks(const Arguments& args) {

// actually call sp_playlist_num_tracks
int numTracks = sp_playlist_num_tracks(playlist->pointer);

return scope.Close(Number::New(numTracks));
}

Expand All @@ -191,10 +266,10 @@ static Handle<Value> Playlist_Track(const Arguments& args) {

// actually call sp_playlist_track
sp_track* sptrack = sp_playlist_track(playlist->pointer, index);

ObjectHandle<sp_track>* track = new ObjectHandle<sp_track>("sp_track");
track->pointer = sptrack;

return scope.Close(track->object);
}

Expand All @@ -217,7 +292,7 @@ static Handle<Value> Playlist_Update_Subscribers(const Arguments& args) {

sp_error error = sp_playlist_update_subscribers(session->pointer, playlist->pointer);
NSP_THROW_IF_ERROR(error);

return scope.Close(Undefined());
}

Expand Down