BaseItem.cs 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Controller.Channels;
  3. using MediaBrowser.Controller.Collections;
  4. using MediaBrowser.Controller.Configuration;
  5. using MediaBrowser.Controller.Drawing;
  6. using MediaBrowser.Controller.Dto;
  7. using MediaBrowser.Controller.Library;
  8. using MediaBrowser.Controller.LiveTv;
  9. using MediaBrowser.Controller.Persistence;
  10. using MediaBrowser.Controller.Providers;
  11. using MediaBrowser.Model.Configuration;
  12. using MediaBrowser.Model.Dto;
  13. using MediaBrowser.Model.Entities;
  14. using MediaBrowser.Model.Library;
  15. using MediaBrowser.Model.Logging;
  16. using MediaBrowser.Model.Users;
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Globalization;
  20. using System.IO;
  21. using System.Linq;
  22. using System.Text;
  23. using System.Threading;
  24. using System.Threading.Tasks;
  25. using MediaBrowser.Controller.Dto;
  26. using MediaBrowser.Controller.Extensions;
  27. using MediaBrowser.Controller.IO;
  28. using MediaBrowser.Controller.MediaEncoding;
  29. using MediaBrowser.Controller.Sorting;
  30. using MediaBrowser.Model.Extensions;
  31. using MediaBrowser.Model.Globalization;
  32. using MediaBrowser.Model.IO;
  33. using MediaBrowser.Model.LiveTv;
  34. using MediaBrowser.Model.Providers;
  35. using MediaBrowser.Model.Querying;
  36. using MediaBrowser.Model.Serialization;
  37. using MediaBrowser.Model.MediaInfo;
  38. namespace MediaBrowser.Controller.Entities
  39. {
  40. /// <summary>
  41. /// Class BaseItem
  42. /// </summary>
  43. public abstract class BaseItem : IHasProviderIds, IHasLookupInfo<ItemLookupInfo>
  44. {
  45. protected static MetadataFields[] EmptyMetadataFieldsArray = new MetadataFields[] { };
  46. protected static MediaUrl[] EmptyMediaUrlArray = new MediaUrl[] { };
  47. protected static ItemImageInfo[] EmptyItemImageInfoArray = new ItemImageInfo[] { };
  48. public static readonly LinkedChild[] EmptyLinkedChildArray = new LinkedChild[] { };
  49. protected BaseItem()
  50. {
  51. ThemeSongIds = new Guid[] {};
  52. ThemeVideoIds = new Guid[] {};
  53. Tags = new string[] {};
  54. Genres = new string[] {};
  55. Studios = new string[] {};
  56. ProviderIds = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  57. LockedFields = EmptyMetadataFieldsArray;
  58. ImageInfos = EmptyItemImageInfoArray;
  59. ProductionLocations = new string[] {};
  60. RemoteTrailers = new MediaUrl[] { };
  61. ExtraIds = new Guid[] {};
  62. }
  63. public static readonly char[] SlugReplaceChars = { '?', '/', '&' };
  64. public static char SlugChar = '-';
  65. /// <summary>
  66. /// The supported image extensions
  67. /// </summary>
  68. public static readonly string[] SupportedImageExtensions = { ".png", ".jpg", ".jpeg", ".tbn", ".gif" };
  69. public static readonly List<string> SupportedImageExtensionsList = SupportedImageExtensions.ToList();
  70. /// <summary>
  71. /// The trailer folder name
  72. /// </summary>
  73. public static string TrailerFolderName = "trailers";
  74. public static string ThemeSongsFolderName = "theme-music";
  75. public static string ThemeSongFilename = "theme";
  76. public static string ThemeVideosFolderName = "backdrops";
  77. [IgnoreDataMember]
  78. public Guid[] ThemeSongIds { get; set; }
  79. [IgnoreDataMember]
  80. public Guid[] ThemeVideoIds { get; set; }
  81. [IgnoreDataMember]
  82. public string PreferredMetadataCountryCode { get; set; }
  83. [IgnoreDataMember]
  84. public string PreferredMetadataLanguage { get; set; }
  85. public long? Size { get; set; }
  86. public string Container { get; set; }
  87. [IgnoreDataMember]
  88. public string Tagline { get; set; }
  89. [IgnoreDataMember]
  90. public virtual ItemImageInfo[] ImageInfos { get; set; }
  91. [IgnoreDataMember]
  92. public bool IsVirtualItem { get; set; }
  93. /// <summary>
  94. /// Gets or sets the album.
  95. /// </summary>
  96. /// <value>The album.</value>
  97. [IgnoreDataMember]
  98. public string Album { get; set; }
  99. /// <summary>
  100. /// Gets or sets the channel identifier.
  101. /// </summary>
  102. /// <value>The channel identifier.</value>
  103. [IgnoreDataMember]
  104. public Guid ChannelId { get; set; }
  105. [IgnoreDataMember]
  106. public virtual bool SupportsAddingToPlaylist
  107. {
  108. get
  109. {
  110. return false;
  111. }
  112. }
  113. [IgnoreDataMember]
  114. public virtual bool AlwaysScanInternalMetadataPath
  115. {
  116. get { return false; }
  117. }
  118. /// <summary>
  119. /// Gets a value indicating whether this instance is in mixed folder.
  120. /// </summary>
  121. /// <value><c>true</c> if this instance is in mixed folder; otherwise, <c>false</c>.</value>
  122. [IgnoreDataMember]
  123. public bool IsInMixedFolder { get; set; }
  124. [IgnoreDataMember]
  125. public virtual bool SupportsPlayedStatus
  126. {
  127. get
  128. {
  129. return false;
  130. }
  131. }
  132. [IgnoreDataMember]
  133. public virtual bool SupportsPositionTicksResume
  134. {
  135. get
  136. {
  137. return false;
  138. }
  139. }
  140. [IgnoreDataMember]
  141. public virtual bool SupportsRemoteImageDownloading
  142. {
  143. get
  144. {
  145. return true;
  146. }
  147. }
  148. private string _name;
  149. /// <summary>
  150. /// Gets or sets the name.
  151. /// </summary>
  152. /// <value>The name.</value>
  153. [IgnoreDataMember]
  154. public virtual string Name
  155. {
  156. get
  157. {
  158. return _name;
  159. }
  160. set
  161. {
  162. _name = value;
  163. // lazy load this again
  164. _sortName = null;
  165. }
  166. }
  167. [IgnoreDataMember]
  168. public bool IsUnaired
  169. {
  170. get { return PremiereDate.HasValue && PremiereDate.Value.ToLocalTime().Date >= DateTime.Now.Date; }
  171. }
  172. [IgnoreDataMember]
  173. public int? TotalBitrate { get; set; }
  174. [IgnoreDataMember]
  175. public ExtraType? ExtraType { get; set; }
  176. [IgnoreDataMember]
  177. public bool IsThemeMedia
  178. {
  179. get
  180. {
  181. return ExtraType.HasValue && (ExtraType.Value == Model.Entities.ExtraType.ThemeSong || ExtraType.Value == Model.Entities.ExtraType.ThemeVideo);
  182. }
  183. }
  184. [IgnoreDataMember]
  185. public string OriginalTitle { get; set; }
  186. /// <summary>
  187. /// Gets or sets the id.
  188. /// </summary>
  189. /// <value>The id.</value>
  190. [IgnoreDataMember]
  191. public Guid Id { get; set; }
  192. [IgnoreDataMember]
  193. public Guid OwnerId { get; set; }
  194. /// <summary>
  195. /// Gets or sets the audio.
  196. /// </summary>
  197. /// <value>The audio.</value>
  198. [IgnoreDataMember]
  199. public ProgramAudio? Audio { get; set; }
  200. /// <summary>
  201. /// Return the id that should be used to key display prefs for this item.
  202. /// Default is based on the type for everything except actual generic folders.
  203. /// </summary>
  204. /// <value>The display prefs id.</value>
  205. [IgnoreDataMember]
  206. public virtual Guid DisplayPreferencesId
  207. {
  208. get
  209. {
  210. var thisType = GetType();
  211. return thisType == typeof(Folder) ? Id : thisType.FullName.GetMD5();
  212. }
  213. }
  214. /// <summary>
  215. /// Gets or sets the path.
  216. /// </summary>
  217. /// <value>The path.</value>
  218. [IgnoreDataMember]
  219. public virtual string Path { get; set; }
  220. [IgnoreDataMember]
  221. public virtual SourceType SourceType
  222. {
  223. get
  224. {
  225. if (!ChannelId.Equals(Guid.Empty))
  226. {
  227. return SourceType.Channel;
  228. }
  229. return SourceType.Library;
  230. }
  231. }
  232. /// <summary>
  233. /// Returns the folder containing the item.
  234. /// If the item is a folder, it returns the folder itself
  235. /// </summary>
  236. [IgnoreDataMember]
  237. public virtual string ContainingFolderPath
  238. {
  239. get
  240. {
  241. if (IsFolder)
  242. {
  243. return Path;
  244. }
  245. return FileSystem.GetDirectoryName(Path);
  246. }
  247. }
  248. /// <summary>
  249. /// Gets or sets the name of the service.
  250. /// </summary>
  251. /// <value>The name of the service.</value>
  252. [IgnoreDataMember]
  253. public string ServiceName { get; set; }
  254. /// <summary>
  255. /// If this content came from an external service, the id of the content on that service
  256. /// </summary>
  257. [IgnoreDataMember]
  258. public string ExternalId { get; set; }
  259. [IgnoreDataMember]
  260. public string ExternalSeriesId { get; set; }
  261. /// <summary>
  262. /// Gets or sets the etag.
  263. /// </summary>
  264. /// <value>The etag.</value>
  265. [IgnoreDataMember]
  266. public string ExternalEtag { get; set; }
  267. [IgnoreDataMember]
  268. public virtual bool IsHidden
  269. {
  270. get
  271. {
  272. return false;
  273. }
  274. }
  275. public BaseItem GetOwner()
  276. {
  277. var ownerId = OwnerId;
  278. return ownerId.Equals(Guid.Empty) ? null : LibraryManager.GetItemById(ownerId);
  279. }
  280. /// <summary>
  281. /// Gets or sets the type of the location.
  282. /// </summary>
  283. /// <value>The type of the location.</value>
  284. [IgnoreDataMember]
  285. public virtual LocationType LocationType
  286. {
  287. get
  288. {
  289. //if (IsOffline)
  290. //{
  291. // return LocationType.Offline;
  292. //}
  293. var path = Path;
  294. if (string.IsNullOrEmpty(path))
  295. {
  296. if (SourceType == SourceType.Channel)
  297. {
  298. return LocationType.Remote;
  299. }
  300. return LocationType.Virtual;
  301. }
  302. return FileSystem.IsPathFile(path) ? LocationType.FileSystem : LocationType.Remote;
  303. }
  304. }
  305. [IgnoreDataMember]
  306. public MediaProtocol? PathProtocol
  307. {
  308. get
  309. {
  310. var path = Path;
  311. if (string.IsNullOrEmpty(path))
  312. {
  313. return null;
  314. }
  315. return MediaSourceManager.GetPathProtocol(path);
  316. }
  317. }
  318. public bool IsPathProtocol(MediaProtocol protocol)
  319. {
  320. var current = PathProtocol;
  321. return current.HasValue && current.Value == protocol;
  322. }
  323. [IgnoreDataMember]
  324. public bool IsFileProtocol
  325. {
  326. get
  327. {
  328. return IsPathProtocol(MediaProtocol.File);
  329. }
  330. }
  331. [IgnoreDataMember]
  332. public bool HasPathProtocol
  333. {
  334. get
  335. {
  336. return PathProtocol.HasValue;
  337. }
  338. }
  339. [IgnoreDataMember]
  340. public virtual bool SupportsLocalMetadata
  341. {
  342. get
  343. {
  344. if (SourceType == SourceType.Channel)
  345. {
  346. return false;
  347. }
  348. return IsFileProtocol;
  349. }
  350. }
  351. [IgnoreDataMember]
  352. public virtual string FileNameWithoutExtension
  353. {
  354. get
  355. {
  356. if (IsFileProtocol)
  357. {
  358. return System.IO.Path.GetFileNameWithoutExtension(Path);
  359. }
  360. return null;
  361. }
  362. }
  363. [IgnoreDataMember]
  364. public virtual bool EnableAlphaNumericSorting
  365. {
  366. get
  367. {
  368. return true;
  369. }
  370. }
  371. private List<Tuple<StringBuilder, bool>> GetSortChunks(string s1)
  372. {
  373. var list = new List<Tuple<StringBuilder, bool>>();
  374. int thisMarker = 0, thisNumericChunk = 0;
  375. while (thisMarker < s1.Length)
  376. {
  377. if (thisMarker >= s1.Length)
  378. {
  379. break;
  380. }
  381. char thisCh = s1[thisMarker];
  382. StringBuilder thisChunk = new StringBuilder();
  383. while ((thisMarker < s1.Length) && (thisChunk.Length == 0 || SortHelper.InChunk(thisCh, thisChunk[0])))
  384. {
  385. thisChunk.Append(thisCh);
  386. thisMarker++;
  387. if (thisMarker < s1.Length)
  388. {
  389. thisCh = s1[thisMarker];
  390. }
  391. }
  392. var isNumeric = thisChunk.Length > 0 && char.IsDigit(thisChunk[0]);
  393. list.Add(new Tuple<StringBuilder, bool>(thisChunk, isNumeric));
  394. }
  395. return list;
  396. }
  397. /// <summary>
  398. /// This is just a helper for convenience
  399. /// </summary>
  400. /// <value>The primary image path.</value>
  401. [IgnoreDataMember]
  402. public string PrimaryImagePath
  403. {
  404. get { return this.GetImagePath(ImageType.Primary); }
  405. }
  406. public bool IsMetadataFetcherEnabled(LibraryOptions libraryOptions, string name)
  407. {
  408. if (SourceType == SourceType.Channel)
  409. {
  410. // hack alert
  411. return !EnableMediaSourceDisplay;
  412. }
  413. var typeOptions = libraryOptions.GetTypeOptions(GetType().Name);
  414. if (typeOptions != null)
  415. {
  416. return typeOptions.MetadataFetchers.Contains(name, StringComparer.OrdinalIgnoreCase);
  417. }
  418. if (!libraryOptions.EnableInternetProviders)
  419. {
  420. return false;
  421. }
  422. var itemConfig = ConfigurationManager.Configuration.MetadataOptions.FirstOrDefault(i => string.Equals(i.ItemType, GetType().Name, StringComparison.OrdinalIgnoreCase));
  423. return itemConfig == null || !itemConfig.DisabledMetadataFetchers.Contains(name, StringComparer.OrdinalIgnoreCase);
  424. }
  425. public bool IsImageFetcherEnabled(LibraryOptions libraryOptions, string name)
  426. {
  427. if (this is Channel)
  428. {
  429. // hack alert
  430. return true;
  431. }
  432. if (SourceType == SourceType.Channel)
  433. {
  434. // hack alert
  435. return !EnableMediaSourceDisplay;
  436. }
  437. var typeOptions = libraryOptions.GetTypeOptions(GetType().Name);
  438. if (typeOptions != null)
  439. {
  440. return typeOptions.ImageFetchers.Contains(name, StringComparer.OrdinalIgnoreCase);
  441. }
  442. if (!libraryOptions.EnableInternetProviders)
  443. {
  444. return false;
  445. }
  446. var itemConfig = ConfigurationManager.Configuration.MetadataOptions.FirstOrDefault(i => string.Equals(i.ItemType, GetType().Name, StringComparison.OrdinalIgnoreCase));
  447. return itemConfig == null || !itemConfig.DisabledImageFetchers.Contains(name, StringComparer.OrdinalIgnoreCase);
  448. }
  449. public virtual bool CanDelete()
  450. {
  451. if (SourceType == SourceType.Channel)
  452. {
  453. return ChannelManager.CanDelete(this);
  454. }
  455. return IsFileProtocol;
  456. }
  457. public virtual bool IsAuthorizedToDelete(User user, List<Folder> allCollectionFolders)
  458. {
  459. if (user.Policy.EnableContentDeletion)
  460. {
  461. return true;
  462. }
  463. var allowed = user.Policy.EnableContentDeletionFromFolders;
  464. if (SourceType == SourceType.Channel)
  465. {
  466. return allowed.Contains(ChannelId.ToString(""), StringComparer.OrdinalIgnoreCase);
  467. }
  468. else
  469. {
  470. var collectionFolders = LibraryManager.GetCollectionFolders(this, allCollectionFolders);
  471. foreach (var folder in collectionFolders)
  472. {
  473. if (allowed.Contains(folder.Id.ToString("N"), StringComparer.OrdinalIgnoreCase))
  474. {
  475. return true;
  476. }
  477. }
  478. }
  479. return false;
  480. }
  481. public bool CanDelete(User user, List<Folder> allCollectionFolders)
  482. {
  483. return CanDelete() && IsAuthorizedToDelete(user, allCollectionFolders);
  484. }
  485. public bool CanDelete(User user)
  486. {
  487. var allCollectionFolders = LibraryManager.GetUserRootFolder().Children.OfType<Folder>().ToList();
  488. return CanDelete(user, allCollectionFolders);
  489. }
  490. public virtual bool CanDownload()
  491. {
  492. return false;
  493. }
  494. public virtual bool IsAuthorizedToDownload(User user)
  495. {
  496. return user.Policy.EnableContentDownloading;
  497. }
  498. public bool CanDownload(User user)
  499. {
  500. return CanDownload() && IsAuthorizedToDownload(user);
  501. }
  502. /// <summary>
  503. /// Gets or sets the date created.
  504. /// </summary>
  505. /// <value>The date created.</value>
  506. [IgnoreDataMember]
  507. public DateTime DateCreated { get; set; }
  508. /// <summary>
  509. /// Gets or sets the date modified.
  510. /// </summary>
  511. /// <value>The date modified.</value>
  512. [IgnoreDataMember]
  513. public DateTime DateModified { get; set; }
  514. [IgnoreDataMember]
  515. public DateTime DateLastSaved { get; set; }
  516. [IgnoreDataMember]
  517. public DateTime DateLastRefreshed { get; set; }
  518. /// <summary>
  519. /// The logger
  520. /// </summary>
  521. public static ILogger Logger { get; set; }
  522. public static ILibraryManager LibraryManager { get; set; }
  523. public static IServerConfigurationManager ConfigurationManager { get; set; }
  524. public static IProviderManager ProviderManager { get; set; }
  525. public static ILocalizationManager LocalizationManager { get; set; }
  526. public static IItemRepository ItemRepository { get; set; }
  527. public static IFileSystem FileSystem { get; set; }
  528. public static IUserDataManager UserDataManager { get; set; }
  529. public static IChannelManager ChannelManager { get; set; }
  530. public static IMediaSourceManager MediaSourceManager { get; set; }
  531. /// <summary>
  532. /// Returns a <see cref="System.String" /> that represents this instance.
  533. /// </summary>
  534. /// <returns>A <see cref="System.String" /> that represents this instance.</returns>
  535. public override string ToString()
  536. {
  537. return Name;
  538. }
  539. [IgnoreDataMember]
  540. public bool IsLocked { get; set; }
  541. /// <summary>
  542. /// Gets or sets the locked fields.
  543. /// </summary>
  544. /// <value>The locked fields.</value>
  545. [IgnoreDataMember]
  546. public MetadataFields[] LockedFields { get; set; }
  547. /// <summary>
  548. /// Gets the type of the media.
  549. /// </summary>
  550. /// <value>The type of the media.</value>
  551. [IgnoreDataMember]
  552. public virtual string MediaType
  553. {
  554. get
  555. {
  556. return null;
  557. }
  558. }
  559. [IgnoreDataMember]
  560. public virtual string[] PhysicalLocations
  561. {
  562. get
  563. {
  564. if (!IsFileProtocol)
  565. {
  566. return new string[] { };
  567. }
  568. return new[] { Path };
  569. }
  570. }
  571. private string _forcedSortName;
  572. /// <summary>
  573. /// Gets or sets the name of the forced sort.
  574. /// </summary>
  575. /// <value>The name of the forced sort.</value>
  576. [IgnoreDataMember]
  577. public string ForcedSortName
  578. {
  579. get { return _forcedSortName; }
  580. set { _forcedSortName = value; _sortName = null; }
  581. }
  582. private string _sortName;
  583. /// <summary>
  584. /// Gets the name of the sort.
  585. /// </summary>
  586. /// <value>The name of the sort.</value>
  587. [IgnoreDataMember]
  588. public string SortName
  589. {
  590. get
  591. {
  592. if (_sortName == null)
  593. {
  594. if (!string.IsNullOrEmpty(ForcedSortName))
  595. {
  596. // Need the ToLower because that's what CreateSortName does
  597. _sortName = ModifySortChunks(ForcedSortName).ToLower();
  598. }
  599. else
  600. {
  601. _sortName = CreateSortName();
  602. }
  603. }
  604. return _sortName;
  605. }
  606. set
  607. {
  608. _sortName = value;
  609. }
  610. }
  611. public string GetInternalMetadataPath()
  612. {
  613. var basePath = ConfigurationManager.ApplicationPaths.InternalMetadataPath;
  614. return GetInternalMetadataPath(basePath);
  615. }
  616. protected virtual string GetInternalMetadataPath(string basePath)
  617. {
  618. if (SourceType == SourceType.Channel)
  619. {
  620. return System.IO.Path.Combine(basePath, "channels", ChannelId.ToString("N"), Id.ToString("N"));
  621. }
  622. var idString = Id.ToString("N");
  623. basePath = System.IO.Path.Combine(basePath, "library");
  624. return System.IO.Path.Combine(basePath, idString.Substring(0, 2), idString);
  625. }
  626. /// <summary>
  627. /// Creates the name of the sort.
  628. /// </summary>
  629. /// <returns>System.String.</returns>
  630. protected virtual string CreateSortName()
  631. {
  632. if (Name == null) return null; //some items may not have name filled in properly
  633. if (!EnableAlphaNumericSorting)
  634. {
  635. return Name.TrimStart();
  636. }
  637. var sortable = Name.Trim().ToLower();
  638. foreach (var removeChar in ConfigurationManager.Configuration.SortRemoveCharacters)
  639. {
  640. sortable = sortable.Replace(removeChar, string.Empty);
  641. }
  642. foreach (var replaceChar in ConfigurationManager.Configuration.SortReplaceCharacters)
  643. {
  644. sortable = sortable.Replace(replaceChar, " ");
  645. }
  646. foreach (var search in ConfigurationManager.Configuration.SortRemoveWords)
  647. {
  648. // Remove from beginning if a space follows
  649. if (sortable.StartsWith(search + " "))
  650. {
  651. sortable = sortable.Remove(0, search.Length + 1);
  652. }
  653. // Remove from middle if surrounded by spaces
  654. sortable = sortable.Replace(" " + search + " ", " ");
  655. // Remove from end if followed by a space
  656. if (sortable.EndsWith(" " + search))
  657. {
  658. sortable = sortable.Remove(sortable.Length - (search.Length + 1));
  659. }
  660. }
  661. return ModifySortChunks(sortable);
  662. }
  663. private string ModifySortChunks(string name)
  664. {
  665. var chunks = GetSortChunks(name);
  666. var builder = new StringBuilder();
  667. foreach (var chunk in chunks)
  668. {
  669. var chunkBuilder = chunk.Item1;
  670. // This chunk is numeric
  671. if (chunk.Item2)
  672. {
  673. while (chunkBuilder.Length < 10)
  674. {
  675. chunkBuilder.Insert(0, '0');
  676. }
  677. }
  678. builder.Append(chunkBuilder);
  679. }
  680. //Logger.Debug("ModifySortChunks Start: {0} End: {1}", name, builder.ToString());
  681. return builder.ToString().RemoveDiacritics();
  682. }
  683. [IgnoreDataMember]
  684. public bool EnableMediaSourceDisplay
  685. {
  686. get
  687. {
  688. if (SourceType == SourceType.Channel)
  689. {
  690. return ChannelManager.EnableMediaSourceDisplay(this);
  691. }
  692. return true;
  693. }
  694. }
  695. [IgnoreDataMember]
  696. public Guid ParentId { get; set; }
  697. /// <summary>
  698. /// Gets or sets the parent.
  699. /// </summary>
  700. /// <value>The parent.</value>
  701. [IgnoreDataMember]
  702. public Folder Parent
  703. {
  704. get { return GetParent() as Folder; }
  705. set
  706. {
  707. }
  708. }
  709. public void SetParent(Folder parent)
  710. {
  711. ParentId = parent == null ? Guid.Empty : parent.Id;
  712. }
  713. public BaseItem GetParent()
  714. {
  715. var parentId = ParentId;
  716. if (!parentId.Equals(Guid.Empty))
  717. {
  718. return LibraryManager.GetItemById(parentId);
  719. }
  720. return null;
  721. }
  722. public IEnumerable<BaseItem> GetParents()
  723. {
  724. var parent = GetParent();
  725. while (parent != null)
  726. {
  727. yield return parent;
  728. parent = parent.GetParent();
  729. }
  730. }
  731. /// <summary>
  732. /// Finds a parent of a given type
  733. /// </summary>
  734. /// <typeparam name="T"></typeparam>
  735. /// <returns>``0.</returns>
  736. public T FindParent<T>()
  737. where T : Folder
  738. {
  739. foreach (var parent in GetParents())
  740. {
  741. var item = parent as T;
  742. if (item != null)
  743. {
  744. return item;
  745. }
  746. }
  747. return null;
  748. }
  749. [IgnoreDataMember]
  750. public virtual Guid DisplayParentId
  751. {
  752. get
  753. {
  754. var parentId = ParentId;
  755. return parentId;
  756. }
  757. }
  758. [IgnoreDataMember]
  759. public BaseItem DisplayParent
  760. {
  761. get
  762. {
  763. var id = DisplayParentId;
  764. if (id.Equals(Guid.Empty))
  765. {
  766. return null;
  767. }
  768. return LibraryManager.GetItemById(id);
  769. }
  770. }
  771. /// <summary>
  772. /// When the item first debuted. For movies this could be premiere date, episodes would be first aired
  773. /// </summary>
  774. /// <value>The premiere date.</value>
  775. [IgnoreDataMember]
  776. public DateTime? PremiereDate { get; set; }
  777. /// <summary>
  778. /// Gets or sets the end date.
  779. /// </summary>
  780. /// <value>The end date.</value>
  781. [IgnoreDataMember]
  782. public DateTime? EndDate { get; set; }
  783. /// <summary>
  784. /// Gets or sets the official rating.
  785. /// </summary>
  786. /// <value>The official rating.</value>
  787. [IgnoreDataMember]
  788. public string OfficialRating { get; set; }
  789. [IgnoreDataMember]
  790. public int InheritedParentalRatingValue { get; set; }
  791. /// <summary>
  792. /// Gets or sets the critic rating.
  793. /// </summary>
  794. /// <value>The critic rating.</value>
  795. [IgnoreDataMember]
  796. public float? CriticRating { get; set; }
  797. /// <summary>
  798. /// Gets or sets the custom rating.
  799. /// </summary>
  800. /// <value>The custom rating.</value>
  801. [IgnoreDataMember]
  802. public string CustomRating { get; set; }
  803. /// <summary>
  804. /// Gets or sets the overview.
  805. /// </summary>
  806. /// <value>The overview.</value>
  807. [IgnoreDataMember]
  808. public string Overview { get; set; }
  809. /// <summary>
  810. /// Gets or sets the studios.
  811. /// </summary>
  812. /// <value>The studios.</value>
  813. [IgnoreDataMember]
  814. public string[] Studios { get; set; }
  815. /// <summary>
  816. /// Gets or sets the genres.
  817. /// </summary>
  818. /// <value>The genres.</value>
  819. [IgnoreDataMember]
  820. public string[] Genres { get; set; }
  821. /// <summary>
  822. /// Gets or sets the tags.
  823. /// </summary>
  824. /// <value>The tags.</value>
  825. [IgnoreDataMember]
  826. public string[] Tags { get; set; }
  827. [IgnoreDataMember]
  828. public string[] ProductionLocations { get; set; }
  829. /// <summary>
  830. /// Gets or sets the home page URL.
  831. /// </summary>
  832. /// <value>The home page URL.</value>
  833. [IgnoreDataMember]
  834. public string HomePageUrl { get; set; }
  835. /// <summary>
  836. /// Gets or sets the community rating.
  837. /// </summary>
  838. /// <value>The community rating.</value>
  839. [IgnoreDataMember]
  840. public float? CommunityRating { get; set; }
  841. /// <summary>
  842. /// Gets or sets the run time ticks.
  843. /// </summary>
  844. /// <value>The run time ticks.</value>
  845. [IgnoreDataMember]
  846. public long? RunTimeTicks { get; set; }
  847. /// <summary>
  848. /// Gets or sets the production year.
  849. /// </summary>
  850. /// <value>The production year.</value>
  851. [IgnoreDataMember]
  852. public int? ProductionYear { get; set; }
  853. /// <summary>
  854. /// If the item is part of a series, this is it's number in the series.
  855. /// This could be episode number, album track number, etc.
  856. /// </summary>
  857. /// <value>The index number.</value>
  858. [IgnoreDataMember]
  859. public int? IndexNumber { get; set; }
  860. /// <summary>
  861. /// For an episode this could be the season number, or for a song this could be the disc number.
  862. /// </summary>
  863. /// <value>The parent index number.</value>
  864. [IgnoreDataMember]
  865. public int? ParentIndexNumber { get; set; }
  866. [IgnoreDataMember]
  867. public virtual bool HasLocalAlternateVersions
  868. {
  869. get { return false; }
  870. }
  871. [IgnoreDataMember]
  872. public string OfficialRatingForComparison
  873. {
  874. get
  875. {
  876. var officialRating = OfficialRating;
  877. if (!string.IsNullOrEmpty(officialRating))
  878. {
  879. return officialRating;
  880. }
  881. var parent = DisplayParent;
  882. if (parent != null)
  883. {
  884. return parent.OfficialRatingForComparison;
  885. }
  886. return null;
  887. }
  888. }
  889. [IgnoreDataMember]
  890. public string CustomRatingForComparison
  891. {
  892. get
  893. {
  894. var customRating = CustomRating;
  895. if (!string.IsNullOrEmpty(customRating))
  896. {
  897. return customRating;
  898. }
  899. var parent = DisplayParent;
  900. if (parent != null)
  901. {
  902. return parent.CustomRatingForComparison;
  903. }
  904. return null;
  905. }
  906. }
  907. /// <summary>
  908. /// Gets the play access.
  909. /// </summary>
  910. /// <param name="user">The user.</param>
  911. /// <returns>PlayAccess.</returns>
  912. public PlayAccess GetPlayAccess(User user)
  913. {
  914. if (!user.Policy.EnableMediaPlayback)
  915. {
  916. return PlayAccess.None;
  917. }
  918. //if (!user.IsParentalScheduleAllowed())
  919. //{
  920. // return PlayAccess.None;
  921. //}
  922. return PlayAccess.Full;
  923. }
  924. public virtual List<MediaStream> GetMediaStreams()
  925. {
  926. return MediaSourceManager.GetMediaStreams(new MediaStreamQuery
  927. {
  928. ItemId = Id
  929. });
  930. }
  931. protected virtual bool IsActiveRecording()
  932. {
  933. return false;
  934. }
  935. public virtual List<MediaSourceInfo> GetMediaSources(bool enablePathSubstitution)
  936. {
  937. if (SourceType == SourceType.Channel)
  938. {
  939. var sources = ChannelManager.GetStaticMediaSources(this, CancellationToken.None)
  940. .ToList();
  941. if (sources.Count > 0)
  942. {
  943. return sources;
  944. }
  945. }
  946. var list = GetAllItemsForMediaSources();
  947. var result = list.Select(i => GetVersionInfo(enablePathSubstitution, i.Item1, i.Item2)).ToList();
  948. if (IsActiveRecording())
  949. {
  950. foreach (var mediaSource in result)
  951. {
  952. mediaSource.Type = MediaSourceType.Placeholder;
  953. }
  954. }
  955. return result.OrderBy(i =>
  956. {
  957. if (i.VideoType == VideoType.VideoFile)
  958. {
  959. return 0;
  960. }
  961. return 1;
  962. }).ThenBy(i => i.Video3DFormat.HasValue ? 1 : 0)
  963. .ThenByDescending(i =>
  964. {
  965. var stream = i.VideoStream;
  966. return stream == null || stream.Width == null ? 0 : stream.Width.Value;
  967. })
  968. .ToList();
  969. }
  970. protected virtual List<Tuple<BaseItem, MediaSourceType>> GetAllItemsForMediaSources()
  971. {
  972. return new List<Tuple<BaseItem, MediaSourceType>>();
  973. }
  974. private MediaSourceInfo GetVersionInfo(bool enablePathSubstitution, BaseItem item, MediaSourceType type)
  975. {
  976. if (item == null)
  977. {
  978. throw new ArgumentNullException("media");
  979. }
  980. var protocol = item.PathProtocol;
  981. var info = new MediaSourceInfo
  982. {
  983. Id = item.Id.ToString("N"),
  984. Protocol = protocol ?? MediaProtocol.File,
  985. MediaStreams = MediaSourceManager.GetMediaStreams(item.Id),
  986. Name = GetMediaSourceName(item),
  987. Path = enablePathSubstitution ? GetMappedPath(item, item.Path, protocol) : item.Path,
  988. RunTimeTicks = item.RunTimeTicks,
  989. Container = item.Container,
  990. Size = item.Size,
  991. Type = type
  992. };
  993. if (string.IsNullOrEmpty(info.Path))
  994. {
  995. info.Type = MediaSourceType.Placeholder;
  996. }
  997. if (info.Protocol == MediaProtocol.File)
  998. {
  999. info.ETag = item.DateModified.Ticks.ToString(CultureInfo.InvariantCulture).GetMD5().ToString("N");
  1000. }
  1001. var video = item as Video;
  1002. if (video != null)
  1003. {
  1004. info.IsoType = video.IsoType;
  1005. info.VideoType = video.VideoType;
  1006. info.Video3DFormat = video.Video3DFormat;
  1007. info.Timestamp = video.Timestamp;
  1008. if (video.IsShortcut)
  1009. {
  1010. info.IsRemote = true;
  1011. info.Path = video.ShortcutPath;
  1012. info.Protocol = MediaSourceManager.GetPathProtocol(info.Path);
  1013. }
  1014. if (string.IsNullOrEmpty(info.Container))
  1015. {
  1016. if (video.VideoType == VideoType.VideoFile || video.VideoType == VideoType.Iso)
  1017. {
  1018. if (protocol.HasValue && protocol.Value == MediaProtocol.File)
  1019. {
  1020. info.Container = System.IO.Path.GetExtension(item.Path).TrimStart('.');
  1021. }
  1022. }
  1023. }
  1024. }
  1025. if (string.IsNullOrEmpty(info.Container))
  1026. {
  1027. if (protocol.HasValue && protocol.Value == MediaProtocol.File)
  1028. {
  1029. info.Container = System.IO.Path.GetExtension(item.Path).TrimStart('.');
  1030. }
  1031. }
  1032. if (info.SupportsDirectStream && !string.IsNullOrEmpty(info.Path))
  1033. {
  1034. info.SupportsDirectStream = MediaSourceManager.SupportsDirectStream(info.Path, info.Protocol);
  1035. }
  1036. if (video != null && video.VideoType != VideoType.VideoFile)
  1037. {
  1038. info.SupportsDirectStream = false;
  1039. }
  1040. info.Bitrate = item.TotalBitrate;
  1041. info.InferTotalBitrate();
  1042. return info;
  1043. }
  1044. private string GetMediaSourceName(BaseItem item)
  1045. {
  1046. var terms = new List<string>();
  1047. var path = item.Path;
  1048. if (item.IsFileProtocol && !string.IsNullOrEmpty(path))
  1049. {
  1050. if (HasLocalAlternateVersions)
  1051. {
  1052. var displayName = System.IO.Path.GetFileNameWithoutExtension(path)
  1053. .Replace(System.IO.Path.GetFileName(ContainingFolderPath), string.Empty, StringComparison.OrdinalIgnoreCase)
  1054. .TrimStart(new char[] { ' ', '-' });
  1055. if (!string.IsNullOrEmpty(displayName))
  1056. {
  1057. terms.Add(displayName);
  1058. }
  1059. }
  1060. if (terms.Count == 0)
  1061. {
  1062. var displayName = System.IO.Path.GetFileNameWithoutExtension(path);
  1063. terms.Add(displayName);
  1064. }
  1065. }
  1066. if (terms.Count == 0)
  1067. {
  1068. terms.Add(item.Name);
  1069. }
  1070. var video = item as Video;
  1071. if (video != null)
  1072. {
  1073. if (video.Video3DFormat.HasValue)
  1074. {
  1075. terms.Add("3D");
  1076. }
  1077. if (video.VideoType == VideoType.BluRay)
  1078. {
  1079. terms.Add("Bluray");
  1080. }
  1081. else if (video.VideoType == VideoType.Dvd)
  1082. {
  1083. terms.Add("DVD");
  1084. }
  1085. else if (video.VideoType == VideoType.Iso)
  1086. {
  1087. if (video.IsoType.HasValue)
  1088. {
  1089. if (video.IsoType.Value == Model.Entities.IsoType.BluRay)
  1090. {
  1091. terms.Add("Bluray");
  1092. }
  1093. else if (video.IsoType.Value == Model.Entities.IsoType.Dvd)
  1094. {
  1095. terms.Add("DVD");
  1096. }
  1097. }
  1098. else
  1099. {
  1100. terms.Add("ISO");
  1101. }
  1102. }
  1103. }
  1104. return string.Join("/", terms.ToArray());
  1105. }
  1106. /// <summary>
  1107. /// Loads the theme songs.
  1108. /// </summary>
  1109. /// <returns>List{Audio.Audio}.</returns>
  1110. private static Audio.Audio[] LoadThemeSongs(List<FileSystemMetadata> fileSystemChildren, IDirectoryService directoryService)
  1111. {
  1112. var files = fileSystemChildren.Where(i => i.IsDirectory)
  1113. .Where(i => string.Equals(i.Name, ThemeSongsFolderName, StringComparison.OrdinalIgnoreCase))
  1114. .SelectMany(i => FileSystem.GetFiles(i.FullName))
  1115. .ToList();
  1116. // Support plex/xbmc convention
  1117. files.AddRange(fileSystemChildren
  1118. .Where(i => !i.IsDirectory && string.Equals(FileSystem.GetFileNameWithoutExtension(i), ThemeSongFilename, StringComparison.OrdinalIgnoreCase))
  1119. );
  1120. return LibraryManager.ResolvePaths(files, directoryService, null, new LibraryOptions())
  1121. .OfType<Audio.Audio>()
  1122. .Select(audio =>
  1123. {
  1124. // Try to retrieve it from the db. If we don't find it, use the resolved version
  1125. var dbItem = LibraryManager.GetItemById(audio.Id) as Audio.Audio;
  1126. if (dbItem != null)
  1127. {
  1128. audio = dbItem;
  1129. }
  1130. else
  1131. {
  1132. // item is new
  1133. audio.ExtraType = MediaBrowser.Model.Entities.ExtraType.ThemeSong;
  1134. }
  1135. return audio;
  1136. // Sort them so that the list can be easily compared for changes
  1137. }).OrderBy(i => i.Path).ToArray();
  1138. }
  1139. /// <summary>
  1140. /// Loads the video backdrops.
  1141. /// </summary>
  1142. /// <returns>List{Video}.</returns>
  1143. private static Video[] LoadThemeVideos(IEnumerable<FileSystemMetadata> fileSystemChildren, IDirectoryService directoryService)
  1144. {
  1145. var files = fileSystemChildren.Where(i => i.IsDirectory)
  1146. .Where(i => string.Equals(i.Name, ThemeVideosFolderName, StringComparison.OrdinalIgnoreCase))
  1147. .SelectMany(i => FileSystem.GetFiles(i.FullName));
  1148. return LibraryManager.ResolvePaths(files, directoryService, null, new LibraryOptions())
  1149. .OfType<Video>()
  1150. .Select(item =>
  1151. {
  1152. // Try to retrieve it from the db. If we don't find it, use the resolved version
  1153. var dbItem = LibraryManager.GetItemById(item.Id) as Video;
  1154. if (dbItem != null)
  1155. {
  1156. item = dbItem;
  1157. }
  1158. else
  1159. {
  1160. // item is new
  1161. item.ExtraType = MediaBrowser.Model.Entities.ExtraType.ThemeVideo;
  1162. }
  1163. return item;
  1164. // Sort them so that the list can be easily compared for changes
  1165. }).OrderBy(i => i.Path).ToArray();
  1166. }
  1167. public Task RefreshMetadata(CancellationToken cancellationToken)
  1168. {
  1169. return RefreshMetadata(new MetadataRefreshOptions(new DirectoryService(Logger, FileSystem)), cancellationToken);
  1170. }
  1171. protected virtual void TriggerOnRefreshStart()
  1172. {
  1173. }
  1174. protected virtual void TriggerOnRefreshComplete()
  1175. {
  1176. }
  1177. /// <summary>
  1178. /// Overrides the base implementation to refresh metadata for local trailers
  1179. /// </summary>
  1180. /// <param name="options">The options.</param>
  1181. /// <param name="cancellationToken">The cancellation token.</param>
  1182. /// <returns>true if a provider reports we changed</returns>
  1183. public async Task<ItemUpdateType> RefreshMetadata(MetadataRefreshOptions options, CancellationToken cancellationToken)
  1184. {
  1185. TriggerOnRefreshStart();
  1186. var requiresSave = false;
  1187. if (SupportsOwnedItems)
  1188. {
  1189. try
  1190. {
  1191. var files = IsFileProtocol ?
  1192. GetFileSystemChildren(options.DirectoryService).ToList() :
  1193. new List<FileSystemMetadata>();
  1194. var ownedItemsChanged = await RefreshedOwnedItems(options, files, cancellationToken).ConfigureAwait(false);
  1195. if (ownedItemsChanged)
  1196. {
  1197. requiresSave = true;
  1198. }
  1199. }
  1200. catch (Exception ex)
  1201. {
  1202. Logger.ErrorException("Error refreshing owned items for {0}", ex, Path ?? Name);
  1203. }
  1204. }
  1205. try
  1206. {
  1207. var refreshOptions = requiresSave
  1208. ? new MetadataRefreshOptions(options)
  1209. {
  1210. ForceSave = true
  1211. }
  1212. : options;
  1213. return await ProviderManager.RefreshSingleItem(this, refreshOptions, cancellationToken).ConfigureAwait(false);
  1214. }
  1215. finally
  1216. {
  1217. TriggerOnRefreshComplete();
  1218. }
  1219. }
  1220. [IgnoreDataMember]
  1221. protected virtual bool SupportsOwnedItems
  1222. {
  1223. get { return !ParentId.Equals(Guid.Empty) && IsFileProtocol; }
  1224. }
  1225. [IgnoreDataMember]
  1226. public virtual bool SupportsPeople
  1227. {
  1228. get { return false; }
  1229. }
  1230. [IgnoreDataMember]
  1231. public virtual bool SupportsThemeMedia
  1232. {
  1233. get { return false; }
  1234. }
  1235. /// <summary>
  1236. /// Refreshes owned items such as trailers, theme videos, special features, etc.
  1237. /// Returns true or false indicating if changes were found.
  1238. /// </summary>
  1239. /// <param name="options"></param>
  1240. /// <param name="fileSystemChildren"></param>
  1241. /// <param name="cancellationToken"></param>
  1242. /// <returns></returns>
  1243. protected virtual async Task<bool> RefreshedOwnedItems(MetadataRefreshOptions options, List<FileSystemMetadata> fileSystemChildren, CancellationToken cancellationToken)
  1244. {
  1245. var themeSongsChanged = false;
  1246. var themeVideosChanged = false;
  1247. var localTrailersChanged = false;
  1248. if (IsFileProtocol && SupportsOwnedItems)
  1249. {
  1250. if (SupportsThemeMedia)
  1251. {
  1252. if (!IsInMixedFolder)
  1253. {
  1254. themeSongsChanged = await RefreshThemeSongs(this, options, fileSystemChildren, cancellationToken).ConfigureAwait(false);
  1255. themeVideosChanged = await RefreshThemeVideos(this, options, fileSystemChildren, cancellationToken).ConfigureAwait(false);
  1256. }
  1257. }
  1258. var hasTrailers = this as IHasTrailers;
  1259. if (hasTrailers != null)
  1260. {
  1261. localTrailersChanged = await RefreshLocalTrailers(hasTrailers, options, fileSystemChildren, cancellationToken).ConfigureAwait(false);
  1262. }
  1263. }
  1264. return themeSongsChanged || themeVideosChanged || localTrailersChanged;
  1265. }
  1266. protected virtual FileSystemMetadata[] GetFileSystemChildren(IDirectoryService directoryService)
  1267. {
  1268. var path = ContainingFolderPath;
  1269. return directoryService.GetFileSystemEntries(path);
  1270. }
  1271. private async Task<bool> RefreshLocalTrailers(IHasTrailers item, MetadataRefreshOptions options, List<FileSystemMetadata> fileSystemChildren, CancellationToken cancellationToken)
  1272. {
  1273. var newItems = LibraryManager.FindTrailers(this, fileSystemChildren, options.DirectoryService).ToList();
  1274. var newItemIds = newItems.Select(i => i.Id).ToArray();
  1275. var itemsChanged = !item.LocalTrailerIds.SequenceEqual(newItemIds);
  1276. var ownerId = item.Id;
  1277. var tasks = newItems.Select(i =>
  1278. {
  1279. var subOptions = new MetadataRefreshOptions(options);
  1280. if (!i.ExtraType.HasValue ||
  1281. i.ExtraType.Value != Model.Entities.ExtraType.Trailer ||
  1282. i.OwnerId != ownerId ||
  1283. !i.ParentId.Equals(Guid.Empty))
  1284. {
  1285. i.ExtraType = Model.Entities.ExtraType.Trailer;
  1286. i.OwnerId = ownerId;
  1287. i.ParentId = Guid.Empty;
  1288. subOptions.ForceSave = true;
  1289. }
  1290. return RefreshMetadataForOwnedItem(i, true, subOptions, cancellationToken);
  1291. });
  1292. await Task.WhenAll(tasks).ConfigureAwait(false);
  1293. item.LocalTrailerIds = newItemIds;
  1294. return itemsChanged;
  1295. }
  1296. private async Task<bool> RefreshThemeVideos(BaseItem item, MetadataRefreshOptions options, IEnumerable<FileSystemMetadata> fileSystemChildren, CancellationToken cancellationToken)
  1297. {
  1298. var newThemeVideos = LoadThemeVideos(fileSystemChildren, options.DirectoryService);
  1299. var newThemeVideoIds = newThemeVideos.Select(i => i.Id).ToArray();
  1300. var themeVideosChanged = !item.ThemeVideoIds.SequenceEqual(newThemeVideoIds);
  1301. var ownerId = item.Id;
  1302. var tasks = newThemeVideos.Select(i =>
  1303. {
  1304. var subOptions = new MetadataRefreshOptions(options);
  1305. if (!i.ExtraType.HasValue ||
  1306. i.ExtraType.Value != Model.Entities.ExtraType.ThemeVideo ||
  1307. i.OwnerId != ownerId ||
  1308. !i.ParentId.Equals(Guid.Empty))
  1309. {
  1310. i.ExtraType = Model.Entities.ExtraType.ThemeVideo;
  1311. i.OwnerId = ownerId;
  1312. i.ParentId = Guid.Empty;
  1313. subOptions.ForceSave = true;
  1314. }
  1315. return RefreshMetadataForOwnedItem(i, true, subOptions, cancellationToken);
  1316. });
  1317. await Task.WhenAll(tasks).ConfigureAwait(false);
  1318. item.ThemeVideoIds = newThemeVideoIds;
  1319. return themeVideosChanged;
  1320. }
  1321. /// <summary>
  1322. /// Refreshes the theme songs.
  1323. /// </summary>
  1324. private async Task<bool> RefreshThemeSongs(BaseItem item, MetadataRefreshOptions options, List<FileSystemMetadata> fileSystemChildren, CancellationToken cancellationToken)
  1325. {
  1326. var newThemeSongs = LoadThemeSongs(fileSystemChildren, options.DirectoryService);
  1327. var newThemeSongIds = newThemeSongs.Select(i => i.Id).ToArray();
  1328. var themeSongsChanged = !item.ThemeSongIds.SequenceEqual(newThemeSongIds);
  1329. var ownerId = item.Id;
  1330. var tasks = newThemeSongs.Select(i =>
  1331. {
  1332. var subOptions = new MetadataRefreshOptions(options);
  1333. if (!i.ExtraType.HasValue ||
  1334. i.ExtraType.Value != Model.Entities.ExtraType.ThemeSong ||
  1335. i.OwnerId != ownerId ||
  1336. !i.ParentId.Equals(Guid.Empty))
  1337. {
  1338. i.ExtraType = Model.Entities.ExtraType.ThemeSong;
  1339. i.OwnerId = ownerId;
  1340. i.ParentId = Guid.Empty;
  1341. subOptions.ForceSave = true;
  1342. }
  1343. return RefreshMetadataForOwnedItem(i, true, subOptions, cancellationToken);
  1344. });
  1345. await Task.WhenAll(tasks).ConfigureAwait(false);
  1346. item.ThemeSongIds = newThemeSongIds;
  1347. return themeSongsChanged;
  1348. }
  1349. /// <summary>
  1350. /// Gets or sets the provider ids.
  1351. /// </summary>
  1352. /// <value>The provider ids.</value>
  1353. [IgnoreDataMember]
  1354. public Dictionary<string, string> ProviderIds { get; set; }
  1355. [IgnoreDataMember]
  1356. public virtual Folder LatestItemsIndexContainer
  1357. {
  1358. get { return null; }
  1359. }
  1360. public virtual double GetDefaultPrimaryImageAspectRatio()
  1361. {
  1362. return 0;
  1363. }
  1364. public virtual string CreatePresentationUniqueKey()
  1365. {
  1366. return Id.ToString("N");
  1367. }
  1368. [IgnoreDataMember]
  1369. public string PresentationUniqueKey { get; set; }
  1370. public string GetPresentationUniqueKey()
  1371. {
  1372. return PresentationUniqueKey ?? CreatePresentationUniqueKey();
  1373. }
  1374. public virtual bool RequiresRefresh()
  1375. {
  1376. return false;
  1377. }
  1378. public virtual List<string> GetUserDataKeys()
  1379. {
  1380. var list = new List<string>();
  1381. if (SourceType == SourceType.Channel)
  1382. {
  1383. if (!string.IsNullOrEmpty(ExternalId))
  1384. {
  1385. list.Add(ExternalId);
  1386. }
  1387. }
  1388. list.Add(Id.ToString());
  1389. return list;
  1390. }
  1391. internal virtual ItemUpdateType UpdateFromResolvedItem(BaseItem newItem)
  1392. {
  1393. var updateType = ItemUpdateType.None;
  1394. if (IsInMixedFolder != newItem.IsInMixedFolder)
  1395. {
  1396. IsInMixedFolder = newItem.IsInMixedFolder;
  1397. updateType |= ItemUpdateType.MetadataImport;
  1398. }
  1399. return updateType;
  1400. }
  1401. public void AfterMetadataRefresh()
  1402. {
  1403. _sortName = null;
  1404. }
  1405. /// <summary>
  1406. /// Gets the preferred metadata language.
  1407. /// </summary>
  1408. /// <returns>System.String.</returns>
  1409. public string GetPreferredMetadataLanguage()
  1410. {
  1411. string lang = PreferredMetadataLanguage;
  1412. if (string.IsNullOrEmpty(lang))
  1413. {
  1414. lang = GetParents()
  1415. .Select(i => i.PreferredMetadataLanguage)
  1416. .FirstOrDefault(i => !string.IsNullOrEmpty(i));
  1417. }
  1418. if (string.IsNullOrEmpty(lang))
  1419. {
  1420. lang = LibraryManager.GetCollectionFolders(this)
  1421. .Select(i => i.PreferredMetadataLanguage)
  1422. .FirstOrDefault(i => !string.IsNullOrEmpty(i));
  1423. }
  1424. if (string.IsNullOrEmpty(lang))
  1425. {
  1426. lang = LibraryManager.GetLibraryOptions(this).PreferredMetadataLanguage;
  1427. }
  1428. if (string.IsNullOrEmpty(lang))
  1429. {
  1430. lang = ConfigurationManager.Configuration.PreferredMetadataLanguage;
  1431. }
  1432. return lang;
  1433. }
  1434. /// <summary>
  1435. /// Gets the preferred metadata language.
  1436. /// </summary>
  1437. /// <returns>System.String.</returns>
  1438. public string GetPreferredMetadataCountryCode()
  1439. {
  1440. string lang = PreferredMetadataCountryCode;
  1441. if (string.IsNullOrEmpty(lang))
  1442. {
  1443. lang = GetParents()
  1444. .Select(i => i.PreferredMetadataCountryCode)
  1445. .FirstOrDefault(i => !string.IsNullOrEmpty(i));
  1446. }
  1447. if (string.IsNullOrEmpty(lang))
  1448. {
  1449. lang = LibraryManager.GetCollectionFolders(this)
  1450. .Select(i => i.PreferredMetadataCountryCode)
  1451. .FirstOrDefault(i => !string.IsNullOrEmpty(i));
  1452. }
  1453. if (string.IsNullOrEmpty(lang))
  1454. {
  1455. lang = LibraryManager.GetLibraryOptions(this).MetadataCountryCode;
  1456. }
  1457. if (string.IsNullOrEmpty(lang))
  1458. {
  1459. lang = ConfigurationManager.Configuration.MetadataCountryCode;
  1460. }
  1461. return lang;
  1462. }
  1463. public virtual bool IsSaveLocalMetadataEnabled()
  1464. {
  1465. if (SourceType == SourceType.Channel)
  1466. {
  1467. return false;
  1468. }
  1469. var libraryOptions = LibraryManager.GetLibraryOptions(this);
  1470. return libraryOptions.SaveLocalMetadata;
  1471. }
  1472. /// <summary>
  1473. /// Determines if a given user has access to this item
  1474. /// </summary>
  1475. /// <param name="user">The user.</param>
  1476. /// <returns><c>true</c> if [is parental allowed] [the specified user]; otherwise, <c>false</c>.</returns>
  1477. /// <exception cref="System.ArgumentNullException">user</exception>
  1478. public bool IsParentalAllowed(User user)
  1479. {
  1480. if (user == null)
  1481. {
  1482. throw new ArgumentNullException("user");
  1483. }
  1484. if (!IsVisibleViaTags(user))
  1485. {
  1486. return false;
  1487. }
  1488. var maxAllowedRating = user.Policy.MaxParentalRating;
  1489. if (maxAllowedRating == null)
  1490. {
  1491. return true;
  1492. }
  1493. var rating = CustomRatingForComparison;
  1494. if (string.IsNullOrEmpty(rating))
  1495. {
  1496. rating = OfficialRatingForComparison;
  1497. }
  1498. if (string.IsNullOrEmpty(rating))
  1499. {
  1500. return !GetBlockUnratedValue(user.Policy);
  1501. }
  1502. var value = LocalizationManager.GetRatingLevel(rating);
  1503. // Could not determine the integer value
  1504. if (!value.HasValue)
  1505. {
  1506. var isAllowed = !GetBlockUnratedValue(user.Policy);
  1507. if (!isAllowed)
  1508. {
  1509. Logger.Debug("{0} has an unrecognized parental rating of {1}.", Name, rating);
  1510. }
  1511. return isAllowed;
  1512. }
  1513. return value.Value <= maxAllowedRating.Value;
  1514. }
  1515. public int? GetParentalRatingValue()
  1516. {
  1517. var rating = CustomRating;
  1518. if (string.IsNullOrEmpty(rating))
  1519. {
  1520. rating = OfficialRating;
  1521. }
  1522. if (string.IsNullOrEmpty(rating))
  1523. {
  1524. return null;
  1525. }
  1526. return LocalizationManager.GetRatingLevel(rating);
  1527. }
  1528. public int? GetInheritedParentalRatingValue()
  1529. {
  1530. var rating = CustomRatingForComparison;
  1531. if (string.IsNullOrEmpty(rating))
  1532. {
  1533. rating = OfficialRatingForComparison;
  1534. }
  1535. if (string.IsNullOrEmpty(rating))
  1536. {
  1537. return null;
  1538. }
  1539. return LocalizationManager.GetRatingLevel(rating);
  1540. }
  1541. public List<string> GetInheritedTags()
  1542. {
  1543. var list = new List<string>();
  1544. list.AddRange(Tags);
  1545. foreach (var parent in GetParents())
  1546. {
  1547. list.AddRange(parent.Tags);
  1548. }
  1549. return list.Distinct(StringComparer.OrdinalIgnoreCase).ToList();
  1550. }
  1551. private bool IsVisibleViaTags(User user)
  1552. {
  1553. var policy = user.Policy;
  1554. if (policy.BlockedTags.Any(i => Tags.Contains(i, StringComparer.OrdinalIgnoreCase)))
  1555. {
  1556. return false;
  1557. }
  1558. return true;
  1559. }
  1560. protected virtual bool IsAllowTagFilterEnforced()
  1561. {
  1562. return true;
  1563. }
  1564. public virtual UnratedItem GetBlockUnratedType()
  1565. {
  1566. if (SourceType == SourceType.Channel)
  1567. {
  1568. return UnratedItem.ChannelContent;
  1569. }
  1570. return UnratedItem.Other;
  1571. }
  1572. /// <summary>
  1573. /// Gets the block unrated value.
  1574. /// </summary>
  1575. /// <param name="config">The configuration.</param>
  1576. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  1577. protected virtual bool GetBlockUnratedValue(UserPolicy config)
  1578. {
  1579. // Don't block plain folders that are unrated. Let the media underneath get blocked
  1580. // Special folders like series and albums will override this method.
  1581. if (IsFolder)
  1582. {
  1583. return false;
  1584. }
  1585. if (this is IItemByName)
  1586. {
  1587. return false;
  1588. }
  1589. return config.BlockUnratedItems.Contains(GetBlockUnratedType());
  1590. }
  1591. /// <summary>
  1592. /// Determines if this folder should be visible to a given user.
  1593. /// Default is just parental allowed. Can be overridden for more functionality.
  1594. /// </summary>
  1595. /// <param name="user">The user.</param>
  1596. /// <returns><c>true</c> if the specified user is visible; otherwise, <c>false</c>.</returns>
  1597. /// <exception cref="System.ArgumentNullException">user</exception>
  1598. public virtual bool IsVisible(User user)
  1599. {
  1600. if (user == null)
  1601. {
  1602. throw new ArgumentNullException("user");
  1603. }
  1604. return IsParentalAllowed(user);
  1605. }
  1606. public virtual bool IsVisibleStandalone(User user)
  1607. {
  1608. if (SourceType == SourceType.Channel)
  1609. {
  1610. return IsVisibleStandaloneInternal(user, false) && Channel.IsChannelVisible(this, user);
  1611. }
  1612. return IsVisibleStandaloneInternal(user, true);
  1613. }
  1614. [IgnoreDataMember]
  1615. public virtual bool SupportsInheritedParentImages
  1616. {
  1617. get { return false; }
  1618. }
  1619. protected bool IsVisibleStandaloneInternal(User user, bool checkFolders)
  1620. {
  1621. if (!IsVisible(user))
  1622. {
  1623. return false;
  1624. }
  1625. if (GetParents().Any(i => !i.IsVisible(user)))
  1626. {
  1627. return false;
  1628. }
  1629. if (checkFolders)
  1630. {
  1631. var topParent = GetParents().LastOrDefault() ?? this;
  1632. if (string.IsNullOrEmpty(topParent.Path))
  1633. {
  1634. return true;
  1635. }
  1636. var itemCollectionFolders = LibraryManager.GetCollectionFolders(this).Select(i => i.Id).ToList();
  1637. if (itemCollectionFolders.Count > 0)
  1638. {
  1639. var userCollectionFolders = LibraryManager.GetUserRootFolder().GetChildren(user, true).Select(i => i.Id).ToList();
  1640. if (!itemCollectionFolders.Any(userCollectionFolders.Contains))
  1641. {
  1642. return false;
  1643. }
  1644. }
  1645. }
  1646. return true;
  1647. }
  1648. /// <summary>
  1649. /// Gets a value indicating whether this instance is folder.
  1650. /// </summary>
  1651. /// <value><c>true</c> if this instance is folder; otherwise, <c>false</c>.</value>
  1652. [IgnoreDataMember]
  1653. public virtual bool IsFolder
  1654. {
  1655. get
  1656. {
  1657. return false;
  1658. }
  1659. }
  1660. [IgnoreDataMember]
  1661. public virtual bool IsDisplayedAsFolder
  1662. {
  1663. get
  1664. {
  1665. return false;
  1666. }
  1667. }
  1668. public virtual string GetClientTypeName()
  1669. {
  1670. if (IsFolder && SourceType == SourceType.Channel && !(this is Channel))
  1671. {
  1672. return "ChannelFolderItem";
  1673. }
  1674. return GetType().Name;
  1675. }
  1676. /// <summary>
  1677. /// Gets the linked child.
  1678. /// </summary>
  1679. /// <param name="info">The info.</param>
  1680. /// <returns>BaseItem.</returns>
  1681. protected BaseItem GetLinkedChild(LinkedChild info)
  1682. {
  1683. // First get using the cached Id
  1684. if (info.ItemId.HasValue)
  1685. {
  1686. if (info.ItemId.Value.Equals(Guid.Empty))
  1687. {
  1688. return null;
  1689. }
  1690. var itemById = LibraryManager.GetItemById(info.ItemId.Value);
  1691. if (itemById != null)
  1692. {
  1693. return itemById;
  1694. }
  1695. }
  1696. var item = FindLinkedChild(info);
  1697. // If still null, log
  1698. if (item == null)
  1699. {
  1700. // Don't keep searching over and over
  1701. info.ItemId = Guid.Empty;
  1702. }
  1703. else
  1704. {
  1705. // Cache the id for next time
  1706. info.ItemId = item.Id;
  1707. }
  1708. return item;
  1709. }
  1710. private BaseItem FindLinkedChild(LinkedChild info)
  1711. {
  1712. var path = info.Path;
  1713. if (!string.IsNullOrEmpty(path))
  1714. {
  1715. path = FileSystem.MakeAbsolutePath(ContainingFolderPath, path);
  1716. var itemByPath = LibraryManager.FindByPath(path, null);
  1717. if (itemByPath == null)
  1718. {
  1719. //Logger.Warn("Unable to find linked item at path {0}", info.Path);
  1720. }
  1721. return itemByPath;
  1722. }
  1723. if (!string.IsNullOrEmpty(info.LibraryItemId))
  1724. {
  1725. var item = LibraryManager.GetItemById(info.LibraryItemId);
  1726. if (item == null)
  1727. {
  1728. //Logger.Warn("Unable to find linked item at path {0}", info.Path);
  1729. }
  1730. return item;
  1731. }
  1732. return null;
  1733. }
  1734. [IgnoreDataMember]
  1735. public virtual bool EnableRememberingTrackSelections
  1736. {
  1737. get
  1738. {
  1739. return true;
  1740. }
  1741. }
  1742. /// <summary>
  1743. /// Adds a studio to the item
  1744. /// </summary>
  1745. /// <param name="name">The name.</param>
  1746. /// <exception cref="System.ArgumentNullException"></exception>
  1747. public void AddStudio(string name)
  1748. {
  1749. if (string.IsNullOrEmpty(name))
  1750. {
  1751. throw new ArgumentNullException("name");
  1752. }
  1753. var current = Studios;
  1754. if (!current.Contains(name, StringComparer.OrdinalIgnoreCase))
  1755. {
  1756. if (current.Length == 0)
  1757. {
  1758. Studios = new[] { name };
  1759. }
  1760. else
  1761. {
  1762. var list =
  1763. Studios = current.Concat(new [] { name }).ToArray();
  1764. }
  1765. }
  1766. }
  1767. public void SetStudios(IEnumerable<string> names)
  1768. {
  1769. Studios = names.Distinct().ToArray();
  1770. }
  1771. /// <summary>
  1772. /// Adds a genre to the item
  1773. /// </summary>
  1774. /// <param name="name">The name.</param>
  1775. /// <exception cref="System.ArgumentNullException"></exception>
  1776. public void AddGenre(string name)
  1777. {
  1778. if (string.IsNullOrEmpty(name))
  1779. {
  1780. throw new ArgumentNullException("name");
  1781. }
  1782. var genres = Genres;
  1783. if (!genres.Contains(name, StringComparer.OrdinalIgnoreCase))
  1784. {
  1785. var list = genres.ToList();
  1786. list.Add(name);
  1787. Genres = list.ToArray();
  1788. }
  1789. }
  1790. /// <summary>
  1791. /// Marks the played.
  1792. /// </summary>
  1793. /// <param name="user">The user.</param>
  1794. /// <param name="datePlayed">The date played.</param>
  1795. /// <param name="resetPosition">if set to <c>true</c> [reset position].</param>
  1796. /// <returns>Task.</returns>
  1797. /// <exception cref="System.ArgumentNullException"></exception>
  1798. public virtual void MarkPlayed(User user,
  1799. DateTime? datePlayed,
  1800. bool resetPosition)
  1801. {
  1802. if (user == null)
  1803. {
  1804. throw new ArgumentNullException();
  1805. }
  1806. var data = UserDataManager.GetUserData(user, this);
  1807. if (datePlayed.HasValue)
  1808. {
  1809. // Increment
  1810. data.PlayCount++;
  1811. }
  1812. // Ensure it's at least one
  1813. data.PlayCount = Math.Max(data.PlayCount, 1);
  1814. if (resetPosition)
  1815. {
  1816. data.PlaybackPositionTicks = 0;
  1817. }
  1818. data.LastPlayedDate = datePlayed ?? data.LastPlayedDate ?? DateTime.UtcNow;
  1819. data.Played = true;
  1820. UserDataManager.SaveUserData(user.Id, this, data, UserDataSaveReason.TogglePlayed, CancellationToken.None);
  1821. }
  1822. /// <summary>
  1823. /// Marks the unplayed.
  1824. /// </summary>
  1825. /// <param name="user">The user.</param>
  1826. /// <returns>Task.</returns>
  1827. /// <exception cref="System.ArgumentNullException"></exception>
  1828. public virtual void MarkUnplayed(User user)
  1829. {
  1830. if (user == null)
  1831. {
  1832. throw new ArgumentNullException();
  1833. }
  1834. var data = UserDataManager.GetUserData(user, this);
  1835. //I think it is okay to do this here.
  1836. // if this is only called when a user is manually forcing something to un-played
  1837. // then it probably is what we want to do...
  1838. data.PlayCount = 0;
  1839. data.PlaybackPositionTicks = 0;
  1840. data.LastPlayedDate = null;
  1841. data.Played = false;
  1842. UserDataManager.SaveUserData(user.Id, this, data, UserDataSaveReason.TogglePlayed, CancellationToken.None);
  1843. }
  1844. /// <summary>
  1845. /// Do whatever refreshing is necessary when the filesystem pertaining to this item has changed.
  1846. /// </summary>
  1847. /// <returns>Task.</returns>
  1848. public virtual void ChangedExternally()
  1849. {
  1850. ProviderManager.QueueRefresh(Id, new MetadataRefreshOptions(FileSystem)
  1851. {
  1852. }, RefreshPriority.High);
  1853. }
  1854. /// <summary>
  1855. /// Gets an image
  1856. /// </summary>
  1857. /// <param name="type">The type.</param>
  1858. /// <param name="imageIndex">Index of the image.</param>
  1859. /// <returns><c>true</c> if the specified type has image; otherwise, <c>false</c>.</returns>
  1860. /// <exception cref="System.ArgumentException">Backdrops should be accessed using Item.Backdrops</exception>
  1861. public bool HasImage(ImageType type, int imageIndex)
  1862. {
  1863. return GetImageInfo(type, imageIndex) != null;
  1864. }
  1865. public void SetImage(ItemImageInfo image, int index)
  1866. {
  1867. if (image.Type == ImageType.Chapter)
  1868. {
  1869. throw new ArgumentException("Cannot set chapter images using SetImagePath");
  1870. }
  1871. var existingImage = GetImageInfo(image.Type, index);
  1872. if (existingImage != null)
  1873. {
  1874. existingImage.Path = image.Path;
  1875. existingImage.DateModified = image.DateModified;
  1876. existingImage.Width = image.Width;
  1877. existingImage.Height = image.Height;
  1878. }
  1879. else
  1880. {
  1881. var currentCount = ImageInfos.Length;
  1882. ImageInfos = ImageInfos.Concat(new [] { image }) .ToArray();
  1883. }
  1884. }
  1885. public void SetImagePath(ImageType type, int index, FileSystemMetadata file)
  1886. {
  1887. if (type == ImageType.Chapter)
  1888. {
  1889. throw new ArgumentException("Cannot set chapter images using SetImagePath");
  1890. }
  1891. var image = GetImageInfo(type, index);
  1892. if (image == null)
  1893. {
  1894. ImageInfos = ImageInfos.Concat(new [] { GetImageInfo(file, type) }) .ToArray();
  1895. }
  1896. else
  1897. {
  1898. var imageInfo = GetImageInfo(file, type);
  1899. image.Path = file.FullName;
  1900. image.DateModified = imageInfo.DateModified;
  1901. // reset these values
  1902. image.Width = 0;
  1903. image.Height = 0;
  1904. }
  1905. }
  1906. /// <summary>
  1907. /// Deletes the image.
  1908. /// </summary>
  1909. /// <param name="type">The type.</param>
  1910. /// <param name="index">The index.</param>
  1911. /// <returns>Task.</returns>
  1912. public void DeleteImage(ImageType type, int index)
  1913. {
  1914. var info = GetImageInfo(type, index);
  1915. if (info == null)
  1916. {
  1917. // Nothing to do
  1918. return;
  1919. }
  1920. // Remove it from the item
  1921. RemoveImage(info);
  1922. if (info.IsLocalFile)
  1923. {
  1924. FileSystem.DeleteFile(info.Path);
  1925. }
  1926. UpdateToRepository(ItemUpdateType.ImageUpdate, CancellationToken.None);
  1927. }
  1928. public void RemoveImage(ItemImageInfo image)
  1929. {
  1930. RemoveImages(new List<ItemImageInfo> { image });
  1931. }
  1932. public void RemoveImages(List<ItemImageInfo> deletedImages)
  1933. {
  1934. ImageInfos = ImageInfos.Except(deletedImages).ToArray();
  1935. }
  1936. public virtual void UpdateToRepository(ItemUpdateType updateReason, CancellationToken cancellationToken)
  1937. {
  1938. LibraryManager.UpdateItem(this, GetParent(), updateReason, cancellationToken);
  1939. }
  1940. /// <summary>
  1941. /// Validates that images within the item are still on the file system
  1942. /// </summary>
  1943. public bool ValidateImages(IDirectoryService directoryService)
  1944. {
  1945. var allFiles = ImageInfos
  1946. .Where(i => i.IsLocalFile)
  1947. .Select(i => FileSystem.GetDirectoryName(i.Path))
  1948. .Distinct(StringComparer.OrdinalIgnoreCase)
  1949. .SelectMany(i => directoryService.GetFilePaths(i))
  1950. .ToList();
  1951. var deletedImages = ImageInfos
  1952. .Where(image => image.IsLocalFile && !allFiles.Contains(image.Path, StringComparer.OrdinalIgnoreCase))
  1953. .ToList();
  1954. if (deletedImages.Count > 0)
  1955. {
  1956. ImageInfos = ImageInfos.Except(deletedImages).ToArray();
  1957. }
  1958. return deletedImages.Count > 0;
  1959. }
  1960. /// <summary>
  1961. /// Gets the image path.
  1962. /// </summary>
  1963. /// <param name="imageType">Type of the image.</param>
  1964. /// <param name="imageIndex">Index of the image.</param>
  1965. /// <returns>System.String.</returns>
  1966. /// <exception cref="System.InvalidOperationException">
  1967. /// </exception>
  1968. /// <exception cref="System.ArgumentNullException">item</exception>
  1969. public string GetImagePath(ImageType imageType, int imageIndex)
  1970. {
  1971. var info = GetImageInfo(imageType, imageIndex);
  1972. return info == null ? null : info.Path;
  1973. }
  1974. /// <summary>
  1975. /// Gets the image information.
  1976. /// </summary>
  1977. /// <param name="imageType">Type of the image.</param>
  1978. /// <param name="imageIndex">Index of the image.</param>
  1979. /// <returns>ItemImageInfo.</returns>
  1980. public ItemImageInfo GetImageInfo(ImageType imageType, int imageIndex)
  1981. {
  1982. if (imageType == ImageType.Chapter)
  1983. {
  1984. var chapter = ItemRepository.GetChapter(this, imageIndex);
  1985. if (chapter == null)
  1986. {
  1987. return null;
  1988. }
  1989. var path = chapter.ImagePath;
  1990. if (string.IsNullOrEmpty(path))
  1991. {
  1992. return null;
  1993. }
  1994. return new ItemImageInfo
  1995. {
  1996. Path = path,
  1997. DateModified = chapter.ImageDateModified,
  1998. Type = imageType
  1999. };
  2000. }
  2001. return GetImages(imageType)
  2002. .ElementAtOrDefault(imageIndex);
  2003. }
  2004. public IEnumerable<ItemImageInfo> GetImages(ImageType imageType)
  2005. {
  2006. if (imageType == ImageType.Chapter)
  2007. {
  2008. throw new ArgumentException("No image info for chapter images");
  2009. }
  2010. return ImageInfos.Where(i => i.Type == imageType);
  2011. }
  2012. /// <summary>
  2013. /// Adds the images.
  2014. /// </summary>
  2015. /// <param name="imageType">Type of the image.</param>
  2016. /// <param name="images">The images.</param>
  2017. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  2018. /// <exception cref="System.ArgumentException">Cannot call AddImages with chapter images</exception>
  2019. public bool AddImages(ImageType imageType, List<FileSystemMetadata> images)
  2020. {
  2021. if (imageType == ImageType.Chapter)
  2022. {
  2023. throw new ArgumentException("Cannot call AddImages with chapter images");
  2024. }
  2025. var existingImages = GetImages(imageType)
  2026. .ToList();
  2027. var newImageList = new List<FileSystemMetadata>();
  2028. var imageAdded = false;
  2029. var imageUpdated = false;
  2030. foreach (var newImage in images)
  2031. {
  2032. if (newImage == null)
  2033. {
  2034. throw new ArgumentException("null image found in list");
  2035. }
  2036. var existing = existingImages
  2037. .FirstOrDefault(i => string.Equals(i.Path, newImage.FullName, StringComparison.OrdinalIgnoreCase));
  2038. if (existing == null)
  2039. {
  2040. newImageList.Add(newImage);
  2041. imageAdded = true;
  2042. }
  2043. else
  2044. {
  2045. if (existing.IsLocalFile)
  2046. {
  2047. var newDateModified = FileSystem.GetLastWriteTimeUtc(newImage);
  2048. // If date changed then we need to reset saved image dimensions
  2049. if (existing.DateModified != newDateModified && (existing.Width > 0 || existing.Height > 0))
  2050. {
  2051. existing.Width = 0;
  2052. existing.Height = 0;
  2053. imageUpdated = true;
  2054. }
  2055. existing.DateModified = newDateModified;
  2056. }
  2057. }
  2058. }
  2059. if (imageAdded || images.Count != existingImages.Count)
  2060. {
  2061. var newImagePaths = images.Select(i => i.FullName).ToList();
  2062. var deleted = existingImages
  2063. .Where(i => i.IsLocalFile && !newImagePaths.Contains(i.Path, StringComparer.OrdinalIgnoreCase) && !FileSystem.FileExists(i.Path))
  2064. .ToList();
  2065. if (deleted.Count > 0)
  2066. {
  2067. ImageInfos = ImageInfos.Except(deleted).ToArray();
  2068. }
  2069. }
  2070. if (newImageList.Count > 0)
  2071. {
  2072. ImageInfos = ImageInfos.Concat(newImageList.Select(i => GetImageInfo(i, imageType))).ToArray();
  2073. }
  2074. return imageUpdated || newImageList.Count > 0;
  2075. }
  2076. private ItemImageInfo GetImageInfo(FileSystemMetadata file, ImageType type)
  2077. {
  2078. return new ItemImageInfo
  2079. {
  2080. Path = file.FullName,
  2081. Type = type,
  2082. DateModified = FileSystem.GetLastWriteTimeUtc(file)
  2083. };
  2084. }
  2085. /// <summary>
  2086. /// Gets the file system path to delete when the item is to be deleted
  2087. /// </summary>
  2088. /// <returns></returns>
  2089. public virtual IEnumerable<FileSystemMetadata> GetDeletePaths()
  2090. {
  2091. return new[] {
  2092. new FileSystemMetadata
  2093. {
  2094. FullName = Path,
  2095. IsDirectory = IsFolder
  2096. }
  2097. }.Concat(GetLocalMetadataFilesToDelete());
  2098. }
  2099. protected List<FileSystemMetadata> GetLocalMetadataFilesToDelete()
  2100. {
  2101. if (IsFolder || !IsInMixedFolder)
  2102. {
  2103. return new List<FileSystemMetadata>();
  2104. }
  2105. var filename = System.IO.Path.GetFileNameWithoutExtension(Path);
  2106. var extensions = new List<string> { ".nfo", ".xml", ".srt", ".vtt", ".sub", ".idx", ".txt", ".edl", ".bif", ".smi", ".ttml" };
  2107. extensions.AddRange(SupportedImageExtensions);
  2108. return FileSystem.GetFiles(FileSystem.GetDirectoryName(Path), extensions.ToArray(), false, false)
  2109. .Where(i => System.IO.Path.GetFileNameWithoutExtension(i.FullName).StartsWith(filename, StringComparison.OrdinalIgnoreCase))
  2110. .ToList();
  2111. }
  2112. public bool AllowsMultipleImages(ImageType type)
  2113. {
  2114. return type == ImageType.Backdrop || type == ImageType.Screenshot || type == ImageType.Chapter;
  2115. }
  2116. public void SwapImages(ImageType type, int index1, int index2)
  2117. {
  2118. if (!AllowsMultipleImages(type))
  2119. {
  2120. throw new ArgumentException("The change index operation is only applicable to backdrops and screenshots");
  2121. }
  2122. var info1 = GetImageInfo(type, index1);
  2123. var info2 = GetImageInfo(type, index2);
  2124. if (info1 == null || info2 == null)
  2125. {
  2126. // Nothing to do
  2127. return;
  2128. }
  2129. if (!info1.IsLocalFile || !info2.IsLocalFile)
  2130. {
  2131. // TODO: Not supported yet
  2132. return;
  2133. }
  2134. var path1 = info1.Path;
  2135. var path2 = info2.Path;
  2136. FileSystem.SwapFiles(path1, path2);
  2137. // Refresh these values
  2138. info1.DateModified = FileSystem.GetLastWriteTimeUtc(info1.Path);
  2139. info2.DateModified = FileSystem.GetLastWriteTimeUtc(info2.Path);
  2140. info1.Width = 0;
  2141. info1.Height = 0;
  2142. info2.Width = 0;
  2143. info2.Height = 0;
  2144. UpdateToRepository(ItemUpdateType.ImageUpdate, CancellationToken.None);
  2145. }
  2146. public virtual bool IsPlayed(User user)
  2147. {
  2148. var userdata = UserDataManager.GetUserData(user, this);
  2149. return userdata != null && userdata.Played;
  2150. }
  2151. public bool IsFavoriteOrLiked(User user)
  2152. {
  2153. var userdata = UserDataManager.GetUserData(user, this);
  2154. return userdata != null && (userdata.IsFavorite || (userdata.Likes ?? false));
  2155. }
  2156. public virtual bool IsUnplayed(User user)
  2157. {
  2158. if (user == null)
  2159. {
  2160. throw new ArgumentNullException("user");
  2161. }
  2162. var userdata = UserDataManager.GetUserData(user, this);
  2163. return userdata == null || !userdata.Played;
  2164. }
  2165. ItemLookupInfo IHasLookupInfo<ItemLookupInfo>.GetLookupInfo()
  2166. {
  2167. return GetItemLookupInfo<ItemLookupInfo>();
  2168. }
  2169. protected T GetItemLookupInfo<T>()
  2170. where T : ItemLookupInfo, new()
  2171. {
  2172. return new T
  2173. {
  2174. MetadataCountryCode = GetPreferredMetadataCountryCode(),
  2175. MetadataLanguage = GetPreferredMetadataLanguage(),
  2176. Name = GetNameForMetadataLookup(),
  2177. ProviderIds = ProviderIds,
  2178. IndexNumber = IndexNumber,
  2179. ParentIndexNumber = ParentIndexNumber,
  2180. Year = ProductionYear,
  2181. PremiereDate = PremiereDate
  2182. };
  2183. }
  2184. protected virtual string GetNameForMetadataLookup()
  2185. {
  2186. return Name;
  2187. }
  2188. /// <summary>
  2189. /// This is called before any metadata refresh and returns true or false indicating if changes were made
  2190. /// </summary>
  2191. public virtual bool BeforeMetadataRefresh(bool replaceAllMetdata)
  2192. {
  2193. _sortName = null;
  2194. var hasChanges = false;
  2195. if (string.IsNullOrEmpty(Name) && !string.IsNullOrEmpty(Path))
  2196. {
  2197. Name = FileSystem.GetFileNameWithoutExtension(Path);
  2198. hasChanges = true;
  2199. }
  2200. return hasChanges;
  2201. }
  2202. protected static string GetMappedPath(BaseItem item, string path, MediaProtocol? protocol)
  2203. {
  2204. if (protocol.HasValue && protocol.Value == MediaProtocol.File)
  2205. {
  2206. return LibraryManager.GetPathAfterNetworkSubstitution(path, item);
  2207. }
  2208. return path;
  2209. }
  2210. public virtual void FillUserDataDtoValues(UserItemDataDto dto, UserItemData userData, BaseItemDto itemDto, User user, DtoOptions fields)
  2211. {
  2212. if (RunTimeTicks.HasValue)
  2213. {
  2214. double pct = RunTimeTicks.Value;
  2215. if (pct > 0)
  2216. {
  2217. pct = userData.PlaybackPositionTicks / pct;
  2218. if (pct > 0)
  2219. {
  2220. dto.PlayedPercentage = 100 * pct;
  2221. }
  2222. }
  2223. }
  2224. }
  2225. protected Task RefreshMetadataForOwnedItem(BaseItem ownedItem, bool copyTitleMetadata, MetadataRefreshOptions options, CancellationToken cancellationToken)
  2226. {
  2227. var newOptions = new MetadataRefreshOptions(options);
  2228. newOptions.SearchResult = null;
  2229. var item = this;
  2230. if (copyTitleMetadata)
  2231. {
  2232. // Take some data from the main item, for querying purposes
  2233. if (!item.Genres.SequenceEqual(ownedItem.Genres, StringComparer.Ordinal))
  2234. {
  2235. newOptions.ForceSave = true;
  2236. ownedItem.Genres = item.Genres;
  2237. }
  2238. if (!item.Studios.SequenceEqual(ownedItem.Studios, StringComparer.Ordinal))
  2239. {
  2240. newOptions.ForceSave = true;
  2241. ownedItem.Studios = item.Studios;
  2242. }
  2243. if (!item.ProductionLocations.SequenceEqual(ownedItem.ProductionLocations, StringComparer.Ordinal))
  2244. {
  2245. newOptions.ForceSave = true;
  2246. ownedItem.ProductionLocations = item.ProductionLocations;
  2247. }
  2248. if (item.CommunityRating != ownedItem.CommunityRating)
  2249. {
  2250. ownedItem.CommunityRating = item.CommunityRating;
  2251. newOptions.ForceSave = true;
  2252. }
  2253. if (item.CriticRating != ownedItem.CriticRating)
  2254. {
  2255. ownedItem.CriticRating = item.CriticRating;
  2256. newOptions.ForceSave = true;
  2257. }
  2258. if (!string.Equals(item.Overview, ownedItem.Overview, StringComparison.Ordinal))
  2259. {
  2260. ownedItem.Overview = item.Overview;
  2261. newOptions.ForceSave = true;
  2262. }
  2263. if (!string.Equals(item.OfficialRating, ownedItem.OfficialRating, StringComparison.Ordinal))
  2264. {
  2265. ownedItem.OfficialRating = item.OfficialRating;
  2266. newOptions.ForceSave = true;
  2267. }
  2268. if (!string.Equals(item.CustomRating, ownedItem.CustomRating, StringComparison.Ordinal))
  2269. {
  2270. ownedItem.CustomRating = item.CustomRating;
  2271. newOptions.ForceSave = true;
  2272. }
  2273. }
  2274. return ownedItem.RefreshMetadata(newOptions, cancellationToken);
  2275. }
  2276. protected Task RefreshMetadataForOwnedVideo(MetadataRefreshOptions options, bool copyTitleMetadata, string path, CancellationToken cancellationToken)
  2277. {
  2278. var newOptions = new MetadataRefreshOptions(options);
  2279. newOptions.SearchResult = null;
  2280. var id = LibraryManager.GetNewItemId(path, typeof(Video));
  2281. // Try to retrieve it from the db. If we don't find it, use the resolved version
  2282. var video = LibraryManager.GetItemById(id) as Video;
  2283. if (video == null)
  2284. {
  2285. video = LibraryManager.ResolvePath(FileSystem.GetFileSystemInfo(path)) as Video;
  2286. newOptions.ForceSave = true;
  2287. }
  2288. //var parentId = Id;
  2289. //if (!video.IsOwnedItem || video.ParentId != parentId)
  2290. //{
  2291. // video.IsOwnedItem = true;
  2292. // video.ParentId = parentId;
  2293. // newOptions.ForceSave = true;
  2294. //}
  2295. if (video == null)
  2296. {
  2297. return Task.FromResult(true);
  2298. }
  2299. return RefreshMetadataForOwnedItem(video, copyTitleMetadata, newOptions, cancellationToken);
  2300. }
  2301. public string GetEtag(User user)
  2302. {
  2303. var list = GetEtagValues(user);
  2304. return string.Join("|", list.ToArray()).GetMD5().ToString("N");
  2305. }
  2306. protected virtual List<string> GetEtagValues(User user)
  2307. {
  2308. return new List<string>
  2309. {
  2310. DateLastSaved.Ticks.ToString(CultureInfo.InvariantCulture)
  2311. };
  2312. }
  2313. public virtual IEnumerable<Guid> GetAncestorIds()
  2314. {
  2315. return GetParents().Select(i => i.Id).Concat(LibraryManager.GetCollectionFolders(this).Select(i => i.Id));
  2316. }
  2317. public BaseItem GetTopParent()
  2318. {
  2319. if (IsTopParent)
  2320. {
  2321. return this;
  2322. }
  2323. foreach (var parent in GetParents())
  2324. {
  2325. if (parent.IsTopParent)
  2326. {
  2327. return parent;
  2328. }
  2329. }
  2330. return null;
  2331. }
  2332. [IgnoreDataMember]
  2333. public virtual bool IsTopParent
  2334. {
  2335. get
  2336. {
  2337. if (this is BasePluginFolder || this is Channel)
  2338. {
  2339. return true;
  2340. }
  2341. var view = this as IHasCollectionType;
  2342. if (view != null)
  2343. {
  2344. if (string.Equals(view.CollectionType, CollectionType.LiveTv, StringComparison.OrdinalIgnoreCase))
  2345. {
  2346. return true;
  2347. }
  2348. }
  2349. if (GetParent() is AggregateFolder)
  2350. {
  2351. return true;
  2352. }
  2353. return false;
  2354. }
  2355. }
  2356. [IgnoreDataMember]
  2357. public virtual bool SupportsAncestors
  2358. {
  2359. get
  2360. {
  2361. return true;
  2362. }
  2363. }
  2364. [IgnoreDataMember]
  2365. public virtual bool StopRefreshIfLocalMetadataFound
  2366. {
  2367. get
  2368. {
  2369. return true;
  2370. }
  2371. }
  2372. public virtual IEnumerable<Guid> GetIdsForAncestorQuery()
  2373. {
  2374. return new[] { Id };
  2375. }
  2376. public virtual List<ExternalUrl> GetRelatedUrls()
  2377. {
  2378. return new List<ExternalUrl>();
  2379. }
  2380. public virtual double? GetRefreshProgress()
  2381. {
  2382. return null;
  2383. }
  2384. public virtual ItemUpdateType OnMetadataChanged()
  2385. {
  2386. var updateType = ItemUpdateType.None;
  2387. var item = this;
  2388. var inheritedParentalRatingValue = item.GetInheritedParentalRatingValue() ?? 0;
  2389. if (inheritedParentalRatingValue != item.InheritedParentalRatingValue)
  2390. {
  2391. item.InheritedParentalRatingValue = inheritedParentalRatingValue;
  2392. updateType |= ItemUpdateType.MetadataImport;
  2393. }
  2394. return updateType;
  2395. }
  2396. /// <summary>
  2397. /// Updates the official rating based on content and returns true or false indicating if it changed.
  2398. /// </summary>
  2399. /// <returns></returns>
  2400. public bool UpdateRatingToItems(IList<BaseItem> children)
  2401. {
  2402. var currentOfficialRating = OfficialRating;
  2403. // Gather all possible ratings
  2404. var ratings = children
  2405. .Select(i => i.OfficialRating)
  2406. .Where(i => !string.IsNullOrEmpty(i))
  2407. .Distinct(StringComparer.OrdinalIgnoreCase)
  2408. .Select(i => new Tuple<string, int?>(i, LocalizationManager.GetRatingLevel(i)))
  2409. .OrderBy(i => i.Item2 ?? 1000)
  2410. .Select(i => i.Item1);
  2411. OfficialRating = ratings.FirstOrDefault() ?? currentOfficialRating;
  2412. return !string.Equals(currentOfficialRating ?? string.Empty, OfficialRating ?? string.Empty,
  2413. StringComparison.OrdinalIgnoreCase);
  2414. }
  2415. public IEnumerable<BaseItem> GetThemeSongs()
  2416. {
  2417. return ThemeVideoIds.Select(LibraryManager.GetItemById).Where(i => i.ExtraType.Equals(Model.Entities.ExtraType.ThemeSong)).OrderBy(i => i.SortName);
  2418. }
  2419. public IEnumerable<BaseItem> GetThemeVideos()
  2420. {
  2421. return ThemeVideoIds.Select(LibraryManager.GetItemById).Where(i => i.ExtraType.Equals(Model.Entities.ExtraType.ThemeVideo)).OrderBy(i => i.SortName);
  2422. }
  2423. public MediaUrl[] RemoteTrailers { get; set; }
  2424. public IEnumerable<BaseItem> GetExtras()
  2425. {
  2426. return ThemeVideoIds.Select(LibraryManager.GetItemById).Where(i => i.ExtraType.Equals(Model.Entities.ExtraType.ThemeVideo)).OrderBy(i => i.SortName);
  2427. }
  2428. public IEnumerable<BaseItem> GetExtras(ExtraType[] unused)
  2429. {
  2430. return GetExtras();
  2431. }
  2432. public IEnumerable<BaseItem> GetDisplayExtras()
  2433. {
  2434. return GetExtras();
  2435. }
  2436. public virtual bool IsHD {
  2437. get{
  2438. return Height >= 720;
  2439. }
  2440. }
  2441. public bool IsShortcut{ get; set;}
  2442. public string ShortcutPath{ get; set;}
  2443. public int Width { get; set; }
  2444. public int Height { get; set; }
  2445. public Guid[] ExtraIds { get; set; }
  2446. public virtual long GetRunTimeTicksForPlayState() {
  2447. return RunTimeTicks ?? 0;
  2448. }
  2449. // what does this do?
  2450. public static ExtraType[] DisplayExtraTypes = new[] {Model.Entities.ExtraType.ThemeSong, Model.Entities.ExtraType.ThemeVideo };
  2451. public virtual bool SupportsExternalTransfer {
  2452. get {
  2453. return false;
  2454. }
  2455. }
  2456. }
  2457. }