ApiClient.js 97 KB

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