summaryrefslogtreecommitdiff
path: root/melodactyl/content/library.js
blob: 5ab691ebd72625f7802ce6c9394b40d6fa56b3e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright (c) 2009 by Prathyush Thota <prathyushthota@gmail.com>
// Copyright (c) 2009 by Doug Kearns <dougkearns@gmail.com>
//
// This work is licensed for reuse under an MIT license. Details are
// given in the LICENSE.txt file included with this file.
"use strict";

// TODO: flesh this out
const Library = Module("library", {
    init: function () {
        this.MAIN_LIBRARY = LibraryUtils.mainLibrary;
    },

    /**
     * Converts an XPCOM enumerator to a JavaScript array.
     *
     * @param {nsISimpleEnumerator|nsIStringEnumerator|nsIArray} enum The enumerator to convert.
     * @returns {Array}
     */
    _toJSArray: function _toJSArray(enum) ArrayConverter.JSArray(enum),

    /**
     * Returns an array of all the artist names in the main library.
     *
     * @returns {[string]}
     */
    getArtists: function getArtists() this._toJSArray(this.MAIN_LIBRARY.getDistinctValuesForProperty(SBProperties.artistName)),

    // FIXME: Prathyush 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 {string} artist The artist's name.
     * @returns {[string]}
     */
    getAlbums: function getAlbums(artist) {
        let albums = this._toJSArray(this.MAIN_LIBRARY.getItemsByProperty(SBProperties.artistName, artist))
                         .map(function (track) track.getProperty(SBProperties.albumName));
        return array.uniq(albums);
    },

    /**
     * Returns an array of all the track names for *artist* and *album* in the
     * main library.
     *
     * @param {string} artist The artist's name.
     * @param {string} album The album's name.
     * @returns {[string]}
     */
    getTracks: function getTracks(artist, album) {
        let properties = services.MutablePropertyArray();

        properties.appendProperty(SBProperties.artistName, artist);
        properties.appendProperty(SBProperties.albumName, album);

        return this._toJSArray(this.MAIN_LIBRARY.getItemsByProperties(properties))
                   .map(function (track) track.getProperty(SBProperties.trackName));
    }
}, {
}, {
});

// vim: set fdm=marker sw=4 sts=4 ts=8 et: