ApiClient.js 98 KB

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