ApiClient.js 73 KB

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