ApiClient.js 97 KB

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