IndexFolder.cs 7.8 KB

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