MusicGenre.cs 4.5 KB

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