BoxSet.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 = EmptyMediaUrlArray;
  23. LocalTrailerIds = EmptyGuidArray;
  24. RemoteTrailerIds = EmptyGuidArray;
  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 Guid[] LocalTrailerIds { get; set; }
  45. public Guid[] RemoteTrailerIds { get; set; }
  46. /// <summary>
  47. /// Gets or sets the remote trailers.
  48. /// </summary>
  49. /// <value>The remote trailers.</value>
  50. public 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. /// Updates the official rating based on content and returns true or false indicating if it changed.
  129. /// </summary>
  130. /// <returns></returns>
  131. public bool UpdateRatingToContent()
  132. {
  133. var currentOfficialRating = OfficialRating;
  134. // Gather all possible ratings
  135. var ratings = GetLinkedChildren()
  136. .Select(i => i.OfficialRating)
  137. .Where(i => !string.IsNullOrEmpty(i))
  138. .Distinct(StringComparer.OrdinalIgnoreCase)
  139. .Select(i => new Tuple<string, int?>(i, LocalizationManager.GetRatingLevel(i)))
  140. .OrderBy(i => i.Item2 ?? 1000)
  141. .Select(i => i.Item1);
  142. OfficialRating = ratings.FirstOrDefault() ?? currentOfficialRating;
  143. return !string.Equals(currentOfficialRating ?? string.Empty, OfficialRating ?? string.Empty,
  144. StringComparison.OrdinalIgnoreCase);
  145. }
  146. public override IEnumerable<BaseItem> GetChildren(User user, bool includeLinkedChildren)
  147. {
  148. var children = base.GetChildren(user, includeLinkedChildren);
  149. if (string.Equals(DisplayOrder, ItemSortBy.SortName, StringComparison.OrdinalIgnoreCase))
  150. {
  151. // Sort by name
  152. return LibraryManager.Sort(children, user, new[] { ItemSortBy.SortName }, SortOrder.Ascending);
  153. }
  154. if (string.Equals(DisplayOrder, ItemSortBy.PremiereDate, StringComparison.OrdinalIgnoreCase))
  155. {
  156. // Sort by release date
  157. return LibraryManager.Sort(children, user, new[] { ItemSortBy.ProductionYear, ItemSortBy.PremiereDate, ItemSortBy.SortName }, SortOrder.Ascending);
  158. }
  159. // Default sorting
  160. return LibraryManager.Sort(children, user, new[] { ItemSortBy.ProductionYear, ItemSortBy.PremiereDate, ItemSortBy.SortName }, SortOrder.Ascending);
  161. }
  162. public BoxSetInfo GetLookupInfo()
  163. {
  164. return GetItemLookupInfo<BoxSetInfo>();
  165. }
  166. public override bool IsVisible(User user)
  167. {
  168. var userId = user.Id.ToString("N");
  169. // Need to check Count > 0 for boxsets created prior to the introduction of Shares
  170. if (Shares.Count > 0 && Shares.Any(i => string.Equals(userId, i.UserId, StringComparison.OrdinalIgnoreCase)))
  171. {
  172. return true;
  173. }
  174. if (base.IsVisible(user))
  175. {
  176. return base.GetChildren(user, true).Any();
  177. }
  178. return false;
  179. }
  180. public override bool IsVisibleStandalone(User user)
  181. {
  182. return IsVisible(user);
  183. }
  184. }
  185. }