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