Person.cs 6.8 KB

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