Person.cs 7.1 KB

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