BoxSet.cs 6.3 KB

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