BoxSet.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using MediaBrowser.Common.Progress;
  2. using MediaBrowser.Controller.Providers;
  3. using MediaBrowser.Model.Configuration;
  4. using MediaBrowser.Model.Entities;
  5. using MediaBrowser.Model.Querying;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Runtime.Serialization;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using MediaBrowser.Model.Users;
  13. namespace MediaBrowser.Controller.Entities.Movies
  14. {
  15. /// <summary>
  16. /// Class BoxSet
  17. /// </summary>
  18. public class BoxSet : Folder, IHasTrailers, IHasKeywords, IHasPreferredMetadataLanguage, 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. public string PreferredMetadataLanguage { get; set; }
  50. /// <summary>
  51. /// Gets or sets the preferred metadata country code.
  52. /// </summary>
  53. /// <value>The preferred metadata country code.</value>
  54. public string PreferredMetadataCountryCode { get; set; }
  55. /// <summary>
  56. /// Gets or sets the display order.
  57. /// </summary>
  58. /// <value>The display order.</value>
  59. public string DisplayOrder { get; set; }
  60. protected override bool GetBlockUnratedValue(UserPolicy config)
  61. {
  62. return config.BlockUnratedItems.Contains(UnratedItem.Movie);
  63. }
  64. [IgnoreDataMember]
  65. public override bool IsPreSorted
  66. {
  67. get
  68. {
  69. return true;
  70. }
  71. }
  72. /// <summary>
  73. /// Gets the trailer ids.
  74. /// </summary>
  75. /// <returns>List&lt;Guid&gt;.</returns>
  76. public List<Guid> GetTrailerIds()
  77. {
  78. var list = LocalTrailerIds.ToList();
  79. list.AddRange(RemoteTrailerIds);
  80. return list;
  81. }
  82. public override IEnumerable<BaseItem> GetChildren(User user, bool includeLinkedChildren)
  83. {
  84. var children = base.GetChildren(user, includeLinkedChildren);
  85. if (string.Equals(DisplayOrder, ItemSortBy.SortName, StringComparison.OrdinalIgnoreCase))
  86. {
  87. // Sort by name
  88. return LibraryManager.Sort(children, user, new[] { ItemSortBy.SortName }, SortOrder.Ascending);
  89. }
  90. if (string.Equals(DisplayOrder, ItemSortBy.PremiereDate, StringComparison.OrdinalIgnoreCase))
  91. {
  92. // Sort by release date
  93. return LibraryManager.Sort(children, user, new[] { ItemSortBy.ProductionYear, ItemSortBy.PremiereDate, ItemSortBy.SortName }, SortOrder.Ascending);
  94. }
  95. // Default sorting
  96. return LibraryManager.Sort(children, user, new[] { ItemSortBy.ProductionYear, ItemSortBy.PremiereDate, ItemSortBy.SortName }, SortOrder.Ascending);
  97. }
  98. public BoxSetInfo GetLookupInfo()
  99. {
  100. return GetItemLookupInfo<BoxSetInfo>();
  101. }
  102. public async Task RefreshAllMetadata(MetadataRefreshOptions refreshOptions, IProgress<double> progress, CancellationToken cancellationToken)
  103. {
  104. // Refresh bottom up, children first, then the boxset
  105. // By then hopefully the movies within will have Tmdb collection values
  106. var items = RecursiveChildren.ToList();
  107. var totalItems = items.Count;
  108. var percentages = new Dictionary<Guid, double>(totalItems);
  109. // Refresh songs
  110. foreach (var item in items)
  111. {
  112. cancellationToken.ThrowIfCancellationRequested();
  113. var innerProgress = new ActionableProgress<double>();
  114. // Avoid implicitly captured closure
  115. var currentChild = item;
  116. innerProgress.RegisterAction(p =>
  117. {
  118. lock (percentages)
  119. {
  120. percentages[currentChild.Id] = p / 100;
  121. var percent = percentages.Values.Sum();
  122. percent /= totalItems;
  123. percent *= 100;
  124. progress.Report(percent);
  125. }
  126. });
  127. // Avoid implicitly captured closure
  128. await RefreshItem(item, refreshOptions, innerProgress, cancellationToken).ConfigureAwait(false);
  129. }
  130. // Refresh current item
  131. await RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false);
  132. progress.Report(100);
  133. }
  134. private async Task RefreshItem(BaseItem item, MetadataRefreshOptions refreshOptions, IProgress<double> progress, CancellationToken cancellationToken)
  135. {
  136. await item.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. }