2
0

Year.cs 3.9 KB

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