Genre.cs 4.0 KB

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