CollectionFolder.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. return NullTaskResult;
  63. }
  64. private List<LinkedChild> _linkedChildren;
  65. /// <summary>
  66. /// Our children are actually just references to the ones in the physical root...
  67. /// </summary>
  68. /// <value>The linked children.</value>
  69. public override List<LinkedChild> LinkedChildren
  70. {
  71. get { return _linkedChildren ?? (_linkedChildren = GetLinkedChildrenInternal()); }
  72. set
  73. {
  74. base.LinkedChildren = value;
  75. }
  76. }
  77. private List<LinkedChild> GetLinkedChildrenInternal()
  78. {
  79. Dictionary<string, string> locationsDicionary;
  80. try
  81. {
  82. locationsDicionary = ResolveArgs.PhysicalLocations.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
  83. }
  84. catch (IOException ex)
  85. {
  86. Logger.ErrorException("Error getting ResolveArgs for {0}", ex, Path);
  87. return new List<LinkedChild>();
  88. }
  89. return LibraryManager.RootFolder.Children
  90. .OfType<Folder>()
  91. .Where(i => i.Path != null && locationsDicionary.ContainsKey(i.Path))
  92. .SelectMany(c => c.LinkedChildren).ToList();
  93. }
  94. private IEnumerable<BaseItem> _actualChildren;
  95. /// <summary>
  96. /// Our children are actually just references to the ones in the physical root...
  97. /// </summary>
  98. /// <value>The actual children.</value>
  99. protected override IEnumerable<BaseItem> ActualChildren
  100. {
  101. get { return _actualChildren ?? (_actualChildren = GetActualChildren()); }
  102. }
  103. private IEnumerable<BaseItem> GetActualChildren()
  104. {
  105. Dictionary<string, string> locationsDicionary;
  106. try
  107. {
  108. locationsDicionary = ResolveArgs.PhysicalLocations.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
  109. }
  110. catch (IOException ex)
  111. {
  112. Logger.ErrorException("Error getting ResolveArgs for {0}", ex, Path);
  113. return new BaseItem[] { };
  114. }
  115. return
  116. LibraryManager.RootFolder.Children
  117. .OfType<Folder>()
  118. .Where(i => i.Path != null && locationsDicionary.ContainsKey(i.Path))
  119. .SelectMany(c => c.Children);
  120. }
  121. public void ResetDynamicChildren()
  122. {
  123. _actualChildren = null;
  124. _linkedChildren = null;
  125. }
  126. }
  127. }