BaseItem.cs 5.6 KB

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