Year.cs 3.8 KB

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