Genre.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using MediaBrowser.Model.Serialization;
  2. using MediaBrowser.Controller.Entities.Audio;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using MediaBrowser.Common.Extensions;
  7. using MediaBrowser.Controller.Extensions;
  8. using MediaBrowser.Model.Extensions;
  9. namespace MediaBrowser.Controller.Entities
  10. {
  11. /// <summary>
  12. /// Class Genre
  13. /// </summary>
  14. public class Genre : BaseItem, IItemByName
  15. {
  16. public override List<string> GetUserDataKeys()
  17. {
  18. var list = base.GetUserDataKeys();
  19. list.Insert(0, GetType().Name + "-" + (Name ?? string.Empty).RemoveDiacritics());
  20. return list;
  21. }
  22. public override string CreatePresentationUniqueKey()
  23. {
  24. return GetUserDataKeys()[0];
  25. }
  26. /// <summary>
  27. /// Returns the folder containing the item.
  28. /// If the item is a folder, it returns the folder itself
  29. /// </summary>
  30. /// <value>The containing folder path.</value>
  31. [IgnoreDataMember]
  32. public override string ContainingFolderPath
  33. {
  34. get
  35. {
  36. return Path;
  37. }
  38. }
  39. public override bool IsSaveLocalMetadataEnabled()
  40. {
  41. return true;
  42. }
  43. public override bool CanDelete()
  44. {
  45. return false;
  46. }
  47. /// <summary>
  48. /// Gets a value indicating whether this instance is owned item.
  49. /// </summary>
  50. /// <value><c>true</c> if this instance is owned item; otherwise, <c>false</c>.</value>
  51. [IgnoreDataMember]
  52. public override bool IsOwnedItem
  53. {
  54. get
  55. {
  56. return false;
  57. }
  58. }
  59. public IEnumerable<BaseItem> GetTaggedItems(IEnumerable<BaseItem> inputItems)
  60. {
  61. return inputItems.Where(GetItemFilter());
  62. }
  63. public Func<BaseItem, bool> GetItemFilter()
  64. {
  65. return i => !(i is Game) && !(i is IHasMusicGenres) && i.Genres.Contains(Name, StringComparer.OrdinalIgnoreCase);
  66. }
  67. public IEnumerable<BaseItem> GetTaggedItems(InternalItemsQuery query)
  68. {
  69. query.Genres = new[] { Name };
  70. query.ExcludeItemTypes = new[] { typeof(Game).Name, typeof(MusicVideo).Name, typeof(Audio.Audio).Name, typeof(MusicAlbum).Name, typeof(MusicArtist).Name };
  71. return LibraryManager.GetItemList(query);
  72. }
  73. [IgnoreDataMember]
  74. public override bool SupportsPeople
  75. {
  76. get
  77. {
  78. return false;
  79. }
  80. }
  81. public static string GetPath(string name, bool normalizeName = true)
  82. {
  83. // Trim the period at the end because windows will have a hard time with that
  84. var validName = normalizeName ?
  85. FileSystem.GetValidFilename(name).Trim().TrimEnd('.') :
  86. name;
  87. return System.IO.Path.Combine(ConfigurationManager.ApplicationPaths.GenrePath, validName);
  88. }
  89. private string GetRebasedPath()
  90. {
  91. return GetPath(System.IO.Path.GetFileName(Path), false);
  92. }
  93. public override bool RequiresRefresh()
  94. {
  95. var newPath = GetRebasedPath();
  96. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  97. {
  98. Logger.Debug("{0} path has changed from {1} to {2}", GetType().Name, Path, newPath);
  99. return true;
  100. }
  101. return base.RequiresRefresh();
  102. }
  103. /// <summary>
  104. /// This is called before any metadata refresh and returns true or false indicating if changes were made
  105. /// </summary>
  106. public override bool BeforeMetadataRefresh()
  107. {
  108. var hasChanges = base.BeforeMetadataRefresh();
  109. var newPath = GetRebasedPath();
  110. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  111. {
  112. Path = newPath;
  113. hasChanges = true;
  114. }
  115. return hasChanges;
  116. }
  117. }
  118. }