2
0

UserRootFolder.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using MediaBrowser.Model.Serialization;
  2. using MediaBrowser.Controller.Providers;
  3. using MediaBrowser.Model.Library;
  4. using MediaBrowser.Model.Querying;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace MediaBrowser.Controller.Entities
  11. {
  12. /// <summary>
  13. /// Special class used for User Roots. Children contain actual ones defined for this user
  14. /// PLUS the virtual folders from the physical root (added by plug-ins).
  15. /// </summary>
  16. public class UserRootFolder : Folder
  17. {
  18. private List<Guid> _childrenIds = null;
  19. private readonly object _childIdsLock = new object();
  20. protected override List<BaseItem> LoadChildren()
  21. {
  22. lock (_childIdsLock)
  23. {
  24. if (_childrenIds == null)
  25. {
  26. var list = base.LoadChildren();
  27. _childrenIds = list.Select(i => i.Id).ToList();
  28. return list;
  29. }
  30. return _childrenIds.Select(LibraryManager.GetItemById).Where(i => i != null).ToList();
  31. }
  32. }
  33. [IgnoreDataMember]
  34. public override bool SupportsInheritedParentImages
  35. {
  36. get
  37. {
  38. return false;
  39. }
  40. }
  41. [IgnoreDataMember]
  42. public override bool SupportsPlayedStatus
  43. {
  44. get
  45. {
  46. return false;
  47. }
  48. }
  49. private void ClearCache()
  50. {
  51. lock (_childIdsLock)
  52. {
  53. _childrenIds = null;
  54. }
  55. }
  56. protected override QueryResult<BaseItem> GetItemsInternal(InternalItemsQuery query)
  57. {
  58. if (query.Recursive)
  59. {
  60. return QueryRecursive(query);
  61. }
  62. var result = UserViewManager.GetUserViews(new UserViewQuery
  63. {
  64. UserId = query.User.Id,
  65. PresetViews = query.PresetViews
  66. });
  67. var itemsArray = result;
  68. var totalCount = itemsArray.Length;
  69. return new QueryResult<BaseItem>
  70. {
  71. TotalRecordCount = totalCount,
  72. Items = itemsArray
  73. };
  74. }
  75. public override int GetChildCount(User user)
  76. {
  77. return GetChildren(user, true).Count;
  78. }
  79. [IgnoreDataMember]
  80. protected override bool SupportsShortcutChildren
  81. {
  82. get
  83. {
  84. return true;
  85. }
  86. }
  87. [IgnoreDataMember]
  88. public override bool IsPreSorted
  89. {
  90. get
  91. {
  92. return true;
  93. }
  94. }
  95. protected override IEnumerable<BaseItem> GetEligibleChildrenForRecursiveChildren(User user)
  96. {
  97. var list = base.GetEligibleChildrenForRecursiveChildren(user).ToList();
  98. list.AddRange(LibraryManager.RootFolder.VirtualChildren);
  99. return list;
  100. }
  101. public override bool BeforeMetadataRefresh(bool replaceAllMetdata)
  102. {
  103. ClearCache();
  104. var hasChanges = base.BeforeMetadataRefresh(replaceAllMetdata);
  105. if (string.Equals("default", Name, StringComparison.OrdinalIgnoreCase))
  106. {
  107. Name = "Media Folders";
  108. hasChanges = true;
  109. }
  110. return hasChanges;
  111. }
  112. protected override IEnumerable<BaseItem> GetNonCachedChildren(IDirectoryService directoryService)
  113. {
  114. ClearCache();
  115. return base.GetNonCachedChildren(directoryService);
  116. }
  117. protected override async Task ValidateChildrenInternal(IProgress<double> progress, CancellationToken cancellationToken, bool recursive, bool refreshChildMetadata, MetadataRefreshOptions refreshOptions, IDirectoryService directoryService)
  118. {
  119. ClearCache();
  120. await base.ValidateChildrenInternal(progress, cancellationToken, recursive, refreshChildMetadata, refreshOptions, directoryService)
  121. .ConfigureAwait(false);
  122. ClearCache();
  123. // Not the best way to handle this, but it solves an issue
  124. // CollectionFolders aren't always getting saved after changes
  125. // This means that grabbing the item by Id may end up returning the old one
  126. // Fix is in two places - make sure the folder gets saved
  127. // And here to remedy it for affected users.
  128. // In theory this can be removed eventually.
  129. foreach (var item in Children)
  130. {
  131. LibraryManager.RegisterItem(item);
  132. }
  133. }
  134. }
  135. }