ApiClient.js 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070
  1. if (!window.MediaBrowser) {
  2. window.MediaBrowser = {};
  3. }
  4. MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout) {
  5. /**
  6. * Creates a new api client instance
  7. * @param {String} serverProtocol
  8. * @param {String} serverHostName
  9. * @param {String} serverPortNumber
  10. * @param {String} clientName
  11. */
  12. return function (serverProtocol, serverHostName, serverPortNumber, clientName) {
  13. if (!serverProtocol) {
  14. throw new Error("Must supply a serverProtocol, e.g. http:");
  15. }
  16. if (!serverHostName) {
  17. throw new Error("Must supply serverHostName, e.g. 192.168.1.1 or myServerName");
  18. }
  19. if (!serverPortNumber) {
  20. throw new Error("Must supply a serverPortNumber");
  21. }
  22. var self = this;
  23. var deviceName = "Web Browser";
  24. var deviceId = MediaBrowser.SHA1(navigator.userAgent + (navigator.cpuClass || ""));
  25. var currentUserId;
  26. var webSocket;
  27. /**
  28. * Gets the server host name.
  29. */
  30. self.serverHostName = function () {
  31. return serverHostName;
  32. };
  33. /**
  34. * Gets the server port number.
  35. */
  36. self.serverPortNumber = function () {
  37. return serverPortNumber;
  38. };
  39. /**
  40. * Gets or sets the current user id.
  41. */
  42. self.currentUserId = function (val) {
  43. if (val !== undefined) {
  44. currentUserId = val;
  45. } else {
  46. return currentUserId;
  47. }
  48. };
  49. deviceName = (function () {
  50. var name = "";
  51. if ($.browser.chrome) {
  52. name = "Chrome";
  53. }
  54. else if ($.browser.safari) {
  55. name = "Safari";
  56. }
  57. else if ($.browser.webkit) {
  58. name = "WebKit";
  59. }
  60. else if ($.browser.msie) {
  61. name = "Internet Explorer";
  62. }
  63. else if ($.browser.firefox || $.browser.mozilla) {
  64. name = "Firefox";
  65. }
  66. else if ($.browser.opera) {
  67. name = "Opera";
  68. }
  69. else {
  70. name = "Web Browser";
  71. }
  72. if ($.browser.ipad) {
  73. name += " Ipad";
  74. }
  75. else if ($.browser.iphone) {
  76. name += " Iphone";
  77. }
  78. else if ($.browser.android) {
  79. name += " Android";
  80. }
  81. return name;
  82. }());
  83. /**
  84. * Wraps around jQuery ajax methods to add additional info to the request.
  85. */
  86. self.ajax = function (request) {
  87. if (!request) {
  88. throw new Error("Request cannot be null");
  89. }
  90. if (clientName) {
  91. var auth = 'MediaBrowser Client="' + clientName + '", Device="' + deviceName + '", DeviceId="' + deviceId + '"';
  92. if (currentUserId) {
  93. auth += ', UserId="' + currentUserId + '"';
  94. }
  95. request.headers = {
  96. Authorization: auth
  97. };
  98. }
  99. return $.ajax(request);
  100. };
  101. /**
  102. * Creates an api url based on a handler name and query string parameters
  103. * @param {String} name
  104. * @param {Object} params
  105. */
  106. self.getUrl = function (name, params) {
  107. if (!name) {
  108. throw new Error("Url name cannot be empty");
  109. }
  110. var url = serverProtocol + "//" + serverHostName + ":" + serverPortNumber + "/mediabrowser/" + name;
  111. if (params) {
  112. url += "?" + $.param(params);
  113. }
  114. return url;
  115. };
  116. self.openWebSocket = function (port) {
  117. var url = "ws://" + serverHostName + ":" + port + "/mediabrowser";
  118. webSocket = new WebSocket(url);
  119. webSocket.onmessage = function (msg) {
  120. msg = JSON.parse(msg.data);
  121. $(self).trigger("websocketmessage", [msg]);
  122. };
  123. webSocket.onopen = function () {
  124. setTimeout(function () {
  125. $(self).trigger("websocketopen");
  126. }, 500);
  127. };
  128. webSocket.onerror = function () {
  129. setTimeout(function () {
  130. $(self).trigger("websocketerror");
  131. }, 0);
  132. };
  133. webSocket.onclose = function () {
  134. setTimeout(function () {
  135. $(self).trigger("websocketclose");
  136. }, 0);
  137. };
  138. };
  139. self.sendWebSocketMessage = function (name, data) {
  140. var msg = { MessageType: name };
  141. if (data) {
  142. msg.Data = data;
  143. }
  144. msg = JSON.stringify(msg);
  145. webSocket.send(msg);
  146. };
  147. self.isWebSocketOpen = function () {
  148. return webSocket && webSocket.readyState === WebSocket.OPEN;
  149. };
  150. self.isWebSocketOpenOrConnecting = function () {
  151. return webSocket && (webSocket.readyState === WebSocket.OPEN || webSocket.readyState === WebSocket.CONNECTING);
  152. };
  153. /**
  154. * Gets an item from the server
  155. * Omit itemId to get the root folder.
  156. */
  157. self.getItem = function (userId, itemId) {
  158. if (!userId) {
  159. throw new Error("null userId");
  160. }
  161. var url = self.getUrl("Users/" + userId + "/Items/" + itemId);
  162. return self.ajax({
  163. type: "GET",
  164. url: url,
  165. dataType: "json"
  166. });
  167. };
  168. /**
  169. * Gets the root folder from the server
  170. */
  171. self.getRootFolder = function (userId) {
  172. if (!userId) {
  173. throw new Error("null userId");
  174. }
  175. var url = self.getUrl("Users/" + userId + "/Items/Root");
  176. return self.ajax({
  177. type: "GET",
  178. url: url,
  179. dataType: "json"
  180. });
  181. };
  182. /**
  183. * Gets the current server status
  184. */
  185. self.getSystemInfo = function () {
  186. var url = self.getUrl("System/Info");
  187. return self.ajax({
  188. type: "GET",
  189. url: url,
  190. dataType: "json"
  191. });
  192. };
  193. /**
  194. * Gets all cultures known to the server
  195. */
  196. self.getCultures = function () {
  197. var url = self.getUrl("Localization/cultures");
  198. return self.ajax({
  199. type: "GET",
  200. url: url,
  201. dataType: "json"
  202. });
  203. };
  204. /**
  205. * Gets all countries known to the server
  206. */
  207. self.getCountries = function () {
  208. var url = self.getUrl("Localization/countries");
  209. return self.ajax({
  210. type: "GET",
  211. url: url,
  212. dataType: "json"
  213. });
  214. };
  215. /**
  216. * Gets plugin security info
  217. */
  218. self.getPluginSecurityInfo = function () {
  219. var url = self.getUrl("Plugins/SecurityInfo");
  220. return self.ajax({
  221. type: "GET",
  222. url: url,
  223. dataType: "json"
  224. });
  225. };
  226. /**
  227. * Gets the directory contents of a path on the server
  228. */
  229. self.getDirectoryContents = function (path, options) {
  230. if (!path) {
  231. throw new Error("null path");
  232. }
  233. options = options || {};
  234. options.path = path;
  235. var url = self.getUrl("Environment/DirectoryContents", options);
  236. return self.ajax({
  237. type: "GET",
  238. url: url,
  239. dataType: "json"
  240. });
  241. };
  242. /**
  243. * Gets a list of physical drives from the server
  244. */
  245. self.getDrives = function () {
  246. var url = self.getUrl("Environment/Drives");
  247. return self.ajax({
  248. type: "GET",
  249. url: url,
  250. dataType: "json"
  251. });
  252. };
  253. /**
  254. * Gets a list of network devices from the server
  255. */
  256. self.getNetworkDevices = function () {
  257. var url = self.getUrl("Environment/NetworkDevices");
  258. return self.ajax({
  259. type: "GET",
  260. url: url,
  261. dataType: "json"
  262. });
  263. };
  264. /**
  265. * Cancels a package installation
  266. */
  267. self.cancelPackageInstallation = function (installationId) {
  268. if (!installationId) {
  269. throw new Error("null installationId");
  270. }
  271. var url = self.getUrl("Packages/Installing/" + id);
  272. return self.ajax({
  273. type: "DELETE",
  274. url: url,
  275. dataType: "json"
  276. });
  277. };
  278. /**
  279. * Installs or updates a new plugin
  280. */
  281. self.installPlugin = function (name, updateClass, version) {
  282. if (!name) {
  283. throw new Error("null name");
  284. }
  285. if (!updateClass) {
  286. throw new Error("null updateClass");
  287. }
  288. var options = {
  289. updateClass: updateClass
  290. };
  291. if (version) {
  292. options.version = version;
  293. }
  294. var url = self.getUrl("Packages/Installed/" + name, options);
  295. return self.ajax({
  296. type: "POST",
  297. url: url
  298. });
  299. };
  300. /**
  301. * Instructs the server to perform a pending kernel reload or app restart.
  302. * If a restart is not currently required, nothing will happen.
  303. */
  304. self.performPendingRestart = function () {
  305. var url = self.getUrl("System/Restart");
  306. return self.ajax({
  307. type: "POST",
  308. url: url
  309. });
  310. };
  311. /**
  312. * Gets information about an installable package
  313. */
  314. self.getPackageInfo = function (name) {
  315. if (!name) {
  316. throw new Error("null name");
  317. }
  318. var url = self.getUrl("Packages/" + name);
  319. return self.ajax({
  320. type: "GET",
  321. url: url,
  322. dataType: "json"
  323. });
  324. };
  325. /**
  326. * Gets the latest available application update (if any)
  327. */
  328. self.getAvailableApplicationUpdate = function () {
  329. var url = self.getUrl("Packages/Updates", { PackageType: "System" });
  330. return self.ajax({
  331. type: "GET",
  332. url: url,
  333. dataType: "json"
  334. });
  335. };
  336. /**
  337. * Gets the latest available plugin updates (if any)
  338. */
  339. self.getAvailablePluginUpdates = function () {
  340. var url = self.getUrl("Packages/Updates", { PackageType: "UserInstalled" });
  341. return self.ajax({
  342. type: "GET",
  343. url: url,
  344. dataType: "json"
  345. });
  346. };
  347. /**
  348. * Gets the virtual folder for a view. Specify a userId to get a user view, or omit for the default view.
  349. */
  350. self.getVirtualFolders = function (userId) {
  351. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  352. url = self.getUrl(url);
  353. return self.ajax({
  354. type: "GET",
  355. url: url,
  356. dataType: "json"
  357. });
  358. };
  359. /**
  360. * Gets all the paths of the locations in the physical root.
  361. */
  362. self.getPhysicalPaths = function () {
  363. var url = self.getUrl("Library/PhysicalPaths");
  364. return self.ajax({
  365. type: "GET",
  366. url: url,
  367. dataType: "json"
  368. });
  369. };
  370. /**
  371. * Gets the current server configuration
  372. */
  373. self.getServerConfiguration = function () {
  374. var url = self.getUrl("System/Configuration");
  375. return self.ajax({
  376. type: "GET",
  377. url: url,
  378. dataType: "json"
  379. });
  380. };
  381. /**
  382. * Gets the server's scheduled tasks
  383. */
  384. self.getScheduledTasks = function () {
  385. var url = self.getUrl("ScheduledTasks");
  386. return self.ajax({
  387. type: "GET",
  388. url: url,
  389. dataType: "json"
  390. });
  391. };
  392. /**
  393. * Starts a scheduled task
  394. */
  395. self.startScheduledTask = function (id) {
  396. if (!id) {
  397. throw new Error("null id");
  398. }
  399. var url = self.getUrl("ScheduledTasks/Running/" + id);
  400. return self.ajax({
  401. type: "POST",
  402. url: url
  403. });
  404. };
  405. /**
  406. * Gets a scheduled task
  407. */
  408. self.getScheduledTask = function (id) {
  409. if (!id) {
  410. throw new Error("null id");
  411. }
  412. var url = self.getUrl("ScheduledTasks/" + id);
  413. return self.ajax({
  414. type: "GET",
  415. url: url,
  416. dataType: "json"
  417. });
  418. };
  419. /**
  420. * Stops a scheduled task
  421. */
  422. self.stopScheduledTask = function (id) {
  423. if (!id) {
  424. throw new Error("null id");
  425. }
  426. var url = self.getUrl("ScheduledTasks/Running/" + id);
  427. return self.ajax({
  428. type: "DELETE",
  429. url: url,
  430. dataType: "json"
  431. });
  432. };
  433. /**
  434. * Gets the configuration of a plugin
  435. * @param {String} Id
  436. */
  437. self.getPluginConfiguration = function (id) {
  438. if (!id) {
  439. throw new Error("null Id");
  440. }
  441. var url = self.getUrl("Plugins/" + id + "/Configuration");
  442. return self.ajax({
  443. type: "GET",
  444. url: url,
  445. dataType: "json"
  446. });
  447. };
  448. /**
  449. * Gets a list of plugins that are available to be installed
  450. */
  451. self.getAvailablePlugins = function (options) {
  452. options = $.extend({}, options || {});
  453. options.PackageType = "UserInstalled";
  454. var url = self.getUrl("Packages", options);
  455. return self.ajax({
  456. type: "GET",
  457. url: url,
  458. dataType: "json"
  459. });
  460. };
  461. /**
  462. * Uninstalls a plugin
  463. * @param {String} Id
  464. */
  465. self.uninstallPlugin = function (id) {
  466. if (!id) {
  467. throw new Error("null Id");
  468. }
  469. var url = self.getUrl("Plugins/" + id);
  470. return self.ajax({
  471. type: "DELETE",
  472. url: url,
  473. dataType: "json"
  474. });
  475. };
  476. /**
  477. * Removes a virtual folder from either the default view or a user view
  478. * @param {String} name
  479. */
  480. self.removeVirtualFolder = function (name, userId) {
  481. if (!name) {
  482. throw new Error("null name");
  483. }
  484. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  485. url += "/" + name;
  486. url = self.getUrl(url);
  487. return self.ajax({
  488. type: "DELETE",
  489. url: url,
  490. dataType: "json"
  491. });
  492. };
  493. /**
  494. * Adds a virtual folder to either the default view or a user view
  495. * @param {String} name
  496. */
  497. self.addVirtualFolder = function (name, userId) {
  498. if (!name) {
  499. throw new Error("null name");
  500. }
  501. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  502. url += "/" + name;
  503. url = self.getUrl(url);
  504. return self.ajax({
  505. type: "POST",
  506. url: url
  507. });
  508. };
  509. /**
  510. * Renames a virtual folder, within either the default view or a user view
  511. * @param {String} name
  512. */
  513. self.renameVirtualFolder = function (name, newName, userId) {
  514. if (!name) {
  515. throw new Error("null name");
  516. }
  517. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  518. url += "/" + name + "/Name";
  519. url = self.getUrl(url, { newName: newName });
  520. return self.ajax({
  521. type: "POST",
  522. url: url
  523. });
  524. };
  525. /**
  526. * Adds an additional mediaPath to an existing virtual folder, within either the default view or a user view
  527. * @param {String} name
  528. */
  529. self.addMediaPath = function (virtualFolderName, mediaPath, userId) {
  530. if (!virtualFolderName) {
  531. throw new Error("null virtualFolderName");
  532. }
  533. if (!mediaPath) {
  534. throw new Error("null mediaPath");
  535. }
  536. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  537. url += "/" + virtualFolderName + "/Paths";
  538. url = self.getUrl(url, { path: mediaPath });
  539. return self.ajax({
  540. type: "POST",
  541. url: url
  542. });
  543. };
  544. /**
  545. * Removes a media path from a virtual folder, within either the default view or a user view
  546. * @param {String} name
  547. */
  548. self.removeMediaPath = function (virtualFolderName, mediaPath, userId) {
  549. if (!virtualFolderName) {
  550. throw new Error("null virtualFolderName");
  551. }
  552. if (!mediaPath) {
  553. throw new Error("null mediaPath");
  554. }
  555. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  556. url += "/" + virtualFolderName + "/Paths";
  557. url = self.getUrl(url, { path: mediaPath });
  558. return self.ajax({
  559. type: "DELETE",
  560. url: url,
  561. dataType: "json"
  562. });
  563. };
  564. /**
  565. * Deletes a user
  566. * @param {String} id
  567. */
  568. self.deleteUser = function (id) {
  569. if (!id) {
  570. throw new Error("null id");
  571. }
  572. var url = self.getUrl("Users/" + id);
  573. return self.ajax({
  574. type: "DELETE",
  575. url: url,
  576. dataType: "json"
  577. });
  578. };
  579. /**
  580. * Deletes a user image
  581. * @param {String} userId
  582. * @param {String} imageType The type of image to delete, based on the server-side ImageType enum.
  583. */
  584. self.deleteUserImage = function (userId, imageType) {
  585. if (!userId) {
  586. throw new Error("null userId");
  587. }
  588. if (!imageType) {
  589. throw new Error("null imageType");
  590. }
  591. var url = self.getUrl("Users/" + userId + "/Images/" + imageType);
  592. return self.ajax({
  593. type: "DELETE",
  594. url: url,
  595. dataType: "json"
  596. });
  597. };
  598. /**
  599. * Uploads a user image
  600. * @param {String} userId
  601. * @param {String} imageType The type of image to delete, based on the server-side ImageType enum.
  602. * @param {Object} file The file from the input element
  603. */
  604. self.uploadUserImage = function (userId, imageType, file) {
  605. if (!userId) {
  606. throw new Error("null userId");
  607. }
  608. if (!imageType) {
  609. throw new Error("null imageType");
  610. }
  611. if (!file || !file.type.match('image.*')) {
  612. throw new Error("File must be an image.");
  613. }
  614. var deferred = $.Deferred();
  615. var reader = new FileReader();
  616. reader.onerror = function () {
  617. deferred.reject();
  618. };
  619. reader.onabort = function () {
  620. deferred.reject();
  621. };
  622. // Closure to capture the file information.
  623. reader.onload = function (e) {
  624. var data = window.btoa(e.target.result);
  625. var url = self.getUrl("Users/" + userId + "/Images/" + imageType);
  626. self.ajax({
  627. type: "POST",
  628. url: url,
  629. data: data,
  630. contentType: "image/" + file.name.substring(file.name.lastIndexOf('.') + 1)
  631. }).done(function (result) {
  632. deferred.resolveWith(null, [result]);
  633. }).fail(function () {
  634. deferred.reject();
  635. });
  636. };
  637. // Read in the image file as a data URL.
  638. reader.readAsBinaryString(file);
  639. return deferred.promise();
  640. };
  641. /**
  642. * Gets the list of installed plugins on the server
  643. */
  644. self.getInstalledPlugins = function () {
  645. var url = self.getUrl("Plugins");
  646. return self.ajax({
  647. type: "GET",
  648. url: url,
  649. dataType: "json"
  650. });
  651. };
  652. /**
  653. * Gets a user by id
  654. * @param {String} id
  655. */
  656. self.getUser = function (id) {
  657. if (!id) {
  658. throw new Error("Must supply a userId");
  659. }
  660. var url = self.getUrl("Users/" + id);
  661. return self.ajax({
  662. type: "GET",
  663. url: url,
  664. dataType: "json"
  665. });
  666. };
  667. /**
  668. * Gets a studio
  669. */
  670. self.getStudio = function (name) {
  671. if (!name) {
  672. throw new Error("null name");
  673. }
  674. var url = self.getUrl("Studios/" + name);
  675. return self.ajax({
  676. type: "GET",
  677. url: url,
  678. dataType: "json"
  679. });
  680. };
  681. /**
  682. * Gets a genre
  683. */
  684. self.getGenre = function (name) {
  685. if (!name) {
  686. throw new Error("null name");
  687. }
  688. var url = self.getUrl("Genres/" + name);
  689. return self.ajax({
  690. type: "GET",
  691. url: url,
  692. dataType: "json"
  693. });
  694. };
  695. /**
  696. * Gets a year
  697. */
  698. self.getYear = function (year) {
  699. if (!year) {
  700. throw new Error("null year");
  701. }
  702. var url = self.getUrl("Years/" + year);
  703. return self.ajax({
  704. type: "GET",
  705. url: url,
  706. dataType: "json"
  707. });
  708. };
  709. /**
  710. * Gets a Person
  711. */
  712. self.getPerson = function (name) {
  713. if (!name) {
  714. throw new Error("null name");
  715. }
  716. var url = self.getUrl("Persons/" + name);
  717. return self.ajax({
  718. type: "GET",
  719. url: url,
  720. dataType: "json"
  721. });
  722. };
  723. /**
  724. * Gets weather info
  725. * @param {String} location - us zip code / city, state, country / city, country
  726. * Omit location to get weather info using stored server configuration value
  727. */
  728. self.getWeatherInfo = function (location) {
  729. var url = self.getUrl("weather", {
  730. location: location
  731. });
  732. return self.ajax({
  733. type: "GET",
  734. url: url,
  735. dataType: "json"
  736. });
  737. };
  738. /**
  739. * Gets all users from the server
  740. */
  741. self.getUsers = function () {
  742. var url = self.getUrl("users");
  743. return self.ajax({
  744. type: "GET",
  745. url: url,
  746. dataType: "json"
  747. });
  748. };
  749. /**
  750. * Gets all available parental ratings from the server
  751. */
  752. self.getParentalRatings = function () {
  753. var url = self.getUrl("Localization/ParentalRatings");
  754. return self.ajax({
  755. type: "GET",
  756. url: url,
  757. dataType: "json"
  758. });
  759. };
  760. /**
  761. * Gets a list of all available conrete BaseItem types from the server
  762. */
  763. self.getItemTypes = function (options) {
  764. var url = self.getUrl("Library/ItemTypes", options);
  765. return self.ajax({
  766. type: "GET",
  767. url: url,
  768. dataType: "json"
  769. });
  770. };
  771. /**
  772. * Constructs a url for a user image
  773. * @param {String} userId
  774. * @param {Object} options
  775. * Options supports the following properties:
  776. * width - download the image at a fixed width
  777. * height - download the image at a fixed height
  778. * maxWidth - download the image at a maxWidth
  779. * maxHeight - download the image at a maxHeight
  780. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  781. * For best results do not specify both width and height together, as aspect ratio might be altered.
  782. */
  783. self.getUserImageUrl = function (userId, options) {
  784. if (!userId) {
  785. throw new Error("null userId");
  786. }
  787. options = options || {
  788. };
  789. var url = "Users/" + userId + "/Images/" + options.type;
  790. if (options.index != null) {
  791. url += "/" + options.index;
  792. }
  793. // Don't put these on the query string
  794. delete options.type;
  795. delete options.index;
  796. return self.getUrl(url, options);
  797. };
  798. /**
  799. * Constructs a url for a person image
  800. * @param {String} name
  801. * @param {Object} options
  802. * Options supports the following properties:
  803. * width - download the image at a fixed width
  804. * height - download the image at a fixed height
  805. * maxWidth - download the image at a maxWidth
  806. * maxHeight - download the image at a maxHeight
  807. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  808. * For best results do not specify both width and height together, as aspect ratio might be altered.
  809. */
  810. self.getPersonImageUrl = function (name, options) {
  811. if (!name) {
  812. throw new Error("null name");
  813. }
  814. options = options || {
  815. };
  816. var url = "Persons/" + name + "/Images/" + options.type;
  817. if (options.index != null) {
  818. url += "/" + options.index;
  819. }
  820. // Don't put these on the query string
  821. delete options.type;
  822. delete options.index;
  823. return self.getUrl(url, options);
  824. };
  825. /**
  826. * Constructs a url for a year image
  827. * @param {String} year
  828. * @param {Object} options
  829. * Options supports the following properties:
  830. * width - download the image at a fixed width
  831. * height - download the image at a fixed height
  832. * maxWidth - download the image at a maxWidth
  833. * maxHeight - download the image at a maxHeight
  834. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  835. * For best results do not specify both width and height together, as aspect ratio might be altered.
  836. */
  837. self.getYearImageUrl = function (year, options) {
  838. if (!year) {
  839. throw new Error("null year");
  840. }
  841. options = options || {
  842. };
  843. var url = "Years/" + year + "/Images/" + options.type;
  844. if (options.index != null) {
  845. url += "/" + options.index;
  846. }
  847. // Don't put these on the query string
  848. delete options.type;
  849. delete options.index;
  850. return self.getUrl(url, options);
  851. };
  852. /**
  853. * Constructs a url for a genre image
  854. * @param {String} name
  855. * @param {Object} options
  856. * Options supports the following properties:
  857. * width - download the image at a fixed width
  858. * height - download the image at a fixed height
  859. * maxWidth - download the image at a maxWidth
  860. * maxHeight - download the image at a maxHeight
  861. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  862. * For best results do not specify both width and height together, as aspect ratio might be altered.
  863. */
  864. self.getGenreImageUrl = function (name, options) {
  865. if (!name) {
  866. throw new Error("null name");
  867. }
  868. options = options || {
  869. };
  870. var url = "Genres/" + name + "/Images/" + options.type;
  871. if (options.index != null) {
  872. url += "/" + options.index;
  873. }
  874. // Don't put these on the query string
  875. delete options.type;
  876. delete options.index;
  877. return self.getUrl(url, options);
  878. };
  879. /**
  880. * Constructs a url for a genre image
  881. * @param {String} name
  882. * @param {Object} options
  883. * Options supports the following properties:
  884. * width - download the image at a fixed width
  885. * height - download the image at a fixed height
  886. * maxWidth - download the image at a maxWidth
  887. * maxHeight - download the image at a maxHeight
  888. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  889. * For best results do not specify both width and height together, as aspect ratio might be altered.
  890. */
  891. self.getStudioImageUrl = function (name, options) {
  892. if (!name) {
  893. throw new Error("null name");
  894. }
  895. options = options || {
  896. };
  897. var url = "Studios/" + name + "/Images/" + options.type;
  898. if (options.index != null) {
  899. url += "/" + options.index;
  900. }
  901. // Don't put these on the query string
  902. delete options.type;
  903. delete options.index;
  904. return self.getUrl(url, options);
  905. };
  906. /**
  907. * Constructs a url for an item image
  908. * @param {String} itemId
  909. * @param {Object} options
  910. * Options supports the following properties:
  911. * type - Primary, logo, backdrop, etc. See the server-side enum ImageType
  912. * index - When downloading a backdrop, use this to specify which one (omitting is equivalent to zero)
  913. * width - download the image at a fixed width
  914. * height - download the image at a fixed height
  915. * maxWidth - download the image at a maxWidth
  916. * maxHeight - download the image at a maxHeight
  917. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  918. * For best results do not specify both width and height together, as aspect ratio might be altered.
  919. */
  920. self.getImageUrl = function (itemId, options) {
  921. if (!itemId) {
  922. throw new Error("itemId cannot be empty");
  923. }
  924. options = options || {
  925. };
  926. var url = "Items/" + itemId + "/Images/" + options.type;
  927. if (options.index != null) {
  928. url += "/" + options.index;
  929. }
  930. // Don't put these on the query string
  931. delete options.type;
  932. delete options.index;
  933. return self.getUrl(url, options);
  934. };
  935. /**
  936. * Constructs a url for an item logo image
  937. * If the item doesn't have a logo, it will inherit a logo from a parent
  938. * @param {Object} item A BaseItem
  939. * @param {Object} options
  940. * Options supports the following properties:
  941. * width - download the image at a fixed width
  942. * height - download the image at a fixed height
  943. * maxWidth - download the image at a maxWidth
  944. * maxHeight - download the image at a maxHeight
  945. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  946. * For best results do not specify both width and height together, as aspect ratio might be altered.
  947. */
  948. self.getLogoImageUrl = function (item, options) {
  949. if (!item) {
  950. throw new Error("null item");
  951. }
  952. options = options || {
  953. };
  954. options.imageType = "logo";
  955. var logoItemId = item.HasLogo ? item.Id : item.ParentLogoItemId;
  956. return logoItemId ? self.getImageUrl(logoItemId, options) : null;
  957. };
  958. /**
  959. * Constructs an array of backdrop image url's for an item
  960. * If the item doesn't have any backdrops, it will inherit them from a parent
  961. * @param {Object} item A BaseItem
  962. * @param {Object} options
  963. * Options supports the following properties:
  964. * width - download the image at a fixed width
  965. * height - download the image at a fixed height
  966. * maxWidth - download the image at a maxWidth
  967. * maxHeight - download the image at a maxHeight
  968. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  969. * For best results do not specify both width and height together, as aspect ratio might be altered.
  970. */
  971. self.getBackdropImageUrl = function (item, options) {
  972. if (!item) {
  973. throw new Error("null item");
  974. }
  975. options = options || {
  976. };
  977. options.imageType = "backdrop";
  978. var backdropItemId;
  979. var backdropCount;
  980. if (!item.BackdropCount) {
  981. backdropItemId = item.ParentBackdropItemId;
  982. backdropCount = item.ParentBackdropCount || 0;
  983. } else {
  984. backdropItemId = item.Id;
  985. backdropCount = item.BackdropCount;
  986. }
  987. if (!backdropItemId) {
  988. return [];
  989. }
  990. var files = [];
  991. for (var i = 0; i < backdropCount; i++) {
  992. options.imageIndex = i;
  993. files[i] = self.getImageUrl(backdropItemId, options);
  994. }
  995. return files;
  996. };
  997. /**
  998. * Authenticates a user
  999. * @param {String} userId
  1000. * @param {String} password
  1001. */
  1002. self.authenticateUser = function (userId, password) {
  1003. if (!userId) {
  1004. throw new Error("null userId");
  1005. }
  1006. var url = self.getUrl("Users/" + userId + "/authenticate");
  1007. var postData = {
  1008. password: MediaBrowser.SHA1(password || "")
  1009. };
  1010. return self.ajax({
  1011. type: "POST",
  1012. url: url,
  1013. data: JSON.stringify(postData),
  1014. dataType: "json",
  1015. contentType: "application/json"
  1016. });
  1017. };
  1018. /**
  1019. * Updates a user's password
  1020. * @param {String} userId
  1021. * @param {String} currentPassword
  1022. * @param {String} newPassword
  1023. */
  1024. self.updateUserPassword = function (userId, currentPassword, newPassword) {
  1025. if (!userId) {
  1026. throw new Error("null userId");
  1027. }
  1028. var url = self.getUrl("Users/" + userId + "/Password");
  1029. var postData = {
  1030. };
  1031. postData.currentPassword = MediaBrowser.SHA1(currentPassword);
  1032. if (newPassword) {
  1033. postData.newPassword = newPassword;
  1034. }
  1035. return self.ajax({
  1036. type: "POST",
  1037. url: url,
  1038. data: postData
  1039. });
  1040. };
  1041. /**
  1042. * Resets a user's password
  1043. * @param {String} userId
  1044. */
  1045. self.resetUserPassword = function (userId) {
  1046. if (!userId) {
  1047. throw new Error("null userId");
  1048. }
  1049. var url = self.getUrl("Users/" + userId + "/Password");
  1050. var postData = {
  1051. };
  1052. postData.resetPassword = true;
  1053. return self.ajax({
  1054. type: "POST",
  1055. url: url,
  1056. data: postData
  1057. });
  1058. };
  1059. /**
  1060. * Updates the server's configuration
  1061. * @param {Object} configuration
  1062. */
  1063. self.updateServerConfiguration = function (configuration) {
  1064. if (!configuration) {
  1065. throw new Error("null configuration");
  1066. }
  1067. var url = self.getUrl("System/Configuration");
  1068. return self.ajax({
  1069. type: "POST",
  1070. url: url,
  1071. data: JSON.stringify(configuration),
  1072. dataType: "json",
  1073. contentType: "application/json"
  1074. });
  1075. };
  1076. /**
  1077. * Updates plugin security info
  1078. */
  1079. self.updatePluginSecurityInfo = function (info) {
  1080. var url = self.getUrl("Plugins/SecurityInfo");
  1081. return self.ajax({
  1082. type: "POST",
  1083. url: url,
  1084. data: JSON.stringify(info),
  1085. dataType: "json",
  1086. contentType: "application/json"
  1087. });
  1088. };
  1089. /**
  1090. * Creates a user
  1091. * @param {Object} user
  1092. */
  1093. self.createUser = function (user) {
  1094. if (!user) {
  1095. throw new Error("null user");
  1096. }
  1097. var url = self.getUrl("Users");
  1098. return self.ajax({
  1099. type: "POST",
  1100. url: url,
  1101. data: JSON.stringify(user),
  1102. dataType: "json",
  1103. contentType: "application/json"
  1104. });
  1105. };
  1106. /**
  1107. * Updates a user
  1108. * @param {Object} user
  1109. */
  1110. self.updateUser = function (user) {
  1111. if (!user) {
  1112. throw new Error("null user");
  1113. }
  1114. var url = self.getUrl("Users/" + user.Id);
  1115. return self.ajax({
  1116. type: "POST",
  1117. url: url,
  1118. data: JSON.stringify(user),
  1119. dataType: "json",
  1120. contentType: "application/json"
  1121. });
  1122. };
  1123. /**
  1124. * Updates the Triggers for a ScheduledTask
  1125. * @param {String} id
  1126. * @param {Object} triggers
  1127. */
  1128. self.updateScheduledTaskTriggers = function (id, triggers) {
  1129. if (!id) {
  1130. throw new Error("null id");
  1131. }
  1132. if (!triggers) {
  1133. throw new Error("null triggers");
  1134. }
  1135. var url = self.getUrl("ScheduledTasks/" + id + "/Triggers");
  1136. return self.ajax({
  1137. type: "POST",
  1138. url: url,
  1139. data: JSON.stringify(triggers),
  1140. dataType: "json",
  1141. contentType: "application/json"
  1142. });
  1143. };
  1144. /**
  1145. * Updates a plugin's configuration
  1146. * @param {String} Id
  1147. * @param {Object} configuration
  1148. */
  1149. self.updatePluginConfiguration = function (id, configuration) {
  1150. if (!id) {
  1151. throw new Error("null Id");
  1152. }
  1153. if (!configuration) {
  1154. throw new Error("null configuration");
  1155. }
  1156. var url = self.getUrl("Plugins/" + id + "/Configuration");
  1157. return self.ajax({
  1158. type: "POST",
  1159. url: url,
  1160. data: JSON.stringify(configuration),
  1161. dataType: "json",
  1162. contentType: "application/json"
  1163. });
  1164. };
  1165. /**
  1166. * Gets items based on a query, typically for children of a folder
  1167. * @param {String} userId
  1168. * @param {Object} options
  1169. * Options accepts the following properties:
  1170. * itemId - Localize the search to a specific folder (root if omitted)
  1171. * startIndex - Use for paging
  1172. * limit - Use to limit results to a certain number of items
  1173. * filter - Specify one or more ItemFilters, comma delimeted (see server-side enum)
  1174. * sortBy - Specify an ItemSortBy (comma-delimeted list see server-side enum)
  1175. * sortOrder - ascending/descending
  1176. * fields - additional fields to include aside from basic info. This is a comma delimited list. See server-side enum ItemFields.
  1177. * index - the name of the dynamic, localized index function
  1178. * dynamicSortBy - the name of the dynamic localized sort function
  1179. * recursive - Whether or not the query should be recursive
  1180. * searchTerm - search term to use as a filter
  1181. */
  1182. self.getItems = function (userId, options) {
  1183. if (!userId) {
  1184. throw new Error("null userId");
  1185. }
  1186. var url = self.getUrl("Users/" + userId + "/Items", options);
  1187. return self.ajax({
  1188. type: "GET",
  1189. url: url,
  1190. dataType: "json"
  1191. });
  1192. };
  1193. /**
  1194. Gets genres from an item
  1195. */
  1196. self.getGenres = function (userId, options) {
  1197. if (!userId) {
  1198. throw new Error("null userId");
  1199. }
  1200. var parentId = options.parentId || "root";
  1201. // Don't put these on the query string
  1202. delete options.parentId;
  1203. var url = self.getUrl("Users/" + userId + "/Items/" + parentId + "/Genres", options);
  1204. return self.ajax({
  1205. type: "GET",
  1206. url: url,
  1207. dataType: "json"
  1208. });
  1209. };
  1210. /**
  1211. Gets people from an item
  1212. */
  1213. self.getPeople = function (userId, options) {
  1214. if (!userId) {
  1215. throw new Error("null userId");
  1216. }
  1217. var parentId = options.parentId || "root";
  1218. // Don't put these on the query string
  1219. delete options.parentId;
  1220. var url = self.getUrl("Users/" + userId + "/Items/" + parentId + "/Persons", options);
  1221. return self.ajax({
  1222. type: "GET",
  1223. url: url,
  1224. dataType: "json"
  1225. });
  1226. };
  1227. /**
  1228. Gets studios from an item
  1229. */
  1230. self.getStudios = function (userId, options) {
  1231. if (!userId) {
  1232. throw new Error("null userId");
  1233. }
  1234. var parentId = options.parentId || "root";
  1235. // Don't put these on the query string
  1236. delete options.parentId;
  1237. var url = self.getUrl("Users/" + userId + "/Items/" + parentId + "/Studios", options);
  1238. return self.ajax({
  1239. type: "GET",
  1240. url: url,
  1241. dataType: "json"
  1242. });
  1243. };
  1244. /**
  1245. * Gets local trailers for an item
  1246. */
  1247. self.getLocalTrailers = function (userId, itemId) {
  1248. if (!userId) {
  1249. throw new Error("null userId");
  1250. }
  1251. if (!itemId) {
  1252. throw new Error("null itemId");
  1253. }
  1254. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/LocalTrailers");
  1255. return self.ajax({
  1256. type: "GET",
  1257. url: url,
  1258. dataType: "json"
  1259. });
  1260. };
  1261. /**
  1262. * Gets special features for an item
  1263. */
  1264. self.getSpecialFeatures = function (userId, itemId) {
  1265. if (!userId) {
  1266. throw new Error("null userId");
  1267. }
  1268. if (!itemId) {
  1269. throw new Error("null itemId");
  1270. }
  1271. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/SpecialFeatures");
  1272. return self.ajax({
  1273. type: "GET",
  1274. url: url,
  1275. dataType: "json"
  1276. });
  1277. };
  1278. /**
  1279. * Marks an item as played or unplayed
  1280. * This should not be used to update playstate following playback.
  1281. * There are separate playstate check-in methods for that. This should be used for a
  1282. * separate option to reset playstate.
  1283. * @param {String} userId
  1284. * @param {String} itemId
  1285. * @param {Boolean} wasPlayed
  1286. */
  1287. self.updatePlayedStatus = function (userId, itemId, wasPlayed) {
  1288. if (!userId) {
  1289. throw new Error("null userId");
  1290. }
  1291. if (!itemId) {
  1292. throw new Error("null itemId");
  1293. }
  1294. var url = self.getUrl("Users/" + userId + "/PlayedItems/" + itemId);
  1295. var method = wasPlayed ? "POST" : "DELETE";
  1296. return self.ajax({
  1297. type: method,
  1298. url: url,
  1299. dataType: "json"
  1300. });
  1301. };
  1302. /**
  1303. * Updates a user's favorite status for an item and returns the updated UserItemData object.
  1304. * @param {String} userId
  1305. * @param {String} itemId
  1306. * @param {Boolean} isFavorite
  1307. */
  1308. self.updateFavoriteStatus = function (userId, itemId, isFavorite) {
  1309. if (!userId) {
  1310. throw new Error("null userId");
  1311. }
  1312. if (!itemId) {
  1313. throw new Error("null itemId");
  1314. }
  1315. var url = self.getUrl("Users/" + userId + "/FavoriteItems/" + itemId);
  1316. var method = isFavorite ? "POST" : "DELETE";
  1317. return self.ajax({
  1318. type: method,
  1319. url: url,
  1320. dataType: "json"
  1321. });
  1322. };
  1323. /**
  1324. * Updates a user's personal rating for an item
  1325. * @param {String} userId
  1326. * @param {String} itemId
  1327. * @param {Boolean} likes
  1328. */
  1329. self.updateUserItemRating = function (userId, itemId, likes) {
  1330. if (!userId) {
  1331. throw new Error("null userId");
  1332. }
  1333. if (!itemId) {
  1334. throw new Error("null itemId");
  1335. }
  1336. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating", {
  1337. likes: likes
  1338. });
  1339. return self.ajax({
  1340. type: "POST",
  1341. url: url
  1342. });
  1343. };
  1344. /**
  1345. * Clears a user's personal rating for an item
  1346. * @param {String} userId
  1347. * @param {String} itemId
  1348. */
  1349. self.clearUserItemRating = function (userId, itemId) {
  1350. if (!userId) {
  1351. throw new Error("null userId");
  1352. }
  1353. if (!itemId) {
  1354. throw new Error("null itemId");
  1355. }
  1356. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating");
  1357. return self.ajax({
  1358. type: "DELETE",
  1359. url: url,
  1360. dataType: "json"
  1361. });
  1362. };
  1363. /**
  1364. * Reports the user has started playing something
  1365. * @param {String} userId
  1366. * @param {String} itemId
  1367. */
  1368. self.reportPlaybackStart = function (userId, itemId) {
  1369. if (!userId) {
  1370. throw new Error("null userId");
  1371. }
  1372. if (!itemId) {
  1373. throw new Error("null itemId");
  1374. }
  1375. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId);
  1376. return self.ajax({
  1377. type: "POST",
  1378. url: url,
  1379. dataType: "json"
  1380. });
  1381. };
  1382. /**
  1383. * Reports progress viewing an item
  1384. * @param {String} userId
  1385. * @param {String} itemId
  1386. */
  1387. self.reportPlaybackProgress = function (userId, itemId, positionTicks) {
  1388. if (!userId) {
  1389. throw new Error("null userId");
  1390. }
  1391. if (!itemId) {
  1392. throw new Error("null itemId");
  1393. }
  1394. var params = {
  1395. };
  1396. if (positionTicks) {
  1397. params.positionTicks = positionTicks;
  1398. }
  1399. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId + "/Progress", params);
  1400. return self.ajax({
  1401. type: "POST",
  1402. url: url,
  1403. dataType: "json"
  1404. });
  1405. };
  1406. /**
  1407. * Reports a user has stopped playing an item
  1408. * @param {String} userId
  1409. * @param {String} itemId
  1410. */
  1411. self.reportPlaybackStopped = function (userId, itemId, positionTicks) {
  1412. if (!userId) {
  1413. throw new Error("null userId");
  1414. }
  1415. if (!itemId) {
  1416. throw new Error("null itemId");
  1417. }
  1418. var params = {
  1419. };
  1420. if (positionTicks) {
  1421. params.positionTicks = positionTicks;
  1422. }
  1423. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId, params);
  1424. return self.ajax({
  1425. type: "DELETE",
  1426. url: url,
  1427. dataType: "json"
  1428. });
  1429. };
  1430. }
  1431. }(jQuery, navigator, JSON, window.WebSocket, setTimeout);
  1432. /**
  1433. * Provides a friendly way to create an api client instance using information from the browser's current url
  1434. */
  1435. MediaBrowser.ApiClient.create = function (clientName) {
  1436. var loc = window.location;
  1437. return new MediaBrowser.ApiClient(loc.protocol, loc.hostname, loc.port, clientName);
  1438. };
  1439. /**
  1440. *
  1441. * Secure Hash Algorithm (SHA1)
  1442. * http://www.webtoolkit.info/
  1443. *
  1444. **/
  1445. MediaBrowser.SHA1 = function (msg) {
  1446. function rotate_left(n, s) {
  1447. var t4 = (n << s) | (n >>> (32 - s));
  1448. return t4;
  1449. }
  1450. function lsb_hex(val) {
  1451. var str = "";
  1452. var i;
  1453. var vh;
  1454. var vl;
  1455. for (i = 0; i <= 6; i += 2) {
  1456. vh = (val >>> (i * 4 + 4)) & 0x0f;
  1457. vl = (val >>> (i * 4)) & 0x0f;
  1458. str += vh.toString(16) + vl.toString(16);
  1459. }
  1460. return str;
  1461. }
  1462. function cvt_hex(val) {
  1463. var str = "";
  1464. var i;
  1465. var v;
  1466. for (i = 7; i >= 0; i--) {
  1467. v = (val >>> (i * 4)) & 0x0f;
  1468. str += v.toString(16);
  1469. }
  1470. return str;
  1471. }
  1472. function Utf8Encode(string) {
  1473. string = string.replace(/\r\n/g, "\n");
  1474. var utftext = "";
  1475. for (var n = 0; n < string.length; n++) {
  1476. var c = string.charCodeAt(n);
  1477. if (c < 128) {
  1478. utftext += String.fromCharCode(c);
  1479. }
  1480. else if ((c > 127) && (c < 2048)) {
  1481. utftext += String.fromCharCode((c >> 6) | 192);
  1482. utftext += String.fromCharCode((c & 63) | 128);
  1483. }
  1484. else {
  1485. utftext += String.fromCharCode((c >> 12) | 224);
  1486. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  1487. utftext += String.fromCharCode((c & 63) | 128);
  1488. }
  1489. }
  1490. return utftext;
  1491. }
  1492. var blockstart;
  1493. var i, j;
  1494. var W = new Array(80);
  1495. var H0 = 0x67452301;
  1496. var H1 = 0xEFCDAB89;
  1497. var H2 = 0x98BADCFE;
  1498. var H3 = 0x10325476;
  1499. var H4 = 0xC3D2E1F0;
  1500. var A, B, C, D, E;
  1501. var temp;
  1502. msg = Utf8Encode(msg);
  1503. var msg_len = msg.length;
  1504. var word_array = new Array();
  1505. for (i = 0; i < msg_len - 3; i += 4) {
  1506. j = msg.charCodeAt(i) << 24 | msg.charCodeAt(i + 1) << 16 |
  1507. msg.charCodeAt(i + 2) << 8 | msg.charCodeAt(i + 3);
  1508. word_array.push(j);
  1509. }
  1510. switch (msg_len % 4) {
  1511. case 0:
  1512. i = 0x080000000;
  1513. break;
  1514. case 1:
  1515. i = msg.charCodeAt(msg_len - 1) << 24 | 0x0800000;
  1516. break;
  1517. case 2:
  1518. i = msg.charCodeAt(msg_len - 2) << 24 | msg.charCodeAt(msg_len - 1) << 16 | 0x08000;
  1519. break;
  1520. case 3:
  1521. i = msg.charCodeAt(msg_len - 3) << 24 | msg.charCodeAt(msg_len - 2) << 16 | msg.charCodeAt(msg_len - 1) << 8 | 0x80;
  1522. break;
  1523. }
  1524. word_array.push(i);
  1525. while ((word_array.length % 16) != 14) word_array.push(0);
  1526. word_array.push(msg_len >>> 29);
  1527. word_array.push((msg_len << 3) & 0x0ffffffff);
  1528. for (blockstart = 0; blockstart < word_array.length; blockstart += 16) {
  1529. for (i = 0; i < 16; i++) W[i] = word_array[blockstart + i];
  1530. for (i = 16; i <= 79; i++) W[i] = rotate_left(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
  1531. A = H0;
  1532. B = H1;
  1533. C = H2;
  1534. D = H3;
  1535. E = H4;
  1536. for (i = 0; i <= 19; i++) {
  1537. temp = (rotate_left(A, 5) + ((B & C) | (~B & D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
  1538. E = D;
  1539. D = C;
  1540. C = rotate_left(B, 30);
  1541. B = A;
  1542. A = temp;
  1543. }
  1544. for (i = 20; i <= 39; i++) {
  1545. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
  1546. E = D;
  1547. D = C;
  1548. C = rotate_left(B, 30);
  1549. B = A;
  1550. A = temp;
  1551. }
  1552. for (i = 40; i <= 59; i++) {
  1553. temp = (rotate_left(A, 5) + ((B & C) | (B & D) | (C & D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
  1554. E = D;
  1555. D = C;
  1556. C = rotate_left(B, 30);
  1557. B = A;
  1558. A = temp;
  1559. }
  1560. for (i = 60; i <= 79; i++) {
  1561. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
  1562. E = D;
  1563. D = C;
  1564. C = rotate_left(B, 30);
  1565. B = A;
  1566. A = temp;
  1567. }
  1568. H0 = (H0 + A) & 0x0ffffffff;
  1569. H1 = (H1 + B) & 0x0ffffffff;
  1570. H2 = (H2 + C) & 0x0ffffffff;
  1571. H3 = (H3 + D) & 0x0ffffffff;
  1572. H4 = (H4 + E) & 0x0ffffffff;
  1573. }
  1574. var temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4);
  1575. return temp.toLowerCase();
  1576. };
  1577. (function (jQuery, window, undefined) {
  1578. "use strict";
  1579. var matched, browser;
  1580. jQuery.uaMatch = function (ua) {
  1581. ua = ua.toLowerCase();
  1582. var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
  1583. /(webkit)[ \/]([\w.]+)/.exec(ua) ||
  1584. /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
  1585. /(msie) ([\w.]+)/.exec(ua) ||
  1586. ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
  1587. [];
  1588. var platform_match = /(ipad)/.exec(ua) ||
  1589. /(iphone)/.exec(ua) ||
  1590. /(android)/.exec(ua) ||
  1591. [];
  1592. return {
  1593. browser: match[1] || "",
  1594. version: match[2] || "0",
  1595. platform: platform_match[0] || ""
  1596. };
  1597. };
  1598. matched = jQuery.uaMatch(window.navigator.userAgent);
  1599. browser = {};
  1600. if (matched.browser) {
  1601. browser[matched.browser] = true;
  1602. browser.version = matched.version;
  1603. }
  1604. if (matched.platform) {
  1605. browser[matched.platform] = true
  1606. }
  1607. // Chrome is Webkit, but Webkit is also Safari.
  1608. if (browser.chrome) {
  1609. browser.webkit = true;
  1610. } else if (browser.webkit) {
  1611. browser.safari = true;
  1612. }
  1613. jQuery.browser = browser;
  1614. })(jQuery, window);