CollectionFolder.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Runtime.Serialization;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace MediaBrowser.Controller.Entities
  10. {
  11. /// <summary>
  12. /// Specialized Folder class that points to a subset of the physical folders in the system.
  13. /// It is created from the user-specific folders within the system root
  14. /// </summary>
  15. public class CollectionFolder : Folder, ICollectionFolder
  16. {
  17. /// <summary>
  18. /// Gets a value indicating whether this instance is virtual folder.
  19. /// </summary>
  20. /// <value><c>true</c> if this instance is virtual folder; otherwise, <c>false</c>.</value>
  21. [IgnoreDataMember]
  22. public override bool IsVirtualFolder
  23. {
  24. get
  25. {
  26. return true;
  27. }
  28. }
  29. public string CollectionType { get; set; }
  30. /// <summary>
  31. /// Allow different display preferences for each collection folder
  32. /// </summary>
  33. /// <value>The display prefs id.</value>
  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. //we don't directly validate our children
  58. //but we do need to clear out the index cache...
  59. IndexCache = new ConcurrentDictionary<string, List<BaseItem>>(StringComparer.OrdinalIgnoreCase);
  60. return NullTaskResult;
  61. }
  62. /// <summary>
  63. /// Our children are actually just references to the ones in the physical root...
  64. /// </summary>
  65. /// <value>The linked children.</value>
  66. public override List<LinkedChild> LinkedChildren
  67. {
  68. get
  69. {
  70. Dictionary<string, string> locationsDicionary;
  71. try
  72. {
  73. locationsDicionary = ResolveArgs.PhysicalLocations.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
  74. }
  75. catch (IOException ex)
  76. {
  77. Logger.ErrorException("Error getting ResolveArgs for {0}", ex, Path);
  78. return new List<LinkedChild>();
  79. }
  80. return LibraryManager.RootFolder.Children
  81. .OfType<Folder>()
  82. .Where(i => i.Path != null && locationsDicionary.ContainsKey(i.Path))
  83. .SelectMany(c => c.LinkedChildren).ToList();
  84. }
  85. set
  86. {
  87. base.LinkedChildren = value;
  88. }
  89. }
  90. /// <summary>
  91. /// Our children are actually just references to the ones in the physical root...
  92. /// </summary>
  93. /// <value>The actual children.</value>
  94. protected override ConcurrentDictionary<Guid, BaseItem> ActualChildren
  95. {
  96. get
  97. {
  98. Dictionary<string, string> locationsDicionary;
  99. try
  100. {
  101. locationsDicionary = ResolveArgs.PhysicalLocations.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
  102. }
  103. catch (IOException ex)
  104. {
  105. Logger.ErrorException("Error getting ResolveArgs for {0}", ex, Path);
  106. return new ConcurrentDictionary<Guid, BaseItem>();
  107. }
  108. var ourChildren =
  109. LibraryManager.RootFolder.Children
  110. .OfType<Folder>()
  111. .Where(i => i.Path != null && locationsDicionary.ContainsKey(i.Path))
  112. .SelectMany(c => c.Children);
  113. return new ConcurrentDictionary<Guid, BaseItem>(ourChildren.ToDictionary(i => i.Id));
  114. }
  115. }
  116. }
  117. }