BoxSet.cs 6.6 KB

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