-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathGlobalSettings.cs
151 lines (131 loc) · 4.39 KB
/
GlobalSettings.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using EasyConnect.Protocols;
using System.Threading.Tasks;
#if APPX
using Windows.Storage;
#endif
namespace EasyConnect
{
/// <summary>
/// Global settings for the application, currently just includes the flag indicating whether the toolbar should be automatically hidden in
/// <see cref="ConnectionWindow"/> instances.
/// </summary>
[Serializable]
[XmlRoot(ElementName = "Options")]
public class GlobalSettings
{
private static readonly string SettingsFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "EasyConnect", "Options.xml");
private GlobalSettings()
{
}
/// <summary>
/// Flag indicating whether the toolbar in <see cref="ConnectionWindow"/> instances should be hidden when the user is focused on the connection content
/// area.
/// </summary>
public bool AutoHideToolbar
{
get;
set;
}
/// <summary>
/// Type of encryption that should be used to protect passwords and other sensitive data in settings files.
/// </summary>
public EncryptionType? EncryptionType
{
get;
set;
}
public bool EnableAeroPeek
{
get;
set;
}
public static GlobalSettings Instance
{
get;
private set;
}
[XmlIgnore]
public bool FirstLaunch
{
get;
private set;
}
/// <summary>
/// Deserializes an instance of this class from an XML file on disk.
/// </summary>
/// <returns></returns>
public static async Task Init()
{
Instance = new GlobalSettings();
#if APPX
IStorageFile optionsFile = (IStorageFile) await ApplicationData.Current.LocalFolder.TryGetItemAsync("Options.xml");
string optionsFileText = null;
// If there isn't a file in the Windows Store app data directory, try the desktop app directory
if (optionsFile == null)
{
try
{
if (File.Exists(SettingsFileName))
{
optionsFileText = File.ReadAllText(SettingsFileName);
}
}
#pragma warning disable RECS0022
catch (Exception)
#pragma warning restore RECS0022
{
}
}
else
{
optionsFileText = await FileIO.ReadTextAsync(optionsFile);
}
if (String.IsNullOrEmpty(optionsFileText))
{
Instance.FirstLaunch = true;
return;
}
using (StringReader optionsFileTextReader = new StringReader(optionsFileText))
using (XmlReader optionsXmlReader = new XmlTextReader(optionsFileTextReader))
{
XmlSerializer serializer = new XmlSerializer(typeof(GlobalSettings));
Instance = (GlobalSettings)serializer.Deserialize(optionsXmlReader);
}
#else
// If the settings file doesn't exist yet (first time the application is being run), just create a new instance of the class
if (!File.Exists(SettingsFileName))
{
Instance = new GlobalSettings();
Instance.FirstLaunch = true;
return;
}
using (FileStream fileStream = new FileStream(SettingsFileName, FileMode.Open, FileAccess.Read))
using (XmlReader reader = new XmlTextReader(fileStream))
{
XmlSerializer serializer = new XmlSerializer(typeof (GlobalSettings));
Instance = (GlobalSettings) serializer.Deserialize(reader);
}
#endif
}
/// <summary>
/// Serializes the option data to disk in an XML file.
/// </summary>
public async Task Save()
{
XmlSerializer serializer = new XmlSerializer(GetType());
#if APPX
IStorageFile optionsFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("Options.xml", CreationCollisionOption.ReplaceExisting);
StringWriter optionsFileText = new StringWriter();
serializer.Serialize(optionsFileText, this);
await FileIO.WriteTextAsync(optionsFile, optionsFileText.ToString());
#else
using (FileStream fileStream = new FileStream(SettingsFileName, FileMode.Create, FileAccess.Write))
serializer.Serialize(fileStream, this);
#endif
}
}
}