BaseItem.cs 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Controller.Channels;
  3. using MediaBrowser.Controller.Collections;
  4. using MediaBrowser.Controller.Configuration;
  5. using MediaBrowser.Controller.Drawing;
  6. using MediaBrowser.Controller.Library;
  7. using MediaBrowser.Controller.LiveTv;
  8. using MediaBrowser.Controller.Localization;
  9. using MediaBrowser.Controller.Persistence;
  10. using MediaBrowser.Controller.Providers;
  11. using MediaBrowser.Model.Configuration;
  12. using MediaBrowser.Model.Dto;
  13. using MediaBrowser.Model.Entities;
  14. using MediaBrowser.Model.Library;
  15. using MediaBrowser.Model.Logging;
  16. using MediaBrowser.Model.Users;
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Globalization;
  20. using System.IO;
  21. using System.Linq;
  22. using System.Runtime.Serialization;
  23. using System.Text;
  24. using System.Threading;
  25. using System.Threading.Tasks;
  26. using CommonIO;
  27. using MediaBrowser.Controller.Sorting;
  28. using MediaBrowser.Model.LiveTv;
  29. using MediaBrowser.Model.Providers;
  30. namespace MediaBrowser.Controller.Entities
  31. {
  32. /// <summary>
  33. /// Class BaseItem
  34. /// </summary>
  35. public abstract class BaseItem : IHasProviderIds, ILibraryItem, IHasImages, IHasUserData, IHasMetadata, IHasLookupInfo<ItemLookupInfo>
  36. {
  37. protected BaseItem()
  38. {
  39. Keywords = new List<string>();
  40. Tags = new List<string>();
  41. Genres = new List<string>();
  42. Studios = new List<string>();
  43. ProviderIds = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  44. LockedFields = new List<MetadataFields>();
  45. ImageInfos = new List<ItemImageInfo>();
  46. }
  47. public static readonly char[] SlugReplaceChars = { '?', '/', '&' };
  48. public static char SlugChar = '-';
  49. /// <summary>
  50. /// The supported image extensions
  51. /// </summary>
  52. public static readonly string[] SupportedImageExtensions = { ".png", ".jpg", ".jpeg", ".tbn", ".gif" };
  53. public static readonly List<string> SupportedImageExtensionsList = SupportedImageExtensions.ToList();
  54. /// <summary>
  55. /// The trailer folder name
  56. /// </summary>
  57. public static string TrailerFolderName = "trailers";
  58. public static string ThemeSongsFolderName = "theme-music";
  59. public static string ThemeSongFilename = "theme";
  60. public static string ThemeVideosFolderName = "backdrops";
  61. [IgnoreDataMember]
  62. public string PreferredMetadataCountryCode { get; set; }
  63. [IgnoreDataMember]
  64. public string PreferredMetadataLanguage { get; set; }
  65. public long? Size { get; set; }
  66. public string Container { get; set; }
  67. public string ShortOverview { get; set; }
  68. public List<ItemImageInfo> ImageInfos { get; set; }
  69. [IgnoreDataMember]
  70. public bool IsVirtualItem { get; set; }
  71. /// <summary>
  72. /// Gets or sets the album.
  73. /// </summary>
  74. /// <value>The album.</value>
  75. [IgnoreDataMember]
  76. public string Album { get; set; }
  77. /// <summary>
  78. /// Gets or sets the channel identifier.
  79. /// </summary>
  80. /// <value>The channel identifier.</value>
  81. [IgnoreDataMember]
  82. public string ChannelId { get; set; }
  83. [IgnoreDataMember]
  84. public virtual bool SupportsAddingToPlaylist
  85. {
  86. get
  87. {
  88. return false;
  89. }
  90. }
  91. [IgnoreDataMember]
  92. public virtual bool AlwaysScanInternalMetadataPath
  93. {
  94. get { return false; }
  95. }
  96. /// <summary>
  97. /// Gets a value indicating whether this instance is in mixed folder.
  98. /// </summary>
  99. /// <value><c>true</c> if this instance is in mixed folder; otherwise, <c>false</c>.</value>
  100. [IgnoreDataMember]
  101. public bool IsInMixedFolder { get; set; }
  102. [IgnoreDataMember]
  103. public virtual bool SupportsRemoteImageDownloading
  104. {
  105. get
  106. {
  107. return true;
  108. }
  109. }
  110. private string _name;
  111. /// <summary>
  112. /// Gets or sets the name.
  113. /// </summary>
  114. /// <value>The name.</value>
  115. [IgnoreDataMember]
  116. public virtual string Name
  117. {
  118. get
  119. {
  120. return _name;
  121. }
  122. set
  123. {
  124. _name = value;
  125. // lazy load this again
  126. _sortName = null;
  127. }
  128. }
  129. [IgnoreDataMember]
  130. public string SlugName
  131. {
  132. get
  133. {
  134. var name = Name;
  135. if (string.IsNullOrWhiteSpace(name))
  136. {
  137. return string.Empty;
  138. }
  139. return SlugReplaceChars.Aggregate(name, (current, c) => current.Replace(c, SlugChar));
  140. }
  141. }
  142. [IgnoreDataMember]
  143. public bool IsUnaired
  144. {
  145. get { return PremiereDate.HasValue && PremiereDate.Value.ToLocalTime().Date >= DateTime.Now.Date; }
  146. }
  147. public string OriginalTitle { get; set; }
  148. /// <summary>
  149. /// Gets or sets the id.
  150. /// </summary>
  151. /// <value>The id.</value>
  152. [IgnoreDataMember]
  153. public Guid Id { get; set; }
  154. /// <summary>
  155. /// Gets or sets a value indicating whether this instance is hd.
  156. /// </summary>
  157. /// <value><c>true</c> if this instance is hd; otherwise, <c>false</c>.</value>
  158. [IgnoreDataMember]
  159. public bool? IsHD { get; set; }
  160. /// <summary>
  161. /// Gets or sets the audio.
  162. /// </summary>
  163. /// <value>The audio.</value>
  164. [IgnoreDataMember]
  165. public ProgramAudio? Audio { get; set; }
  166. /// <summary>
  167. /// Return the id that should be used to key display prefs for this item.
  168. /// Default is based on the type for everything except actual generic folders.
  169. /// </summary>
  170. /// <value>The display prefs id.</value>
  171. [IgnoreDataMember]
  172. public virtual Guid DisplayPreferencesId
  173. {
  174. get
  175. {
  176. var thisType = GetType();
  177. return thisType == typeof(Folder) ? Id : thisType.FullName.GetMD5();
  178. }
  179. }
  180. /// <summary>
  181. /// Gets or sets the path.
  182. /// </summary>
  183. /// <value>The path.</value>
  184. [IgnoreDataMember]
  185. public virtual string Path { get; set; }
  186. [IgnoreDataMember]
  187. public bool IsOffline { get; set; }
  188. [IgnoreDataMember]
  189. public virtual SourceType SourceType { get; set; }
  190. /// <summary>
  191. /// Returns the folder containing the item.
  192. /// If the item is a folder, it returns the folder itself
  193. /// </summary>
  194. [IgnoreDataMember]
  195. public virtual string ContainingFolderPath
  196. {
  197. get
  198. {
  199. if (IsFolder)
  200. {
  201. return Path;
  202. }
  203. return System.IO.Path.GetDirectoryName(Path);
  204. }
  205. }
  206. /// <summary>
  207. /// Gets or sets the name of the service.
  208. /// </summary>
  209. /// <value>The name of the service.</value>
  210. [IgnoreDataMember]
  211. public string ServiceName { get; set; }
  212. /// <summary>
  213. /// If this content came from an external service, the id of the content on that service
  214. /// </summary>
  215. [IgnoreDataMember]
  216. public string ExternalId
  217. {
  218. get { return this.GetProviderId("ProviderExternalId"); }
  219. set
  220. {
  221. this.SetProviderId("ProviderExternalId", value);
  222. }
  223. }
  224. /// <summary>
  225. /// Gets or sets the etag.
  226. /// </summary>
  227. /// <value>The etag.</value>
  228. [IgnoreDataMember]
  229. public string ExternalEtag { get; set; }
  230. [IgnoreDataMember]
  231. public virtual bool IsHidden
  232. {
  233. get
  234. {
  235. return false;
  236. }
  237. }
  238. [IgnoreDataMember]
  239. public virtual bool IsOwnedItem
  240. {
  241. get
  242. {
  243. // Local trailer, special feature, theme video, etc.
  244. // An item that belongs to another item but is not part of the Parent-Child tree
  245. return !IsFolder && ParentId == Guid.Empty && LocationType == LocationType.FileSystem;
  246. }
  247. }
  248. /// <summary>
  249. /// Gets or sets the type of the location.
  250. /// </summary>
  251. /// <value>The type of the location.</value>
  252. [IgnoreDataMember]
  253. public virtual LocationType LocationType
  254. {
  255. get
  256. {
  257. if (IsOffline)
  258. {
  259. return LocationType.Offline;
  260. }
  261. if (string.IsNullOrWhiteSpace(Path))
  262. {
  263. if (SourceType == SourceType.Channel)
  264. {
  265. return LocationType.Remote;
  266. }
  267. return LocationType.Virtual;
  268. }
  269. return FileSystem.IsPathFile(Path) ? LocationType.FileSystem : LocationType.Remote;
  270. }
  271. }
  272. [IgnoreDataMember]
  273. public virtual bool SupportsLocalMetadata
  274. {
  275. get
  276. {
  277. if (SourceType == SourceType.Channel)
  278. {
  279. return false;
  280. }
  281. var locationType = LocationType;
  282. return locationType != LocationType.Remote && locationType != LocationType.Virtual;
  283. }
  284. }
  285. [IgnoreDataMember]
  286. public virtual string FileNameWithoutExtension
  287. {
  288. get
  289. {
  290. if (LocationType == LocationType.FileSystem)
  291. {
  292. return System.IO.Path.GetFileNameWithoutExtension(Path);
  293. }
  294. return null;
  295. }
  296. }
  297. [IgnoreDataMember]
  298. public virtual bool EnableAlphaNumericSorting
  299. {
  300. get
  301. {
  302. return true;
  303. }
  304. }
  305. private List<Tuple<StringBuilder, bool>> GetSortChunks(string s1)
  306. {
  307. var list = new List<Tuple<StringBuilder, bool>>();
  308. int thisMarker = 0, thisNumericChunk = 0;
  309. while (thisMarker < s1.Length)
  310. {
  311. if (thisMarker >= s1.Length)
  312. {
  313. break;
  314. }
  315. char thisCh = s1[thisMarker];
  316. StringBuilder thisChunk = new StringBuilder();
  317. while ((thisMarker < s1.Length) && (thisChunk.Length == 0 || SortHelper.InChunk(thisCh, thisChunk[0])))
  318. {
  319. thisChunk.Append(thisCh);
  320. thisMarker++;
  321. if (thisMarker < s1.Length)
  322. {
  323. thisCh = s1[thisMarker];
  324. }
  325. }
  326. var isNumeric = thisChunk.Length > 0 && char.IsDigit(thisChunk[0]);
  327. list.Add(new Tuple<StringBuilder, bool>(thisChunk, isNumeric));
  328. }
  329. return list;
  330. }
  331. /// <summary>
  332. /// This is just a helper for convenience
  333. /// </summary>
  334. /// <value>The primary image path.</value>
  335. [IgnoreDataMember]
  336. public string PrimaryImagePath
  337. {
  338. get { return this.GetImagePath(ImageType.Primary); }
  339. }
  340. public virtual bool IsInternetMetadataEnabled()
  341. {
  342. return ConfigurationManager.Configuration.EnableInternetProviders;
  343. }
  344. public virtual bool CanDelete()
  345. {
  346. if (SourceType == SourceType.Channel)
  347. {
  348. return false;
  349. }
  350. var locationType = LocationType;
  351. return locationType != LocationType.Remote &&
  352. locationType != LocationType.Virtual;
  353. }
  354. public virtual bool IsAuthorizedToDelete(User user)
  355. {
  356. return user.Policy.EnableContentDeletion;
  357. }
  358. public bool CanDelete(User user)
  359. {
  360. return CanDelete() && IsAuthorizedToDelete(user);
  361. }
  362. public virtual bool CanDownload()
  363. {
  364. return false;
  365. }
  366. public virtual bool IsAuthorizedToDownload(User user)
  367. {
  368. return user.Policy.EnableContentDownloading;
  369. }
  370. public bool CanDownload(User user)
  371. {
  372. return CanDownload() && IsAuthorizedToDownload(user);
  373. }
  374. /// <summary>
  375. /// Gets or sets the date created.
  376. /// </summary>
  377. /// <value>The date created.</value>
  378. [IgnoreDataMember]
  379. public DateTime DateCreated { get; set; }
  380. /// <summary>
  381. /// Gets or sets the date modified.
  382. /// </summary>
  383. /// <value>The date modified.</value>
  384. [IgnoreDataMember]
  385. public DateTime DateModified { get; set; }
  386. [IgnoreDataMember]
  387. public DateTime DateLastSaved { get; set; }
  388. [IgnoreDataMember]
  389. public DateTime DateLastRefreshed { get; set; }
  390. [IgnoreDataMember]
  391. public virtual bool EnableForceSaveOnDateModifiedChange
  392. {
  393. get { return false; }
  394. }
  395. /// <summary>
  396. /// The logger
  397. /// </summary>
  398. public static ILogger Logger { get; set; }
  399. public static ILibraryManager LibraryManager { get; set; }
  400. public static IServerConfigurationManager ConfigurationManager { get; set; }
  401. public static IProviderManager ProviderManager { get; set; }
  402. public static ILocalizationManager LocalizationManager { get; set; }
  403. public static IItemRepository ItemRepository { get; set; }
  404. public static IFileSystem FileSystem { get; set; }
  405. public static IUserDataManager UserDataManager { get; set; }
  406. public static ILiveTvManager LiveTvManager { get; set; }
  407. public static IChannelManager ChannelManager { get; set; }
  408. public static ICollectionManager CollectionManager { get; set; }
  409. public static IImageProcessor ImageProcessor { get; set; }
  410. public static IMediaSourceManager MediaSourceManager { get; set; }
  411. /// <summary>
  412. /// Returns a <see cref="System.String" /> that represents this instance.
  413. /// </summary>
  414. /// <returns>A <see cref="System.String" /> that represents this instance.</returns>
  415. public override string ToString()
  416. {
  417. return Name;
  418. }
  419. [IgnoreDataMember]
  420. public bool IsLocked { get; set; }
  421. /// <summary>
  422. /// Gets or sets the locked fields.
  423. /// </summary>
  424. /// <value>The locked fields.</value>
  425. [IgnoreDataMember]
  426. public List<MetadataFields> LockedFields { get; set; }
  427. /// <summary>
  428. /// Gets the type of the media.
  429. /// </summary>
  430. /// <value>The type of the media.</value>
  431. [IgnoreDataMember]
  432. public virtual string MediaType
  433. {
  434. get
  435. {
  436. return null;
  437. }
  438. }
  439. [IgnoreDataMember]
  440. public virtual IEnumerable<string> PhysicalLocations
  441. {
  442. get
  443. {
  444. var locationType = LocationType;
  445. if (locationType == LocationType.Remote || locationType == LocationType.Virtual)
  446. {
  447. return new string[] { };
  448. }
  449. return new[] { Path };
  450. }
  451. }
  452. private string _forcedSortName;
  453. /// <summary>
  454. /// Gets or sets the name of the forced sort.
  455. /// </summary>
  456. /// <value>The name of the forced sort.</value>
  457. [IgnoreDataMember]
  458. public string ForcedSortName
  459. {
  460. get { return _forcedSortName; }
  461. set { _forcedSortName = value; _sortName = null; }
  462. }
  463. private string _sortName;
  464. /// <summary>
  465. /// Gets the name of the sort.
  466. /// </summary>
  467. /// <value>The name of the sort.</value>
  468. [IgnoreDataMember]
  469. public string SortName
  470. {
  471. get
  472. {
  473. if (_sortName == null)
  474. {
  475. if (!string.IsNullOrWhiteSpace(ForcedSortName))
  476. {
  477. // Need the ToLower because that's what CreateSortName does
  478. _sortName = ModifySortChunks(ForcedSortName).ToLower();
  479. }
  480. else
  481. {
  482. _sortName = CreateSortName();
  483. }
  484. }
  485. return _sortName;
  486. }
  487. set
  488. {
  489. _sortName = value;
  490. }
  491. }
  492. public string GetInternalMetadataPath()
  493. {
  494. var basePath = ConfigurationManager.ApplicationPaths.InternalMetadataPath;
  495. return GetInternalMetadataPath(basePath);
  496. }
  497. protected virtual string GetInternalMetadataPath(string basePath)
  498. {
  499. if (SourceType == SourceType.Channel)
  500. {
  501. return System.IO.Path.Combine(basePath, "channels", ChannelId, Id.ToString("N"));
  502. }
  503. var idString = Id.ToString("N");
  504. basePath = System.IO.Path.Combine(basePath, "library");
  505. return System.IO.Path.Combine(basePath, idString.Substring(0, 2), idString);
  506. }
  507. /// <summary>
  508. /// Creates the name of the sort.
  509. /// </summary>
  510. /// <returns>System.String.</returns>
  511. protected virtual string CreateSortName()
  512. {
  513. if (Name == null) return null; //some items may not have name filled in properly
  514. if (!EnableAlphaNumericSorting)
  515. {
  516. return Name.TrimStart();
  517. }
  518. var sortable = Name.Trim().ToLower();
  519. sortable = ConfigurationManager.Configuration.SortRemoveCharacters.Aggregate(sortable, (current, search) => current.Replace(search.ToLower(), string.Empty));
  520. sortable = ConfigurationManager.Configuration.SortReplaceCharacters.Aggregate(sortable, (current, search) => current.Replace(search.ToLower(), " "));
  521. foreach (var search in ConfigurationManager.Configuration.SortRemoveWords)
  522. {
  523. var searchLower = search.ToLower();
  524. // Remove from beginning if a space follows
  525. if (sortable.StartsWith(searchLower + " "))
  526. {
  527. sortable = sortable.Remove(0, searchLower.Length + 1);
  528. }
  529. // Remove from middle if surrounded by spaces
  530. sortable = sortable.Replace(" " + searchLower + " ", " ");
  531. // Remove from end if followed by a space
  532. if (sortable.EndsWith(" " + searchLower))
  533. {
  534. sortable = sortable.Remove(sortable.Length - (searchLower.Length + 1));
  535. }
  536. }
  537. return ModifySortChunks(sortable);
  538. }
  539. private string ModifySortChunks(string name)
  540. {
  541. var chunks = GetSortChunks(name);
  542. var builder = new StringBuilder();
  543. foreach (var chunk in chunks)
  544. {
  545. var chunkBuilder = chunk.Item1;
  546. // This chunk is numeric
  547. if (chunk.Item2)
  548. {
  549. while (chunkBuilder.Length < 10)
  550. {
  551. chunkBuilder.Insert(0, '0');
  552. }
  553. }
  554. builder.Append(chunkBuilder);
  555. }
  556. //Logger.Debug("ModifySortChunks Start: {0} End: {1}", name, builder.ToString());
  557. return builder.ToString().RemoveDiacritics();
  558. }
  559. [IgnoreDataMember]
  560. public Guid ParentId { get; set; }
  561. /// <summary>
  562. /// Gets or sets the parent.
  563. /// </summary>
  564. /// <value>The parent.</value>
  565. [IgnoreDataMember]
  566. public Folder Parent
  567. {
  568. get { return GetParent() as Folder; }
  569. set
  570. {
  571. }
  572. }
  573. public void SetParent(Folder parent)
  574. {
  575. ParentId = parent == null ? Guid.Empty : parent.Id;
  576. }
  577. [IgnoreDataMember]
  578. public IEnumerable<Folder> Parents
  579. {
  580. get { return GetParents().OfType<Folder>(); }
  581. }
  582. public BaseItem GetParent()
  583. {
  584. if (ParentId != Guid.Empty)
  585. {
  586. return LibraryManager.GetItemById(ParentId);
  587. }
  588. return null;
  589. }
  590. public IEnumerable<BaseItem> GetParents()
  591. {
  592. var parent = GetParent();
  593. while (parent != null)
  594. {
  595. yield return parent;
  596. parent = parent.GetParent();
  597. }
  598. }
  599. /// <summary>
  600. /// Finds a parent of a given type
  601. /// </summary>
  602. /// <typeparam name="T"></typeparam>
  603. /// <returns>``0.</returns>
  604. public T FindParent<T>()
  605. where T : Folder
  606. {
  607. return GetParents().OfType<T>().FirstOrDefault();
  608. }
  609. [IgnoreDataMember]
  610. public virtual Guid? DisplayParentId
  611. {
  612. get
  613. {
  614. if (ParentId == Guid.Empty)
  615. {
  616. return null;
  617. }
  618. return ParentId;
  619. }
  620. }
  621. [IgnoreDataMember]
  622. public BaseItem DisplayParent
  623. {
  624. get
  625. {
  626. var id = DisplayParentId;
  627. if (!id.HasValue || id.Value == Guid.Empty)
  628. {
  629. return null;
  630. }
  631. return LibraryManager.GetItemById(id.Value);
  632. }
  633. }
  634. /// <summary>
  635. /// When the item first debuted. For movies this could be premiere date, episodes would be first aired
  636. /// </summary>
  637. /// <value>The premiere date.</value>
  638. [IgnoreDataMember]
  639. public DateTime? PremiereDate { get; set; }
  640. /// <summary>
  641. /// Gets or sets the end date.
  642. /// </summary>
  643. /// <value>The end date.</value>
  644. [IgnoreDataMember]
  645. public DateTime? EndDate { get; set; }
  646. /// <summary>
  647. /// Gets or sets the display type of the media.
  648. /// </summary>
  649. /// <value>The display type of the media.</value>
  650. [IgnoreDataMember]
  651. public string DisplayMediaType { get; set; }
  652. /// <summary>
  653. /// Gets or sets the official rating.
  654. /// </summary>
  655. /// <value>The official rating.</value>
  656. [IgnoreDataMember]
  657. public string OfficialRating { get; set; }
  658. /// <summary>
  659. /// Gets or sets the critic rating.
  660. /// </summary>
  661. /// <value>The critic rating.</value>
  662. [IgnoreDataMember]
  663. public float? CriticRating { get; set; }
  664. /// <summary>
  665. /// Gets or sets the critic rating summary.
  666. /// </summary>
  667. /// <value>The critic rating summary.</value>
  668. [IgnoreDataMember]
  669. public string CriticRatingSummary { get; set; }
  670. /// <summary>
  671. /// Gets or sets the official rating description.
  672. /// </summary>
  673. /// <value>The official rating description.</value>
  674. [IgnoreDataMember]
  675. public string OfficialRatingDescription { get; set; }
  676. /// <summary>
  677. /// Gets or sets the custom rating.
  678. /// </summary>
  679. /// <value>The custom rating.</value>
  680. [IgnoreDataMember]
  681. public string CustomRating { get; set; }
  682. /// <summary>
  683. /// Gets or sets the overview.
  684. /// </summary>
  685. /// <value>The overview.</value>
  686. [IgnoreDataMember]
  687. public string Overview { get; set; }
  688. /// <summary>
  689. /// Gets or sets the studios.
  690. /// </summary>
  691. /// <value>The studios.</value>
  692. [IgnoreDataMember]
  693. public List<string> Studios { get; set; }
  694. /// <summary>
  695. /// Gets or sets the genres.
  696. /// </summary>
  697. /// <value>The genres.</value>
  698. [IgnoreDataMember]
  699. public List<string> Genres { get; set; }
  700. /// <summary>
  701. /// Gets or sets the tags.
  702. /// </summary>
  703. /// <value>The tags.</value>
  704. [IgnoreDataMember]
  705. public List<string> Tags { get; set; }
  706. public List<string> Keywords { get; set; }
  707. /// <summary>
  708. /// Gets or sets the home page URL.
  709. /// </summary>
  710. /// <value>The home page URL.</value>
  711. [IgnoreDataMember]
  712. public string HomePageUrl { get; set; }
  713. /// <summary>
  714. /// Gets or sets the community rating.
  715. /// </summary>
  716. /// <value>The community rating.</value>
  717. [IgnoreDataMember]
  718. public float? CommunityRating { get; set; }
  719. /// <summary>
  720. /// Gets or sets the community rating vote count.
  721. /// </summary>
  722. /// <value>The community rating vote count.</value>
  723. [IgnoreDataMember]
  724. public int? VoteCount { get; set; }
  725. /// <summary>
  726. /// Gets or sets the run time ticks.
  727. /// </summary>
  728. /// <value>The run time ticks.</value>
  729. [IgnoreDataMember]
  730. public long? RunTimeTicks { get; set; }
  731. /// <summary>
  732. /// Gets or sets the production year.
  733. /// </summary>
  734. /// <value>The production year.</value>
  735. [IgnoreDataMember]
  736. public int? ProductionYear { get; set; }
  737. /// <summary>
  738. /// If the item is part of a series, this is it's number in the series.
  739. /// This could be episode number, album track number, etc.
  740. /// </summary>
  741. /// <value>The index number.</value>
  742. [IgnoreDataMember]
  743. public int? IndexNumber { get; set; }
  744. /// <summary>
  745. /// For an episode this could be the season number, or for a song this could be the disc number.
  746. /// </summary>
  747. /// <value>The parent index number.</value>
  748. [IgnoreDataMember]
  749. public int? ParentIndexNumber { get; set; }
  750. [IgnoreDataMember]
  751. public string OfficialRatingForComparison
  752. {
  753. get
  754. {
  755. if (!string.IsNullOrWhiteSpace(OfficialRating))
  756. {
  757. return OfficialRating;
  758. }
  759. var parent = DisplayParent;
  760. if (parent != null)
  761. {
  762. return parent.OfficialRatingForComparison;
  763. }
  764. return null;
  765. }
  766. }
  767. [IgnoreDataMember]
  768. public string CustomRatingForComparison
  769. {
  770. get
  771. {
  772. if (!string.IsNullOrWhiteSpace(CustomRating))
  773. {
  774. return CustomRating;
  775. }
  776. var parent = DisplayParent;
  777. if (parent != null)
  778. {
  779. return parent.CustomRatingForComparison;
  780. }
  781. return null;
  782. }
  783. }
  784. /// <summary>
  785. /// Gets the play access.
  786. /// </summary>
  787. /// <param name="user">The user.</param>
  788. /// <returns>PlayAccess.</returns>
  789. public PlayAccess GetPlayAccess(User user)
  790. {
  791. if (!user.Policy.EnableMediaPlayback)
  792. {
  793. return PlayAccess.None;
  794. }
  795. //if (!user.IsParentalScheduleAllowed())
  796. //{
  797. // return PlayAccess.None;
  798. //}
  799. return PlayAccess.Full;
  800. }
  801. /// <summary>
  802. /// Loads the theme songs.
  803. /// </summary>
  804. /// <returns>List{Audio.Audio}.</returns>
  805. private IEnumerable<Audio.Audio> LoadThemeSongs(List<FileSystemMetadata> fileSystemChildren, IDirectoryService directoryService)
  806. {
  807. var files = fileSystemChildren.Where(i => i.IsDirectory)
  808. .Where(i => string.Equals(i.Name, ThemeSongsFolderName, StringComparison.OrdinalIgnoreCase))
  809. .SelectMany(i => directoryService.GetFiles(i.FullName))
  810. .ToList();
  811. // Support plex/xbmc convention
  812. files.AddRange(fileSystemChildren
  813. .Where(i => !i.IsDirectory && string.Equals(FileSystem.GetFileNameWithoutExtension(i), ThemeSongFilename, StringComparison.OrdinalIgnoreCase))
  814. );
  815. return LibraryManager.ResolvePaths(files, directoryService, null)
  816. .OfType<Audio.Audio>()
  817. .Select(audio =>
  818. {
  819. // Try to retrieve it from the db. If we don't find it, use the resolved version
  820. var dbItem = LibraryManager.GetItemById(audio.Id) as Audio.Audio;
  821. if (dbItem != null)
  822. {
  823. audio = dbItem;
  824. }
  825. audio.ExtraType = ExtraType.ThemeSong;
  826. return audio;
  827. // Sort them so that the list can be easily compared for changes
  828. }).OrderBy(i => i.Path).ToList();
  829. }
  830. /// <summary>
  831. /// Loads the video backdrops.
  832. /// </summary>
  833. /// <returns>List{Video}.</returns>
  834. private IEnumerable<Video> LoadThemeVideos(IEnumerable<FileSystemMetadata> fileSystemChildren, IDirectoryService directoryService)
  835. {
  836. var files = fileSystemChildren.Where(i => i.IsDirectory)
  837. .Where(i => string.Equals(i.Name, ThemeVideosFolderName, StringComparison.OrdinalIgnoreCase))
  838. .SelectMany(i => directoryService.GetFiles(i.FullName));
  839. return LibraryManager.ResolvePaths(files, directoryService, null)
  840. .OfType<Video>()
  841. .Select(item =>
  842. {
  843. // Try to retrieve it from the db. If we don't find it, use the resolved version
  844. var dbItem = LibraryManager.GetItemById(item.Id) as Video;
  845. if (dbItem != null)
  846. {
  847. item = dbItem;
  848. }
  849. item.ExtraType = ExtraType.ThemeVideo;
  850. return item;
  851. // Sort them so that the list can be easily compared for changes
  852. }).OrderBy(i => i.Path).ToList();
  853. }
  854. public Task RefreshMetadata(CancellationToken cancellationToken)
  855. {
  856. return RefreshMetadata(new MetadataRefreshOptions(new DirectoryService(Logger, FileSystem)), cancellationToken);
  857. }
  858. /// <summary>
  859. /// Overrides the base implementation to refresh metadata for local trailers
  860. /// </summary>
  861. /// <param name="options">The options.</param>
  862. /// <param name="cancellationToken">The cancellation token.</param>
  863. /// <returns>true if a provider reports we changed</returns>
  864. public async Task<ItemUpdateType> RefreshMetadata(MetadataRefreshOptions options, CancellationToken cancellationToken)
  865. {
  866. var locationType = LocationType;
  867. var requiresSave = false;
  868. if (SupportsOwnedItems)
  869. {
  870. try
  871. {
  872. var files = locationType != LocationType.Remote && locationType != LocationType.Virtual ?
  873. GetFileSystemChildren(options.DirectoryService).ToList() :
  874. new List<FileSystemMetadata>();
  875. var ownedItemsChanged = await RefreshedOwnedItems(options, files, cancellationToken).ConfigureAwait(false);
  876. if (ownedItemsChanged)
  877. {
  878. requiresSave = true;
  879. }
  880. }
  881. catch (Exception ex)
  882. {
  883. Logger.ErrorException("Error refreshing owned items for {0}", ex, Path ?? Name);
  884. }
  885. }
  886. var refreshOptions = requiresSave
  887. ? new MetadataRefreshOptions(options)
  888. {
  889. ForceSave = true
  890. }
  891. : options;
  892. return await ProviderManager.RefreshSingleItem(this, refreshOptions, cancellationToken).ConfigureAwait(false);
  893. }
  894. [IgnoreDataMember]
  895. protected virtual bool SupportsOwnedItems
  896. {
  897. get { return IsFolder || GetParent() != null; }
  898. }
  899. [IgnoreDataMember]
  900. public virtual bool SupportsPeople
  901. {
  902. get { return true; }
  903. }
  904. /// <summary>
  905. /// Refreshes owned items such as trailers, theme videos, special features, etc.
  906. /// Returns true or false indicating if changes were found.
  907. /// </summary>
  908. /// <param name="options"></param>
  909. /// <param name="fileSystemChildren"></param>
  910. /// <param name="cancellationToken"></param>
  911. /// <returns></returns>
  912. protected virtual async Task<bool> RefreshedOwnedItems(MetadataRefreshOptions options, List<FileSystemMetadata> fileSystemChildren, CancellationToken cancellationToken)
  913. {
  914. var themeSongsChanged = false;
  915. var themeVideosChanged = false;
  916. var localTrailersChanged = false;
  917. if (LocationType == LocationType.FileSystem && GetParent() != null)
  918. {
  919. var hasThemeMedia = this as IHasThemeMedia;
  920. if (hasThemeMedia != null)
  921. {
  922. if (!IsInMixedFolder)
  923. {
  924. themeSongsChanged = await RefreshThemeSongs(hasThemeMedia, options, fileSystemChildren, cancellationToken).ConfigureAwait(false);
  925. themeVideosChanged = await RefreshThemeVideos(hasThemeMedia, options, fileSystemChildren, cancellationToken).ConfigureAwait(false);
  926. }
  927. }
  928. var hasTrailers = this as IHasTrailers;
  929. if (hasTrailers != null)
  930. {
  931. localTrailersChanged = await RefreshLocalTrailers(hasTrailers, options, fileSystemChildren, cancellationToken).ConfigureAwait(false);
  932. }
  933. }
  934. return themeSongsChanged || themeVideosChanged || localTrailersChanged;
  935. }
  936. protected virtual IEnumerable<FileSystemMetadata> GetFileSystemChildren(IDirectoryService directoryService)
  937. {
  938. var path = ContainingFolderPath;
  939. return directoryService.GetFileSystemEntries(path);
  940. }
  941. private async Task<bool> RefreshLocalTrailers(IHasTrailers item, MetadataRefreshOptions options, List<FileSystemMetadata> fileSystemChildren, CancellationToken cancellationToken)
  942. {
  943. var newItems = LibraryManager.FindTrailers(this, fileSystemChildren, options.DirectoryService).ToList();
  944. var newItemIds = newItems.Select(i => i.Id).ToList();
  945. var itemsChanged = !item.LocalTrailerIds.SequenceEqual(newItemIds);
  946. var tasks = newItems.Select(i => i.RefreshMetadata(options, cancellationToken));
  947. await Task.WhenAll(tasks).ConfigureAwait(false);
  948. item.LocalTrailerIds = newItemIds;
  949. return itemsChanged;
  950. }
  951. private async Task<bool> RefreshThemeVideos(IHasThemeMedia item, MetadataRefreshOptions options, IEnumerable<FileSystemMetadata> fileSystemChildren, CancellationToken cancellationToken)
  952. {
  953. var newThemeVideos = LoadThemeVideos(fileSystemChildren, options.DirectoryService).ToList();
  954. var newThemeVideoIds = newThemeVideos.Select(i => i.Id).ToList();
  955. var themeVideosChanged = !item.ThemeVideoIds.SequenceEqual(newThemeVideoIds);
  956. var tasks = newThemeVideos.Select(i =>
  957. {
  958. var subOptions = new MetadataRefreshOptions(options);
  959. if (!i.IsThemeMedia)
  960. {
  961. i.ExtraType = ExtraType.ThemeVideo;
  962. subOptions.ForceSave = true;
  963. }
  964. return i.RefreshMetadata(subOptions, cancellationToken);
  965. });
  966. await Task.WhenAll(tasks).ConfigureAwait(false);
  967. item.ThemeVideoIds = newThemeVideoIds;
  968. return themeVideosChanged;
  969. }
  970. /// <summary>
  971. /// Refreshes the theme songs.
  972. /// </summary>
  973. private async Task<bool> RefreshThemeSongs(IHasThemeMedia item, MetadataRefreshOptions options, List<FileSystemMetadata> fileSystemChildren, CancellationToken cancellationToken)
  974. {
  975. var newThemeSongs = LoadThemeSongs(fileSystemChildren, options.DirectoryService).ToList();
  976. var newThemeSongIds = newThemeSongs.Select(i => i.Id).ToList();
  977. var themeSongsChanged = !item.ThemeSongIds.SequenceEqual(newThemeSongIds);
  978. var tasks = newThemeSongs.Select(i =>
  979. {
  980. var subOptions = new MetadataRefreshOptions(options);
  981. if (!i.IsThemeMedia)
  982. {
  983. i.ExtraType = ExtraType.ThemeSong;
  984. subOptions.ForceSave = true;
  985. }
  986. return i.RefreshMetadata(subOptions, cancellationToken);
  987. });
  988. await Task.WhenAll(tasks).ConfigureAwait(false);
  989. item.ThemeSongIds = newThemeSongIds;
  990. return themeSongsChanged;
  991. }
  992. /// <summary>
  993. /// Gets or sets the provider ids.
  994. /// </summary>
  995. /// <value>The provider ids.</value>
  996. public Dictionary<string, string> ProviderIds { get; set; }
  997. [IgnoreDataMember]
  998. public virtual Folder LatestItemsIndexContainer
  999. {
  1000. get { return null; }
  1001. }
  1002. public virtual string CreatePresentationUniqueKey()
  1003. {
  1004. return Id.ToString("N");
  1005. }
  1006. [IgnoreDataMember]
  1007. public string PresentationUniqueKey { get; set; }
  1008. public string GetPresentationUniqueKey()
  1009. {
  1010. return PresentationUniqueKey ?? CreatePresentationUniqueKey();
  1011. }
  1012. public virtual bool RequiresRefresh()
  1013. {
  1014. return false;
  1015. }
  1016. public virtual List<string> GetUserDataKeys()
  1017. {
  1018. var list = new List<string>();
  1019. if (SourceType == SourceType.Channel)
  1020. {
  1021. if (!string.IsNullOrWhiteSpace(ExternalId))
  1022. {
  1023. list.Add(ExternalId);
  1024. }
  1025. }
  1026. list.Add(Id.ToString());
  1027. return list;
  1028. }
  1029. internal virtual bool IsValidFromResolver(BaseItem newItem)
  1030. {
  1031. var current = this;
  1032. return current.IsInMixedFolder == newItem.IsInMixedFolder;
  1033. }
  1034. public void AfterMetadataRefresh()
  1035. {
  1036. _sortName = null;
  1037. }
  1038. /// <summary>
  1039. /// Gets the preferred metadata language.
  1040. /// </summary>
  1041. /// <returns>System.String.</returns>
  1042. public string GetPreferredMetadataLanguage()
  1043. {
  1044. string lang = PreferredMetadataLanguage;
  1045. if (string.IsNullOrWhiteSpace(lang))
  1046. {
  1047. lang = GetParents()
  1048. .Select(i => i.PreferredMetadataLanguage)
  1049. .FirstOrDefault(i => !string.IsNullOrWhiteSpace(i));
  1050. }
  1051. if (string.IsNullOrWhiteSpace(lang))
  1052. {
  1053. lang = LibraryManager.GetCollectionFolders(this)
  1054. .Select(i => i.PreferredMetadataLanguage)
  1055. .FirstOrDefault(i => !string.IsNullOrWhiteSpace(i));
  1056. }
  1057. if (string.IsNullOrWhiteSpace(lang))
  1058. {
  1059. lang = ConfigurationManager.Configuration.PreferredMetadataLanguage;
  1060. }
  1061. return lang;
  1062. }
  1063. /// <summary>
  1064. /// Gets the preferred metadata language.
  1065. /// </summary>
  1066. /// <returns>System.String.</returns>
  1067. public string GetPreferredMetadataCountryCode()
  1068. {
  1069. string lang = PreferredMetadataCountryCode;
  1070. if (string.IsNullOrWhiteSpace(lang))
  1071. {
  1072. lang = GetParents()
  1073. .Select(i => i.PreferredMetadataCountryCode)
  1074. .FirstOrDefault(i => !string.IsNullOrWhiteSpace(i));
  1075. }
  1076. if (string.IsNullOrWhiteSpace(lang))
  1077. {
  1078. lang = LibraryManager.GetCollectionFolders(this)
  1079. .Select(i => i.PreferredMetadataCountryCode)
  1080. .FirstOrDefault(i => !string.IsNullOrWhiteSpace(i));
  1081. }
  1082. if (string.IsNullOrWhiteSpace(lang))
  1083. {
  1084. lang = ConfigurationManager.Configuration.MetadataCountryCode;
  1085. }
  1086. return lang;
  1087. }
  1088. public virtual bool IsSaveLocalMetadataEnabled()
  1089. {
  1090. if (SourceType == SourceType.Channel)
  1091. {
  1092. return false;
  1093. }
  1094. return ConfigurationManager.Configuration.SaveLocalMeta;
  1095. }
  1096. /// <summary>
  1097. /// Determines if a given user has access to this item
  1098. /// </summary>
  1099. /// <param name="user">The user.</param>
  1100. /// <returns><c>true</c> if [is parental allowed] [the specified user]; otherwise, <c>false</c>.</returns>
  1101. /// <exception cref="System.ArgumentNullException">user</exception>
  1102. public bool IsParentalAllowed(User user)
  1103. {
  1104. if (user == null)
  1105. {
  1106. throw new ArgumentNullException("user");
  1107. }
  1108. if (!IsVisibleViaTags(user))
  1109. {
  1110. return false;
  1111. }
  1112. var maxAllowedRating = user.Policy.MaxParentalRating;
  1113. if (maxAllowedRating == null)
  1114. {
  1115. return true;
  1116. }
  1117. var rating = CustomRatingForComparison;
  1118. if (string.IsNullOrWhiteSpace(rating))
  1119. {
  1120. rating = OfficialRatingForComparison;
  1121. }
  1122. if (string.IsNullOrWhiteSpace(rating))
  1123. {
  1124. return !GetBlockUnratedValue(user.Policy);
  1125. }
  1126. var value = LocalizationManager.GetRatingLevel(rating);
  1127. // Could not determine the integer value
  1128. if (!value.HasValue)
  1129. {
  1130. var isAllowed = !GetBlockUnratedValue(user.Policy);
  1131. if (!isAllowed)
  1132. {
  1133. Logger.Debug("{0} has an unrecognized parental rating of {1}.", Name, rating);
  1134. }
  1135. return isAllowed;
  1136. }
  1137. return value.Value <= maxAllowedRating.Value;
  1138. }
  1139. public int? GetParentalRatingValue()
  1140. {
  1141. var rating = CustomRating;
  1142. if (string.IsNullOrWhiteSpace(rating))
  1143. {
  1144. rating = OfficialRating;
  1145. }
  1146. if (string.IsNullOrWhiteSpace(rating))
  1147. {
  1148. return null;
  1149. }
  1150. return LocalizationManager.GetRatingLevel(rating);
  1151. }
  1152. public int? GetInheritedParentalRatingValue()
  1153. {
  1154. var rating = CustomRatingForComparison;
  1155. if (string.IsNullOrWhiteSpace(rating))
  1156. {
  1157. rating = OfficialRatingForComparison;
  1158. }
  1159. if (string.IsNullOrWhiteSpace(rating))
  1160. {
  1161. return null;
  1162. }
  1163. return LocalizationManager.GetRatingLevel(rating);
  1164. }
  1165. public List<string> GetInheritedTags()
  1166. {
  1167. var list = new List<string>();
  1168. list.AddRange(Tags);
  1169. foreach (var parent in GetParents())
  1170. {
  1171. list.AddRange(parent.Tags);
  1172. }
  1173. return list.Distinct(StringComparer.OrdinalIgnoreCase).ToList();
  1174. }
  1175. private bool IsVisibleViaTags(User user)
  1176. {
  1177. var policy = user.Policy;
  1178. if (policy.BlockedTags.Any(i => Tags.Contains(i, StringComparer.OrdinalIgnoreCase)))
  1179. {
  1180. return false;
  1181. }
  1182. return true;
  1183. }
  1184. protected virtual bool IsAllowTagFilterEnforced()
  1185. {
  1186. return true;
  1187. }
  1188. public virtual UnratedItem GetBlockUnratedType()
  1189. {
  1190. if (SourceType == SourceType.Channel)
  1191. {
  1192. return UnratedItem.ChannelContent;
  1193. }
  1194. return UnratedItem.Other;
  1195. }
  1196. /// <summary>
  1197. /// Gets the block unrated value.
  1198. /// </summary>
  1199. /// <param name="config">The configuration.</param>
  1200. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  1201. protected virtual bool GetBlockUnratedValue(UserPolicy config)
  1202. {
  1203. // Don't block plain folders that are unrated. Let the media underneath get blocked
  1204. // Special folders like series and albums will override this method.
  1205. if (IsFolder)
  1206. {
  1207. return false;
  1208. }
  1209. if (this is IItemByName)
  1210. {
  1211. return false;
  1212. }
  1213. return config.BlockUnratedItems.Contains(GetBlockUnratedType());
  1214. }
  1215. /// <summary>
  1216. /// Determines if this folder should be visible to a given user.
  1217. /// Default is just parental allowed. Can be overridden for more functionality.
  1218. /// </summary>
  1219. /// <param name="user">The user.</param>
  1220. /// <returns><c>true</c> if the specified user is visible; otherwise, <c>false</c>.</returns>
  1221. /// <exception cref="System.ArgumentNullException">user</exception>
  1222. public virtual bool IsVisible(User user)
  1223. {
  1224. if (user == null)
  1225. {
  1226. throw new ArgumentNullException("user");
  1227. }
  1228. return IsParentalAllowed(user);
  1229. }
  1230. public virtual bool IsVisibleStandalone(User user)
  1231. {
  1232. if (SourceType == SourceType.Channel)
  1233. {
  1234. return IsVisibleStandaloneInternal(user, false) && Channel.IsChannelVisible(this, user);
  1235. }
  1236. return IsVisibleStandaloneInternal(user, true);
  1237. }
  1238. protected bool IsVisibleStandaloneInternal(User user, bool checkFolders)
  1239. {
  1240. if (!IsVisible(user))
  1241. {
  1242. return false;
  1243. }
  1244. if (GetParents().Any(i => !i.IsVisible(user)))
  1245. {
  1246. return false;
  1247. }
  1248. if (checkFolders)
  1249. {
  1250. var topParent = GetParents().LastOrDefault() ?? this;
  1251. if (string.IsNullOrWhiteSpace(topParent.Path))
  1252. {
  1253. return true;
  1254. }
  1255. var userCollectionFolders = user.RootFolder.GetChildren(user, true).Select(i => i.Id).ToList();
  1256. var itemCollectionFolders = LibraryManager.GetCollectionFolders(this).Select(i => i.Id);
  1257. if (!itemCollectionFolders.Any(userCollectionFolders.Contains))
  1258. {
  1259. return false;
  1260. }
  1261. }
  1262. return true;
  1263. }
  1264. /// <summary>
  1265. /// Gets a value indicating whether this instance is folder.
  1266. /// </summary>
  1267. /// <value><c>true</c> if this instance is folder; otherwise, <c>false</c>.</value>
  1268. [IgnoreDataMember]
  1269. public virtual bool IsFolder
  1270. {
  1271. get
  1272. {
  1273. return false;
  1274. }
  1275. }
  1276. public virtual string GetClientTypeName()
  1277. {
  1278. if (IsFolder && SourceType == SourceType.Channel && !(this is Channel))
  1279. {
  1280. return "ChannelFolderItem";
  1281. }
  1282. return GetType().Name;
  1283. }
  1284. /// <summary>
  1285. /// Gets the linked child.
  1286. /// </summary>
  1287. /// <param name="info">The info.</param>
  1288. /// <returns>BaseItem.</returns>
  1289. protected BaseItem GetLinkedChild(LinkedChild info)
  1290. {
  1291. // First get using the cached Id
  1292. if (info.ItemId.HasValue)
  1293. {
  1294. if (info.ItemId.Value == Guid.Empty)
  1295. {
  1296. return null;
  1297. }
  1298. var itemById = LibraryManager.GetItemById(info.ItemId.Value);
  1299. if (itemById != null)
  1300. {
  1301. return itemById;
  1302. }
  1303. }
  1304. var item = FindLinkedChild(info);
  1305. // If still null, log
  1306. if (item == null)
  1307. {
  1308. // Don't keep searching over and over
  1309. info.ItemId = Guid.Empty;
  1310. }
  1311. else
  1312. {
  1313. // Cache the id for next time
  1314. info.ItemId = item.Id;
  1315. }
  1316. return item;
  1317. }
  1318. private BaseItem FindLinkedChild(LinkedChild info)
  1319. {
  1320. if (!string.IsNullOrEmpty(info.Path))
  1321. {
  1322. var itemByPath = LibraryManager.FindByPath(info.Path, null);
  1323. if (itemByPath == null)
  1324. {
  1325. //Logger.Warn("Unable to find linked item at path {0}", info.Path);
  1326. }
  1327. return itemByPath;
  1328. }
  1329. return null;
  1330. }
  1331. [IgnoreDataMember]
  1332. public virtual bool EnableRememberingTrackSelections
  1333. {
  1334. get
  1335. {
  1336. return true;
  1337. }
  1338. }
  1339. /// <summary>
  1340. /// Adds a studio to the item
  1341. /// </summary>
  1342. /// <param name="name">The name.</param>
  1343. /// <exception cref="System.ArgumentNullException"></exception>
  1344. public void AddStudio(string name)
  1345. {
  1346. if (string.IsNullOrWhiteSpace(name))
  1347. {
  1348. throw new ArgumentNullException("name");
  1349. }
  1350. if (!Studios.Contains(name, StringComparer.OrdinalIgnoreCase))
  1351. {
  1352. Studios.Add(name);
  1353. }
  1354. }
  1355. /// <summary>
  1356. /// Adds a genre to the item
  1357. /// </summary>
  1358. /// <param name="name">The name.</param>
  1359. /// <exception cref="System.ArgumentNullException"></exception>
  1360. public void AddGenre(string name)
  1361. {
  1362. if (string.IsNullOrWhiteSpace(name))
  1363. {
  1364. throw new ArgumentNullException("name");
  1365. }
  1366. if (!Genres.Contains(name, StringComparer.OrdinalIgnoreCase))
  1367. {
  1368. Genres.Add(name);
  1369. }
  1370. }
  1371. /// <summary>
  1372. /// Marks the played.
  1373. /// </summary>
  1374. /// <param name="user">The user.</param>
  1375. /// <param name="datePlayed">The date played.</param>
  1376. /// <param name="resetPosition">if set to <c>true</c> [reset position].</param>
  1377. /// <returns>Task.</returns>
  1378. /// <exception cref="System.ArgumentNullException"></exception>
  1379. public virtual async Task MarkPlayed(User user,
  1380. DateTime? datePlayed,
  1381. bool resetPosition)
  1382. {
  1383. if (user == null)
  1384. {
  1385. throw new ArgumentNullException();
  1386. }
  1387. var data = UserDataManager.GetUserData(user, this);
  1388. if (datePlayed.HasValue)
  1389. {
  1390. // Increment
  1391. data.PlayCount++;
  1392. }
  1393. // Ensure it's at least one
  1394. data.PlayCount = Math.Max(data.PlayCount, 1);
  1395. if (resetPosition)
  1396. {
  1397. data.PlaybackPositionTicks = 0;
  1398. }
  1399. data.LastPlayedDate = datePlayed ?? data.LastPlayedDate ?? DateTime.UtcNow;
  1400. data.Played = true;
  1401. await UserDataManager.SaveUserData(user.Id, this, data, UserDataSaveReason.TogglePlayed, CancellationToken.None).ConfigureAwait(false);
  1402. }
  1403. /// <summary>
  1404. /// Marks the unplayed.
  1405. /// </summary>
  1406. /// <param name="user">The user.</param>
  1407. /// <returns>Task.</returns>
  1408. /// <exception cref="System.ArgumentNullException"></exception>
  1409. public virtual async Task MarkUnplayed(User user)
  1410. {
  1411. if (user == null)
  1412. {
  1413. throw new ArgumentNullException();
  1414. }
  1415. var data = UserDataManager.GetUserData(user, this);
  1416. //I think it is okay to do this here.
  1417. // if this is only called when a user is manually forcing something to un-played
  1418. // then it probably is what we want to do...
  1419. data.PlayCount = 0;
  1420. data.PlaybackPositionTicks = 0;
  1421. data.LastPlayedDate = null;
  1422. data.Played = false;
  1423. await UserDataManager.SaveUserData(user.Id, this, data, UserDataSaveReason.TogglePlayed, CancellationToken.None).ConfigureAwait(false);
  1424. }
  1425. /// <summary>
  1426. /// Do whatever refreshing is necessary when the filesystem pertaining to this item has changed.
  1427. /// </summary>
  1428. /// <returns>Task.</returns>
  1429. public virtual Task ChangedExternally()
  1430. {
  1431. ProviderManager.QueueRefresh(Id, new MetadataRefreshOptions(FileSystem));
  1432. return Task.FromResult(true);
  1433. }
  1434. /// <summary>
  1435. /// Gets an image
  1436. /// </summary>
  1437. /// <param name="type">The type.</param>
  1438. /// <param name="imageIndex">Index of the image.</param>
  1439. /// <returns><c>true</c> if the specified type has image; otherwise, <c>false</c>.</returns>
  1440. /// <exception cref="System.ArgumentException">Backdrops should be accessed using Item.Backdrops</exception>
  1441. public bool HasImage(ImageType type, int imageIndex)
  1442. {
  1443. return GetImageInfo(type, imageIndex) != null;
  1444. }
  1445. public void SetImage(ItemImageInfo image, int index)
  1446. {
  1447. if (image.Type == ImageType.Chapter)
  1448. {
  1449. throw new ArgumentException("Cannot set chapter images using SetImagePath");
  1450. }
  1451. var existingImage = GetImageInfo(image.Type, index);
  1452. if (existingImage != null)
  1453. {
  1454. ImageInfos.Remove(existingImage);
  1455. }
  1456. ImageInfos.Add(image);
  1457. }
  1458. public void SetImagePath(ImageType type, int index, FileSystemMetadata file)
  1459. {
  1460. if (type == ImageType.Chapter)
  1461. {
  1462. throw new ArgumentException("Cannot set chapter images using SetImagePath");
  1463. }
  1464. var image = GetImageInfo(type, index);
  1465. if (image == null)
  1466. {
  1467. ImageInfos.Add(GetImageInfo(file, type));
  1468. }
  1469. else
  1470. {
  1471. var imageInfo = GetImageInfo(file, type);
  1472. image.Path = file.FullName;
  1473. image.DateModified = imageInfo.DateModified;
  1474. image.IsPlaceholder = false;
  1475. }
  1476. }
  1477. /// <summary>
  1478. /// Deletes the image.
  1479. /// </summary>
  1480. /// <param name="type">The type.</param>
  1481. /// <param name="index">The index.</param>
  1482. /// <returns>Task.</returns>
  1483. public Task DeleteImage(ImageType type, int index)
  1484. {
  1485. var info = GetImageInfo(type, index);
  1486. if (info == null)
  1487. {
  1488. // Nothing to do
  1489. return Task.FromResult(true);
  1490. }
  1491. // Remove it from the item
  1492. RemoveImage(info);
  1493. if (info.IsLocalFile)
  1494. {
  1495. // Delete the source file
  1496. var currentFile = new FileInfo(info.Path);
  1497. // Deletion will fail if the file is hidden so remove the attribute first
  1498. if (currentFile.Exists)
  1499. {
  1500. if ((currentFile.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
  1501. {
  1502. currentFile.Attributes &= ~FileAttributes.Hidden;
  1503. }
  1504. FileSystem.DeleteFile(currentFile.FullName);
  1505. }
  1506. }
  1507. return UpdateToRepository(ItemUpdateType.ImageUpdate, CancellationToken.None);
  1508. }
  1509. public void RemoveImage(ItemImageInfo image)
  1510. {
  1511. ImageInfos.Remove(image);
  1512. }
  1513. public virtual Task UpdateToRepository(ItemUpdateType updateReason, CancellationToken cancellationToken)
  1514. {
  1515. return LibraryManager.UpdateItem(this, updateReason, cancellationToken);
  1516. }
  1517. /// <summary>
  1518. /// Validates that images within the item are still on the file system
  1519. /// </summary>
  1520. public bool ValidateImages(IDirectoryService directoryService)
  1521. {
  1522. var allFiles = ImageInfos
  1523. .Where(i => i.IsLocalFile)
  1524. .Select(i => System.IO.Path.GetDirectoryName(i.Path))
  1525. .Distinct(StringComparer.OrdinalIgnoreCase)
  1526. .SelectMany(directoryService.GetFiles)
  1527. .Select(i => i.FullName)
  1528. .ToList();
  1529. var deletedImages = ImageInfos
  1530. .Where(image => image.IsLocalFile && !allFiles.Contains(image.Path, StringComparer.OrdinalIgnoreCase))
  1531. .ToList();
  1532. if (deletedImages.Count > 0)
  1533. {
  1534. ImageInfos = ImageInfos.Except(deletedImages).ToList();
  1535. }
  1536. return deletedImages.Count > 0;
  1537. }
  1538. /// <summary>
  1539. /// Gets the image path.
  1540. /// </summary>
  1541. /// <param name="imageType">Type of the image.</param>
  1542. /// <param name="imageIndex">Index of the image.</param>
  1543. /// <returns>System.String.</returns>
  1544. /// <exception cref="System.InvalidOperationException">
  1545. /// </exception>
  1546. /// <exception cref="System.ArgumentNullException">item</exception>
  1547. public string GetImagePath(ImageType imageType, int imageIndex)
  1548. {
  1549. var info = GetImageInfo(imageType, imageIndex);
  1550. return info == null ? null : info.Path;
  1551. }
  1552. /// <summary>
  1553. /// Gets the image information.
  1554. /// </summary>
  1555. /// <param name="imageType">Type of the image.</param>
  1556. /// <param name="imageIndex">Index of the image.</param>
  1557. /// <returns>ItemImageInfo.</returns>
  1558. public ItemImageInfo GetImageInfo(ImageType imageType, int imageIndex)
  1559. {
  1560. if (imageType == ImageType.Chapter)
  1561. {
  1562. var chapter = ItemRepository.GetChapter(Id, imageIndex);
  1563. if (chapter == null)
  1564. {
  1565. return null;
  1566. }
  1567. var path = chapter.ImagePath;
  1568. if (string.IsNullOrWhiteSpace(path))
  1569. {
  1570. return null;
  1571. }
  1572. return new ItemImageInfo
  1573. {
  1574. Path = path,
  1575. DateModified = chapter.ImageDateModified,
  1576. Type = imageType
  1577. };
  1578. }
  1579. return GetImages(imageType)
  1580. .ElementAtOrDefault(imageIndex);
  1581. }
  1582. public IEnumerable<ItemImageInfo> GetImages(ImageType imageType)
  1583. {
  1584. if (imageType == ImageType.Chapter)
  1585. {
  1586. throw new ArgumentException("No image info for chapter images");
  1587. }
  1588. return ImageInfos.Where(i => i.Type == imageType);
  1589. }
  1590. /// <summary>
  1591. /// Adds the images.
  1592. /// </summary>
  1593. /// <param name="imageType">Type of the image.</param>
  1594. /// <param name="images">The images.</param>
  1595. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  1596. /// <exception cref="System.ArgumentException">Cannot call AddImages with chapter images</exception>
  1597. public bool AddImages(ImageType imageType, List<FileSystemMetadata> images)
  1598. {
  1599. if (imageType == ImageType.Chapter)
  1600. {
  1601. throw new ArgumentException("Cannot call AddImages with chapter images");
  1602. }
  1603. var existingImages = GetImages(imageType)
  1604. .ToList();
  1605. var newImageList = new List<FileSystemMetadata>();
  1606. var imageAdded = false;
  1607. foreach (var newImage in images)
  1608. {
  1609. if (newImage == null)
  1610. {
  1611. throw new ArgumentException("null image found in list");
  1612. }
  1613. var existing = existingImages
  1614. .FirstOrDefault(i => string.Equals(i.Path, newImage.FullName, StringComparison.OrdinalIgnoreCase));
  1615. if (existing == null)
  1616. {
  1617. newImageList.Add(newImage);
  1618. imageAdded = true;
  1619. }
  1620. else
  1621. {
  1622. if (existing.IsLocalFile)
  1623. {
  1624. existing.DateModified = FileSystem.GetLastWriteTimeUtc(newImage);
  1625. }
  1626. }
  1627. }
  1628. if (imageAdded || images.Count != existingImages.Count)
  1629. {
  1630. var newImagePaths = images.Select(i => i.FullName).ToList();
  1631. var deleted = existingImages
  1632. .Where(i => i.IsLocalFile && !newImagePaths.Contains(i.Path, StringComparer.OrdinalIgnoreCase) && !FileSystem.FileExists(i.Path))
  1633. .ToList();
  1634. ImageInfos = ImageInfos.Except(deleted).ToList();
  1635. }
  1636. ImageInfos.AddRange(newImageList.Select(i => GetImageInfo(i, imageType)));
  1637. return newImageList.Count > 0;
  1638. }
  1639. private ItemImageInfo GetImageInfo(FileSystemMetadata file, ImageType type)
  1640. {
  1641. return new ItemImageInfo
  1642. {
  1643. Path = file.FullName,
  1644. Type = type,
  1645. DateModified = FileSystem.GetLastWriteTimeUtc(file)
  1646. };
  1647. }
  1648. /// <summary>
  1649. /// Gets the file system path to delete when the item is to be deleted
  1650. /// </summary>
  1651. /// <returns></returns>
  1652. public virtual IEnumerable<string> GetDeletePaths()
  1653. {
  1654. return new[] { Path };
  1655. }
  1656. public bool AllowsMultipleImages(ImageType type)
  1657. {
  1658. return type == ImageType.Backdrop || type == ImageType.Screenshot || type == ImageType.Chapter;
  1659. }
  1660. public Task SwapImages(ImageType type, int index1, int index2)
  1661. {
  1662. if (!AllowsMultipleImages(type))
  1663. {
  1664. throw new ArgumentException("The change index operation is only applicable to backdrops and screenshots");
  1665. }
  1666. var info1 = GetImageInfo(type, index1);
  1667. var info2 = GetImageInfo(type, index2);
  1668. if (info1 == null || info2 == null)
  1669. {
  1670. // Nothing to do
  1671. return Task.FromResult(true);
  1672. }
  1673. if (!info1.IsLocalFile || !info2.IsLocalFile)
  1674. {
  1675. // TODO: Not supported yet
  1676. return Task.FromResult(true);
  1677. }
  1678. var path1 = info1.Path;
  1679. var path2 = info2.Path;
  1680. FileSystem.SwapFiles(path1, path2);
  1681. // Refresh these values
  1682. info1.DateModified = FileSystem.GetLastWriteTimeUtc(info1.Path);
  1683. info2.DateModified = FileSystem.GetLastWriteTimeUtc(info2.Path);
  1684. return UpdateToRepository(ItemUpdateType.ImageUpdate, CancellationToken.None);
  1685. }
  1686. public virtual bool IsPlayed(User user)
  1687. {
  1688. var userdata = UserDataManager.GetUserData(user, this);
  1689. return userdata != null && userdata.Played;
  1690. }
  1691. public bool IsFavoriteOrLiked(User user)
  1692. {
  1693. var userdata = UserDataManager.GetUserData(user, this);
  1694. return userdata != null && (userdata.IsFavorite || (userdata.Likes ?? false));
  1695. }
  1696. public virtual bool IsUnplayed(User user)
  1697. {
  1698. if (user == null)
  1699. {
  1700. throw new ArgumentNullException("user");
  1701. }
  1702. var userdata = UserDataManager.GetUserData(user, this);
  1703. return userdata == null || !userdata.Played;
  1704. }
  1705. ItemLookupInfo IHasLookupInfo<ItemLookupInfo>.GetLookupInfo()
  1706. {
  1707. return GetItemLookupInfo<ItemLookupInfo>();
  1708. }
  1709. protected T GetItemLookupInfo<T>()
  1710. where T : ItemLookupInfo, new()
  1711. {
  1712. return new T
  1713. {
  1714. MetadataCountryCode = GetPreferredMetadataCountryCode(),
  1715. MetadataLanguage = GetPreferredMetadataLanguage(),
  1716. Name = Name,
  1717. ProviderIds = ProviderIds,
  1718. IndexNumber = IndexNumber,
  1719. ParentIndexNumber = ParentIndexNumber,
  1720. Year = ProductionYear,
  1721. PremiereDate = PremiereDate
  1722. };
  1723. }
  1724. /// <summary>
  1725. /// This is called before any metadata refresh and returns true or false indicating if changes were made
  1726. /// </summary>
  1727. public virtual bool BeforeMetadataRefresh()
  1728. {
  1729. _sortName = null;
  1730. var hasChanges = false;
  1731. if (string.IsNullOrEmpty(Name) && !string.IsNullOrEmpty(Path))
  1732. {
  1733. Name = FileSystem.GetFileNameWithoutExtension(Path);
  1734. hasChanges = true;
  1735. }
  1736. return hasChanges;
  1737. }
  1738. protected static string GetMappedPath(string path, LocationType locationType)
  1739. {
  1740. if (locationType == LocationType.FileSystem || locationType == LocationType.Offline)
  1741. {
  1742. foreach (var map in ConfigurationManager.Configuration.PathSubstitutions)
  1743. {
  1744. path = LibraryManager.SubstitutePath(path, map.From, map.To);
  1745. }
  1746. }
  1747. return path;
  1748. }
  1749. public virtual Task FillUserDataDtoValues(UserItemDataDto dto, UserItemData userData, BaseItemDto itemDto, User user)
  1750. {
  1751. if (RunTimeTicks.HasValue)
  1752. {
  1753. double pct = RunTimeTicks.Value;
  1754. if (pct > 0)
  1755. {
  1756. pct = userData.PlaybackPositionTicks / pct;
  1757. if (pct > 0)
  1758. {
  1759. dto.PlayedPercentage = 100 * pct;
  1760. }
  1761. }
  1762. }
  1763. return Task.FromResult(true);
  1764. }
  1765. protected Task RefreshMetadataForOwnedVideo(MetadataRefreshOptions options, string path, CancellationToken cancellationToken)
  1766. {
  1767. var newOptions = new MetadataRefreshOptions(options.DirectoryService)
  1768. {
  1769. ImageRefreshMode = options.ImageRefreshMode,
  1770. MetadataRefreshMode = options.MetadataRefreshMode,
  1771. ReplaceAllMetadata = options.ReplaceAllMetadata
  1772. };
  1773. var id = LibraryManager.GetNewItemId(path, typeof(Video));
  1774. // Try to retrieve it from the db. If we don't find it, use the resolved version
  1775. var video = LibraryManager.GetItemById(id) as Video;
  1776. if (video == null)
  1777. {
  1778. video = LibraryManager.ResolvePath(FileSystem.GetFileSystemInfo(path)) as Video;
  1779. newOptions.ForceSave = true;
  1780. }
  1781. if (video == null)
  1782. {
  1783. return Task.FromResult(true);
  1784. }
  1785. return video.RefreshMetadata(newOptions, cancellationToken);
  1786. }
  1787. public string GetEtag(User user)
  1788. {
  1789. return string.Join("|", GetEtagValues(user).ToArray()).GetMD5().ToString("N");
  1790. }
  1791. protected virtual List<string> GetEtagValues(User user)
  1792. {
  1793. return new List<string>
  1794. {
  1795. DateLastSaved.Ticks.ToString(CultureInfo.InvariantCulture)
  1796. };
  1797. }
  1798. public virtual IEnumerable<Guid> GetAncestorIds()
  1799. {
  1800. return GetParents().Select(i => i.Id).Concat(LibraryManager.GetCollectionFolders(this).Select(i => i.Id));
  1801. }
  1802. public BaseItem GetTopParent()
  1803. {
  1804. if (IsTopParent)
  1805. {
  1806. return this;
  1807. }
  1808. return GetParents().FirstOrDefault(i => i.IsTopParent);
  1809. }
  1810. [IgnoreDataMember]
  1811. public virtual bool IsTopParent
  1812. {
  1813. get
  1814. {
  1815. if (GetParent() is AggregateFolder || this is BasePluginFolder || this is Channel)
  1816. {
  1817. return true;
  1818. }
  1819. var view = this as UserView;
  1820. if (view != null && string.Equals(view.ViewType, CollectionType.LiveTv, StringComparison.OrdinalIgnoreCase))
  1821. {
  1822. return true;
  1823. }
  1824. if (view != null && string.Equals(view.ViewType, CollectionType.Channels, StringComparison.OrdinalIgnoreCase))
  1825. {
  1826. return true;
  1827. }
  1828. return false;
  1829. }
  1830. }
  1831. [IgnoreDataMember]
  1832. public virtual bool SupportsAncestors
  1833. {
  1834. get
  1835. {
  1836. return true;
  1837. }
  1838. }
  1839. public virtual IEnumerable<Guid> GetIdsForAncestorQuery()
  1840. {
  1841. return new[] { Id };
  1842. }
  1843. public virtual Task Delete(DeleteOptions options)
  1844. {
  1845. return LibraryManager.DeleteItem(this, options);
  1846. }
  1847. public virtual Task OnFileDeleted()
  1848. {
  1849. // Remove from database
  1850. return Delete(new DeleteOptions
  1851. {
  1852. DeleteFileLocation = false
  1853. });
  1854. }
  1855. public virtual List<ExternalUrl> GetRelatedUrls()
  1856. {
  1857. return new List<ExternalUrl>();
  1858. }
  1859. }
  1860. }