2
0

Genre.cs 3.5 KB

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