(hoofbeats) Added MusicBrainz API implementation (only getArtist so far).
[profile/ivi/cowhide.git] / examples / hoofbeats / javascripts / library.js
1 /* vi: set et sw=4 ts=4 si: */
2 $(function() {
3     var library = function() {
4         // For readability:
5         this.initialized = false;
6
7         this.fetchCount = 100;
8         this.fetchOffset = 0;
9         this.mediaItems = [];
10
11         this.initialize = function() {
12             if (this.initialized)
13                 return;
14
15             if (window.tizen === undefined) {
16                 throw Error("You need the Tizen web API  to run Hoofbeats.");
17             }
18
19             this.audioTypeFilter = new tizen.AttributeFilter(
20                 "type", "EXACTLY", "AUDIO");
21             this.videoTypeFilter = new tizen.AttributeFilter(
22                 "type", "EXACTLY", "VIDEO");
23             this.typeFilter = new tizen.CompositeFilter(
24                 "UNION", [this.audioTypeFilter, this.videoTypeFilter]);
25
26             this.sortMode = new tizen.SortMode("trackNumber", "ASC");
27             this.mediaSource = tizen.mediacontent.getLocalMediaSource();
28         };
29
30         this.scan = function() {
31             this.initialize()
32
33             this.mediaItems = [];
34             this.fetchOffset = 0;
35
36             this.mediaSource.findItems(
37                 this.findItemsCB.bind(this),
38                 this.errorCB.bind(this),
39                 null,
40                 this.typeFilter,
41                 this.sortMode,
42                 this.fetchCount,
43                 this.fetchOffset);
44         };
45
46         this.errorCB = function(error) {
47             console.log("Error: " + error.name);
48             throw new Error(error.name);
49         };
50
51         this.findItemsCB = function(items) {
52             var self = this;
53
54             items.forEach(function(item, index, items) {
55                 self.mediaItems.push(item);
56                 window.MusicBrainz.getArtist(item.name).done(function(data) {
57                     console.log(data);
58                 });
59             });
60
61             if (items.length == this.fetchCount) {
62                 // There *might* be more items.
63                 this.fetchOffset += this.fetchCount;
64                 this.mediaSource.findItems(
65                     this.findItemsCB.bind(this),
66                     this.errorCB.bind(this),
67                     null,
68                     this.typeFilter,
69                     this.sortMode,
70                     this.fetchCount,
71                     this.fetchOffset);
72             }
73         };
74     };
75
76     library.prototype = {
77         set initialized(value) {this._initialized = value; },
78         get initialized() { return this._initialized; }
79     };
80
81     window.HoofbeatsLibrary = library;
82 });