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
/// </summary>
[Serializable]
sealed class AppConfiguration
public sealed class AppConfiguration
{
// XXX: All the fields need to be checked in overriden Equals method
// Also: sensible defaults should be provided in defaultConfiguration field
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()
{
var cfg = loadSavedConfiguration();
@ -25,20 +32,41 @@ namespace QuickPlay
}
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()
{
// 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
var newConfig = AppConfiguration.loadSavedConfiguration();
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",
};
@ -67,7 +95,7 @@ namespace QuickPlay
///
/// Contains details about connection, configured songs &c.
/// </summary>
class PlayerConfiguration
public class PlayerConfiguration
{
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.
/// </summary>
interface IPlayer
public interface IPlayer
{
void Play(string identifier);
float CurrentProgress { get; }

Loading…
Cancel
Save