Year.cs 3.8 KB

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