Person.cs 6.1 KB

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