Enumerating a Library or List

    Table of contents
    No headers

    Songbird does not include a statistics API just yet, so finding out things like "how many tracks do I have for each artist in my library" isn't entirely straightforward.

    The following example shows one way to solve this problem using the existing API.  

    // Make sure we have the javascript modules we're going to use
    if (!window.SBProperties) 
      Cu.import("resource://app/jsmodules/sbProperties.jsm");
    if (!window.LibraryUtils) 
      Cu.import("resource://app/jsmodules/sbLibraryUtils.jsm");
    
    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;
    
    // Count the number of media items for each distinct artist
    while (artists.hasMore()) {
      artist = artists.getNext();
      list.enumerateItemsByProperty(SBProperties.artistName,
                                    artist,
                                    listener,
                                    Ci.sbIMediaList.ENUMERATIONTYPE_LOCKING);
      artistCounts[artist] = listener.count;
    }
    
    alert(artistCounts.toSource());

    The same enumeration is possible with .enumerateAllItems() to walk all the items in a list. 

    Tag page
    • No tags
    You must login to post a comment.
    Powered by MindTouch Core
    Real Time Web Analytics