Year.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using MediaBrowser.Model.Serialization;
  6. namespace MediaBrowser.Controller.Entities
  7. {
  8. /// <summary>
  9. /// Class Year
  10. /// </summary>
  11. public class Year : BaseItem, IItemByName
  12. {
  13. public override List<string> GetUserDataKeys()
  14. {
  15. var list = base.GetUserDataKeys();
  16. list.Insert(0, "Year-" + Name);
  17. return list;
  18. }
  19. /// <summary>
  20. /// Returns the folder containing the item.
  21. /// If the item is a folder, it returns the folder itself
  22. /// </summary>
  23. /// <value>The containing folder path.</value>
  24. [IgnoreDataMember]
  25. public override string ContainingFolderPath
  26. {
  27. get
  28. {
  29. return Path;
  30. }
  31. }
  32. [IgnoreDataMember]
  33. public override bool SupportsAncestors
  34. {
  35. get
  36. {
  37. return false;
  38. }
  39. }
  40. public override bool CanDelete()
  41. {
  42. return false;
  43. }
  44. /// <summary>
  45. /// Gets a value indicating whether this instance is owned item.
  46. /// </summary>
  47. /// <value><c>true</c> if this instance is owned item; otherwise, <c>false</c>.</value>
  48. [IgnoreDataMember]
  49. public override bool IsOwnedItem
  50. {
  51. get
  52. {
  53. return false;
  54. }
  55. }
  56. public override bool IsSaveLocalMetadataEnabled()
  57. {
  58. return true;
  59. }
  60. public IEnumerable<BaseItem> GetTaggedItems(IEnumerable<BaseItem> inputItems)
  61. {
  62. int year;
  63. var usCulture = new CultureInfo("en-US");
  64. if (!int.TryParse(Name, NumberStyles.Integer, usCulture, out year))
  65. {
  66. return inputItems;
  67. }
  68. return inputItems.Where(i => i.ProductionYear.HasValue && i.ProductionYear.Value == year);
  69. }
  70. public IEnumerable<BaseItem> GetTaggedItems(InternalItemsQuery query)
  71. {
  72. int year;
  73. var usCulture = new CultureInfo("en-US");
  74. if (!int.TryParse(Name, NumberStyles.Integer, usCulture, out year))
  75. {
  76. return new List<BaseItem>();
  77. }
  78. query.Years = new[] { year };
  79. return LibraryManager.GetItemList(query);
  80. }
  81. public int? GetYearValue()
  82. {
  83. int i;
  84. if (int.TryParse(Name, NumberStyles.Integer, CultureInfo.InvariantCulture, out i))
  85. {
  86. return i;
  87. }
  88. return null;
  89. }
  90. public Func<BaseItem, bool> GetItemFilter()
  91. {
  92. var val = GetYearValue();
  93. return i => i.ProductionYear.HasValue && val.HasValue && i.ProductionYear.Value == val.Value;
  94. }
  95. [IgnoreDataMember]
  96. public override bool SupportsPeople
  97. {
  98. get
  99. {
  100. return false;
  101. }
  102. }
  103. public static string GetPath(string name)
  104. {
  105. return GetPath(name, true);
  106. }
  107. public static string GetPath(string name, bool normalizeName)
  108. {
  109. // Trim the period at the end because windows will have a hard time with that
  110. var validName = normalizeName ?
  111. FileSystem.GetValidFilename(name).Trim().TrimEnd('.') :
  112. name;
  113. return System.IO.Path.Combine(ConfigurationManager.ApplicationPaths.YearPath, validName);
  114. }
  115. private string GetRebasedPath()
  116. {
  117. return GetPath(System.IO.Path.GetFileName(Path), false);
  118. }
  119. public override bool RequiresRefresh()
  120. {
  121. var newPath = GetRebasedPath();
  122. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  123. {
  124. Logger.Debug("{0} path has changed from {1} to {2}", GetType().Name, Path, newPath);
  125. return true;
  126. }
  127. return base.RequiresRefresh();
  128. }
  129. /// <summary>
  130. /// This is called before any metadata refresh and returns true or false indicating if changes were made
  131. /// </summary>
  132. public override bool BeforeMetadataRefresh()
  133. {
  134. var hasChanges = base.BeforeMetadataRefresh();
  135. var newPath = GetRebasedPath();
  136. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  137. {
  138. Path = newPath;
  139. hasChanges = true;
  140. }
  141. return hasChanges;
  142. }
  143. }
  144. }