IndexFolder.cs 7.5 KB

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