MusicArtist.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 System.Runtime.Serialization;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using MediaBrowser.Common.Extensions;
  13. namespace MediaBrowser.Controller.Entities.Audio
  14. {
  15. /// <summary>
  16. /// Class MusicArtist
  17. /// </summary>
  18. public class MusicArtist : Folder, IMetadataContainer, IItemByName, IHasMusicGenres, IHasDualAccess, IHasLookupInfo<ArtistInfo>
  19. {
  20. [IgnoreDataMember]
  21. public bool IsAccessedByName
  22. {
  23. get { return ParentId == 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 SupportsCumulativeRunTimeTicks
  35. {
  36. get
  37. {
  38. return true;
  39. }
  40. }
  41. [IgnoreDataMember]
  42. public override bool SupportsAddingToPlaylist
  43. {
  44. get { return true; }
  45. }
  46. public override bool CanDelete()
  47. {
  48. return !IsAccessedByName;
  49. }
  50. public IEnumerable<BaseItem> GetTaggedItems(InternalItemsQuery query)
  51. {
  52. if (query.IncludeItemTypes.Length == 0)
  53. {
  54. query.IncludeItemTypes = new[] { typeof(Audio).Name, typeof(MusicVideo).Name, typeof(MusicAlbum).Name };
  55. query.ArtistNames = new[] { Name };
  56. }
  57. return LibraryManager.GetItemList(query);
  58. }
  59. [IgnoreDataMember]
  60. protected override IEnumerable<BaseItem> ActualChildren
  61. {
  62. get
  63. {
  64. if (IsAccessedByName)
  65. {
  66. return new List<BaseItem>();
  67. }
  68. return base.ActualChildren;
  69. }
  70. }
  71. public override int GetChildCount(User user)
  72. {
  73. if (IsAccessedByName)
  74. {
  75. return 0;
  76. }
  77. return base.GetChildCount(user);
  78. }
  79. public override bool IsSaveLocalMetadataEnabled()
  80. {
  81. if (IsAccessedByName)
  82. {
  83. return true;
  84. }
  85. return base.IsSaveLocalMetadataEnabled();
  86. }
  87. private readonly Task _cachedTask = Task.FromResult(true);
  88. protected override Task ValidateChildrenInternal(IProgress<double> progress, CancellationToken cancellationToken, bool recursive, bool refreshChildMetadata, MetadataRefreshOptions refreshOptions, IDirectoryService directoryService)
  89. {
  90. if (IsAccessedByName)
  91. {
  92. // Should never get in here anyway
  93. return _cachedTask;
  94. }
  95. return base.ValidateChildrenInternal(progress, cancellationToken, recursive, refreshChildMetadata, refreshOptions, directoryService);
  96. }
  97. public override List<string> GetUserDataKeys()
  98. {
  99. var list = base.GetUserDataKeys();
  100. list.InsertRange(0, GetUserDataKeys(this));
  101. return list;
  102. }
  103. /// <summary>
  104. /// Returns the folder containing the item.
  105. /// If the item is a folder, it returns the folder itself
  106. /// </summary>
  107. /// <value>The containing folder path.</value>
  108. [IgnoreDataMember]
  109. public override string ContainingFolderPath
  110. {
  111. get
  112. {
  113. return Path;
  114. }
  115. }
  116. /// <summary>
  117. /// Gets a value indicating whether this instance is owned item.
  118. /// </summary>
  119. /// <value><c>true</c> if this instance is owned item; otherwise, <c>false</c>.</value>
  120. [IgnoreDataMember]
  121. public override bool IsOwnedItem
  122. {
  123. get
  124. {
  125. return false;
  126. }
  127. }
  128. /// <summary>
  129. /// Gets the user data key.
  130. /// </summary>
  131. /// <param name="item">The item.</param>
  132. /// <returns>System.String.</returns>
  133. private static List<string> GetUserDataKeys(MusicArtist item)
  134. {
  135. var list = new List<string>();
  136. var id = item.GetProviderId(MetadataProviders.MusicBrainzArtist);
  137. if (!string.IsNullOrEmpty(id))
  138. {
  139. list.Add("Artist-Musicbrainz-" + id);
  140. }
  141. list.Add("Artist-" + (item.Name ?? string.Empty).RemoveDiacritics());
  142. return list;
  143. }
  144. public override string CreatePresentationUniqueKey()
  145. {
  146. return "Artist-" + (Name ?? string.Empty).RemoveDiacritics();
  147. }
  148. protected override bool GetBlockUnratedValue(UserPolicy config)
  149. {
  150. return config.BlockUnratedItems.Contains(UnratedItem.Music);
  151. }
  152. public override UnratedItem GetBlockUnratedType()
  153. {
  154. return UnratedItem.Music;
  155. }
  156. public async Task RefreshAllMetadata(MetadataRefreshOptions refreshOptions, IProgress<double> progress, CancellationToken cancellationToken)
  157. {
  158. var items = GetRecursiveChildren().ToList();
  159. var songs = items.OfType<Audio>().ToList();
  160. var others = items.Except(songs).ToList();
  161. var totalItems = songs.Count + others.Count;
  162. var numComplete = 0;
  163. var childUpdateType = ItemUpdateType.None;
  164. // Refresh songs
  165. foreach (var item in songs)
  166. {
  167. cancellationToken.ThrowIfCancellationRequested();
  168. var updateType = await item.RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false);
  169. childUpdateType = childUpdateType | updateType;
  170. numComplete++;
  171. double percent = numComplete;
  172. percent /= totalItems;
  173. progress.Report(percent * 100);
  174. }
  175. var parentRefreshOptions = refreshOptions;
  176. if (childUpdateType > ItemUpdateType.None)
  177. {
  178. parentRefreshOptions = new MetadataRefreshOptions(refreshOptions);
  179. parentRefreshOptions.MetadataRefreshMode = MetadataRefreshMode.FullRefresh;
  180. }
  181. // Refresh current item
  182. await RefreshMetadata(parentRefreshOptions, cancellationToken).ConfigureAwait(false);
  183. // Refresh all non-songs
  184. foreach (var item in others)
  185. {
  186. cancellationToken.ThrowIfCancellationRequested();
  187. var updateType = await item.RefreshMetadata(parentRefreshOptions, cancellationToken).ConfigureAwait(false);
  188. numComplete++;
  189. double percent = numComplete;
  190. percent /= totalItems;
  191. progress.Report(percent * 100);
  192. }
  193. progress.Report(100);
  194. }
  195. public ArtistInfo GetLookupInfo()
  196. {
  197. var info = GetItemLookupInfo<ArtistInfo>();
  198. info.SongInfos = GetRecursiveChildren(i => i is Audio)
  199. .Cast<Audio>()
  200. .Select(i => i.GetLookupInfo())
  201. .ToList();
  202. return info;
  203. }
  204. public IEnumerable<BaseItem> GetTaggedItems(IEnumerable<BaseItem> inputItems)
  205. {
  206. return inputItems.Where(GetItemFilter());
  207. }
  208. public Func<BaseItem, bool> GetItemFilter()
  209. {
  210. return i =>
  211. {
  212. var hasArtist = i as IHasArtist;
  213. return hasArtist != null && hasArtist.HasAnyArtist(Name);
  214. };
  215. }
  216. [IgnoreDataMember]
  217. public override bool SupportsPeople
  218. {
  219. get
  220. {
  221. return false;
  222. }
  223. }
  224. public static string GetPath(string name, bool normalizeName = true)
  225. {
  226. // Trim the period at the end because windows will have a hard time with that
  227. var validName = normalizeName ?
  228. FileSystem.GetValidFilename(name).Trim().TrimEnd('.') :
  229. name;
  230. return System.IO.Path.Combine(ConfigurationManager.ApplicationPaths.ArtistsPath, validName);
  231. }
  232. private string GetRebasedPath()
  233. {
  234. return GetPath(System.IO.Path.GetFileName(Path), false);
  235. }
  236. public override bool RequiresRefresh()
  237. {
  238. if (IsAccessedByName)
  239. {
  240. var newPath = GetRebasedPath();
  241. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  242. {
  243. Logger.Debug("{0} path has changed from {1} to {2}", GetType().Name, Path, newPath);
  244. return true;
  245. }
  246. }
  247. return base.RequiresRefresh();
  248. }
  249. /// <summary>
  250. /// This is called before any metadata refresh and returns true or false indicating if changes were made
  251. /// </summary>
  252. public override bool BeforeMetadataRefresh()
  253. {
  254. var hasChanges = base.BeforeMetadataRefresh();
  255. if (IsAccessedByName)
  256. {
  257. var newPath = GetRebasedPath();
  258. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  259. {
  260. Path = newPath;
  261. hasChanges = true;
  262. }
  263. }
  264. return hasChanges;
  265. }
  266. }
  267. }