Year.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Runtime.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. public override bool CanDelete()
  33. {
  34. return false;
  35. }
  36. /// <summary>
  37. /// Gets a value indicating whether this instance is owned item.
  38. /// </summary>
  39. /// <value><c>true</c> if this instance is owned item; otherwise, <c>false</c>.</value>
  40. [IgnoreDataMember]
  41. public override bool IsOwnedItem
  42. {
  43. get
  44. {
  45. return false;
  46. }
  47. }
  48. public override bool IsSaveLocalMetadataEnabled()
  49. {
  50. return true;
  51. }
  52. public IEnumerable<BaseItem> GetTaggedItems(IEnumerable<BaseItem> inputItems)
  53. {
  54. int year;
  55. var usCulture = new CultureInfo("en-US");
  56. if (!int.TryParse(Name, NumberStyles.Integer, usCulture, out year))
  57. {
  58. return inputItems;
  59. }
  60. return inputItems.Where(i => i.ProductionYear.HasValue && i.ProductionYear.Value == year);
  61. }
  62. public IEnumerable<BaseItem> GetTaggedItems(InternalItemsQuery query)
  63. {
  64. int year;
  65. var usCulture = new CultureInfo("en-US");
  66. if (!int.TryParse(Name, NumberStyles.Integer, usCulture, out year))
  67. {
  68. return new List<BaseItem>();
  69. }
  70. query.Years = new[] { year };
  71. return LibraryManager.GetItemList(query);
  72. }
  73. public int? GetYearValue()
  74. {
  75. int i;
  76. if (int.TryParse(Name, NumberStyles.Integer, CultureInfo.InvariantCulture, out i))
  77. {
  78. return i;
  79. }
  80. return null;
  81. }
  82. public Func<BaseItem, bool> GetItemFilter()
  83. {
  84. var val = GetYearValue();
  85. return i => i.ProductionYear.HasValue && val.HasValue && i.ProductionYear.Value == val.Value;
  86. }
  87. [IgnoreDataMember]
  88. public override bool SupportsPeople
  89. {
  90. get
  91. {
  92. return false;
  93. }
  94. }
  95. public static string GetPath(string name, bool normalizeName = true)
  96. {
  97. // Trim the period at the end because windows will have a hard time with that
  98. var validName = normalizeName ?
  99. FileSystem.GetValidFilename(name).Trim().TrimEnd('.') :
  100. name;
  101. return System.IO.Path.Combine(ConfigurationManager.ApplicationPaths.YearPath, validName);
  102. }
  103. private string GetRebasedPath()
  104. {
  105. return GetPath(System.IO.Path.GetFileName(Path), false);
  106. }
  107. public override bool RequiresRefresh()
  108. {
  109. var newPath = GetRebasedPath();
  110. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  111. {
  112. Logger.Debug("{0} path has changed from {1} to {2}", GetType().Name, Path, newPath);
  113. return true;
  114. }
  115. return base.RequiresRefresh();
  116. }
  117. /// <summary>
  118. /// This is called before any metadata refresh and returns true or false indicating if changes were made
  119. /// </summary>
  120. public override bool BeforeMetadataRefresh()
  121. {
  122. var hasChanges = base.BeforeMetadataRefresh();
  123. var newPath = GetRebasedPath();
  124. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  125. {
  126. Path = newPath;
  127. hasChanges = true;
  128. }
  129. return hasChanges;
  130. }
  131. }
  132. }