MusicGenre.cs 3.7 KB

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