From 2499c84a48f02c7f1476fee43c3ad9302a0e699c Mon Sep 17 00:00:00 2001 From: Doug Kearns Date: Thu, 2 Apr 2009 01:02:20 +1100 Subject: [PATCH] Move library convenience functions to a library module. --- common/content/completion.js | 14 ++++-- xulmus/content/config.js | 7 ++- xulmus/content/library.js | 72 +++++++++++++++++++++++++++ xulmus/content/player.js | 96 ------------------------------------ 4 files changed, 84 insertions(+), 105 deletions(-) create mode 100644 xulmus/content/library.js diff --git a/common/content/completion.js b/common/content/completion.js index aa90fd03..34eed959 100644 --- a/common/content/completion.js +++ b/common/content/completion.js @@ -1392,20 +1392,24 @@ function Completion() //{{{ song: function song(context, args) { + // TODO: useful descriptions? + function map(list) list.map(function (i) [i, ""]); + let [artist, album] = [args[0], args[1]]; + if (args.completeArg == 0) { context.title = ["Artists"]; - context.completions = player.getArtists(); + context.completions = map(library.getArtists()); } else if (args.completeArg == 1) { - context.title = ["Albums by " + args[0]]; - context.completions = player.getAlbums(args[0]); + context.title = ["Albums by " + artist]; + context.completions = map(library.getAlbums(artist)); } else if (args.completeArg == 2) { - context.title = ["Tracks from " + args[1] + " by " + args[0]]; - context.completions = player.getTracks(args[0], args[1]); + context.title = ["Tracks from " + album + " by " + artist]; + context.completions = map(library.getTracks(artist, album)); } }, diff --git a/xulmus/content/config.js b/xulmus/content/config.js index 34da38e2..7ddca7a0 100644 --- a/xulmus/content/config.js +++ b/xulmus/content/config.js @@ -169,6 +169,7 @@ const config = { //{{{ "bookmarks.js", "tabs.js", "player.js", + "library.js" ], stop: function() { @@ -177,11 +178,9 @@ const config = { //{{{ init: function () { - //Adding a mode for Player + // Adding a mode for Player //modes.addMode("PLAYER"); // Player mode for songbird - // var artistArray = getArtists(); - // TODO: support 'nrformats'? -> probably not worth it --mst function incrementURL(count) { @@ -246,8 +245,8 @@ const config = { //{{{ liberator.loadModule("marks", Marks); liberator.loadModule("quickmarks", QuickMarks); liberator.loadModule("hints", Hints); - // Load the Player module liberator.loadModule("player", Player); + liberator.loadModule("library", Library); //////////////////////////////////////////////////////////////////////////////// ////////////////////// STYLES ////////////////////////////////////////////////// diff --git a/xulmus/content/library.js b/xulmus/content/library.js new file mode 100644 index 00000000..7487b37c --- /dev/null +++ b/xulmus/content/library.js @@ -0,0 +1,72 @@ +function Library() // {{{ +{ + //////////////////////////////////////////////////////////////////////////////// + ////////////////////// PRIVATE SECTION ///////////////////////////////////////// + /////////////////////////////////////////////////////////////////////////////{{{ + + const MAIN_LIBRARY = LibraryUtils.mainLibrary; + + function toJSArray(enum) ArrayConverter.JSArray(enum) + + function getArtistsArray() + { + return toJSArray(MAIN_LIBRARY.getDistinctValuesForProperty(SBProperties.artistName)); + } + + // Get the artist names before hand. + let artists = getArtistsArray(); + + /////////////////////////////////////////////////////////////////////////////}}} + ////////////////////// PUBLIC SECTION ////////////////////////////////////////// + /////////////////////////////////////////////////////////////////////////////{{{ + + // TODO: return some actually useful objects. ;-) + return { + + /** + * Returns an array of all the artist names in the main library. + * + * @returns {string[]} + */ + getArtists: function getArtists() artists, + + // FIXME: ken do we really want to remove duplicates? If so, why not tracks too? --djk + /** + * Returns an array of all the album names for artist in the + * main library. + * + * @param {param} artist The artist name. + * @returns {string[]} + */ + getAlbums: function getAlbums(artist) + { + let albums = toJSArray(MAIN_LIBRARY.getItemsByProperty(SBProperties.artistName, artist)) + .map(function (track) track.getProperty(SBProperties.albumName)); + return util.Array.uniq(albums); + }, + + /** + * Returns an array of all the track names for artist and + * album in the main library. + * + * @param {param} artist The artist name. + * @param {param} album The album name. + * @returns {string[]} + */ + getTracks: function getTracks(artist, album) + { + const properties = Cc["@songbirdnest.com/Songbird/Properties/MutablePropertyArray;1"] + .createInstance(Ci.sbIMutablePropertyArray); + + properties.appendProperty(SBProperties.artistName, artist); + properties.appendProperty(SBProperties.albumName, album); + + return toJSArray(MAIN_LIBRARY.getItemsByProperties(properties)) + .map(function (track) track.getProperty(SBProperties.trackName)); + } + + }; + //}}} +} // }}} + +// vim: set fdm=marker sw=4 ts=4 et: diff --git a/xulmus/content/player.js b/xulmus/content/player.js index 96c8d234..1ff1b437 100644 --- a/xulmus/content/player.js +++ b/xulmus/content/player.js @@ -11,47 +11,6 @@ function Player() // {{{ // Get the focus to the visible playlist first //window._SBShowMainLibrary(); - function getArtistsArray() - { - var list = LibraryUtils.mainLibrary; - - // Create an enumeration listener to count each item - var listener = { - count: 0, - onEnumerationBegin: function (aMediaList) { - this.count = 0; - }, - onEnumeratedItem: function (aMediaList, aMediaItem) { - this.count++; - }, - onEnumerationEnd: function (aMediaList, aStatusCode) {} - }; - - var artistCounts = {}; - var artists = list.getDistinctValuesForProperty(SBProperties.artistName); - var artist; - var artistArray = []; - var i = 0; - // Count the number of media items for each distinct artist - while (artists.hasMore()) - { - artist = artists.getNext(); - artistArray[i] = [artist, artist]; - list.enumerateItemsByProperty(SBProperties.artistName, - artist, - listener, - Ci.sbIMediaList.ENUMERATIONTYPE_LOCKING); - artistCounts[artist] = listener.count; - i++; - } - - //liberator.dump("Count : "+artistCounts.toSource()); - return artistArray; - } - - // Get the artist names before hand. - let artists = getArtistsArray(); - const pageService = Cc["@songbirdnest.com/Songbird/MediaPageManager;1"].getService(Ci.sbIMediaPageManager); // Register Callbacks for searching. @@ -684,61 +643,6 @@ function Player() // {{{ { if (gMM.sequencer.currentItem) gMM.sequencer.currentItem.setProperty(SBProperties.rating, rating); - }, - - getArtists: function getArtists() - { - return artists; - }, - - getAlbums: function getAlbums(artist) - { - var list = LibraryUtils.mainLibrary; - var albumArray = [], returnArray = []; - var items = list.getItemsByProperty(SBProperties.artistName, artist).enumerate(); - var i = 0, j = 0; - - while (items.hasMoreElements()) - { - album = items.getNext().getProperty(SBProperties.albumName); - albumArray[i] = [album, album]; - - if (i == 0) - { - returnArray[j] = albumArray[i]; - j++; - } - else if (albumArray[i-1].toString() != albumArray[i].toString()) - { - returnArray[i] = albumArray[i]; - j++; - } - i++; - } - - return returnArray; - }, - - getTracks: function getTracks(artist, album) - { - var list = LibraryUtils.mainLibrary; - var tracksArray = []; - var pa = Cc["@songbirdnest.com/Songbird/Properties/MutablePropertyArray;1"] - .createInstance(Ci.sbIMutablePropertyArray); - var i = 0; - - pa.appendProperty(SBProperties.artistName, artist.toString()); - pa.appendProperty(SBProperties.albumName, album.toString()); - var items = list.getItemsByProperties(pa).enumerate(); - - while (items.hasMoreElements()) - { - track = items.getNext().getProperty(SBProperties.trackName); - tracksArray[i] = [track, track]; - i++; - } - - return tracksArray; } };