ApiClient.js 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721
  1. if (!window.MediaBrowser) {
  2. window.MediaBrowser = {};
  3. }
  4. MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout, window) {
  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. * @param {String} applicationVersion
  12. */
  13. return function (serverProtocol, serverHostName, serverPortNumber, clientName, applicationVersion) {
  14. if (!serverProtocol) {
  15. throw new Error("Must supply a serverProtocol, e.g. http:");
  16. }
  17. if (!serverHostName) {
  18. throw new Error("Must supply serverHostName, e.g. 192.168.1.1 or myServerName");
  19. }
  20. var self = this;
  21. var deviceName = "Web Browser";
  22. var deviceId = MediaBrowser.SHA1(navigator.userAgent + (navigator.cpuClass || ""));
  23. var currentUserId;
  24. var webSocket;
  25. /**
  26. * Gets the server host name.
  27. */
  28. self.serverHostName = function () {
  29. return serverHostName;
  30. };
  31. /**
  32. * Gets or sets the current user id.
  33. */
  34. self.currentUserId = function (val) {
  35. if (val !== undefined) {
  36. currentUserId = val;
  37. } else {
  38. return currentUserId;
  39. }
  40. };
  41. deviceName = (function () {
  42. var name = "";
  43. if ($.browser.chrome) {
  44. name = "Chrome";
  45. }
  46. else if ($.browser.safari) {
  47. name = "Safari";
  48. }
  49. else if ($.browser.webkit) {
  50. name = "WebKit";
  51. }
  52. else if ($.browser.msie) {
  53. name = "Internet Explorer";
  54. }
  55. else if ($.browser.opera) {
  56. name = "Opera";
  57. }
  58. else if ($.browser.firefox || $.browser.mozilla) {
  59. name = "Firefox";
  60. }
  61. if (name) {
  62. if ($.browser.version) {
  63. name += " " + $.browser.version;
  64. }
  65. }
  66. else {
  67. name = "Web Browser";
  68. }
  69. if ($.browser.ipad) {
  70. name += " Ipad";
  71. }
  72. else if ($.browser.iphone) {
  73. name += " Iphone";
  74. }
  75. else if ($.browser.android) {
  76. name += " Android";
  77. }
  78. return name;
  79. }());
  80. self.deviceId = function () {
  81. return deviceId;
  82. };
  83. self.encodeName = function (name) {
  84. name = name.split('/').join('-');
  85. name = name.split('?').join('-');
  86. var val = $.param({ name: name });
  87. return val.substring(val.indexOf('=') + 1).replace("'", '%27');
  88. };
  89. /**
  90. * Wraps around jQuery ajax methods to add additional info to the request.
  91. */
  92. self.ajax = function (request) {
  93. if (!request) {
  94. throw new Error("Request cannot be null");
  95. }
  96. if (clientName) {
  97. var auth = 'MediaBrowser Client="' + clientName + '", Device="' + deviceName + '", DeviceId="' + deviceId + '", Version="' + applicationVersion + '"';
  98. if (currentUserId) {
  99. auth += ', UserId="' + currentUserId + '"';
  100. }
  101. request.headers = {
  102. Authorization: auth
  103. };
  104. }
  105. return $.ajax(request);
  106. };
  107. /**
  108. * Creates an api url based on a handler name and query string parameters
  109. * @param {String} name
  110. * @param {Object} params
  111. */
  112. self.getUrl = function (name, params) {
  113. if (!name) {
  114. throw new Error("Url name cannot be empty");
  115. }
  116. var url = serverProtocol + "//" + serverHostName;
  117. if (serverPortNumber) {
  118. url += ":" + serverPortNumber;
  119. }
  120. url += "/mediabrowser/" + name;
  121. if (params) {
  122. url += "?" + $.param(params);
  123. }
  124. return url;
  125. };
  126. self.openWebSocket = function (port) {
  127. var url = "ws://" + serverHostName + ":" + port + "/mediabrowser";
  128. webSocket = new WebSocket(url);
  129. webSocket.onmessage = function (msg) {
  130. msg = JSON.parse(msg.data);
  131. $(self).trigger("websocketmessage", [msg]);
  132. };
  133. webSocket.onopen = function () {
  134. setTimeout(function () {
  135. self.sendWebSocketMessage("Identity", clientName + "|" + deviceId + "|" + applicationVersion + "|" + deviceName);
  136. $(self).trigger("websocketopen");
  137. }, 500);
  138. };
  139. webSocket.onerror = function () {
  140. setTimeout(function () {
  141. $(self).trigger("websocketerror");
  142. }, 0);
  143. };
  144. webSocket.onclose = function () {
  145. setTimeout(function () {
  146. $(self).trigger("websocketclose");
  147. }, 0);
  148. };
  149. };
  150. self.closeWebSocket = function () {
  151. if (webSocket && webSocket.readyState === WebSocket.OPEN) {
  152. webSocket.close();
  153. }
  154. };
  155. self.sendWebSocketMessage = function (name, data) {
  156. var msg = { MessageType: name };
  157. if (data) {
  158. msg.Data = data;
  159. }
  160. msg = JSON.stringify(msg);
  161. webSocket.send(msg);
  162. };
  163. self.isWebSocketOpen = function () {
  164. return webSocket && webSocket.readyState === WebSocket.OPEN;
  165. };
  166. self.isWebSocketOpenOrConnecting = function () {
  167. return webSocket && (webSocket.readyState === WebSocket.OPEN || webSocket.readyState === WebSocket.CONNECTING);
  168. };
  169. /**
  170. * Gets an item from the server
  171. * Omit itemId to get the root folder.
  172. */
  173. self.getItem = function (userId, itemId) {
  174. if (!userId) {
  175. throw new Error("null userId");
  176. }
  177. var url = self.getUrl("Users/" + userId + "/Items/" + itemId);
  178. return self.ajax({
  179. type: "GET",
  180. url: url,
  181. dataType: "json"
  182. });
  183. };
  184. /**
  185. * Gets the root folder from the server
  186. */
  187. self.getRootFolder = function (userId) {
  188. if (!userId) {
  189. throw new Error("null userId");
  190. }
  191. var url = self.getUrl("Users/" + userId + "/Items/Root");
  192. return self.ajax({
  193. type: "GET",
  194. url: url,
  195. dataType: "json"
  196. });
  197. };
  198. self.getNotificationSummary = function (userId) {
  199. if (!userId) {
  200. throw new Error("null userId");
  201. }
  202. var url = self.getUrl("Notifications/" + userId + "/Summary");
  203. return self.ajax({
  204. type: "GET",
  205. url: url,
  206. dataType: "json"
  207. });
  208. };
  209. self.getNotifications = function (userId, options) {
  210. if (!userId) {
  211. throw new Error("null userId");
  212. }
  213. var url = self.getUrl("Notifications/" + userId, options || {});
  214. return self.ajax({
  215. type: "GET",
  216. url: url,
  217. dataType: "json"
  218. });
  219. };
  220. self.markNotificationsRead = function (userId, idList, isRead) {
  221. if (!userId) {
  222. throw new Error("null userId");
  223. }
  224. if (!idList || !idList.length) {
  225. throw new Error("null idList");
  226. }
  227. var suffix = isRead ? "Read" : "Unread";
  228. var params = {
  229. UserId: userId,
  230. Ids: idList.join(',')
  231. };
  232. var url = self.getUrl("Notifications/" + userId + "/" + suffix, params);
  233. return self.ajax({
  234. type: "POST",
  235. url: url
  236. });
  237. };
  238. /**
  239. * Gets the current server status
  240. */
  241. self.getSystemInfo = function () {
  242. var url = self.getUrl("System/Info");
  243. return self.ajax({
  244. type: "GET",
  245. url: url,
  246. dataType: "json"
  247. });
  248. };
  249. self.getInstantMixFromSong = function (itemId, options) {
  250. var url = self.getUrl("Songs/" + itemId + "/InstantMix", options);
  251. return self.ajax({
  252. type: "GET",
  253. url: url,
  254. dataType: "json"
  255. });
  256. };
  257. self.getInstantMixFromAlbum = function (itemId, options) {
  258. var url = self.getUrl("Albums/" + itemId + "/InstantMix", options);
  259. return self.ajax({
  260. type: "GET",
  261. url: url,
  262. dataType: "json"
  263. });
  264. };
  265. self.getInstantMixFromArtist = function (name, options) {
  266. var url = self.getUrl("Artists/" + self.encodeName(name) + "/InstantMix", options);
  267. return self.ajax({
  268. type: "GET",
  269. url: url,
  270. dataType: "json"
  271. });
  272. };
  273. self.getInstantMixFromMusicGenre = function (name, options) {
  274. var url = self.getUrl("MusicGenres/" + self.encodeName(name) + "/InstantMix", options);
  275. return self.ajax({
  276. type: "GET",
  277. url: url,
  278. dataType: "json"
  279. });
  280. };
  281. self.getSimilarMovies = function (itemId, options) {
  282. var url = self.getUrl("Movies/" + itemId + "/Similar", options);
  283. return self.ajax({
  284. type: "GET",
  285. url: url,
  286. dataType: "json"
  287. });
  288. };
  289. self.getSimilarTrailers = function (itemId, options) {
  290. var url = self.getUrl("Trailers/" + itemId + "/Similar", options);
  291. return self.ajax({
  292. type: "GET",
  293. url: url,
  294. dataType: "json"
  295. });
  296. };
  297. self.getSimilarShows = function (itemId, options) {
  298. var url = self.getUrl("Shows/" + itemId + "/Similar", options);
  299. return self.ajax({
  300. type: "GET",
  301. url: url,
  302. dataType: "json"
  303. });
  304. };
  305. self.getSimilarAlbums = function (itemId, options) {
  306. var url = self.getUrl("Albums/" + itemId + "/Similar", options);
  307. return self.ajax({
  308. type: "GET",
  309. url: url,
  310. dataType: "json"
  311. });
  312. };
  313. self.getSimilarGames = function (itemId, options) {
  314. var url = self.getUrl("Games/" + itemId + "/Similar", options);
  315. return self.ajax({
  316. type: "GET",
  317. url: url,
  318. dataType: "json"
  319. });
  320. };
  321. /**
  322. * Gets all cultures known to the server
  323. */
  324. self.getCultures = function () {
  325. var url = self.getUrl("Localization/cultures");
  326. return self.ajax({
  327. type: "GET",
  328. url: url,
  329. dataType: "json"
  330. });
  331. };
  332. /**
  333. * Gets all countries known to the server
  334. */
  335. self.getCountries = function () {
  336. var url = self.getUrl("Localization/countries");
  337. return self.ajax({
  338. type: "GET",
  339. url: url,
  340. dataType: "json"
  341. });
  342. };
  343. /**
  344. * Gets plugin security info
  345. */
  346. self.getPluginSecurityInfo = function () {
  347. var url = self.getUrl("Plugins/SecurityInfo");
  348. return self.ajax({
  349. type: "GET",
  350. url: url,
  351. dataType: "json"
  352. });
  353. };
  354. /**
  355. * Gets the directory contents of a path on the server
  356. */
  357. self.getDirectoryContents = function (path, options) {
  358. if (!path) {
  359. throw new Error("null path");
  360. }
  361. options = options || {};
  362. options.path = path;
  363. var url = self.getUrl("Environment/DirectoryContents", options);
  364. return self.ajax({
  365. type: "GET",
  366. url: url,
  367. dataType: "json"
  368. });
  369. };
  370. /**
  371. * Gets a list of physical drives from the server
  372. */
  373. self.getDrives = function () {
  374. var url = self.getUrl("Environment/Drives");
  375. return self.ajax({
  376. type: "GET",
  377. url: url,
  378. dataType: "json"
  379. });
  380. };
  381. /**
  382. * Gets a list of network devices from the server
  383. */
  384. self.getNetworkDevices = function () {
  385. var url = self.getUrl("Environment/NetworkDevices");
  386. return self.ajax({
  387. type: "GET",
  388. url: url,
  389. dataType: "json"
  390. });
  391. };
  392. /**
  393. * Cancels a package installation
  394. */
  395. self.cancelPackageInstallation = function (installationId) {
  396. if (!installationId) {
  397. throw new Error("null installationId");
  398. }
  399. var url = self.getUrl("Packages/Installing/" + installationId);
  400. return self.ajax({
  401. type: "DELETE",
  402. url: url
  403. });
  404. };
  405. /**
  406. * Refreshes metadata for an item
  407. */
  408. self.refreshItem = function (itemId, force, recursive) {
  409. if (!itemId) {
  410. throw new Error("null itemId");
  411. }
  412. var url = self.getUrl("Items/" + itemId + "/Refresh", {
  413. forced: force || false,
  414. recursive: recursive || false
  415. });
  416. return self.ajax({
  417. type: "POST",
  418. url: url
  419. });
  420. };
  421. self.refreshArtist = function (name, force) {
  422. if (!name) {
  423. throw new Error("null name");
  424. }
  425. var url = self.getUrl("Artists/" + self.encodeName(name) + "/Refresh", {
  426. forced: force || false
  427. });
  428. return self.ajax({
  429. type: "POST",
  430. url: url
  431. });
  432. };
  433. self.refreshGenre = function (name, force) {
  434. if (!name) {
  435. throw new Error("null name");
  436. }
  437. var url = self.getUrl("Genres/" + self.encodeName(name) + "/Refresh", {
  438. forced: force || false
  439. });
  440. return self.ajax({
  441. type: "POST",
  442. url: url
  443. });
  444. };
  445. self.refreshMusicGenre = function (name, force) {
  446. if (!name) {
  447. throw new Error("null name");
  448. }
  449. var url = self.getUrl("MusicGenres/" + self.encodeName(name) + "/Refresh", {
  450. forced: force || false
  451. });
  452. return self.ajax({
  453. type: "POST",
  454. url: url
  455. });
  456. };
  457. self.refreshGameGenre = function (name, force) {
  458. if (!name) {
  459. throw new Error("null name");
  460. }
  461. var url = self.getUrl("GameGenres/" + self.encodeName(name) + "/Refresh", {
  462. forced: force || false
  463. });
  464. return self.ajax({
  465. type: "POST",
  466. url: url
  467. });
  468. };
  469. self.refreshPerson = function (name, force) {
  470. if (!name) {
  471. throw new Error("null name");
  472. }
  473. var url = self.getUrl("Persons/" + self.encodeName(name) + "/Refresh", {
  474. forced: force || false
  475. });
  476. return self.ajax({
  477. type: "POST",
  478. url: url
  479. });
  480. };
  481. self.refreshStudio = function (name, force) {
  482. if (!name) {
  483. throw new Error("null name");
  484. }
  485. var url = self.getUrl("Studios/" + self.encodeName(name) + "/Refresh", {
  486. forced: force || false
  487. });
  488. return self.ajax({
  489. type: "POST",
  490. url: url
  491. });
  492. };
  493. /**
  494. * Installs or updates a new plugin
  495. */
  496. self.installPlugin = function (name, updateClass, version) {
  497. if (!name) {
  498. throw new Error("null name");
  499. }
  500. if (!updateClass) {
  501. throw new Error("null updateClass");
  502. }
  503. var options = {
  504. updateClass: updateClass
  505. };
  506. if (version) {
  507. options.version = version;
  508. }
  509. var url = self.getUrl("Packages/Installed/" + name, options);
  510. return self.ajax({
  511. type: "POST",
  512. url: url
  513. });
  514. };
  515. /**
  516. * Instructs the server to perform a restart.
  517. */
  518. self.restartServer = function () {
  519. var url = self.getUrl("System/Restart");
  520. return self.ajax({
  521. type: "POST",
  522. url: url
  523. });
  524. };
  525. /**
  526. * Instructs the server to perform a shutdown.
  527. */
  528. self.shutdownServer = function () {
  529. var url = self.getUrl("System/Shutdown");
  530. return self.ajax({
  531. type: "POST",
  532. url: url
  533. });
  534. };
  535. /**
  536. * Gets information about an installable package
  537. */
  538. self.getPackageInfo = function (name) {
  539. if (!name) {
  540. throw new Error("null name");
  541. }
  542. var url = self.getUrl("Packages/" + name);
  543. return self.ajax({
  544. type: "GET",
  545. url: url,
  546. dataType: "json"
  547. });
  548. };
  549. /**
  550. * Gets the latest available application update (if any)
  551. */
  552. self.getAvailableApplicationUpdate = function () {
  553. var url = self.getUrl("Packages/Updates", { PackageType: "System" });
  554. return self.ajax({
  555. type: "GET",
  556. url: url,
  557. dataType: "json"
  558. });
  559. };
  560. /**
  561. * Gets the latest available plugin updates (if any)
  562. */
  563. self.getAvailablePluginUpdates = function () {
  564. var url = self.getUrl("Packages/Updates", { PackageType: "UserInstalled" });
  565. return self.ajax({
  566. type: "GET",
  567. url: url,
  568. dataType: "json"
  569. });
  570. };
  571. /**
  572. * Gets the virtual folder for a view. Specify a userId to get a user view, or omit for the default view.
  573. */
  574. self.getVirtualFolders = function (userId) {
  575. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  576. url = self.getUrl(url);
  577. return self.ajax({
  578. type: "GET",
  579. url: url,
  580. dataType: "json"
  581. });
  582. };
  583. /**
  584. * Gets all the paths of the locations in the physical root.
  585. */
  586. self.getPhysicalPaths = function () {
  587. var url = self.getUrl("Library/PhysicalPaths");
  588. return self.ajax({
  589. type: "GET",
  590. url: url,
  591. dataType: "json"
  592. });
  593. };
  594. /**
  595. * Gets the current server configuration
  596. */
  597. self.getServerConfiguration = function () {
  598. var url = self.getUrl("System/Configuration");
  599. return self.ajax({
  600. type: "GET",
  601. url: url,
  602. dataType: "json"
  603. });
  604. };
  605. /**
  606. * Gets the server's scheduled tasks
  607. */
  608. self.getScheduledTasks = function () {
  609. var url = self.getUrl("ScheduledTasks");
  610. return self.ajax({
  611. type: "GET",
  612. url: url,
  613. dataType: "json"
  614. });
  615. };
  616. /**
  617. * Starts a scheduled task
  618. */
  619. self.startScheduledTask = function (id) {
  620. if (!id) {
  621. throw new Error("null id");
  622. }
  623. var url = self.getUrl("ScheduledTasks/Running/" + id);
  624. return self.ajax({
  625. type: "POST",
  626. url: url
  627. });
  628. };
  629. /**
  630. * Gets a scheduled task
  631. */
  632. self.getScheduledTask = function (id) {
  633. if (!id) {
  634. throw new Error("null id");
  635. }
  636. var url = self.getUrl("ScheduledTasks/" + id);
  637. return self.ajax({
  638. type: "GET",
  639. url: url,
  640. dataType: "json"
  641. });
  642. };
  643. self.getNextUpEpisodes = function (options) {
  644. var url = self.getUrl("Shows/NextUp", options);
  645. return self.ajax({
  646. type: "GET",
  647. url: url,
  648. dataType: "json"
  649. });
  650. };
  651. /**
  652. * Stops a scheduled task
  653. */
  654. self.stopScheduledTask = function (id) {
  655. if (!id) {
  656. throw new Error("null id");
  657. }
  658. var url = self.getUrl("ScheduledTasks/Running/" + id);
  659. return self.ajax({
  660. type: "DELETE",
  661. url: url
  662. });
  663. };
  664. /**
  665. * Gets the configuration of a plugin
  666. * @param {String} Id
  667. */
  668. self.getPluginConfiguration = function (id) {
  669. if (!id) {
  670. throw new Error("null Id");
  671. }
  672. var url = self.getUrl("Plugins/" + id + "/Configuration");
  673. return self.ajax({
  674. type: "GET",
  675. url: url,
  676. dataType: "json"
  677. });
  678. };
  679. /**
  680. * Gets a list of plugins that are available to be installed
  681. */
  682. self.getAvailablePlugins = function (options) {
  683. options = $.extend({}, options || {});
  684. options.PackageType = "UserInstalled";
  685. var url = self.getUrl("Packages", options);
  686. return self.ajax({
  687. type: "GET",
  688. url: url,
  689. dataType: "json"
  690. });
  691. };
  692. /**
  693. * Uninstalls a plugin
  694. * @param {String} Id
  695. */
  696. self.uninstallPlugin = function (id) {
  697. if (!id) {
  698. throw new Error("null Id");
  699. }
  700. var url = self.getUrl("Plugins/" + id);
  701. return self.ajax({
  702. type: "DELETE",
  703. url: url
  704. });
  705. };
  706. /**
  707. * Removes a virtual folder from either the default view or a user view
  708. * @param {String} name
  709. */
  710. self.removeVirtualFolder = function (name, userId, refreshLibrary) {
  711. if (!name) {
  712. throw new Error("null name");
  713. }
  714. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  715. url += "/" + name;
  716. url = self.getUrl(url, {
  717. refreshLibrary: refreshLibrary ? true : false
  718. });
  719. return self.ajax({
  720. type: "DELETE",
  721. url: url
  722. });
  723. };
  724. /**
  725. * Adds a virtual folder to either the default view or a user view
  726. * @param {String} name
  727. */
  728. self.addVirtualFolder = function (name, type, userId, refreshLibrary) {
  729. if (!name) {
  730. throw new Error("null name");
  731. }
  732. var options = {};
  733. if (type) {
  734. options.collectionType = type;
  735. }
  736. options.refreshLibrary = refreshLibrary ? true : false;
  737. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  738. url += "/" + name;
  739. url = self.getUrl(url, options);
  740. return self.ajax({
  741. type: "POST",
  742. url: url
  743. });
  744. };
  745. /**
  746. * Renames a virtual folder, within either the default view or a user view
  747. * @param {String} name
  748. */
  749. self.renameVirtualFolder = function (name, newName, userId, refreshLibrary) {
  750. if (!name) {
  751. throw new Error("null name");
  752. }
  753. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  754. url += "/" + name + "/Name";
  755. url = self.getUrl(url, {
  756. refreshLibrary: refreshLibrary ? true : false,
  757. newName: newName
  758. });
  759. return self.ajax({
  760. type: "POST",
  761. url: url
  762. });
  763. };
  764. /**
  765. * Adds an additional mediaPath to an existing virtual folder, within either the default view or a user view
  766. * @param {String} name
  767. */
  768. self.addMediaPath = function (virtualFolderName, mediaPath, userId, refreshLibrary) {
  769. if (!virtualFolderName) {
  770. throw new Error("null virtualFolderName");
  771. }
  772. if (!mediaPath) {
  773. throw new Error("null mediaPath");
  774. }
  775. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  776. url += "/" + virtualFolderName + "/Paths";
  777. url = self.getUrl(url, {
  778. refreshLibrary: refreshLibrary ? true : false,
  779. path: mediaPath
  780. });
  781. return self.ajax({
  782. type: "POST",
  783. url: url
  784. });
  785. };
  786. /**
  787. * Removes a media path from a virtual folder, within either the default view or a user view
  788. * @param {String} name
  789. */
  790. self.removeMediaPath = function (virtualFolderName, mediaPath, userId, refreshLibrary) {
  791. if (!virtualFolderName) {
  792. throw new Error("null virtualFolderName");
  793. }
  794. if (!mediaPath) {
  795. throw new Error("null mediaPath");
  796. }
  797. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  798. url += "/" + virtualFolderName + "/Paths";
  799. url = self.getUrl(url, {
  800. refreshLibrary: refreshLibrary ? true : false,
  801. path: mediaPath
  802. });
  803. return self.ajax({
  804. type: "DELETE",
  805. url: url
  806. });
  807. };
  808. /**
  809. * Deletes a user
  810. * @param {String} id
  811. */
  812. self.deleteUser = function (id) {
  813. if (!id) {
  814. throw new Error("null id");
  815. }
  816. var url = self.getUrl("Users/" + id);
  817. return self.ajax({
  818. type: "DELETE",
  819. url: url
  820. });
  821. };
  822. /**
  823. * Deletes a user image
  824. * @param {String} userId
  825. * @param {String} imageType The type of image to delete, based on the server-side ImageType enum.
  826. */
  827. self.deleteUserImage = function (userId, imageType, imageIndex) {
  828. if (!userId) {
  829. throw new Error("null userId");
  830. }
  831. if (!imageType) {
  832. throw new Error("null imageType");
  833. }
  834. var url = self.getUrl("Users/" + userId + "/Images/" + imageType);
  835. if (imageIndex != null) {
  836. url += "/" + imageIndex;
  837. }
  838. return self.ajax({
  839. type: "DELETE",
  840. url: url
  841. });
  842. };
  843. self.deleteItemImage = function (itemId, itemType, itemName, imageType, imageIndex) {
  844. if (!imageType) {
  845. throw new Error("null imageType");
  846. }
  847. if (!itemType) {
  848. throw new Error("null itemType");
  849. }
  850. var url;
  851. if (itemType == "Artist") {
  852. url = self.getUrl("Artists/" + self.encodeName(itemName) + "/Images");
  853. }
  854. else if (itemType == "Genre") {
  855. url = self.getUrl("Genres/" + self.encodeName(itemName) + "/Images");
  856. }
  857. else if (itemType == "GameGenre") {
  858. url = self.getUrl("GameGenres/" + self.encodeName(itemName) + "/Images");
  859. }
  860. else if (itemType == "MusicGenre") {
  861. url = self.getUrl("MusicGenres/" + self.encodeName(itemName) + "/Images");
  862. }
  863. else if (itemType == "Person") {
  864. url = self.getUrl("Persons/" + self.encodeName(itemName) + "/Images");
  865. }
  866. else if (itemType == "Studio") {
  867. url = self.getUrl("Studios/" + self.encodeName(itemName) + "/Images");
  868. }
  869. else {
  870. url = self.getUrl("Items/" + itemId + "/Images");
  871. }
  872. url += "/" + imageType;
  873. if (imageIndex != null) {
  874. url += "/" + imageIndex;
  875. }
  876. return self.ajax({
  877. type: "DELETE",
  878. url: url
  879. });
  880. };
  881. self.deleteItem = function (itemId) {
  882. if (!itemId) {
  883. throw new Error("null itemId");
  884. }
  885. var url = self.getUrl("Items/" + itemId);
  886. return self.ajax({
  887. type: "DELETE",
  888. url: url
  889. });
  890. };
  891. self.updateItemImageIndex = function (itemId, itemType, itemName, imageType, imageIndex, newIndex) {
  892. if (!imageType) {
  893. throw new Error("null imageType");
  894. }
  895. if (!itemType) {
  896. throw new Error("null itemType");
  897. }
  898. var url;
  899. var options = { newIndex: newIndex };
  900. if (itemType == "Artist") {
  901. url = self.getUrl("Artists/" + self.encodeName(itemName) + "/Images/" + imageType + "/" + imageIndex + "/Index", options);
  902. }
  903. else if (itemType == "Genre") {
  904. url = self.getUrl("Genres/" + self.encodeName(itemName) + "/Images/" + imageType + "/" + imageIndex + "/Index", options);
  905. }
  906. else if (itemType == "GameGenre") {
  907. url = self.getUrl("GameGenres/" + self.encodeName(itemName) + "/Images/" + imageType + "/" + imageIndex + "/Index", options);
  908. }
  909. else if (itemType == "MusicGenre") {
  910. url = self.getUrl("MusicGenres/" + self.encodeName(itemName) + "/Images/" + imageType + "/" + imageIndex + "/Index", options);
  911. }
  912. else if (itemType == "Person") {
  913. url = self.getUrl("Persons/" + self.encodeName(itemName) + "/Images/" + imageType + "/" + imageIndex + "/Index", options);
  914. }
  915. else if (itemType == "Studio") {
  916. url = self.getUrl("Studios/" + self.encodeName(itemName) + "/Images/" + imageType + "/" + imageIndex + "/Index", options);
  917. }
  918. else {
  919. url = self.getUrl("Items/" + itemId + "/Images/" + imageType + "/" + imageIndex + "/Index", options);
  920. }
  921. return self.ajax({
  922. type: "POST",
  923. url: url
  924. });
  925. };
  926. self.getItemImageInfos = function (itemId, itemType, itemName) {
  927. if (!itemType) {
  928. throw new Error("null itemType");
  929. }
  930. var url;
  931. if (itemType == "Artist") {
  932. url = self.getUrl("Artists/" + self.encodeName(itemName) + "/Images");
  933. }
  934. else if (itemType == "Genre") {
  935. url = self.getUrl("Genres/" + self.encodeName(itemName) + "/Images");
  936. }
  937. else if (itemType == "GameGenre") {
  938. url = self.getUrl("GameGenres/" + self.encodeName(itemName) + "/Images");
  939. }
  940. else if (itemType == "MusicGenre") {
  941. url = self.getUrl("MusicGenres/" + self.encodeName(itemName) + "/Images");
  942. }
  943. else if (itemType == "Person") {
  944. url = self.getUrl("Persons/" + self.encodeName(itemName) + "/Images");
  945. }
  946. else if (itemType == "Studio") {
  947. url = self.getUrl("Studios/" + self.encodeName(itemName) + "/Images");
  948. }
  949. else {
  950. url = self.getUrl("Items/" + itemId + "/Images");
  951. }
  952. return self.ajax({
  953. type: "GET",
  954. url: url,
  955. dataType: "json"
  956. });
  957. };
  958. self.getCriticReviews = function (itemId, options) {
  959. if (!itemId) {
  960. throw new Error("null itemId");
  961. }
  962. var url = self.getUrl("Items/" + itemId + "/CriticReviews", options);
  963. return self.ajax({
  964. type: "GET",
  965. url: url,
  966. dataType: "json"
  967. });
  968. };
  969. self.getSessions = function (options) {
  970. var url = self.getUrl("Sessions", options);
  971. return self.ajax({
  972. type: "GET",
  973. url: url,
  974. dataType: "json"
  975. });
  976. };
  977. /**
  978. * Uploads a user image
  979. * @param {String} userId
  980. * @param {String} imageType The type of image to delete, based on the server-side ImageType enum.
  981. * @param {Object} file The file from the input element
  982. */
  983. self.uploadUserImage = function (userId, imageType, file) {
  984. if (!userId) {
  985. throw new Error("null userId");
  986. }
  987. if (!imageType) {
  988. throw new Error("null imageType");
  989. }
  990. if (!file) {
  991. throw new Error("File must be an image.");
  992. }
  993. if (file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/jpeg") {
  994. throw new Error("File must be an image.");
  995. }
  996. var deferred = $.Deferred();
  997. var reader = new FileReader();
  998. reader.onerror = function () {
  999. deferred.reject();
  1000. };
  1001. reader.onabort = function () {
  1002. deferred.reject();
  1003. };
  1004. // Closure to capture the file information.
  1005. reader.onload = function (e) {
  1006. // Split by a comma to remove the url: prefix
  1007. var data = e.target.result.split(',')[1];
  1008. var url = self.getUrl("Users/" + userId + "/Images/" + imageType);
  1009. self.ajax({
  1010. type: "POST",
  1011. url: url,
  1012. data: data,
  1013. contentType: "image/" + file.name.substring(file.name.lastIndexOf('.') + 1)
  1014. }).done(function (result) {
  1015. deferred.resolveWith(null, [result]);
  1016. }).fail(function () {
  1017. deferred.reject();
  1018. });
  1019. };
  1020. // Read in the image file as a data URL.
  1021. reader.readAsDataURL(file);
  1022. return deferred.promise();
  1023. };
  1024. self.uploadItemImage = function (itemId, itemType, itemName, imageType, file) {
  1025. if (!itemId) {
  1026. throw new Error("null itemId");
  1027. }
  1028. if (!imageType) {
  1029. throw new Error("null imageType");
  1030. }
  1031. if (!file) {
  1032. throw new Error("File must be an image.");
  1033. }
  1034. if (file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/jpeg") {
  1035. throw new Error("File must be an image.");
  1036. }
  1037. var url;
  1038. if (itemType == "Artist") {
  1039. url = self.getUrl("Artists/" + self.encodeName(itemName) + "/Images");
  1040. }
  1041. else if (itemType == "Genre") {
  1042. url = self.getUrl("Genres/" + self.encodeName(itemName) + "/Images");
  1043. }
  1044. else if (itemType == "GameGenre") {
  1045. url = self.getUrl("GameGenres/" + self.encodeName(itemName) + "/Images");
  1046. }
  1047. else if (itemType == "MusicGenre") {
  1048. url = self.getUrl("MusicGenres/" + self.encodeName(itemName) + "/Images");
  1049. }
  1050. else if (itemType == "Person") {
  1051. url = self.getUrl("Persons/" + self.encodeName(itemName) + "/Images");
  1052. }
  1053. else if (itemType == "Studio") {
  1054. url = self.getUrl("Studios/" + self.encodeName(itemName) + "/Images");
  1055. }
  1056. else {
  1057. url = self.getUrl("Items/" + itemId + "/Images");
  1058. }
  1059. url += "/" + imageType;
  1060. var deferred = $.Deferred();
  1061. var reader = new FileReader();
  1062. reader.onerror = function () {
  1063. deferred.reject();
  1064. };
  1065. reader.onabort = function () {
  1066. deferred.reject();
  1067. };
  1068. // Closure to capture the file information.
  1069. reader.onload = function (e) {
  1070. // Split by a comma to remove the url: prefix
  1071. var data = e.target.result.split(',')[1];
  1072. self.ajax({
  1073. type: "POST",
  1074. url: url,
  1075. data: data,
  1076. contentType: "image/" + file.name.substring(file.name.lastIndexOf('.') + 1)
  1077. }).done(function (result) {
  1078. deferred.resolveWith(null, [result]);
  1079. }).fail(function () {
  1080. deferred.reject();
  1081. });
  1082. };
  1083. // Read in the image file as a data URL.
  1084. reader.readAsDataURL(file);
  1085. return deferred.promise();
  1086. };
  1087. /**
  1088. * Gets the list of installed plugins on the server
  1089. */
  1090. self.getInstalledPlugins = function () {
  1091. var url = self.getUrl("Plugins");
  1092. return self.ajax({
  1093. type: "GET",
  1094. url: url,
  1095. dataType: "json"
  1096. });
  1097. };
  1098. /**
  1099. * Gets a user by id
  1100. * @param {String} id
  1101. */
  1102. self.getUser = function (id) {
  1103. if (!id) {
  1104. throw new Error("Must supply a userId");
  1105. }
  1106. var url = self.getUrl("Users/" + id);
  1107. return self.ajax({
  1108. type: "GET",
  1109. url: url,
  1110. dataType: "json"
  1111. });
  1112. };
  1113. /**
  1114. * Gets a studio
  1115. */
  1116. self.getStudio = function (name, userId) {
  1117. if (!name) {
  1118. throw new Error("null name");
  1119. }
  1120. var options = {};
  1121. if (userId) {
  1122. options.userId = userId;
  1123. }
  1124. var url = self.getUrl("Studios/" + self.encodeName(name), options);
  1125. return self.ajax({
  1126. type: "GET",
  1127. url: url,
  1128. dataType: "json"
  1129. });
  1130. };
  1131. /**
  1132. * Gets a genre
  1133. */
  1134. self.getGenre = function (name, userId) {
  1135. if (!name) {
  1136. throw new Error("null name");
  1137. }
  1138. var options = {};
  1139. if (userId) {
  1140. options.userId = userId;
  1141. }
  1142. var url = self.getUrl("Genres/" + self.encodeName(name), options);
  1143. return self.ajax({
  1144. type: "GET",
  1145. url: url,
  1146. dataType: "json"
  1147. });
  1148. };
  1149. self.getMusicGenre = function (name, userId) {
  1150. if (!name) {
  1151. throw new Error("null name");
  1152. }
  1153. var options = {};
  1154. if (userId) {
  1155. options.userId = userId;
  1156. }
  1157. var url = self.getUrl("MusicGenres/" + self.encodeName(name), options);
  1158. return self.ajax({
  1159. type: "GET",
  1160. url: url,
  1161. dataType: "json"
  1162. });
  1163. };
  1164. self.getGameGenre = function (name, userId) {
  1165. if (!name) {
  1166. throw new Error("null name");
  1167. }
  1168. var options = {};
  1169. if (userId) {
  1170. options.userId = userId;
  1171. }
  1172. var url = self.getUrl("GameGenres/" + self.encodeName(name), options);
  1173. return self.ajax({
  1174. type: "GET",
  1175. url: url,
  1176. dataType: "json"
  1177. });
  1178. };
  1179. /**
  1180. * Gets an artist
  1181. */
  1182. self.getArtist = function (name, userId) {
  1183. if (!name) {
  1184. throw new Error("null name");
  1185. }
  1186. var options = {};
  1187. if (userId) {
  1188. options.userId = userId;
  1189. }
  1190. var url = self.getUrl("Artists/" + self.encodeName(name), options);
  1191. return self.ajax({
  1192. type: "GET",
  1193. url: url,
  1194. dataType: "json"
  1195. });
  1196. };
  1197. /**
  1198. * Gets a year
  1199. */
  1200. self.getYear = function (yea, userId) {
  1201. if (!name) {
  1202. throw new Error("null name");
  1203. }
  1204. var options = {};
  1205. if (userId) {
  1206. options.userId = userId;
  1207. }
  1208. var url = self.getUrl("Years/" + self.encodeName(name), options);
  1209. return self.ajax({
  1210. type: "GET",
  1211. url: url,
  1212. dataType: "json"
  1213. });
  1214. };
  1215. /**
  1216. * Gets a Person
  1217. */
  1218. self.getPerson = function (name, userId) {
  1219. if (!name) {
  1220. throw new Error("null name");
  1221. }
  1222. var options = {};
  1223. if (userId) {
  1224. options.userId = userId;
  1225. }
  1226. var url = self.getUrl("Persons/" + self.encodeName(name), options);
  1227. return self.ajax({
  1228. type: "GET",
  1229. url: url,
  1230. dataType: "json"
  1231. });
  1232. };
  1233. self.getPublicUsers = function () {
  1234. var url = self.getUrl("users/public");
  1235. return self.ajax({
  1236. type: "GET",
  1237. url: url,
  1238. dataType: "json"
  1239. });
  1240. };
  1241. /**
  1242. * Gets all users from the server
  1243. */
  1244. self.getUsers = function (options) {
  1245. var url = self.getUrl("users", options || {});
  1246. return self.ajax({
  1247. type: "GET",
  1248. url: url,
  1249. dataType: "json"
  1250. });
  1251. };
  1252. /**
  1253. * Gets all available parental ratings from the server
  1254. */
  1255. self.getParentalRatings = function () {
  1256. var url = self.getUrl("Localization/ParentalRatings");
  1257. return self.ajax({
  1258. type: "GET",
  1259. url: url,
  1260. dataType: "json"
  1261. });
  1262. };
  1263. /**
  1264. * Gets a list of all available conrete BaseItem types from the server
  1265. */
  1266. self.getItemTypes = function (options) {
  1267. var url = self.getUrl("Library/ItemTypes", options);
  1268. return self.ajax({
  1269. type: "GET",
  1270. url: url,
  1271. dataType: "json"
  1272. });
  1273. };
  1274. /**
  1275. * Constructs a url for a user image
  1276. * @param {String} userId
  1277. * @param {Object} options
  1278. * Options supports the following properties:
  1279. * width - download the image at a fixed width
  1280. * height - download the image at a fixed height
  1281. * maxWidth - download the image at a maxWidth
  1282. * maxHeight - download the image at a maxHeight
  1283. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1284. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1285. */
  1286. self.getUserImageUrl = function (userId, options) {
  1287. if (!userId) {
  1288. throw new Error("null userId");
  1289. }
  1290. options = options || {
  1291. };
  1292. var url = "Users/" + userId + "/Images/" + options.type;
  1293. if (options.index != null) {
  1294. url += "/" + options.index;
  1295. }
  1296. // Don't put these on the query string
  1297. delete options.type;
  1298. delete options.index;
  1299. return self.getUrl(url, options);
  1300. };
  1301. /**
  1302. * Constructs a url for a person image
  1303. * @param {String} name
  1304. * @param {Object} options
  1305. * Options supports the following properties:
  1306. * width - download the image at a fixed width
  1307. * height - download the image at a fixed height
  1308. * maxWidth - download the image at a maxWidth
  1309. * maxHeight - download the image at a maxHeight
  1310. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1311. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1312. */
  1313. self.getPersonImageUrl = function (name, options) {
  1314. if (!name) {
  1315. throw new Error("null name");
  1316. }
  1317. options = options || {
  1318. };
  1319. var url = "Persons/" + self.encodeName(name) + "/Images/" + options.type;
  1320. if (options.index != null) {
  1321. url += "/" + options.index;
  1322. }
  1323. // Don't put these on the query string
  1324. delete options.type;
  1325. delete options.index;
  1326. return self.getUrl(url, options);
  1327. };
  1328. /**
  1329. * Constructs a url for a year image
  1330. * @param {String} year
  1331. * @param {Object} options
  1332. * Options supports the following properties:
  1333. * width - download the image at a fixed width
  1334. * height - download the image at a fixed height
  1335. * maxWidth - download the image at a maxWidth
  1336. * maxHeight - download the image at a maxHeight
  1337. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1338. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1339. */
  1340. self.getYearImageUrl = function (year, options) {
  1341. if (!year) {
  1342. throw new Error("null year");
  1343. }
  1344. options = options || {
  1345. };
  1346. var url = "Years/" + year + "/Images/" + options.type;
  1347. if (options.index != null) {
  1348. url += "/" + options.index;
  1349. }
  1350. // Don't put these on the query string
  1351. delete options.type;
  1352. delete options.index;
  1353. return self.getUrl(url, options);
  1354. };
  1355. /**
  1356. * Constructs a url for a genre image
  1357. * @param {String} name
  1358. * @param {Object} options
  1359. * Options supports the following properties:
  1360. * width - download the image at a fixed width
  1361. * height - download the image at a fixed height
  1362. * maxWidth - download the image at a maxWidth
  1363. * maxHeight - download the image at a maxHeight
  1364. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1365. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1366. */
  1367. self.getGenreImageUrl = function (name, options) {
  1368. if (!name) {
  1369. throw new Error("null name");
  1370. }
  1371. options = options || {
  1372. };
  1373. var url = "Genres/" + self.encodeName(name) + "/Images/" + options.type;
  1374. if (options.index != null) {
  1375. url += "/" + options.index;
  1376. }
  1377. // Don't put these on the query string
  1378. delete options.type;
  1379. delete options.index;
  1380. return self.getUrl(url, options);
  1381. };
  1382. /**
  1383. * Constructs a url for a genre image
  1384. * @param {String} name
  1385. * @param {Object} options
  1386. * Options supports the following properties:
  1387. * width - download the image at a fixed width
  1388. * height - download the image at a fixed height
  1389. * maxWidth - download the image at a maxWidth
  1390. * maxHeight - download the image at a maxHeight
  1391. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1392. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1393. */
  1394. self.getMusicGenreImageUrl = function (name, options) {
  1395. if (!name) {
  1396. throw new Error("null name");
  1397. }
  1398. options = options || {
  1399. };
  1400. var url = "MusicGenres/" + self.encodeName(name) + "/Images/" + options.type;
  1401. if (options.index != null) {
  1402. url += "/" + options.index;
  1403. }
  1404. // Don't put these on the query string
  1405. delete options.type;
  1406. delete options.index;
  1407. return self.getUrl(url, options);
  1408. };
  1409. /**
  1410. * Constructs a url for a genre image
  1411. * @param {String} name
  1412. * @param {Object} options
  1413. * Options supports the following properties:
  1414. * width - download the image at a fixed width
  1415. * height - download the image at a fixed height
  1416. * maxWidth - download the image at a maxWidth
  1417. * maxHeight - download the image at a maxHeight
  1418. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1419. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1420. */
  1421. self.getGameGenreImageUrl = function (name, options) {
  1422. if (!name) {
  1423. throw new Error("null name");
  1424. }
  1425. options = options || {
  1426. };
  1427. var url = "GameGenres/" + self.encodeName(name) + "/Images/" + options.type;
  1428. if (options.index != null) {
  1429. url += "/" + options.index;
  1430. }
  1431. // Don't put these on the query string
  1432. delete options.type;
  1433. delete options.index;
  1434. return self.getUrl(url, options);
  1435. };
  1436. /**
  1437. * Constructs a url for a artist image
  1438. * @param {String} name
  1439. * @param {Object} options
  1440. * Options supports the following properties:
  1441. * width - download the image at a fixed width
  1442. * height - download the image at a fixed height
  1443. * maxWidth - download the image at a maxWidth
  1444. * maxHeight - download the image at a maxHeight
  1445. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1446. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1447. */
  1448. self.getArtistImageUrl = function (name, options) {
  1449. if (!name) {
  1450. throw new Error("null name");
  1451. }
  1452. options = options || {
  1453. };
  1454. var url = "Artists/" + self.encodeName(name) + "/Images/" + options.type;
  1455. if (options.index != null) {
  1456. url += "/" + options.index;
  1457. }
  1458. // Don't put these on the query string
  1459. delete options.type;
  1460. delete options.index;
  1461. return self.getUrl(url, options);
  1462. };
  1463. /**
  1464. * Constructs a url for a studio image
  1465. * @param {String} name
  1466. * @param {Object} options
  1467. * Options supports the following properties:
  1468. * width - download the image at a fixed width
  1469. * height - download the image at a fixed height
  1470. * maxWidth - download the image at a maxWidth
  1471. * maxHeight - download the image at a maxHeight
  1472. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1473. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1474. */
  1475. self.getStudioImageUrl = function (name, options) {
  1476. if (!name) {
  1477. throw new Error("null name");
  1478. }
  1479. options = options || {
  1480. };
  1481. var url = "Studios/" + self.encodeName(name) + "/Images/" + options.type;
  1482. if (options.index != null) {
  1483. url += "/" + options.index;
  1484. }
  1485. // Don't put these on the query string
  1486. delete options.type;
  1487. delete options.index;
  1488. return self.getUrl(url, options);
  1489. };
  1490. /**
  1491. * Constructs a url for an item image
  1492. * @param {String} itemId
  1493. * @param {Object} options
  1494. * Options supports the following properties:
  1495. * type - Primary, logo, backdrop, etc. See the server-side enum ImageType
  1496. * index - When downloading a backdrop, use this to specify which one (omitting is equivalent to zero)
  1497. * width - download the image at a fixed width
  1498. * height - download the image at a fixed height
  1499. * maxWidth - download the image at a maxWidth
  1500. * maxHeight - download the image at a maxHeight
  1501. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1502. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1503. */
  1504. self.getImageUrl = function (itemId, options) {
  1505. if (!itemId) {
  1506. throw new Error("itemId cannot be empty");
  1507. }
  1508. options = options || {
  1509. };
  1510. var url = "Items/" + itemId + "/Images/" + options.type;
  1511. if (options.index != null) {
  1512. url += "/" + options.index;
  1513. }
  1514. // Don't put these on the query string
  1515. delete options.type;
  1516. delete options.index;
  1517. return self.getUrl(url, options);
  1518. };
  1519. /**
  1520. * Constructs a url for an item logo image
  1521. * If the item doesn't have a logo, it will inherit a logo from a parent
  1522. * @param {Object} item A BaseItem
  1523. * @param {Object} options
  1524. * Options supports the following properties:
  1525. * width - download the image at a fixed width
  1526. * height - download the image at a fixed height
  1527. * maxWidth - download the image at a maxWidth
  1528. * maxHeight - download the image at a maxHeight
  1529. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1530. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1531. */
  1532. self.getLogoImageUrl = function (item, options) {
  1533. if (!item) {
  1534. throw new Error("null item");
  1535. }
  1536. options = options || {
  1537. };
  1538. options.imageType = "logo";
  1539. var logoItemId = item.ImageTags && item.ImageTags.Logo ? item.Id : item.ParentLogoItemId;
  1540. return logoItemId ? self.getImageUrl(logoItemId, options) : null;
  1541. };
  1542. self.getThumbImageUrl = function (item, options) {
  1543. if (!item) {
  1544. throw new Error("null item");
  1545. }
  1546. options = options || {
  1547. };
  1548. options.imageType = "thumb";
  1549. var itemId = item.ImageTags && item.ImageTags.Thumb ? item.Id : item.ParentThumbItemId;
  1550. return itemId ? self.getImageUrl(itemId, options) : null;
  1551. };
  1552. /**
  1553. * Constructs an array of backdrop image url's for an item
  1554. * If the item doesn't have any backdrops, it will inherit them from a parent
  1555. * @param {Object} item A BaseItem
  1556. * @param {Object} options
  1557. * Options supports the following properties:
  1558. * width - download the image at a fixed width
  1559. * height - download the image at a fixed height
  1560. * maxWidth - download the image at a maxWidth
  1561. * maxHeight - download the image at a maxHeight
  1562. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1563. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1564. */
  1565. self.getBackdropImageUrl = function (item, options) {
  1566. if (!item) {
  1567. throw new Error("null item");
  1568. }
  1569. options = options || {
  1570. };
  1571. options.imageType = "backdrop";
  1572. var backdropItemId;
  1573. var backdropCount;
  1574. if (!item.BackdropCount) {
  1575. backdropItemId = item.ParentBackdropItemId;
  1576. backdropCount = item.ParentBackdropCount || 0;
  1577. } else {
  1578. backdropItemId = item.Id;
  1579. backdropCount = item.BackdropCount;
  1580. }
  1581. if (!backdropItemId) {
  1582. return [];
  1583. }
  1584. var files = [];
  1585. for (var i = 0; i < backdropCount; i++) {
  1586. options.imageIndex = i;
  1587. files[i] = self.getImageUrl(backdropItemId, options);
  1588. }
  1589. return files;
  1590. };
  1591. /**
  1592. * Authenticates a user
  1593. * @param {String} name
  1594. * @param {String} password
  1595. */
  1596. self.authenticateUserByName = function (name, password) {
  1597. if (!name) {
  1598. throw new Error("null name");
  1599. }
  1600. var url = self.getUrl("Users/authenticatebyname");
  1601. var postData = {
  1602. password: MediaBrowser.SHA1(password || ""),
  1603. Username: name
  1604. };
  1605. return self.ajax({
  1606. type: "POST",
  1607. url: url,
  1608. data: JSON.stringify(postData),
  1609. dataType: "json",
  1610. contentType: "application/json"
  1611. });
  1612. };
  1613. /**
  1614. * Authenticates a user
  1615. * @param {String} userId
  1616. * @param {String} password
  1617. */
  1618. self.authenticateUser = function (userId, password) {
  1619. if (!userId) {
  1620. throw new Error("null userId");
  1621. }
  1622. var url = self.getUrl("Users/" + userId + "/authenticate");
  1623. var postData = {
  1624. password: MediaBrowser.SHA1(password || "")
  1625. };
  1626. return self.ajax({
  1627. type: "POST",
  1628. url: url,
  1629. data: JSON.stringify(postData),
  1630. dataType: "json",
  1631. contentType: "application/json"
  1632. });
  1633. };
  1634. /**
  1635. * Updates a user's password
  1636. * @param {String} userId
  1637. * @param {String} currentPassword
  1638. * @param {String} newPassword
  1639. */
  1640. self.updateUserPassword = function (userId, currentPassword, newPassword) {
  1641. if (!userId) {
  1642. throw new Error("null userId");
  1643. }
  1644. var url = self.getUrl("Users/" + userId + "/Password");
  1645. var postData = {
  1646. };
  1647. postData.currentPassword = MediaBrowser.SHA1(currentPassword);
  1648. if (newPassword) {
  1649. postData.newPassword = newPassword;
  1650. }
  1651. return self.ajax({
  1652. type: "POST",
  1653. url: url,
  1654. data: postData
  1655. });
  1656. };
  1657. /**
  1658. * Resets a user's password
  1659. * @param {String} userId
  1660. */
  1661. self.resetUserPassword = function (userId) {
  1662. if (!userId) {
  1663. throw new Error("null userId");
  1664. }
  1665. var url = self.getUrl("Users/" + userId + "/Password");
  1666. var postData = {
  1667. };
  1668. postData.resetPassword = true;
  1669. return self.ajax({
  1670. type: "POST",
  1671. url: url,
  1672. data: postData
  1673. });
  1674. };
  1675. /**
  1676. * Updates the server's configuration
  1677. * @param {Object} configuration
  1678. */
  1679. self.updateServerConfiguration = function (configuration) {
  1680. if (!configuration) {
  1681. throw new Error("null configuration");
  1682. }
  1683. var url = self.getUrl("System/Configuration");
  1684. return self.ajax({
  1685. type: "POST",
  1686. url: url,
  1687. data: JSON.stringify(configuration),
  1688. contentType: "application/json"
  1689. });
  1690. };
  1691. self.updateItem = function (item) {
  1692. if (!item) {
  1693. throw new Error("null item");
  1694. }
  1695. var url = self.getUrl("Items/" + item.Id);
  1696. return self.ajax({
  1697. type: "POST",
  1698. url: url,
  1699. data: JSON.stringify(item),
  1700. contentType: "application/json"
  1701. });
  1702. };
  1703. self.updateArtist = function (item) {
  1704. if (!item) {
  1705. throw new Error("null item");
  1706. }
  1707. var url = self.getUrl("Artists/" + self.encodeName(item.Name));
  1708. return self.ajax({
  1709. type: "POST",
  1710. url: url,
  1711. data: JSON.stringify(item),
  1712. contentType: "application/json"
  1713. });
  1714. };
  1715. self.updatePerson = function (item) {
  1716. if (!item) {
  1717. throw new Error("null item");
  1718. }
  1719. var url = self.getUrl("Persons/" + self.encodeName(item.Name));
  1720. return self.ajax({
  1721. type: "POST",
  1722. url: url,
  1723. data: JSON.stringify(item),
  1724. contentType: "application/json"
  1725. });
  1726. };
  1727. self.updateStudio = function (item) {
  1728. if (!item) {
  1729. throw new Error("null item");
  1730. }
  1731. var url = self.getUrl("Studios/" + self.encodeName(item.Name));
  1732. return self.ajax({
  1733. type: "POST",
  1734. url: url,
  1735. data: JSON.stringify(item),
  1736. contentType: "application/json"
  1737. });
  1738. };
  1739. self.updateGenre = function (item) {
  1740. if (!item) {
  1741. throw new Error("null item");
  1742. }
  1743. var url = self.getUrl("Genres/" + self.encodeName(item.Name));
  1744. return self.ajax({
  1745. type: "POST",
  1746. url: url,
  1747. data: JSON.stringify(item),
  1748. contentType: "application/json"
  1749. });
  1750. };
  1751. self.updateMusicGenre = function (item) {
  1752. if (!item) {
  1753. throw new Error("null item");
  1754. }
  1755. var url = self.getUrl("MusicGenres/" + self.encodeName(item.Name));
  1756. return self.ajax({
  1757. type: "POST",
  1758. url: url,
  1759. data: JSON.stringify(item),
  1760. contentType: "application/json"
  1761. });
  1762. };
  1763. self.updateGameGenre = function (item) {
  1764. if (!item) {
  1765. throw new Error("null item");
  1766. }
  1767. var url = self.getUrl("GameGenres/" + self.encodeName(item.Name));
  1768. return self.ajax({
  1769. type: "POST",
  1770. url: url,
  1771. data: JSON.stringify(item),
  1772. contentType: "application/json"
  1773. });
  1774. };
  1775. /**
  1776. * Updates plugin security info
  1777. */
  1778. self.updatePluginSecurityInfo = function (info) {
  1779. var url = self.getUrl("Plugins/SecurityInfo");
  1780. return self.ajax({
  1781. type: "POST",
  1782. url: url,
  1783. data: JSON.stringify(info),
  1784. contentType: "application/json"
  1785. });
  1786. };
  1787. /**
  1788. * Creates a user
  1789. * @param {Object} user
  1790. */
  1791. self.createUser = function (user) {
  1792. if (!user) {
  1793. throw new Error("null user");
  1794. }
  1795. var url = self.getUrl("Users");
  1796. return self.ajax({
  1797. type: "POST",
  1798. url: url,
  1799. data: JSON.stringify(user),
  1800. dataType: "json",
  1801. contentType: "application/json"
  1802. });
  1803. };
  1804. /**
  1805. * Updates a user
  1806. * @param {Object} user
  1807. */
  1808. self.updateUser = function (user) {
  1809. if (!user) {
  1810. throw new Error("null user");
  1811. }
  1812. var url = self.getUrl("Users/" + user.Id);
  1813. return self.ajax({
  1814. type: "POST",
  1815. url: url,
  1816. data: JSON.stringify(user),
  1817. contentType: "application/json"
  1818. });
  1819. };
  1820. /**
  1821. * Updates the Triggers for a ScheduledTask
  1822. * @param {String} id
  1823. * @param {Object} triggers
  1824. */
  1825. self.updateScheduledTaskTriggers = function (id, triggers) {
  1826. if (!id) {
  1827. throw new Error("null id");
  1828. }
  1829. if (!triggers) {
  1830. throw new Error("null triggers");
  1831. }
  1832. var url = self.getUrl("ScheduledTasks/" + id + "/Triggers");
  1833. return self.ajax({
  1834. type: "POST",
  1835. url: url,
  1836. data: JSON.stringify(triggers),
  1837. contentType: "application/json"
  1838. });
  1839. };
  1840. /**
  1841. * Updates a plugin's configuration
  1842. * @param {String} Id
  1843. * @param {Object} configuration
  1844. */
  1845. self.updatePluginConfiguration = function (id, configuration) {
  1846. if (!id) {
  1847. throw new Error("null Id");
  1848. }
  1849. if (!configuration) {
  1850. throw new Error("null configuration");
  1851. }
  1852. var url = self.getUrl("Plugins/" + id + "/Configuration");
  1853. return self.ajax({
  1854. type: "POST",
  1855. url: url,
  1856. data: JSON.stringify(configuration),
  1857. contentType: "application/json"
  1858. });
  1859. };
  1860. self.getAncestorItems = function (itemId, userId) {
  1861. if (!itemId) {
  1862. throw new Error("null itemId");
  1863. }
  1864. var options = {};
  1865. if (userId) {
  1866. options.userId = userId;
  1867. }
  1868. var url = self.getUrl("Items/" + itemId + "/Ancestors", options);
  1869. return self.ajax({
  1870. type: "GET",
  1871. url: url,
  1872. dataType: "json"
  1873. });
  1874. };
  1875. /**
  1876. * Gets items based on a query, typically for children of a folder
  1877. * @param {String} userId
  1878. * @param {Object} options
  1879. * Options accepts the following properties:
  1880. * itemId - Localize the search to a specific folder (root if omitted)
  1881. * startIndex - Use for paging
  1882. * limit - Use to limit results to a certain number of items
  1883. * filter - Specify one or more ItemFilters, comma delimeted (see server-side enum)
  1884. * sortBy - Specify an ItemSortBy (comma-delimeted list see server-side enum)
  1885. * sortOrder - ascending/descending
  1886. * fields - additional fields to include aside from basic info. This is a comma delimited list. See server-side enum ItemFields.
  1887. * index - the name of the dynamic, localized index function
  1888. * dynamicSortBy - the name of the dynamic localized sort function
  1889. * recursive - Whether or not the query should be recursive
  1890. * searchTerm - search term to use as a filter
  1891. */
  1892. self.getItems = function (userId, options) {
  1893. if (!userId) {
  1894. throw new Error("null userId");
  1895. }
  1896. var url = self.getUrl("Users/" + userId + "/Items", options);
  1897. return self.ajax({
  1898. type: "GET",
  1899. url: url,
  1900. dataType: "json"
  1901. });
  1902. };
  1903. /**
  1904. Gets artists from an item
  1905. */
  1906. self.getArtists = function (userId, options) {
  1907. if (!userId) {
  1908. throw new Error("null userId");
  1909. }
  1910. options = options || {};
  1911. options.userId = userId;
  1912. var url = self.getUrl("Artists", options);
  1913. return self.ajax({
  1914. type: "GET",
  1915. url: url,
  1916. dataType: "json"
  1917. });
  1918. };
  1919. /**
  1920. Gets genres from an item
  1921. */
  1922. self.getGenres = function (userId, options) {
  1923. if (!userId) {
  1924. throw new Error("null userId");
  1925. }
  1926. options = options || {};
  1927. options.userId = userId;
  1928. var url = self.getUrl("Genres", options);
  1929. return self.ajax({
  1930. type: "GET",
  1931. url: url,
  1932. dataType: "json"
  1933. });
  1934. };
  1935. self.getMusicGenres = function (userId, options) {
  1936. if (!userId) {
  1937. throw new Error("null userId");
  1938. }
  1939. options = options || {};
  1940. options.userId = userId;
  1941. var url = self.getUrl("MusicGenres", options);
  1942. return self.ajax({
  1943. type: "GET",
  1944. url: url,
  1945. dataType: "json"
  1946. });
  1947. };
  1948. self.getGameGenres = function (userId, options) {
  1949. if (!userId) {
  1950. throw new Error("null userId");
  1951. }
  1952. options = options || {};
  1953. options.userId = userId;
  1954. var url = self.getUrl("GameGenres", options);
  1955. return self.ajax({
  1956. type: "GET",
  1957. url: url,
  1958. dataType: "json"
  1959. });
  1960. };
  1961. /**
  1962. Gets people from an item
  1963. */
  1964. self.getPeople = function (userId, options) {
  1965. if (!userId) {
  1966. throw new Error("null userId");
  1967. }
  1968. options = options || {};
  1969. options.userId = userId;
  1970. var url = self.getUrl("Persons", options);
  1971. return self.ajax({
  1972. type: "GET",
  1973. url: url,
  1974. dataType: "json"
  1975. });
  1976. };
  1977. /**
  1978. Gets studios from an item
  1979. */
  1980. self.getStudios = function (userId, options) {
  1981. if (!userId) {
  1982. throw new Error("null userId");
  1983. }
  1984. options = options || {};
  1985. options.userId = userId;
  1986. var url = self.getUrl("Studios", options);
  1987. return self.ajax({
  1988. type: "GET",
  1989. url: url,
  1990. dataType: "json"
  1991. });
  1992. };
  1993. /**
  1994. * Gets local trailers for an item
  1995. */
  1996. self.getLocalTrailers = function (userId, itemId) {
  1997. if (!userId) {
  1998. throw new Error("null userId");
  1999. }
  2000. if (!itemId) {
  2001. throw new Error("null itemId");
  2002. }
  2003. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/LocalTrailers");
  2004. return self.ajax({
  2005. type: "GET",
  2006. url: url,
  2007. dataType: "json"
  2008. });
  2009. };
  2010. self.getAdditionalVideoParts = function (userId, itemId) {
  2011. if (!itemId) {
  2012. throw new Error("null itemId");
  2013. }
  2014. var options = {};
  2015. if (userId) {
  2016. options.userId = userId;
  2017. }
  2018. var url = self.getUrl("Videos/" + itemId + "/AdditionalParts", options);
  2019. return self.ajax({
  2020. type: "GET",
  2021. url: url,
  2022. dataType: "json"
  2023. });
  2024. };
  2025. /**
  2026. * Gets theme songs for an item
  2027. */
  2028. self.getThemeSongs = function (userId, itemId) {
  2029. if (!itemId) {
  2030. throw new Error("null itemId");
  2031. }
  2032. var options = {};
  2033. if (userId) {
  2034. options.userId = userId;
  2035. }
  2036. var url = self.getUrl("Items/" + itemId + "/ThemeSongs", options);
  2037. return self.ajax({
  2038. type: "GET",
  2039. url: url,
  2040. dataType: "json"
  2041. });
  2042. };
  2043. self.getThemeVideos = function (userId, itemId) {
  2044. if (!itemId) {
  2045. throw new Error("null itemId");
  2046. }
  2047. var options = {};
  2048. if (userId) {
  2049. options.userId = userId;
  2050. }
  2051. var url = self.getUrl("Items/" + itemId + "/ThemeVideos", options);
  2052. return self.ajax({
  2053. type: "GET",
  2054. url: url,
  2055. dataType: "json"
  2056. });
  2057. };
  2058. self.getSearchHints = function (options) {
  2059. var url = self.getUrl("Search/Hints", options);
  2060. return self.ajax({
  2061. type: "GET",
  2062. url: url,
  2063. dataType: "json"
  2064. });
  2065. };
  2066. /**
  2067. * Gets special features for an item
  2068. */
  2069. self.getSpecialFeatures = function (userId, itemId) {
  2070. if (!userId) {
  2071. throw new Error("null userId");
  2072. }
  2073. if (!itemId) {
  2074. throw new Error("null itemId");
  2075. }
  2076. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/SpecialFeatures");
  2077. return self.ajax({
  2078. type: "GET",
  2079. url: url,
  2080. dataType: "json"
  2081. });
  2082. };
  2083. self.getDateParamValue = function (date) {
  2084. function formatDigit(i) {
  2085. return i < 10 ? "0" + i : i;
  2086. }
  2087. var d = date;
  2088. return "" + d.getFullYear() + formatDigit(d.getMonth() + 1) + formatDigit(d.getDate()) + formatDigit(d.getHours()) + formatDigit(d.getMinutes()) + formatDigit(d.getSeconds());
  2089. };
  2090. self.markPlayed = function (userId, itemId, date) {
  2091. if (!userId) {
  2092. throw new Error("null userId");
  2093. }
  2094. if (!itemId) {
  2095. throw new Error("null itemId");
  2096. }
  2097. var options = {};
  2098. if (date) {
  2099. options.DatePlayed = self.getDateParamValue(date);
  2100. }
  2101. var url = self.getUrl("Users/" + userId + "/PlayedItems/" + itemId, options);
  2102. return self.ajax({
  2103. type: "POST",
  2104. url: url,
  2105. dataType: "json"
  2106. });
  2107. };
  2108. self.markUnplayed = function (userId, itemId) {
  2109. if (!userId) {
  2110. throw new Error("null userId");
  2111. }
  2112. if (!itemId) {
  2113. throw new Error("null itemId");
  2114. }
  2115. var url = self.getUrl("Users/" + userId + "/PlayedItems/" + itemId);
  2116. return self.ajax({
  2117. type: "DELETE",
  2118. url: url,
  2119. dataType: "json"
  2120. });
  2121. };
  2122. /**
  2123. * Updates a user's favorite status for an item.
  2124. * @param {String} userId
  2125. * @param {String} itemId
  2126. * @param {Boolean} isFavorite
  2127. */
  2128. self.updateFavoriteStatus = function (userId, itemId, isFavorite) {
  2129. if (!userId) {
  2130. throw new Error("null userId");
  2131. }
  2132. if (!itemId) {
  2133. throw new Error("null itemId");
  2134. }
  2135. var url = self.getUrl("Users/" + userId + "/FavoriteItems/" + itemId);
  2136. var method = isFavorite ? "POST" : "DELETE";
  2137. return self.ajax({
  2138. type: method,
  2139. url: url,
  2140. dataType: "json"
  2141. });
  2142. };
  2143. /**
  2144. * Updates a user's personal rating for an item
  2145. * @param {String} userId
  2146. * @param {String} itemId
  2147. * @param {Boolean} likes
  2148. */
  2149. self.updateUserItemRating = function (userId, itemId, likes) {
  2150. if (!userId) {
  2151. throw new Error("null userId");
  2152. }
  2153. if (!itemId) {
  2154. throw new Error("null itemId");
  2155. }
  2156. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating", {
  2157. likes: likes
  2158. });
  2159. return self.ajax({
  2160. type: "POST",
  2161. url: url,
  2162. dataType: "json"
  2163. });
  2164. };
  2165. /**
  2166. * Updates a user's favorite status for an item by name.
  2167. * @param {String} userId
  2168. * @param {String} name
  2169. * @param {Boolean} isFavorite
  2170. */
  2171. self.updateFavoriteArtistStatus = function (userId, name, isFavorite) {
  2172. if (!userId) {
  2173. throw new Error("null userId");
  2174. }
  2175. if (!name) {
  2176. throw new Error("null name");
  2177. }
  2178. var url = self.getUrl("Users/" + userId + "/Favorites/Artists/" + self.encodeName(name));
  2179. var method = isFavorite ? "POST" : "DELETE";
  2180. return self.ajax({
  2181. type: method,
  2182. url: url,
  2183. dataType: "json"
  2184. });
  2185. };
  2186. self.updateFavoritePersonStatus = function (userId, name, isFavorite) {
  2187. if (!userId) {
  2188. throw new Error("null userId");
  2189. }
  2190. if (!name) {
  2191. throw new Error("null name");
  2192. }
  2193. var url = self.getUrl("Users/" + userId + "/Favorites/Persons/" + self.encodeName(name));
  2194. var method = isFavorite ? "POST" : "DELETE";
  2195. return self.ajax({
  2196. type: method,
  2197. url: url,
  2198. dataType: "json"
  2199. });
  2200. };
  2201. self.updateFavoriteStudioStatus = function (userId, name, isFavorite) {
  2202. if (!userId) {
  2203. throw new Error("null userId");
  2204. }
  2205. if (!name) {
  2206. throw new Error("null name");
  2207. }
  2208. var url = self.getUrl("Users/" + userId + "/Favorites/Studios/" + self.encodeName(name));
  2209. var method = isFavorite ? "POST" : "DELETE";
  2210. return self.ajax({
  2211. type: method,
  2212. url: url,
  2213. dataType: "json"
  2214. });
  2215. };
  2216. self.updateFavoriteGenreStatus = function (userId, name, isFavorite) {
  2217. if (!userId) {
  2218. throw new Error("null userId");
  2219. }
  2220. if (!name) {
  2221. throw new Error("null name");
  2222. }
  2223. var url = self.getUrl("Users/" + userId + "/Favorites/Genres/" + self.encodeName(name));
  2224. var method = isFavorite ? "POST" : "DELETE";
  2225. return self.ajax({
  2226. type: method,
  2227. url: url,
  2228. dataType: "json"
  2229. });
  2230. };
  2231. self.updateFavoriteMusicGenreStatus = function (userId, name, isFavorite) {
  2232. if (!userId) {
  2233. throw new Error("null userId");
  2234. }
  2235. if (!name) {
  2236. throw new Error("null name");
  2237. }
  2238. var url = self.getUrl("Users/" + userId + "/Favorites/MusicGenres/" + self.encodeName(name));
  2239. var method = isFavorite ? "POST" : "DELETE";
  2240. return self.ajax({
  2241. type: method,
  2242. url: url,
  2243. dataType: "json"
  2244. });
  2245. };
  2246. self.updateFavoriteGameGenreStatus = function (userId, name, isFavorite) {
  2247. if (!userId) {
  2248. throw new Error("null userId");
  2249. }
  2250. if (!name) {
  2251. throw new Error("null name");
  2252. }
  2253. var url = self.getUrl("Users/" + userId + "/Favorites/GameGenres/" + self.encodeName(name));
  2254. var method = isFavorite ? "POST" : "DELETE";
  2255. return self.ajax({
  2256. type: method,
  2257. url: url,
  2258. dataType: "json"
  2259. });
  2260. };
  2261. /**
  2262. * Updates a user's rating for an item by name.
  2263. * @param {String} userId
  2264. * @param {String} name
  2265. * @param {Boolean} likes
  2266. */
  2267. self.updateArtistRating = function (userId, name, likes) {
  2268. if (!userId) {
  2269. throw new Error("null userId");
  2270. }
  2271. if (!name) {
  2272. throw new Error("null name");
  2273. }
  2274. var url = self.getUrl("Users/" + userId + "/Ratings/Artists/" + self.encodeName(name), {
  2275. likes: likes
  2276. });
  2277. return self.ajax({
  2278. type: "POST",
  2279. url: url,
  2280. dataType: "json"
  2281. });
  2282. };
  2283. self.updatePersonRating = function (userId, name, likes) {
  2284. if (!userId) {
  2285. throw new Error("null userId");
  2286. }
  2287. if (!name) {
  2288. throw new Error("null name");
  2289. }
  2290. var url = self.getUrl("Users/" + userId + "/Ratings/Persons/" + self.encodeName(name), {
  2291. likes: likes
  2292. });
  2293. return self.ajax({
  2294. type: "POST",
  2295. url: url,
  2296. dataType: "json"
  2297. });
  2298. };
  2299. self.updateStudioRating = function (userId, name, likes) {
  2300. if (!userId) {
  2301. throw new Error("null userId");
  2302. }
  2303. if (!name) {
  2304. throw new Error("null name");
  2305. }
  2306. var url = self.getUrl("Users/" + userId + "/Ratings/Studios/" + self.encodeName(name), {
  2307. likes: likes
  2308. });
  2309. return self.ajax({
  2310. type: "POST",
  2311. url: url,
  2312. dataType: "json"
  2313. });
  2314. };
  2315. self.updateGenreRating = function (userId, name, likes) {
  2316. if (!userId) {
  2317. throw new Error("null userId");
  2318. }
  2319. if (!name) {
  2320. throw new Error("null name");
  2321. }
  2322. var url = self.getUrl("Users/" + userId + "/Ratings/Genres/" + self.encodeName(name), {
  2323. likes: likes
  2324. });
  2325. return self.ajax({
  2326. type: "POST",
  2327. url: url,
  2328. dataType: "json"
  2329. });
  2330. };
  2331. self.updateMusicGenreRating = function (userId, name, likes) {
  2332. if (!userId) {
  2333. throw new Error("null userId");
  2334. }
  2335. if (!name) {
  2336. throw new Error("null name");
  2337. }
  2338. var url = self.getUrl("Users/" + userId + "/Ratings/MusicGenres/" + self.encodeName(name), {
  2339. likes: likes
  2340. });
  2341. return self.ajax({
  2342. type: "POST",
  2343. url: url,
  2344. dataType: "json"
  2345. });
  2346. };
  2347. self.updateGameGenreRating = function (userId, name, likes) {
  2348. if (!userId) {
  2349. throw new Error("null userId");
  2350. }
  2351. if (!name) {
  2352. throw new Error("null name");
  2353. }
  2354. var url = self.getUrl("Users/" + userId + "/Ratings/GameGenres/" + self.encodeName(name), {
  2355. likes: likes
  2356. });
  2357. return self.ajax({
  2358. type: "POST",
  2359. url: url,
  2360. dataType: "json"
  2361. });
  2362. };
  2363. /**
  2364. * Clears a user's rating for an item by name.
  2365. * @param {String} userId
  2366. * @param {String} name
  2367. */
  2368. self.clearArtistRating = function (userId, name) {
  2369. if (!userId) {
  2370. throw new Error("null userId");
  2371. }
  2372. if (!name) {
  2373. throw new Error("null name");
  2374. }
  2375. var url = self.getUrl("Users/" + userId + "/Ratings/Artists/" + self.encodeName(name));
  2376. return self.ajax({
  2377. type: "DELETE",
  2378. url: url,
  2379. dataType: "json"
  2380. });
  2381. };
  2382. self.clearPersonRating = function (userId, name) {
  2383. if (!userId) {
  2384. throw new Error("null userId");
  2385. }
  2386. if (!name) {
  2387. throw new Error("null name");
  2388. }
  2389. var url = self.getUrl("Users/" + userId + "/Ratings/Persons/" + self.encodeName(name));
  2390. return self.ajax({
  2391. type: "DELETE",
  2392. url: url,
  2393. dataType: "json"
  2394. });
  2395. };
  2396. self.clearStudioRating = function (userId, name) {
  2397. if (!userId) {
  2398. throw new Error("null userId");
  2399. }
  2400. if (!name) {
  2401. throw new Error("null name");
  2402. }
  2403. var url = self.getUrl("Users/" + userId + "/Ratings/Studios/" + self.encodeName(name));
  2404. return self.ajax({
  2405. type: "DELETE",
  2406. url: url,
  2407. dataType: "json"
  2408. });
  2409. };
  2410. self.clearGenreRating = function (userId, name) {
  2411. if (!userId) {
  2412. throw new Error("null userId");
  2413. }
  2414. if (!name) {
  2415. throw new Error("null name");
  2416. }
  2417. var url = self.getUrl("Users/" + userId + "/Ratings/Genres/" + self.encodeName(name));
  2418. return self.ajax({
  2419. type: "DELETE",
  2420. url: url,
  2421. dataType: "json"
  2422. });
  2423. };
  2424. self.clearMusicGenreRating = function (userId, name) {
  2425. if (!userId) {
  2426. throw new Error("null userId");
  2427. }
  2428. if (!name) {
  2429. throw new Error("null name");
  2430. }
  2431. var url = self.getUrl("Users/" + userId + "/Ratings/MusicGenres/" + self.encodeName(name));
  2432. return self.ajax({
  2433. type: "DELETE",
  2434. url: url,
  2435. dataType: "json"
  2436. });
  2437. };
  2438. self.clearGameGenreRating = function (userId, name) {
  2439. if (!userId) {
  2440. throw new Error("null userId");
  2441. }
  2442. if (!name) {
  2443. throw new Error("null name");
  2444. }
  2445. var url = self.getUrl("Users/" + userId + "/Ratings/GameGenres/" + self.encodeName(name));
  2446. return self.ajax({
  2447. type: "DELETE",
  2448. url: url,
  2449. dataType: "json"
  2450. });
  2451. };
  2452. self.getItemCounts = function (userId) {
  2453. var options = {};
  2454. if (userId) {
  2455. options.userId = userId;
  2456. }
  2457. var url = self.getUrl("Items/Counts", options);
  2458. return self.ajax({
  2459. type: "GET",
  2460. url: url,
  2461. dataType: "json"
  2462. });
  2463. };
  2464. /**
  2465. * Clears a user's personal rating for an item
  2466. * @param {String} userId
  2467. * @param {String} itemId
  2468. */
  2469. self.clearUserItemRating = function (userId, itemId) {
  2470. if (!userId) {
  2471. throw new Error("null userId");
  2472. }
  2473. if (!itemId) {
  2474. throw new Error("null itemId");
  2475. }
  2476. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating");
  2477. return self.ajax({
  2478. type: "DELETE",
  2479. url: url,
  2480. dataType: "json"
  2481. });
  2482. };
  2483. /**
  2484. * Reports the user has started playing something
  2485. * @param {String} userId
  2486. * @param {String} itemId
  2487. */
  2488. self.reportPlaybackStart = function (userId, itemId, canSeek, queueableMediaTypes) {
  2489. if (!userId) {
  2490. throw new Error("null userId");
  2491. }
  2492. if (!itemId) {
  2493. throw new Error("null itemId");
  2494. }
  2495. canSeek = canSeek || false;
  2496. queueableMediaTypes = queueableMediaTypes || '';
  2497. if (self.isWebSocketOpen()) {
  2498. var deferred = $.Deferred();
  2499. var msg = [itemId, canSeek, queueableMediaTypes];
  2500. self.sendWebSocketMessage("PlaybackStart", msg.join('|'));
  2501. deferred.resolveWith(null, []);
  2502. return deferred.promise();
  2503. }
  2504. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId, {
  2505. CanSeek: canSeek,
  2506. QueueableMediaTypes: queueableMediaTypes
  2507. });
  2508. return self.ajax({
  2509. type: "POST",
  2510. url: url
  2511. });
  2512. };
  2513. /**
  2514. * Reports progress viewing an item
  2515. * @param {String} userId
  2516. * @param {String} itemId
  2517. */
  2518. self.reportPlaybackProgress = function (userId, itemId, positionTicks, isPaused, isMuted) {
  2519. if (!userId) {
  2520. throw new Error("null userId");
  2521. }
  2522. if (!itemId) {
  2523. throw new Error("null itemId");
  2524. }
  2525. if (self.isWebSocketOpen()) {
  2526. var deferred = $.Deferred();
  2527. var msgData = itemId + "|" + (positionTicks == null ? "" : positionTicks) + "|" + (isPaused == null ? "" : isPaused) + "|" + (isMuted == null ? "" : isMuted);
  2528. self.sendWebSocketMessage("PlaybackProgress", msgData);
  2529. deferred.resolveWith(null, []);
  2530. return deferred.promise();
  2531. }
  2532. var params = {
  2533. isPaused: isPaused,
  2534. isMuted: isMuted
  2535. };
  2536. if (positionTicks) {
  2537. params.positionTicks = positionTicks;
  2538. }
  2539. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId + "/Progress", params);
  2540. return self.ajax({
  2541. type: "POST",
  2542. url: url
  2543. });
  2544. };
  2545. /**
  2546. * Reports a user has stopped playing an item
  2547. * @param {String} userId
  2548. * @param {String} itemId
  2549. */
  2550. self.reportPlaybackStopped = function (userId, itemId, positionTicks) {
  2551. if (!userId) {
  2552. throw new Error("null userId");
  2553. }
  2554. if (!itemId) {
  2555. throw new Error("null itemId");
  2556. }
  2557. if (self.isWebSocketOpen()) {
  2558. var deferred = $.Deferred();
  2559. self.sendWebSocketMessage("PlaybackStopped", itemId + "|" + (positionTicks == null ? "" : positionTicks));
  2560. deferred.resolveWith(null, []);
  2561. return deferred.promise();
  2562. }
  2563. var params = {
  2564. };
  2565. if (positionTicks) {
  2566. params.positionTicks = positionTicks;
  2567. }
  2568. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId, params);
  2569. return self.ajax({
  2570. type: "DELETE",
  2571. url: url
  2572. });
  2573. };
  2574. self.sendBrowseCommand = function (sessionId, options) {
  2575. if (!sessionId) {
  2576. throw new Error("null sessionId");
  2577. }
  2578. if (!options) {
  2579. throw new Error("null options");
  2580. }
  2581. var url = self.getUrl("Sessions/" + sessionId + "/Viewing", options);
  2582. return self.ajax({
  2583. type: "POST",
  2584. url: url
  2585. });
  2586. };
  2587. self.sendPlayCommand = function (sessionId, options) {
  2588. if (!sessionId) {
  2589. throw new Error("null sessionId");
  2590. }
  2591. if (!options) {
  2592. throw new Error("null options");
  2593. }
  2594. var url = self.getUrl("Sessions/" + sessionId + "/Playing", options);
  2595. return self.ajax({
  2596. type: "POST",
  2597. url: url
  2598. });
  2599. };
  2600. self.sendSystemCommand = function (sessionId, command) {
  2601. if (!sessionId) {
  2602. throw new Error("null sessionId");
  2603. }
  2604. if (!command) {
  2605. throw new Error("null command");
  2606. }
  2607. var url = self.getUrl("Sessions/" + sessionId + "/System/" + command);
  2608. return self.ajax({
  2609. type: "POST",
  2610. url: url
  2611. });
  2612. };
  2613. self.sendMessageCommand = function (sessionId, options) {
  2614. if (!sessionId) {
  2615. throw new Error("null sessionId");
  2616. }
  2617. if (!options) {
  2618. throw new Error("null options");
  2619. }
  2620. var url = self.getUrl("Sessions/" + sessionId + "/Message", options);
  2621. return self.ajax({
  2622. type: "POST",
  2623. url: url
  2624. });
  2625. };
  2626. self.sendPlayStateCommand = function (sessionId, command, options) {
  2627. if (!sessionId) {
  2628. throw new Error("null sessionId");
  2629. }
  2630. if (!command) {
  2631. throw new Error("null command");
  2632. }
  2633. var url = self.getUrl("Sessions/" + sessionId + "/Playing/" + command, options || {});
  2634. return self.ajax({
  2635. type: "POST",
  2636. url: url
  2637. });
  2638. };
  2639. }
  2640. }(jQuery, navigator, window.JSON, window.WebSocket, setTimeout, window);
  2641. /**
  2642. * Provides a friendly way to create an api client instance using information from the browser's current url
  2643. */
  2644. MediaBrowser.ApiClient.create = function (clientName, applicationVersion) {
  2645. var loc = window.location;
  2646. return new MediaBrowser.ApiClient(loc.protocol, loc.hostname, loc.port, clientName, applicationVersion);
  2647. };
  2648. /**
  2649. *
  2650. * Secure Hash Algorithm (SHA1)
  2651. * http://www.webtoolkit.info/
  2652. *
  2653. **/
  2654. MediaBrowser.SHA1 = function (msg) {
  2655. function rotate_left(n, s) {
  2656. var t4 = (n << s) | (n >>> (32 - s));
  2657. return t4;
  2658. }
  2659. function lsb_hex(val) {
  2660. var str = "";
  2661. var i;
  2662. var vh;
  2663. var vl;
  2664. for (i = 0; i <= 6; i += 2) {
  2665. vh = (val >>> (i * 4 + 4)) & 0x0f;
  2666. vl = (val >>> (i * 4)) & 0x0f;
  2667. str += vh.toString(16) + vl.toString(16);
  2668. }
  2669. return str;
  2670. }
  2671. function cvt_hex(val) {
  2672. var str = "";
  2673. var i;
  2674. var v;
  2675. for (i = 7; i >= 0; i--) {
  2676. v = (val >>> (i * 4)) & 0x0f;
  2677. str += v.toString(16);
  2678. }
  2679. return str;
  2680. }
  2681. function Utf8Encode(string) {
  2682. string = string.replace(/\r\n/g, "\n");
  2683. var utftext = "";
  2684. for (var n = 0; n < string.length; n++) {
  2685. var c = string.charCodeAt(n);
  2686. if (c < 128) {
  2687. utftext += String.fromCharCode(c);
  2688. }
  2689. else if ((c > 127) && (c < 2048)) {
  2690. utftext += String.fromCharCode((c >> 6) | 192);
  2691. utftext += String.fromCharCode((c & 63) | 128);
  2692. }
  2693. else {
  2694. utftext += String.fromCharCode((c >> 12) | 224);
  2695. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  2696. utftext += String.fromCharCode((c & 63) | 128);
  2697. }
  2698. }
  2699. return utftext;
  2700. }
  2701. var blockstart;
  2702. var i, j;
  2703. var W = new Array(80);
  2704. var H0 = 0x67452301;
  2705. var H1 = 0xEFCDAB89;
  2706. var H2 = 0x98BADCFE;
  2707. var H3 = 0x10325476;
  2708. var H4 = 0xC3D2E1F0;
  2709. var A, B, C, D, E;
  2710. var temp;
  2711. msg = Utf8Encode(msg);
  2712. var msg_len = msg.length;
  2713. var word_array = new Array();
  2714. for (i = 0; i < msg_len - 3; i += 4) {
  2715. j = msg.charCodeAt(i) << 24 | msg.charCodeAt(i + 1) << 16 |
  2716. msg.charCodeAt(i + 2) << 8 | msg.charCodeAt(i + 3);
  2717. word_array.push(j);
  2718. }
  2719. switch (msg_len % 4) {
  2720. case 0:
  2721. i = 0x080000000;
  2722. break;
  2723. case 1:
  2724. i = msg.charCodeAt(msg_len - 1) << 24 | 0x0800000;
  2725. break;
  2726. case 2:
  2727. i = msg.charCodeAt(msg_len - 2) << 24 | msg.charCodeAt(msg_len - 1) << 16 | 0x08000;
  2728. break;
  2729. case 3:
  2730. i = msg.charCodeAt(msg_len - 3) << 24 | msg.charCodeAt(msg_len - 2) << 16 | msg.charCodeAt(msg_len - 1) << 8 | 0x80;
  2731. break;
  2732. }
  2733. word_array.push(i);
  2734. while ((word_array.length % 16) != 14) word_array.push(0);
  2735. word_array.push(msg_len >>> 29);
  2736. word_array.push((msg_len << 3) & 0x0ffffffff);
  2737. for (blockstart = 0; blockstart < word_array.length; blockstart += 16) {
  2738. for (i = 0; i < 16; i++) W[i] = word_array[blockstart + i];
  2739. for (i = 16; i <= 79; i++) W[i] = rotate_left(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
  2740. A = H0;
  2741. B = H1;
  2742. C = H2;
  2743. D = H3;
  2744. E = H4;
  2745. for (i = 0; i <= 19; i++) {
  2746. temp = (rotate_left(A, 5) + ((B & C) | (~B & D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
  2747. E = D;
  2748. D = C;
  2749. C = rotate_left(B, 30);
  2750. B = A;
  2751. A = temp;
  2752. }
  2753. for (i = 20; i <= 39; i++) {
  2754. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
  2755. E = D;
  2756. D = C;
  2757. C = rotate_left(B, 30);
  2758. B = A;
  2759. A = temp;
  2760. }
  2761. for (i = 40; i <= 59; i++) {
  2762. temp = (rotate_left(A, 5) + ((B & C) | (B & D) | (C & D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
  2763. E = D;
  2764. D = C;
  2765. C = rotate_left(B, 30);
  2766. B = A;
  2767. A = temp;
  2768. }
  2769. for (i = 60; i <= 79; i++) {
  2770. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
  2771. E = D;
  2772. D = C;
  2773. C = rotate_left(B, 30);
  2774. B = A;
  2775. A = temp;
  2776. }
  2777. H0 = (H0 + A) & 0x0ffffffff;
  2778. H1 = (H1 + B) & 0x0ffffffff;
  2779. H2 = (H2 + C) & 0x0ffffffff;
  2780. H3 = (H3 + D) & 0x0ffffffff;
  2781. H4 = (H4 + E) & 0x0ffffffff;
  2782. }
  2783. var temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4);
  2784. return temp.toLowerCase();
  2785. };
  2786. (function (jQuery, window, undefined) {
  2787. "use strict";
  2788. var matched, browser;
  2789. jQuery.uaMatch = function (ua) {
  2790. ua = ua.toLowerCase();
  2791. var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
  2792. /(webkit)[ \/]([\w.]+)/.exec(ua) ||
  2793. /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
  2794. /(msie) ([\w.]+)/.exec(ua) ||
  2795. ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
  2796. [];
  2797. var platform_match = /(ipad)/.exec(ua) ||
  2798. /(iphone)/.exec(ua) ||
  2799. /(android)/.exec(ua) ||
  2800. [];
  2801. var browser = match[1] || "";
  2802. if (ua.indexOf("like gecko") != -1 && ua.indexOf('webkit') == -1 && ua.indexOf('opera') == -1) {
  2803. browser = "msie";
  2804. }
  2805. return {
  2806. browser: browser,
  2807. version: match[2] || "0",
  2808. platform: platform_match[0] || ""
  2809. };
  2810. };
  2811. matched = jQuery.uaMatch(window.navigator.userAgent);
  2812. browser = {};
  2813. if (matched.browser) {
  2814. browser[matched.browser] = true;
  2815. browser.version = matched.version;
  2816. }
  2817. if (matched.platform) {
  2818. browser[matched.platform] = true
  2819. }
  2820. // Chrome is Webkit, but Webkit is also Safari.
  2821. if (browser.chrome) {
  2822. browser.webkit = true;
  2823. } else if (browser.webkit) {
  2824. browser.safari = true;
  2825. }
  2826. jQuery.browser = browser;
  2827. })(jQuery, window);