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