Winify – Diff between revs 49 and 55
?pathlinks?
Rev 49 | Rev 55 | |||
---|---|---|---|---|
1 | using System; |
1 | using System; |
|
2 | using System.Collections.Concurrent; |
2 | using System.Collections.Concurrent; |
|
3 | using System.ComponentModel; |
3 | using System.ComponentModel; |
|
4 | using System.Drawing; |
4 | using System.Drawing; |
|
5 | using System.IO; |
5 | using System.IO; |
|
6 | using System.Net; |
6 | using System.Net; |
|
7 | using System.Reflection; |
7 | using System.Reflection; |
|
8 | using System.Text; |
8 | using System.Text; |
|
9 | using System.Threading; |
9 | using System.Threading; |
|
10 | using System.Threading.Tasks; |
10 | using System.Threading.Tasks; |
|
11 | using System.Windows.Forms; |
11 | using System.Windows.Forms; |
|
12 | using NetSparkleUpdater; |
12 | using NetSparkleUpdater; |
|
13 | using NetSparkleUpdater.Enums; |
13 | using NetSparkleUpdater.Enums; |
|
14 | using NetSparkleUpdater.SignatureVerifiers; |
14 | using NetSparkleUpdater.SignatureVerifiers; |
|
15 | using NetSparkleUpdater.UI.WinForms; |
15 | using NetSparkleUpdater.UI.WinForms; |
|
16 | using Serilog; |
16 | using Serilog; |
|
17 | using Servers; |
17 | using Servers; |
|
18 | using Toasts; |
18 | using Toasts; |
|
19 | using Winify.Gotify; |
19 | using Winify.Gotify; |
|
20 | using Winify.Settings; |
20 | using Winify.Settings; |
|
21 | using Winify.Utilities; |
21 | using Winify.Utilities; |
|
22 | using Winify.Utilities.Serialization; |
22 | using Winify.Utilities.Serialization; |
|
23 | |
23 | |
|
24 | namespace Winify |
24 | namespace Winify |
|
25 | { |
25 | { |
|
26 | public partial class MainForm : Form |
26 | public partial class MainForm : Form |
|
27 | { |
27 | { |
|
28 | private void LogViewToolStripMenuItem_Click(object sender, EventArgs e) |
28 | private void LogViewToolStripMenuItem_Click(object sender, EventArgs e) |
|
29 | { |
29 | { |
|
30 | if (_logViewForm != null) return; |
30 | if (_logViewForm != null) return; |
|
31 | |
31 | |
|
32 | _logViewForm = new LogViewForm(this, _memorySink, _cancellationToken); |
32 | _logViewForm = new LogViewForm(this, _memorySink, _cancellationToken); |
|
33 | _logViewForm.Closing += LogViewForm_Closing; |
33 | _logViewForm.Closing += LogViewForm_Closing; |
|
34 | _logViewForm.Show(); |
34 | _logViewForm.Show(); |
|
35 | } |
35 | } |
|
36 | |
36 | |
|
37 | private void LogViewForm_Closing(object sender, CancelEventArgs e) |
37 | private void LogViewForm_Closing(object sender, CancelEventArgs e) |
|
38 | { |
38 | { |
|
39 | if (_logViewForm == null) return; |
39 | if (_logViewForm == null) return; |
|
40 | |
40 | |
|
41 | _logViewForm.Closing -= LogViewForm_Closing; |
41 | _logViewForm.Closing -= LogViewForm_Closing; |
|
42 | _logViewForm.Close(); |
42 | _logViewForm.Close(); |
|
43 | _logViewForm = null; |
43 | _logViewForm = null; |
|
44 | } |
44 | } |
|
45 | |
45 | |
|
46 | #region Public Enums, Properties and Fields |
46 | #region Public Enums, Properties and Fields |
|
47 | |
47 | |
|
48 | public Configuration.Configuration Configuration { get; set; } |
48 | public Configuration.Configuration Configuration { get; set; } |
|
49 | |
49 | |
|
50 | public ScheduledContinuation ChangedConfigurationContinuation { get; set; } |
50 | public ScheduledContinuation ChangedConfigurationContinuation { get; set; } |
|
51 | |
51 | |
|
52 | public bool MemorySinkEnabled { get; set; } |
52 | public bool MemorySinkEnabled { get; set; } |
|
53 | |
53 | |
|
54 | #endregion |
54 | #endregion |
|
55 | |
55 | |
|
56 | #region Private Delegates, Events, Enums, Properties, Indexers and Fields |
56 | #region Private Delegates, Events, Enums, Properties, Indexers and Fields |
|
57 | |
57 | |
|
58 | private AboutForm _aboutForm; |
58 | private AboutForm _aboutForm; |
|
59 | |
59 | |
|
60 | private ConcurrentBag<GotifyConnection> _gotifyConnections; |
60 | private ConcurrentBag<GotifyConnection> _gotifyConnections; |
|
61 | |
61 | |
|
62 | private SettingsForm _settingsForm; |
62 | private SettingsForm _settingsForm; |
|
63 | |
63 | |
|
64 | private readonly SparkleUpdater _sparkle; |
64 | private readonly SparkleUpdater _sparkle; |
|
65 | |
65 | |
|
66 | private readonly CancellationTokenSource _cancellationTokenSource; |
66 | private readonly CancellationTokenSource _cancellationTokenSource; |
|
67 | |
67 | |
|
68 | private readonly CancellationToken _cancellationToken; |
68 | private readonly CancellationToken _cancellationToken; |
|
69 | |
69 | |
|
70 | private LogViewForm _logViewForm; |
70 | private LogViewForm _logViewForm; |
|
71 | |
71 | |
|
72 | private readonly LogMemorySink _memorySink; |
72 | private readonly LogMemorySink _memorySink; |
|
73 | |
73 | |
|
74 | #endregion |
74 | #endregion |
|
75 | |
75 | |
|
76 | #region Constructors, Destructors and Finalizers |
76 | #region Constructors, Destructors and Finalizers |
|
77 | |
77 | |
|
78 | public MainForm() |
78 | public MainForm() |
|
79 | { |
79 | { |
|
80 | _cancellationTokenSource = new CancellationTokenSource(); |
80 | _cancellationTokenSource = new CancellationTokenSource(); |
|
81 | _cancellationToken = _cancellationTokenSource.Token; |
81 | _cancellationToken = _cancellationTokenSource.Token; |
|
82 | |
82 | |
|
83 | ChangedConfigurationContinuation = new ScheduledContinuation(); |
83 | ChangedConfigurationContinuation = new ScheduledContinuation(); |
|
84 | } |
84 | } |
|
85 | |
85 | |
|
86 | public MainForm(Mutex mutex) : this() |
86 | public MainForm(Mutex mutex) : this() |
|
87 | { |
87 | { |
|
88 | InitializeComponent(); |
88 | InitializeComponent(); |
|
89 | |
89 | |
|
90 | _memorySink = new LogMemorySink(); |
90 | _memorySink = new LogMemorySink(); |
|
91 | Log.Logger = new LoggerConfiguration() |
91 | Log.Logger = new LoggerConfiguration() |
|
92 | .MinimumLevel.Debug() |
92 | .MinimumLevel.Debug() |
|
93 | .WriteTo.Conditional(condition => MemorySinkEnabled, configureSink => configureSink.Sink(_memorySink)) |
93 | .WriteTo.Conditional(condition => MemorySinkEnabled, configureSink => configureSink.Sink(_memorySink)) |
|
94 | .WriteTo.File(Path.Combine(Constants.UserApplicationDirectory, "Logs", $"{Constants.AssemblyName}.log"), |
94 | .WriteTo.File(Path.Combine(Constants.UserApplicationDirectory, "Logs", $"{Constants.AssemblyName}.log"), |
|
95 | rollingInterval: RollingInterval.Day) |
95 | rollingInterval: RollingInterval.Day) |
|
96 | .CreateLogger(); |
96 | .CreateLogger(); |
|
97 | |
97 | |
|
98 | // Start application update. |
98 | // Start application update. |
|
99 | var manifestModuleName = Assembly.GetEntryAssembly().ManifestModule.FullyQualifiedName; |
99 | var manifestModuleName = Assembly.GetEntryAssembly().ManifestModule.FullyQualifiedName; |
|
100 | var icon = Icon.ExtractAssociatedIcon(manifestModuleName); |
100 | var icon = Icon.ExtractAssociatedIcon(manifestModuleName); |
|
101 | |
101 | |
|
102 | _sparkle = new SparkleUpdater("https://winify.grimore.org/update/appcast.xml", |
102 | _sparkle = new SparkleUpdater("https://winify.grimore.org/update/appcast.xml", |
|
103 | new Ed25519Checker(SecurityMode.Strict, "LonrgxVjSF0GnY4hzwlRJnLkaxnDn2ikdmOifILzLJY=")) |
103 | new Ed25519Checker(SecurityMode.Strict, "LonrgxVjSF0GnY4hzwlRJnLkaxnDn2ikdmOifILzLJY=")) |
|
104 | { |
104 | { |
|
105 | UIFactory = new UIFactory(icon), |
105 | UIFactory = new UIFactory(icon), |
|
106 | RelaunchAfterUpdate = true, |
106 | RelaunchAfterUpdate = true, |
|
107 | SecurityProtocolType = SecurityProtocolType.Tls12 |
107 | SecurityProtocolType = SecurityProtocolType.Tls12 |
|
108 | }; |
108 | }; |
|
109 | _sparkle.StartLoop(true, true); |
109 | _sparkle.StartLoop(true, true); |
|
110 | } |
110 | } |
|
111 | |
111 | |
|
112 | /// <summary> |
112 | /// <summary> |
|
113 | /// Clean up any resources being used. |
113 | /// Clean up any resources being used. |
|
114 | /// </summary> |
114 | /// </summary> |
|
115 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> |
115 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> |
|
116 | protected override void Dispose(bool disposing) |
116 | protected override void Dispose(bool disposing) |
|
117 | { |
117 | { |
|
118 | if (disposing && components != null) components.Dispose(); |
118 | if (disposing && components != null) components.Dispose(); |
|
119 | |
119 | |
|
120 | base.Dispose(disposing); |
120 | base.Dispose(disposing); |
|
121 | } |
121 | } |
|
122 | |
122 | |
|
123 | #endregion |
123 | #endregion |
|
124 | |
124 | |
|
125 | #region Event Handlers |
125 | #region Event Handlers |
|
126 | |
126 | |
|
127 | private async void MainForm_Load(object sender, EventArgs e) |
127 | private async void MainForm_Load(object sender, EventArgs e) |
|
128 | { |
128 | { |
|
129 | Configuration = await LoadConfiguration(); |
129 | Configuration = await LoadConfiguration(); |
|
130 | |
130 | |
|
131 | var servers = await LoadServers(); |
131 | var servers = await LoadServers(); |
|
132 | _gotifyConnections = new ConcurrentBag<GotifyConnection>(); |
132 | _gotifyConnections = new ConcurrentBag<GotifyConnection>(); |
|
133 | foreach (var server in servers.Server) |
133 | foreach (var server in servers.Server) |
|
134 | { |
134 | { |
|
135 | var gotifyConnection = new GotifyConnection(server, Configuration); |
135 | var gotifyConnection = new GotifyConnection(server, Configuration); |
|
136 | gotifyConnection.GotifyNotification += GotifyConnection_GotifyNotification; |
136 | gotifyConnection.GotifyNotification += GotifyConnection_GotifyNotification; |
|
137 | gotifyConnection.Start(); |
137 | gotifyConnection.Start(); |
|
138 | _gotifyConnections.Add(gotifyConnection); |
138 | _gotifyConnections.Add(gotifyConnection); |
|
139 | } |
139 | } |
|
140 | } |
140 | } |
|
141 | |
141 | |
|
142 | private async void SettingsToolStripMenuItem_Click(object sender, EventArgs e) |
142 | private async void SettingsToolStripMenuItem_Click(object sender, EventArgs e) |
|
143 | { |
143 | { |
|
144 | if (_settingsForm == null) |
144 | if (_settingsForm == null) |
|
145 | { |
145 | { |
|
146 | var servers = await LoadServers(); |
146 | var servers = await LoadServers(); |
|
147 | var announcements = await LoadAnnouncements(); |
147 | var announcements = await LoadAnnouncements(); |
|
148 | |
148 | |
|
149 | _settingsForm = new SettingsForm(this, servers, announcements, _cancellationToken); |
149 | _settingsForm = new SettingsForm(this, servers, announcements, _cancellationToken); |
|
150 | _settingsForm.Save += SettingsForm_Save; |
150 | _settingsForm.Save += SettingsForm_Save; |
|
151 | _settingsForm.Closing += SettingsForm_Closing; |
151 | _settingsForm.Closing += SettingsForm_Closing; |
|
152 | _settingsForm.Show(); |
152 | _settingsForm.Show(); |
|
153 | } |
153 | } |
|
154 | } |
154 | } |
|
155 | |
155 | |
|
156 | private async void SettingsForm_Save(object sender, SettingsSavedEventArgs e) |
156 | private async void SettingsForm_Save(object sender, SettingsSavedEventArgs e) |
|
157 | { |
157 | { |
|
- | 158 | // Save the configuration. |
||
- | 159 | Miscellaneous.LaunchOnBootSet(Configuration.LaunchOnBoot); |
||
- | 160 | ChangedConfigurationContinuation.Schedule(TimeSpan.FromSeconds(1), |
||
- | 161 | async () => { await SaveConfiguration(); }, _cancellationToken); |
||
- | 162 | |
||
158 | // Save the servers. |
163 | // Save the servers. |
|
159 | await Task.WhenAll(SaveServers(e.Servers), SaveAnnouncements(e.Announcements)); |
164 | await Task.WhenAll(SaveServers(e.Servers), SaveAnnouncements(e.Announcements)); |
|
160 | |
165 | |
|
161 | // Update connections to gotify servers. |
166 | // Update connections to gotify servers. |
|
162 | while (_gotifyConnections.TryTake(out var gotifyConnection)) |
167 | while (_gotifyConnections.TryTake(out var gotifyConnection)) |
|
163 | { |
168 | { |
|
164 | gotifyConnection.GotifyNotification -= GotifyConnection_GotifyNotification; |
169 | gotifyConnection.GotifyNotification -= GotifyConnection_GotifyNotification; |
|
165 | gotifyConnection.Stop(); |
170 | gotifyConnection.Stop(); |
|
166 | gotifyConnection.Dispose(); |
171 | gotifyConnection.Dispose(); |
|
167 | gotifyConnection = null; |
172 | gotifyConnection = null; |
|
168 | } |
173 | } |
|
169 | |
174 | |
|
170 | foreach (var server in e.Servers.Server) |
175 | foreach (var server in e.Servers.Server) |
|
171 | { |
176 | { |
|
172 | var gotifyConnection = new GotifyConnection(server, Configuration); |
177 | var gotifyConnection = new GotifyConnection(server, Configuration); |
|
173 | gotifyConnection.GotifyNotification += GotifyConnection_GotifyNotification; |
178 | gotifyConnection.GotifyNotification += GotifyConnection_GotifyNotification; |
|
174 | gotifyConnection.Start(); |
179 | gotifyConnection.Start(); |
|
175 | _gotifyConnections.Add(gotifyConnection); |
180 | _gotifyConnections.Add(gotifyConnection); |
|
176 | } |
181 | } |
|
177 | } |
182 | } |
|
178 | |
183 | |
|
179 | private async void GotifyConnection_GotifyNotification(object sender, GotifyNotificationEventArgs e) |
184 | private async void GotifyConnection_GotifyNotification(object sender, GotifyNotificationEventArgs e) |
|
180 | { |
185 | { |
|
181 | var announcements = await LoadAnnouncements(); |
186 | var announcements = await LoadAnnouncements(); |
|
182 | |
187 | |
|
183 | foreach (var announcement in announcements.Announcement) |
188 | foreach (var announcement in announcements.Announcement) |
|
184 | if (announcement.AppId == e.Notification.AppId) |
189 | if (announcement.AppId == e.Notification.AppId) |
|
185 | { |
190 | { |
|
186 | var configuredNotification = new ToastForm( |
191 | var configuredNotification = new ToastForm( |
|
187 | $"{e.Notification.Title} ({e.Notification.Server.Name}/{e.Notification.AppId})", |
192 | $"{e.Notification.Title} ({e.Notification.Server.Name}/{e.Notification.AppId})", |
|
188 | e.Notification.Message, announcement.LingerTime, e.Image); |
193 | e.Notification.Message, announcement.LingerTime, e.Image); |
|
189 | |
194 | |
|
190 | Application.Run(configuredNotification); |
195 | Application.Run(configuredNotification); |
|
191 | |
196 | |
|
192 | return; |
197 | return; |
|
193 | } |
198 | } |
|
- | 199 | |
||
- | 200 | if (Configuration.InfiniteToastDuration) |
||
- | 201 | { |
||
- | 202 | var infiniteToastForm = new ToastForm( |
||
- | 203 | $"{e.Notification.Title} ({e.Notification.Server.Name}/{e.Notification.AppId})", |
||
- | 204 | e.Notification.Message, e.Image); |
||
- | 205 | |
||
- | 206 | Application.Run(infiniteToastForm); |
||
- | 207 | |
||
- | 208 | return; |
||
- | 209 | } |
||
194 | |
210 | |
|
195 | var notification = new ToastForm( |
211 | var toastForm = new ToastForm( |
|
196 | $"{e.Notification.Title} ({e.Notification.Server.Name}/{e.Notification.AppId})", |
212 | $"{e.Notification.Title} ({e.Notification.Server.Name}/{e.Notification.AppId})", |
|
197 | e.Notification.Message, 5000, e.Image); |
213 | e.Notification.Message, Configuration.ToastDuration, e.Image); |
|
198 | |
214 | |
|
199 | Application.Run(notification); |
215 | Application.Run(toastForm); |
|
200 | } |
216 | } |
|
201 | |
217 | |
|
202 | private void SettingsForm_Closing(object sender, CancelEventArgs e) |
218 | private void SettingsForm_Closing(object sender, CancelEventArgs e) |
|
203 | { |
219 | { |
|
204 | if (_settingsForm == null) return; |
220 | if (_settingsForm == null) return; |
|
205 | |
221 | |
|
206 | _settingsForm.Save -= SettingsForm_Save; |
222 | _settingsForm.Save -= SettingsForm_Save; |
|
207 | _settingsForm.Closing -= SettingsForm_Closing; |
223 | _settingsForm.Closing -= SettingsForm_Closing; |
|
208 | _settingsForm.Dispose(); |
224 | _settingsForm.Dispose(); |
|
209 | _settingsForm = null; |
225 | _settingsForm = null; |
|
210 | } |
226 | } |
|
211 | |
227 | |
|
212 | private void AboutToolStripMenuItem_Click(object sender, EventArgs e) |
228 | private void AboutToolStripMenuItem_Click(object sender, EventArgs e) |
|
213 | { |
229 | { |
|
214 | if (_aboutForm != null) return; |
230 | if (_aboutForm != null) return; |
|
215 | |
231 | |
|
216 | _aboutForm = new AboutForm(); |
232 | _aboutForm = new AboutForm(); |
|
217 | _aboutForm.Closing += AboutForm_Closing; |
233 | _aboutForm.Closing += AboutForm_Closing; |
|
218 | _aboutForm.Show(); |
234 | _aboutForm.Show(); |
|
219 | } |
235 | } |
|
220 | |
236 | |
|
221 | private void AboutForm_Closing(object sender, CancelEventArgs e) |
237 | private void AboutForm_Closing(object sender, CancelEventArgs e) |
|
222 | { |
238 | { |
|
223 | if (_aboutForm == null) return; |
239 | if (_aboutForm == null) return; |
|
224 | |
240 | |
|
225 | _aboutForm.Closing -= AboutForm_Closing; |
241 | _aboutForm.Closing -= AboutForm_Closing; |
|
226 | _aboutForm.Dispose(); |
242 | _aboutForm.Dispose(); |
|
227 | _aboutForm = null; |
243 | _aboutForm = null; |
|
228 | } |
244 | } |
|
229 | |
245 | |
|
230 | private void QuitToolStripMenuItem_Click(object sender, EventArgs e) |
246 | private void QuitToolStripMenuItem_Click(object sender, EventArgs e) |
|
231 | { |
247 | { |
|
232 | Close(); |
248 | Close(); |
|
233 | } |
249 | } |
|
234 | |
250 | |
|
235 | private async void UpdateToolStripMenuItem_Click(object sender, EventArgs e) |
251 | private async void UpdateToolStripMenuItem_Click(object sender, EventArgs e) |
|
236 | { |
252 | { |
|
237 | // Manually check for updates, this will not show a ui |
253 | // Manually check for updates, this will not show a ui |
|
238 | var result = await _sparkle.CheckForUpdatesQuietly(); |
254 | var result = await _sparkle.CheckForUpdatesQuietly(); |
|
239 | var updates = result.Updates; |
255 | var updates = result.Updates; |
|
240 | if (result.Status == UpdateStatus.UpdateAvailable) |
256 | if (result.Status == UpdateStatus.UpdateAvailable) |
|
241 | { |
257 | { |
|
242 | // if update(s) are found, then we have to trigger the UI to show it gracefully |
258 | // if update(s) are found, then we have to trigger the UI to show it gracefully |
|
243 | _sparkle.ShowUpdateNeededUI(); |
259 | _sparkle.ShowUpdateNeededUI(); |
|
244 | return; |
260 | return; |
|
245 | } |
261 | } |
|
246 | |
262 | |
|
247 | MessageBox.Show("No updates available at this time.", "Winify", MessageBoxButtons.OK, |
263 | MessageBox.Show("No updates available at this time.", "Winify", MessageBoxButtons.OK, |
|
248 | MessageBoxIcon.Asterisk, |
264 | MessageBoxIcon.Asterisk, |
|
249 | MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly, false); |
265 | MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly, false); |
|
250 | } |
266 | } |
|
251 | |
267 | |
|
252 | #endregion |
268 | #endregion |
|
253 | |
269 | |
|
254 | #region Public Methods |
270 | #region Public Methods |
|
255 | |
271 | |
|
256 | public async Task SaveConfiguration() |
272 | public async Task SaveConfiguration() |
|
257 | { |
273 | { |
|
258 | if (!Directory.Exists(Constants.UserApplicationDirectory)) |
274 | if (!Directory.Exists(Constants.UserApplicationDirectory)) |
|
259 | Directory.CreateDirectory(Constants.UserApplicationDirectory); |
275 | Directory.CreateDirectory(Constants.UserApplicationDirectory); |
|
260 | |
276 | |
|
261 | switch (await Serialization.Serialize(Configuration, Constants.ConfigurationFile, "Configuration", |
277 | switch (await Serialization.Serialize(Configuration, Constants.ConfigurationFile, "Configuration", |
|
262 | "<!ATTLIST Configuration xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>", |
278 | "<!ATTLIST Configuration xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>", |
|
263 | CancellationToken.None)) |
279 | CancellationToken.None)) |
|
264 | { |
280 | { |
|
265 | case SerializationSuccess<Configuration.Configuration> _: |
281 | case SerializationSuccess<Configuration.Configuration> _: |
|
266 | Log.Information("Serialized configuration."); |
282 | Log.Information("Serialized configuration."); |
|
267 | break; |
283 | break; |
|
268 | case SerializationFailure serializationFailure: |
284 | case SerializationFailure serializationFailure: |
|
269 | Log.Warning(serializationFailure.Exception.Message, "Failed to serialize configuration."); |
285 | Log.Warning(serializationFailure.Exception.Message, "Failed to serialize configuration."); |
|
270 | break; |
286 | break; |
|
271 | } |
287 | } |
|
272 | } |
288 | } |
|
273 | |
289 | |
|
274 | public static async Task<Configuration.Configuration> LoadConfiguration() |
290 | public static async Task<Configuration.Configuration> LoadConfiguration() |
|
275 | { |
291 | { |
|
276 | if (!Directory.Exists(Constants.UserApplicationDirectory)) |
292 | if (!Directory.Exists(Constants.UserApplicationDirectory)) |
|
277 | Directory.CreateDirectory(Constants.UserApplicationDirectory); |
293 | Directory.CreateDirectory(Constants.UserApplicationDirectory); |
|
278 | |
294 | |
|
279 | var deserializationResult = |
295 | var deserializationResult = |
|
280 | await Serialization.Deserialize<Configuration.Configuration>(Constants.ConfigurationFile, |
296 | await Serialization.Deserialize<Configuration.Configuration>(Constants.ConfigurationFile, |
|
281 | Constants.ConfigurationNamespace, Constants.ConfigurationXsd, CancellationToken.None); |
297 | Constants.ConfigurationNamespace, Constants.ConfigurationXsd, CancellationToken.None); |
|
282 | |
298 | |
|
283 | switch (deserializationResult) |
299 | switch (deserializationResult) |
|
284 | { |
300 | { |
|
285 | case SerializationSuccess<Configuration.Configuration> serializationSuccess: |
301 | case SerializationSuccess<Configuration.Configuration> serializationSuccess: |
|
286 | return serializationSuccess.Result; |
302 | return serializationSuccess.Result; |
|
287 | case SerializationFailure serializationFailure: |
303 | case SerializationFailure serializationFailure: |
|
288 | Log.Warning(serializationFailure.Exception, "Failed to load configuration."); |
304 | Log.Warning(serializationFailure.Exception, "Failed to load configuration."); |
|
289 | return new Configuration.Configuration(); |
305 | return new Configuration.Configuration(); |
|
290 | default: |
306 | default: |
|
291 | return new Configuration.Configuration(); |
307 | return new Configuration.Configuration(); |
|
292 | } |
308 | } |
|
293 | } |
309 | } |
|
294 | |
310 | |
|
295 | #endregion |
311 | #endregion |
|
296 | |
312 | |
|
297 | #region Private Methods |
313 | #region Private Methods |
|
298 | |
314 | |
|
299 | private static async Task SaveAnnouncements(Announcements.Announcements announcements) |
315 | private static async Task SaveAnnouncements(Announcements.Announcements announcements) |
|
300 | { |
316 | { |
|
301 | switch (await Serialization.Serialize(announcements, Constants.AnnouncementsFile, "Announcements", |
317 | switch (await Serialization.Serialize(announcements, Constants.AnnouncementsFile, "Announcements", |
|
302 | "<!ATTLIST Announcements xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>", |
318 | "<!ATTLIST Announcements xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>", |
|
303 | CancellationToken.None)) |
319 | CancellationToken.None)) |
|
304 | { |
320 | { |
|
305 | case SerializationFailure serializationFailure: |
321 | case SerializationFailure serializationFailure: |
|
306 | Log.Warning(serializationFailure.Exception, "Unable to serialize announcements."); |
322 | Log.Warning(serializationFailure.Exception, "Unable to serialize announcements."); |
|
307 | break; |
323 | break; |
|
308 | } |
324 | } |
|
309 | } |
325 | } |
|
310 | |
326 | |
|
311 | private static async Task SaveServers(Servers.Servers servers) |
327 | private static async Task SaveServers(Servers.Servers servers) |
|
312 | { |
328 | { |
|
313 | // Encrypt password for all servers. |
329 | // Encrypt password for all servers. |
|
314 | var deviceId = Miscellaneous.GetMachineGuid(); |
330 | var deviceId = Miscellaneous.GetMachineGuid(); |
|
315 | var @protected = new Servers.Servers |
331 | var @protected = new Servers.Servers |
|
316 | { |
332 | { |
|
317 | Server = new BindingListWithCollectionChanged<Server>() |
333 | Server = new BindingListWithCollectionChanged<Server>() |
|
318 | }; |
334 | }; |
|
319 | |
335 | |
|
320 | foreach (var server in servers.Server) |
336 | foreach (var server in servers.Server) |
|
321 | { |
337 | { |
|
322 | var password = Encoding.UTF8.GetBytes(server.Password); |
338 | var password = Encoding.UTF8.GetBytes(server.Password); |
|
323 | var encrypted = await AES.Encrypt(password, deviceId); |
339 | var encrypted = await AES.Encrypt(password, deviceId); |
|
324 | var armored = Convert.ToBase64String(encrypted); |
340 | var armored = Convert.ToBase64String(encrypted); |
|
325 | |
341 | |
|
326 | @protected.Server.Add(new Server(server.Name, server.Url, server.Username, armored)); |
342 | @protected.Server.Add(new Server(server.Name, server.Url, server.Username, armored)); |
|
327 | } |
343 | } |
|
328 | |
344 | |
|
329 | switch (await Serialization.Serialize(@protected, Constants.ServersFile, "Servers", |
345 | switch (await Serialization.Serialize(@protected, Constants.ServersFile, "Servers", |
|
330 | "<!ATTLIST Servers xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>", |
346 | "<!ATTLIST Servers xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>", |
|
331 | CancellationToken.None)) |
347 | CancellationToken.None)) |
|
332 | { |
348 | { |
|
333 | case SerializationFailure serializationFailure: |
349 | case SerializationFailure serializationFailure: |
|
334 | Log.Warning(serializationFailure.Exception, "Unable to serialize servers."); |
350 | Log.Warning(serializationFailure.Exception, "Unable to serialize servers."); |
|
335 | break; |
351 | break; |
|
336 | } |
352 | } |
|
337 | } |
353 | } |
|
338 | |
354 | |
|
339 | private static async Task<Announcements.Announcements> LoadAnnouncements() |
355 | private static async Task<Announcements.Announcements> LoadAnnouncements() |
|
340 | { |
356 | { |
|
341 | if (!Directory.Exists(Constants.UserApplicationDirectory)) |
357 | if (!Directory.Exists(Constants.UserApplicationDirectory)) |
|
342 | Directory.CreateDirectory(Constants.UserApplicationDirectory); |
358 | Directory.CreateDirectory(Constants.UserApplicationDirectory); |
|
343 | |
359 | |
|
344 | var deserializationResult = |
360 | var deserializationResult = |
|
345 | await Serialization.Deserialize<Announcements.Announcements>(Constants.AnnouncementsFile, |
361 | await Serialization.Deserialize<Announcements.Announcements>(Constants.AnnouncementsFile, |
|
346 | "urn:winify-announcements-schema", "Announcements.xsd", CancellationToken.None); |
362 | "urn:winify-announcements-schema", "Announcements.xsd", CancellationToken.None); |
|
347 | |
363 | |
|
348 | switch (deserializationResult) |
364 | switch (deserializationResult) |
|
349 | { |
365 | { |
|
350 | case SerializationSuccess<Announcements.Announcements> serializationSuccess: |
366 | case SerializationSuccess<Announcements.Announcements> serializationSuccess: |
|
351 | return serializationSuccess.Result; |
367 | return serializationSuccess.Result; |
|
352 | case SerializationFailure serializationFailure: |
368 | case SerializationFailure serializationFailure: |
|
353 | Log.Warning(serializationFailure.Exception, "Unable to load announcements."); |
369 | Log.Warning(serializationFailure.Exception, "Unable to load announcements."); |
|
354 | return new Announcements.Announcements(); |
370 | return new Announcements.Announcements(); |
|
355 | default: |
371 | default: |
|
356 | return new Announcements.Announcements(); |
372 | return new Announcements.Announcements(); |
|
357 | } |
373 | } |
|
358 | } |
374 | } |
|
359 | |
375 | |
|
360 | private static async Task<Servers.Servers> LoadServers() |
376 | private static async Task<Servers.Servers> LoadServers() |
|
361 | { |
377 | { |
|
362 | if (!Directory.Exists(Constants.UserApplicationDirectory)) |
378 | if (!Directory.Exists(Constants.UserApplicationDirectory)) |
|
363 | Directory.CreateDirectory(Constants.UserApplicationDirectory); |
379 | Directory.CreateDirectory(Constants.UserApplicationDirectory); |
|
364 | |
380 | |
|
365 | var deserializationResult = |
381 | var deserializationResult = |
|
366 | await Serialization.Deserialize<Servers.Servers>(Constants.ServersFile, |
382 | await Serialization.Deserialize<Servers.Servers>(Constants.ServersFile, |
|
367 | "urn:winify-servers-schema", "Servers.xsd", CancellationToken.None); |
383 | "urn:winify-servers-schema", "Servers.xsd", CancellationToken.None); |
|
368 | |
384 | |
|
369 | switch (deserializationResult) |
385 | switch (deserializationResult) |
|
370 | { |
386 | { |
|
371 | case SerializationSuccess<Servers.Servers> serializationSuccess: |
387 | case SerializationSuccess<Servers.Servers> serializationSuccess: |
|
372 | // Decrypt password. |
388 | // Decrypt password. |
|
373 | var deviceId = Miscellaneous.GetMachineGuid(); |
389 | var deviceId = Miscellaneous.GetMachineGuid(); |
|
374 | var @protected = new Servers.Servers |
390 | var @protected = new Servers.Servers |
|
375 | { |
391 | { |
|
376 | Server = new BindingListWithCollectionChanged<Server>() |
392 | Server = new BindingListWithCollectionChanged<Server>() |
|
377 | }; |
393 | }; |
|
378 | foreach (var server in serializationSuccess.Result.Server) |
394 | foreach (var server in serializationSuccess.Result.Server) |
|
379 | { |
395 | { |
|
380 | var unarmored = Convert.FromBase64String(server.Password); |
396 | var unarmored = Convert.FromBase64String(server.Password); |
|
381 | var result = await AES.Decrypt(unarmored, deviceId); |
397 | var result = await AES.Decrypt(unarmored, deviceId); |
|
382 | var decrypted = Encoding.UTF8.GetString(result); |
398 | var decrypted = Encoding.UTF8.GetString(result); |
|
383 | |
399 | |
|
384 | @protected.Server.Add(new Server(server.Name, server.Url, server.Username, decrypted)); |
400 | @protected.Server.Add(new Server(server.Name, server.Url, server.Username, decrypted)); |
|
385 | } |
401 | } |
|
386 | |
402 | |
|
387 | return @protected; |
403 | return @protected; |
|
388 | |
404 | |
|
389 | case SerializationFailure serializationFailure: |
405 | case SerializationFailure serializationFailure: |
|
390 | Log.Warning(serializationFailure.Exception, "Unable to load servers."); |
406 | Log.Warning(serializationFailure.Exception, "Unable to load servers."); |
|
391 | return new Servers.Servers(); |
407 | return new Servers.Servers(); |
|
392 | |
408 | |
|
393 | default: |
409 | default: |
|
394 | return new Servers.Servers(); |
410 | return new Servers.Servers(); |
|
395 | } |
411 | } |
|
396 | } |
412 | } |
|
397 | |
413 | |
|
398 | #endregion |
414 | #endregion |
|
399 | } |
415 | } |
|
400 | } |
416 | } |
|
401 | |
417 | |
|
402 |
|
418 |
|
|
403 | |
419 | |
|
404 | |
420 | |
|
405 | |
421 | |