MusicArtist.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 override UnratedItem GetBlockUnratedType()
  123. {
  124. return UnratedItem.Music;
  125. }
  126. public async Task RefreshAllMetadata(MetadataRefreshOptions refreshOptions, IProgress<double> progress, CancellationToken cancellationToken)
  127. {
  128. var items = GetRecursiveChildren().ToList();
  129. var songs = items.OfType<Audio>().ToList();
  130. var others = items.Except(songs).ToList();
  131. var totalItems = songs.Count + others.Count;
  132. var numComplete = 0;
  133. var childUpdateType = ItemUpdateType.None;
  134. // Refresh songs
  135. foreach (var item in songs)
  136. {
  137. cancellationToken.ThrowIfCancellationRequested();
  138. var updateType = await item.RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false);
  139. childUpdateType = childUpdateType | updateType;
  140. numComplete++;
  141. double percent = numComplete;
  142. percent /= totalItems;
  143. progress.Report(percent * 100);
  144. }
  145. var parentRefreshOptions = refreshOptions;
  146. if (childUpdateType > ItemUpdateType.None)
  147. {
  148. parentRefreshOptions = new MetadataRefreshOptions(refreshOptions);
  149. parentRefreshOptions.MetadataRefreshMode = MetadataRefreshMode.FullRefresh;
  150. }
  151. // Refresh current item
  152. await RefreshMetadata(parentRefreshOptions, cancellationToken).ConfigureAwait(false);
  153. // Refresh all non-songs
  154. foreach (var item in others)
  155. {
  156. cancellationToken.ThrowIfCancellationRequested();
  157. var updateType = await item.RefreshMetadata(parentRefreshOptions, cancellationToken).ConfigureAwait(false);
  158. numComplete++;
  159. double percent = numComplete;
  160. percent /= totalItems;
  161. progress.Report(percent * 100);
  162. }
  163. progress.Report(100);
  164. }
  165. public ArtistInfo GetLookupInfo()
  166. {
  167. var info = GetItemLookupInfo<ArtistInfo>();
  168. info.SongInfos = GetRecursiveChildren(i => i is Audio)
  169. .Cast<Audio>()
  170. .Select(i => i.GetLookupInfo())
  171. .ToList();
  172. return info;
  173. }
  174. public IEnumerable<BaseItem> GetTaggedItems(IEnumerable<BaseItem> inputItems)
  175. {
  176. return inputItems.Where(GetItemFilter());
  177. }
  178. public Func<BaseItem, bool> GetItemFilter()
  179. {
  180. return i =>
  181. {
  182. var hasArtist = i as IHasArtist;
  183. return hasArtist != null && hasArtist.HasAnyArtist(Name);
  184. };
  185. }
  186. [IgnoreDataMember]
  187. public override bool SupportsPeople
  188. {
  189. get
  190. {
  191. return false;
  192. }
  193. }
  194. }
  195. }