SongBrowserSettings.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. }
  17. [Serializable]
  18. public class SongBrowserSettings
  19. {
  20. public SongSortMode sortMode = default(SongSortMode);
  21. public List<String> favorites;
  22. [NonSerialized]
  23. private static Logger Log = new Logger("SongBrowserSettings");
  24. /// <summary>
  25. /// Constructor.
  26. /// </summary>
  27. public SongBrowserSettings()
  28. {
  29. favorites = new List<String>();
  30. }
  31. /// <summary>
  32. /// Helper to acquire settings path at runtime.
  33. /// </summary>
  34. /// <returns></returns>
  35. public static String SettingsPath()
  36. {
  37. return Path.Combine(Environment.CurrentDirectory, "song_browser_settings.xml");
  38. }
  39. /// <summary>
  40. /// Load the settings file for this plugin.
  41. /// If we fail to load return Default settings.
  42. /// </summary>
  43. /// <returns>SongBrowserSettings</returns>
  44. public static SongBrowserSettings Load()
  45. {
  46. Log.Trace("Load()");
  47. SongBrowserSettings retVal = null;
  48. String settingsFilePath = SongBrowserSettings.SettingsPath();
  49. if (!File.Exists(settingsFilePath))
  50. {
  51. Log.Debug("Settings file does not exist, returning defaults: " + settingsFilePath);
  52. return new SongBrowserSettings();
  53. }
  54. // Deserialization from JSON
  55. FileStream fs = null;
  56. try
  57. {
  58. fs = File.OpenRead(settingsFilePath);
  59. XmlSerializer serializer = new XmlSerializer(typeof(SongBrowserSettings));
  60. retVal = (SongBrowserSettings)serializer.Deserialize(fs);
  61. }
  62. catch (Exception e)
  63. {
  64. Log.Exception("Unable to deserialize song browser settings file: ", e);
  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. }