MusicArtist.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using MediaBrowser.Controller.Providers;
  2. using MediaBrowser.Model.Configuration;
  3. using MediaBrowser.Model.Entities;
  4. using MediaBrowser.Model.Users;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using MediaBrowser.Model.Serialization;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using MediaBrowser.Controller.Extensions;
  12. using Microsoft.Extensions.Logging;
  13. namespace MediaBrowser.Controller.Entities.Audio
  14. {
  15. /// <summary>
  16. /// Class MusicArtist
  17. /// </summary>
  18. public class MusicArtist : Folder, IItemByName, IHasMusicGenres, IHasDualAccess, IHasLookupInfo<ArtistInfo>
  19. {
  20. [IgnoreDataMember]
  21. public bool IsAccessedByName
  22. {
  23. get { return ParentId.Equals(Guid.Empty); }
  24. }
  25. [IgnoreDataMember]
  26. public override bool IsFolder
  27. {
  28. get
  29. {
  30. return !IsAccessedByName;
  31. }
  32. }
  33. [IgnoreDataMember]
  34. public override bool SupportsInheritedParentImages
  35. {
  36. get
  37. {
  38. return false;
  39. }
  40. }
  41. [IgnoreDataMember]
  42. public override bool SupportsCumulativeRunTimeTicks
  43. {
  44. get
  45. {
  46. return true;
  47. }
  48. }
  49. [IgnoreDataMember]
  50. public override bool IsDisplayedAsFolder
  51. {
  52. get
  53. {
  54. return true;
  55. }
  56. }
  57. [IgnoreDataMember]
  58. public override bool SupportsAddingToPlaylist
  59. {
  60. get { return true; }
  61. }
  62. [IgnoreDataMember]
  63. public override bool SupportsPlayedStatus
  64. {
  65. get
  66. {
  67. return false;
  68. }
  69. }
  70. public override double GetDefaultPrimaryImageAspectRatio()
  71. {
  72. return 1;
  73. }
  74. public override bool CanDelete()
  75. {
  76. return !IsAccessedByName;
  77. }
  78. public IList<BaseItem> GetTaggedItems(InternalItemsQuery query)
  79. {
  80. if (query.IncludeItemTypes.Length == 0)
  81. {
  82. query.IncludeItemTypes = new[] { typeof(Audio).Name, typeof(MusicVideo).Name, typeof(MusicAlbum).Name };
  83. query.ArtistIds = new[] { Id };
  84. }
  85. return LibraryManager.GetItemList(query);
  86. }
  87. [IgnoreDataMember]
  88. public override IEnumerable<BaseItem> Children
  89. {
  90. get
  91. {
  92. if (IsAccessedByName)
  93. {
  94. return new List<BaseItem>();
  95. }
  96. return base.Children;
  97. }
  98. }
  99. public override int GetChildCount(User user)
  100. {
  101. if (IsAccessedByName)
  102. {
  103. return 0;
  104. }
  105. return base.GetChildCount(user);
  106. }
  107. public override bool IsSaveLocalMetadataEnabled()
  108. {
  109. if (IsAccessedByName)
  110. {
  111. return true;
  112. }
  113. return base.IsSaveLocalMetadataEnabled();
  114. }
  115. private readonly Task _cachedTask = Task.FromResult(true);
  116. protected override Task ValidateChildrenInternal(IProgress<double> progress, CancellationToken cancellationToken, bool recursive, bool refreshChildMetadata, MetadataRefreshOptions refreshOptions, IDirectoryService directoryService)
  117. {
  118. if (IsAccessedByName)
  119. {
  120. // Should never get in here anyway
  121. return _cachedTask;
  122. }
  123. return base.ValidateChildrenInternal(progress, cancellationToken, recursive, refreshChildMetadata, refreshOptions, directoryService);
  124. }
  125. public override List<string> GetUserDataKeys()
  126. {
  127. var list = base.GetUserDataKeys();
  128. list.InsertRange(0, GetUserDataKeys(this));
  129. return list;
  130. }
  131. /// <summary>
  132. /// Returns the folder containing the item.
  133. /// If the item is a folder, it returns the folder itself
  134. /// </summary>
  135. /// <value>The containing folder path.</value>
  136. [IgnoreDataMember]
  137. public override string ContainingFolderPath
  138. {
  139. get
  140. {
  141. return Path;
  142. }
  143. }
  144. /// <summary>
  145. /// Gets the user data key.
  146. /// </summary>
  147. /// <param name="item">The item.</param>
  148. /// <returns>System.String.</returns>
  149. private static List<string> GetUserDataKeys(MusicArtist item)
  150. {
  151. var list = new List<string>();
  152. var id = item.GetProviderId(MetadataProviders.MusicBrainzArtist);
  153. if (!string.IsNullOrEmpty(id))
  154. {
  155. list.Add("Artist-Musicbrainz-" + id);
  156. }
  157. list.Add("Artist-" + (item.Name ?? string.Empty).RemoveDiacritics());
  158. return list;
  159. }
  160. public override string CreatePresentationUniqueKey()
  161. {
  162. return "Artist-" + (Name ?? string.Empty).RemoveDiacritics();
  163. }
  164. protected override bool GetBlockUnratedValue(UserPolicy config)
  165. {
  166. return config.BlockUnratedItems.Contains(UnratedItem.Music);
  167. }
  168. public override UnratedItem GetBlockUnratedType()
  169. {
  170. return UnratedItem.Music;
  171. }
  172. public ArtistInfo GetLookupInfo()
  173. {
  174. var info = GetItemLookupInfo<ArtistInfo>();
  175. info.SongInfos = GetRecursiveChildren(i => i is Audio)
  176. .Cast<Audio>()
  177. .Select(i => i.GetLookupInfo())
  178. .ToList();
  179. return info;
  180. }
  181. [IgnoreDataMember]
  182. public override bool SupportsPeople
  183. {
  184. get
  185. {
  186. return false;
  187. }
  188. }
  189. public static string GetPath(string name)
  190. {
  191. return GetPath(name, true);
  192. }
  193. public static string GetPath(string name, bool normalizeName)
  194. {
  195. // Trim the period at the end because windows will have a hard time with that
  196. var validName = normalizeName ?
  197. FileSystem.GetValidFilename(name).Trim().TrimEnd('.') :
  198. name;
  199. return System.IO.Path.Combine(ConfigurationManager.ApplicationPaths.ArtistsPath, validName);
  200. }
  201. private string GetRebasedPath()
  202. {
  203. return GetPath(System.IO.Path.GetFileName(Path), false);
  204. }
  205. public override bool RequiresRefresh()
  206. {
  207. if (IsAccessedByName)
  208. {
  209. var newPath = GetRebasedPath();
  210. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  211. {
  212. Logger.LogDebug("{0} path has changed from {1} to {2}", GetType().Name, Path, newPath);
  213. return true;
  214. }
  215. }
  216. return base.RequiresRefresh();
  217. }
  218. /// <summary>
  219. /// This is called before any metadata refresh and returns true or false indicating if changes were made
  220. /// </summary>
  221. public override bool BeforeMetadataRefresh(bool replaceAllMetdata)
  222. {
  223. var hasChanges = base.BeforeMetadataRefresh(replaceAllMetdata);
  224. if (IsAccessedByName)
  225. {
  226. var newPath = GetRebasedPath();
  227. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  228. {
  229. Path = newPath;
  230. hasChanges = true;
  231. }
  232. }
  233. return hasChanges;
  234. }
  235. }
  236. }