BoxSet.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.Threading;
  10. using System.Threading.Tasks;
  11. namespace MediaBrowser.Controller.Entities.Movies
  12. {
  13. /// <summary>
  14. /// Class BoxSet
  15. /// </summary>
  16. public class BoxSet : Folder, IHasTrailers, IHasTags, IHasKeywords, IHasPreferredMetadataLanguage, IHasDisplayOrder, IHasLookupInfo<BoxSetInfo>, IMetadataContainer
  17. {
  18. public BoxSet()
  19. {
  20. RemoteTrailers = new List<MediaUrl>();
  21. LocalTrailerIds = new List<Guid>();
  22. Tags = new List<string>();
  23. DisplayOrder = ItemSortBy.PremiereDate;
  24. Keywords = new List<string>();
  25. }
  26. public List<Guid> LocalTrailerIds { get; set; }
  27. /// <summary>
  28. /// Gets or sets the remote trailers.
  29. /// </summary>
  30. /// <value>The remote trailers.</value>
  31. public List<MediaUrl> RemoteTrailers { get; set; }
  32. /// <summary>
  33. /// Gets or sets the tags.
  34. /// </summary>
  35. /// <value>The tags.</value>
  36. public List<string> Tags { get; set; }
  37. public List<string> Keywords { get; set; }
  38. public string PreferredMetadataLanguage { get; set; }
  39. /// <summary>
  40. /// Gets or sets the preferred metadata country code.
  41. /// </summary>
  42. /// <value>The preferred metadata country code.</value>
  43. public string PreferredMetadataCountryCode { get; set; }
  44. /// <summary>
  45. /// Gets or sets the display order.
  46. /// </summary>
  47. /// <value>The display order.</value>
  48. public string DisplayOrder { get; set; }
  49. protected override bool GetBlockUnratedValue(UserConfiguration config)
  50. {
  51. return config.BlockUnratedItems.Contains(UnratedItem.Movie);
  52. }
  53. public override IEnumerable<BaseItem> GetChildren(User user, bool includeLinkedChildren)
  54. {
  55. var children = base.GetChildren(user, includeLinkedChildren);
  56. if (string.Equals(DisplayOrder, ItemSortBy.SortName, StringComparison.OrdinalIgnoreCase))
  57. {
  58. // Sort by name
  59. return LibraryManager.Sort(children, user, new[] { ItemSortBy.SortName }, SortOrder.Ascending);
  60. }
  61. if (string.Equals(DisplayOrder, ItemSortBy.PremiereDate, StringComparison.OrdinalIgnoreCase))
  62. {
  63. // Sort by release date
  64. return LibraryManager.Sort(children, user, new[] { ItemSortBy.ProductionYear, ItemSortBy.PremiereDate, ItemSortBy.SortName }, SortOrder.Ascending);
  65. }
  66. // Default sorting
  67. return LibraryManager.Sort(children, user, new[] { ItemSortBy.ProductionYear, ItemSortBy.PremiereDate, ItemSortBy.SortName }, SortOrder.Ascending);
  68. }
  69. public BoxSetInfo GetLookupInfo()
  70. {
  71. return GetItemLookupInfo<BoxSetInfo>();
  72. }
  73. public async Task RefreshAllMetadata(MetadataRefreshOptions refreshOptions, IProgress<double> progress, CancellationToken cancellationToken)
  74. {
  75. // Refresh bottom up, children first, then the boxset
  76. // By then hopefully the movies within will have Tmdb collection values
  77. var items = RecursiveChildren.ToList();
  78. var totalItems = items.Count;
  79. var percentages = new Dictionary<Guid, double>(totalItems);
  80. var tasks = new List<Task>();
  81. // Refresh songs
  82. foreach (var item in items)
  83. {
  84. if (tasks.Count >= 3)
  85. {
  86. await Task.WhenAll(tasks).ConfigureAwait(false);
  87. tasks.Clear();
  88. }
  89. cancellationToken.ThrowIfCancellationRequested();
  90. var innerProgress = new ActionableProgress<double>();
  91. // Avoid implicitly captured closure
  92. var currentChild = item;
  93. innerProgress.RegisterAction(p =>
  94. {
  95. lock (percentages)
  96. {
  97. percentages[currentChild.Id] = p / 100;
  98. var percent = percentages.Values.Sum();
  99. percent /= totalItems;
  100. percent *= 100;
  101. progress.Report(percent);
  102. }
  103. });
  104. // Avoid implicitly captured closure
  105. var taskChild = item;
  106. tasks.Add(Task.Run(async () => await RefreshItem(taskChild, refreshOptions, innerProgress, cancellationToken).ConfigureAwait(false), cancellationToken));
  107. }
  108. await Task.WhenAll(tasks).ConfigureAwait(false);
  109. tasks.Clear();
  110. // Refresh current item
  111. await RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false);
  112. progress.Report(100);
  113. }
  114. private async Task RefreshItem(BaseItem item, MetadataRefreshOptions refreshOptions, IProgress<double> progress, CancellationToken cancellationToken)
  115. {
  116. await item.RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false);
  117. progress.Report(100);
  118. }
  119. }
  120. }