Winify – Blame information for rev 41
?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; |
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 | { |
41 | office | 294 | var password = Encoding.UTF8.GetBytes(server.Password); |
295 | var encrypted = await AES.Encrypt(password, deviceId); |
||
21 | office | 296 | var armored = Convert.ToBase64String(encrypted); |
297 | |||
298 | @protected.Server.Add(new Server(server.Name, server.Url, server.Username, armored)); |
||
299 | } |
||
300 | |||
30 | office | 301 | switch (await Serialization.Serialize(@protected, Constants.ServersFile, "Servers", |
302 | "<!ATTLIST Servers xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>", |
||
303 | CancellationToken.None)) |
||
21 | office | 304 | { |
305 | case SerializationFailure serializationFailure: |
||
306 | Log.Warning(serializationFailure.Exception, "Unable to serialize servers."); |
||
307 | break; |
||
308 | } |
||
309 | } |
||
310 | |||
15 | office | 311 | private static async Task<Announcements.Announcements> LoadAnnouncements() |
312 | { |
||
313 | if (!Directory.Exists(Constants.UserApplicationDirectory)) |
||
314 | Directory.CreateDirectory(Constants.UserApplicationDirectory); |
||
315 | |||
316 | var deserializationResult = |
||
30 | office | 317 | await Serialization.Deserialize<Announcements.Announcements>(Constants.AnnouncementsFile, |
318 | "urn:winify-announcements-schema", "Announcements.xsd", CancellationToken.None); |
||
15 | office | 319 | |
320 | switch (deserializationResult) |
||
321 | { |
||
322 | case SerializationSuccess<Announcements.Announcements> serializationSuccess: |
||
323 | return serializationSuccess.Result; |
||
21 | office | 324 | case SerializationFailure serializationFailure: |
325 | Log.Warning(serializationFailure.Exception, "Unable to load announcements."); |
||
326 | return new Announcements.Announcements(); |
||
15 | office | 327 | default: |
328 | return new Announcements.Announcements(); |
||
329 | } |
||
330 | } |
||
331 | |||
30 | office | 332 | private static async Task<Servers.Servers> LoadServers() |
4 | office | 333 | { |
334 | if (!Directory.Exists(Constants.UserApplicationDirectory)) |
||
335 | Directory.CreateDirectory(Constants.UserApplicationDirectory); |
||
336 | |||
15 | office | 337 | var deserializationResult = |
30 | office | 338 | await Serialization.Deserialize<Servers.Servers>(Constants.ServersFile, |
339 | "urn:winify-servers-schema", "Servers.xsd", CancellationToken.None); |
||
4 | office | 340 | |
341 | switch (deserializationResult) |
||
342 | { |
||
30 | office | 343 | case SerializationSuccess<Servers.Servers> serializationSuccess: |
15 | office | 344 | // Decrypt password. |
345 | var deviceId = Miscellaneous.GetMachineGuid(); |
||
30 | office | 346 | var @protected = new Servers.Servers |
15 | office | 347 | { |
348 | Server = new BindingListWithCollectionChanged<Server>() |
||
349 | }; |
||
350 | foreach (var server in serializationSuccess.Result.Server) |
||
351 | { |
||
352 | var unarmored = Convert.FromBase64String(server.Password); |
||
41 | office | 353 | var result = await AES.Decrypt(unarmored, deviceId); |
354 | var decrypted = Encoding.UTF8.GetString(result); |
||
4 | office | 355 | |
15 | office | 356 | @protected.Server.Add(new Server(server.Name, server.Url, server.Username, decrypted)); |
357 | } |
||
358 | |||
359 | return @protected; |
||
360 | |||
21 | office | 361 | case SerializationFailure serializationFailure: |
362 | Log.Warning(serializationFailure.Exception, "Unable to load servers."); |
||
30 | office | 363 | return new Servers.Servers(); |
21 | office | 364 | |
4 | office | 365 | default: |
30 | office | 366 | return new Servers.Servers(); |
4 | office | 367 | } |
368 | } |
||
369 | |||
370 | #endregion |
||
1 | office | 371 | } |
372 | } |