MusicArtist.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. namespace MediaBrowser.Controller.Entities.Audio
  13. {
  14. /// <summary>
  15. /// Class MusicArtist
  16. /// </summary>
  17. public class MusicArtist : Folder, IMetadataContainer, IItemByName, IHasMusicGenres, IHasDualAccess, IHasProductionLocations, IHasLookupInfo<ArtistInfo>
  18. {
  19. public bool IsAccessedByName { get; set; }
  20. public List<string> ProductionLocations { get; set; }
  21. [IgnoreDataMember]
  22. public override bool IsFolder
  23. {
  24. get
  25. {
  26. return !IsAccessedByName;
  27. }
  28. }
  29. [IgnoreDataMember]
  30. public override bool SupportsAddingToPlaylist
  31. {
  32. get { return true; }
  33. }
  34. public override bool CanDelete()
  35. {
  36. return !IsAccessedByName;
  37. }
  38. protected override IEnumerable<BaseItem> ActualChildren
  39. {
  40. get
  41. {
  42. if (IsAccessedByName)
  43. {
  44. return new List<BaseItem>();
  45. }
  46. return base.ActualChildren;
  47. }
  48. }
  49. public override bool IsSaveLocalMetadataEnabled()
  50. {
  51. if (IsAccessedByName)
  52. {
  53. return true;
  54. }
  55. return base.IsSaveLocalMetadataEnabled();
  56. }
  57. private readonly Task _cachedTask = Task.FromResult(true);
  58. protected override Task ValidateChildrenInternal(IProgress<double> progress, CancellationToken cancellationToken, bool recursive, bool refreshChildMetadata, MetadataRefreshOptions refreshOptions, IDirectoryService directoryService)
  59. {
  60. if (IsAccessedByName)
  61. {
  62. // Should never get in here anyway
  63. return _cachedTask;
  64. }
  65. return base.ValidateChildrenInternal(progress, cancellationToken, recursive, refreshChildMetadata, refreshOptions, directoryService);
  66. }
  67. public MusicArtist()
  68. {
  69. ProductionLocations = new List<string>();
  70. }
  71. /// <summary>
  72. /// Gets the user data key.
  73. /// </summary>
  74. /// <returns>System.String.</returns>
  75. protected override string CreateUserDataKey()
  76. {
  77. return GetUserDataKey(this);
  78. }
  79. /// <summary>
  80. /// Returns the folder containing the item.
  81. /// If the item is a folder, it returns the folder itself
  82. /// </summary>
  83. /// <value>The containing folder path.</value>
  84. [IgnoreDataMember]
  85. public override string ContainingFolderPath
  86. {
  87. get
  88. {
  89. return Path;
  90. }
  91. }
  92. /// <summary>
  93. /// Gets a value indicating whether this instance is owned item.
  94. /// </summary>
  95. /// <value><c>true</c> if this instance is owned item; otherwise, <c>false</c>.</value>
  96. [IgnoreDataMember]
  97. public override bool IsOwnedItem
  98. {
  99. get
  100. {
  101. return false;
  102. }
  103. }
  104. /// <summary>
  105. /// Gets the user data key.
  106. /// </summary>
  107. /// <param name="item">The item.</param>
  108. /// <returns>System.String.</returns>
  109. private static string GetUserDataKey(MusicArtist item)
  110. {
  111. var id = item.GetProviderId(MetadataProviders.MusicBrainzArtist);
  112. if (!string.IsNullOrEmpty(id))
  113. {
  114. return "Artist-Musicbrainz-" + id;
  115. }
  116. return "Artist-" + item.Name;
  117. }
  118. protected override bool GetBlockUnratedValue(UserPolicy config)
  119. {
  120. return config.BlockUnratedItems.Contains(UnratedItem.Music);
  121. }
  122. public async Task RefreshAllMetadata(MetadataRefreshOptions refreshOptions, IProgress<double> progress, CancellationToken cancellationToken)
  123. {
  124. var items = GetRecursiveChildren().ToList();
  125. var songs = items.OfType<Audio>().ToList();
  126. var others = items.Except(songs).ToList();
  127. var totalItems = songs.Count + others.Count;
  128. var numComplete = 0;
  129. var childUpdateType = ItemUpdateType.None;
  130. // Refresh songs
  131. foreach (var item in songs)
  132. {
  133. cancellationToken.ThrowIfCancellationRequested();
  134. var updateType = await item.RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false);
  135. childUpdateType = childUpdateType | updateType;
  136. numComplete++;
  137. double percent = numComplete;
  138. percent /= totalItems;
  139. progress.Report(percent * 100);
  140. }
  141. var parentRefreshOptions = refreshOptions;
  142. if (childUpdateType > ItemUpdateType.None)
  143. {
  144. parentRefreshOptions = new MetadataRefreshOptions(refreshOptions);
  145. parentRefreshOptions.MetadataRefreshMode = MetadataRefreshMode.FullRefresh;
  146. }
  147. // Refresh current item
  148. await RefreshMetadata(parentRefreshOptions, cancellationToken).ConfigureAwait(false);
  149. // Refresh all non-songs
  150. foreach (var item in others)
  151. {
  152. cancellationToken.ThrowIfCancellationRequested();
  153. var updateType = await item.RefreshMetadata(parentRefreshOptions, cancellationToken).ConfigureAwait(false);
  154. numComplete++;
  155. double percent = numComplete;
  156. percent /= totalItems;
  157. progress.Report(percent * 100);
  158. }
  159. progress.Report(100);
  160. }
  161. public ArtistInfo GetLookupInfo()
  162. {
  163. var info = GetItemLookupInfo<ArtistInfo>();
  164. info.SongInfos = GetRecursiveChildren(i => i is Audio)
  165. .Cast<Audio>()
  166. .Select(i => i.GetLookupInfo())
  167. .ToList();
  168. return info;
  169. }
  170. public IEnumerable<BaseItem> GetTaggedItems(IEnumerable<BaseItem> inputItems)
  171. {
  172. return inputItems.Where(GetItemFilter());
  173. }
  174. public Func<BaseItem, bool> GetItemFilter()
  175. {
  176. return i =>
  177. {
  178. var hasArtist = i as IHasArtist;
  179. return hasArtist != null && hasArtist.HasAnyArtist(Name);
  180. };
  181. }
  182. [IgnoreDataMember]
  183. public override bool SupportsPeople
  184. {
  185. get
  186. {
  187. return false;
  188. }
  189. }
  190. }
  191. }