BoxSet.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. public override bool IsAuthorizedToDelete(User user)
  67. {
  68. return true;
  69. }
  70. public override bool IsSaveLocalMetadataEnabled()
  71. {
  72. return true;
  73. }
  74. /// <summary>
  75. /// Gets the trailer ids.
  76. /// </summary>
  77. /// <returns>List&lt;Guid&gt;.</returns>
  78. public List<Guid> GetTrailerIds()
  79. {
  80. var list = LocalTrailerIds.ToList();
  81. list.AddRange(RemoteTrailerIds);
  82. return list;
  83. }
  84. /// <summary>
  85. /// Updates the official rating based on content and returns true or false indicating if it changed.
  86. /// </summary>
  87. /// <returns></returns>
  88. public bool UpdateRatingToContent()
  89. {
  90. var currentOfficialRating = OfficialRating;
  91. // Gather all possible ratings
  92. var ratings = GetRecursiveChildren()
  93. .Concat(GetLinkedChildren())
  94. .Where(i => i is Movie || i is Series)
  95. .Select(i => i.OfficialRating)
  96. .Where(i => !string.IsNullOrEmpty(i))
  97. .Distinct(StringComparer.OrdinalIgnoreCase)
  98. .Select(i => new Tuple<string, int?>(i, LocalizationManager.GetRatingLevel(i)))
  99. .OrderBy(i => i.Item2 ?? 1000)
  100. .Select(i => i.Item1);
  101. OfficialRating = ratings.FirstOrDefault() ?? currentOfficialRating;
  102. return !string.Equals(currentOfficialRating ?? string.Empty, OfficialRating ?? string.Empty,
  103. StringComparison.OrdinalIgnoreCase);
  104. }
  105. public override IEnumerable<BaseItem> GetChildren(User user, bool includeLinkedChildren)
  106. {
  107. var children = base.GetChildren(user, includeLinkedChildren);
  108. if (string.Equals(DisplayOrder, ItemSortBy.SortName, StringComparison.OrdinalIgnoreCase))
  109. {
  110. // Sort by name
  111. return LibraryManager.Sort(children, user, new[] { ItemSortBy.SortName }, SortOrder.Ascending);
  112. }
  113. if (string.Equals(DisplayOrder, ItemSortBy.PremiereDate, StringComparison.OrdinalIgnoreCase))
  114. {
  115. // Sort by release date
  116. return LibraryManager.Sort(children, user, new[] { ItemSortBy.ProductionYear, ItemSortBy.PremiereDate, ItemSortBy.SortName }, SortOrder.Ascending);
  117. }
  118. // Default sorting
  119. return LibraryManager.Sort(children, user, new[] { ItemSortBy.ProductionYear, ItemSortBy.PremiereDate, ItemSortBy.SortName }, SortOrder.Ascending);
  120. }
  121. public BoxSetInfo GetLookupInfo()
  122. {
  123. return GetItemLookupInfo<BoxSetInfo>();
  124. }
  125. public async Task RefreshAllMetadata(MetadataRefreshOptions refreshOptions, IProgress<double> progress, CancellationToken cancellationToken)
  126. {
  127. // Refresh bottom up, children first, then the boxset
  128. // By then hopefully the movies within will have Tmdb collection values
  129. var items = GetRecursiveChildren().ToList();
  130. var totalItems = items.Count;
  131. var numComplete = 0;
  132. // Refresh songs
  133. foreach (var item in items)
  134. {
  135. cancellationToken.ThrowIfCancellationRequested();
  136. await item.RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false);
  137. numComplete++;
  138. double percent = numComplete;
  139. percent /= totalItems;
  140. progress.Report(percent * 100);
  141. }
  142. // Refresh current item
  143. await RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false);
  144. progress.Report(100);
  145. }
  146. public override bool IsVisible(User user)
  147. {
  148. if (base.IsVisible(user))
  149. {
  150. var userId = user.Id.ToString("N");
  151. // Need to check Count > 0 for boxsets created prior to the introduction of Shares
  152. if (Shares.Count > 0 && !Shares.Any(i => string.Equals(userId, i.UserId, StringComparison.OrdinalIgnoreCase)))
  153. {
  154. //return false;
  155. }
  156. return true;
  157. }
  158. return false;
  159. }
  160. }
  161. }