(hoofbeats) Add support for pluggable resolvers.
[profile/ivi/cowhide.git] / examples / hoofbeats / javascripts / library.js
1 /* vi: set et sw=4 ts=4 si: */
2 (function(win, $) {
3     var library = function() {
4         // Private stuff
5
6         var _priv = {
7             initialized: false,
8             deferred: undefined,
9             fetchCount: 100,
10             fetchOffset: 0,
11             oneTime: false,
12             mediaItems: [],
13             resolveBackend: MusicBrainz,
14
15             errorCB: function(error) {
16                 console.log("HoofbeatsLibrary.errorCB: " + error.name);
17                 this.deferred.reject();
18                 throw new Error(error.name);
19             },
20
21             findCB: function(items) {
22                 items.forEach(function(item, index, items) {
23                     _priv.mediaItems.push(item);
24                     if (_priv.resolveBackend !== undefined) {
25                         _priv.resolveBackend.getArtist(item.artists[0]).done(
26                             function(data) {
27                                 console.log(
28                                     "HoofbeatsLibrary.findCB: " +
29                                     "item resolved on " + _priv.resolveBackend.name() +
30                                     ": " + data.name);
31                             }
32                         );
33                     }
34                 });
35
36                 if (items.length == _priv.fetchCount && !_priv.oneTime) {
37                     // There *might* be more items.
38                     _priv.fetchOffset += _priv.fetchCount;
39                     tizen.content.find(
40                         _priv.findCB.bind(this),
41                         _priv.errorCB.bind(this),
42                         null,
43                         _priv.typeFilter,
44                         null,
45                         _priv.fetchCount,
46                         _priv.fetchOffset);
47                 } else {
48                     _priv.deferred.resolve();
49                 }
50             }
51         }; // _priv
52
53
54         // Public stuff
55         return {
56             initialize: function() {
57                 if (_priv.initialized)
58                     return;
59
60                 if (win.tizen === undefined) {
61                     throw Error("You need the Tizen web API  to run Hoofbeats.");
62                 }
63
64                 _priv.audioTypeFilter = new tizen.AttributeFilter(
65                     "type", "EXACTLY", "AUDIO");
66                 _priv.videoTypeFilter = new tizen.AttributeFilter(
67                     "type", "EXACTLY", "VIDEO");
68                 _priv.typeFilter = new tizen.CompositeFilter(
69                     "UNION", [_priv.audioTypeFilter, _priv.videoTypeFilter]);
70             },
71
72             scan: function(options) {
73                 var opts = options || {};
74                 if (opts.resolveBackend !== MusicBrainz) {
75                     _priv.resolveBackend = opts.resolveBackend;
76                 }
77
78                 _priv.deferred = new $.Deferred();
79                 this.initialize();
80
81                 _priv.mediaItems = [];
82                 _priv.fetchOffset = 0;
83
84                 if (opts.count !== undefined) {
85                     _priv.fetchCount = opts.count;
86                     _priv.oneTime = true;
87                 }
88
89                 tizen.content.find(
90                     _priv.findCB.bind(this),
91                     _priv.errorCB.bind(this),
92                     null,
93                     _priv.typeFilter,
94                     null,
95                     _priv.fetchCount,
96                     _priv.fetchOffset);
97
98                 return _priv.deferred.promise();
99             },
100
101             item: function(id) {
102                 var ret;
103
104                 _priv.mediaItems.forEach(function(item, index, items) {
105                     if (item.id == id)
106                         ret = item;
107                 });
108
109                 return ret;
110             },
111
112             // Some simple getters
113             getInitialized: function() { return _priv.initialized; },
114             getItems: function() { return _priv.mediaItems; },
115             getSize: function() { return _priv.mediaItems.length; },
116             getResolve: function() {return _priv.resolveBackend !== undefined; }
117         };
118     };
119
120     win.HoofbeatsLibrary = library;
121 }(window, jQuery));