CollectionFolder.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. [IgnoreDataMember]
  34. public override Guid DisplayPreferencesId
  35. {
  36. get
  37. {
  38. return Id;
  39. }
  40. }
  41. // Cache this since it will be used a lot
  42. /// <summary>
  43. /// The null task result
  44. /// </summary>
  45. private static readonly Task NullTaskResult = Task.FromResult<object>(null);
  46. /// <summary>
  47. /// Compare our current children (presumably just read from the repo) with the current state of the file system and adjust for any changes
  48. /// ***Currently does not contain logic to maintain items that are unavailable in the file system***
  49. /// </summary>
  50. /// <param name="progress">The progress.</param>
  51. /// <param name="cancellationToken">The cancellation token.</param>
  52. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  53. /// <param name="forceRefreshMetadata">if set to <c>true</c> [force refresh metadata].</param>
  54. /// <returns>Task.</returns>
  55. protected override Task ValidateChildrenInternal(IProgress<double> progress, CancellationToken cancellationToken, bool? recursive = null, bool forceRefreshMetadata = false)
  56. {
  57. ResetDynamicChildren();
  58. return NullTaskResult;
  59. }
  60. private List<LinkedChild> _linkedChildren;
  61. /// <summary>
  62. /// Our children are actually just references to the ones in the physical root...
  63. /// </summary>
  64. /// <value>The linked children.</value>
  65. public override List<LinkedChild> LinkedChildren
  66. {
  67. get { return _linkedChildren ?? (_linkedChildren = GetLinkedChildrenInternal()); }
  68. set
  69. {
  70. base.LinkedChildren = value;
  71. }
  72. }
  73. private List<LinkedChild> GetLinkedChildrenInternal()
  74. {
  75. Dictionary<string, string> locationsDicionary;
  76. try
  77. {
  78. locationsDicionary = ResolveArgs.PhysicalLocations.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
  79. }
  80. catch (IOException ex)
  81. {
  82. Logger.ErrorException("Error getting ResolveArgs for {0}", ex, Path);
  83. return new List<LinkedChild>();
  84. }
  85. return LibraryManager.RootFolder.Children
  86. .OfType<Folder>()
  87. .Where(i => i.Path != null && locationsDicionary.ContainsKey(i.Path))
  88. .SelectMany(c => c.LinkedChildren)
  89. .ToList();
  90. }
  91. private IEnumerable<BaseItem> _actualChildren;
  92. /// <summary>
  93. /// Our children are actually just references to the ones in the physical root...
  94. /// </summary>
  95. /// <value>The actual children.</value>
  96. protected override IEnumerable<BaseItem> ActualChildren
  97. {
  98. get { return _actualChildren ?? (_actualChildren = GetActualChildren()); }
  99. }
  100. private IEnumerable<BaseItem> GetActualChildren()
  101. {
  102. Dictionary<string, string> locationsDicionary;
  103. try
  104. {
  105. locationsDicionary = ResolveArgs.PhysicalLocations.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
  106. }
  107. catch (IOException ex)
  108. {
  109. Logger.ErrorException("Error getting ResolveArgs for {0}", ex, Path);
  110. return new BaseItem[] { };
  111. }
  112. return
  113. LibraryManager.RootFolder.Children
  114. .OfType<Folder>()
  115. .Where(i => i.Path != null && locationsDicionary.ContainsKey(i.Path))
  116. .SelectMany(c => c.Children)
  117. .ToList();
  118. }
  119. public void ResetDynamicChildren()
  120. {
  121. _actualChildren = null;
  122. _linkedChildren = null;
  123. }
  124. }
  125. }