Person.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using MediaBrowser.Controller.Providers;
  2. using System;
  3. using System.Collections.Generic;
  4. using MediaBrowser.Common.Extensions;
  5. using MediaBrowser.Controller.Extensions;
  6. using MediaBrowser.Model.Entities;
  7. using MediaBrowser.Model.Extensions;
  8. using MediaBrowser.Model.Serialization;
  9. namespace MediaBrowser.Controller.Entities
  10. {
  11. /// <summary>
  12. /// This is the full Person object that can be retrieved with all of it's data.
  13. /// </summary>
  14. public class Person : BaseItem, IItemByName, IHasLookupInfo<PersonLookupInfo>
  15. {
  16. public override List<string> GetUserDataKeys()
  17. {
  18. var list = base.GetUserDataKeys();
  19. list.Insert(0, GetType().Name + "-" + (Name ?? string.Empty).RemoveDiacritics());
  20. return list;
  21. }
  22. public override string CreatePresentationUniqueKey()
  23. {
  24. return GetUserDataKeys()[0];
  25. }
  26. public PersonLookupInfo GetLookupInfo()
  27. {
  28. return GetItemLookupInfo<PersonLookupInfo>();
  29. }
  30. public override double? GetDefaultPrimaryImageAspectRatio()
  31. {
  32. double value = 2;
  33. value /= 3;
  34. return value;
  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. [IgnoreDataMember]
  83. public override bool SupportsPeople
  84. {
  85. get
  86. {
  87. return false;
  88. }
  89. }
  90. [IgnoreDataMember]
  91. public override bool SupportsAncestors
  92. {
  93. get
  94. {
  95. return false;
  96. }
  97. }
  98. public static string GetPath(string name)
  99. {
  100. return GetPath(name, true);
  101. }
  102. public static string GetPath(string name, bool normalizeName)
  103. {
  104. // Trim the period at the end because windows will have a hard time with that
  105. var validFilename = normalizeName ?
  106. FileSystem.GetValidFilename(name).Trim().TrimEnd('.') :
  107. name;
  108. string subFolderPrefix = null;
  109. foreach (char c in validFilename)
  110. {
  111. if (char.IsLetterOrDigit(c))
  112. {
  113. subFolderPrefix = c.ToString();
  114. break;
  115. }
  116. }
  117. var path = ConfigurationManager.ApplicationPaths.PeoplePath;
  118. return string.IsNullOrEmpty(subFolderPrefix) ?
  119. System.IO.Path.Combine(path, validFilename) :
  120. System.IO.Path.Combine(path, subFolderPrefix, validFilename);
  121. }
  122. private string GetRebasedPath()
  123. {
  124. return GetPath(System.IO.Path.GetFileName(Path), false);
  125. }
  126. public override bool RequiresRefresh()
  127. {
  128. var newPath = GetRebasedPath();
  129. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  130. {
  131. Logger.Debug("{0} path has changed from {1} to {2}", GetType().Name, Path, newPath);
  132. return true;
  133. }
  134. return base.RequiresRefresh();
  135. }
  136. /// <summary>
  137. /// This is called before any metadata refresh and returns true or false indicating if changes were made
  138. /// </summary>
  139. public override bool BeforeMetadataRefresh()
  140. {
  141. var hasChanges = base.BeforeMetadataRefresh();
  142. var newPath = GetRebasedPath();
  143. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  144. {
  145. Path = newPath;
  146. hasChanges = true;
  147. }
  148. return hasChanges;
  149. }
  150. }
  151. /// <summary>
  152. /// This is the small Person stub that is attached to BaseItems
  153. /// </summary>
  154. public class PersonInfo : IHasProviderIds
  155. {
  156. public PersonInfo()
  157. {
  158. ProviderIds = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  159. }
  160. public Guid ItemId { get; set; }
  161. /// <summary>
  162. /// Gets or sets the name.
  163. /// </summary>
  164. /// <value>The name.</value>
  165. public string Name { get; set; }
  166. /// <summary>
  167. /// Gets or sets the role.
  168. /// </summary>
  169. /// <value>The role.</value>
  170. public string Role { get; set; }
  171. /// <summary>
  172. /// Gets or sets the type.
  173. /// </summary>
  174. /// <value>The type.</value>
  175. public string Type { get; set; }
  176. /// <summary>
  177. /// Gets or sets the sort order - ascending
  178. /// </summary>
  179. /// <value>The sort order.</value>
  180. public int? SortOrder { get; set; }
  181. public string ImageUrl { get; set; }
  182. public Dictionary<string, string> ProviderIds { get; set; }
  183. /// <summary>
  184. /// Returns a <see cref="System.String" /> that represents this instance.
  185. /// </summary>
  186. /// <returns>A <see cref="System.String" /> that represents this instance.</returns>
  187. public override string ToString()
  188. {
  189. return Name;
  190. }
  191. public bool IsType(string type)
  192. {
  193. return string.Equals(Type, type, StringComparison.OrdinalIgnoreCase) || string.Equals(Role, type, StringComparison.OrdinalIgnoreCase);
  194. }
  195. }
  196. }