Folder.cs 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Common.Win32;
  3. using MediaBrowser.Controller.Library;
  4. using MediaBrowser.Controller.Localization;
  5. using MediaBrowser.Controller.Resolvers;
  6. using MediaBrowser.Controller.Sorting;
  7. using MediaBrowser.Model.Entities;
  8. using MediaBrowser.Model.Tasks;
  9. using System;
  10. using System.Collections.Concurrent;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Runtime.Serialization;
  15. using System.Threading;
  16. using System.Threading.Tasks;
  17. using SortOrder = MediaBrowser.Controller.Sorting.SortOrder;
  18. namespace MediaBrowser.Controller.Entities
  19. {
  20. /// <summary>
  21. /// Class Folder
  22. /// </summary>
  23. public class Folder : BaseItem
  24. {
  25. /// <summary>
  26. /// Gets a value indicating whether this instance is folder.
  27. /// </summary>
  28. /// <value><c>true</c> if this instance is folder; otherwise, <c>false</c>.</value>
  29. [IgnoreDataMember]
  30. public override bool IsFolder
  31. {
  32. get
  33. {
  34. return true;
  35. }
  36. }
  37. /// <summary>
  38. /// Gets or sets a value indicating whether this instance is physical root.
  39. /// </summary>
  40. /// <value><c>true</c> if this instance is physical root; otherwise, <c>false</c>.</value>
  41. public bool IsPhysicalRoot { get; set; }
  42. /// <summary>
  43. /// Gets or sets a value indicating whether this instance is root.
  44. /// </summary>
  45. /// <value><c>true</c> if this instance is root; otherwise, <c>false</c>.</value>
  46. public bool IsRoot { get; set; }
  47. /// <summary>
  48. /// Gets a value indicating whether this instance is virtual folder.
  49. /// </summary>
  50. /// <value><c>true</c> if this instance is virtual folder; otherwise, <c>false</c>.</value>
  51. [IgnoreDataMember]
  52. public virtual bool IsVirtualFolder
  53. {
  54. get
  55. {
  56. return false;
  57. }
  58. }
  59. /// <summary>
  60. /// Return the id that should be used to key display prefs for this item.
  61. /// Default is based on the type for everything except actual generic folders.
  62. /// </summary>
  63. /// <value>The display prefs id.</value>
  64. [IgnoreDataMember]
  65. public virtual Guid DisplayPrefsId
  66. {
  67. get
  68. {
  69. var thisType = GetType();
  70. return thisType == typeof(Folder) ? Id : thisType.FullName.GetMD5();
  71. }
  72. }
  73. /// <summary>
  74. /// The _display prefs
  75. /// </summary>
  76. private IEnumerable<DisplayPreferences> _displayPrefs;
  77. /// <summary>
  78. /// The _display prefs initialized
  79. /// </summary>
  80. private bool _displayPrefsInitialized;
  81. /// <summary>
  82. /// The _display prefs sync lock
  83. /// </summary>
  84. private object _displayPrefsSyncLock = new object();
  85. /// <summary>
  86. /// Gets the display prefs.
  87. /// </summary>
  88. /// <value>The display prefs.</value>
  89. [IgnoreDataMember]
  90. public IEnumerable<DisplayPreferences> DisplayPrefs
  91. {
  92. get
  93. {
  94. // Call ToList to exhaust the stream because we'll be iterating over this multiple times
  95. LazyInitializer.EnsureInitialized(ref _displayPrefs, ref _displayPrefsInitialized, ref _displayPrefsSyncLock, () => Kernel.Instance.DisplayPreferencesRepository.RetrieveDisplayPrefs(this).ToList());
  96. return _displayPrefs;
  97. }
  98. private set
  99. {
  100. _displayPrefs = value;
  101. if (value == null)
  102. {
  103. _displayPrefsInitialized = false;
  104. }
  105. }
  106. }
  107. /// <summary>
  108. /// Gets the display prefs.
  109. /// </summary>
  110. /// <param name="user">The user.</param>
  111. /// <param name="createIfNull">if set to <c>true</c> [create if null].</param>
  112. /// <returns>DisplayPreferences.</returns>
  113. /// <exception cref="System.ArgumentNullException"></exception>
  114. public DisplayPreferences GetDisplayPrefs(User user, bool createIfNull)
  115. {
  116. if (user == null)
  117. {
  118. throw new ArgumentNullException();
  119. }
  120. if (DisplayPrefs == null)
  121. {
  122. if (!createIfNull)
  123. {
  124. return null;
  125. }
  126. AddOrUpdateDisplayPrefs(user, new DisplayPreferences { UserId = user.Id });
  127. }
  128. var data = DisplayPrefs.FirstOrDefault(u => u.UserId == user.Id);
  129. if (data == null && createIfNull)
  130. {
  131. data = new DisplayPreferences { UserId = user.Id };
  132. AddOrUpdateDisplayPrefs(user, data);
  133. }
  134. return data;
  135. }
  136. /// <summary>
  137. /// Adds the or update display prefs.
  138. /// </summary>
  139. /// <param name="user">The user.</param>
  140. /// <param name="data">The data.</param>
  141. /// <exception cref="System.ArgumentNullException"></exception>
  142. public void AddOrUpdateDisplayPrefs(User user, DisplayPreferences data)
  143. {
  144. if (user == null)
  145. {
  146. throw new ArgumentNullException();
  147. }
  148. if (data == null)
  149. {
  150. throw new ArgumentNullException();
  151. }
  152. data.UserId = user.Id;
  153. if (DisplayPrefs == null)
  154. {
  155. DisplayPrefs = new[] { data };
  156. }
  157. else
  158. {
  159. var list = DisplayPrefs.Where(u => u.UserId != user.Id).ToList();
  160. list.Add(data);
  161. DisplayPrefs = list;
  162. }
  163. }
  164. #region Sorting
  165. /// <summary>
  166. /// The _sort by options
  167. /// </summary>
  168. private Dictionary<string, IComparer<BaseItem>> _sortByOptions;
  169. /// <summary>
  170. /// Dictionary of sort options - consists of a display value (localized) and an IComparer of BaseItem
  171. /// </summary>
  172. /// <value>The sort by options.</value>
  173. [IgnoreDataMember]
  174. public Dictionary<string, IComparer<BaseItem>> SortByOptions
  175. {
  176. get { return _sortByOptions ?? (_sortByOptions = GetSortByOptions()); }
  177. }
  178. /// <summary>
  179. /// Returns the valid set of sort by options for this folder type.
  180. /// Override or extend to modify.
  181. /// </summary>
  182. /// <returns>Dictionary{System.StringIComparer{BaseItem}}.</returns>
  183. protected virtual Dictionary<string, IComparer<BaseItem>> GetSortByOptions()
  184. {
  185. return new Dictionary<string, IComparer<BaseItem>> {
  186. {LocalizedStrings.Instance.GetString("NameDispPref"), new BaseItemComparer(SortOrder.Name, Logger)},
  187. {LocalizedStrings.Instance.GetString("DateDispPref"), new BaseItemComparer(SortOrder.Date, Logger)},
  188. {LocalizedStrings.Instance.GetString("RatingDispPref"), new BaseItemComparer(SortOrder.Rating, Logger)},
  189. {LocalizedStrings.Instance.GetString("RuntimeDispPref"), new BaseItemComparer(SortOrder.Runtime, Logger)},
  190. {LocalizedStrings.Instance.GetString("YearDispPref"), new BaseItemComparer(SortOrder.Year, Logger)}
  191. };
  192. }
  193. /// <summary>
  194. /// Get a sorting comparer by name
  195. /// </summary>
  196. /// <param name="name">The name.</param>
  197. /// <returns>IComparer{BaseItem}.</returns>
  198. private IComparer<BaseItem> GetSortingFunction(string name)
  199. {
  200. IComparer<BaseItem> sorting;
  201. SortByOptions.TryGetValue(name ?? "", out sorting);
  202. return sorting ?? new BaseItemComparer(SortOrder.Name, Logger);
  203. }
  204. /// <summary>
  205. /// Get the list of sort by choices for this folder (localized).
  206. /// </summary>
  207. /// <value>The sort by option strings.</value>
  208. [IgnoreDataMember]
  209. public IEnumerable<string> SortByOptionStrings
  210. {
  211. get { return SortByOptions.Keys; }
  212. }
  213. #endregion
  214. #region Indexing
  215. /// <summary>
  216. /// The _index by options
  217. /// </summary>
  218. private Dictionary<string, Func<User, IEnumerable<BaseItem>>> _indexByOptions;
  219. /// <summary>
  220. /// Dictionary of index options - consists of a display value and an indexing function
  221. /// which takes User as a parameter and returns an IEnum of BaseItem
  222. /// </summary>
  223. /// <value>The index by options.</value>
  224. [IgnoreDataMember]
  225. public Dictionary<string, Func<User, IEnumerable<BaseItem>>> IndexByOptions
  226. {
  227. get { return _indexByOptions ?? (_indexByOptions = GetIndexByOptions()); }
  228. }
  229. /// <summary>
  230. /// Returns the valid set of index by options for this folder type.
  231. /// Override or extend to modify.
  232. /// </summary>
  233. /// <returns>Dictionary{System.StringFunc{UserIEnumerable{BaseItem}}}.</returns>
  234. protected virtual Dictionary<string, Func<User, IEnumerable<BaseItem>>> GetIndexByOptions()
  235. {
  236. return new Dictionary<string, Func<User, IEnumerable<BaseItem>>> {
  237. {LocalizedStrings.Instance.GetString("NoneDispPref"), null},
  238. {LocalizedStrings.Instance.GetString("PerformerDispPref"), GetIndexByPerformer},
  239. {LocalizedStrings.Instance.GetString("GenreDispPref"), GetIndexByGenre},
  240. {LocalizedStrings.Instance.GetString("DirectorDispPref"), GetIndexByDirector},
  241. {LocalizedStrings.Instance.GetString("YearDispPref"), GetIndexByYear},
  242. {LocalizedStrings.Instance.GetString("OfficialRatingDispPref"), null},
  243. {LocalizedStrings.Instance.GetString("StudioDispPref"), GetIndexByStudio}
  244. };
  245. }
  246. /// <summary>
  247. /// Gets the index by actor.
  248. /// </summary>
  249. /// <param name="user">The user.</param>
  250. /// <returns>IEnumerable{BaseItem}.</returns>
  251. protected IEnumerable<BaseItem> GetIndexByPerformer(User user)
  252. {
  253. return GetIndexByPerson(user, new List<string> { PersonType.Actor, PersonType.MusicArtist }, LocalizedStrings.Instance.GetString("PerformerDispPref"));
  254. }
  255. /// <summary>
  256. /// Gets the index by director.
  257. /// </summary>
  258. /// <param name="user">The user.</param>
  259. /// <returns>IEnumerable{BaseItem}.</returns>
  260. protected IEnumerable<BaseItem> GetIndexByDirector(User user)
  261. {
  262. return GetIndexByPerson(user, new List<string> { PersonType.Director }, LocalizedStrings.Instance.GetString("DirectorDispPref"));
  263. }
  264. /// <summary>
  265. /// Gets the index by person.
  266. /// </summary>
  267. /// <param name="user">The user.</param>
  268. /// <param name="personTypes">The person types we should match on</param>
  269. /// <param name="indexName">Name of the index.</param>
  270. /// <returns>IEnumerable{BaseItem}.</returns>
  271. protected IEnumerable<BaseItem> GetIndexByPerson(User user, List<string> personTypes, string indexName)
  272. {
  273. // Even though this implementation means multiple iterations over the target list - it allows us to defer
  274. // the retrieval of the individual children for each index value until they are requested.
  275. using (new Profiler(indexName + " Index Build for " + Name, Logger))
  276. {
  277. // Put this in a local variable to avoid an implicitly captured closure
  278. var currentIndexName = indexName;
  279. var us = this;
  280. var candidates = RecursiveChildren.Where(i => i.IncludeInIndex && i.AllPeople != null).ToList();
  281. return candidates.AsParallel().SelectMany(i => i.AllPeople.Where(p => personTypes.Contains(p.Type))
  282. .Select(a => a.Name))
  283. .Distinct()
  284. .Select(i =>
  285. {
  286. try
  287. {
  288. return Kernel.Instance.LibraryManager.GetPerson(i).Result;
  289. }
  290. catch (IOException ex)
  291. {
  292. Logger.ErrorException("Error getting person {0}", ex, i);
  293. return null;
  294. }
  295. catch (AggregateException ex)
  296. {
  297. Logger.ErrorException("Error getting person {0}", ex, i);
  298. return null;
  299. }
  300. })
  301. .Where(i => i != null)
  302. .Select(a => new IndexFolder(us, a,
  303. candidates.Where(i => i.AllPeople.Any(p => personTypes.Contains(p.Type) && p.Name.Equals(a.Name, StringComparison.OrdinalIgnoreCase))
  304. ), currentIndexName));
  305. }
  306. }
  307. /// <summary>
  308. /// Gets the index by studio.
  309. /// </summary>
  310. /// <param name="user">The user.</param>
  311. /// <returns>IEnumerable{BaseItem}.</returns>
  312. protected IEnumerable<BaseItem> GetIndexByStudio(User user)
  313. {
  314. // Even though this implementation means multiple iterations over the target list - it allows us to defer
  315. // the retrieval of the individual children for each index value until they are requested.
  316. using (new Profiler("Studio Index Build for " + Name, Logger))
  317. {
  318. var indexName = LocalizedStrings.Instance.GetString("StudioDispPref");
  319. var candidates = RecursiveChildren.Where(i => i.IncludeInIndex && i.Studios != null).ToList();
  320. return candidates.AsParallel().SelectMany(i => i.Studios)
  321. .Distinct()
  322. .Select(i =>
  323. {
  324. try
  325. {
  326. return Kernel.Instance.LibraryManager.GetStudio(i).Result;
  327. }
  328. catch (IOException ex)
  329. {
  330. Logger.ErrorException("Error getting studio {0}", ex, i);
  331. return null;
  332. }
  333. catch (AggregateException ex)
  334. {
  335. Logger.ErrorException("Error getting studio {0}", ex, i);
  336. return null;
  337. }
  338. })
  339. .Where(i => i != null)
  340. .Select(ndx => new IndexFolder(this, ndx, candidates.Where(i => i.Studios.Any(s => s.Equals(ndx.Name, StringComparison.OrdinalIgnoreCase))), indexName));
  341. }
  342. }
  343. /// <summary>
  344. /// Gets the index by genre.
  345. /// </summary>
  346. /// <param name="user">The user.</param>
  347. /// <returns>IEnumerable{BaseItem}.</returns>
  348. protected IEnumerable<BaseItem> GetIndexByGenre(User user)
  349. {
  350. // Even though this implementation means multiple iterations over the target list - it allows us to defer
  351. // the retrieval of the individual children for each index value until they are requested.
  352. using (new Profiler("Genre Index Build for " + Name, Logger))
  353. {
  354. var indexName = LocalizedStrings.Instance.GetString("GenreDispPref");
  355. //we need a copy of this so we don't double-recurse
  356. var candidates = RecursiveChildren.Where(i => i.IncludeInIndex && i.Genres != null).ToList();
  357. return candidates.AsParallel().SelectMany(i => i.Genres)
  358. .Distinct()
  359. .Select(i =>
  360. {
  361. try
  362. {
  363. return Kernel.Instance.LibraryManager.GetGenre(i).Result;
  364. }
  365. catch (IOException ex)
  366. {
  367. Logger.ErrorException("Error getting genre {0}", ex, i);
  368. return null;
  369. }
  370. catch (AggregateException ex)
  371. {
  372. Logger.ErrorException("Error getting genre {0}", ex, i);
  373. return null;
  374. }
  375. })
  376. .Where(i => i != null)
  377. .Select(genre => new IndexFolder(this, genre, candidates.Where(i => i.Genres.Any(g => g.Equals(genre.Name, StringComparison.OrdinalIgnoreCase))), indexName)
  378. );
  379. }
  380. }
  381. /// <summary>
  382. /// Gets the index by year.
  383. /// </summary>
  384. /// <param name="user">The user.</param>
  385. /// <returns>IEnumerable{BaseItem}.</returns>
  386. protected IEnumerable<BaseItem> GetIndexByYear(User user)
  387. {
  388. // Even though this implementation means multiple iterations over the target list - it allows us to defer
  389. // the retrieval of the individual children for each index value until they are requested.
  390. using (new Profiler("Production Year Index Build for " + Name, Logger))
  391. {
  392. var indexName = LocalizedStrings.Instance.GetString("YearDispPref");
  393. //we need a copy of this so we don't double-recurse
  394. var candidates = RecursiveChildren.Where(i => i.IncludeInIndex && i.ProductionYear.HasValue).ToList();
  395. return candidates.AsParallel().Select(i => i.ProductionYear.Value)
  396. .Distinct()
  397. .Select(i =>
  398. {
  399. try
  400. {
  401. return Kernel.Instance.LibraryManager.GetYear(i).Result;
  402. }
  403. catch (IOException ex)
  404. {
  405. Logger.ErrorException("Error getting year {0}", ex, i);
  406. return null;
  407. }
  408. catch (AggregateException ex)
  409. {
  410. Logger.ErrorException("Error getting year {0}", ex, i);
  411. return null;
  412. }
  413. })
  414. .Where(i => i != null)
  415. .Select(ndx => new IndexFolder(this, ndx, candidates.Where(i => i.ProductionYear == int.Parse(ndx.Name)), indexName));
  416. }
  417. }
  418. /// <summary>
  419. /// Returns the indexed children for this user from the cache. Caches them if not already there.
  420. /// </summary>
  421. /// <param name="user">The user.</param>
  422. /// <param name="indexBy">The index by.</param>
  423. /// <returns>IEnumerable{BaseItem}.</returns>
  424. private IEnumerable<BaseItem> GetIndexedChildren(User user, string indexBy)
  425. {
  426. List<BaseItem> result;
  427. var cacheKey = user.Name + indexBy;
  428. IndexCache.TryGetValue(cacheKey, out result);
  429. if (result == null)
  430. {
  431. //not cached - cache it
  432. Func<User, IEnumerable<BaseItem>> indexing;
  433. IndexByOptions.TryGetValue(indexBy, out indexing);
  434. result = BuildIndex(indexBy, indexing, user);
  435. }
  436. return result;
  437. }
  438. /// <summary>
  439. /// Get the list of indexy by choices for this folder (localized).
  440. /// </summary>
  441. /// <value>The index by option strings.</value>
  442. [IgnoreDataMember]
  443. public IEnumerable<string> IndexByOptionStrings
  444. {
  445. get { return IndexByOptions.Keys; }
  446. }
  447. /// <summary>
  448. /// The index cache
  449. /// </summary>
  450. protected ConcurrentDictionary<string, List<BaseItem>> IndexCache = new ConcurrentDictionary<string, List<BaseItem>>(StringComparer.OrdinalIgnoreCase);
  451. /// <summary>
  452. /// Builds the index.
  453. /// </summary>
  454. /// <param name="indexKey">The index key.</param>
  455. /// <param name="indexFunction">The index function.</param>
  456. /// <param name="user">The user.</param>
  457. /// <returns>List{BaseItem}.</returns>
  458. protected virtual List<BaseItem> BuildIndex(string indexKey, Func<User, IEnumerable<BaseItem>> indexFunction, User user)
  459. {
  460. return indexFunction != null
  461. ? IndexCache[user.Name + indexKey] = indexFunction(user).ToList()
  462. : null;
  463. }
  464. #endregion
  465. /// <summary>
  466. /// The children
  467. /// </summary>
  468. private ConcurrentBag<BaseItem> _children;
  469. /// <summary>
  470. /// The _children initialized
  471. /// </summary>
  472. private bool _childrenInitialized;
  473. /// <summary>
  474. /// The _children sync lock
  475. /// </summary>
  476. private object _childrenSyncLock = new object();
  477. /// <summary>
  478. /// Gets or sets the actual children.
  479. /// </summary>
  480. /// <value>The actual children.</value>
  481. protected virtual ConcurrentBag<BaseItem> ActualChildren
  482. {
  483. get
  484. {
  485. LazyInitializer.EnsureInitialized(ref _children, ref _childrenInitialized, ref _childrenSyncLock, LoadChildren);
  486. return _children;
  487. }
  488. private set
  489. {
  490. _children = value;
  491. if (value == null)
  492. {
  493. _childrenInitialized = false;
  494. }
  495. }
  496. }
  497. /// <summary>
  498. /// thread-safe access to the actual children of this folder - without regard to user
  499. /// </summary>
  500. /// <value>The children.</value>
  501. [IgnoreDataMember]
  502. public ConcurrentBag<BaseItem> Children
  503. {
  504. get
  505. {
  506. return ActualChildren;
  507. }
  508. }
  509. /// <summary>
  510. /// thread-safe access to all recursive children of this folder - without regard to user
  511. /// </summary>
  512. /// <value>The recursive children.</value>
  513. [IgnoreDataMember]
  514. public IEnumerable<BaseItem> RecursiveChildren
  515. {
  516. get
  517. {
  518. foreach (var item in Children)
  519. {
  520. yield return item;
  521. if (item.IsFolder)
  522. {
  523. var subFolder = (Folder)item;
  524. foreach (var subitem in subFolder.RecursiveChildren)
  525. {
  526. yield return subitem;
  527. }
  528. }
  529. }
  530. }
  531. }
  532. /// <summary>
  533. /// Loads our children. Validation will occur externally.
  534. /// We want this sychronous.
  535. /// </summary>
  536. /// <returns>ConcurrentBag{BaseItem}.</returns>
  537. protected virtual ConcurrentBag<BaseItem> LoadChildren()
  538. {
  539. //just load our children from the repo - the library will be validated and maintained in other processes
  540. return new ConcurrentBag<BaseItem>(GetCachedChildren());
  541. }
  542. /// <summary>
  543. /// Gets or sets the current validation cancellation token source.
  544. /// </summary>
  545. /// <value>The current validation cancellation token source.</value>
  546. private CancellationTokenSource CurrentValidationCancellationTokenSource { get; set; }
  547. /// <summary>
  548. /// Validates that the children of the folder still exist
  549. /// </summary>
  550. /// <param name="progress">The progress.</param>
  551. /// <param name="cancellationToken">The cancellation token.</param>
  552. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  553. /// <returns>Task.</returns>
  554. public async Task ValidateChildren(IProgress<double> progress, CancellationToken cancellationToken, bool? recursive = null)
  555. {
  556. cancellationToken.ThrowIfCancellationRequested();
  557. // Cancel the current validation, if any
  558. if (CurrentValidationCancellationTokenSource != null)
  559. {
  560. CurrentValidationCancellationTokenSource.Cancel();
  561. }
  562. // Create an inner cancellation token. This can cancel all validations from this level on down,
  563. // but nothing above this
  564. var innerCancellationTokenSource = new CancellationTokenSource();
  565. try
  566. {
  567. CurrentValidationCancellationTokenSource = innerCancellationTokenSource;
  568. var linkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(innerCancellationTokenSource.Token, cancellationToken);
  569. await ValidateChildrenInternal(progress, linkedCancellationTokenSource.Token, recursive).ConfigureAwait(false);
  570. }
  571. catch (OperationCanceledException ex)
  572. {
  573. Logger.Info("ValidateChildren cancelled for " + Name);
  574. // If the outer cancelletion token in the cause for the cancellation, throw it
  575. if (cancellationToken.IsCancellationRequested && ex.CancellationToken == cancellationToken)
  576. {
  577. throw;
  578. }
  579. }
  580. finally
  581. {
  582. // Null out the token source
  583. if (CurrentValidationCancellationTokenSource == innerCancellationTokenSource)
  584. {
  585. CurrentValidationCancellationTokenSource = null;
  586. }
  587. innerCancellationTokenSource.Dispose();
  588. }
  589. }
  590. /// <summary>
  591. /// Compare our current children (presumably just read from the repo) with the current state of the file system and adjust for any changes
  592. /// ***Currently does not contain logic to maintain items that are unavailable in the file system***
  593. /// </summary>
  594. /// <param name="progress">The progress.</param>
  595. /// <param name="cancellationToken">The cancellation token.</param>
  596. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  597. /// <returns>Task.</returns>
  598. protected async virtual Task ValidateChildrenInternal(IProgress<double> progress, CancellationToken cancellationToken, bool? recursive = null)
  599. {
  600. // Nothing to do here
  601. if (LocationType != LocationType.FileSystem)
  602. {
  603. return;
  604. }
  605. cancellationToken.ThrowIfCancellationRequested();
  606. var changedArgs = new ChildrenChangedEventArgs(this);
  607. //get the current valid children from filesystem (or wherever)
  608. var nonCachedChildren = GetNonCachedChildren();
  609. if (nonCachedChildren == null) return; //nothing to validate
  610. progress.Report(5);
  611. //build a dictionary of the current children we have now by Id so we can compare quickly and easily
  612. var currentChildren = ActualChildren.ToDictionary(i => i.Id);
  613. //create a list for our validated children
  614. var validChildren = new ConcurrentBag<Tuple<BaseItem, bool>>();
  615. cancellationToken.ThrowIfCancellationRequested();
  616. Parallel.ForEach(nonCachedChildren, child =>
  617. {
  618. BaseItem currentChild;
  619. if (currentChildren.TryGetValue(child.Id, out currentChild))
  620. {
  621. currentChild.ResolveArgs = child.ResolveArgs;
  622. //existing item - check if it has changed
  623. if (currentChild.HasChanged(child))
  624. {
  625. EntityResolutionHelper.EnsureDates(currentChild, child.ResolveArgs);
  626. changedArgs.AddUpdatedItem(currentChild);
  627. validChildren.Add(new Tuple<BaseItem, bool>(currentChild, true));
  628. }
  629. else
  630. {
  631. validChildren.Add(new Tuple<BaseItem, bool>(currentChild, false));
  632. }
  633. }
  634. else
  635. {
  636. //brand new item - needs to be added
  637. changedArgs.AddNewItem(child);
  638. validChildren.Add(new Tuple<BaseItem, bool>(child, true));
  639. }
  640. });
  641. // If any items were added or removed....
  642. if (!changedArgs.ItemsAdded.IsEmpty || currentChildren.Count != validChildren.Count)
  643. {
  644. var newChildren = validChildren.Select(c => c.Item1).ToList();
  645. //that's all the new and changed ones - now see if there are any that are missing
  646. changedArgs.ItemsRemoved = currentChildren.Values.Except(newChildren).ToList();
  647. foreach (var item in changedArgs.ItemsRemoved)
  648. {
  649. Logger.Info("** " + item.Name + " Removed from library.");
  650. }
  651. var childrenReplaced = false;
  652. if (changedArgs.ItemsRemoved.Count > 0)
  653. {
  654. ActualChildren = new ConcurrentBag<BaseItem>(newChildren);
  655. childrenReplaced = true;
  656. }
  657. var saveTasks = new List<Task>();
  658. foreach (var item in changedArgs.ItemsAdded)
  659. {
  660. Logger.Info("** " + item.Name + " Added to library.");
  661. if (!childrenReplaced)
  662. {
  663. _children.Add(item);
  664. }
  665. saveTasks.Add(Kernel.Instance.ItemRepository.SaveItem(item, CancellationToken.None));
  666. }
  667. await Task.WhenAll(saveTasks).ConfigureAwait(false);
  668. //and save children in repo...
  669. Logger.Info("*** Saving " + newChildren.Count + " children for " + Name);
  670. await Kernel.Instance.ItemRepository.SaveChildren(Id, newChildren, CancellationToken.None).ConfigureAwait(false);
  671. }
  672. if (changedArgs.HasChange)
  673. {
  674. //force the indexes to rebuild next time
  675. IndexCache.Clear();
  676. //and fire event
  677. Kernel.Instance.LibraryManager.OnLibraryChanged(changedArgs);
  678. }
  679. progress.Report(15);
  680. cancellationToken.ThrowIfCancellationRequested();
  681. await RefreshChildren(validChildren, progress, cancellationToken, recursive).ConfigureAwait(false);
  682. progress.Report(100);
  683. }
  684. /// <summary>
  685. /// Refreshes the children.
  686. /// </summary>
  687. /// <param name="children">The children.</param>
  688. /// <param name="progress">The progress.</param>
  689. /// <param name="cancellationToken">The cancellation token.</param>
  690. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  691. /// <returns>Task.</returns>
  692. private Task RefreshChildren(IEnumerable<Tuple<BaseItem, bool>> children, IProgress<double> progress, CancellationToken cancellationToken, bool? recursive)
  693. {
  694. var numComplete = 0;
  695. var list = children.ToList();
  696. var tasks = list.Select(tuple => Task.Run(async () =>
  697. {
  698. cancellationToken.ThrowIfCancellationRequested();
  699. var child = tuple.Item1;
  700. //refresh it
  701. await child.RefreshMetadata(cancellationToken, resetResolveArgs: child.IsFolder).ConfigureAwait(false);
  702. //and add it to our valid children
  703. //fire an added event...?
  704. //if it is a folder we need to validate its children as well
  705. // Refresh children if a folder and the item changed or recursive is set to true
  706. var refreshChildren = child.IsFolder && (tuple.Item2 || (recursive.HasValue && recursive.Value));
  707. if (refreshChildren)
  708. {
  709. // Don't refresh children if explicitly set to false
  710. if (recursive.HasValue && recursive.Value == false)
  711. {
  712. refreshChildren = false;
  713. }
  714. }
  715. if (refreshChildren)
  716. {
  717. cancellationToken.ThrowIfCancellationRequested();
  718. await ((Folder)child).ValidateChildren(new Progress<double> { }, cancellationToken, recursive: recursive).ConfigureAwait(false);
  719. }
  720. lock (progress)
  721. {
  722. numComplete++;
  723. double percent = numComplete;
  724. percent /= list.Count;
  725. progress.Report((85 * percent) + 15);
  726. }
  727. }));
  728. cancellationToken.ThrowIfCancellationRequested();
  729. return Task.WhenAll(tasks);
  730. }
  731. /// <summary>
  732. /// Get the children of this folder from the actual file system
  733. /// </summary>
  734. /// <returns>IEnumerable{BaseItem}.</returns>
  735. protected virtual IEnumerable<BaseItem> GetNonCachedChildren()
  736. {
  737. IEnumerable<WIN32_FIND_DATA> fileSystemChildren;
  738. try
  739. {
  740. fileSystemChildren = ResolveArgs.FileSystemChildren;
  741. }
  742. catch (IOException ex)
  743. {
  744. Logger.ErrorException("Error getting ResolveArgs for {0}", ex, Path);
  745. return new List<BaseItem> { };
  746. }
  747. return Kernel.Instance.LibraryManager.GetItems<BaseItem>(fileSystemChildren, this);
  748. }
  749. /// <summary>
  750. /// Get our children from the repo - stubbed for now
  751. /// </summary>
  752. /// <returns>IEnumerable{BaseItem}.</returns>
  753. protected virtual IEnumerable<BaseItem> GetCachedChildren()
  754. {
  755. return Kernel.Instance.ItemRepository.RetrieveChildren(this);
  756. }
  757. /// <summary>
  758. /// Gets allowed children of an item
  759. /// </summary>
  760. /// <param name="user">The user.</param>
  761. /// <param name="indexBy">The index by.</param>
  762. /// <param name="sortBy">The sort by.</param>
  763. /// <param name="sortOrder">The sort order.</param>
  764. /// <returns>IEnumerable{BaseItem}.</returns>
  765. /// <exception cref="System.ArgumentNullException"></exception>
  766. public virtual IEnumerable<BaseItem> GetChildren(User user, string indexBy = null, string sortBy = null, Model.Entities.SortOrder? sortOrder = null)
  767. {
  768. if (user == null)
  769. {
  770. throw new ArgumentNullException();
  771. }
  772. //the true root should return our users root folder children
  773. if (IsPhysicalRoot) return user.RootFolder.GetChildren(user, indexBy, sortBy, sortOrder);
  774. IEnumerable<BaseItem> result = null;
  775. if (!string.IsNullOrEmpty(indexBy))
  776. {
  777. result = GetIndexedChildren(user, indexBy);
  778. }
  779. // If indexed is false or the indexing function is null
  780. if (result == null)
  781. {
  782. result = ActualChildren.Where(c => c.IsVisible(user));
  783. }
  784. if (string.IsNullOrEmpty(sortBy))
  785. {
  786. return result;
  787. }
  788. return sortOrder.HasValue && sortOrder.Value == Model.Entities.SortOrder.Descending
  789. ? result.OrderByDescending(i => i, GetSortingFunction(sortBy))
  790. : result.OrderBy(i => i, GetSortingFunction(sortBy));
  791. }
  792. /// <summary>
  793. /// Gets allowed recursive children of an item
  794. /// </summary>
  795. /// <param name="user">The user.</param>
  796. /// <returns>IEnumerable{BaseItem}.</returns>
  797. /// <exception cref="System.ArgumentNullException"></exception>
  798. public IEnumerable<BaseItem> GetRecursiveChildren(User user)
  799. {
  800. if (user == null)
  801. {
  802. throw new ArgumentNullException();
  803. }
  804. foreach (var item in GetChildren(user))
  805. {
  806. yield return item;
  807. var subFolder = item as Folder;
  808. if (subFolder != null)
  809. {
  810. foreach (var subitem in subFolder.GetRecursiveChildren(user))
  811. {
  812. yield return subitem;
  813. }
  814. }
  815. }
  816. }
  817. /// <summary>
  818. /// Folders need to validate and refresh
  819. /// </summary>
  820. /// <returns>Task.</returns>
  821. public override async Task ChangedExternally()
  822. {
  823. await base.ChangedExternally().ConfigureAwait(false);
  824. var progress = new Progress<double> { };
  825. await ValidateChildren(progress, CancellationToken.None).ConfigureAwait(false);
  826. }
  827. /// <summary>
  828. /// Marks the item as either played or unplayed
  829. /// </summary>
  830. /// <param name="user">The user.</param>
  831. /// <param name="wasPlayed">if set to <c>true</c> [was played].</param>
  832. /// <returns>Task.</returns>
  833. public override async Task SetPlayedStatus(User user, bool wasPlayed)
  834. {
  835. await base.SetPlayedStatus(user, wasPlayed).ConfigureAwait(false);
  836. // Now sweep through recursively and update status
  837. var tasks = GetChildren(user).Select(c => c.SetPlayedStatus(user, wasPlayed));
  838. await Task.WhenAll(tasks).ConfigureAwait(false);
  839. }
  840. /// <summary>
  841. /// Finds an item by ID, recursively
  842. /// </summary>
  843. /// <param name="id">The id.</param>
  844. /// <param name="user">The user.</param>
  845. /// <returns>BaseItem.</returns>
  846. public override BaseItem FindItemById(Guid id, User user)
  847. {
  848. var result = base.FindItemById(id, user);
  849. if (result != null)
  850. {
  851. return result;
  852. }
  853. var children = user == null ? ActualChildren : GetChildren(user);
  854. foreach (var child in children)
  855. {
  856. result = child.FindItemById(id, user);
  857. if (result != null)
  858. {
  859. return result;
  860. }
  861. }
  862. return null;
  863. }
  864. /// <summary>
  865. /// Finds an item by path, recursively
  866. /// </summary>
  867. /// <param name="path">The path.</param>
  868. /// <returns>BaseItem.</returns>
  869. /// <exception cref="System.ArgumentNullException"></exception>
  870. public BaseItem FindByPath(string path)
  871. {
  872. if (string.IsNullOrEmpty(path))
  873. {
  874. throw new ArgumentNullException();
  875. }
  876. try
  877. {
  878. if (ResolveArgs.PhysicalLocations.Contains(path, StringComparer.OrdinalIgnoreCase))
  879. {
  880. return this;
  881. }
  882. }
  883. catch (IOException ex)
  884. {
  885. Logger.ErrorException("Error getting ResolveArgs for {0}", ex, Path);
  886. }
  887. //this should be functionally equivilent to what was here since it is IEnum and works on a thread-safe copy
  888. return RecursiveChildren.FirstOrDefault(i =>
  889. {
  890. try
  891. {
  892. return i.ResolveArgs.PhysicalLocations.Contains(path, StringComparer.OrdinalIgnoreCase);
  893. }
  894. catch (IOException ex)
  895. {
  896. Logger.ErrorException("Error getting ResolveArgs for {0}", ex, Path);
  897. return false;
  898. }
  899. });
  900. }
  901. }
  902. }