diff --git a/lib/PlaylistContainer.js b/lib/PlaylistContainer.js index 91cf0c0..ad0efe9 100644 --- a/lib/PlaylistContainer.js +++ b/lib/PlaylistContainer.js @@ -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(); }; @@ -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 */ @@ -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++) { diff --git a/src/playlist.cc b/src/playlist.cc index 17f5ca5..e8ccc3e 100644 --- a/src/playlist.cc +++ b/src/playlist.cc @@ -61,8 +61,76 @@ static Handle 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 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* playlistcontainer = ObjectHandle::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(playlistType))); +} + +/** + * JS playlist folder id implementation. + */ +static Handle 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* playlistcontainer = ObjectHandle::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 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* playlistcontainer = ObjectHandle::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)); } /** @@ -90,23 +158,30 @@ static Handle 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* playlist = new ObjectHandle("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 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(SP_PLAYLIST_TYPE_PLAYLIST)), ReadOnly); + target->Set(v8::String::NewSymbol("SP_PLAYLIST_TYPE_START_FOLDER"), v8::Int32::New(static_cast(SP_PLAYLIST_TYPE_START_FOLDER)), ReadOnly); + target->Set(v8::String::NewSymbol("SP_PLAYLIST_TYPE_END_FOLDER"), v8::Int32::New(static_cast(SP_PLAYLIST_TYPE_END_FOLDER)), ReadOnly); + target->Set(v8::String::NewSymbol("SP_PLAYLIST_TYPE_PLACEHOLDER"), v8::Int32::New(static_cast(SP_PLAYLIST_TYPE_PLACEHOLDER)), ReadOnly); } /* @@ -166,7 +241,7 @@ static Handle 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)); } @@ -191,10 +266,10 @@ static Handle Playlist_Track(const Arguments& args) { // actually call sp_playlist_track sp_track* sptrack = sp_playlist_track(playlist->pointer, index); - + ObjectHandle* track = new ObjectHandle("sp_track"); track->pointer = sptrack; - + return scope.Close(track->object); } @@ -217,7 +292,7 @@ static Handle 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()); }