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