BaseItem.cs 91 KB

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