Add Intel copyright header.
[profile/ivi/cowhide.git] / examples / hoofbeats / javascripts / library.js
1 /* vi: set et sw=4 ts=4 si: */
2 /*
3  * Copyright (c) 2012, Intel Corporation.
4  *
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
8  *
9  */
10
11 (function(win, $) {
12     var library = function() {
13         // Private stuff
14
15         var _priv = {
16             initialized: false,
17             deferred: undefined,
18             fetchCount: 100,
19             fetchOffset: 0,
20             oneTime: false,
21             mediaItems: [],
22             resolveBackend: MusicBrainz,
23
24             errorCB: function(error) {
25                 console.log("HoofbeatsLibrary.errorCB: " + error.name);
26                 this.deferred.reject();
27                 throw new Error(error.name);
28             },
29
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(
35                             function(data) {
36                                 console.log(
37                                     "HoofbeatsLibrary.findCB: " +
38                                     "item resolved on " + _priv.resolveBackend.name() +
39                                     ": " + data.name);
40                             }
41                         );
42                     }
43                 });
44
45                 if (items.length == _priv.fetchCount && !_priv.oneTime) {
46                     // There *might* be more items.
47                     _priv.fetchOffset += _priv.fetchCount;
48                     tizen.content.find(
49                         _priv.findCB.bind(this),
50                         _priv.errorCB.bind(this),
51                         null,
52                         _priv.typeFilter,
53                         null,
54                         _priv.fetchCount,
55                         _priv.fetchOffset);
56                 } else {
57                     _priv.deferred.resolve();
58                 }
59             }
60         }; // _priv
61
62
63         // Public stuff
64         return {
65             initialize: function() {
66                 if (_priv.initialized)
67                     return;
68
69                 if (win.tizen === undefined) {
70                     throw Error("You need the Tizen web API  to run Hoofbeats.");
71                 }
72
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]);
79             },
80
81             scan: function(options) {
82                 var opts = options || {};
83                 if (opts.resolveBackend !== MusicBrainz) {
84                     _priv.resolveBackend = opts.resolveBackend;
85                 }
86
87                 _priv.deferred = new $.Deferred();
88                 this.initialize();
89
90                 _priv.mediaItems = [];
91                 _priv.fetchOffset = 0;
92
93                 if (opts.count !== undefined) {
94                     _priv.fetchCount = opts.count;
95                     _priv.oneTime = true;
96                 }
97
98                 tizen.content.find(
99                     _priv.findCB.bind(this),
100                     _priv.errorCB.bind(this),
101                     null,
102                     _priv.typeFilter,
103                     null,
104                     _priv.fetchCount,
105                     _priv.fetchOffset);
106
107                 return _priv.deferred.promise();
108             },
109
110             item: function(id) {
111                 var ret;
112
113                 _priv.mediaItems.forEach(function(item, index, items) {
114                     if (item.id == id)
115                         ret = item;
116                 });
117
118                 return ret;
119             },
120
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; }
126         };
127     };
128
129     win.HoofbeatsLibrary = library;
130 }(window, jQuery));