Person.cs 6.9 KB

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