BoxSet.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using MediaBrowser.Controller.Entities.TV;
  2. using MediaBrowser.Controller.Providers;
  3. using MediaBrowser.Model.Configuration;
  4. using MediaBrowser.Model.Entities;
  5. using MediaBrowser.Model.Querying;
  6. using MediaBrowser.Model.Users;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using MediaBrowser.Model.Serialization;
  11. using MediaBrowser.Controller.Entities.Audio;
  12. namespace MediaBrowser.Controller.Entities.Movies
  13. {
  14. /// <summary>
  15. /// Class BoxSet
  16. /// </summary>
  17. public class BoxSet : Folder, IHasTrailers, IHasDisplayOrder, IHasLookupInfo<BoxSetInfo>, IHasShares
  18. {
  19. public List<Share> Shares { get; set; }
  20. public BoxSet()
  21. {
  22. RemoteTrailers = new List<MediaUrl>();
  23. LocalTrailerIds = new List<Guid>();
  24. RemoteTrailerIds = new List<Guid>();
  25. DisplayOrder = ItemSortBy.PremiereDate;
  26. Shares = new List<Share>();
  27. }
  28. [IgnoreDataMember]
  29. protected override bool FilterLinkedChildrenPerUser
  30. {
  31. get
  32. {
  33. return true;
  34. }
  35. }
  36. [IgnoreDataMember]
  37. public override bool SupportsInheritedParentImages
  38. {
  39. get
  40. {
  41. return false;
  42. }
  43. }
  44. public List<Guid> LocalTrailerIds { get; set; }
  45. public List<Guid> RemoteTrailerIds { get; set; }
  46. /// <summary>
  47. /// Gets or sets the remote trailers.
  48. /// </summary>
  49. /// <value>The remote trailers.</value>
  50. public List<MediaUrl> RemoteTrailers { get; set; }
  51. /// <summary>
  52. /// Gets or sets the display order.
  53. /// </summary>
  54. /// <value>The display order.</value>
  55. public string DisplayOrder { get; set; }
  56. protected override bool GetBlockUnratedValue(UserPolicy config)
  57. {
  58. return config.BlockUnratedItems.Contains(UnratedItem.Movie);
  59. }
  60. public override double? GetDefaultPrimaryImageAspectRatio()
  61. {
  62. double value = 2;
  63. value /= 3;
  64. return value;
  65. }
  66. public override UnratedItem GetBlockUnratedType()
  67. {
  68. return UnratedItem.Movie;
  69. }
  70. protected override IEnumerable<BaseItem> GetNonCachedChildren(IDirectoryService directoryService)
  71. {
  72. if (IsLegacyBoxSet)
  73. {
  74. return base.GetNonCachedChildren(directoryService);
  75. }
  76. return new List<BaseItem>();
  77. }
  78. protected override List<BaseItem> LoadChildren()
  79. {
  80. if (IsLegacyBoxSet)
  81. {
  82. return base.LoadChildren();
  83. }
  84. // Save a trip to the database
  85. return new List<BaseItem>();
  86. }
  87. [IgnoreDataMember]
  88. public override bool IsPreSorted
  89. {
  90. get
  91. {
  92. return true;
  93. }
  94. }
  95. [IgnoreDataMember]
  96. protected override bool SupportsShortcutChildren
  97. {
  98. get
  99. {
  100. if (IsLegacyBoxSet)
  101. {
  102. return true;
  103. }
  104. return false;
  105. }
  106. }
  107. [IgnoreDataMember]
  108. private bool IsLegacyBoxSet
  109. {
  110. get
  111. {
  112. if (string.IsNullOrWhiteSpace(Path))
  113. {
  114. return false;
  115. }
  116. return !FileSystem.ContainsSubPath(ConfigurationManager.ApplicationPaths.DataPath, Path);
  117. }
  118. }
  119. public override bool IsAuthorizedToDelete(User user)
  120. {
  121. return true;
  122. }
  123. public override bool IsSaveLocalMetadataEnabled()
  124. {
  125. return true;
  126. }
  127. /// <summary>
  128. /// Gets the trailer ids.
  129. /// </summary>
  130. /// <returns>List&lt;Guid&gt;.</returns>
  131. public List<Guid> GetTrailerIds()
  132. {
  133. var list = LocalTrailerIds.ToList();
  134. list.AddRange(RemoteTrailerIds);
  135. return list;
  136. }
  137. /// <summary>
  138. /// Updates the official rating based on content and returns true or false indicating if it changed.
  139. /// </summary>
  140. /// <returns></returns>
  141. public bool UpdateRatingToContent()
  142. {
  143. var currentOfficialRating = OfficialRating;
  144. // Gather all possible ratings
  145. var ratings = GetLinkedChildren()
  146. .Select(i => i.OfficialRating)
  147. .Where(i => !string.IsNullOrEmpty(i))
  148. .Distinct(StringComparer.OrdinalIgnoreCase)
  149. .Select(i => new Tuple<string, int?>(i, LocalizationManager.GetRatingLevel(i)))
  150. .OrderBy(i => i.Item2 ?? 1000)
  151. .Select(i => i.Item1);
  152. OfficialRating = ratings.FirstOrDefault() ?? currentOfficialRating;
  153. return !string.Equals(currentOfficialRating ?? string.Empty, OfficialRating ?? string.Empty,
  154. StringComparison.OrdinalIgnoreCase);
  155. }
  156. public override IEnumerable<BaseItem> GetChildren(User user, bool includeLinkedChildren)
  157. {
  158. var children = base.GetChildren(user, includeLinkedChildren);
  159. if (string.Equals(DisplayOrder, ItemSortBy.SortName, StringComparison.OrdinalIgnoreCase))
  160. {
  161. // Sort by name
  162. return LibraryManager.Sort(children, user, new[] { ItemSortBy.SortName }, SortOrder.Ascending);
  163. }
  164. if (string.Equals(DisplayOrder, ItemSortBy.PremiereDate, StringComparison.OrdinalIgnoreCase))
  165. {
  166. // Sort by release date
  167. return LibraryManager.Sort(children, user, new[] { ItemSortBy.ProductionYear, ItemSortBy.PremiereDate, ItemSortBy.SortName }, SortOrder.Ascending);
  168. }
  169. // Default sorting
  170. return LibraryManager.Sort(children, user, new[] { ItemSortBy.ProductionYear, ItemSortBy.PremiereDate, ItemSortBy.SortName }, SortOrder.Ascending);
  171. }
  172. public BoxSetInfo GetLookupInfo()
  173. {
  174. return GetItemLookupInfo<BoxSetInfo>();
  175. }
  176. public override bool IsVisible(User user)
  177. {
  178. var userId = user.Id.ToString("N");
  179. // Need to check Count > 0 for boxsets created prior to the introduction of Shares
  180. if (Shares.Count > 0 && Shares.Any(i => string.Equals(userId, i.UserId, StringComparison.OrdinalIgnoreCase)))
  181. {
  182. return true;
  183. }
  184. if (base.IsVisible(user))
  185. {
  186. return base.GetChildren(user, true).Any();
  187. }
  188. return false;
  189. }
  190. public override bool IsVisibleStandalone(User user)
  191. {
  192. return IsVisible(user);
  193. }
  194. }
  195. }