IndexFolder.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using MediaBrowser.Common.Extensions;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Runtime.Serialization;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace MediaBrowser.Controller.Entities
  9. {
  10. /// <summary>
  11. /// Class IndexFolder
  12. /// </summary>
  13. public class IndexFolder : Folder
  14. {
  15. /// <summary>
  16. /// Initializes a new instance of the <see cref="IndexFolder" /> class.
  17. /// </summary>
  18. /// <param name="parent">The parent.</param>
  19. /// <param name="shadow">The shadow.</param>
  20. /// <param name="children">The children.</param>
  21. /// <param name="indexName">Name of the index.</param>
  22. /// <param name="groupContents">if set to <c>true</c> [group contents].</param>
  23. public IndexFolder(Folder parent, BaseItem shadow, IEnumerable<BaseItem> children, string indexName, bool groupContents = true)
  24. {
  25. ChildSource = children;
  26. ShadowItem = shadow;
  27. GroupContents = groupContents;
  28. if (shadow == null)
  29. {
  30. Name = ForcedSortName = "<Unknown>";
  31. }
  32. else
  33. {
  34. SetShadowValues();
  35. }
  36. Id = (parent.Id.ToString() + Name).GetMBId(typeof(IndexFolder));
  37. IndexName = indexName;
  38. Parent = parent;
  39. }
  40. /// <summary>
  41. /// Resets the parent.
  42. /// </summary>
  43. /// <param name="parent">The parent.</param>
  44. public void ResetParent(Folder parent)
  45. {
  46. Parent = parent;
  47. Id = (parent.Id.ToString() + Name).GetMBId(typeof(IndexFolder));
  48. }
  49. /// <summary>
  50. /// Override this to true if class should be grouped under a container in indicies
  51. /// The container class should be defined via IndexContainer
  52. /// </summary>
  53. /// <value><c>true</c> if [group in index]; otherwise, <c>false</c>.</value>
  54. [IgnoreDataMember]
  55. public override bool GroupInIndex
  56. {
  57. get
  58. {
  59. return ShadowItem != null && ShadowItem.GroupInIndex;
  60. }
  61. }
  62. /// <summary>
  63. /// Override this to return the folder that should be used to construct a container
  64. /// for this item in an index. GroupInIndex should be true as well.
  65. /// </summary>
  66. /// <value>The index container.</value>
  67. [IgnoreDataMember]
  68. public override Folder IndexContainer
  69. {
  70. get { return ShadowItem != null ? ShadowItem.IndexContainer : new IndexFolder(this, null, null, "<Unknown>", false); }
  71. }
  72. /// <summary>
  73. /// Gets or sets a value indicating whether [group contents].
  74. /// </summary>
  75. /// <value><c>true</c> if [group contents]; otherwise, <c>false</c>.</value>
  76. protected bool GroupContents { get; set; }
  77. /// <summary>
  78. /// Gets or sets the child source.
  79. /// </summary>
  80. /// <value>The child source.</value>
  81. protected IEnumerable<BaseItem> ChildSource { get; set; }
  82. /// <summary>
  83. /// Gets or sets our children.
  84. /// </summary>
  85. /// <value>Our children.</value>
  86. protected ConcurrentBag<BaseItem> OurChildren { get; set; }
  87. /// <summary>
  88. /// Gets the name of the index.
  89. /// </summary>
  90. /// <value>The name of the index.</value>
  91. public string IndexName { get; private set; }
  92. /// <summary>
  93. /// Override to return the children defined to us when we were created
  94. /// </summary>
  95. /// <value>The actual children.</value>
  96. protected override ConcurrentBag<BaseItem> LoadChildren()
  97. {
  98. var originalChildSource = ChildSource.ToList();
  99. var kids = originalChildSource;
  100. if (GroupContents)
  101. {
  102. // Recursively group up the chain
  103. var group = true;
  104. var isSubsequentLoop = false;
  105. while (group)
  106. {
  107. kids = isSubsequentLoop || kids.Any(i => i.GroupInIndex)
  108. ? GroupedSource(kids).ToList()
  109. : originalChildSource;
  110. group = kids.Any(i => i.GroupInIndex);
  111. isSubsequentLoop = true;
  112. }
  113. }
  114. // Now - since we built the index grouping from the bottom up - we now need to properly set Parents from the top down
  115. SetParents(this, kids.OfType<IndexFolder>());
  116. return new ConcurrentBag<BaseItem>(kids);
  117. }
  118. /// <summary>
  119. /// Sets the parents.
  120. /// </summary>
  121. /// <param name="parent">The parent.</param>
  122. /// <param name="kids">The kids.</param>
  123. private void SetParents(Folder parent, IEnumerable<IndexFolder> kids)
  124. {
  125. foreach (var child in kids)
  126. {
  127. child.ResetParent(parent);
  128. child.SetParents(child, child.Children.OfType<IndexFolder>());
  129. }
  130. }
  131. /// <summary>
  132. /// Groupeds the source.
  133. /// </summary>
  134. /// <param name="source">The source.</param>
  135. /// <returns>IEnumerable{BaseItem}.</returns>
  136. protected IEnumerable<BaseItem> GroupedSource(IEnumerable<BaseItem> source)
  137. {
  138. return source.GroupBy(i => i.IndexContainer).Select(container => new IndexFolder(this, container.Key, container, null, false));
  139. }
  140. /// <summary>
  141. /// The item we are shadowing as a folder (Genre, Actor, etc.)
  142. /// We inherit the images and other meta from this item
  143. /// </summary>
  144. /// <value>The shadow item.</value>
  145. protected BaseItem ShadowItem { get; set; }
  146. /// <summary>
  147. /// Sets the shadow values.
  148. /// </summary>
  149. protected void SetShadowValues()
  150. {
  151. if (ShadowItem != null)
  152. {
  153. Name = ShadowItem.Name;
  154. ForcedSortName = ShadowItem.SortName;
  155. Genres = ShadowItem.Genres;
  156. Studios = ShadowItem.Studios;
  157. OfficialRating = ShadowItem.OfficialRating;
  158. BackdropImagePaths = ShadowItem.BackdropImagePaths;
  159. Images = ShadowItem.Images;
  160. Overview = ShadowItem.Overview;
  161. DisplayMediaType = ShadowItem.GetType().Name;
  162. }
  163. }
  164. /// <summary>
  165. /// Overrides the base implementation to refresh metadata for local trailers
  166. /// </summary>
  167. /// <param name="cancellationToken">The cancellation token.</param>
  168. /// <param name="forceSave">if set to <c>true</c> [is new item].</param>
  169. /// <param name="forceRefresh">if set to <c>true</c> [force].</param>
  170. /// <param name="allowSlowProviders">if set to <c>true</c> [allow slow providers].</param>
  171. /// <param name="resetResolveArgs">if set to <c>true</c> [reset resolve args].</param>
  172. /// <returns>Task{System.Boolean}.</returns>
  173. public override async Task<bool> RefreshMetadata(CancellationToken cancellationToken, bool forceSave = false, bool forceRefresh = false, bool allowSlowProviders = true, bool resetResolveArgs = true)
  174. {
  175. if (ShadowItem != null)
  176. {
  177. var changed = await ShadowItem.RefreshMetadata(cancellationToken, forceSave, forceRefresh, allowSlowProviders, resetResolveArgs).ConfigureAwait(false);
  178. cancellationToken.ThrowIfCancellationRequested();
  179. SetShadowValues();
  180. return changed;
  181. }
  182. return false;
  183. }
  184. }
  185. }