BaseItem.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using MediaBrowser.Model.Entities;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Controller.IO;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. namespace MediaBrowser.Controller.Entities
  8. {
  9. public abstract class BaseItem : BaseEntity, IHasProviderIds
  10. {
  11. protected ItemResolveEventArgs _resolveArgs;
  12. /// <summary>
  13. /// We attach these to the item so that we only ever have to hit the file system once
  14. /// (this includes the children of the containing folder)
  15. /// Use ResolveArgs.FileSystemChildren to check for the existence of files instead of File.Exists
  16. /// </summary>
  17. public ItemResolveEventArgs ResolveArgs
  18. {
  19. get
  20. {
  21. if (_resolveArgs == null)
  22. {
  23. _resolveArgs = new ItemResolveEventArgs()
  24. {
  25. FileInfo = FileData.GetFileData(this.Path),
  26. Parent = this.Parent,
  27. Cancel = false,
  28. Path = this.Path
  29. };
  30. _resolveArgs = FileSystemHelper.FilterChildFileSystemEntries(_resolveArgs, (this.Parent != null && this.Parent.IsRoot));
  31. }
  32. return _resolveArgs;
  33. }
  34. set
  35. {
  36. _resolveArgs = value;
  37. }
  38. }
  39. public string SortName { get; set; }
  40. /// <summary>
  41. /// When the item first debuted. For movies this could be premiere date, episodes would be first aired
  42. /// </summary>
  43. public DateTime? PremiereDate { get; set; }
  44. public string Path { get; set; }
  45. public Folder Parent { get; set; }
  46. public string LogoImagePath { get; set; }
  47. public string ArtImagePath { get; set; }
  48. public string ThumbnailImagePath { get; set; }
  49. public string BannerImagePath { get; set; }
  50. public IEnumerable<string> BackdropImagePaths { get; set; }
  51. public string OfficialRating { get; set; }
  52. public string CustomRating { get; set; }
  53. public string CustomPin { get; set; }
  54. public string Language { get; set; }
  55. public string Overview { get; set; }
  56. public List<string> Taglines { get; set; }
  57. /// <summary>
  58. /// Using a Dictionary to prevent duplicates
  59. /// </summary>
  60. public Dictionary<string,PersonInfo> People { get; set; }
  61. public List<string> Studios { get; set; }
  62. public List<string> Genres { get; set; }
  63. public string DisplayMediaType { get; set; }
  64. public float? UserRating { get; set; }
  65. public long? RunTimeTicks { get; set; }
  66. public string AspectRatio { get; set; }
  67. public int? ProductionYear { get; set; }
  68. /// <summary>
  69. /// If the item is part of a series, this is it's number in the series.
  70. /// This could be episode number, album track number, etc.
  71. /// </summary>
  72. public int? IndexNumber { get; set; }
  73. /// <summary>
  74. /// For an episode this could be the season number, or for a song this could be the disc number.
  75. /// </summary>
  76. public int? ParentIndexNumber { get; set; }
  77. public IEnumerable<Video> LocalTrailers { get; set; }
  78. public string TrailerUrl { get; set; }
  79. public Dictionary<string, string> ProviderIds { get; set; }
  80. public Dictionary<Guid, UserItemData> UserData { get; set; }
  81. public UserItemData GetUserData(User user, bool createIfNull)
  82. {
  83. if (UserData == null || !UserData.ContainsKey(user.Id))
  84. {
  85. if (createIfNull)
  86. {
  87. AddUserData(user, new UserItemData());
  88. }
  89. else
  90. {
  91. return null;
  92. }
  93. }
  94. return UserData[user.Id];
  95. }
  96. private void AddUserData(User user, UserItemData data)
  97. {
  98. if (UserData == null)
  99. {
  100. UserData = new Dictionary<Guid, UserItemData>();
  101. }
  102. UserData[user.Id] = data;
  103. }
  104. /// <summary>
  105. /// Determines if a given user has access to this item
  106. /// </summary>
  107. internal bool IsParentalAllowed(User user)
  108. {
  109. return true;
  110. }
  111. /// <summary>
  112. /// Finds an item by ID, recursively
  113. /// </summary>
  114. public virtual BaseItem FindItemById(Guid id)
  115. {
  116. if (Id == id)
  117. {
  118. return this;
  119. }
  120. if (LocalTrailers != null)
  121. {
  122. return LocalTrailers.FirstOrDefault(i => i.Id == id);
  123. }
  124. return null;
  125. }
  126. public virtual bool IsFolder
  127. {
  128. get
  129. {
  130. return false;
  131. }
  132. }
  133. /// <summary>
  134. /// Determine if we have changed vs the passed in copy
  135. /// </summary>
  136. /// <param name="original"></param>
  137. /// <returns></returns>
  138. public virtual bool IsChanged(BaseItem original)
  139. {
  140. bool changed = original.DateModified != this.DateModified;
  141. changed |= original.DateCreated != this.DateCreated;
  142. return changed;
  143. }
  144. /// <summary>
  145. /// Refresh metadata on us by execution our provider chain
  146. /// </summary>
  147. /// <returns>true if a provider reports we changed</returns>
  148. public bool RefreshMetadata()
  149. {
  150. return false;
  151. }
  152. /// <summary>
  153. /// Determines if the item is considered new based on user settings
  154. /// </summary>
  155. public bool IsRecentlyAdded(User user)
  156. {
  157. return (DateTime.UtcNow - DateCreated).TotalDays < user.RecentItemDays;
  158. }
  159. public void AddPerson(PersonInfo person)
  160. {
  161. if (People == null)
  162. {
  163. People = new Dictionary<string, PersonInfo>(StringComparer.OrdinalIgnoreCase);
  164. }
  165. People[person.Name] = person;
  166. }
  167. /// <summary>
  168. /// Marks the item as either played or unplayed
  169. /// </summary>
  170. public virtual void SetPlayedStatus(User user, bool wasPlayed)
  171. {
  172. UserItemData data = GetUserData(user, true);
  173. if (wasPlayed)
  174. {
  175. data.PlayCount = Math.Max(data.PlayCount, 1);
  176. }
  177. else
  178. {
  179. data.PlayCount = 0;
  180. data.PlaybackPositionTicks = 0;
  181. }
  182. }
  183. }
  184. }