BoxSet.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. namespace MediaBrowser.Controller.Entities.Movies
  13. {
  14. /// <summary>
  15. /// Class BoxSet
  16. /// </summary>
  17. public class BoxSet : Folder, IHasTrailers, IHasKeywords, IHasPreferredMetadataLanguage, IHasDisplayOrder, IHasLookupInfo<BoxSetInfo>, IMetadataContainer, IHasShares
  18. {
  19. public List<Share> Shares { get; set; }
  20. public BoxSet()
  21. {
  22. RemoteTrailers = new List<MediaUrl>();
  23. LocalTrailerIds = new List<Guid>();
  24. RemoteTrailerIds = new List<Guid>();
  25. DisplayOrder = ItemSortBy.PremiereDate;
  26. Keywords = new List<string>();
  27. Shares = new List<Share>();
  28. }
  29. protected override bool FilterLinkedChildrenPerUser
  30. {
  31. get
  32. {
  33. return true;
  34. }
  35. }
  36. public List<Guid> LocalTrailerIds { get; set; }
  37. public List<Guid> RemoteTrailerIds { get; set; }
  38. /// <summary>
  39. /// Gets or sets the remote trailers.
  40. /// </summary>
  41. /// <value>The remote trailers.</value>
  42. public List<MediaUrl> RemoteTrailers { get; set; }
  43. /// <summary>
  44. /// Gets or sets the tags.
  45. /// </summary>
  46. /// <value>The tags.</value>
  47. public List<string> Keywords { get; set; }
  48. public string PreferredMetadataLanguage { get; set; }
  49. /// <summary>
  50. /// Gets or sets the preferred metadata country code.
  51. /// </summary>
  52. /// <value>The preferred metadata country code.</value>
  53. public string PreferredMetadataCountryCode { get; set; }
  54. /// <summary>
  55. /// Gets or sets the display order.
  56. /// </summary>
  57. /// <value>The display order.</value>
  58. public string DisplayOrder { get; set; }
  59. protected override bool GetBlockUnratedValue(UserConfiguration config)
  60. {
  61. return config.BlockUnratedItems.Contains(UnratedItem.Movie);
  62. }
  63. [IgnoreDataMember]
  64. public override bool IsPreSorted
  65. {
  66. get
  67. {
  68. return true;
  69. }
  70. }
  71. /// <summary>
  72. /// Gets the trailer ids.
  73. /// </summary>
  74. /// <returns>List&lt;Guid&gt;.</returns>
  75. public List<Guid> GetTrailerIds()
  76. {
  77. var list = LocalTrailerIds.ToList();
  78. list.AddRange(RemoteTrailerIds);
  79. return list;
  80. }
  81. public override IEnumerable<BaseItem> GetChildren(User user, bool includeLinkedChildren)
  82. {
  83. var children = base.GetChildren(user, includeLinkedChildren);
  84. if (string.Equals(DisplayOrder, ItemSortBy.SortName, StringComparison.OrdinalIgnoreCase))
  85. {
  86. // Sort by name
  87. return LibraryManager.Sort(children, user, new[] { ItemSortBy.SortName }, SortOrder.Ascending);
  88. }
  89. if (string.Equals(DisplayOrder, ItemSortBy.PremiereDate, StringComparison.OrdinalIgnoreCase))
  90. {
  91. // Sort by release date
  92. return LibraryManager.Sort(children, user, new[] { ItemSortBy.ProductionYear, ItemSortBy.PremiereDate, ItemSortBy.SortName }, SortOrder.Ascending);
  93. }
  94. // Default sorting
  95. return LibraryManager.Sort(children, user, new[] { ItemSortBy.ProductionYear, ItemSortBy.PremiereDate, ItemSortBy.SortName }, SortOrder.Ascending);
  96. }
  97. public BoxSetInfo GetLookupInfo()
  98. {
  99. return GetItemLookupInfo<BoxSetInfo>();
  100. }
  101. public async Task RefreshAllMetadata(MetadataRefreshOptions refreshOptions, IProgress<double> progress, CancellationToken cancellationToken)
  102. {
  103. // Refresh bottom up, children first, then the boxset
  104. // By then hopefully the movies within will have Tmdb collection values
  105. var items = RecursiveChildren.ToList();
  106. var totalItems = items.Count;
  107. var percentages = new Dictionary<Guid, double>(totalItems);
  108. // Refresh songs
  109. foreach (var item in items)
  110. {
  111. cancellationToken.ThrowIfCancellationRequested();
  112. var innerProgress = new ActionableProgress<double>();
  113. // Avoid implicitly captured closure
  114. var currentChild = item;
  115. innerProgress.RegisterAction(p =>
  116. {
  117. lock (percentages)
  118. {
  119. percentages[currentChild.Id] = p / 100;
  120. var percent = percentages.Values.Sum();
  121. percent /= totalItems;
  122. percent *= 100;
  123. progress.Report(percent);
  124. }
  125. });
  126. // Avoid implicitly captured closure
  127. await RefreshItem(item, refreshOptions, innerProgress, cancellationToken).ConfigureAwait(false);
  128. }
  129. // Refresh current item
  130. await RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false);
  131. progress.Report(100);
  132. }
  133. private async Task RefreshItem(BaseItem item, MetadataRefreshOptions refreshOptions, IProgress<double> progress, CancellationToken cancellationToken)
  134. {
  135. await item.RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false);
  136. progress.Report(100);
  137. }
  138. public override bool IsVisible(User user)
  139. {
  140. if (base.IsVisible(user))
  141. {
  142. var userId = user.Id.ToString("N");
  143. return Shares.Any(i => string.Equals(userId, i.UserId, StringComparison.OrdinalIgnoreCase)) ||
  144. // Need to support this for boxsets created prior to the creation of Shares
  145. Shares.Count == 0;
  146. }
  147. return false;
  148. }
  149. }
  150. }