]> git.donarmstrong.com Git - dactyl.git/blob - melodactyl/content/library.js
be884741d19e5637202d34eb7953fc2b3ef5d572
[dactyl.git] / melodactyl / content / library.js
1 // Copyright (c) 2009 by Prathyush Thota <prathyushthota@gmail.com>
2 // Copyright (c) 2009 by Doug Kearns <dougkearns@gmail.com>
3 //
4 // This work is licensed for reuse under an MIT license. Details are
5 // given in the LICENSE.txt file included with this file.
6 "use strict";
7
8 // TODO: flesh this out
9 const Library = Module("library", {
10     init: function () {
11         this.MAIN_LIBRARY = LibraryUtils.mainLibrary;
12     },
13
14     /**
15      * Converts an XPCOM enumerator to a JavaScript array.
16      *
17      * @param {nsISimpleEnumerator|nsIStringEnumerator|nsIArray} enum The enumerator to convert.
18      * @returns {Array}
19      */
20     _toJSArray: function _toJSArray(enum) ArrayConverter.JSArray(enum),
21
22     /**
23      * Returns an array of all the artist names in the main library.
24      *
25      * @returns {string[]}
26      */
27     getArtists: function getArtists() this._toJSArray(this.MAIN_LIBRARY.getDistinctValuesForProperty(SBProperties.artistName)),
28
29     // FIXME: Prathyush do we really want to remove duplicates? If so, why not tracks too? --djk
30     /**
31      * Returns an array of all the album names for *artist* in the main
32      * library.
33      *
34      * @param {string} artist The artist's name.
35      * @returns {string[]}
36      */
37     getAlbums: function getAlbums(artist) {
38         let albums = this._toJSArray(this.MAIN_LIBRARY.getItemsByProperty(SBProperties.artistName, artist))
39                          .map(function (track) track.getProperty(SBProperties.albumName));
40         return array.uniq(albums);
41     },
42
43     /**
44      * Returns an array of all the track names for *artist* and *album* in the
45      * main library.
46      *
47      * @param {string} artist The artist's name.
48      * @param {string} album The album's name.
49      * @returns {string[]}
50      */
51     getTracks: function getTracks(artist, album) {
52         let properties = services.MutablePropertyArray();
53
54         properties.appendProperty(SBProperties.artistName, artist);
55         properties.appendProperty(SBProperties.albumName, album);
56
57         return this._toJSArray(this.MAIN_LIBRARY.getItemsByProperties(properties))
58                    .map(function (track) track.getProperty(SBProperties.trackName));
59     }
60 }, {
61 }, {
62 });
63
64 // vim: set fdm=marker sw=4 ts=4 et: