BaseItem.cs 6.0 KB

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