MusicArtist.cs 9.6 KB

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