Person.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using MediaBrowser.Controller.Providers;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using MediaBrowser.Common.Extensions;
  6. using MediaBrowser.Controller.Extensions;
  7. using MediaBrowser.Model.Entities;
  8. using MediaBrowser.Model.Extensions;
  9. using MediaBrowser.Model.Serialization;
  10. namespace MediaBrowser.Controller.Entities
  11. {
  12. /// <summary>
  13. /// This is the full Person object that can be retrieved with all of it's data.
  14. /// </summary>
  15. public class Person : BaseItem, IItemByName, IHasLookupInfo<PersonLookupInfo>
  16. {
  17. public override List<string> GetUserDataKeys()
  18. {
  19. var list = base.GetUserDataKeys();
  20. list.Insert(0, GetType().Name + "-" + (Name ?? string.Empty).RemoveDiacritics());
  21. return list;
  22. }
  23. public override string CreatePresentationUniqueKey()
  24. {
  25. return GetUserDataKeys()[0];
  26. }
  27. public PersonLookupInfo GetLookupInfo()
  28. {
  29. return GetItemLookupInfo<PersonLookupInfo>();
  30. }
  31. public override double? GetDefaultPrimaryImageAspectRatio()
  32. {
  33. double value = 2;
  34. value /= 3;
  35. return value;
  36. }
  37. public IEnumerable<BaseItem> GetTaggedItems(InternalItemsQuery query)
  38. {
  39. query.PersonIds = new[] { Id.ToString("N") };
  40. return LibraryManager.GetItemList(query);
  41. }
  42. /// <summary>
  43. /// Returns the folder containing the item.
  44. /// If the item is a folder, it returns the folder itself
  45. /// </summary>
  46. /// <value>The containing folder path.</value>
  47. [IgnoreDataMember]
  48. public override string ContainingFolderPath
  49. {
  50. get
  51. {
  52. return Path;
  53. }
  54. }
  55. public override bool CanDelete()
  56. {
  57. return false;
  58. }
  59. public override bool IsSaveLocalMetadataEnabled()
  60. {
  61. return true;
  62. }
  63. [IgnoreDataMember]
  64. public override bool EnableAlphaNumericSorting
  65. {
  66. get
  67. {
  68. return false;
  69. }
  70. }
  71. /// <summary>
  72. /// Gets a value indicating whether this instance is owned item.
  73. /// </summary>
  74. /// <value><c>true</c> if this instance is owned item; otherwise, <c>false</c>.</value>
  75. [IgnoreDataMember]
  76. public override bool IsOwnedItem
  77. {
  78. get
  79. {
  80. return false;
  81. }
  82. }
  83. public IEnumerable<BaseItem> GetTaggedItems(IEnumerable<BaseItem> inputItems)
  84. {
  85. var itemsWithPerson = LibraryManager.GetItemIds(new InternalItemsQuery
  86. {
  87. PersonIds = new[] { Id.ToString("N") }
  88. });
  89. return inputItems.Where(i => itemsWithPerson.Contains(i.Id));
  90. }
  91. public Func<BaseItem, bool> GetItemFilter()
  92. {
  93. return i => LibraryManager.GetPeople(i).Any(p => string.Equals(p.Name, Name, StringComparison.OrdinalIgnoreCase));
  94. }
  95. [IgnoreDataMember]
  96. public override bool SupportsPeople
  97. {
  98. get
  99. {
  100. return false;
  101. }
  102. }
  103. [IgnoreDataMember]
  104. public override bool SupportsAncestors
  105. {
  106. get
  107. {
  108. return false;
  109. }
  110. }
  111. public static string GetPath(string name)
  112. {
  113. return GetPath(name, true);
  114. }
  115. public static string GetPath(string name, bool normalizeName)
  116. {
  117. // Trim the period at the end because windows will have a hard time with that
  118. var validFilename = normalizeName ?
  119. FileSystem.GetValidFilename(name).Trim().TrimEnd('.') :
  120. name;
  121. string subFolderPrefix = null;
  122. foreach (char c in validFilename)
  123. {
  124. if (char.IsLetterOrDigit(c))
  125. {
  126. subFolderPrefix = c.ToString();
  127. break;
  128. }
  129. }
  130. var path = ConfigurationManager.ApplicationPaths.PeoplePath;
  131. return string.IsNullOrEmpty(subFolderPrefix) ?
  132. System.IO.Path.Combine(path, validFilename) :
  133. System.IO.Path.Combine(path, subFolderPrefix, validFilename);
  134. }
  135. private string GetRebasedPath()
  136. {
  137. return GetPath(System.IO.Path.GetFileName(Path), false);
  138. }
  139. public override bool RequiresRefresh()
  140. {
  141. var newPath = GetRebasedPath();
  142. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  143. {
  144. Logger.Debug("{0} path has changed from {1} to {2}", GetType().Name, Path, newPath);
  145. return true;
  146. }
  147. return base.RequiresRefresh();
  148. }
  149. /// <summary>
  150. /// This is called before any metadata refresh and returns true or false indicating if changes were made
  151. /// </summary>
  152. public override bool BeforeMetadataRefresh()
  153. {
  154. var hasChanges = base.BeforeMetadataRefresh();
  155. var newPath = GetRebasedPath();
  156. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  157. {
  158. Path = newPath;
  159. hasChanges = true;
  160. }
  161. return hasChanges;
  162. }
  163. }
  164. /// <summary>
  165. /// This is the small Person stub that is attached to BaseItems
  166. /// </summary>
  167. public class PersonInfo : IHasProviderIds
  168. {
  169. public PersonInfo()
  170. {
  171. ProviderIds = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  172. }
  173. public Guid ItemId { get; set; }
  174. /// <summary>
  175. /// Gets or sets the name.
  176. /// </summary>
  177. /// <value>The name.</value>
  178. public string Name { get; set; }
  179. /// <summary>
  180. /// Gets or sets the role.
  181. /// </summary>
  182. /// <value>The role.</value>
  183. public string Role { get; set; }
  184. /// <summary>
  185. /// Gets or sets the type.
  186. /// </summary>
  187. /// <value>The type.</value>
  188. public string Type { get; set; }
  189. /// <summary>
  190. /// Gets or sets the sort order - ascending
  191. /// </summary>
  192. /// <value>The sort order.</value>
  193. public int? SortOrder { get; set; }
  194. public string ImageUrl { get; set; }
  195. public Dictionary<string, string> ProviderIds { get; set; }
  196. /// <summary>
  197. /// Returns a <see cref="System.String" /> that represents this instance.
  198. /// </summary>
  199. /// <returns>A <see cref="System.String" /> that represents this instance.</returns>
  200. public override string ToString()
  201. {
  202. return Name;
  203. }
  204. public bool IsType(string type)
  205. {
  206. return string.Equals(Type, type, StringComparison.OrdinalIgnoreCase) || string.Equals(Role, type, StringComparison.OrdinalIgnoreCase);
  207. }
  208. }
  209. }