Implement App config saves and restores

develop
LEdoian 3 years ago
parent d479889fd9
commit 33f1c0f91f

@ -8,12 +8,19 @@ namespace QuickPlay
/// Application (global) configuration data class /// Application (global) configuration data class
/// </summary> /// </summary>
[Serializable] [Serializable]
sealed class AppConfiguration public sealed class AppConfiguration
{ {
// XXX: All the fields need to be checked in overriden Equals method // XXX: All the fields need to be checked in overriden Equals method
// Also: sensible defaults should be provided in defaultConfiguration field // Also: sensible defaults should be provided in defaultConfiguration field
public string playerConfigUrl; public string playerConfigUrl;
// XXX: Any other fields of this model need [NonSerialized] attribute
[NonSerialized]
public static readonly string configFilePath =
Path.Combine(
System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData),
"appConfig.xml"
);
public static AppConfiguration loadConfiguration() public static AppConfiguration loadConfiguration()
{ {
var cfg = loadSavedConfiguration(); var cfg = loadSavedConfiguration();
@ -25,20 +32,41 @@ namespace QuickPlay
} }
public static AppConfiguration loadSavedConfiguration() public static AppConfiguration loadSavedConfiguration()
{ {
throw new NotImplementedException(); var serializer = new System.Xml.Serialization.XmlSerializer(typeof(AppConfiguration));
AppConfiguration result = null;
try
{
using (var stream = new System.IO.FileStream(configFilePath, System.IO.FileMode.Open))
{
result = (AppConfiguration)serializer.Deserialize(stream);
}
} catch (FileNotFoundException)
{
// This is fine, we will return null anyway.
} catch (InvalidCastException) {
// This is not so much fine, TODO log it.
// Still, we cannot supply config, so treat it as if it does not exist.
}
return result;
} }
public void saveConfiguration() public void saveConfiguration()
{ {
// First we save the config
var serializer = new System.Xml.Serialization.XmlSerializer(typeof(AppConfiguration));
using (var stream = new System.IO.FileStream(configFilePath, System.IO.FileMode.Create))
{
serializer.Serialize(stream, this);
}
// Make sure that the configuration is same // Make sure that the configuration is same
var newConfig = AppConfiguration.loadSavedConfiguration(); var newConfig = AppConfiguration.loadSavedConfiguration();
if (this != newConfig) throw new InvalidDataException("Saved configuration is different from the supplied one."); if (this != newConfig) throw new InvalidDataException("Saved configuration is different from the supplied one.");
} }
public static AppConfiguration defaultConfiguration = new AppConfiguration [NonSerialized]
public static readonly AppConfiguration defaultConfiguration = new AppConfiguration
{ {
playerConfigUrl = "file:///dev/null", playerConfigUrl = "file:///dev/null",
}; };
@ -67,7 +95,7 @@ namespace QuickPlay
/// ///
/// Contains details about connection, configured songs &c. /// Contains details about connection, configured songs &c.
/// </summary> /// </summary>
class PlayerConfiguration public class PlayerConfiguration
{ {
public static PlayerConfiguration FromFile(StreamReader reader) public static PlayerConfiguration FromFile(StreamReader reader)
{ {

@ -9,7 +9,7 @@ namespace QuickPlay
/// ///
/// That means that the interface may be extended in the future, which is sad. /// That means that the interface may be extended in the future, which is sad.
/// </summary> /// </summary>
interface IPlayer public interface IPlayer
{ {
void Play(string identifier); void Play(string identifier);
float CurrentProgress { get; } float CurrentProgress { get; }

Loading…
Cancel
Save