Filtering & Searching Playlists & Libraries

If you've read the overview of Libraries, Playlists, and Tracks, you may be wondering how you can do free text search and/or filtering of playlists & libraries.

sbIMediaListView

Songbird has the concept of sbIMediaListViews.  Media list views are literally a "view of an sbIMediaList", where the view filters the media list in some way, either by applying a text-search-filter, or by filtering based on existing properties.  To create a new view, you simply call .createView() on the playlist.  For instance, to create a new unfiltered view of the main library:

Components.utils.import("resource://app/jsmodules/sbLibraryUtils.jsm");
Components.utils.import("resource://app/jsmodules/sbProperties.jsm");

var myView = LibraryUtils.mainLibrary.createView();

The view, at this time is unfiltered.  If we wanted to load this view we could get a reference to the main browser object, and call the loadMediaList() method:

// get a pointer to the global tab browser
if (typeof(gBrowser) == "undefined")
    var gBrowser =  Cc["@mozilla.org/appshell/window-mediator;1"]
            .getService(Ci.nsIWindowMediator)
            .getMostRecentWindow("Songbird:Main").window.gBrowser;
gBrowser.loadMediaList(LibraryUtils.mainLibrary, null, null, myView,
    "chrome://songbird/content/mediapages/filtersPage.xul");
If you've looked at the interface for .loadMediaList(), you may have seen that most of the parameters are optional, including the sbIMediaListView we passed as 'view'.  If we only wanted to load an unfiltered view, we could have simply passed the sbIMediaList (in this case, LibraryUtils.mainLibrary) to loadMediaList() without needing any of the other parameters.

So now let's get onto the interesting bit of how to actually apply filters to a media list view...

sbICascadeFilterSet

Filtering (whether by properties or text-searches) are done via sbICasecadeFilterSet instances.  Every sbIMediaListView has a .cascadeFilterSet member for this purpose.

Filtering Based on Properties

For now, if setting a cascadeFilterSet and exposing it to view with the Filter List media page, you need to have at least three filters set.  The standard set of filters are genre, artist name, and album name.  For this example, let's say we want to filter the main library (reusing our previous 'myView' media list view) to filter only on all tracks by the band, Weezer:
myView.cascadeFilterSet.appendFilter(SBProperties.genre);  
var propIndex = view.cascadeFilterSet.appendFilter(SBProperties.artistName);  
view.cascadeFilterSet.appendFilter(SBProperties.albumName);  


myView.cascadeFilterSet.set(propIndex, ["Weezer"], 1);

Note that if we had any hidden items with the artistName set to "Weezer", or playlists with the name "Weezer", they would show up in this view.  For this reason, we can use the sbIFilterableMediaListView's filterConstraint member and set it to an sbILibraryConstraint to further constrain the search results.  The LibraryUtils JSM handily defines a LibraryUtils.standardFilterConstraint sbILibraryConstraint value for us which filters out playlists and hidden items which we can use by doing: 

// By default, we never want to show lists and hidden 
// things in the playlist
myView.filterConstraint = LibraryUtils.standardFilterConstraint;

We can then load this view, as above by doing:

gBrowser.loadMediaList(LibraryUtils.mainLibrary, null, null, myView,
    "chrome://songbird/content/mediapages/filtersPage.xul");

Filtering Based on Free-Text-Search

If we want to filter down the media list view based on an arbitrary search term, you can manipulate the cascadeFilterSet by utilising its .appendSearch() routine (analogous to the above appendFilter() call)... but an even easier option is to utilise the LibraryUtils JSM again which defines a handy createStandardMediaListView() call which takes an sbIMediaList and a string to filter on:
var myView =
    LibraryUtils.createStandardMediaListView(LibraryUtils.mainLibrary, "Danger")

This would create a new sbIMediaListView view of the main library filtered on all results matching "Danger".  This text search applies over the default filter properties, namely: Genre, Artist, and Album.

Tag page
You must login to post a comment.