BoxSet.cs 6.2 KB

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