MusicGenre.cs 4.5 KB

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