/
Settings.cs
104 lines (83 loc) · 2.52 KB
/
Settings.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Xml;
namespace MusicBeePlugin
{
[DataContract(Name = "settings")]
public class Settings
{
public string FilePath { get; set; }
[DataMember]
private string requestMethod;
public string RequestMethod
{
get => requestMethod ?? "POST";
set => requestMethod = value;
}
[DataMember]
private string requestUri;
public string RequestUri
{
get => requestUri ?? "";
set => requestUri = value;
}
[DataMember]
private Dictionary<string, string> headers;
public Dictionary<string, string> Headers
{
get => headers ?? new Dictionary<string, string>();
set => headers = value;
}
[DataMember]
private string mediaType;
public string MediaType
{
get => mediaType ?? "application/json";
set => mediaType = value;
}
[DataMember]
private string content;
public string Content
{
get => content ?? "{\r\n \"title\": \"{title}\",\r\n \"artist\": \"{artist}\",\r\n \"album\": \"{album}\"\r\n}";
set => content = value;
}
public static Settings GetInstance(string filePath)
{
Settings newSettings;
try
{
newSettings = Load(filePath);
}
catch
{
newSettings = new Settings();
}
newSettings.FilePath = filePath;
return newSettings;
}
private static Settings Load(string filePath)
{
using (var stream = File.OpenRead(filePath))
{
var serializer = new DataContractSerializer(typeof(Settings));
return serializer.ReadObject(stream) as Settings;
}
}
public void Save()
{
Directory.CreateDirectory(Path.GetDirectoryName(FilePath));
XmlWriterSettings writerSettings = new XmlWriterSettings
{
NewLineHandling = NewLineHandling.Entitize,
};
using (var writer = XmlWriter.Create(FilePath, writerSettings))
{
var serializer = new DataContractSerializer(GetType());
serializer.WriteObject(writer, this);
writer.Flush();
}
}
}
}