GameGenre.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MediaBrowser.Model.Serialization;
  5. using MediaBrowser.Common.Extensions;
  6. using MediaBrowser.Controller.Extensions;
  7. using MediaBrowser.Model.Extensions;
  8. namespace MediaBrowser.Controller.Entities
  9. {
  10. public class GameGenre : BaseItem, IItemByName
  11. {
  12. public override List<string> GetUserDataKeys()
  13. {
  14. var list = base.GetUserDataKeys();
  15. list.Insert(0, GetType().Name + "-" + (Name ?? string.Empty).RemoveDiacritics());
  16. return list;
  17. }
  18. public override string CreatePresentationUniqueKey()
  19. {
  20. return GetUserDataKeys()[0];
  21. }
  22. /// <summary>
  23. /// Returns the folder containing the item.
  24. /// If the item is a folder, it returns the folder itself
  25. /// </summary>
  26. /// <value>The containing folder path.</value>
  27. [IgnoreDataMember]
  28. public override string ContainingFolderPath
  29. {
  30. get
  31. {
  32. return Path;
  33. }
  34. }
  35. [IgnoreDataMember]
  36. public override bool SupportsAncestors
  37. {
  38. get
  39. {
  40. return false;
  41. }
  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 override bool CanDelete()
  60. {
  61. return false;
  62. }
  63. public IEnumerable<BaseItem> GetTaggedItems(IEnumerable<BaseItem> inputItems)
  64. {
  65. return inputItems.Where(GetItemFilter());
  66. }
  67. public Func<BaseItem, bool> GetItemFilter()
  68. {
  69. return i => i is Game && i.Genres.Contains(Name, StringComparer.OrdinalIgnoreCase);
  70. }
  71. public IEnumerable<BaseItem> GetTaggedItems(InternalItemsQuery query)
  72. {
  73. query.Genres = new[] { Name };
  74. query.IncludeItemTypes = new[] { typeof(Game).Name };
  75. return LibraryManager.GetItemList(query);
  76. }
  77. [IgnoreDataMember]
  78. public override bool SupportsPeople
  79. {
  80. get
  81. {
  82. return false;
  83. }
  84. }
  85. public static string GetPath(string name)
  86. {
  87. return GetPath(name, true);
  88. }
  89. public static string GetPath(string name, bool normalizeName)
  90. {
  91. // Trim the period at the end because windows will have a hard time with that
  92. var validName = normalizeName ?
  93. FileSystem.GetValidFilename(name).Trim().TrimEnd('.') :
  94. name;
  95. return System.IO.Path.Combine(ConfigurationManager.ApplicationPaths.GameGenrePath, validName);
  96. }
  97. private string GetRebasedPath()
  98. {
  99. return GetPath(System.IO.Path.GetFileName(Path), false);
  100. }
  101. public override bool RequiresRefresh()
  102. {
  103. var newPath = GetRebasedPath();
  104. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  105. {
  106. Logger.Debug("{0} path has changed from {1} to {2}", GetType().Name, Path, newPath);
  107. return true;
  108. }
  109. return base.RequiresRefresh();
  110. }
  111. /// <summary>
  112. /// This is called before any metadata refresh and returns true or false indicating if changes were made
  113. /// </summary>
  114. public override bool BeforeMetadataRefresh()
  115. {
  116. var hasChanges = base.BeforeMetadataRefresh();
  117. var newPath = GetRebasedPath();
  118. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  119. {
  120. Path = newPath;
  121. hasChanges = true;
  122. }
  123. return hasChanges;
  124. }
  125. }
  126. }