CollectionFolder.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  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. /// Specialized Folder class that points to a subset of the physical folders in the system.
  12. /// It is created from the user-specific folders within the system root
  13. /// </summary>
  14. public class CollectionFolder : Folder, ICollectionFolder
  15. {
  16. /// <summary>
  17. /// Gets a value indicating whether this instance is virtual folder.
  18. /// </summary>
  19. /// <value><c>true</c> if this instance is virtual folder; otherwise, <c>false</c>.</value>
  20. [IgnoreDataMember]
  21. public override bool IsVirtualFolder
  22. {
  23. get
  24. {
  25. return true;
  26. }
  27. }
  28. public string CollectionType { get; set; }
  29. /// <summary>
  30. /// Allow different display preferences for each collection folder
  31. /// </summary>
  32. /// <value>The display prefs id.</value>
  33. public override Guid DisplayPreferencesId
  34. {
  35. get
  36. {
  37. return Id;
  38. }
  39. }
  40. // Cache this since it will be used a lot
  41. /// <summary>
  42. /// The null task result
  43. /// </summary>
  44. private static readonly Task NullTaskResult = Task.FromResult<object>(null);
  45. /// <summary>
  46. /// Compare our current children (presumably just read from the repo) with the current state of the file system and adjust for any changes
  47. /// ***Currently does not contain logic to maintain items that are unavailable in the file system***
  48. /// </summary>
  49. /// <param name="progress">The progress.</param>
  50. /// <param name="cancellationToken">The cancellation token.</param>
  51. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  52. /// <param name="forceRefreshMetadata">if set to <c>true</c> [force refresh metadata].</param>
  53. /// <returns>Task.</returns>
  54. protected override Task ValidateChildrenInternal(IProgress<double> progress, CancellationToken cancellationToken, bool? recursive = null, bool forceRefreshMetadata = false)
  55. {
  56. //we don't directly validate our children
  57. //but we do need to clear out the index cache...
  58. if (IndexCache != null)
  59. {
  60. IndexCache.Clear();
  61. }
  62. ResetDynamicChildren();
  63. return NullTaskResult;
  64. }
  65. private List<LinkedChild> _linkedChildren;
  66. /// <summary>
  67. /// Our children are actually just references to the ones in the physical root...
  68. /// </summary>
  69. /// <value>The linked children.</value>
  70. public override List<LinkedChild> LinkedChildren
  71. {
  72. get { return _linkedChildren ?? (_linkedChildren = GetLinkedChildrenInternal()); }
  73. set
  74. {
  75. base.LinkedChildren = value;
  76. }
  77. }
  78. private List<LinkedChild> GetLinkedChildrenInternal()
  79. {
  80. Dictionary<string, string> locationsDicionary;
  81. try
  82. {
  83. locationsDicionary = ResolveArgs.PhysicalLocations.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
  84. }
  85. catch (IOException ex)
  86. {
  87. Logger.ErrorException("Error getting ResolveArgs for {0}", ex, Path);
  88. return new List<LinkedChild>();
  89. }
  90. return LibraryManager.RootFolder.Children
  91. .OfType<Folder>()
  92. .Where(i => i.Path != null && locationsDicionary.ContainsKey(i.Path))
  93. .SelectMany(c => c.LinkedChildren)
  94. .ToList();
  95. }
  96. private IEnumerable<BaseItem> _actualChildren;
  97. /// <summary>
  98. /// Our children are actually just references to the ones in the physical root...
  99. /// </summary>
  100. /// <value>The actual children.</value>
  101. protected override IEnumerable<BaseItem> ActualChildren
  102. {
  103. get { return _actualChildren ?? (_actualChildren = GetActualChildren()); }
  104. }
  105. private IEnumerable<BaseItem> GetActualChildren()
  106. {
  107. Dictionary<string, string> locationsDicionary;
  108. try
  109. {
  110. locationsDicionary = ResolveArgs.PhysicalLocations.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
  111. }
  112. catch (IOException ex)
  113. {
  114. Logger.ErrorException("Error getting ResolveArgs for {0}", ex, Path);
  115. return new BaseItem[] { };
  116. }
  117. return
  118. LibraryManager.RootFolder.Children
  119. .OfType<Folder>()
  120. .Where(i => i.Path != null && locationsDicionary.ContainsKey(i.Path))
  121. .SelectMany(c => c.Children)
  122. .ToList();
  123. }
  124. public void ResetDynamicChildren()
  125. {
  126. _actualChildren = null;
  127. _linkedChildren = null;
  128. }
  129. }
  130. }