Winify – Blame information for rev 40

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