1 /* vi: set et sw=4 ts=4 si: */
3 * Copyright (c) 2012, Intel Corporation.
5 * This program is licensed under the terms and conditions of the
6 * Apache License, version 2.0. The full text of the Apache License is at
7 * http://www.apache.org/licenses/LICENSE-2.0
12 var library = function() {
22 resolveBackend: MusicBrainz,
24 errorCB: function(error) {
25 console.log("HoofbeatsLibrary.errorCB: " + error.name);
26 this.deferred.reject();
27 throw new Error(error.name);
30 findCB: function(items) {
31 items.forEach(function(item, index, items) {
32 _priv.mediaItems.push(item);
33 if (_priv.resolveBackend !== undefined) {
34 _priv.resolveBackend.getArtist(item.artists[0]).done(
37 "HoofbeatsLibrary.findCB: " +
38 "item resolved on " + _priv.resolveBackend.name() +
45 if (items.length == _priv.fetchCount && !_priv.oneTime) {
46 // There *might* be more items.
47 _priv.fetchOffset += _priv.fetchCount;
49 _priv.findCB.bind(this),
50 _priv.errorCB.bind(this),
57 _priv.deferred.resolve();
65 initialize: function() {
66 if (_priv.initialized)
69 if (win.tizen === undefined) {
70 throw Error("You need the Tizen web API to run Hoofbeats.");
73 _priv.audioTypeFilter = new tizen.AttributeFilter(
74 "type", "EXACTLY", "AUDIO");
75 _priv.videoTypeFilter = new tizen.AttributeFilter(
76 "type", "EXACTLY", "VIDEO");
77 _priv.typeFilter = new tizen.CompositeFilter(
78 "UNION", [_priv.audioTypeFilter, _priv.videoTypeFilter]);
81 scan: function(options) {
82 var opts = options || {};
83 if (opts.resolveBackend !== MusicBrainz) {
84 _priv.resolveBackend = opts.resolveBackend;
87 _priv.deferred = new $.Deferred();
90 _priv.mediaItems = [];
91 _priv.fetchOffset = 0;
93 if (opts.count !== undefined) {
94 _priv.fetchCount = opts.count;
99 _priv.findCB.bind(this),
100 _priv.errorCB.bind(this),
107 return _priv.deferred.promise();
113 _priv.mediaItems.forEach(function(item, index, items) {
121 // Some simple getters
122 getInitialized: function() { return _priv.initialized; },
123 getItems: function() { return _priv.mediaItems; },
124 getSize: function() { return _priv.mediaItems.length; },
125 getResolve: function() {return _priv.resolveBackend !== undefined; }
129 win.HoofbeatsLibrary = library;