Person.cs 6.4 KB

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