ApiClient.js 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666
  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);
  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.updateItemImageIndex = function (itemId, itemType, itemName, imageType, imageIndex, newIndex) {
  882. if (!imageType) {
  883. throw new Error("null imageType");
  884. }
  885. if (!itemType) {
  886. throw new Error("null itemType");
  887. }
  888. var url;
  889. var options = { newIndex: newIndex };
  890. if (itemType == "Artist") {
  891. url = self.getUrl("Artists/" + self.encodeName(itemName) + "/Images/" + imageType + "/" + imageIndex + "/Index", options);
  892. }
  893. else if (itemType == "Genre") {
  894. url = self.getUrl("Genres/" + self.encodeName(itemName) + "/Images/" + imageType + "/" + imageIndex + "/Index", options);
  895. }
  896. else if (itemType == "GameGenre") {
  897. url = self.getUrl("GameGenres/" + self.encodeName(itemName) + "/Images/" + imageType + "/" + imageIndex + "/Index", options);
  898. }
  899. else if (itemType == "MusicGenre") {
  900. url = self.getUrl("MusicGenres/" + self.encodeName(itemName) + "/Images/" + imageType + "/" + imageIndex + "/Index", options);
  901. }
  902. else if (itemType == "Person") {
  903. url = self.getUrl("Persons/" + self.encodeName(itemName) + "/Images/" + imageType + "/" + imageIndex + "/Index", options);
  904. }
  905. else if (itemType == "Studio") {
  906. url = self.getUrl("Studios/" + self.encodeName(itemName) + "/Images/" + imageType + "/" + imageIndex + "/Index", options);
  907. }
  908. else {
  909. url = self.getUrl("Items/" + itemId + "/Images/" + imageType + "/" + imageIndex + "/Index", options);
  910. }
  911. return self.ajax({
  912. type: "POST",
  913. url: url
  914. });
  915. };
  916. self.getItemImageInfos = function (itemId, itemType, itemName) {
  917. if (!itemType) {
  918. throw new Error("null itemType");
  919. }
  920. var url;
  921. if (itemType == "Artist") {
  922. url = self.getUrl("Artists/" + self.encodeName(itemName) + "/Images");
  923. }
  924. else if (itemType == "Genre") {
  925. url = self.getUrl("Genres/" + self.encodeName(itemName) + "/Images");
  926. }
  927. else if (itemType == "GameGenre") {
  928. url = self.getUrl("GameGenres/" + self.encodeName(itemName) + "/Images");
  929. }
  930. else if (itemType == "MusicGenre") {
  931. url = self.getUrl("MusicGenres/" + self.encodeName(itemName) + "/Images");
  932. }
  933. else if (itemType == "Person") {
  934. url = self.getUrl("Persons/" + self.encodeName(itemName) + "/Images");
  935. }
  936. else if (itemType == "Studio") {
  937. url = self.getUrl("Studios/" + self.encodeName(itemName) + "/Images");
  938. }
  939. else {
  940. url = self.getUrl("Items/" + itemId + "/Images");
  941. }
  942. return self.ajax({
  943. type: "GET",
  944. url: url,
  945. dataType: "json"
  946. });
  947. };
  948. self.getCriticReviews = function (itemId, options) {
  949. if (!itemId) {
  950. throw new Error("null itemId");
  951. }
  952. var url = self.getUrl("Items/" + itemId + "/CriticReviews", options);
  953. return self.ajax({
  954. type: "GET",
  955. url: url,
  956. dataType: "json"
  957. });
  958. };
  959. self.getSessions = function (options) {
  960. var url = self.getUrl("Sessions", options);
  961. return self.ajax({
  962. type: "GET",
  963. url: url,
  964. dataType: "json"
  965. });
  966. };
  967. /**
  968. * Uploads a user image
  969. * @param {String} userId
  970. * @param {String} imageType The type of image to delete, based on the server-side ImageType enum.
  971. * @param {Object} file The file from the input element
  972. */
  973. self.uploadUserImage = function (userId, imageType, file) {
  974. if (!userId) {
  975. throw new Error("null userId");
  976. }
  977. if (!imageType) {
  978. throw new Error("null imageType");
  979. }
  980. if (!file) {
  981. throw new Error("File must be an image.");
  982. }
  983. if (file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/jpeg") {
  984. throw new Error("File must be an image.");
  985. }
  986. var deferred = $.Deferred();
  987. var reader = new FileReader();
  988. reader.onerror = function () {
  989. deferred.reject();
  990. };
  991. reader.onabort = function () {
  992. deferred.reject();
  993. };
  994. // Closure to capture the file information.
  995. reader.onload = function (e) {
  996. // Split by a comma to remove the url: prefix
  997. var data = e.target.result.split(',')[1];
  998. var url = self.getUrl("Users/" + userId + "/Images/" + imageType);
  999. self.ajax({
  1000. type: "POST",
  1001. url: url,
  1002. data: data,
  1003. contentType: "image/" + file.name.substring(file.name.lastIndexOf('.') + 1)
  1004. }).done(function (result) {
  1005. deferred.resolveWith(null, [result]);
  1006. }).fail(function () {
  1007. deferred.reject();
  1008. });
  1009. };
  1010. // Read in the image file as a data URL.
  1011. reader.readAsDataURL(file);
  1012. return deferred.promise();
  1013. };
  1014. self.uploadItemImage = function (itemId, itemType, itemName, imageType, file) {
  1015. if (!itemId) {
  1016. throw new Error("null itemId");
  1017. }
  1018. if (!imageType) {
  1019. throw new Error("null imageType");
  1020. }
  1021. if (!file) {
  1022. throw new Error("File must be an image.");
  1023. }
  1024. if (file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/jpeg") {
  1025. throw new Error("File must be an image.");
  1026. }
  1027. var url;
  1028. if (itemType == "Artist") {
  1029. url = self.getUrl("Artists/" + self.encodeName(itemName) + "/Images");
  1030. }
  1031. else if (itemType == "Genre") {
  1032. url = self.getUrl("Genres/" + self.encodeName(itemName) + "/Images");
  1033. }
  1034. else if (itemType == "GameGenre") {
  1035. url = self.getUrl("GameGenres/" + self.encodeName(itemName) + "/Images");
  1036. }
  1037. else if (itemType == "MusicGenre") {
  1038. url = self.getUrl("MusicGenres/" + self.encodeName(itemName) + "/Images");
  1039. }
  1040. else if (itemType == "Person") {
  1041. url = self.getUrl("Persons/" + self.encodeName(itemName) + "/Images");
  1042. }
  1043. else if (itemType == "Studio") {
  1044. url = self.getUrl("Studios/" + self.encodeName(itemName) + "/Images");
  1045. }
  1046. else {
  1047. url = self.getUrl("Items/" + itemId + "/Images");
  1048. }
  1049. url += "/" + imageType;
  1050. var deferred = $.Deferred();
  1051. var reader = new FileReader();
  1052. reader.onerror = function () {
  1053. deferred.reject();
  1054. };
  1055. reader.onabort = function () {
  1056. deferred.reject();
  1057. };
  1058. // Closure to capture the file information.
  1059. reader.onload = function (e) {
  1060. // Split by a comma to remove the url: prefix
  1061. var data = e.target.result.split(',')[1];
  1062. self.ajax({
  1063. type: "POST",
  1064. url: url,
  1065. data: data,
  1066. contentType: "image/" + file.name.substring(file.name.lastIndexOf('.') + 1)
  1067. }).done(function (result) {
  1068. deferred.resolveWith(null, [result]);
  1069. }).fail(function () {
  1070. deferred.reject();
  1071. });
  1072. };
  1073. // Read in the image file as a data URL.
  1074. reader.readAsDataURL(file);
  1075. return deferred.promise();
  1076. };
  1077. /**
  1078. * Gets the list of installed plugins on the server
  1079. */
  1080. self.getInstalledPlugins = function () {
  1081. var url = self.getUrl("Plugins");
  1082. return self.ajax({
  1083. type: "GET",
  1084. url: url,
  1085. dataType: "json"
  1086. });
  1087. };
  1088. /**
  1089. * Gets a user by id
  1090. * @param {String} id
  1091. */
  1092. self.getUser = function (id) {
  1093. if (!id) {
  1094. throw new Error("Must supply a userId");
  1095. }
  1096. var url = self.getUrl("Users/" + id);
  1097. return self.ajax({
  1098. type: "GET",
  1099. url: url,
  1100. dataType: "json"
  1101. });
  1102. };
  1103. /**
  1104. * Gets a studio
  1105. */
  1106. self.getStudio = function (name, userId) {
  1107. if (!name) {
  1108. throw new Error("null name");
  1109. }
  1110. var options = {};
  1111. if (userId) {
  1112. options.userId = userId;
  1113. }
  1114. var url = self.getUrl("Studios/" + self.encodeName(name), options);
  1115. return self.ajax({
  1116. type: "GET",
  1117. url: url,
  1118. dataType: "json"
  1119. });
  1120. };
  1121. /**
  1122. * Gets a genre
  1123. */
  1124. self.getGenre = function (name, userId) {
  1125. if (!name) {
  1126. throw new Error("null name");
  1127. }
  1128. var options = {};
  1129. if (userId) {
  1130. options.userId = userId;
  1131. }
  1132. var url = self.getUrl("Genres/" + self.encodeName(name), options);
  1133. return self.ajax({
  1134. type: "GET",
  1135. url: url,
  1136. dataType: "json"
  1137. });
  1138. };
  1139. self.getMusicGenre = function (name, userId) {
  1140. if (!name) {
  1141. throw new Error("null name");
  1142. }
  1143. var options = {};
  1144. if (userId) {
  1145. options.userId = userId;
  1146. }
  1147. var url = self.getUrl("MusicGenres/" + self.encodeName(name), options);
  1148. return self.ajax({
  1149. type: "GET",
  1150. url: url,
  1151. dataType: "json"
  1152. });
  1153. };
  1154. self.getGameGenre = function (name, userId) {
  1155. if (!name) {
  1156. throw new Error("null name");
  1157. }
  1158. var options = {};
  1159. if (userId) {
  1160. options.userId = userId;
  1161. }
  1162. var url = self.getUrl("GameGenres/" + self.encodeName(name), options);
  1163. return self.ajax({
  1164. type: "GET",
  1165. url: url,
  1166. dataType: "json"
  1167. });
  1168. };
  1169. /**
  1170. * Gets an artist
  1171. */
  1172. self.getArtist = function (name, userId) {
  1173. if (!name) {
  1174. throw new Error("null name");
  1175. }
  1176. var options = {};
  1177. if (userId) {
  1178. options.userId = userId;
  1179. }
  1180. var url = self.getUrl("Artists/" + self.encodeName(name), options);
  1181. return self.ajax({
  1182. type: "GET",
  1183. url: url,
  1184. dataType: "json"
  1185. });
  1186. };
  1187. /**
  1188. * Gets a year
  1189. */
  1190. self.getYear = function (yea, userId) {
  1191. if (!name) {
  1192. throw new Error("null name");
  1193. }
  1194. var options = {};
  1195. if (userId) {
  1196. options.userId = userId;
  1197. }
  1198. var url = self.getUrl("Years/" + self.encodeName(name), options);
  1199. return self.ajax({
  1200. type: "GET",
  1201. url: url,
  1202. dataType: "json"
  1203. });
  1204. };
  1205. /**
  1206. * Gets a Person
  1207. */
  1208. self.getPerson = function (name, userId) {
  1209. if (!name) {
  1210. throw new Error("null name");
  1211. }
  1212. var options = {};
  1213. if (userId) {
  1214. options.userId = userId;
  1215. }
  1216. var url = self.getUrl("Persons/" + self.encodeName(name), options);
  1217. return self.ajax({
  1218. type: "GET",
  1219. url: url,
  1220. dataType: "json"
  1221. });
  1222. };
  1223. self.getPublicUsers = function () {
  1224. var url = self.getUrl("users/public");
  1225. return self.ajax({
  1226. type: "GET",
  1227. url: url,
  1228. dataType: "json"
  1229. });
  1230. };
  1231. /**
  1232. * Gets all users from the server
  1233. */
  1234. self.getUsers = function (options) {
  1235. var url = self.getUrl("users", options || {});
  1236. return self.ajax({
  1237. type: "GET",
  1238. url: url,
  1239. dataType: "json"
  1240. });
  1241. };
  1242. /**
  1243. * Gets all available parental ratings from the server
  1244. */
  1245. self.getParentalRatings = function () {
  1246. var url = self.getUrl("Localization/ParentalRatings");
  1247. return self.ajax({
  1248. type: "GET",
  1249. url: url,
  1250. dataType: "json"
  1251. });
  1252. };
  1253. /**
  1254. * Gets a list of all available conrete BaseItem types from the server
  1255. */
  1256. self.getItemTypes = function (options) {
  1257. var url = self.getUrl("Library/ItemTypes", options);
  1258. return self.ajax({
  1259. type: "GET",
  1260. url: url,
  1261. dataType: "json"
  1262. });
  1263. };
  1264. /**
  1265. * Constructs a url for a user image
  1266. * @param {String} userId
  1267. * @param {Object} options
  1268. * Options supports the following properties:
  1269. * width - download the image at a fixed width
  1270. * height - download the image at a fixed height
  1271. * maxWidth - download the image at a maxWidth
  1272. * maxHeight - download the image at a maxHeight
  1273. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1274. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1275. */
  1276. self.getUserImageUrl = function (userId, options) {
  1277. if (!userId) {
  1278. throw new Error("null userId");
  1279. }
  1280. options = options || {
  1281. };
  1282. var url = "Users/" + userId + "/Images/" + options.type;
  1283. if (options.index != null) {
  1284. url += "/" + options.index;
  1285. }
  1286. // Don't put these on the query string
  1287. delete options.type;
  1288. delete options.index;
  1289. return self.getUrl(url, options);
  1290. };
  1291. /**
  1292. * Constructs a url for a person image
  1293. * @param {String} name
  1294. * @param {Object} options
  1295. * Options supports the following properties:
  1296. * width - download the image at a fixed width
  1297. * height - download the image at a fixed height
  1298. * maxWidth - download the image at a maxWidth
  1299. * maxHeight - download the image at a maxHeight
  1300. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1301. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1302. */
  1303. self.getPersonImageUrl = function (name, options) {
  1304. if (!name) {
  1305. throw new Error("null name");
  1306. }
  1307. options = options || {
  1308. };
  1309. var url = "Persons/" + self.encodeName(name) + "/Images/" + options.type;
  1310. if (options.index != null) {
  1311. url += "/" + options.index;
  1312. }
  1313. // Don't put these on the query string
  1314. delete options.type;
  1315. delete options.index;
  1316. return self.getUrl(url, options);
  1317. };
  1318. /**
  1319. * Constructs a url for a year image
  1320. * @param {String} year
  1321. * @param {Object} options
  1322. * Options supports the following properties:
  1323. * width - download the image at a fixed width
  1324. * height - download the image at a fixed height
  1325. * maxWidth - download the image at a maxWidth
  1326. * maxHeight - download the image at a maxHeight
  1327. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1328. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1329. */
  1330. self.getYearImageUrl = function (year, options) {
  1331. if (!year) {
  1332. throw new Error("null year");
  1333. }
  1334. options = options || {
  1335. };
  1336. var url = "Years/" + year + "/Images/" + options.type;
  1337. if (options.index != null) {
  1338. url += "/" + options.index;
  1339. }
  1340. // Don't put these on the query string
  1341. delete options.type;
  1342. delete options.index;
  1343. return self.getUrl(url, options);
  1344. };
  1345. /**
  1346. * Constructs a url for a genre image
  1347. * @param {String} name
  1348. * @param {Object} options
  1349. * Options supports the following properties:
  1350. * width - download the image at a fixed width
  1351. * height - download the image at a fixed height
  1352. * maxWidth - download the image at a maxWidth
  1353. * maxHeight - download the image at a maxHeight
  1354. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1355. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1356. */
  1357. self.getGenreImageUrl = function (name, options) {
  1358. if (!name) {
  1359. throw new Error("null name");
  1360. }
  1361. options = options || {
  1362. };
  1363. var url = "Genres/" + self.encodeName(name) + "/Images/" + options.type;
  1364. if (options.index != null) {
  1365. url += "/" + options.index;
  1366. }
  1367. // Don't put these on the query string
  1368. delete options.type;
  1369. delete options.index;
  1370. return self.getUrl(url, options);
  1371. };
  1372. /**
  1373. * Constructs a url for a genre image
  1374. * @param {String} name
  1375. * @param {Object} options
  1376. * Options supports the following properties:
  1377. * width - download the image at a fixed width
  1378. * height - download the image at a fixed height
  1379. * maxWidth - download the image at a maxWidth
  1380. * maxHeight - download the image at a maxHeight
  1381. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1382. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1383. */
  1384. self.getMusicGenreImageUrl = function (name, options) {
  1385. if (!name) {
  1386. throw new Error("null name");
  1387. }
  1388. options = options || {
  1389. };
  1390. var url = "MusicGenres/" + self.encodeName(name) + "/Images/" + options.type;
  1391. if (options.index != null) {
  1392. url += "/" + options.index;
  1393. }
  1394. // Don't put these on the query string
  1395. delete options.type;
  1396. delete options.index;
  1397. return self.getUrl(url, options);
  1398. };
  1399. /**
  1400. * Constructs a url for a genre image
  1401. * @param {String} name
  1402. * @param {Object} options
  1403. * Options supports the following properties:
  1404. * width - download the image at a fixed width
  1405. * height - download the image at a fixed height
  1406. * maxWidth - download the image at a maxWidth
  1407. * maxHeight - download the image at a maxHeight
  1408. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1409. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1410. */
  1411. self.getGameGenreImageUrl = function (name, options) {
  1412. if (!name) {
  1413. throw new Error("null name");
  1414. }
  1415. options = options || {
  1416. };
  1417. var url = "GameGenres/" + self.encodeName(name) + "/Images/" + options.type;
  1418. if (options.index != null) {
  1419. url += "/" + options.index;
  1420. }
  1421. // Don't put these on the query string
  1422. delete options.type;
  1423. delete options.index;
  1424. return self.getUrl(url, options);
  1425. };
  1426. /**
  1427. * Constructs a url for a artist image
  1428. * @param {String} name
  1429. * @param {Object} options
  1430. * Options supports the following properties:
  1431. * width - download the image at a fixed width
  1432. * height - download the image at a fixed height
  1433. * maxWidth - download the image at a maxWidth
  1434. * maxHeight - download the image at a maxHeight
  1435. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1436. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1437. */
  1438. self.getArtistImageUrl = function (name, options) {
  1439. if (!name) {
  1440. throw new Error("null name");
  1441. }
  1442. options = options || {
  1443. };
  1444. var url = "Artists/" + self.encodeName(name) + "/Images/" + options.type;
  1445. if (options.index != null) {
  1446. url += "/" + options.index;
  1447. }
  1448. // Don't put these on the query string
  1449. delete options.type;
  1450. delete options.index;
  1451. return self.getUrl(url, options);
  1452. };
  1453. /**
  1454. * Constructs a url for a studio image
  1455. * @param {String} name
  1456. * @param {Object} options
  1457. * Options supports the following properties:
  1458. * width - download the image at a fixed width
  1459. * height - download the image at a fixed height
  1460. * maxWidth - download the image at a maxWidth
  1461. * maxHeight - download the image at a maxHeight
  1462. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1463. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1464. */
  1465. self.getStudioImageUrl = function (name, options) {
  1466. if (!name) {
  1467. throw new Error("null name");
  1468. }
  1469. options = options || {
  1470. };
  1471. var url = "Studios/" + self.encodeName(name) + "/Images/" + options.type;
  1472. if (options.index != null) {
  1473. url += "/" + options.index;
  1474. }
  1475. // Don't put these on the query string
  1476. delete options.type;
  1477. delete options.index;
  1478. return self.getUrl(url, options);
  1479. };
  1480. /**
  1481. * Constructs a url for an item image
  1482. * @param {String} itemId
  1483. * @param {Object} options
  1484. * Options supports the following properties:
  1485. * type - Primary, logo, backdrop, etc. See the server-side enum ImageType
  1486. * index - When downloading a backdrop, use this to specify which one (omitting is equivalent to zero)
  1487. * width - download the image at a fixed width
  1488. * height - download the image at a fixed height
  1489. * maxWidth - download the image at a maxWidth
  1490. * maxHeight - download the image at a maxHeight
  1491. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1492. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1493. */
  1494. self.getImageUrl = function (itemId, options) {
  1495. if (!itemId) {
  1496. throw new Error("itemId cannot be empty");
  1497. }
  1498. options = options || {
  1499. };
  1500. var url = "Items/" + itemId + "/Images/" + options.type;
  1501. if (options.index != null) {
  1502. url += "/" + options.index;
  1503. }
  1504. // Don't put these on the query string
  1505. delete options.type;
  1506. delete options.index;
  1507. return self.getUrl(url, options);
  1508. };
  1509. /**
  1510. * Constructs a url for an item logo image
  1511. * If the item doesn't have a logo, it will inherit a logo from a parent
  1512. * @param {Object} item A BaseItem
  1513. * @param {Object} options
  1514. * Options supports the following properties:
  1515. * width - download the image at a fixed width
  1516. * height - download the image at a fixed height
  1517. * maxWidth - download the image at a maxWidth
  1518. * maxHeight - download the image at a maxHeight
  1519. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1520. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1521. */
  1522. self.getLogoImageUrl = function (item, options) {
  1523. if (!item) {
  1524. throw new Error("null item");
  1525. }
  1526. options = options || {
  1527. };
  1528. options.imageType = "logo";
  1529. var logoItemId = item.HasLogo ? item.Id : item.ParentLogoItemId;
  1530. return logoItemId ? self.getImageUrl(logoItemId, options) : null;
  1531. };
  1532. /**
  1533. * Constructs an array of backdrop image url's for an item
  1534. * If the item doesn't have any backdrops, it will inherit them from a parent
  1535. * @param {Object} item A BaseItem
  1536. * @param {Object} options
  1537. * Options supports the following properties:
  1538. * width - download the image at a fixed width
  1539. * height - download the image at a fixed height
  1540. * maxWidth - download the image at a maxWidth
  1541. * maxHeight - download the image at a maxHeight
  1542. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1543. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1544. */
  1545. self.getBackdropImageUrl = function (item, options) {
  1546. if (!item) {
  1547. throw new Error("null item");
  1548. }
  1549. options = options || {
  1550. };
  1551. options.imageType = "backdrop";
  1552. var backdropItemId;
  1553. var backdropCount;
  1554. if (!item.BackdropCount) {
  1555. backdropItemId = item.ParentBackdropItemId;
  1556. backdropCount = item.ParentBackdropCount || 0;
  1557. } else {
  1558. backdropItemId = item.Id;
  1559. backdropCount = item.BackdropCount;
  1560. }
  1561. if (!backdropItemId) {
  1562. return [];
  1563. }
  1564. var files = [];
  1565. for (var i = 0; i < backdropCount; i++) {
  1566. options.imageIndex = i;
  1567. files[i] = self.getImageUrl(backdropItemId, options);
  1568. }
  1569. return files;
  1570. };
  1571. /**
  1572. * Authenticates a user
  1573. * @param {String} name
  1574. * @param {String} password
  1575. */
  1576. self.authenticateUserByName = function (name, password) {
  1577. if (!name) {
  1578. throw new Error("null name");
  1579. }
  1580. var url = self.getUrl("Users/authenticatebyname");
  1581. var postData = {
  1582. password: MediaBrowser.SHA1(password || ""),
  1583. Username: name
  1584. };
  1585. return self.ajax({
  1586. type: "POST",
  1587. url: url,
  1588. data: JSON.stringify(postData),
  1589. dataType: "json",
  1590. contentType: "application/json"
  1591. });
  1592. };
  1593. /**
  1594. * Authenticates a user
  1595. * @param {String} userId
  1596. * @param {String} password
  1597. */
  1598. self.authenticateUser = function (userId, password) {
  1599. if (!userId) {
  1600. throw new Error("null userId");
  1601. }
  1602. var url = self.getUrl("Users/" + userId + "/authenticate");
  1603. var postData = {
  1604. password: MediaBrowser.SHA1(password || "")
  1605. };
  1606. return self.ajax({
  1607. type: "POST",
  1608. url: url,
  1609. data: JSON.stringify(postData),
  1610. dataType: "json",
  1611. contentType: "application/json"
  1612. });
  1613. };
  1614. /**
  1615. * Updates a user's password
  1616. * @param {String} userId
  1617. * @param {String} currentPassword
  1618. * @param {String} newPassword
  1619. */
  1620. self.updateUserPassword = function (userId, currentPassword, newPassword) {
  1621. if (!userId) {
  1622. throw new Error("null userId");
  1623. }
  1624. var url = self.getUrl("Users/" + userId + "/Password");
  1625. var postData = {
  1626. };
  1627. postData.currentPassword = MediaBrowser.SHA1(currentPassword);
  1628. if (newPassword) {
  1629. postData.newPassword = newPassword;
  1630. }
  1631. return self.ajax({
  1632. type: "POST",
  1633. url: url,
  1634. data: postData
  1635. });
  1636. };
  1637. /**
  1638. * Resets a user's password
  1639. * @param {String} userId
  1640. */
  1641. self.resetUserPassword = function (userId) {
  1642. if (!userId) {
  1643. throw new Error("null userId");
  1644. }
  1645. var url = self.getUrl("Users/" + userId + "/Password");
  1646. var postData = {
  1647. };
  1648. postData.resetPassword = true;
  1649. return self.ajax({
  1650. type: "POST",
  1651. url: url,
  1652. data: postData
  1653. });
  1654. };
  1655. /**
  1656. * Updates the server's configuration
  1657. * @param {Object} configuration
  1658. */
  1659. self.updateServerConfiguration = function (configuration) {
  1660. if (!configuration) {
  1661. throw new Error("null configuration");
  1662. }
  1663. var url = self.getUrl("System/Configuration");
  1664. return self.ajax({
  1665. type: "POST",
  1666. url: url,
  1667. data: JSON.stringify(configuration),
  1668. contentType: "application/json"
  1669. });
  1670. };
  1671. self.updateItem = function (item) {
  1672. if (!item) {
  1673. throw new Error("null item");
  1674. }
  1675. var url = self.getUrl("Items/" + item.Id);
  1676. return self.ajax({
  1677. type: "POST",
  1678. url: url,
  1679. data: JSON.stringify(item),
  1680. contentType: "application/json"
  1681. });
  1682. };
  1683. self.updateArtist = function (item) {
  1684. if (!item) {
  1685. throw new Error("null item");
  1686. }
  1687. var url = self.getUrl("Artists/" + self.encodeName(item.Name));
  1688. return self.ajax({
  1689. type: "POST",
  1690. url: url,
  1691. data: JSON.stringify(item),
  1692. contentType: "application/json"
  1693. });
  1694. };
  1695. self.updatePerson = function (item) {
  1696. if (!item) {
  1697. throw new Error("null item");
  1698. }
  1699. var url = self.getUrl("Persons/" + self.encodeName(item.Name));
  1700. return self.ajax({
  1701. type: "POST",
  1702. url: url,
  1703. data: JSON.stringify(item),
  1704. contentType: "application/json"
  1705. });
  1706. };
  1707. self.updateStudio = function (item) {
  1708. if (!item) {
  1709. throw new Error("null item");
  1710. }
  1711. var url = self.getUrl("Studios/" + self.encodeName(item.Name));
  1712. return self.ajax({
  1713. type: "POST",
  1714. url: url,
  1715. data: JSON.stringify(item),
  1716. contentType: "application/json"
  1717. });
  1718. };
  1719. self.updateGenre = function (item) {
  1720. if (!item) {
  1721. throw new Error("null item");
  1722. }
  1723. var url = self.getUrl("Genres/" + self.encodeName(item.Name));
  1724. return self.ajax({
  1725. type: "POST",
  1726. url: url,
  1727. data: JSON.stringify(item),
  1728. contentType: "application/json"
  1729. });
  1730. };
  1731. self.updateMusicGenre = function (item) {
  1732. if (!item) {
  1733. throw new Error("null item");
  1734. }
  1735. var url = self.getUrl("MusicGenres/" + self.encodeName(item.Name));
  1736. return self.ajax({
  1737. type: "POST",
  1738. url: url,
  1739. data: JSON.stringify(item),
  1740. contentType: "application/json"
  1741. });
  1742. };
  1743. self.updateGameGenre = function (item) {
  1744. if (!item) {
  1745. throw new Error("null item");
  1746. }
  1747. var url = self.getUrl("GameGenres/" + self.encodeName(item.Name));
  1748. return self.ajax({
  1749. type: "POST",
  1750. url: url,
  1751. data: JSON.stringify(item),
  1752. contentType: "application/json"
  1753. });
  1754. };
  1755. /**
  1756. * Updates plugin security info
  1757. */
  1758. self.updatePluginSecurityInfo = function (info) {
  1759. var url = self.getUrl("Plugins/SecurityInfo");
  1760. return self.ajax({
  1761. type: "POST",
  1762. url: url,
  1763. data: JSON.stringify(info),
  1764. contentType: "application/json"
  1765. });
  1766. };
  1767. /**
  1768. * Creates a user
  1769. * @param {Object} user
  1770. */
  1771. self.createUser = function (user) {
  1772. if (!user) {
  1773. throw new Error("null user");
  1774. }
  1775. var url = self.getUrl("Users");
  1776. return self.ajax({
  1777. type: "POST",
  1778. url: url,
  1779. data: JSON.stringify(user),
  1780. dataType: "json",
  1781. contentType: "application/json"
  1782. });
  1783. };
  1784. /**
  1785. * Updates a user
  1786. * @param {Object} user
  1787. */
  1788. self.updateUser = function (user) {
  1789. if (!user) {
  1790. throw new Error("null user");
  1791. }
  1792. var url = self.getUrl("Users/" + user.Id);
  1793. return self.ajax({
  1794. type: "POST",
  1795. url: url,
  1796. data: JSON.stringify(user),
  1797. contentType: "application/json"
  1798. });
  1799. };
  1800. /**
  1801. * Updates the Triggers for a ScheduledTask
  1802. * @param {String} id
  1803. * @param {Object} triggers
  1804. */
  1805. self.updateScheduledTaskTriggers = function (id, triggers) {
  1806. if (!id) {
  1807. throw new Error("null id");
  1808. }
  1809. if (!triggers) {
  1810. throw new Error("null triggers");
  1811. }
  1812. var url = self.getUrl("ScheduledTasks/" + id + "/Triggers");
  1813. return self.ajax({
  1814. type: "POST",
  1815. url: url,
  1816. data: JSON.stringify(triggers),
  1817. contentType: "application/json"
  1818. });
  1819. };
  1820. /**
  1821. * Updates a plugin's configuration
  1822. * @param {String} Id
  1823. * @param {Object} configuration
  1824. */
  1825. self.updatePluginConfiguration = function (id, configuration) {
  1826. if (!id) {
  1827. throw new Error("null Id");
  1828. }
  1829. if (!configuration) {
  1830. throw new Error("null configuration");
  1831. }
  1832. var url = self.getUrl("Plugins/" + id + "/Configuration");
  1833. return self.ajax({
  1834. type: "POST",
  1835. url: url,
  1836. data: JSON.stringify(configuration),
  1837. contentType: "application/json"
  1838. });
  1839. };
  1840. self.getAncestorItems = function (itemId, userId) {
  1841. if (!itemId) {
  1842. throw new Error("null itemId");
  1843. }
  1844. var options = {};
  1845. if (userId) {
  1846. options.userId = userId;
  1847. }
  1848. var url = self.getUrl("Items/" + itemId + "/Ancestors", options);
  1849. return self.ajax({
  1850. type: "GET",
  1851. url: url,
  1852. dataType: "json"
  1853. });
  1854. };
  1855. /**
  1856. * Gets items based on a query, typically for children of a folder
  1857. * @param {String} userId
  1858. * @param {Object} options
  1859. * Options accepts the following properties:
  1860. * itemId - Localize the search to a specific folder (root if omitted)
  1861. * startIndex - Use for paging
  1862. * limit - Use to limit results to a certain number of items
  1863. * filter - Specify one or more ItemFilters, comma delimeted (see server-side enum)
  1864. * sortBy - Specify an ItemSortBy (comma-delimeted list see server-side enum)
  1865. * sortOrder - ascending/descending
  1866. * fields - additional fields to include aside from basic info. This is a comma delimited list. See server-side enum ItemFields.
  1867. * index - the name of the dynamic, localized index function
  1868. * dynamicSortBy - the name of the dynamic localized sort function
  1869. * recursive - Whether or not the query should be recursive
  1870. * searchTerm - search term to use as a filter
  1871. */
  1872. self.getItems = function (userId, options) {
  1873. if (!userId) {
  1874. throw new Error("null userId");
  1875. }
  1876. var url = self.getUrl("Users/" + userId + "/Items", options);
  1877. return self.ajax({
  1878. type: "GET",
  1879. url: url,
  1880. dataType: "json"
  1881. });
  1882. };
  1883. /**
  1884. Gets artists from an item
  1885. */
  1886. self.getArtists = function (userId, options) {
  1887. if (!userId) {
  1888. throw new Error("null userId");
  1889. }
  1890. options = options || {};
  1891. options.userId = userId;
  1892. var url = self.getUrl("Artists", options);
  1893. return self.ajax({
  1894. type: "GET",
  1895. url: url,
  1896. dataType: "json"
  1897. });
  1898. };
  1899. /**
  1900. Gets genres from an item
  1901. */
  1902. self.getGenres = function (userId, options) {
  1903. if (!userId) {
  1904. throw new Error("null userId");
  1905. }
  1906. options = options || {};
  1907. options.userId = userId;
  1908. var url = self.getUrl("Genres", options);
  1909. return self.ajax({
  1910. type: "GET",
  1911. url: url,
  1912. dataType: "json"
  1913. });
  1914. };
  1915. self.getMusicGenres = function (userId, options) {
  1916. if (!userId) {
  1917. throw new Error("null userId");
  1918. }
  1919. options = options || {};
  1920. options.userId = userId;
  1921. var url = self.getUrl("MusicGenres", options);
  1922. return self.ajax({
  1923. type: "GET",
  1924. url: url,
  1925. dataType: "json"
  1926. });
  1927. };
  1928. self.getGameGenres = function (userId, options) {
  1929. if (!userId) {
  1930. throw new Error("null userId");
  1931. }
  1932. options = options || {};
  1933. options.userId = userId;
  1934. var url = self.getUrl("GameGenres", options);
  1935. return self.ajax({
  1936. type: "GET",
  1937. url: url,
  1938. dataType: "json"
  1939. });
  1940. };
  1941. /**
  1942. Gets people from an item
  1943. */
  1944. self.getPeople = function (userId, options) {
  1945. if (!userId) {
  1946. throw new Error("null userId");
  1947. }
  1948. options = options || {};
  1949. options.userId = userId;
  1950. var url = self.getUrl("Persons", options);
  1951. return self.ajax({
  1952. type: "GET",
  1953. url: url,
  1954. dataType: "json"
  1955. });
  1956. };
  1957. /**
  1958. Gets studios from an item
  1959. */
  1960. self.getStudios = function (userId, options) {
  1961. if (!userId) {
  1962. throw new Error("null userId");
  1963. }
  1964. options = options || {};
  1965. options.userId = userId;
  1966. var url = self.getUrl("Studios", options);
  1967. return self.ajax({
  1968. type: "GET",
  1969. url: url,
  1970. dataType: "json"
  1971. });
  1972. };
  1973. /**
  1974. * Gets local trailers for an item
  1975. */
  1976. self.getLocalTrailers = function (userId, itemId) {
  1977. if (!userId) {
  1978. throw new Error("null userId");
  1979. }
  1980. if (!itemId) {
  1981. throw new Error("null itemId");
  1982. }
  1983. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/LocalTrailers");
  1984. return self.ajax({
  1985. type: "GET",
  1986. url: url,
  1987. dataType: "json"
  1988. });
  1989. };
  1990. self.getAdditionalVideoParts = function (userId, itemId) {
  1991. if (!itemId) {
  1992. throw new Error("null itemId");
  1993. }
  1994. var options = {};
  1995. if (userId) {
  1996. options.userId = userId;
  1997. }
  1998. var url = self.getUrl("Videos/" + itemId + "/AdditionalParts", options);
  1999. return self.ajax({
  2000. type: "GET",
  2001. url: url,
  2002. dataType: "json"
  2003. });
  2004. };
  2005. /**
  2006. * Gets theme songs for an item
  2007. */
  2008. self.getThemeSongs = function (userId, itemId) {
  2009. if (!itemId) {
  2010. throw new Error("null itemId");
  2011. }
  2012. var options = {};
  2013. if (userId) {
  2014. options.userId = userId;
  2015. }
  2016. var url = self.getUrl("Items/" + itemId + "/ThemeSongs", options);
  2017. return self.ajax({
  2018. type: "GET",
  2019. url: url,
  2020. dataType: "json"
  2021. });
  2022. };
  2023. self.getThemeVideos = function (userId, itemId) {
  2024. if (!itemId) {
  2025. throw new Error("null itemId");
  2026. }
  2027. var options = {};
  2028. if (userId) {
  2029. options.userId = userId;
  2030. }
  2031. var url = self.getUrl("Items/" + itemId + "/ThemeVideos", options);
  2032. return self.ajax({
  2033. type: "GET",
  2034. url: url,
  2035. dataType: "json"
  2036. });
  2037. };
  2038. self.getSearchHints = function (options) {
  2039. var url = self.getUrl("Search/Hints", options);
  2040. return self.ajax({
  2041. type: "GET",
  2042. url: url,
  2043. dataType: "json"
  2044. });
  2045. };
  2046. /**
  2047. * Gets special features for an item
  2048. */
  2049. self.getSpecialFeatures = function (userId, itemId) {
  2050. if (!userId) {
  2051. throw new Error("null userId");
  2052. }
  2053. if (!itemId) {
  2054. throw new Error("null itemId");
  2055. }
  2056. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/SpecialFeatures");
  2057. return self.ajax({
  2058. type: "GET",
  2059. url: url,
  2060. dataType: "json"
  2061. });
  2062. };
  2063. /**
  2064. * Marks an item as played or unplayed
  2065. * This should not be used to update playstate following playback.
  2066. * There are separate playstate check-in methods for that. This should be used for a
  2067. * separate option to reset playstate.
  2068. * @param {String} userId
  2069. * @param {String} itemId
  2070. * @param {Boolean} wasPlayed
  2071. */
  2072. self.updatePlayedStatus = function (userId, itemId, wasPlayed) {
  2073. if (!userId) {
  2074. throw new Error("null userId");
  2075. }
  2076. if (!itemId) {
  2077. throw new Error("null itemId");
  2078. }
  2079. var url = self.getUrl("Users/" + userId + "/PlayedItems/" + itemId);
  2080. var method = wasPlayed ? "POST" : "DELETE";
  2081. return self.ajax({
  2082. type: method,
  2083. url: url,
  2084. dataType: "json"
  2085. });
  2086. };
  2087. /**
  2088. * Updates a user's favorite status for an item.
  2089. * @param {String} userId
  2090. * @param {String} itemId
  2091. * @param {Boolean} isFavorite
  2092. */
  2093. self.updateFavoriteStatus = function (userId, itemId, isFavorite) {
  2094. if (!userId) {
  2095. throw new Error("null userId");
  2096. }
  2097. if (!itemId) {
  2098. throw new Error("null itemId");
  2099. }
  2100. var url = self.getUrl("Users/" + userId + "/FavoriteItems/" + itemId);
  2101. var method = isFavorite ? "POST" : "DELETE";
  2102. return self.ajax({
  2103. type: method,
  2104. url: url,
  2105. dataType: "json"
  2106. });
  2107. };
  2108. /**
  2109. * Updates a user's personal rating for an item
  2110. * @param {String} userId
  2111. * @param {String} itemId
  2112. * @param {Boolean} likes
  2113. */
  2114. self.updateUserItemRating = function (userId, itemId, likes) {
  2115. if (!userId) {
  2116. throw new Error("null userId");
  2117. }
  2118. if (!itemId) {
  2119. throw new Error("null itemId");
  2120. }
  2121. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating", {
  2122. likes: likes
  2123. });
  2124. return self.ajax({
  2125. type: "POST",
  2126. url: url,
  2127. dataType: "json"
  2128. });
  2129. };
  2130. /**
  2131. * Updates a user's favorite status for an item by name.
  2132. * @param {String} userId
  2133. * @param {String} name
  2134. * @param {Boolean} isFavorite
  2135. */
  2136. self.updateFavoriteArtistStatus = function (userId, name, isFavorite) {
  2137. if (!userId) {
  2138. throw new Error("null userId");
  2139. }
  2140. if (!name) {
  2141. throw new Error("null name");
  2142. }
  2143. var url = self.getUrl("Users/" + userId + "/Favorites/Artists/" + self.encodeName(name));
  2144. var method = isFavorite ? "POST" : "DELETE";
  2145. return self.ajax({
  2146. type: method,
  2147. url: url,
  2148. dataType: "json"
  2149. });
  2150. };
  2151. self.updateFavoritePersonStatus = function (userId, name, isFavorite) {
  2152. if (!userId) {
  2153. throw new Error("null userId");
  2154. }
  2155. if (!name) {
  2156. throw new Error("null name");
  2157. }
  2158. var url = self.getUrl("Users/" + userId + "/Favorites/Persons/" + self.encodeName(name));
  2159. var method = isFavorite ? "POST" : "DELETE";
  2160. return self.ajax({
  2161. type: method,
  2162. url: url,
  2163. dataType: "json"
  2164. });
  2165. };
  2166. self.updateFavoriteStudioStatus = function (userId, name, isFavorite) {
  2167. if (!userId) {
  2168. throw new Error("null userId");
  2169. }
  2170. if (!name) {
  2171. throw new Error("null name");
  2172. }
  2173. var url = self.getUrl("Users/" + userId + "/Favorites/Studios/" + self.encodeName(name));
  2174. var method = isFavorite ? "POST" : "DELETE";
  2175. return self.ajax({
  2176. type: method,
  2177. url: url,
  2178. dataType: "json"
  2179. });
  2180. };
  2181. self.updateFavoriteGenreStatus = function (userId, name, isFavorite) {
  2182. if (!userId) {
  2183. throw new Error("null userId");
  2184. }
  2185. if (!name) {
  2186. throw new Error("null name");
  2187. }
  2188. var url = self.getUrl("Users/" + userId + "/Favorites/Genres/" + self.encodeName(name));
  2189. var method = isFavorite ? "POST" : "DELETE";
  2190. return self.ajax({
  2191. type: method,
  2192. url: url,
  2193. dataType: "json"
  2194. });
  2195. };
  2196. self.updateFavoriteMusicGenreStatus = function (userId, name, isFavorite) {
  2197. if (!userId) {
  2198. throw new Error("null userId");
  2199. }
  2200. if (!name) {
  2201. throw new Error("null name");
  2202. }
  2203. var url = self.getUrl("Users/" + userId + "/Favorites/MusicGenres/" + self.encodeName(name));
  2204. var method = isFavorite ? "POST" : "DELETE";
  2205. return self.ajax({
  2206. type: method,
  2207. url: url,
  2208. dataType: "json"
  2209. });
  2210. };
  2211. self.updateFavoriteGameGenreStatus = function (userId, name, isFavorite) {
  2212. if (!userId) {
  2213. throw new Error("null userId");
  2214. }
  2215. if (!name) {
  2216. throw new Error("null name");
  2217. }
  2218. var url = self.getUrl("Users/" + userId + "/Favorites/GameGenres/" + self.encodeName(name));
  2219. var method = isFavorite ? "POST" : "DELETE";
  2220. return self.ajax({
  2221. type: method,
  2222. url: url,
  2223. dataType: "json"
  2224. });
  2225. };
  2226. /**
  2227. * Updates a user's rating for an item by name.
  2228. * @param {String} userId
  2229. * @param {String} name
  2230. * @param {Boolean} likes
  2231. */
  2232. self.updateArtistRating = function (userId, name, likes) {
  2233. if (!userId) {
  2234. throw new Error("null userId");
  2235. }
  2236. if (!name) {
  2237. throw new Error("null name");
  2238. }
  2239. var url = self.getUrl("Users/" + userId + "/Ratings/Artists/" + self.encodeName(name), {
  2240. likes: likes
  2241. });
  2242. return self.ajax({
  2243. type: "POST",
  2244. url: url,
  2245. dataType: "json"
  2246. });
  2247. };
  2248. self.updatePersonRating = function (userId, name, likes) {
  2249. if (!userId) {
  2250. throw new Error("null userId");
  2251. }
  2252. if (!name) {
  2253. throw new Error("null name");
  2254. }
  2255. var url = self.getUrl("Users/" + userId + "/Ratings/Persons/" + self.encodeName(name), {
  2256. likes: likes
  2257. });
  2258. return self.ajax({
  2259. type: "POST",
  2260. url: url,
  2261. dataType: "json"
  2262. });
  2263. };
  2264. self.updateStudioRating = function (userId, name, likes) {
  2265. if (!userId) {
  2266. throw new Error("null userId");
  2267. }
  2268. if (!name) {
  2269. throw new Error("null name");
  2270. }
  2271. var url = self.getUrl("Users/" + userId + "/Ratings/Studios/" + self.encodeName(name), {
  2272. likes: likes
  2273. });
  2274. return self.ajax({
  2275. type: "POST",
  2276. url: url,
  2277. dataType: "json"
  2278. });
  2279. };
  2280. self.updateGenreRating = function (userId, name, likes) {
  2281. if (!userId) {
  2282. throw new Error("null userId");
  2283. }
  2284. if (!name) {
  2285. throw new Error("null name");
  2286. }
  2287. var url = self.getUrl("Users/" + userId + "/Ratings/Genres/" + self.encodeName(name), {
  2288. likes: likes
  2289. });
  2290. return self.ajax({
  2291. type: "POST",
  2292. url: url,
  2293. dataType: "json"
  2294. });
  2295. };
  2296. self.updateMusicGenreRating = function (userId, name, likes) {
  2297. if (!userId) {
  2298. throw new Error("null userId");
  2299. }
  2300. if (!name) {
  2301. throw new Error("null name");
  2302. }
  2303. var url = self.getUrl("Users/" + userId + "/Ratings/MusicGenres/" + self.encodeName(name), {
  2304. likes: likes
  2305. });
  2306. return self.ajax({
  2307. type: "POST",
  2308. url: url,
  2309. dataType: "json"
  2310. });
  2311. };
  2312. self.updateGameGenreRating = function (userId, name, likes) {
  2313. if (!userId) {
  2314. throw new Error("null userId");
  2315. }
  2316. if (!name) {
  2317. throw new Error("null name");
  2318. }
  2319. var url = self.getUrl("Users/" + userId + "/Ratings/GameGenres/" + self.encodeName(name), {
  2320. likes: likes
  2321. });
  2322. return self.ajax({
  2323. type: "POST",
  2324. url: url,
  2325. dataType: "json"
  2326. });
  2327. };
  2328. /**
  2329. * Clears a user's rating for an item by name.
  2330. * @param {String} userId
  2331. * @param {String} name
  2332. */
  2333. self.clearArtistRating = function (userId, name) {
  2334. if (!userId) {
  2335. throw new Error("null userId");
  2336. }
  2337. if (!name) {
  2338. throw new Error("null name");
  2339. }
  2340. var url = self.getUrl("Users/" + userId + "/Ratings/Artists/" + self.encodeName(name));
  2341. return self.ajax({
  2342. type: "DELETE",
  2343. url: url,
  2344. dataType: "json"
  2345. });
  2346. };
  2347. self.clearPersonRating = function (userId, name) {
  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/Persons/" + self.encodeName(name));
  2355. return self.ajax({
  2356. type: "DELETE",
  2357. url: url,
  2358. dataType: "json"
  2359. });
  2360. };
  2361. self.clearStudioRating = function (userId, name) {
  2362. if (!userId) {
  2363. throw new Error("null userId");
  2364. }
  2365. if (!name) {
  2366. throw new Error("null name");
  2367. }
  2368. var url = self.getUrl("Users/" + userId + "/Ratings/Studios/" + self.encodeName(name));
  2369. return self.ajax({
  2370. type: "DELETE",
  2371. url: url,
  2372. dataType: "json"
  2373. });
  2374. };
  2375. self.clearGenreRating = function (userId, name) {
  2376. if (!userId) {
  2377. throw new Error("null userId");
  2378. }
  2379. if (!name) {
  2380. throw new Error("null name");
  2381. }
  2382. var url = self.getUrl("Users/" + userId + "/Ratings/Genres/" + self.encodeName(name));
  2383. return self.ajax({
  2384. type: "DELETE",
  2385. url: url,
  2386. dataType: "json"
  2387. });
  2388. };
  2389. self.clearMusicGenreRating = function (userId, name) {
  2390. if (!userId) {
  2391. throw new Error("null userId");
  2392. }
  2393. if (!name) {
  2394. throw new Error("null name");
  2395. }
  2396. var url = self.getUrl("Users/" + userId + "/Ratings/MusicGenres/" + self.encodeName(name));
  2397. return self.ajax({
  2398. type: "DELETE",
  2399. url: url,
  2400. dataType: "json"
  2401. });
  2402. };
  2403. self.clearGameGenreRating = function (userId, name) {
  2404. if (!userId) {
  2405. throw new Error("null userId");
  2406. }
  2407. if (!name) {
  2408. throw new Error("null name");
  2409. }
  2410. var url = self.getUrl("Users/" + userId + "/Ratings/GameGenres/" + self.encodeName(name));
  2411. return self.ajax({
  2412. type: "DELETE",
  2413. url: url,
  2414. dataType: "json"
  2415. });
  2416. };
  2417. self.getItemCounts = function (userId) {
  2418. var options = {};
  2419. if (userId) {
  2420. options.userId = userId;
  2421. }
  2422. var url = self.getUrl("Items/Counts", options);
  2423. return self.ajax({
  2424. type: "GET",
  2425. url: url,
  2426. dataType: "json"
  2427. });
  2428. };
  2429. /**
  2430. * Clears a user's personal rating for an item
  2431. * @param {String} userId
  2432. * @param {String} itemId
  2433. */
  2434. self.clearUserItemRating = function (userId, itemId) {
  2435. if (!userId) {
  2436. throw new Error("null userId");
  2437. }
  2438. if (!itemId) {
  2439. throw new Error("null itemId");
  2440. }
  2441. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating");
  2442. return self.ajax({
  2443. type: "DELETE",
  2444. url: url,
  2445. dataType: "json"
  2446. });
  2447. };
  2448. /**
  2449. * Reports the user has started playing something
  2450. * @param {String} userId
  2451. * @param {String} itemId
  2452. */
  2453. self.reportPlaybackStart = function (userId, itemId, canSeek, queueableMediaTypes) {
  2454. if (!userId) {
  2455. throw new Error("null userId");
  2456. }
  2457. if (!itemId) {
  2458. throw new Error("null itemId");
  2459. }
  2460. canSeek = canSeek || false;
  2461. queueableMediaTypes = queueableMediaTypes || '';
  2462. if (self.isWebSocketOpen()) {
  2463. var deferred = $.Deferred();
  2464. var msg = [itemId, canSeek, queueableMediaTypes];
  2465. self.sendWebSocketMessage("PlaybackStart", msg.join('|'));
  2466. deferred.resolveWith(null, []);
  2467. return deferred.promise();
  2468. }
  2469. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId, {
  2470. CanSeek: canSeek,
  2471. QueueableMediaTypes: queueableMediaTypes
  2472. });
  2473. return self.ajax({
  2474. type: "POST",
  2475. url: url
  2476. });
  2477. };
  2478. /**
  2479. * Reports progress viewing an item
  2480. * @param {String} userId
  2481. * @param {String} itemId
  2482. */
  2483. self.reportPlaybackProgress = function (userId, itemId, positionTicks, isPaused, isMuted) {
  2484. if (!userId) {
  2485. throw new Error("null userId");
  2486. }
  2487. if (!itemId) {
  2488. throw new Error("null itemId");
  2489. }
  2490. if (self.isWebSocketOpen()) {
  2491. var deferred = $.Deferred();
  2492. var msgData = itemId + "|" + (positionTicks == null ? "" : positionTicks) + "|" + (isPaused == null ? "" : isPaused) + "|" + (isMuted == null ? "" : isMuted);
  2493. self.sendWebSocketMessage("PlaybackProgress", msgData);
  2494. deferred.resolveWith(null, []);
  2495. return deferred.promise();
  2496. }
  2497. var params = {
  2498. isPaused: isPaused,
  2499. isMuted: isMuted
  2500. };
  2501. if (positionTicks) {
  2502. params.positionTicks = positionTicks;
  2503. }
  2504. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId + "/Progress", params);
  2505. return self.ajax({
  2506. type: "POST",
  2507. url: url
  2508. });
  2509. };
  2510. /**
  2511. * Reports a user has stopped playing an item
  2512. * @param {String} userId
  2513. * @param {String} itemId
  2514. */
  2515. self.reportPlaybackStopped = function (userId, itemId, positionTicks) {
  2516. if (!userId) {
  2517. throw new Error("null userId");
  2518. }
  2519. if (!itemId) {
  2520. throw new Error("null itemId");
  2521. }
  2522. if (self.isWebSocketOpen()) {
  2523. var deferred = $.Deferred();
  2524. self.sendWebSocketMessage("PlaybackStopped", itemId + "|" + (positionTicks == null ? "" : positionTicks));
  2525. deferred.resolveWith(null, []);
  2526. return deferred.promise();
  2527. }
  2528. var params = {
  2529. };
  2530. if (positionTicks) {
  2531. params.positionTicks = positionTicks;
  2532. }
  2533. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId, params);
  2534. return self.ajax({
  2535. type: "DELETE",
  2536. url: url
  2537. });
  2538. };
  2539. self.sendBrowseCommand = function (sessionId, options) {
  2540. if (!sessionId) {
  2541. throw new Error("null sessionId");
  2542. }
  2543. if (!options) {
  2544. throw new Error("null options");
  2545. }
  2546. var url = self.getUrl("Sessions/" + sessionId + "/Viewing", options);
  2547. return self.ajax({
  2548. type: "POST",
  2549. url: url
  2550. });
  2551. };
  2552. self.sendPlayCommand = function (sessionId, options) {
  2553. if (!sessionId) {
  2554. throw new Error("null sessionId");
  2555. }
  2556. if (!options) {
  2557. throw new Error("null options");
  2558. }
  2559. var url = self.getUrl("Sessions/" + sessionId + "/Playing", options);
  2560. return self.ajax({
  2561. type: "POST",
  2562. url: url
  2563. });
  2564. };
  2565. self.sendSystemCommand = function (sessionId, command) {
  2566. if (!sessionId) {
  2567. throw new Error("null sessionId");
  2568. }
  2569. if (!command) {
  2570. throw new Error("null command");
  2571. }
  2572. var url = self.getUrl("Sessions/" + sessionId + "/System/" + command);
  2573. return self.ajax({
  2574. type: "POST",
  2575. url: url
  2576. });
  2577. };
  2578. self.sendMessageCommand = function (sessionId, options) {
  2579. if (!sessionId) {
  2580. throw new Error("null sessionId");
  2581. }
  2582. if (!options) {
  2583. throw new Error("null options");
  2584. }
  2585. var url = self.getUrl("Sessions/" + sessionId + "/Message", options);
  2586. return self.ajax({
  2587. type: "POST",
  2588. url: url
  2589. });
  2590. };
  2591. self.sendPlayStateCommand = function (sessionId, command, options) {
  2592. if (!sessionId) {
  2593. throw new Error("null sessionId");
  2594. }
  2595. if (!command) {
  2596. throw new Error("null command");
  2597. }
  2598. var url = self.getUrl("Sessions/" + sessionId + "/Playing/" + command, options || {});
  2599. return self.ajax({
  2600. type: "POST",
  2601. url: url
  2602. });
  2603. };
  2604. }
  2605. }(jQuery, navigator, window.JSON, window.WebSocket, setTimeout, window);
  2606. /**
  2607. * Provides a friendly way to create an api client instance using information from the browser's current url
  2608. */
  2609. MediaBrowser.ApiClient.create = function (clientName, applicationVersion) {
  2610. var loc = window.location;
  2611. return new MediaBrowser.ApiClient(loc.protocol, loc.hostname, loc.port, clientName, applicationVersion);
  2612. };
  2613. /**
  2614. *
  2615. * Secure Hash Algorithm (SHA1)
  2616. * http://www.webtoolkit.info/
  2617. *
  2618. **/
  2619. MediaBrowser.SHA1 = function (msg) {
  2620. function rotate_left(n, s) {
  2621. var t4 = (n << s) | (n >>> (32 - s));
  2622. return t4;
  2623. }
  2624. function lsb_hex(val) {
  2625. var str = "";
  2626. var i;
  2627. var vh;
  2628. var vl;
  2629. for (i = 0; i <= 6; i += 2) {
  2630. vh = (val >>> (i * 4 + 4)) & 0x0f;
  2631. vl = (val >>> (i * 4)) & 0x0f;
  2632. str += vh.toString(16) + vl.toString(16);
  2633. }
  2634. return str;
  2635. }
  2636. function cvt_hex(val) {
  2637. var str = "";
  2638. var i;
  2639. var v;
  2640. for (i = 7; i >= 0; i--) {
  2641. v = (val >>> (i * 4)) & 0x0f;
  2642. str += v.toString(16);
  2643. }
  2644. return str;
  2645. }
  2646. function Utf8Encode(string) {
  2647. string = string.replace(/\r\n/g, "\n");
  2648. var utftext = "";
  2649. for (var n = 0; n < string.length; n++) {
  2650. var c = string.charCodeAt(n);
  2651. if (c < 128) {
  2652. utftext += String.fromCharCode(c);
  2653. }
  2654. else if ((c > 127) && (c < 2048)) {
  2655. utftext += String.fromCharCode((c >> 6) | 192);
  2656. utftext += String.fromCharCode((c & 63) | 128);
  2657. }
  2658. else {
  2659. utftext += String.fromCharCode((c >> 12) | 224);
  2660. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  2661. utftext += String.fromCharCode((c & 63) | 128);
  2662. }
  2663. }
  2664. return utftext;
  2665. }
  2666. var blockstart;
  2667. var i, j;
  2668. var W = new Array(80);
  2669. var H0 = 0x67452301;
  2670. var H1 = 0xEFCDAB89;
  2671. var H2 = 0x98BADCFE;
  2672. var H3 = 0x10325476;
  2673. var H4 = 0xC3D2E1F0;
  2674. var A, B, C, D, E;
  2675. var temp;
  2676. msg = Utf8Encode(msg);
  2677. var msg_len = msg.length;
  2678. var word_array = new Array();
  2679. for (i = 0; i < msg_len - 3; i += 4) {
  2680. j = msg.charCodeAt(i) << 24 | msg.charCodeAt(i + 1) << 16 |
  2681. msg.charCodeAt(i + 2) << 8 | msg.charCodeAt(i + 3);
  2682. word_array.push(j);
  2683. }
  2684. switch (msg_len % 4) {
  2685. case 0:
  2686. i = 0x080000000;
  2687. break;
  2688. case 1:
  2689. i = msg.charCodeAt(msg_len - 1) << 24 | 0x0800000;
  2690. break;
  2691. case 2:
  2692. i = msg.charCodeAt(msg_len - 2) << 24 | msg.charCodeAt(msg_len - 1) << 16 | 0x08000;
  2693. break;
  2694. case 3:
  2695. i = msg.charCodeAt(msg_len - 3) << 24 | msg.charCodeAt(msg_len - 2) << 16 | msg.charCodeAt(msg_len - 1) << 8 | 0x80;
  2696. break;
  2697. }
  2698. word_array.push(i);
  2699. while ((word_array.length % 16) != 14) word_array.push(0);
  2700. word_array.push(msg_len >>> 29);
  2701. word_array.push((msg_len << 3) & 0x0ffffffff);
  2702. for (blockstart = 0; blockstart < word_array.length; blockstart += 16) {
  2703. for (i = 0; i < 16; i++) W[i] = word_array[blockstart + i];
  2704. for (i = 16; i <= 79; i++) W[i] = rotate_left(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
  2705. A = H0;
  2706. B = H1;
  2707. C = H2;
  2708. D = H3;
  2709. E = H4;
  2710. for (i = 0; i <= 19; i++) {
  2711. temp = (rotate_left(A, 5) + ((B & C) | (~B & D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
  2712. E = D;
  2713. D = C;
  2714. C = rotate_left(B, 30);
  2715. B = A;
  2716. A = temp;
  2717. }
  2718. for (i = 20; i <= 39; i++) {
  2719. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
  2720. E = D;
  2721. D = C;
  2722. C = rotate_left(B, 30);
  2723. B = A;
  2724. A = temp;
  2725. }
  2726. for (i = 40; i <= 59; i++) {
  2727. temp = (rotate_left(A, 5) + ((B & C) | (B & D) | (C & D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
  2728. E = D;
  2729. D = C;
  2730. C = rotate_left(B, 30);
  2731. B = A;
  2732. A = temp;
  2733. }
  2734. for (i = 60; i <= 79; i++) {
  2735. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
  2736. E = D;
  2737. D = C;
  2738. C = rotate_left(B, 30);
  2739. B = A;
  2740. A = temp;
  2741. }
  2742. H0 = (H0 + A) & 0x0ffffffff;
  2743. H1 = (H1 + B) & 0x0ffffffff;
  2744. H2 = (H2 + C) & 0x0ffffffff;
  2745. H3 = (H3 + D) & 0x0ffffffff;
  2746. H4 = (H4 + E) & 0x0ffffffff;
  2747. }
  2748. var temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4);
  2749. return temp.toLowerCase();
  2750. };
  2751. (function (jQuery, window, undefined) {
  2752. "use strict";
  2753. var matched, browser;
  2754. jQuery.uaMatch = function (ua) {
  2755. ua = ua.toLowerCase();
  2756. var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
  2757. /(webkit)[ \/]([\w.]+)/.exec(ua) ||
  2758. /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
  2759. /(msie) ([\w.]+)/.exec(ua) ||
  2760. ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
  2761. [];
  2762. var platform_match = /(ipad)/.exec(ua) ||
  2763. /(iphone)/.exec(ua) ||
  2764. /(android)/.exec(ua) ||
  2765. [];
  2766. var browser = match[1] || "";
  2767. if (ua.indexOf("like gecko") != -1 && ua.indexOf('webkit') == -1 && ua.indexOf('opera') == -1) {
  2768. browser = "msie";
  2769. }
  2770. return {
  2771. browser: browser,
  2772. version: match[2] || "0",
  2773. platform: platform_match[0] || ""
  2774. };
  2775. };
  2776. matched = jQuery.uaMatch(window.navigator.userAgent);
  2777. browser = {};
  2778. if (matched.browser) {
  2779. browser[matched.browser] = true;
  2780. browser.version = matched.version;
  2781. }
  2782. if (matched.platform) {
  2783. browser[matched.platform] = true
  2784. }
  2785. // Chrome is Webkit, but Webkit is also Safari.
  2786. if (browser.chrome) {
  2787. browser.webkit = true;
  2788. } else if (browser.webkit) {
  2789. browser.safari = true;
  2790. }
  2791. jQuery.browser = browser;
  2792. })(jQuery, window);