MusicArtist.cs 9.8 KB

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