User Tools

Site Tools


developer_center:recipe_book:extensions_and_core:media_library:enumerating_a_library_or_list

Enumerating a Library or List

Nightingale 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());
developer_center/recipe_book/extensions_and_core/media_library/enumerating_a_library_or_list.txt · Last modified: 2013/12/28 09:07 by geekshadow