Genre.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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
  10. {
  11. /// <summary>
  12. /// Class Genre.
  13. /// </summary>
  14. [Common.RequiresSourceSerialisation]
  15. public class Genre : BaseItem, IItemByName
  16. {
  17. /// <summary>
  18. /// Gets the folder containing the item.
  19. /// If the item is a folder, it returns the folder itself.
  20. /// </summary>
  21. /// <value>The containing folder path.</value>
  22. [JsonIgnore]
  23. public override string ContainingFolderPath => Path;
  24. [JsonIgnore]
  25. public override bool IsDisplayedAsFolder => true;
  26. [JsonIgnore]
  27. public override bool SupportsAncestors => false;
  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 IsSaveLocalMetadataEnabled()
  45. {
  46. return true;
  47. }
  48. public override bool CanDelete()
  49. {
  50. return false;
  51. }
  52. public IReadOnlyList<BaseItem> GetTaggedItems(InternalItemsQuery query)
  53. {
  54. query.GenreIds = new[] { Id };
  55. query.ExcludeItemTypes = new[]
  56. {
  57. BaseItemKind.MusicVideo,
  58. BaseItemKind.Audio,
  59. BaseItemKind.MusicAlbum,
  60. BaseItemKind.MusicArtist
  61. };
  62. return LibraryManager.GetItemList(query);
  63. }
  64. public TaggedItemCounts GetTaggedItemCounts(InternalItemsQuery query)
  65. {
  66. query.GenreIds = [Id];
  67. query.ExcludeItemTypes =
  68. [
  69. BaseItemKind.MusicVideo,
  70. BaseItemKind.Audio,
  71. BaseItemKind.MusicAlbum,
  72. BaseItemKind.MusicArtist
  73. ];
  74. var counts = new TaggedItemCounts();
  75. query.IncludeItemTypes = [BaseItemKind.Episode];
  76. counts.EpisodeCount = LibraryManager.GetCount(query);
  77. query.IncludeItemTypes = [BaseItemKind.Movie];
  78. counts.MovieCount = LibraryManager.GetCount(query);
  79. query.IncludeItemTypes = [BaseItemKind.LiveTvProgram];
  80. counts.ProgramCount = LibraryManager.GetCount(query);
  81. query.IncludeItemTypes = [BaseItemKind.Series];
  82. counts.SeriesCount = LibraryManager.GetCount(query);
  83. query.IncludeItemTypes = [BaseItemKind.Trailer];
  84. counts.TrailerCount = LibraryManager.GetCount(query);
  85. return counts;
  86. }
  87. public static string GetPath(string name)
  88. {
  89. return GetPath(name, true);
  90. }
  91. public static string GetPath(string name, bool normalizeName)
  92. {
  93. // Trim the period at the end because windows will have a hard time with that
  94. var validName = normalizeName ?
  95. FileSystem.GetValidFilename(name).Trim().TrimEnd('.') :
  96. name;
  97. return System.IO.Path.Combine(ConfigurationManager.ApplicationPaths.GenrePath, validName);
  98. }
  99. private string GetRebasedPath()
  100. {
  101. return GetPath(System.IO.Path.GetFileName(Path), false);
  102. }
  103. public override bool RequiresRefresh()
  104. {
  105. var newPath = GetRebasedPath();
  106. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  107. {
  108. Logger.LogDebug("{0} path has changed from {1} to {2}", GetType().Name, Path, newPath);
  109. return true;
  110. }
  111. return base.RequiresRefresh();
  112. }
  113. /// <summary>
  114. /// This is called before any metadata refresh and returns true if changes were made.
  115. /// </summary>
  116. /// <param name="replaceAllMetadata">Whether to replace all metadata.</param>
  117. /// <returns>true if the item has change, else false.</returns>
  118. public override bool BeforeMetadataRefresh(bool replaceAllMetadata)
  119. {
  120. var hasChanges = base.BeforeMetadataRefresh(replaceAllMetadata);
  121. var newPath = GetRebasedPath();
  122. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  123. {
  124. Path = newPath;
  125. hasChanges = true;
  126. }
  127. return hasChanges;
  128. }
  129. }
  130. }