SongBrowserSettings.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Xml.Serialization;
  5. namespace SongBrowserPlugin.DataAccess
  6. {
  7. [Serializable]
  8. public enum SongSortMode
  9. {
  10. Default,
  11. Author,
  12. Favorites,
  13. Original,
  14. Newest,
  15. PlayCount,
  16. Random,
  17. Search
  18. }
  19. [Serializable]
  20. public class SongBrowserSettings
  21. {
  22. public SongSortMode sortMode = default(SongSortMode);
  23. public List<String> favorites = default(List<String>);
  24. public List<String> searchTerms = default(List<String>);
  25. [NonSerialized]
  26. private static Logger Log = new Logger("SongBrowserSettings");
  27. /// <summary>
  28. /// Constructor.
  29. /// </summary>
  30. public SongBrowserSettings()
  31. {
  32. favorites = new List<String>();
  33. searchTerms = new List<string>();
  34. }
  35. /// <summary>
  36. /// Helper to acquire settings path at runtime.
  37. /// </summary>
  38. /// <returns></returns>
  39. public static String SettingsPath()
  40. {
  41. return Path.Combine(Environment.CurrentDirectory, "song_browser_settings.xml");
  42. }
  43. /// <summary>
  44. /// Load the settings file for this plugin.
  45. /// If we fail to load return Default settings.
  46. /// </summary>
  47. /// <returns>SongBrowserSettings</returns>
  48. public static SongBrowserSettings Load()
  49. {
  50. Log.Trace("Load()");
  51. SongBrowserSettings retVal = null;
  52. String settingsFilePath = SongBrowserSettings.SettingsPath();
  53. if (!File.Exists(settingsFilePath))
  54. {
  55. Log.Debug("Settings file does not exist, returning defaults: " + settingsFilePath);
  56. return new SongBrowserSettings();
  57. }
  58. // Deserialization from JSON
  59. FileStream fs = null;
  60. try
  61. {
  62. fs = File.OpenRead(settingsFilePath);
  63. XmlSerializer serializer = new XmlSerializer(typeof(SongBrowserSettings));
  64. retVal = (SongBrowserSettings)serializer.Deserialize(fs);
  65. }
  66. catch (Exception e)
  67. {
  68. Log.Exception("Unable to deserialize song browser settings file: ", e);
  69. // Return default settings
  70. retVal = new SongBrowserSettings();
  71. }
  72. finally
  73. {
  74. if (fs != null) { fs.Close(); }
  75. }
  76. return retVal;
  77. }
  78. /// <summary>
  79. /// Save this Settings insance to file.
  80. /// </summary>
  81. public void Save()
  82. {
  83. String settingsFilePath = SongBrowserSettings.SettingsPath();
  84. // TODO - not here
  85. if (searchTerms.Count > 10)
  86. {
  87. searchTerms.RemoveRange(10, searchTerms.Count - 10);
  88. }
  89. FileStream fs = new FileStream(settingsFilePath, FileMode.Create, FileAccess.Write);
  90. XmlSerializer serializer = new XmlSerializer(typeof(SongBrowserSettings));
  91. serializer.Serialize(fs, this);
  92. fs.Close();
  93. }
  94. }
  95. }