CollectionFolder.cs 4.9 KB

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