BaseItem.cs 4.9 KB

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