GameGenre.cs 3.7 KB

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