MusicArtist.cs 9.9 KB

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