ApiClient.js 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075
  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 weather info
  948. * @param {String} location - us zip code / city, state, country / city, country
  949. * Omit location to get weather info using stored server configuration value
  950. */
  951. self.getWeatherInfo = function (location) {
  952. var url = self.getUrl("weather", {
  953. location: location
  954. });
  955. return self.ajax({
  956. type: "GET",
  957. url: url,
  958. dataType: "json"
  959. });
  960. };
  961. /**
  962. * Gets all users from the server
  963. */
  964. self.getUsers = function () {
  965. var url = self.getUrl("users");
  966. return self.ajax({
  967. type: "GET",
  968. url: url,
  969. dataType: "json"
  970. });
  971. };
  972. /**
  973. * Gets all available parental ratings from the server
  974. */
  975. self.getParentalRatings = function () {
  976. var url = self.getUrl("Localization/ParentalRatings");
  977. return self.ajax({
  978. type: "GET",
  979. url: url,
  980. dataType: "json"
  981. });
  982. };
  983. /**
  984. * Gets a list of all available conrete BaseItem types from the server
  985. */
  986. self.getItemTypes = function (options) {
  987. var url = self.getUrl("Library/ItemTypes", options);
  988. return self.ajax({
  989. type: "GET",
  990. url: url,
  991. dataType: "json"
  992. });
  993. };
  994. /**
  995. * Constructs a url for a user image
  996. * @param {String} userId
  997. * @param {Object} options
  998. * Options supports the following properties:
  999. * width - download the image at a fixed width
  1000. * height - download the image at a fixed height
  1001. * maxWidth - download the image at a maxWidth
  1002. * maxHeight - download the image at a maxHeight
  1003. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1004. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1005. */
  1006. self.getUserImageUrl = function (userId, options) {
  1007. if (!userId) {
  1008. throw new Error("null userId");
  1009. }
  1010. options = options || {
  1011. };
  1012. var url = "Users/" + userId + "/Images/" + options.type;
  1013. if (options.index != null) {
  1014. url += "/" + options.index;
  1015. }
  1016. // Don't put these on the query string
  1017. delete options.type;
  1018. delete options.index;
  1019. return self.getUrl(url, options);
  1020. };
  1021. /**
  1022. * Constructs a url for a person image
  1023. * @param {String} name
  1024. * @param {Object} options
  1025. * Options supports the following properties:
  1026. * width - download the image at a fixed width
  1027. * height - download the image at a fixed height
  1028. * maxWidth - download the image at a maxWidth
  1029. * maxHeight - download the image at a maxHeight
  1030. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1031. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1032. */
  1033. self.getPersonImageUrl = function (name, options) {
  1034. if (!name) {
  1035. throw new Error("null name");
  1036. }
  1037. options = options || {
  1038. };
  1039. var url = "Persons/" + self.encodeName(name) + "/Images/" + options.type;
  1040. if (options.index != null) {
  1041. url += "/" + options.index;
  1042. }
  1043. // Don't put these on the query string
  1044. delete options.type;
  1045. delete options.index;
  1046. return self.getUrl(url, options);
  1047. };
  1048. /**
  1049. * Constructs a url for a year image
  1050. * @param {String} year
  1051. * @param {Object} options
  1052. * Options supports the following properties:
  1053. * width - download the image at a fixed width
  1054. * height - download the image at a fixed height
  1055. * maxWidth - download the image at a maxWidth
  1056. * maxHeight - download the image at a maxHeight
  1057. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1058. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1059. */
  1060. self.getYearImageUrl = function (year, options) {
  1061. if (!year) {
  1062. throw new Error("null year");
  1063. }
  1064. options = options || {
  1065. };
  1066. var url = "Years/" + year + "/Images/" + options.type;
  1067. if (options.index != null) {
  1068. url += "/" + options.index;
  1069. }
  1070. // Don't put these on the query string
  1071. delete options.type;
  1072. delete options.index;
  1073. return self.getUrl(url, options);
  1074. };
  1075. /**
  1076. * Constructs a url for a genre image
  1077. * @param {String} name
  1078. * @param {Object} options
  1079. * Options supports the following properties:
  1080. * width - download the image at a fixed width
  1081. * height - download the image at a fixed height
  1082. * maxWidth - download the image at a maxWidth
  1083. * maxHeight - download the image at a maxHeight
  1084. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1085. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1086. */
  1087. self.getGenreImageUrl = function (name, options) {
  1088. if (!name) {
  1089. throw new Error("null name");
  1090. }
  1091. options = options || {
  1092. };
  1093. var url = "Genres/" + self.encodeName(name) + "/Images/" + options.type;
  1094. if (options.index != null) {
  1095. url += "/" + options.index;
  1096. }
  1097. // Don't put these on the query string
  1098. delete options.type;
  1099. delete options.index;
  1100. return self.getUrl(url, options);
  1101. };
  1102. /**
  1103. * Constructs a url for a genre image
  1104. * @param {String} name
  1105. * @param {Object} options
  1106. * Options supports the following properties:
  1107. * width - download the image at a fixed width
  1108. * height - download the image at a fixed height
  1109. * maxWidth - download the image at a maxWidth
  1110. * maxHeight - download the image at a maxHeight
  1111. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1112. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1113. */
  1114. self.getMusicGenreImageUrl = function (name, options) {
  1115. if (!name) {
  1116. throw new Error("null name");
  1117. }
  1118. options = options || {
  1119. };
  1120. var url = "MusicGenres/" + self.encodeName(name) + "/Images/" + options.type;
  1121. if (options.index != null) {
  1122. url += "/" + options.index;
  1123. }
  1124. // Don't put these on the query string
  1125. delete options.type;
  1126. delete options.index;
  1127. return self.getUrl(url, options);
  1128. };
  1129. /**
  1130. * Constructs a url for a artist image
  1131. * @param {String} name
  1132. * @param {Object} options
  1133. * Options supports the following properties:
  1134. * width - download the image at a fixed width
  1135. * height - download the image at a fixed height
  1136. * maxWidth - download the image at a maxWidth
  1137. * maxHeight - download the image at a maxHeight
  1138. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1139. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1140. */
  1141. self.getArtistImageUrl = function (name, options) {
  1142. if (!name) {
  1143. throw new Error("null name");
  1144. }
  1145. options = options || {
  1146. };
  1147. var url = "Artists/" + self.encodeName(name) + "/Images/" + options.type;
  1148. if (options.index != null) {
  1149. url += "/" + options.index;
  1150. }
  1151. // Don't put these on the query string
  1152. delete options.type;
  1153. delete options.index;
  1154. return self.getUrl(url, options);
  1155. };
  1156. /**
  1157. * Constructs a url for a studio image
  1158. * @param {String} name
  1159. * @param {Object} options
  1160. * Options supports the following properties:
  1161. * width - download the image at a fixed width
  1162. * height - download the image at a fixed height
  1163. * maxWidth - download the image at a maxWidth
  1164. * maxHeight - download the image at a maxHeight
  1165. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1166. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1167. */
  1168. self.getStudioImageUrl = function (name, options) {
  1169. if (!name) {
  1170. throw new Error("null name");
  1171. }
  1172. options = options || {
  1173. };
  1174. var url = "Studios/" + self.encodeName(name) + "/Images/" + options.type;
  1175. if (options.index != null) {
  1176. url += "/" + options.index;
  1177. }
  1178. // Don't put these on the query string
  1179. delete options.type;
  1180. delete options.index;
  1181. return self.getUrl(url, options);
  1182. };
  1183. /**
  1184. * Constructs a url for an item image
  1185. * @param {String} itemId
  1186. * @param {Object} options
  1187. * Options supports the following properties:
  1188. * type - Primary, logo, backdrop, etc. See the server-side enum ImageType
  1189. * index - When downloading a backdrop, use this to specify which one (omitting is equivalent to zero)
  1190. * width - download the image at a fixed width
  1191. * height - download the image at a fixed height
  1192. * maxWidth - download the image at a maxWidth
  1193. * maxHeight - download the image at a maxHeight
  1194. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1195. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1196. */
  1197. self.getImageUrl = function (itemId, options) {
  1198. if (!itemId) {
  1199. throw new Error("itemId cannot be empty");
  1200. }
  1201. options = options || {
  1202. };
  1203. var url = "Items/" + itemId + "/Images/" + options.type;
  1204. if (options.index != null) {
  1205. url += "/" + options.index;
  1206. }
  1207. // Don't put these on the query string
  1208. delete options.type;
  1209. delete options.index;
  1210. return self.getUrl(url, options);
  1211. };
  1212. /**
  1213. * Constructs a url for an item logo image
  1214. * If the item doesn't have a logo, it will inherit a logo from a parent
  1215. * @param {Object} item A BaseItem
  1216. * @param {Object} options
  1217. * Options supports the following properties:
  1218. * width - download the image at a fixed width
  1219. * height - download the image at a fixed height
  1220. * maxWidth - download the image at a maxWidth
  1221. * maxHeight - download the image at a maxHeight
  1222. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1223. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1224. */
  1225. self.getLogoImageUrl = function (item, options) {
  1226. if (!item) {
  1227. throw new Error("null item");
  1228. }
  1229. options = options || {
  1230. };
  1231. options.imageType = "logo";
  1232. var logoItemId = item.HasLogo ? item.Id : item.ParentLogoItemId;
  1233. return logoItemId ? self.getImageUrl(logoItemId, options) : null;
  1234. };
  1235. /**
  1236. * Constructs an array of backdrop image url's for an item
  1237. * If the item doesn't have any backdrops, it will inherit them from a parent
  1238. * @param {Object} item A BaseItem
  1239. * @param {Object} options
  1240. * Options supports the following properties:
  1241. * width - download the image at a fixed width
  1242. * height - download the image at a fixed height
  1243. * maxWidth - download the image at a maxWidth
  1244. * maxHeight - download the image at a maxHeight
  1245. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1246. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1247. */
  1248. self.getBackdropImageUrl = function (item, options) {
  1249. if (!item) {
  1250. throw new Error("null item");
  1251. }
  1252. options = options || {
  1253. };
  1254. options.imageType = "backdrop";
  1255. var backdropItemId;
  1256. var backdropCount;
  1257. if (!item.BackdropCount) {
  1258. backdropItemId = item.ParentBackdropItemId;
  1259. backdropCount = item.ParentBackdropCount || 0;
  1260. } else {
  1261. backdropItemId = item.Id;
  1262. backdropCount = item.BackdropCount;
  1263. }
  1264. if (!backdropItemId) {
  1265. return [];
  1266. }
  1267. var files = [];
  1268. for (var i = 0; i < backdropCount; i++) {
  1269. options.imageIndex = i;
  1270. files[i] = self.getImageUrl(backdropItemId, options);
  1271. }
  1272. return files;
  1273. };
  1274. /**
  1275. * Authenticates a user
  1276. * @param {String} userId
  1277. * @param {String} password
  1278. */
  1279. self.authenticateUser = function (userId, password) {
  1280. if (!userId) {
  1281. throw new Error("null userId");
  1282. }
  1283. var url = self.getUrl("Users/" + userId + "/authenticate");
  1284. var postData = {
  1285. password: MediaBrowser.SHA1(password || "")
  1286. };
  1287. return self.ajax({
  1288. type: "POST",
  1289. url: url,
  1290. data: JSON.stringify(postData),
  1291. contentType: "application/json"
  1292. });
  1293. };
  1294. /**
  1295. * Updates a user's password
  1296. * @param {String} userId
  1297. * @param {String} currentPassword
  1298. * @param {String} newPassword
  1299. */
  1300. self.updateUserPassword = function (userId, currentPassword, newPassword) {
  1301. if (!userId) {
  1302. throw new Error("null userId");
  1303. }
  1304. var url = self.getUrl("Users/" + userId + "/Password");
  1305. var postData = {
  1306. };
  1307. postData.currentPassword = MediaBrowser.SHA1(currentPassword);
  1308. if (newPassword) {
  1309. postData.newPassword = newPassword;
  1310. }
  1311. return self.ajax({
  1312. type: "POST",
  1313. url: url,
  1314. data: postData
  1315. });
  1316. };
  1317. /**
  1318. * Resets a user's password
  1319. * @param {String} userId
  1320. */
  1321. self.resetUserPassword = function (userId) {
  1322. if (!userId) {
  1323. throw new Error("null userId");
  1324. }
  1325. var url = self.getUrl("Users/" + userId + "/Password");
  1326. var postData = {
  1327. };
  1328. postData.resetPassword = true;
  1329. return self.ajax({
  1330. type: "POST",
  1331. url: url,
  1332. data: postData
  1333. });
  1334. };
  1335. /**
  1336. * Updates the server's configuration
  1337. * @param {Object} configuration
  1338. */
  1339. self.updateServerConfiguration = function (configuration) {
  1340. if (!configuration) {
  1341. throw new Error("null configuration");
  1342. }
  1343. var url = self.getUrl("System/Configuration");
  1344. return self.ajax({
  1345. type: "POST",
  1346. url: url,
  1347. data: JSON.stringify(configuration),
  1348. contentType: "application/json"
  1349. });
  1350. };
  1351. self.updateItem = function (item) {
  1352. if (!item) {
  1353. throw new Error("null item");
  1354. }
  1355. var url = self.getUrl("Items/" + item.Id);
  1356. return self.ajax({
  1357. type: "POST",
  1358. url: url,
  1359. data: JSON.stringify(item),
  1360. contentType: "application/json"
  1361. });
  1362. };
  1363. /**
  1364. * Updates plugin security info
  1365. */
  1366. self.updatePluginSecurityInfo = function (info) {
  1367. var url = self.getUrl("Plugins/SecurityInfo");
  1368. return self.ajax({
  1369. type: "POST",
  1370. url: url,
  1371. data: JSON.stringify(info),
  1372. contentType: "application/json"
  1373. });
  1374. };
  1375. /**
  1376. * Creates a user
  1377. * @param {Object} user
  1378. */
  1379. self.createUser = function (user) {
  1380. if (!user) {
  1381. throw new Error("null user");
  1382. }
  1383. var url = self.getUrl("Users");
  1384. return self.ajax({
  1385. type: "POST",
  1386. url: url,
  1387. data: JSON.stringify(user),
  1388. dataType: "json",
  1389. contentType: "application/json"
  1390. });
  1391. };
  1392. /**
  1393. * Updates a user
  1394. * @param {Object} user
  1395. */
  1396. self.updateUser = function (user) {
  1397. if (!user) {
  1398. throw new Error("null user");
  1399. }
  1400. var url = self.getUrl("Users/" + user.Id);
  1401. return self.ajax({
  1402. type: "POST",
  1403. url: url,
  1404. data: JSON.stringify(user),
  1405. contentType: "application/json"
  1406. });
  1407. };
  1408. /**
  1409. * Updates the Triggers for a ScheduledTask
  1410. * @param {String} id
  1411. * @param {Object} triggers
  1412. */
  1413. self.updateScheduledTaskTriggers = function (id, triggers) {
  1414. if (!id) {
  1415. throw new Error("null id");
  1416. }
  1417. if (!triggers) {
  1418. throw new Error("null triggers");
  1419. }
  1420. var url = self.getUrl("ScheduledTasks/" + id + "/Triggers");
  1421. return self.ajax({
  1422. type: "POST",
  1423. url: url,
  1424. data: JSON.stringify(triggers),
  1425. contentType: "application/json"
  1426. });
  1427. };
  1428. /**
  1429. * Updates a plugin's configuration
  1430. * @param {String} Id
  1431. * @param {Object} configuration
  1432. */
  1433. self.updatePluginConfiguration = function (id, configuration) {
  1434. if (!id) {
  1435. throw new Error("null Id");
  1436. }
  1437. if (!configuration) {
  1438. throw new Error("null configuration");
  1439. }
  1440. var url = self.getUrl("Plugins/" + id + "/Configuration");
  1441. return self.ajax({
  1442. type: "POST",
  1443. url: url,
  1444. data: JSON.stringify(configuration),
  1445. contentType: "application/json"
  1446. });
  1447. };
  1448. /**
  1449. * Gets items based on a query, typically for children of a folder
  1450. * @param {String} userId
  1451. * @param {Object} options
  1452. * Options accepts the following properties:
  1453. * itemId - Localize the search to a specific folder (root if omitted)
  1454. * startIndex - Use for paging
  1455. * limit - Use to limit results to a certain number of items
  1456. * filter - Specify one or more ItemFilters, comma delimeted (see server-side enum)
  1457. * sortBy - Specify an ItemSortBy (comma-delimeted list see server-side enum)
  1458. * sortOrder - ascending/descending
  1459. * fields - additional fields to include aside from basic info. This is a comma delimited list. See server-side enum ItemFields.
  1460. * index - the name of the dynamic, localized index function
  1461. * dynamicSortBy - the name of the dynamic localized sort function
  1462. * recursive - Whether or not the query should be recursive
  1463. * searchTerm - search term to use as a filter
  1464. */
  1465. self.getItems = function (userId, options) {
  1466. if (!userId) {
  1467. throw new Error("null userId");
  1468. }
  1469. var url = self.getUrl("Users/" + userId + "/Items", options);
  1470. return self.ajax({
  1471. type: "GET",
  1472. url: url,
  1473. dataType: "json"
  1474. });
  1475. };
  1476. /**
  1477. Gets artists from an item
  1478. */
  1479. self.getArtists = function (userId, options) {
  1480. if (!userId) {
  1481. throw new Error("null userId");
  1482. }
  1483. options = options || {};
  1484. options.userId = userId;
  1485. var url = self.getUrl("Artists", options);
  1486. return self.ajax({
  1487. type: "GET",
  1488. url: url,
  1489. dataType: "json"
  1490. });
  1491. };
  1492. /**
  1493. Gets genres from an item
  1494. */
  1495. self.getGenres = function (userId, options) {
  1496. if (!userId) {
  1497. throw new Error("null userId");
  1498. }
  1499. options = options || {};
  1500. options.userId = userId;
  1501. var url = self.getUrl("Genres", options);
  1502. return self.ajax({
  1503. type: "GET",
  1504. url: url,
  1505. dataType: "json"
  1506. });
  1507. };
  1508. self.getMusicGenres = function (userId, options) {
  1509. if (!userId) {
  1510. throw new Error("null userId");
  1511. }
  1512. options = options || {};
  1513. options.userId = userId;
  1514. var url = self.getUrl("MusicGenres", options);
  1515. return self.ajax({
  1516. type: "GET",
  1517. url: url,
  1518. dataType: "json"
  1519. });
  1520. };
  1521. /**
  1522. Gets people from an item
  1523. */
  1524. self.getPeople = function (userId, options) {
  1525. if (!userId) {
  1526. throw new Error("null userId");
  1527. }
  1528. options = options || {};
  1529. options.userId = userId;
  1530. var url = self.getUrl("Persons", options);
  1531. return self.ajax({
  1532. type: "GET",
  1533. url: url,
  1534. dataType: "json"
  1535. });
  1536. };
  1537. /**
  1538. Gets studios from an item
  1539. */
  1540. self.getStudios = function (userId, options) {
  1541. if (!userId) {
  1542. throw new Error("null userId");
  1543. }
  1544. options = options || {};
  1545. options.userId = userId;
  1546. var url = self.getUrl("Studios", options);
  1547. return self.ajax({
  1548. type: "GET",
  1549. url: url,
  1550. dataType: "json"
  1551. });
  1552. };
  1553. /**
  1554. * Gets local trailers for an item
  1555. */
  1556. self.getLocalTrailers = function (userId, itemId) {
  1557. if (!userId) {
  1558. throw new Error("null userId");
  1559. }
  1560. if (!itemId) {
  1561. throw new Error("null itemId");
  1562. }
  1563. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/LocalTrailers");
  1564. return self.ajax({
  1565. type: "GET",
  1566. url: url,
  1567. dataType: "json"
  1568. });
  1569. };
  1570. self.getAdditionalVideoParts = function (userId, itemId) {
  1571. if (!itemId) {
  1572. throw new Error("null itemId");
  1573. }
  1574. var options = {};
  1575. if (userId) {
  1576. options.userId = userId;
  1577. }
  1578. var url = self.getUrl("Videos/" + itemId + "/AdditionalParts", options);
  1579. return self.ajax({
  1580. type: "GET",
  1581. url: url,
  1582. dataType: "json"
  1583. });
  1584. };
  1585. /**
  1586. * Gets theme songs for an item
  1587. */
  1588. self.getThemeSongs = 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 + "/ThemeSongs", options);
  1597. return self.ajax({
  1598. type: "GET",
  1599. url: url,
  1600. dataType: "json"
  1601. });
  1602. };
  1603. self.getThemeVideos = function (userId, itemId) {
  1604. if (!itemId) {
  1605. throw new Error("null itemId");
  1606. }
  1607. var options = {};
  1608. if (userId) {
  1609. options.userId = userId;
  1610. }
  1611. var url = self.getUrl("Items/" + itemId + "/ThemeVideos", options);
  1612. return self.ajax({
  1613. type: "GET",
  1614. url: url,
  1615. dataType: "json"
  1616. });
  1617. };
  1618. self.getSearchHints = function (options) {
  1619. var url = self.getUrl("Search/Hints", options);
  1620. return self.ajax({
  1621. type: "GET",
  1622. url: url,
  1623. dataType: "json"
  1624. });
  1625. };
  1626. /**
  1627. * Gets special features for an item
  1628. */
  1629. self.getSpecialFeatures = function (userId, itemId) {
  1630. if (!userId) {
  1631. throw new Error("null userId");
  1632. }
  1633. if (!itemId) {
  1634. throw new Error("null itemId");
  1635. }
  1636. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/SpecialFeatures");
  1637. return self.ajax({
  1638. type: "GET",
  1639. url: url,
  1640. dataType: "json"
  1641. });
  1642. };
  1643. /**
  1644. * Marks an item as played or unplayed
  1645. * This should not be used to update playstate following playback.
  1646. * There are separate playstate check-in methods for that. This should be used for a
  1647. * separate option to reset playstate.
  1648. * @param {String} userId
  1649. * @param {String} itemId
  1650. * @param {Boolean} wasPlayed
  1651. */
  1652. self.updatePlayedStatus = function (userId, itemId, wasPlayed) {
  1653. if (!userId) {
  1654. throw new Error("null userId");
  1655. }
  1656. if (!itemId) {
  1657. throw new Error("null itemId");
  1658. }
  1659. var url = self.getUrl("Users/" + userId + "/PlayedItems/" + itemId);
  1660. var method = wasPlayed ? "POST" : "DELETE";
  1661. return self.ajax({
  1662. type: method,
  1663. url: url
  1664. });
  1665. };
  1666. /**
  1667. * Updates a user's favorite status for an item.
  1668. * @param {String} userId
  1669. * @param {String} itemId
  1670. * @param {Boolean} isFavorite
  1671. */
  1672. self.updateFavoriteStatus = function (userId, itemId, isFavorite) {
  1673. if (!userId) {
  1674. throw new Error("null userId");
  1675. }
  1676. if (!itemId) {
  1677. throw new Error("null itemId");
  1678. }
  1679. var url = self.getUrl("Users/" + userId + "/FavoriteItems/" + itemId);
  1680. var method = isFavorite ? "POST" : "DELETE";
  1681. return self.ajax({
  1682. type: method,
  1683. url: url
  1684. });
  1685. };
  1686. /**
  1687. * Updates a user's personal rating for an item
  1688. * @param {String} userId
  1689. * @param {String} itemId
  1690. * @param {Boolean} likes
  1691. */
  1692. self.updateUserItemRating = function (userId, itemId, likes) {
  1693. if (!userId) {
  1694. throw new Error("null userId");
  1695. }
  1696. if (!itemId) {
  1697. throw new Error("null itemId");
  1698. }
  1699. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating", {
  1700. likes: likes
  1701. });
  1702. return self.ajax({
  1703. type: "POST",
  1704. url: url
  1705. });
  1706. };
  1707. /**
  1708. * Updates a user's favorite status for an item by name.
  1709. * @param {String} userId
  1710. * @param {String} name
  1711. * @param {Boolean} isFavorite
  1712. */
  1713. self.updateFavoriteArtistStatus = function (userId, name, isFavorite) {
  1714. if (!userId) {
  1715. throw new Error("null userId");
  1716. }
  1717. if (!name) {
  1718. throw new Error("null name");
  1719. }
  1720. var url = self.getUrl("Users/" + userId + "/Favorites/Artists/" + self.encodeName(name));
  1721. var method = isFavorite ? "POST" : "DELETE";
  1722. return self.ajax({
  1723. type: method,
  1724. url: url
  1725. });
  1726. };
  1727. self.updateFavoritePersonStatus = function (userId, name, isFavorite) {
  1728. if (!userId) {
  1729. throw new Error("null userId");
  1730. }
  1731. if (!name) {
  1732. throw new Error("null name");
  1733. }
  1734. var url = self.getUrl("Users/" + userId + "/Favorites/Persons/" + self.encodeName(name));
  1735. var method = isFavorite ? "POST" : "DELETE";
  1736. return self.ajax({
  1737. type: method,
  1738. url: url
  1739. });
  1740. };
  1741. self.updateFavoriteStudioStatus = function (userId, name, isFavorite) {
  1742. if (!userId) {
  1743. throw new Error("null userId");
  1744. }
  1745. if (!name) {
  1746. throw new Error("null name");
  1747. }
  1748. var url = self.getUrl("Users/" + userId + "/Favorites/Studios/" + self.encodeName(name));
  1749. var method = isFavorite ? "POST" : "DELETE";
  1750. return self.ajax({
  1751. type: method,
  1752. url: url
  1753. });
  1754. };
  1755. self.updateFavoriteGenreStatus = function (userId, name, isFavorite) {
  1756. if (!userId) {
  1757. throw new Error("null userId");
  1758. }
  1759. if (!name) {
  1760. throw new Error("null name");
  1761. }
  1762. var url = self.getUrl("Users/" + userId + "/Favorites/Genres/" + self.encodeName(name));
  1763. var method = isFavorite ? "POST" : "DELETE";
  1764. return self.ajax({
  1765. type: method,
  1766. url: url
  1767. });
  1768. };
  1769. self.updateFavoriteMusicGenreStatus = function (userId, name, isFavorite) {
  1770. if (!userId) {
  1771. throw new Error("null userId");
  1772. }
  1773. if (!name) {
  1774. throw new Error("null name");
  1775. }
  1776. var url = self.getUrl("Users/" + userId + "/Favorites/MusicGenres/" + self.encodeName(name));
  1777. var method = isFavorite ? "POST" : "DELETE";
  1778. return self.ajax({
  1779. type: method,
  1780. url: url
  1781. });
  1782. };
  1783. /**
  1784. * Updates a user's rating for an item by name.
  1785. * @param {String} userId
  1786. * @param {String} name
  1787. * @param {Boolean} likes
  1788. */
  1789. self.updateArtistRating = 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/Artists/" + self.encodeName(name), {
  1797. likes: likes
  1798. });
  1799. return self.ajax({
  1800. type: "POST",
  1801. url: url
  1802. });
  1803. };
  1804. self.updatePersonRating = 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/Persons/" + self.encodeName(name), {
  1812. likes: likes
  1813. });
  1814. return self.ajax({
  1815. type: "POST",
  1816. url: url
  1817. });
  1818. };
  1819. self.updateStudioRating = 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/Studios/" + self.encodeName(name), {
  1827. likes: likes
  1828. });
  1829. return self.ajax({
  1830. type: "POST",
  1831. url: url
  1832. });
  1833. };
  1834. self.updateGenreRating = 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/Genres/" + self.encodeName(name), {
  1842. likes: likes
  1843. });
  1844. return self.ajax({
  1845. type: "POST",
  1846. url: url
  1847. });
  1848. };
  1849. self.updateMusicGenreRating = function (userId, name, likes) {
  1850. if (!userId) {
  1851. throw new Error("null userId");
  1852. }
  1853. if (!name) {
  1854. throw new Error("null name");
  1855. }
  1856. var url = self.getUrl("Users/" + userId + "/Ratings/MusicGenres/" + self.encodeName(name), {
  1857. likes: likes
  1858. });
  1859. return self.ajax({
  1860. type: "POST",
  1861. url: url
  1862. });
  1863. };
  1864. /**
  1865. * Clears a user's rating for an item by name.
  1866. * @param {String} userId
  1867. * @param {String} name
  1868. */
  1869. self.clearArtistRating = function (userId, name) {
  1870. if (!userId) {
  1871. throw new Error("null userId");
  1872. }
  1873. if (!name) {
  1874. throw new Error("null name");
  1875. }
  1876. var url = self.getUrl("Users/" + userId + "/Ratings/Artists/" + self.encodeName(name));
  1877. return self.ajax({
  1878. type: "DELETE",
  1879. url: url
  1880. });
  1881. };
  1882. self.clearPersonRating = function (userId, name) {
  1883. if (!userId) {
  1884. throw new Error("null userId");
  1885. }
  1886. if (!name) {
  1887. throw new Error("null name");
  1888. }
  1889. var url = self.getUrl("Users/" + userId + "/Ratings/Persons/" + self.encodeName(name));
  1890. return self.ajax({
  1891. type: "DELETE",
  1892. url: url
  1893. });
  1894. };
  1895. self.clearStudioRating = function (userId, name) {
  1896. if (!userId) {
  1897. throw new Error("null userId");
  1898. }
  1899. if (!name) {
  1900. throw new Error("null name");
  1901. }
  1902. var url = self.getUrl("Users/" + userId + "/Ratings/Studios/" + self.encodeName(name));
  1903. return self.ajax({
  1904. type: "DELETE",
  1905. url: url
  1906. });
  1907. };
  1908. self.clearGenreRating = function (userId, name) {
  1909. if (!userId) {
  1910. throw new Error("null userId");
  1911. }
  1912. if (!name) {
  1913. throw new Error("null name");
  1914. }
  1915. var url = self.getUrl("Users/" + userId + "/Ratings/Genres/" + self.encodeName(name));
  1916. return self.ajax({
  1917. type: "DELETE",
  1918. url: url
  1919. });
  1920. };
  1921. self.clearMusicGenreRating = function (userId, name) {
  1922. if (!userId) {
  1923. throw new Error("null userId");
  1924. }
  1925. if (!name) {
  1926. throw new Error("null name");
  1927. }
  1928. var url = self.getUrl("Users/" + userId + "/Ratings/MusicGenres/" + self.encodeName(name));
  1929. return self.ajax({
  1930. type: "DELETE",
  1931. url: url
  1932. });
  1933. };
  1934. self.getItemCounts = function (userId) {
  1935. var options = {};
  1936. if (userId) {
  1937. options.userId = userId;
  1938. }
  1939. var url = self.getUrl("Items/Counts", options);
  1940. return self.ajax({
  1941. type: "GET",
  1942. url: url,
  1943. dataType: "json"
  1944. });
  1945. };
  1946. /**
  1947. Gets a variety of item counts that a person appears in
  1948. */
  1949. self.getPersonItemCounts = function (userId, name) {
  1950. if (!userId) {
  1951. throw new Error("null userId");
  1952. }
  1953. if (!name) {
  1954. throw new Error("null name");
  1955. }
  1956. var url = self.getUrl("Persons/" + self.encodeName(name) + "/Counts", {
  1957. userId: userId
  1958. });
  1959. return self.ajax({
  1960. type: "GET",
  1961. url: url,
  1962. dataType: "json"
  1963. });
  1964. };
  1965. /**
  1966. Gets a variety of item counts that a genre appears in
  1967. */
  1968. self.getGenreItemCounts = function (userId, name) {
  1969. if (!userId) {
  1970. throw new Error("null userId");
  1971. }
  1972. if (!name) {
  1973. throw new Error("null name");
  1974. }
  1975. var url = self.getUrl("Genres/" + self.encodeName(name) + "/Counts", {
  1976. userId: userId
  1977. });
  1978. return self.ajax({
  1979. type: "GET",
  1980. url: url,
  1981. dataType: "json"
  1982. });
  1983. };
  1984. self.getMusicGenreItemCounts = function (userId, name) {
  1985. if (!userId) {
  1986. throw new Error("null userId");
  1987. }
  1988. if (!name) {
  1989. throw new Error("null name");
  1990. }
  1991. var url = self.getUrl("MusicGenres/" + self.encodeName(name) + "/Counts", {
  1992. userId: userId
  1993. });
  1994. return self.ajax({
  1995. type: "GET",
  1996. url: url,
  1997. dataType: "json"
  1998. });
  1999. };
  2000. /**
  2001. Gets a variety of item counts that an artist appears in
  2002. */
  2003. self.getArtistItemCounts = function (userId, name) {
  2004. if (!userId) {
  2005. throw new Error("null userId");
  2006. }
  2007. if (!name) {
  2008. throw new Error("null name");
  2009. }
  2010. var url = self.getUrl("Artists/" + self.encodeName(name) + "/Counts", {
  2011. userId: userId
  2012. });
  2013. return self.ajax({
  2014. type: "GET",
  2015. url: url,
  2016. dataType: "json"
  2017. });
  2018. };
  2019. /**
  2020. Gets a variety of item counts that a studio appears in
  2021. */
  2022. self.getStudioItemCounts = function (userId, name) {
  2023. if (!userId) {
  2024. throw new Error("null userId");
  2025. }
  2026. if (!name) {
  2027. throw new Error("null name");
  2028. }
  2029. var url = self.getUrl("Studios/" + self.encodeName(name) + "/Counts", {
  2030. userId: userId
  2031. });
  2032. return self.ajax({
  2033. type: "GET",
  2034. url: url,
  2035. dataType: "json"
  2036. });
  2037. };
  2038. /**
  2039. * Clears a user's personal rating for an item
  2040. * @param {String} userId
  2041. * @param {String} itemId
  2042. */
  2043. self.clearUserItemRating = function (userId, itemId) {
  2044. if (!userId) {
  2045. throw new Error("null userId");
  2046. }
  2047. if (!itemId) {
  2048. throw new Error("null itemId");
  2049. }
  2050. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating");
  2051. return self.ajax({
  2052. type: "DELETE",
  2053. url: url
  2054. });
  2055. };
  2056. /**
  2057. * Reports the user has started playing something
  2058. * @param {String} userId
  2059. * @param {String} itemId
  2060. */
  2061. self.reportPlaybackStart = function (userId, itemId) {
  2062. if (!userId) {
  2063. throw new Error("null userId");
  2064. }
  2065. if (!itemId) {
  2066. throw new Error("null itemId");
  2067. }
  2068. if (self.isWebSocketOpen()) {
  2069. var deferred = $.Deferred();
  2070. self.sendWebSocketMessage("PlaybackStart", itemId);
  2071. deferred.resolveWith(null, []);
  2072. return deferred.promise();
  2073. }
  2074. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId);
  2075. return self.ajax({
  2076. type: "POST",
  2077. url: url
  2078. });
  2079. };
  2080. /**
  2081. * Reports progress viewing an item
  2082. * @param {String} userId
  2083. * @param {String} itemId
  2084. */
  2085. self.reportPlaybackProgress = function (userId, itemId, positionTicks, isPaused) {
  2086. if (!userId) {
  2087. throw new Error("null userId");
  2088. }
  2089. if (!itemId) {
  2090. throw new Error("null itemId");
  2091. }
  2092. if (self.isWebSocketOpen()) {
  2093. var deferred = $.Deferred();
  2094. self.sendWebSocketMessage("PlaybackProgress", itemId + "|" + (positionTicks == null ? "" : positionTicks) + "|" + (isPaused == null ? "" : isPaused));
  2095. deferred.resolveWith(null, []);
  2096. return deferred.promise();
  2097. }
  2098. var params = {
  2099. };
  2100. if (positionTicks) {
  2101. params.positionTicks = positionTicks;
  2102. }
  2103. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId + "/Progress", params);
  2104. return self.ajax({
  2105. type: "POST",
  2106. url: url
  2107. });
  2108. };
  2109. /**
  2110. * Reports a user has stopped playing an item
  2111. * @param {String} userId
  2112. * @param {String} itemId
  2113. */
  2114. self.reportPlaybackStopped = function (userId, itemId, positionTicks) {
  2115. if (!userId) {
  2116. throw new Error("null userId");
  2117. }
  2118. if (!itemId) {
  2119. throw new Error("null itemId");
  2120. }
  2121. if (self.isWebSocketOpen()) {
  2122. var deferred = $.Deferred();
  2123. self.sendWebSocketMessage("PlaybackStopped", itemId + "|" + (positionTicks == null ? "" : positionTicks));
  2124. deferred.resolveWith(null, []);
  2125. return deferred.promise();
  2126. }
  2127. var params = {
  2128. };
  2129. if (positionTicks) {
  2130. params.positionTicks = positionTicks;
  2131. }
  2132. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId, params);
  2133. return self.ajax({
  2134. type: "DELETE",
  2135. url: url
  2136. });
  2137. };
  2138. self.sendBrowseCommand = function (sessionId, options) {
  2139. if (!sessionId) {
  2140. throw new Error("null sessionId");
  2141. }
  2142. if (!options) {
  2143. throw new Error("null options");
  2144. }
  2145. var url = self.getUrl("Sessions/" + sessionId + "/Viewing", options);
  2146. return self.ajax({
  2147. type: "POST",
  2148. url: url
  2149. });
  2150. };
  2151. self.sendPlayCommand = function (sessionId, options) {
  2152. if (!sessionId) {
  2153. throw new Error("null sessionId");
  2154. }
  2155. if (!options) {
  2156. throw new Error("null options");
  2157. }
  2158. var url = self.getUrl("Sessions/" + sessionId + "/Playing", options);
  2159. return self.ajax({
  2160. type: "POST",
  2161. url: url
  2162. });
  2163. };
  2164. }
  2165. }(jQuery, navigator, window.JSON, window.WebSocket, setTimeout);
  2166. /**
  2167. * Provides a friendly way to create an api client instance using information from the browser's current url
  2168. */
  2169. MediaBrowser.ApiClient.create = function (clientName) {
  2170. var loc = window.location;
  2171. return new MediaBrowser.ApiClient(loc.protocol, loc.hostname, loc.port, clientName);
  2172. };
  2173. /**
  2174. *
  2175. * Secure Hash Algorithm (SHA1)
  2176. * http://www.webtoolkit.info/
  2177. *
  2178. **/
  2179. MediaBrowser.SHA1 = function (msg) {
  2180. function rotate_left(n, s) {
  2181. var t4 = (n << s) | (n >>> (32 - s));
  2182. return t4;
  2183. }
  2184. function lsb_hex(val) {
  2185. var str = "";
  2186. var i;
  2187. var vh;
  2188. var vl;
  2189. for (i = 0; i <= 6; i += 2) {
  2190. vh = (val >>> (i * 4 + 4)) & 0x0f;
  2191. vl = (val >>> (i * 4)) & 0x0f;
  2192. str += vh.toString(16) + vl.toString(16);
  2193. }
  2194. return str;
  2195. }
  2196. function cvt_hex(val) {
  2197. var str = "";
  2198. var i;
  2199. var v;
  2200. for (i = 7; i >= 0; i--) {
  2201. v = (val >>> (i * 4)) & 0x0f;
  2202. str += v.toString(16);
  2203. }
  2204. return str;
  2205. }
  2206. function Utf8Encode(string) {
  2207. string = string.replace(/\r\n/g, "\n");
  2208. var utftext = "";
  2209. for (var n = 0; n < string.length; n++) {
  2210. var c = string.charCodeAt(n);
  2211. if (c < 128) {
  2212. utftext += String.fromCharCode(c);
  2213. }
  2214. else if ((c > 127) && (c < 2048)) {
  2215. utftext += String.fromCharCode((c >> 6) | 192);
  2216. utftext += String.fromCharCode((c & 63) | 128);
  2217. }
  2218. else {
  2219. utftext += String.fromCharCode((c >> 12) | 224);
  2220. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  2221. utftext += String.fromCharCode((c & 63) | 128);
  2222. }
  2223. }
  2224. return utftext;
  2225. }
  2226. var blockstart;
  2227. var i, j;
  2228. var W = new Array(80);
  2229. var H0 = 0x67452301;
  2230. var H1 = 0xEFCDAB89;
  2231. var H2 = 0x98BADCFE;
  2232. var H3 = 0x10325476;
  2233. var H4 = 0xC3D2E1F0;
  2234. var A, B, C, D, E;
  2235. var temp;
  2236. msg = Utf8Encode(msg);
  2237. var msg_len = msg.length;
  2238. var word_array = new Array();
  2239. for (i = 0; i < msg_len - 3; i += 4) {
  2240. j = msg.charCodeAt(i) << 24 | msg.charCodeAt(i + 1) << 16 |
  2241. msg.charCodeAt(i + 2) << 8 | msg.charCodeAt(i + 3);
  2242. word_array.push(j);
  2243. }
  2244. switch (msg_len % 4) {
  2245. case 0:
  2246. i = 0x080000000;
  2247. break;
  2248. case 1:
  2249. i = msg.charCodeAt(msg_len - 1) << 24 | 0x0800000;
  2250. break;
  2251. case 2:
  2252. i = msg.charCodeAt(msg_len - 2) << 24 | msg.charCodeAt(msg_len - 1) << 16 | 0x08000;
  2253. break;
  2254. case 3:
  2255. i = msg.charCodeAt(msg_len - 3) << 24 | msg.charCodeAt(msg_len - 2) << 16 | msg.charCodeAt(msg_len - 1) << 8 | 0x80;
  2256. break;
  2257. }
  2258. word_array.push(i);
  2259. while ((word_array.length % 16) != 14) word_array.push(0);
  2260. word_array.push(msg_len >>> 29);
  2261. word_array.push((msg_len << 3) & 0x0ffffffff);
  2262. for (blockstart = 0; blockstart < word_array.length; blockstart += 16) {
  2263. for (i = 0; i < 16; i++) W[i] = word_array[blockstart + i];
  2264. for (i = 16; i <= 79; i++) W[i] = rotate_left(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
  2265. A = H0;
  2266. B = H1;
  2267. C = H2;
  2268. D = H3;
  2269. E = H4;
  2270. for (i = 0; i <= 19; i++) {
  2271. temp = (rotate_left(A, 5) + ((B & C) | (~B & D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
  2272. E = D;
  2273. D = C;
  2274. C = rotate_left(B, 30);
  2275. B = A;
  2276. A = temp;
  2277. }
  2278. for (i = 20; i <= 39; i++) {
  2279. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
  2280. E = D;
  2281. D = C;
  2282. C = rotate_left(B, 30);
  2283. B = A;
  2284. A = temp;
  2285. }
  2286. for (i = 40; i <= 59; i++) {
  2287. temp = (rotate_left(A, 5) + ((B & C) | (B & D) | (C & D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
  2288. E = D;
  2289. D = C;
  2290. C = rotate_left(B, 30);
  2291. B = A;
  2292. A = temp;
  2293. }
  2294. for (i = 60; i <= 79; i++) {
  2295. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
  2296. E = D;
  2297. D = C;
  2298. C = rotate_left(B, 30);
  2299. B = A;
  2300. A = temp;
  2301. }
  2302. H0 = (H0 + A) & 0x0ffffffff;
  2303. H1 = (H1 + B) & 0x0ffffffff;
  2304. H2 = (H2 + C) & 0x0ffffffff;
  2305. H3 = (H3 + D) & 0x0ffffffff;
  2306. H4 = (H4 + E) & 0x0ffffffff;
  2307. }
  2308. var temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4);
  2309. return temp.toLowerCase();
  2310. };
  2311. (function (jQuery, window, undefined) {
  2312. "use strict";
  2313. var matched, browser;
  2314. jQuery.uaMatch = function (ua) {
  2315. ua = ua.toLowerCase();
  2316. var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
  2317. /(webkit)[ \/]([\w.]+)/.exec(ua) ||
  2318. /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
  2319. /(msie) ([\w.]+)/.exec(ua) ||
  2320. ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
  2321. [];
  2322. var platform_match = /(ipad)/.exec(ua) ||
  2323. /(iphone)/.exec(ua) ||
  2324. /(android)/.exec(ua) ||
  2325. [];
  2326. return {
  2327. browser: match[1] || "",
  2328. version: match[2] || "0",
  2329. platform: platform_match[0] || ""
  2330. };
  2331. };
  2332. matched = jQuery.uaMatch(window.navigator.userAgent);
  2333. browser = {};
  2334. if (matched.browser) {
  2335. browser[matched.browser] = true;
  2336. browser.version = matched.version;
  2337. }
  2338. if (matched.platform) {
  2339. browser[matched.platform] = true
  2340. }
  2341. // Chrome is Webkit, but Webkit is also Safari.
  2342. if (browser.chrome) {
  2343. browser.webkit = true;
  2344. } else if (browser.webkit) {
  2345. browser.safari = true;
  2346. }
  2347. jQuery.browser = browser;
  2348. })(jQuery, window);