Person.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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)
  117. {
  118. return GetPath(name, true);
  119. }
  120. public static string GetPath(string name, bool normalizeName)
  121. {
  122. // Trim the period at the end because windows will have a hard time with that
  123. var validFilename = normalizeName ?
  124. FileSystem.GetValidFilename(name).Trim().TrimEnd('.') :
  125. name;
  126. string subFolderPrefix = null;
  127. foreach (char c in validFilename)
  128. {
  129. if (char.IsLetterOrDigit(c))
  130. {
  131. subFolderPrefix = c.ToString();
  132. break;
  133. }
  134. }
  135. var path = ConfigurationManager.ApplicationPaths.PeoplePath;
  136. return string.IsNullOrEmpty(subFolderPrefix) ?
  137. System.IO.Path.Combine(path, validFilename) :
  138. System.IO.Path.Combine(path, subFolderPrefix, validFilename);
  139. }
  140. private string GetRebasedPath()
  141. {
  142. return GetPath(System.IO.Path.GetFileName(Path), false);
  143. }
  144. public override bool RequiresRefresh()
  145. {
  146. var newPath = GetRebasedPath();
  147. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  148. {
  149. Logger.Debug("{0} path has changed from {1} to {2}", GetType().Name, Path, newPath);
  150. return true;
  151. }
  152. return base.RequiresRefresh();
  153. }
  154. /// <summary>
  155. /// This is called before any metadata refresh and returns true or false indicating if changes were made
  156. /// </summary>
  157. public override bool BeforeMetadataRefresh()
  158. {
  159. var hasChanges = base.BeforeMetadataRefresh();
  160. var newPath = GetRebasedPath();
  161. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  162. {
  163. Path = newPath;
  164. hasChanges = true;
  165. }
  166. return hasChanges;
  167. }
  168. }
  169. /// <summary>
  170. /// This is the small Person stub that is attached to BaseItems
  171. /// </summary>
  172. public class PersonInfo : IHasProviderIds
  173. {
  174. public PersonInfo()
  175. {
  176. ProviderIds = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  177. }
  178. public Guid ItemId { get; set; }
  179. /// <summary>
  180. /// Gets or sets the name.
  181. /// </summary>
  182. /// <value>The name.</value>
  183. public string Name { get; set; }
  184. /// <summary>
  185. /// Gets or sets the role.
  186. /// </summary>
  187. /// <value>The role.</value>
  188. public string Role { get; set; }
  189. /// <summary>
  190. /// Gets or sets the type.
  191. /// </summary>
  192. /// <value>The type.</value>
  193. public string Type { get; set; }
  194. /// <summary>
  195. /// Gets or sets the sort order - ascending
  196. /// </summary>
  197. /// <value>The sort order.</value>
  198. public int? SortOrder { get; set; }
  199. public string ImageUrl { get; set; }
  200. public Dictionary<string, string> ProviderIds { get; set; }
  201. /// <summary>
  202. /// Returns a <see cref="System.String" /> that represents this instance.
  203. /// </summary>
  204. /// <returns>A <see cref="System.String" /> that represents this instance.</returns>
  205. public override string ToString()
  206. {
  207. return Name;
  208. }
  209. public bool IsType(string type)
  210. {
  211. return string.Equals(Type, type, StringComparison.OrdinalIgnoreCase) || string.Equals(Role, type, StringComparison.OrdinalIgnoreCase);
  212. }
  213. }
  214. }