BaseItem.cs 6.0 KB

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