MusicGenre.cs 4.0 KB

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