ApiClient.js 80 KB

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