MusicGenre.cs 3.5 KB

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