BoxSet.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 System.Threading;
  12. using System.Threading.Tasks;
  13. namespace MediaBrowser.Controller.Entities.Movies
  14. {
  15. /// <summary>
  16. /// Class BoxSet
  17. /// </summary>
  18. public class BoxSet : Folder, IHasTrailers, IHasKeywords, IHasDisplayOrder, IHasLookupInfo<BoxSetInfo>, IMetadataContainer, IHasShares
  19. {
  20. public List<Share> Shares { get; set; }
  21. public BoxSet()
  22. {
  23. RemoteTrailers = new List<MediaUrl>();
  24. LocalTrailerIds = new List<Guid>();
  25. RemoteTrailerIds = new List<Guid>();
  26. DisplayOrder = ItemSortBy.PremiereDate;
  27. Keywords = new List<string>();
  28. Shares = new List<Share>();
  29. }
  30. protected override bool FilterLinkedChildrenPerUser
  31. {
  32. get
  33. {
  34. return true;
  35. }
  36. }
  37. public List<Guid> LocalTrailerIds { get; set; }
  38. public List<Guid> RemoteTrailerIds { get; set; }
  39. /// <summary>
  40. /// Gets or sets the remote trailers.
  41. /// </summary>
  42. /// <value>The remote trailers.</value>
  43. public List<MediaUrl> RemoteTrailers { get; set; }
  44. /// <summary>
  45. /// Gets or sets the tags.
  46. /// </summary>
  47. /// <value>The tags.</value>
  48. public List<string> Keywords { get; set; }
  49. /// <summary>
  50. /// Gets or sets the display order.
  51. /// </summary>
  52. /// <value>The display order.</value>
  53. public string DisplayOrder { get; set; }
  54. protected override bool GetBlockUnratedValue(UserPolicy config)
  55. {
  56. return config.BlockUnratedItems.Contains(UnratedItem.Movie);
  57. }
  58. [IgnoreDataMember]
  59. public override bool IsPreSorted
  60. {
  61. get
  62. {
  63. return true;
  64. }
  65. }
  66. [IgnoreDataMember]
  67. protected override bool SupportsShortcutChildren
  68. {
  69. get
  70. {
  71. return true;
  72. }
  73. }
  74. public override bool IsAuthorizedToDelete(User user)
  75. {
  76. return true;
  77. }
  78. public override bool IsSaveLocalMetadataEnabled()
  79. {
  80. return true;
  81. }
  82. /// <summary>
  83. /// Gets the trailer ids.
  84. /// </summary>
  85. /// <returns>List&lt;Guid&gt;.</returns>
  86. public List<Guid> GetTrailerIds()
  87. {
  88. var list = LocalTrailerIds.ToList();
  89. list.AddRange(RemoteTrailerIds);
  90. return list;
  91. }
  92. /// <summary>
  93. /// Updates the official rating based on content and returns true or false indicating if it changed.
  94. /// </summary>
  95. /// <returns></returns>
  96. public bool UpdateRatingToContent()
  97. {
  98. var currentOfficialRating = OfficialRating;
  99. // Gather all possible ratings
  100. var ratings = GetRecursiveChildren()
  101. .Concat(GetLinkedChildren())
  102. .Where(i => i is Movie || i is Series)
  103. .Select(i => i.OfficialRating)
  104. .Where(i => !string.IsNullOrEmpty(i))
  105. .Distinct(StringComparer.OrdinalIgnoreCase)
  106. .Select(i => new Tuple<string, int?>(i, LocalizationManager.GetRatingLevel(i)))
  107. .OrderBy(i => i.Item2 ?? 1000)
  108. .Select(i => i.Item1);
  109. OfficialRating = ratings.FirstOrDefault() ?? currentOfficialRating;
  110. return !string.Equals(currentOfficialRating ?? string.Empty, OfficialRating ?? string.Empty,
  111. StringComparison.OrdinalIgnoreCase);
  112. }
  113. public override IEnumerable<BaseItem> GetChildren(User user, bool includeLinkedChildren)
  114. {
  115. var children = base.GetChildren(user, includeLinkedChildren);
  116. if (string.Equals(DisplayOrder, ItemSortBy.SortName, StringComparison.OrdinalIgnoreCase))
  117. {
  118. // Sort by name
  119. return LibraryManager.Sort(children, user, new[] { ItemSortBy.SortName }, SortOrder.Ascending);
  120. }
  121. if (string.Equals(DisplayOrder, ItemSortBy.PremiereDate, StringComparison.OrdinalIgnoreCase))
  122. {
  123. // Sort by release date
  124. return LibraryManager.Sort(children, user, new[] { ItemSortBy.ProductionYear, ItemSortBy.PremiereDate, ItemSortBy.SortName }, SortOrder.Ascending);
  125. }
  126. // Default sorting
  127. return LibraryManager.Sort(children, user, new[] { ItemSortBy.ProductionYear, ItemSortBy.PremiereDate, ItemSortBy.SortName }, SortOrder.Ascending);
  128. }
  129. public BoxSetInfo GetLookupInfo()
  130. {
  131. return GetItemLookupInfo<BoxSetInfo>();
  132. }
  133. public async Task RefreshAllMetadata(MetadataRefreshOptions refreshOptions, IProgress<double> progress, CancellationToken cancellationToken)
  134. {
  135. // Refresh bottom up, children first, then the boxset
  136. // By then hopefully the movies within will have Tmdb collection values
  137. var items = GetRecursiveChildren().ToList();
  138. var totalItems = items.Count;
  139. var numComplete = 0;
  140. // Refresh songs
  141. foreach (var item in items)
  142. {
  143. cancellationToken.ThrowIfCancellationRequested();
  144. await item.RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false);
  145. numComplete++;
  146. double percent = numComplete;
  147. percent /= totalItems;
  148. progress.Report(percent * 100);
  149. }
  150. // Refresh current item
  151. await RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false);
  152. progress.Report(100);
  153. }
  154. public override bool IsVisible(User user)
  155. {
  156. var userId = user.Id.ToString("N");
  157. // Need to check Count > 0 for boxsets created prior to the introduction of Shares
  158. if (Shares.Count > 0 && Shares.Any(i => string.Equals(userId, i.UserId, StringComparison.OrdinalIgnoreCase)))
  159. {
  160. return true;
  161. }
  162. if (base.IsVisible(user))
  163. {
  164. return GetChildren(user, true).Any();
  165. }
  166. return false;
  167. }
  168. }
  169. }