IndexFolder.cs 7.6 KB

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