BoxSet.cs 6.2 KB

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