MusicArtist.cs 7.6 KB

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