2
0

Genre.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text.Json.Serialization;
  5. using MediaBrowser.Controller.Entities.Audio;
  6. using MediaBrowser.Controller.Extensions;
  7. using Microsoft.Extensions.Logging;
  8. namespace MediaBrowser.Controller.Entities
  9. {
  10. /// <summary>
  11. /// Class Genre.
  12. /// </summary>
  13. public class Genre : BaseItem, IItemByName
  14. {
  15. public override List<string> GetUserDataKeys()
  16. {
  17. var list = base.GetUserDataKeys();
  18. list.Insert(0, GetType().Name + "-" + (Name ?? string.Empty).RemoveDiacritics());
  19. return list;
  20. }
  21. public override string CreatePresentationUniqueKey()
  22. {
  23. return GetUserDataKeys()[0];
  24. }
  25. public override double GetDefaultPrimaryImageAspectRatio()
  26. {
  27. return 1;
  28. }
  29. /// <summary>
  30. /// Returns the folder containing the item.
  31. /// If the item is a folder, it returns the folder itself.
  32. /// </summary>
  33. /// <value>The containing folder path.</value>
  34. [JsonIgnore]
  35. public override string ContainingFolderPath => Path;
  36. [JsonIgnore]
  37. public override bool IsDisplayedAsFolder => true;
  38. [JsonIgnore]
  39. public override bool SupportsAncestors => false;
  40. public override bool IsSaveLocalMetadataEnabled()
  41. {
  42. return true;
  43. }
  44. public override bool CanDelete()
  45. {
  46. return false;
  47. }
  48. public IList<BaseItem> GetTaggedItems(InternalItemsQuery query)
  49. {
  50. query.GenreIds = new[] { Id };
  51. query.ExcludeItemTypes = new[]
  52. {
  53. nameof(MusicVideo),
  54. nameof(Entities.Audio.Audio),
  55. nameof(MusicAlbum),
  56. nameof(MusicArtist)
  57. };
  58. return LibraryManager.GetItemList(query);
  59. }
  60. [JsonIgnore]
  61. public override bool SupportsPeople => false;
  62. public static string GetPath(string name)
  63. {
  64. return GetPath(name, true);
  65. }
  66. public static string GetPath(string name, bool normalizeName)
  67. {
  68. // Trim the period at the end because windows will have a hard time with that
  69. var validName = normalizeName ?
  70. FileSystem.GetValidFilename(name).Trim().TrimEnd('.') :
  71. name;
  72. return System.IO.Path.Combine(ConfigurationManager.ApplicationPaths.GenrePath, validName);
  73. }
  74. private string GetRebasedPath()
  75. {
  76. return GetPath(System.IO.Path.GetFileName(Path), false);
  77. }
  78. public override bool RequiresRefresh()
  79. {
  80. var newPath = GetRebasedPath();
  81. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  82. {
  83. Logger.LogDebug("{0} path has changed from {1} to {2}", GetType().Name, Path, newPath);
  84. return true;
  85. }
  86. return base.RequiresRefresh();
  87. }
  88. /// <summary>
  89. /// This is called before any metadata refresh and returns true or false indicating if changes were made.
  90. /// </summary>
  91. public override bool BeforeMetadataRefresh(bool replaceAllMetdata)
  92. {
  93. var hasChanges = base.BeforeMetadataRefresh(replaceAllMetdata);
  94. var newPath = GetRebasedPath();
  95. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  96. {
  97. Path = newPath;
  98. hasChanges = true;
  99. }
  100. return hasChanges;
  101. }
  102. }
  103. }