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