MusicArtist.cs 9.7 KB

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