SongBrowserSettings.cs 3.0 KB

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