forked from lucifurious/jquery-xbmc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.xbmc.js
273 lines (235 loc) · 6.54 KB
/
jquery.xbmc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/**
* jquery.xbmc.js
* Copyright 2011, Pogostick, LLC. (http://www.pogostick.com/)
*
* Dual licensed under the MIT License and the GNU General Public License (GPL) Version 2.
* See {@link http://www.pogostick.com/licensing/} for complete information.
*
* @copyright Copyright 2011, Pogostick, LLC. (http://www.pogostick.com/)
* @link http://github.com/lucifurious/jquery-xbmc/ XBMC Plug-In for jQuery
* @license http://www.pogostick.com/licensing/
* @author Jerry Ablan <[email protected]>
* @package jquery-xbmc
* @version 1.0
* @filesource
*/
;
(function($) {
//********************************************************************************
//* XBMC Client Plug-in
//********************************************************************************
/**
* Create our plug-in
* @param options
*/
$.xbmc = function(options) {
//*************************************************************************
//* Properties
//*************************************************************************
/**
* @const object Our defaults
*/
var defaults = {
/**
* @var string The XBMC server
*/
serverHostName : 'localhost',
/**
* @var int The server's port
*/
serverPort : 8080,
/**
* @var string Server access user
*/
serverUserName : 'xbmc',
/**
* @var string Server access password
*/
serverPassword : null,
/**
* @var boolean Server available flag
*/
serverAvailable : false,
/**
* @var object An overridable mapping of available namespaces and methods
*/
namespaceMap : {
'JSONRPC.Introspect' : {}
},
/**
* @var object A lookup cache of called methods
*/
_namespaceCache : {},
/**
* @var object A lookup cache for labels
*/
_labelCache : {},
/**
* @var string
*/
_lastLabel : null
};
/**
* @const jQuery Our evil twin
*/
var _this = this;
/**
* @var object Our settings
*/
_this.settings = {};
//*************************************************************************
//* Private Plug-in Methods
//*************************************************************************
/**
* Finds the method name in a namespace, returns false if not found
* @param namespace
* @param cmd
*/
var _hasCommand = function(namespace,cmd) {
var _command = namespace + '.' + cmd;
// Cached?
if (_this.settings._namespaceCache[_command]) {
return _this.settings._namespaceCache[_command];
}
// Look it up
for ( var _methodName in _this.settings.namespaceMap )
{
if (_command.toLowerCase().trim() == _methodName.toLowerCase().trim()) {
_this.settings._namespaceCache[_command] = _methodName;
return _methodName;
}
}
return false;
};
/**
* Makes an arbitrary XBMC JSONRPC request
* @param namespace
* @param cmd
* @param options
*/
var _xbmcCommand = function(namespace, cmd, options) {
var _command = _hasCommand(namespace,cmd);
if (!_command) {
throw "The command \"" + cmd + "\" is not valid.";
} else {
// Allow options to be the success and error method
if ($.isFunction(options)) {
var _callback = options;
options = {
success: _callback,
error: _callback
};
}
var _options = $.extend({}, options);
if (!_options.params) {
_options.params = {};
}
// Otherwise send on to server...
return $.jsonRPC.request(_command, _options);
}
};
/**
* Constructor
*/
var _init = function() {
_this.settings = $.extend({}, defaults, options);
// Initialize jsonRPC plug-in
$.jsonRPC.setup({
endPoint: 'http://' + _this.settings.serverHostName + ':' + _this.settings.serverPort + '/jsonrpc'
});
/**
* @var object Introspective method list
*/
var _xbmcMethods = null;
// Call introspect and populate the namespace map...
_xbmcCommand(
'JSONRPC',
'introspect',
{
success : function(response) {
if (!response.result) {
alert('Cannot retrieve introspect from server. Cannot continue.');
throw 'Cannot retrieve introspect from server. Cannot continue.';
} else {
if (response.result.methods) {
_xbmcMethods = response.result.methods;
}
}
},
async : false
}
);
if (!_xbmcMethods) {
alert('Cannot/did not retrieve introspect from server. Cannot continue.');
throw 'Cannot/did not retrieve introspect from server. Cannot continue.';
} else {
for ( var _methodName in _xbmcMethods ) {
if (_methodName) {
var _parts = _methodName.split('.');
if (2 == _parts.length) {
if (!_this.settings.namespaceMap[_methodName]) {
_this.settings.namespaceMap[_methodName] = _xbmcMethods[_methodName];
// console.log('Added command: ' + _methodName);
}
// If there is no namespace helper function, define it
if (!$.isFunction(_this[_parts[0]])) {
var _function = "_this['" + _parts[0] + "'] = function(cmd,options) { return _xbmcCommand('" + _parts[0] + "',cmd,options); }";
eval(_function);
// console.log('Added method: _this.' + _parts[0]);
}
}
}
}
}
};
//*************************************************************************
//* Public XBMC Helper Methods
//*************************************************************************
_this.getItemList = function(itemType, label) {
var _id = itemType + 'id';
var _label = label || 'label';
};
/**
* Gets a single info label
* @param label
*/
_this.getLabel = function( label ) {
var _result = false;
_xbmc.getInfoLabels( label, function(response) {
if (response.result) {
_result = response.result[label];
}
}, false);
return _result;
};
/**
* Given one or more info labels, retrieve and return them
* @param label One or more labels. Accepts string or array
*/
_this.getInfoLabels = function(label, callback, async) {
var _labels = label;
var _singleLabel = false;
var _result =
[
];
if (!$.isArray(_labels)) {
_labels = new Array(_labels);
_singleLabel = true;
}
// Make the call...
_this.System('getInfoLabels', {
params : {
labels : _labels
},
success : callback,
// Synchronous unless otherwise specified
async : true === async
});
};
//*************************************************************************
//* And kick it all off by calling init()
//*************************************************************************
// Construct!
_init();
};
})(jQuery);