SongBrowserSettings.cs 3.0 KB

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