ApiClient.js 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806
  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. self.deviceId = function () {
  84. return deviceId;
  85. };
  86. self.encodeName = function (name) {
  87. name = name.split('/').join('-');
  88. name = name.split('?').join('-');
  89. var val = $.param({ name: name });
  90. return val.substring(val.indexOf('=') + 1).replace("'", '%27');
  91. };
  92. /**
  93. * Wraps around jQuery ajax methods to add additional info to the request.
  94. */
  95. self.ajax = function (request) {
  96. if (!request) {
  97. throw new Error("Request cannot be null");
  98. }
  99. if (clientName) {
  100. var auth = 'MediaBrowser Client="' + clientName + '", Device="' + deviceName + '", DeviceId="' + deviceId + '"';
  101. if (currentUserId) {
  102. auth += ', UserId="' + currentUserId + '"';
  103. }
  104. request.headers = {
  105. Authorization: auth
  106. };
  107. }
  108. return $.ajax(request);
  109. };
  110. /**
  111. * Creates an api url based on a handler name and query string parameters
  112. * @param {String} name
  113. * @param {Object} params
  114. */
  115. self.getUrl = function (name, params) {
  116. if (!name) {
  117. throw new Error("Url name cannot be empty");
  118. }
  119. var url = serverProtocol + "//" + serverHostName + ":" + serverPortNumber + "/mediabrowser/" + name;
  120. if (params) {
  121. url += "?" + $.param(params);
  122. }
  123. return url;
  124. };
  125. self.openWebSocket = function (port) {
  126. var url = "ws://" + serverHostName + ":" + port + "/mediabrowser";
  127. webSocket = new WebSocket(url);
  128. webSocket.onmessage = function (msg) {
  129. msg = JSON.parse(msg.data);
  130. $(self).trigger("websocketmessage", [msg]);
  131. };
  132. webSocket.onopen = function () {
  133. setTimeout(function () {
  134. self.sendWebSocketMessage("Identity", clientName + "|" + deviceId);
  135. $(self).trigger("websocketopen");
  136. }, 500);
  137. };
  138. webSocket.onerror = function () {
  139. setTimeout(function () {
  140. $(self).trigger("websocketerror");
  141. }, 0);
  142. };
  143. webSocket.onclose = function () {
  144. setTimeout(function () {
  145. $(self).trigger("websocketclose");
  146. }, 0);
  147. };
  148. };
  149. self.sendWebSocketMessage = function (name, data) {
  150. var msg = { MessageType: name };
  151. if (data) {
  152. msg.Data = data;
  153. }
  154. msg = JSON.stringify(msg);
  155. webSocket.send(msg);
  156. };
  157. self.isWebSocketOpen = function () {
  158. return webSocket && webSocket.readyState === WebSocket.OPEN;
  159. };
  160. self.isWebSocketOpenOrConnecting = function () {
  161. return webSocket && (webSocket.readyState === WebSocket.OPEN || webSocket.readyState === WebSocket.CONNECTING);
  162. };
  163. /**
  164. * Gets an item from the server
  165. * Omit itemId to get the root folder.
  166. */
  167. self.getItem = function (userId, itemId) {
  168. if (!userId) {
  169. throw new Error("null userId");
  170. }
  171. var url = self.getUrl("Users/" + userId + "/Items/" + itemId);
  172. return self.ajax({
  173. type: "GET",
  174. url: url,
  175. dataType: "json"
  176. });
  177. };
  178. /**
  179. * Gets the root folder from the server
  180. */
  181. self.getRootFolder = function (userId) {
  182. if (!userId) {
  183. throw new Error("null userId");
  184. }
  185. var url = self.getUrl("Users/" + userId + "/Items/Root");
  186. return self.ajax({
  187. type: "GET",
  188. url: url,
  189. dataType: "json"
  190. });
  191. };
  192. /**
  193. * Gets the current server status
  194. */
  195. self.getSystemInfo = function () {
  196. var url = self.getUrl("System/Info");
  197. return self.ajax({
  198. type: "GET",
  199. url: url,
  200. dataType: "json"
  201. });
  202. };
  203. self.getSimilarItems = function (itemId, options) {
  204. var url = self.getUrl("Items/" + itemId + "/Similar", options);
  205. return self.ajax({
  206. type: "GET",
  207. url: url,
  208. dataType: "json"
  209. });
  210. };
  211. /**
  212. * Gets all cultures known to the server
  213. */
  214. self.getCultures = function () {
  215. var url = self.getUrl("Localization/cultures");
  216. return self.ajax({
  217. type: "GET",
  218. url: url,
  219. dataType: "json"
  220. });
  221. };
  222. /**
  223. * Gets all countries known to the server
  224. */
  225. self.getCountries = function () {
  226. var url = self.getUrl("Localization/countries");
  227. return self.ajax({
  228. type: "GET",
  229. url: url,
  230. dataType: "json"
  231. });
  232. };
  233. /**
  234. * Gets plugin security info
  235. */
  236. self.getPluginSecurityInfo = function () {
  237. var url = self.getUrl("Plugins/SecurityInfo");
  238. return self.ajax({
  239. type: "GET",
  240. url: url,
  241. dataType: "json"
  242. });
  243. };
  244. /**
  245. * Gets the directory contents of a path on the server
  246. */
  247. self.getDirectoryContents = function (path, options) {
  248. if (!path) {
  249. throw new Error("null path");
  250. }
  251. options = options || {};
  252. options.path = path;
  253. var url = self.getUrl("Environment/DirectoryContents", options);
  254. return self.ajax({
  255. type: "GET",
  256. url: url,
  257. dataType: "json"
  258. });
  259. };
  260. /**
  261. * Gets a list of physical drives from the server
  262. */
  263. self.getDrives = function () {
  264. var url = self.getUrl("Environment/Drives");
  265. return self.ajax({
  266. type: "GET",
  267. url: url,
  268. dataType: "json"
  269. });
  270. };
  271. /**
  272. * Gets a list of network devices from the server
  273. */
  274. self.getNetworkDevices = function () {
  275. var url = self.getUrl("Environment/NetworkDevices");
  276. return self.ajax({
  277. type: "GET",
  278. url: url,
  279. dataType: "json"
  280. });
  281. };
  282. /**
  283. * Cancels a package installation
  284. */
  285. self.cancelPackageInstallation = function (installationId) {
  286. if (!installationId) {
  287. throw new Error("null installationId");
  288. }
  289. var url = self.getUrl("Packages/Installing/" + id);
  290. return self.ajax({
  291. type: "DELETE",
  292. url: url
  293. });
  294. };
  295. /**
  296. * Refreshes metadata for an item
  297. */
  298. self.refreshItem = function (itemId, force, recursive) {
  299. if (!itemId) {
  300. throw new Error("null itemId");
  301. }
  302. var url = self.getUrl("Items/" + itemId + "/Refresh", {
  303. forced: force || false,
  304. recursive: recursive || false
  305. });
  306. return self.ajax({
  307. type: "POST",
  308. url: url
  309. });
  310. };
  311. /**
  312. * Installs or updates a new plugin
  313. */
  314. self.installPlugin = function (name, updateClass, version) {
  315. if (!name) {
  316. throw new Error("null name");
  317. }
  318. if (!updateClass) {
  319. throw new Error("null updateClass");
  320. }
  321. var options = {
  322. updateClass: updateClass
  323. };
  324. if (version) {
  325. options.version = version;
  326. }
  327. var url = self.getUrl("Packages/Installed/" + name, options);
  328. return self.ajax({
  329. type: "POST",
  330. url: url
  331. });
  332. };
  333. /**
  334. * Instructs the server to perform a pending kernel reload or app restart.
  335. * If a restart is not currently required, nothing will happen.
  336. */
  337. self.performPendingRestart = function () {
  338. var url = self.getUrl("System/Restart");
  339. return self.ajax({
  340. type: "POST",
  341. url: url
  342. });
  343. };
  344. /**
  345. * Gets information about an installable package
  346. */
  347. self.getPackageInfo = function (name) {
  348. if (!name) {
  349. throw new Error("null name");
  350. }
  351. var url = self.getUrl("Packages/" + name);
  352. return self.ajax({
  353. type: "GET",
  354. url: url,
  355. dataType: "json"
  356. });
  357. };
  358. /**
  359. * Gets the latest available application update (if any)
  360. */
  361. self.getAvailableApplicationUpdate = function () {
  362. var url = self.getUrl("Packages/Updates", { PackageType: "System" });
  363. return self.ajax({
  364. type: "GET",
  365. url: url,
  366. dataType: "json"
  367. });
  368. };
  369. /**
  370. * Gets the latest available plugin updates (if any)
  371. */
  372. self.getAvailablePluginUpdates = function () {
  373. var url = self.getUrl("Packages/Updates", { PackageType: "UserInstalled" });
  374. return self.ajax({
  375. type: "GET",
  376. url: url,
  377. dataType: "json"
  378. });
  379. };
  380. /**
  381. * Gets the virtual folder for a view. Specify a userId to get a user view, or omit for the default view.
  382. */
  383. self.getVirtualFolders = function (userId) {
  384. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  385. url = self.getUrl(url);
  386. return self.ajax({
  387. type: "GET",
  388. url: url,
  389. dataType: "json"
  390. });
  391. };
  392. /**
  393. * Gets all the paths of the locations in the physical root.
  394. */
  395. self.getPhysicalPaths = function () {
  396. var url = self.getUrl("Library/PhysicalPaths");
  397. return self.ajax({
  398. type: "GET",
  399. url: url,
  400. dataType: "json"
  401. });
  402. };
  403. /**
  404. * Gets the current server configuration
  405. */
  406. self.getServerConfiguration = function () {
  407. var url = self.getUrl("System/Configuration");
  408. return self.ajax({
  409. type: "GET",
  410. url: url,
  411. dataType: "json"
  412. });
  413. };
  414. /**
  415. * Gets the server's scheduled tasks
  416. */
  417. self.getScheduledTasks = function () {
  418. var url = self.getUrl("ScheduledTasks");
  419. return self.ajax({
  420. type: "GET",
  421. url: url,
  422. dataType: "json"
  423. });
  424. };
  425. /**
  426. * Starts a scheduled task
  427. */
  428. self.startScheduledTask = function (id) {
  429. if (!id) {
  430. throw new Error("null id");
  431. }
  432. var url = self.getUrl("ScheduledTasks/Running/" + id);
  433. return self.ajax({
  434. type: "POST",
  435. url: url
  436. });
  437. };
  438. /**
  439. * Gets a scheduled task
  440. */
  441. self.getScheduledTask = function (id) {
  442. if (!id) {
  443. throw new Error("null id");
  444. }
  445. var url = self.getUrl("ScheduledTasks/" + id);
  446. return self.ajax({
  447. type: "GET",
  448. url: url,
  449. dataType: "json"
  450. });
  451. };
  452. self.getNextUpEpisodes = function (options) {
  453. var url = self.getUrl("Shows/NextUp", options);
  454. return self.ajax({
  455. type: "GET",
  456. url: url,
  457. dataType: "json"
  458. });
  459. };
  460. /**
  461. * Stops a scheduled task
  462. */
  463. self.stopScheduledTask = function (id) {
  464. if (!id) {
  465. throw new Error("null id");
  466. }
  467. var url = self.getUrl("ScheduledTasks/Running/" + id);
  468. return self.ajax({
  469. type: "DELETE",
  470. url: url
  471. });
  472. };
  473. /**
  474. * Gets the configuration of a plugin
  475. * @param {String} Id
  476. */
  477. self.getPluginConfiguration = function (id) {
  478. if (!id) {
  479. throw new Error("null Id");
  480. }
  481. var url = self.getUrl("Plugins/" + id + "/Configuration");
  482. return self.ajax({
  483. type: "GET",
  484. url: url,
  485. dataType: "json"
  486. });
  487. };
  488. /**
  489. * Gets a list of plugins that are available to be installed
  490. */
  491. self.getAvailablePlugins = function (options) {
  492. options = $.extend({}, options || {});
  493. options.PackageType = "UserInstalled";
  494. var url = self.getUrl("Packages", options);
  495. return self.ajax({
  496. type: "GET",
  497. url: url,
  498. dataType: "json"
  499. });
  500. };
  501. /**
  502. * Uninstalls a plugin
  503. * @param {String} Id
  504. */
  505. self.uninstallPlugin = function (id) {
  506. if (!id) {
  507. throw new Error("null Id");
  508. }
  509. var url = self.getUrl("Plugins/" + id);
  510. return self.ajax({
  511. type: "DELETE",
  512. url: url
  513. });
  514. };
  515. /**
  516. * Removes a virtual folder from either the default view or a user view
  517. * @param {String} name
  518. */
  519. self.removeVirtualFolder = function (name, userId) {
  520. if (!name) {
  521. throw new Error("null name");
  522. }
  523. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  524. url += "/" + name;
  525. url = self.getUrl(url);
  526. return self.ajax({
  527. type: "DELETE",
  528. url: url
  529. });
  530. };
  531. /**
  532. * Adds a virtual folder to either the default view or a user view
  533. * @param {String} name
  534. */
  535. self.addVirtualFolder = function (name, userId) {
  536. if (!name) {
  537. throw new Error("null name");
  538. }
  539. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  540. url += "/" + name;
  541. url = self.getUrl(url);
  542. return self.ajax({
  543. type: "POST",
  544. url: url
  545. });
  546. };
  547. /**
  548. * Renames a virtual folder, within either the default view or a user view
  549. * @param {String} name
  550. */
  551. self.renameVirtualFolder = function (name, newName, userId) {
  552. if (!name) {
  553. throw new Error("null name");
  554. }
  555. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  556. url += "/" + name + "/Name";
  557. url = self.getUrl(url, { newName: newName });
  558. return self.ajax({
  559. type: "POST",
  560. url: url
  561. });
  562. };
  563. /**
  564. * Adds an additional mediaPath to an existing virtual folder, within either the default view or a user view
  565. * @param {String} name
  566. */
  567. self.addMediaPath = function (virtualFolderName, mediaPath, userId) {
  568. if (!virtualFolderName) {
  569. throw new Error("null virtualFolderName");
  570. }
  571. if (!mediaPath) {
  572. throw new Error("null mediaPath");
  573. }
  574. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  575. url += "/" + virtualFolderName + "/Paths";
  576. url = self.getUrl(url, { path: mediaPath });
  577. return self.ajax({
  578. type: "POST",
  579. url: url
  580. });
  581. };
  582. /**
  583. * Removes a media path from a virtual folder, within either the default view or a user view
  584. * @param {String} name
  585. */
  586. self.removeMediaPath = function (virtualFolderName, mediaPath, userId) {
  587. if (!virtualFolderName) {
  588. throw new Error("null virtualFolderName");
  589. }
  590. if (!mediaPath) {
  591. throw new Error("null mediaPath");
  592. }
  593. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  594. url += "/" + virtualFolderName + "/Paths";
  595. url = self.getUrl(url, { path: mediaPath });
  596. return self.ajax({
  597. type: "DELETE",
  598. url: url
  599. });
  600. };
  601. /**
  602. * Deletes a user
  603. * @param {String} id
  604. */
  605. self.deleteUser = function (id) {
  606. if (!id) {
  607. throw new Error("null id");
  608. }
  609. var url = self.getUrl("Users/" + id);
  610. return self.ajax({
  611. type: "DELETE",
  612. url: url
  613. });
  614. };
  615. /**
  616. * Deletes a user image
  617. * @param {String} userId
  618. * @param {String} imageType The type of image to delete, based on the server-side ImageType enum.
  619. */
  620. self.deleteUserImage = function (userId, imageType, imageIndex) {
  621. if (!userId) {
  622. throw new Error("null userId");
  623. }
  624. if (!imageType) {
  625. throw new Error("null imageType");
  626. }
  627. var url = self.getUrl("Users/" + userId + "/Images/" + imageType);
  628. if (imageIndex != null) {
  629. url += "/" + imageIndex;
  630. }
  631. return self.ajax({
  632. type: "DELETE",
  633. url: url
  634. });
  635. };
  636. self.deleteItemImage = function (itemId, imageType, imageIndex) {
  637. if (!itemId) {
  638. throw new Error("null itemId");
  639. }
  640. if (!imageType) {
  641. throw new Error("null imageType");
  642. }
  643. var url = self.getUrl("Items/" + itemId + "/Images/" + imageType);
  644. if (imageIndex != null) {
  645. url += "/" + imageIndex;
  646. }
  647. return self.ajax({
  648. type: "DELETE",
  649. url: url
  650. });
  651. };
  652. self.updateItemImageIndex = function (itemId, imageType, imageIndex, newIndex) {
  653. if (!itemId) {
  654. throw new Error("null itemId");
  655. }
  656. if (!imageType) {
  657. throw new Error("null imageType");
  658. }
  659. var url = self.getUrl("Items/" + itemId + "/Images/" + imageType + "/" + imageIndex + "/Index", { newIndex: newIndex });
  660. return self.ajax({
  661. type: "POST",
  662. url: url
  663. });
  664. };
  665. self.getItemImageInfos = function (itemId) {
  666. if (!itemId) {
  667. throw new Error("null itemId");
  668. }
  669. var url = self.getUrl("Items/" + itemId + "/Images");
  670. return self.ajax({
  671. type: "GET",
  672. url: url,
  673. dataType: "json"
  674. });
  675. };
  676. self.getCriticReviews = function (itemId, options) {
  677. if (!itemId) {
  678. throw new Error("null itemId");
  679. }
  680. var url = self.getUrl("Items/" + itemId + "/CriticReviews", options);
  681. return self.ajax({
  682. type: "GET",
  683. url: url,
  684. dataType: "json"
  685. });
  686. };
  687. self.getSessions = function (options) {
  688. var url = self.getUrl("Sessions", options);
  689. return self.ajax({
  690. type: "GET",
  691. url: url,
  692. dataType: "json"
  693. });
  694. };
  695. /**
  696. * Uploads a user image
  697. * @param {String} userId
  698. * @param {String} imageType The type of image to delete, based on the server-side ImageType enum.
  699. * @param {Object} file The file from the input element
  700. */
  701. self.uploadUserImage = function (userId, imageType, file) {
  702. if (!userId) {
  703. throw new Error("null userId");
  704. }
  705. if (!imageType) {
  706. throw new Error("null imageType");
  707. }
  708. if (!file) {
  709. throw new Error("File must be an image.");
  710. }
  711. if (file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/jpeg") {
  712. throw new Error("File must be an image.");
  713. }
  714. var deferred = $.Deferred();
  715. var reader = new FileReader();
  716. reader.onerror = function () {
  717. deferred.reject();
  718. };
  719. reader.onabort = function () {
  720. deferred.reject();
  721. };
  722. // Closure to capture the file information.
  723. reader.onload = function (e) {
  724. // Split by a comma to remove the url: prefix
  725. var data = e.target.result.split(',')[1];
  726. var url = self.getUrl("Users/" + userId + "/Images/" + imageType);
  727. self.ajax({
  728. type: "POST",
  729. url: url,
  730. data: data,
  731. contentType: "image/" + file.name.substring(file.name.lastIndexOf('.') + 1)
  732. }).done(function (result) {
  733. deferred.resolveWith(null, [result]);
  734. }).fail(function () {
  735. deferred.reject();
  736. });
  737. };
  738. // Read in the image file as a data URL.
  739. reader.readAsDataURL(file);
  740. return deferred.promise();
  741. };
  742. self.uploadItemImage = function (itemId, imageType, file) {
  743. if (!itemId) {
  744. throw new Error("null itemId");
  745. }
  746. if (!imageType) {
  747. throw new Error("null imageType");
  748. }
  749. if (!file) {
  750. throw new Error("File must be an image.");
  751. }
  752. if (file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/jpeg") {
  753. throw new Error("File must be an image.");
  754. }
  755. var deferred = $.Deferred();
  756. var reader = new FileReader();
  757. reader.onerror = function () {
  758. deferred.reject();
  759. };
  760. reader.onabort = function () {
  761. deferred.reject();
  762. };
  763. // Closure to capture the file information.
  764. reader.onload = function (e) {
  765. // Split by a comma to remove the url: prefix
  766. var data = e.target.result.split(',')[1];
  767. var url = self.getUrl("Items/" + itemId + "/Images/" + imageType);
  768. self.ajax({
  769. type: "POST",
  770. url: url,
  771. data: data,
  772. contentType: "image/" + file.name.substring(file.name.lastIndexOf('.') + 1)
  773. }).done(function (result) {
  774. deferred.resolveWith(null, [result]);
  775. }).fail(function () {
  776. deferred.reject();
  777. });
  778. };
  779. // Read in the image file as a data URL.
  780. reader.readAsDataURL(file);
  781. return deferred.promise();
  782. };
  783. /**
  784. * Gets the list of installed plugins on the server
  785. */
  786. self.getInstalledPlugins = function () {
  787. var url = self.getUrl("Plugins");
  788. return self.ajax({
  789. type: "GET",
  790. url: url,
  791. dataType: "json"
  792. });
  793. };
  794. /**
  795. * Gets a user by id
  796. * @param {String} id
  797. */
  798. self.getUser = function (id) {
  799. if (!id) {
  800. throw new Error("Must supply a userId");
  801. }
  802. var url = self.getUrl("Users/" + id);
  803. return self.ajax({
  804. type: "GET",
  805. url: url,
  806. dataType: "json"
  807. });
  808. };
  809. /**
  810. * Gets a studio
  811. */
  812. self.getStudio = function (name, userId) {
  813. if (!name) {
  814. throw new Error("null name");
  815. }
  816. var options = {};
  817. if (userId) {
  818. options.userId = userId;
  819. }
  820. var url = self.getUrl("Studios/" + self.encodeName(name), options);
  821. return self.ajax({
  822. type: "GET",
  823. url: url,
  824. dataType: "json"
  825. });
  826. };
  827. /**
  828. * Gets a genre
  829. */
  830. self.getGenre = function (name, userId) {
  831. if (!name) {
  832. throw new Error("null name");
  833. }
  834. var options = {};
  835. if (userId) {
  836. options.userId = userId;
  837. }
  838. var url = self.getUrl("Genres/" + self.encodeName(name), options);
  839. return self.ajax({
  840. type: "GET",
  841. url: url,
  842. dataType: "json"
  843. });
  844. };
  845. /**
  846. * Gets an artist
  847. */
  848. self.getArtist = function (name, userId) {
  849. if (!name) {
  850. throw new Error("null name");
  851. }
  852. var options = {};
  853. if (userId) {
  854. options.userId = userId;
  855. }
  856. var url = self.getUrl("Artists/" + self.encodeName(name), options);
  857. return self.ajax({
  858. type: "GET",
  859. url: url,
  860. dataType: "json"
  861. });
  862. };
  863. /**
  864. * Gets a year
  865. */
  866. self.getYear = function (yea, userId) {
  867. if (!name) {
  868. throw new Error("null name");
  869. }
  870. var options = {};
  871. if (userId) {
  872. options.userId = userId;
  873. }
  874. var url = self.getUrl("Years/" + self.encodeName(name), options);
  875. return self.ajax({
  876. type: "GET",
  877. url: url,
  878. dataType: "json"
  879. });
  880. };
  881. /**
  882. * Gets a Person
  883. */
  884. self.getPerson = function (name, userId) {
  885. if (!name) {
  886. throw new Error("null name");
  887. }
  888. var options = {};
  889. if (userId) {
  890. options.userId = userId;
  891. }
  892. var url = self.getUrl("Persons/" + self.encodeName(name), options);
  893. return self.ajax({
  894. type: "GET",
  895. url: url,
  896. dataType: "json"
  897. });
  898. };
  899. /**
  900. * Gets weather info
  901. * @param {String} location - us zip code / city, state, country / city, country
  902. * Omit location to get weather info using stored server configuration value
  903. */
  904. self.getWeatherInfo = function (location) {
  905. var url = self.getUrl("weather", {
  906. location: location
  907. });
  908. return self.ajax({
  909. type: "GET",
  910. url: url,
  911. dataType: "json"
  912. });
  913. };
  914. /**
  915. * Gets all users from the server
  916. */
  917. self.getUsers = function () {
  918. var url = self.getUrl("users");
  919. return self.ajax({
  920. type: "GET",
  921. url: url,
  922. dataType: "json"
  923. });
  924. };
  925. /**
  926. * Gets all available parental ratings from the server
  927. */
  928. self.getParentalRatings = function () {
  929. var url = self.getUrl("Localization/ParentalRatings");
  930. return self.ajax({
  931. type: "GET",
  932. url: url,
  933. dataType: "json"
  934. });
  935. };
  936. /**
  937. * Gets a list of all available conrete BaseItem types from the server
  938. */
  939. self.getItemTypes = function (options) {
  940. var url = self.getUrl("Library/ItemTypes", options);
  941. return self.ajax({
  942. type: "GET",
  943. url: url,
  944. dataType: "json"
  945. });
  946. };
  947. /**
  948. * Constructs a url for a user image
  949. * @param {String} userId
  950. * @param {Object} options
  951. * Options supports the following properties:
  952. * width - download the image at a fixed width
  953. * height - download the image at a fixed height
  954. * maxWidth - download the image at a maxWidth
  955. * maxHeight - download the image at a maxHeight
  956. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  957. * For best results do not specify both width and height together, as aspect ratio might be altered.
  958. */
  959. self.getUserImageUrl = function (userId, options) {
  960. if (!userId) {
  961. throw new Error("null userId");
  962. }
  963. options = options || {
  964. };
  965. var url = "Users/" + userId + "/Images/" + options.type;
  966. if (options.index != null) {
  967. url += "/" + options.index;
  968. }
  969. // Don't put these on the query string
  970. delete options.type;
  971. delete options.index;
  972. return self.getUrl(url, options);
  973. };
  974. /**
  975. * Constructs a url for a person image
  976. * @param {String} name
  977. * @param {Object} options
  978. * Options supports the following properties:
  979. * width - download the image at a fixed width
  980. * height - download the image at a fixed height
  981. * maxWidth - download the image at a maxWidth
  982. * maxHeight - download the image at a maxHeight
  983. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  984. * For best results do not specify both width and height together, as aspect ratio might be altered.
  985. */
  986. self.getPersonImageUrl = function (name, options) {
  987. if (!name) {
  988. throw new Error("null name");
  989. }
  990. options = options || {
  991. };
  992. var url = "Persons/" + self.encodeName(name) + "/Images/" + options.type;
  993. if (options.index != null) {
  994. url += "/" + options.index;
  995. }
  996. // Don't put these on the query string
  997. delete options.type;
  998. delete options.index;
  999. return self.getUrl(url, options);
  1000. };
  1001. /**
  1002. * Constructs a url for a year image
  1003. * @param {String} year
  1004. * @param {Object} options
  1005. * Options supports the following properties:
  1006. * width - download the image at a fixed width
  1007. * height - download the image at a fixed height
  1008. * maxWidth - download the image at a maxWidth
  1009. * maxHeight - download the image at a maxHeight
  1010. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1011. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1012. */
  1013. self.getYearImageUrl = function (year, options) {
  1014. if (!year) {
  1015. throw new Error("null year");
  1016. }
  1017. options = options || {
  1018. };
  1019. var url = "Years/" + year + "/Images/" + options.type;
  1020. if (options.index != null) {
  1021. url += "/" + options.index;
  1022. }
  1023. // Don't put these on the query string
  1024. delete options.type;
  1025. delete options.index;
  1026. return self.getUrl(url, options);
  1027. };
  1028. /**
  1029. * Constructs a url for a genre image
  1030. * @param {String} name
  1031. * @param {Object} options
  1032. * Options supports the following properties:
  1033. * width - download the image at a fixed width
  1034. * height - download the image at a fixed height
  1035. * maxWidth - download the image at a maxWidth
  1036. * maxHeight - download the image at a maxHeight
  1037. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1038. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1039. */
  1040. self.getGenreImageUrl = function (name, options) {
  1041. if (!name) {
  1042. throw new Error("null name");
  1043. }
  1044. options = options || {
  1045. };
  1046. var url = "Genres/" + self.encodeName(name) + "/Images/" + options.type;
  1047. if (options.index != null) {
  1048. url += "/" + options.index;
  1049. }
  1050. // Don't put these on the query string
  1051. delete options.type;
  1052. delete options.index;
  1053. return self.getUrl(url, options);
  1054. };
  1055. /**
  1056. * Constructs a url for a artist image
  1057. * @param {String} name
  1058. * @param {Object} options
  1059. * Options supports the following properties:
  1060. * width - download the image at a fixed width
  1061. * height - download the image at a fixed height
  1062. * maxWidth - download the image at a maxWidth
  1063. * maxHeight - download the image at a maxHeight
  1064. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1065. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1066. */
  1067. self.getArtistImageUrl = function (name, options) {
  1068. if (!name) {
  1069. throw new Error("null name");
  1070. }
  1071. options = options || {
  1072. };
  1073. var url = "Artists/" + self.encodeName(name) + "/Images/" + options.type;
  1074. if (options.index != null) {
  1075. url += "/" + options.index;
  1076. }
  1077. // Don't put these on the query string
  1078. delete options.type;
  1079. delete options.index;
  1080. return self.getUrl(url, options);
  1081. };
  1082. /**
  1083. * Constructs a url for a studio image
  1084. * @param {String} name
  1085. * @param {Object} options
  1086. * Options supports the following properties:
  1087. * width - download the image at a fixed width
  1088. * height - download the image at a fixed height
  1089. * maxWidth - download the image at a maxWidth
  1090. * maxHeight - download the image at a maxHeight
  1091. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1092. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1093. */
  1094. self.getStudioImageUrl = function (name, options) {
  1095. if (!name) {
  1096. throw new Error("null name");
  1097. }
  1098. options = options || {
  1099. };
  1100. var url = "Studios/" + self.encodeName(name) + "/Images/" + options.type;
  1101. if (options.index != null) {
  1102. url += "/" + options.index;
  1103. }
  1104. // Don't put these on the query string
  1105. delete options.type;
  1106. delete options.index;
  1107. return self.getUrl(url, options);
  1108. };
  1109. /**
  1110. * Constructs a url for an item image
  1111. * @param {String} itemId
  1112. * @param {Object} options
  1113. * Options supports the following properties:
  1114. * type - Primary, logo, backdrop, etc. See the server-side enum ImageType
  1115. * index - When downloading a backdrop, use this to specify which one (omitting is equivalent to zero)
  1116. * width - download the image at a fixed width
  1117. * height - download the image at a fixed height
  1118. * maxWidth - download the image at a maxWidth
  1119. * maxHeight - download the image at a maxHeight
  1120. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1121. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1122. */
  1123. self.getImageUrl = function (itemId, options) {
  1124. if (!itemId) {
  1125. throw new Error("itemId cannot be empty");
  1126. }
  1127. options = options || {
  1128. };
  1129. var url = "Items/" + itemId + "/Images/" + options.type;
  1130. if (options.index != null) {
  1131. url += "/" + options.index;
  1132. }
  1133. // Don't put these on the query string
  1134. delete options.type;
  1135. delete options.index;
  1136. return self.getUrl(url, options);
  1137. };
  1138. /**
  1139. * Constructs a url for an item logo image
  1140. * If the item doesn't have a logo, it will inherit a logo from a parent
  1141. * @param {Object} item A BaseItem
  1142. * @param {Object} options
  1143. * Options supports the following properties:
  1144. * width - download the image at a fixed width
  1145. * height - download the image at a fixed height
  1146. * maxWidth - download the image at a maxWidth
  1147. * maxHeight - download the image at a maxHeight
  1148. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1149. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1150. */
  1151. self.getLogoImageUrl = function (item, options) {
  1152. if (!item) {
  1153. throw new Error("null item");
  1154. }
  1155. options = options || {
  1156. };
  1157. options.imageType = "logo";
  1158. var logoItemId = item.HasLogo ? item.Id : item.ParentLogoItemId;
  1159. return logoItemId ? self.getImageUrl(logoItemId, options) : null;
  1160. };
  1161. /**
  1162. * Constructs an array of backdrop image url's for an item
  1163. * If the item doesn't have any backdrops, it will inherit them from a parent
  1164. * @param {Object} item A BaseItem
  1165. * @param {Object} options
  1166. * Options supports the following properties:
  1167. * width - download the image at a fixed width
  1168. * height - download the image at a fixed height
  1169. * maxWidth - download the image at a maxWidth
  1170. * maxHeight - download the image at a maxHeight
  1171. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1172. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1173. */
  1174. self.getBackdropImageUrl = function (item, options) {
  1175. if (!item) {
  1176. throw new Error("null item");
  1177. }
  1178. options = options || {
  1179. };
  1180. options.imageType = "backdrop";
  1181. var backdropItemId;
  1182. var backdropCount;
  1183. if (!item.BackdropCount) {
  1184. backdropItemId = item.ParentBackdropItemId;
  1185. backdropCount = item.ParentBackdropCount || 0;
  1186. } else {
  1187. backdropItemId = item.Id;
  1188. backdropCount = item.BackdropCount;
  1189. }
  1190. if (!backdropItemId) {
  1191. return [];
  1192. }
  1193. var files = [];
  1194. for (var i = 0; i < backdropCount; i++) {
  1195. options.imageIndex = i;
  1196. files[i] = self.getImageUrl(backdropItemId, options);
  1197. }
  1198. return files;
  1199. };
  1200. /**
  1201. * Authenticates a user
  1202. * @param {String} userId
  1203. * @param {String} password
  1204. */
  1205. self.authenticateUser = function (userId, password) {
  1206. if (!userId) {
  1207. throw new Error("null userId");
  1208. }
  1209. var url = self.getUrl("Users/" + userId + "/authenticate");
  1210. var postData = {
  1211. password: MediaBrowser.SHA1(password || "")
  1212. };
  1213. return self.ajax({
  1214. type: "POST",
  1215. url: url,
  1216. data: JSON.stringify(postData),
  1217. contentType: "application/json"
  1218. });
  1219. };
  1220. /**
  1221. * Updates a user's password
  1222. * @param {String} userId
  1223. * @param {String} currentPassword
  1224. * @param {String} newPassword
  1225. */
  1226. self.updateUserPassword = function (userId, currentPassword, newPassword) {
  1227. if (!userId) {
  1228. throw new Error("null userId");
  1229. }
  1230. var url = self.getUrl("Users/" + userId + "/Password");
  1231. var postData = {
  1232. };
  1233. postData.currentPassword = MediaBrowser.SHA1(currentPassword);
  1234. if (newPassword) {
  1235. postData.newPassword = newPassword;
  1236. }
  1237. return self.ajax({
  1238. type: "POST",
  1239. url: url,
  1240. data: postData
  1241. });
  1242. };
  1243. /**
  1244. * Resets a user's password
  1245. * @param {String} userId
  1246. */
  1247. self.resetUserPassword = function (userId) {
  1248. if (!userId) {
  1249. throw new Error("null userId");
  1250. }
  1251. var url = self.getUrl("Users/" + userId + "/Password");
  1252. var postData = {
  1253. };
  1254. postData.resetPassword = true;
  1255. return self.ajax({
  1256. type: "POST",
  1257. url: url,
  1258. data: postData
  1259. });
  1260. };
  1261. /**
  1262. * Updates the server's configuration
  1263. * @param {Object} configuration
  1264. */
  1265. self.updateServerConfiguration = function (configuration) {
  1266. if (!configuration) {
  1267. throw new Error("null configuration");
  1268. }
  1269. var url = self.getUrl("System/Configuration");
  1270. return self.ajax({
  1271. type: "POST",
  1272. url: url,
  1273. data: JSON.stringify(configuration),
  1274. contentType: "application/json"
  1275. });
  1276. };
  1277. /**
  1278. * Updates plugin security info
  1279. */
  1280. self.updatePluginSecurityInfo = function (info) {
  1281. var url = self.getUrl("Plugins/SecurityInfo");
  1282. return self.ajax({
  1283. type: "POST",
  1284. url: url,
  1285. data: JSON.stringify(info),
  1286. contentType: "application/json"
  1287. });
  1288. };
  1289. /**
  1290. * Creates a user
  1291. * @param {Object} user
  1292. */
  1293. self.createUser = function (user) {
  1294. if (!user) {
  1295. throw new Error("null user");
  1296. }
  1297. var url = self.getUrl("Users");
  1298. return self.ajax({
  1299. type: "POST",
  1300. url: url,
  1301. data: JSON.stringify(user),
  1302. dataType: "json",
  1303. contentType: "application/json"
  1304. });
  1305. };
  1306. /**
  1307. * Updates a user
  1308. * @param {Object} user
  1309. */
  1310. self.updateUser = function (user) {
  1311. if (!user) {
  1312. throw new Error("null user");
  1313. }
  1314. var url = self.getUrl("Users/" + user.Id);
  1315. return self.ajax({
  1316. type: "POST",
  1317. url: url,
  1318. data: JSON.stringify(user),
  1319. contentType: "application/json"
  1320. });
  1321. };
  1322. /**
  1323. * Updates the Triggers for a ScheduledTask
  1324. * @param {String} id
  1325. * @param {Object} triggers
  1326. */
  1327. self.updateScheduledTaskTriggers = function (id, triggers) {
  1328. if (!id) {
  1329. throw new Error("null id");
  1330. }
  1331. if (!triggers) {
  1332. throw new Error("null triggers");
  1333. }
  1334. var url = self.getUrl("ScheduledTasks/" + id + "/Triggers");
  1335. return self.ajax({
  1336. type: "POST",
  1337. url: url,
  1338. data: JSON.stringify(triggers),
  1339. contentType: "application/json"
  1340. });
  1341. };
  1342. /**
  1343. * Updates a plugin's configuration
  1344. * @param {String} Id
  1345. * @param {Object} configuration
  1346. */
  1347. self.updatePluginConfiguration = function (id, configuration) {
  1348. if (!id) {
  1349. throw new Error("null Id");
  1350. }
  1351. if (!configuration) {
  1352. throw new Error("null configuration");
  1353. }
  1354. var url = self.getUrl("Plugins/" + id + "/Configuration");
  1355. return self.ajax({
  1356. type: "POST",
  1357. url: url,
  1358. data: JSON.stringify(configuration),
  1359. contentType: "application/json"
  1360. });
  1361. };
  1362. /**
  1363. * Gets items based on a query, typically for children of a folder
  1364. * @param {String} userId
  1365. * @param {Object} options
  1366. * Options accepts the following properties:
  1367. * itemId - Localize the search to a specific folder (root if omitted)
  1368. * startIndex - Use for paging
  1369. * limit - Use to limit results to a certain number of items
  1370. * filter - Specify one or more ItemFilters, comma delimeted (see server-side enum)
  1371. * sortBy - Specify an ItemSortBy (comma-delimeted list see server-side enum)
  1372. * sortOrder - ascending/descending
  1373. * fields - additional fields to include aside from basic info. This is a comma delimited list. See server-side enum ItemFields.
  1374. * index - the name of the dynamic, localized index function
  1375. * dynamicSortBy - the name of the dynamic localized sort function
  1376. * recursive - Whether or not the query should be recursive
  1377. * searchTerm - search term to use as a filter
  1378. */
  1379. self.getItems = function (userId, options) {
  1380. if (!userId) {
  1381. throw new Error("null userId");
  1382. }
  1383. var url = self.getUrl("Users/" + userId + "/Items", options);
  1384. return self.ajax({
  1385. type: "GET",
  1386. url: url,
  1387. dataType: "json"
  1388. });
  1389. };
  1390. /**
  1391. Gets artists from an item
  1392. */
  1393. self.getArtists = function (userId, options) {
  1394. if (!userId) {
  1395. throw new Error("null userId");
  1396. }
  1397. options = options || {};
  1398. options.userId = userId;
  1399. var url = self.getUrl("Artists", options);
  1400. return self.ajax({
  1401. type: "GET",
  1402. url: url,
  1403. dataType: "json"
  1404. });
  1405. };
  1406. /**
  1407. Gets genres from an item
  1408. */
  1409. self.getGenres = function (userId, options) {
  1410. if (!userId) {
  1411. throw new Error("null userId");
  1412. }
  1413. options = options || {};
  1414. options.userId = userId;
  1415. var url = self.getUrl("Genres", options);
  1416. return self.ajax({
  1417. type: "GET",
  1418. url: url,
  1419. dataType: "json"
  1420. });
  1421. };
  1422. /**
  1423. Gets people from an item
  1424. */
  1425. self.getPeople = function (userId, options) {
  1426. if (!userId) {
  1427. throw new Error("null userId");
  1428. }
  1429. options = options || {};
  1430. options.userId = userId;
  1431. var url = self.getUrl("Persons", options);
  1432. return self.ajax({
  1433. type: "GET",
  1434. url: url,
  1435. dataType: "json"
  1436. });
  1437. };
  1438. /**
  1439. Gets studios from an item
  1440. */
  1441. self.getStudios = function (userId, options) {
  1442. if (!userId) {
  1443. throw new Error("null userId");
  1444. }
  1445. options = options || {};
  1446. options.userId = userId;
  1447. var url = self.getUrl("Studios", options);
  1448. return self.ajax({
  1449. type: "GET",
  1450. url: url,
  1451. dataType: "json"
  1452. });
  1453. };
  1454. /**
  1455. * Gets local trailers for an item
  1456. */
  1457. self.getLocalTrailers = function (userId, itemId) {
  1458. if (!userId) {
  1459. throw new Error("null userId");
  1460. }
  1461. if (!itemId) {
  1462. throw new Error("null itemId");
  1463. }
  1464. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/LocalTrailers");
  1465. return self.ajax({
  1466. type: "GET",
  1467. url: url,
  1468. dataType: "json"
  1469. });
  1470. };
  1471. /**
  1472. * Gets theme songs for an item
  1473. */
  1474. self.getThemeSongs = function (userId, itemId) {
  1475. if (!itemId) {
  1476. throw new Error("null itemId");
  1477. }
  1478. var options = {};
  1479. if (userId) {
  1480. options.userId = userId;
  1481. }
  1482. var url = self.getUrl("Items/" + itemId + "/ThemeSongs", options);
  1483. return self.ajax({
  1484. type: "GET",
  1485. url: url,
  1486. dataType: "json"
  1487. });
  1488. };
  1489. self.getThemeVideos = function (userId, itemId) {
  1490. if (!itemId) {
  1491. throw new Error("null itemId");
  1492. }
  1493. var options = {};
  1494. if (userId) {
  1495. options.userId = userId;
  1496. }
  1497. var url = self.getUrl("Items/" + itemId + "/ThemeVideos", options);
  1498. return self.ajax({
  1499. type: "GET",
  1500. url: url,
  1501. dataType: "json"
  1502. });
  1503. };
  1504. self.getSearchHints = function (options) {
  1505. var url = self.getUrl("Search/Hints", options);
  1506. return self.ajax({
  1507. type: "GET",
  1508. url: url,
  1509. dataType: "json"
  1510. });
  1511. };
  1512. /**
  1513. * Gets special features for an item
  1514. */
  1515. self.getSpecialFeatures = function (userId, itemId) {
  1516. if (!userId) {
  1517. throw new Error("null userId");
  1518. }
  1519. if (!itemId) {
  1520. throw new Error("null itemId");
  1521. }
  1522. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/SpecialFeatures");
  1523. return self.ajax({
  1524. type: "GET",
  1525. url: url,
  1526. dataType: "json"
  1527. });
  1528. };
  1529. /**
  1530. * Marks an item as played or unplayed
  1531. * This should not be used to update playstate following playback.
  1532. * There are separate playstate check-in methods for that. This should be used for a
  1533. * separate option to reset playstate.
  1534. * @param {String} userId
  1535. * @param {String} itemId
  1536. * @param {Boolean} wasPlayed
  1537. */
  1538. self.updatePlayedStatus = function (userId, itemId, wasPlayed) {
  1539. if (!userId) {
  1540. throw new Error("null userId");
  1541. }
  1542. if (!itemId) {
  1543. throw new Error("null itemId");
  1544. }
  1545. var url = self.getUrl("Users/" + userId + "/PlayedItems/" + itemId);
  1546. var method = wasPlayed ? "POST" : "DELETE";
  1547. return self.ajax({
  1548. type: method,
  1549. url: url
  1550. });
  1551. };
  1552. /**
  1553. * Updates a user's favorite status for an item.
  1554. * @param {String} userId
  1555. * @param {String} itemId
  1556. * @param {Boolean} isFavorite
  1557. */
  1558. self.updateFavoriteStatus = function (userId, itemId, isFavorite) {
  1559. if (!userId) {
  1560. throw new Error("null userId");
  1561. }
  1562. if (!itemId) {
  1563. throw new Error("null itemId");
  1564. }
  1565. var url = self.getUrl("Users/" + userId + "/FavoriteItems/" + itemId);
  1566. var method = isFavorite ? "POST" : "DELETE";
  1567. return self.ajax({
  1568. type: method,
  1569. url: url
  1570. });
  1571. };
  1572. /**
  1573. * Updates a user's personal rating for an item
  1574. * @param {String} userId
  1575. * @param {String} itemId
  1576. * @param {Boolean} likes
  1577. */
  1578. self.updateUserItemRating = function (userId, itemId, likes) {
  1579. if (!userId) {
  1580. throw new Error("null userId");
  1581. }
  1582. if (!itemId) {
  1583. throw new Error("null itemId");
  1584. }
  1585. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating", {
  1586. likes: likes
  1587. });
  1588. return self.ajax({
  1589. type: "POST",
  1590. url: url
  1591. });
  1592. };
  1593. /**
  1594. * Updates a user's favorite status for an item by name.
  1595. * @param {String} userId
  1596. * @param {String} name
  1597. * @param {Boolean} isFavorite
  1598. */
  1599. self.updateFavoriteArtistStatus = function (userId, name, isFavorite) {
  1600. if (!userId) {
  1601. throw new Error("null userId");
  1602. }
  1603. if (!name) {
  1604. throw new Error("null name");
  1605. }
  1606. var url = self.getUrl("Users/" + userId + "/Favorites/Artists/" + self.encodeName(name));
  1607. var method = isFavorite ? "POST" : "DELETE";
  1608. return self.ajax({
  1609. type: method,
  1610. url: url
  1611. });
  1612. };
  1613. self.updateFavoritePersonStatus = function (userId, name, isFavorite) {
  1614. if (!userId) {
  1615. throw new Error("null userId");
  1616. }
  1617. if (!name) {
  1618. throw new Error("null name");
  1619. }
  1620. var url = self.getUrl("Users/" + userId + "/Favorites/Persons/" + self.encodeName(name));
  1621. var method = isFavorite ? "POST" : "DELETE";
  1622. return self.ajax({
  1623. type: method,
  1624. url: url
  1625. });
  1626. };
  1627. self.updateFavoriteStudioStatus = function (userId, name, isFavorite) {
  1628. if (!userId) {
  1629. throw new Error("null userId");
  1630. }
  1631. if (!name) {
  1632. throw new Error("null name");
  1633. }
  1634. var url = self.getUrl("Users/" + userId + "/Favorites/Studios/" + self.encodeName(name));
  1635. var method = isFavorite ? "POST" : "DELETE";
  1636. return self.ajax({
  1637. type: method,
  1638. url: url
  1639. });
  1640. };
  1641. self.updateFavoriteGenreStatus = function (userId, name, isFavorite) {
  1642. if (!userId) {
  1643. throw new Error("null userId");
  1644. }
  1645. if (!name) {
  1646. throw new Error("null name");
  1647. }
  1648. var url = self.getUrl("Users/" + userId + "/Favorites/Genres/" + self.encodeName(name));
  1649. var method = isFavorite ? "POST" : "DELETE";
  1650. return self.ajax({
  1651. type: method,
  1652. url: url
  1653. });
  1654. };
  1655. /**
  1656. * Updates a user's rating for an item by name.
  1657. * @param {String} userId
  1658. * @param {String} name
  1659. * @param {Boolean} likes
  1660. */
  1661. self.updateArtistRating = function (userId, name, likes) {
  1662. if (!userId) {
  1663. throw new Error("null userId");
  1664. }
  1665. if (!name) {
  1666. throw new Error("null name");
  1667. }
  1668. var url = self.getUrl("Users/" + userId + "/Ratings/Artists/" + self.encodeName(name), {
  1669. likes: likes
  1670. });
  1671. return self.ajax({
  1672. type: "POST",
  1673. url: url
  1674. });
  1675. };
  1676. self.updatePersonRating = function (userId, name, likes) {
  1677. if (!userId) {
  1678. throw new Error("null userId");
  1679. }
  1680. if (!name) {
  1681. throw new Error("null name");
  1682. }
  1683. var url = self.getUrl("Users/" + userId + "/Ratings/Persons/" + self.encodeName(name), {
  1684. likes: likes
  1685. });
  1686. return self.ajax({
  1687. type: "POST",
  1688. url: url
  1689. });
  1690. };
  1691. self.updateStudioRating = function (userId, name, likes) {
  1692. if (!userId) {
  1693. throw new Error("null userId");
  1694. }
  1695. if (!name) {
  1696. throw new Error("null name");
  1697. }
  1698. var url = self.getUrl("Users/" + userId + "/Ratings/Studios/" + self.encodeName(name), {
  1699. likes: likes
  1700. });
  1701. return self.ajax({
  1702. type: "POST",
  1703. url: url
  1704. });
  1705. };
  1706. self.updateGenreRating = function (userId, name, likes) {
  1707. if (!userId) {
  1708. throw new Error("null userId");
  1709. }
  1710. if (!name) {
  1711. throw new Error("null name");
  1712. }
  1713. var url = self.getUrl("Users/" + userId + "/Ratings/Genres/" + self.encodeName(name), {
  1714. likes: likes
  1715. });
  1716. return self.ajax({
  1717. type: "POST",
  1718. url: url
  1719. });
  1720. };
  1721. /**
  1722. * Clears a user's rating for an item by name.
  1723. * @param {String} userId
  1724. * @param {String} name
  1725. */
  1726. self.clearArtistRating = function (userId, name) {
  1727. if (!userId) {
  1728. throw new Error("null userId");
  1729. }
  1730. if (!name) {
  1731. throw new Error("null name");
  1732. }
  1733. var url = self.getUrl("Users/" + userId + "/Ratings/Artists/" + self.encodeName(name));
  1734. return self.ajax({
  1735. type: "DELETE",
  1736. url: url
  1737. });
  1738. };
  1739. self.clearPersonRating = function (userId, name) {
  1740. if (!userId) {
  1741. throw new Error("null userId");
  1742. }
  1743. if (!name) {
  1744. throw new Error("null name");
  1745. }
  1746. var url = self.getUrl("Users/" + userId + "/Ratings/Persons/" + self.encodeName(name));
  1747. return self.ajax({
  1748. type: "DELETE",
  1749. url: url
  1750. });
  1751. };
  1752. self.clearStudioRating = function (userId, name) {
  1753. if (!userId) {
  1754. throw new Error("null userId");
  1755. }
  1756. if (!name) {
  1757. throw new Error("null name");
  1758. }
  1759. var url = self.getUrl("Users/" + userId + "/Ratings/Studios/" + self.encodeName(name));
  1760. return self.ajax({
  1761. type: "DELETE",
  1762. url: url
  1763. });
  1764. };
  1765. self.clearGenreRating = function (userId, name) {
  1766. if (!userId) {
  1767. throw new Error("null userId");
  1768. }
  1769. if (!name) {
  1770. throw new Error("null name");
  1771. }
  1772. var url = self.getUrl("Users/" + userId + "/Ratings/Genres/" + self.encodeName(name));
  1773. return self.ajax({
  1774. type: "DELETE",
  1775. url: url
  1776. });
  1777. };
  1778. self.getItemCounts = function (userId) {
  1779. var options = {};
  1780. if (userId) {
  1781. options.userId = userId;
  1782. }
  1783. var url = self.getUrl("Items/Counts", options);
  1784. return self.ajax({
  1785. type: "GET",
  1786. url: url,
  1787. dataType: "json"
  1788. });
  1789. };
  1790. /**
  1791. Gets a variety of item counts that a person appears in
  1792. */
  1793. self.getPersonItemCounts = function (userId, name) {
  1794. if (!userId) {
  1795. throw new Error("null userId");
  1796. }
  1797. if (!name) {
  1798. throw new Error("null name");
  1799. }
  1800. var url = self.getUrl("Persons/" + self.encodeName(name) + "/Counts", {
  1801. userId: userId
  1802. });
  1803. return self.ajax({
  1804. type: "GET",
  1805. url: url,
  1806. dataType: "json"
  1807. });
  1808. };
  1809. /**
  1810. Gets a variety of item counts that a genre appears in
  1811. */
  1812. self.getGenreItemCounts = function (userId, name) {
  1813. if (!userId) {
  1814. throw new Error("null userId");
  1815. }
  1816. if (!name) {
  1817. throw new Error("null name");
  1818. }
  1819. var url = self.getUrl("Genres/" + self.encodeName(name) + "/Counts", {
  1820. userId: userId
  1821. });
  1822. return self.ajax({
  1823. type: "GET",
  1824. url: url,
  1825. dataType: "json"
  1826. });
  1827. };
  1828. /**
  1829. Gets a variety of item counts that an artist appears in
  1830. */
  1831. self.getArtistItemCounts = function (userId, name) {
  1832. if (!userId) {
  1833. throw new Error("null userId");
  1834. }
  1835. if (!name) {
  1836. throw new Error("null name");
  1837. }
  1838. var url = self.getUrl("Artists/" + self.encodeName(name) + "/Counts", {
  1839. userId: userId
  1840. });
  1841. return self.ajax({
  1842. type: "GET",
  1843. url: url,
  1844. dataType: "json"
  1845. });
  1846. };
  1847. /**
  1848. Gets a variety of item counts that a studio appears in
  1849. */
  1850. self.getStudioItemCounts = function (userId, name) {
  1851. if (!userId) {
  1852. throw new Error("null userId");
  1853. }
  1854. if (!name) {
  1855. throw new Error("null name");
  1856. }
  1857. var url = self.getUrl("Studios/" + self.encodeName(name) + "/Counts", {
  1858. userId: userId
  1859. });
  1860. return self.ajax({
  1861. type: "GET",
  1862. url: url,
  1863. dataType: "json"
  1864. });
  1865. };
  1866. /**
  1867. * Clears a user's personal rating for an item
  1868. * @param {String} userId
  1869. * @param {String} itemId
  1870. */
  1871. self.clearUserItemRating = function (userId, itemId) {
  1872. if (!userId) {
  1873. throw new Error("null userId");
  1874. }
  1875. if (!itemId) {
  1876. throw new Error("null itemId");
  1877. }
  1878. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating");
  1879. return self.ajax({
  1880. type: "DELETE",
  1881. url: url
  1882. });
  1883. };
  1884. /**
  1885. * Reports the user has started playing something
  1886. * @param {String} userId
  1887. * @param {String} itemId
  1888. */
  1889. self.reportPlaybackStart = function (userId, itemId) {
  1890. if (!userId) {
  1891. throw new Error("null userId");
  1892. }
  1893. if (!itemId) {
  1894. throw new Error("null itemId");
  1895. }
  1896. if (self.isWebSocketOpen()) {
  1897. var deferred = $.Deferred();
  1898. self.sendWebSocketMessage("PlaybackStart", itemId);
  1899. deferred.resolveWith(null, []);
  1900. return deferred.promise();
  1901. }
  1902. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId);
  1903. return self.ajax({
  1904. type: "POST",
  1905. url: url
  1906. });
  1907. };
  1908. /**
  1909. * Reports progress viewing an item
  1910. * @param {String} userId
  1911. * @param {String} itemId
  1912. */
  1913. self.reportPlaybackProgress = function (userId, itemId, positionTicks, isPaused) {
  1914. if (!userId) {
  1915. throw new Error("null userId");
  1916. }
  1917. if (!itemId) {
  1918. throw new Error("null itemId");
  1919. }
  1920. if (self.isWebSocketOpen()) {
  1921. var deferred = $.Deferred();
  1922. self.sendWebSocketMessage("PlaybackProgress", itemId + "|" + (positionTicks == null ? "" : positionTicks) + "|" + (isPaused == null ? "" : isPaused));
  1923. deferred.resolveWith(null, []);
  1924. return deferred.promise();
  1925. }
  1926. var params = {
  1927. };
  1928. if (positionTicks) {
  1929. params.positionTicks = positionTicks;
  1930. }
  1931. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId + "/Progress", params);
  1932. return self.ajax({
  1933. type: "POST",
  1934. url: url
  1935. });
  1936. };
  1937. /**
  1938. * Reports a user has stopped playing an item
  1939. * @param {String} userId
  1940. * @param {String} itemId
  1941. */
  1942. self.reportPlaybackStopped = function (userId, itemId, positionTicks) {
  1943. if (!userId) {
  1944. throw new Error("null userId");
  1945. }
  1946. if (!itemId) {
  1947. throw new Error("null itemId");
  1948. }
  1949. if (self.isWebSocketOpen()) {
  1950. var deferred = $.Deferred();
  1951. self.sendWebSocketMessage("PlaybackStopped", itemId + "|" + (positionTicks == null ? "" : positionTicks));
  1952. deferred.resolveWith(null, []);
  1953. return deferred.promise();
  1954. }
  1955. var params = {
  1956. };
  1957. if (positionTicks) {
  1958. params.positionTicks = positionTicks;
  1959. }
  1960. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId, params);
  1961. return self.ajax({
  1962. type: "DELETE",
  1963. url: url
  1964. });
  1965. };
  1966. }
  1967. }(jQuery, navigator, window.JSON, window.WebSocket, setTimeout);
  1968. /**
  1969. * Provides a friendly way to create an api client instance using information from the browser's current url
  1970. */
  1971. MediaBrowser.ApiClient.create = function (clientName) {
  1972. var loc = window.location;
  1973. return new MediaBrowser.ApiClient(loc.protocol, loc.hostname, loc.port, clientName);
  1974. };
  1975. /**
  1976. *
  1977. * Secure Hash Algorithm (SHA1)
  1978. * http://www.webtoolkit.info/
  1979. *
  1980. **/
  1981. MediaBrowser.SHA1 = function (msg) {
  1982. function rotate_left(n, s) {
  1983. var t4 = (n << s) | (n >>> (32 - s));
  1984. return t4;
  1985. }
  1986. function lsb_hex(val) {
  1987. var str = "";
  1988. var i;
  1989. var vh;
  1990. var vl;
  1991. for (i = 0; i <= 6; i += 2) {
  1992. vh = (val >>> (i * 4 + 4)) & 0x0f;
  1993. vl = (val >>> (i * 4)) & 0x0f;
  1994. str += vh.toString(16) + vl.toString(16);
  1995. }
  1996. return str;
  1997. }
  1998. function cvt_hex(val) {
  1999. var str = "";
  2000. var i;
  2001. var v;
  2002. for (i = 7; i >= 0; i--) {
  2003. v = (val >>> (i * 4)) & 0x0f;
  2004. str += v.toString(16);
  2005. }
  2006. return str;
  2007. }
  2008. function Utf8Encode(string) {
  2009. string = string.replace(/\r\n/g, "\n");
  2010. var utftext = "";
  2011. for (var n = 0; n < string.length; n++) {
  2012. var c = string.charCodeAt(n);
  2013. if (c < 128) {
  2014. utftext += String.fromCharCode(c);
  2015. }
  2016. else if ((c > 127) && (c < 2048)) {
  2017. utftext += String.fromCharCode((c >> 6) | 192);
  2018. utftext += String.fromCharCode((c & 63) | 128);
  2019. }
  2020. else {
  2021. utftext += String.fromCharCode((c >> 12) | 224);
  2022. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  2023. utftext += String.fromCharCode((c & 63) | 128);
  2024. }
  2025. }
  2026. return utftext;
  2027. }
  2028. var blockstart;
  2029. var i, j;
  2030. var W = new Array(80);
  2031. var H0 = 0x67452301;
  2032. var H1 = 0xEFCDAB89;
  2033. var H2 = 0x98BADCFE;
  2034. var H3 = 0x10325476;
  2035. var H4 = 0xC3D2E1F0;
  2036. var A, B, C, D, E;
  2037. var temp;
  2038. msg = Utf8Encode(msg);
  2039. var msg_len = msg.length;
  2040. var word_array = new Array();
  2041. for (i = 0; i < msg_len - 3; i += 4) {
  2042. j = msg.charCodeAt(i) << 24 | msg.charCodeAt(i + 1) << 16 |
  2043. msg.charCodeAt(i + 2) << 8 | msg.charCodeAt(i + 3);
  2044. word_array.push(j);
  2045. }
  2046. switch (msg_len % 4) {
  2047. case 0:
  2048. i = 0x080000000;
  2049. break;
  2050. case 1:
  2051. i = msg.charCodeAt(msg_len - 1) << 24 | 0x0800000;
  2052. break;
  2053. case 2:
  2054. i = msg.charCodeAt(msg_len - 2) << 24 | msg.charCodeAt(msg_len - 1) << 16 | 0x08000;
  2055. break;
  2056. case 3:
  2057. i = msg.charCodeAt(msg_len - 3) << 24 | msg.charCodeAt(msg_len - 2) << 16 | msg.charCodeAt(msg_len - 1) << 8 | 0x80;
  2058. break;
  2059. }
  2060. word_array.push(i);
  2061. while ((word_array.length % 16) != 14) word_array.push(0);
  2062. word_array.push(msg_len >>> 29);
  2063. word_array.push((msg_len << 3) & 0x0ffffffff);
  2064. for (blockstart = 0; blockstart < word_array.length; blockstart += 16) {
  2065. for (i = 0; i < 16; i++) W[i] = word_array[blockstart + i];
  2066. for (i = 16; i <= 79; i++) W[i] = rotate_left(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
  2067. A = H0;
  2068. B = H1;
  2069. C = H2;
  2070. D = H3;
  2071. E = H4;
  2072. for (i = 0; i <= 19; i++) {
  2073. temp = (rotate_left(A, 5) + ((B & C) | (~B & D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
  2074. E = D;
  2075. D = C;
  2076. C = rotate_left(B, 30);
  2077. B = A;
  2078. A = temp;
  2079. }
  2080. for (i = 20; i <= 39; i++) {
  2081. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
  2082. E = D;
  2083. D = C;
  2084. C = rotate_left(B, 30);
  2085. B = A;
  2086. A = temp;
  2087. }
  2088. for (i = 40; i <= 59; i++) {
  2089. temp = (rotate_left(A, 5) + ((B & C) | (B & D) | (C & D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
  2090. E = D;
  2091. D = C;
  2092. C = rotate_left(B, 30);
  2093. B = A;
  2094. A = temp;
  2095. }
  2096. for (i = 60; i <= 79; i++) {
  2097. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
  2098. E = D;
  2099. D = C;
  2100. C = rotate_left(B, 30);
  2101. B = A;
  2102. A = temp;
  2103. }
  2104. H0 = (H0 + A) & 0x0ffffffff;
  2105. H1 = (H1 + B) & 0x0ffffffff;
  2106. H2 = (H2 + C) & 0x0ffffffff;
  2107. H3 = (H3 + D) & 0x0ffffffff;
  2108. H4 = (H4 + E) & 0x0ffffffff;
  2109. }
  2110. var temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4);
  2111. return temp.toLowerCase();
  2112. };
  2113. (function (jQuery, window, undefined) {
  2114. "use strict";
  2115. var matched, browser;
  2116. jQuery.uaMatch = function (ua) {
  2117. ua = ua.toLowerCase();
  2118. var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
  2119. /(webkit)[ \/]([\w.]+)/.exec(ua) ||
  2120. /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
  2121. /(msie) ([\w.]+)/.exec(ua) ||
  2122. ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
  2123. [];
  2124. var platform_match = /(ipad)/.exec(ua) ||
  2125. /(iphone)/.exec(ua) ||
  2126. /(android)/.exec(ua) ||
  2127. [];
  2128. return {
  2129. browser: match[1] || "",
  2130. version: match[2] || "0",
  2131. platform: platform_match[0] || ""
  2132. };
  2133. };
  2134. matched = jQuery.uaMatch(window.navigator.userAgent);
  2135. browser = {};
  2136. if (matched.browser) {
  2137. browser[matched.browser] = true;
  2138. browser.version = matched.version;
  2139. }
  2140. if (matched.platform) {
  2141. browser[matched.platform] = true
  2142. }
  2143. // Chrome is Webkit, but Webkit is also Safari.
  2144. if (browser.chrome) {
  2145. browser.webkit = true;
  2146. } else if (browser.webkit) {
  2147. browser.safari = true;
  2148. }
  2149. jQuery.browser = browser;
  2150. })(jQuery, window);