Winify – Diff between revs 17 and 18
?pathlinks?
Rev 17 | Rev 18 | |||
---|---|---|---|---|
1 | using System; |
1 | using System; |
|
2 | using System.Collections.Specialized; |
2 | using System.Collections.Specialized; |
|
3 | using System.ComponentModel; |
3 | using System.ComponentModel; |
|
4 | using System.Configuration; |
4 | using System.Configuration; |
|
5 | using System.Diagnostics; |
- | ||
6 | using System.IO; |
5 | using System.IO; |
|
7 | using System.Text; |
6 | using System.Text; |
|
8 | using System.Threading.Tasks; |
7 | using System.Threading.Tasks; |
|
9 | using System.Windows.Forms; |
8 | using System.Windows.Forms; |
|
10 | using AutoUpdaterDotNET; |
9 | using AutoUpdaterDotNET; |
|
- | 10 | using Serilog; |
||
11 | using Servers; |
11 | using Servers; |
|
12 | using Winify.Gotify; |
12 | using Winify.Gotify; |
|
13 | using Winify.Properties; |
13 | using Winify.Properties; |
|
14 | using Winify.Servers.Serialization; |
14 | using Winify.Servers.Serialization; |
|
15 | using Winify.Utilities; |
15 | using Winify.Utilities; |
|
16 | |
16 | |
|
17 | namespace Winify |
17 | namespace Winify |
|
18 | { |
18 | { |
|
19 | public partial class Form1 : Form |
19 | public partial class Form1 : Form |
|
20 | { |
20 | { |
|
21 | #region Private Delegates, Events, Enums, Properties, Indexers and Fields |
21 | #region Private Delegates, Events, Enums, Properties, Indexers and Fields |
|
22 | |
22 | |
|
23 | private readonly Announcements.Announcements _notifications; |
23 | private readonly Announcements.Announcements _notifications; |
|
24 | |
24 | |
|
25 | private readonly global::Servers.Servers _servers; |
25 | private readonly global::Servers.Servers _servers; |
|
26 | |
26 | |
|
27 | private readonly TaskScheduler _taskScheduler; |
27 | private readonly TaskScheduler _taskScheduler; |
|
28 | |
28 | |
|
29 | private AboutForm _aboutForm; |
29 | private AboutForm _aboutForm; |
|
30 | |
30 | |
|
31 | private GotifyConnectionManager _gotifyConnectionManager; |
31 | private GotifyConnectionManager _gotifyConnectionManager; |
|
32 | |
32 | |
|
33 | private NotificationManager _notificationManager; |
33 | private NotificationManager _notificationManager; |
|
34 | |
34 | |
|
35 | private SettingsForm _settingsForm; |
35 | private SettingsForm _settingsForm; |
|
36 | |
36 | |
|
37 | #endregion |
37 | #endregion |
|
38 | |
38 | |
|
39 | #region Constructors, Destructors and Finalizers |
39 | #region Constructors, Destructors and Finalizers |
|
40 | |
40 | |
|
41 | public Form1() |
41 | public Form1() |
|
42 | { |
42 | { |
|
43 | InitializeComponent(); |
43 | InitializeComponent(); |
|
- | 44 | |
||
- | 45 | Log.Logger = new LoggerConfiguration() |
||
- | 46 | .MinimumLevel.Debug() |
||
- | 47 | .WriteTo.File(Path.Combine(Constants.UserApplicationDirectory, "Logs", $"{Constants.AssemblyName}.log"), |
||
- | 48 | rollingInterval: RollingInterval.Day) |
||
- | 49 | .CreateLogger(); |
||
44 | |
50 | |
|
45 | AutoUpdater.Start("http://winify.grimore.org/update/winify.xml"); |
51 | AutoUpdater.Start("http://winify.grimore.org/update/winify.xml"); |
|
46 | |
52 | |
|
47 | // Upgrade settings if required. |
53 | // Upgrade settings if required. |
|
48 | if (!ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).HasFile) |
54 | if (!ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).HasFile) |
|
49 | { |
55 | { |
|
50 | Settings.Default.Upgrade(); |
56 | Settings.Default.Upgrade(); |
|
51 | } |
57 | } |
|
52 | |
58 | |
|
53 | // Bind to settings changed event. |
59 | // Bind to settings changed event. |
|
54 | Settings.Default.SettingsLoaded += Default_SettingsLoaded; |
60 | Settings.Default.SettingsLoaded += Default_SettingsLoaded; |
|
55 | Settings.Default.SettingsSaving += Default_SettingsSaving; |
61 | Settings.Default.SettingsSaving += Default_SettingsSaving; |
|
56 | Settings.Default.PropertyChanged += Default_PropertyChanged; |
62 | Settings.Default.PropertyChanged += Default_PropertyChanged; |
|
57 | |
63 | |
|
58 | _servers = new global::Servers.Servers(); |
64 | _servers = new global::Servers.Servers(); |
|
59 | _servers.Server.CollectionChanged += Server_CollectionChanged; |
65 | _servers.Server.CollectionChanged += Server_CollectionChanged; |
|
60 | _notifications = new Announcements.Announcements(); |
66 | _notifications = new Announcements.Announcements(); |
|
61 | _notifications.Announcement.CollectionChanged += Announcements_CollectionChanged; |
67 | _notifications.Announcement.CollectionChanged += Announcements_CollectionChanged; |
|
62 | |
68 | |
|
63 | _taskScheduler = TaskScheduler.FromCurrentSynchronizationContext(); |
69 | _taskScheduler = TaskScheduler.FromCurrentSynchronizationContext(); |
|
64 | |
70 | |
|
65 | _notificationManager = new NotificationManager(_taskScheduler); |
71 | _notificationManager = new NotificationManager(_taskScheduler); |
|
66 | |
72 | |
|
67 | _gotifyConnectionManager = new GotifyConnectionManager(_servers); |
73 | _gotifyConnectionManager = new GotifyConnectionManager(_servers); |
|
68 | _gotifyConnectionManager.GotifyNotification += GotifyConnectionManager_GotifyNotification; |
74 | _gotifyConnectionManager.GotifyNotification += GotifyConnectionManager_GotifyNotification; |
|
69 | |
75 | |
|
70 | LoadServers().ContinueWith(async task => |
76 | LoadServers().ContinueWith(async task => |
|
71 | { |
77 | { |
|
72 | var restoredServers = await task; |
78 | var restoredServers = await task; |
|
73 | |
79 | |
|
74 | foreach (var server in restoredServers.Server) |
80 | foreach (var server in restoredServers.Server) |
|
75 | { |
81 | { |
|
76 | _servers.Server.Add(server); |
82 | _servers.Server.Add(server); |
|
77 | } |
83 | } |
|
78 | }); |
84 | }); |
|
79 | |
85 | |
|
80 | LoadAnnouncements().ContinueWith(async task => |
86 | LoadAnnouncements().ContinueWith(async task => |
|
81 | { |
87 | { |
|
82 | var restoreAnnouncements = await task; |
88 | var restoreAnnouncements = await task; |
|
83 | |
89 | |
|
84 | foreach (var announcement in restoreAnnouncements.Announcement) |
90 | foreach (var announcement in restoreAnnouncements.Announcement) |
|
85 | { |
91 | { |
|
86 | _notifications.Announcement.Add(announcement); |
92 | _notifications.Announcement.Add(announcement); |
|
87 | } |
93 | } |
|
88 | }); |
94 | }); |
|
89 | } |
95 | } |
|
90 | |
96 | |
|
91 | /// <summary> |
97 | /// <summary> |
|
92 | /// Clean up any resources being used. |
98 | /// Clean up any resources being used. |
|
93 | /// </summary> |
99 | /// </summary> |
|
94 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> |
100 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> |
|
95 | protected override void Dispose(bool disposing) |
101 | protected override void Dispose(bool disposing) |
|
96 | { |
102 | { |
|
97 | if (disposing && components != null) |
103 | if (disposing && components != null) |
|
98 | { |
104 | { |
|
99 | _servers.Server.CollectionChanged -= Server_CollectionChanged; |
105 | _servers.Server.CollectionChanged -= Server_CollectionChanged; |
|
100 | _notifications.Announcement.CollectionChanged -= Announcements_CollectionChanged; |
106 | _notifications.Announcement.CollectionChanged -= Announcements_CollectionChanged; |
|
101 | |
107 | |
|
102 | Settings.Default.SettingsLoaded -= Default_SettingsLoaded; |
108 | Settings.Default.SettingsLoaded -= Default_SettingsLoaded; |
|
103 | Settings.Default.SettingsSaving -= Default_SettingsSaving; |
109 | Settings.Default.SettingsSaving -= Default_SettingsSaving; |
|
104 | Settings.Default.PropertyChanged -= Default_PropertyChanged; |
110 | Settings.Default.PropertyChanged -= Default_PropertyChanged; |
|
105 | |
111 | |
|
106 | _gotifyConnectionManager.GotifyNotification -= GotifyConnectionManager_GotifyNotification; |
112 | _gotifyConnectionManager.GotifyNotification -= GotifyConnectionManager_GotifyNotification; |
|
107 | _gotifyConnectionManager?.Dispose(); |
113 | _gotifyConnectionManager?.Dispose(); |
|
108 | _gotifyConnectionManager = null; |
114 | _gotifyConnectionManager = null; |
|
109 | |
115 | |
|
110 | _notificationManager?.Dispose(); |
116 | _notificationManager?.Dispose(); |
|
111 | _notificationManager = null; |
117 | _notificationManager = null; |
|
112 | |
118 | |
|
113 | components.Dispose(); |
119 | components.Dispose(); |
|
114 | } |
120 | } |
|
115 | |
121 | |
|
116 | base.Dispose(disposing); |
122 | base.Dispose(disposing); |
|
117 | } |
123 | } |
|
118 | |
124 | |
|
119 | #endregion |
125 | #endregion |
|
120 | |
126 | |
|
121 | #region Event Handlers |
127 | #region Event Handlers |
|
122 | |
128 | |
|
123 | private async void Announcements_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) |
129 | private async void Announcements_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) |
|
124 | { |
130 | { |
|
125 | switch (await ServersSerialization.Serialize(_notifications, Constants.NotificationsFile, "Announcements", |
131 | switch (await ServersSerialization.Serialize(_notifications, Constants.NotificationsFile, "Announcements", |
|
126 | "<!ATTLIST Announcements xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>")) |
132 | "<!ATTLIST Announcements xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>")) |
|
127 | { |
133 | { |
|
128 | case SerializationFailure serializationFailure: |
134 | case SerializationFailure serializationFailure: |
|
129 | Debug.WriteLine(serializationFailure.Exception.Message); |
135 | Log.Warning(serializationFailure.Exception, "Unable to serialize announcements."); |
|
130 | break; |
136 | break; |
|
131 | } |
137 | } |
|
132 | } |
138 | } |
|
133 | |
139 | |
|
134 | private void GotifyConnectionManager_GotifyNotification(object sender, GotifyNotificationEventArgs e) |
140 | private void GotifyConnectionManager_GotifyNotification(object sender, GotifyNotificationEventArgs e) |
|
135 | { |
141 | { |
|
136 | _notificationManager.ShowNotification(e, _notifications); |
142 | _notificationManager.ShowNotification(e, _notifications); |
|
137 | } |
143 | } |
|
138 | |
144 | |
|
139 | private async void Server_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) |
145 | private async void Server_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) |
|
140 | { |
146 | { |
|
141 | // Encrypt password for all servers. |
147 | // Encrypt password for all servers. |
|
142 | var deviceId = Miscellaneous.GetMachineGuid(); |
148 | var deviceId = Miscellaneous.GetMachineGuid(); |
|
143 | var @protected = new global::Servers.Servers |
149 | var @protected = new global::Servers.Servers |
|
144 | { |
150 | { |
|
145 | Server = new BindingListWithCollectionChanged<Server>() |
151 | Server = new BindingListWithCollectionChanged<Server>() |
|
146 | }; |
152 | }; |
|
147 | foreach (var server in _servers.Server) |
153 | foreach (var server in _servers.Server) |
|
148 | { |
154 | { |
|
149 | var encrypted = AES.Encrypt(Encoding.UTF8.GetBytes(server.Password), deviceId); |
155 | var encrypted = AES.Encrypt(Encoding.UTF8.GetBytes(server.Password), deviceId); |
|
150 | var armored = Convert.ToBase64String(encrypted); |
156 | var armored = Convert.ToBase64String(encrypted); |
|
151 | |
157 | |
|
152 | @protected.Server.Add(new Server(server.Name, server.Url, server.Username, armored)); |
158 | @protected.Server.Add(new Server(server.Name, server.Url, server.Username, armored)); |
|
153 | } |
159 | } |
|
154 | |
160 | |
|
155 | switch (await ServersSerialization.Serialize(@protected, Constants.ServersFile, "Servers", |
161 | switch (await ServersSerialization.Serialize(@protected, Constants.ServersFile, "Servers", |
|
156 | "<!ATTLIST Servers xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>")) |
162 | "<!ATTLIST Servers xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>")) |
|
157 | { |
163 | { |
|
158 | case SerializationFailure serializationFailure: |
164 | case SerializationFailure serializationFailure: |
|
159 | Debug.WriteLine(serializationFailure.Exception.Message); |
165 | Log.Warning(serializationFailure.Exception, "Unable to serialize servers."); |
|
160 | break; |
166 | break; |
|
161 | } |
167 | } |
|
162 | } |
168 | } |
|
163 | |
169 | |
|
164 | private static void Default_PropertyChanged(object sender, PropertyChangedEventArgs e) |
170 | private static void Default_PropertyChanged(object sender, PropertyChangedEventArgs e) |
|
165 | { |
171 | { |
|
166 | Settings.Default.Save(); |
172 | Settings.Default.Save(); |
|
167 | } |
173 | } |
|
168 | |
174 | |
|
169 | private static void Default_SettingsSaving(object sender, CancelEventArgs e) |
175 | private static void Default_SettingsSaving(object sender, CancelEventArgs e) |
|
170 | { |
176 | { |
|
171 | } |
177 | } |
|
172 | |
178 | |
|
173 | private static void Default_SettingsLoaded(object sender, SettingsLoadedEventArgs e) |
179 | private static void Default_SettingsLoaded(object sender, SettingsLoadedEventArgs e) |
|
174 | { |
180 | { |
|
175 | } |
181 | } |
|
176 | |
182 | |
|
177 | private void SettingsToolStripMenuItem_Click(object sender, EventArgs e) |
183 | private void SettingsToolStripMenuItem_Click(object sender, EventArgs e) |
|
178 | { |
184 | { |
|
179 | if (_settingsForm != null) |
185 | if (_settingsForm != null) |
|
180 | { |
186 | { |
|
181 | return; |
187 | return; |
|
182 | } |
188 | } |
|
183 | |
189 | |
|
184 | _settingsForm = new SettingsForm(_servers, _notifications); |
190 | _settingsForm = new SettingsForm(_servers, _notifications); |
|
185 | _settingsForm.Closing += SettingsForm_Closing; |
191 | _settingsForm.Closing += SettingsForm_Closing; |
|
186 | _settingsForm.Show(); |
192 | _settingsForm.Show(); |
|
187 | } |
193 | } |
|
188 | |
194 | |
|
189 | private void SettingsForm_Closing(object sender, CancelEventArgs e) |
195 | private void SettingsForm_Closing(object sender, CancelEventArgs e) |
|
190 | { |
196 | { |
|
191 | if (_settingsForm == null) |
197 | if (_settingsForm == null) |
|
192 | { |
198 | { |
|
193 | return; |
199 | return; |
|
194 | } |
200 | } |
|
195 | |
201 | |
|
196 | _settingsForm.Closing -= SettingsForm_Closing; |
202 | _settingsForm.Closing -= SettingsForm_Closing; |
|
197 | _settingsForm.Dispose(); |
203 | _settingsForm.Dispose(); |
|
198 | _settingsForm = null; |
204 | _settingsForm = null; |
|
199 | } |
205 | } |
|
200 | |
206 | |
|
201 | private void AboutToolStripMenuItem_Click(object sender, EventArgs e) |
207 | private void AboutToolStripMenuItem_Click(object sender, EventArgs e) |
|
202 | { |
208 | { |
|
203 | if (_aboutForm != null) |
209 | if (_aboutForm != null) |
|
204 | { |
210 | { |
|
205 | return; |
211 | return; |
|
206 | } |
212 | } |
|
207 | |
213 | |
|
208 | _aboutForm = new AboutForm(); |
214 | _aboutForm = new AboutForm(); |
|
209 | _aboutForm.Closing += AboutForm_Closing; |
215 | _aboutForm.Closing += AboutForm_Closing; |
|
210 | _aboutForm.Show(); |
216 | _aboutForm.Show(); |
|
211 | } |
217 | } |
|
212 | |
218 | |
|
213 | private void AboutForm_Closing(object sender, CancelEventArgs e) |
219 | private void AboutForm_Closing(object sender, CancelEventArgs e) |
|
214 | { |
220 | { |
|
215 | if (_aboutForm == null) |
221 | if (_aboutForm == null) |
|
216 | { |
222 | { |
|
217 | return; |
223 | return; |
|
218 | } |
224 | } |
|
219 | |
225 | |
|
220 | _aboutForm.Closing -= AboutForm_Closing; |
226 | _aboutForm.Closing -= AboutForm_Closing; |
|
221 | _aboutForm.Dispose(); |
227 | _aboutForm.Dispose(); |
|
222 | _aboutForm = null; |
228 | _aboutForm = null; |
|
223 | } |
229 | } |
|
224 | |
230 | |
|
225 | private void QuitToolStripMenuItem_Click(object sender, EventArgs e) |
231 | private void QuitToolStripMenuItem_Click(object sender, EventArgs e) |
|
226 | { |
232 | { |
|
227 | Close(); |
233 | Close(); |
|
228 | |
234 | |
|
229 | Environment.Exit(0); |
235 | Environment.Exit(0); |
|
230 | } |
236 | } |
|
231 | |
237 | |
|
232 | private void UpdateToolStripMenuItem_Click(object sender, EventArgs e) |
238 | private void UpdateToolStripMenuItem_Click(object sender, EventArgs e) |
|
233 | { |
239 | { |
|
234 | AutoUpdater.Start("http://winify.grimore.org/update/winify.xml"); |
240 | AutoUpdater.Start("http://winify.grimore.org/update/winify.xml"); |
|
235 | } |
241 | } |
|
236 | |
242 | |
|
237 | #endregion |
243 | #endregion |
|
238 | |
244 | |
|
239 | #region Private Methods |
245 | #region Private Methods |
|
240 | |
246 | |
|
241 | private static async Task<Announcements.Announcements> LoadAnnouncements() |
247 | private static async Task<Announcements.Announcements> LoadAnnouncements() |
|
242 | { |
248 | { |
|
243 | if (!Directory.Exists(Constants.UserApplicationDirectory)) |
249 | if (!Directory.Exists(Constants.UserApplicationDirectory)) |
|
244 | { |
250 | { |
|
245 | Directory.CreateDirectory(Constants.UserApplicationDirectory); |
251 | Directory.CreateDirectory(Constants.UserApplicationDirectory); |
|
246 | } |
252 | } |
|
247 | |
253 | |
|
248 | var deserializationResult = |
254 | var deserializationResult = |
|
249 | await ServersSerialization.Deserialize<Announcements.Announcements>(Constants.NotificationsFile, |
255 | await ServersSerialization.Deserialize<Announcements.Announcements>(Constants.NotificationsFile, |
|
250 | "urn:winify-announcements-schema", "Announcements.xsd"); |
256 | "urn:winify-announcements-schema", "Announcements.xsd"); |
|
251 | |
257 | |
|
252 | switch (deserializationResult) |
258 | switch (deserializationResult) |
|
253 | { |
259 | { |
|
254 | case SerializationSuccess<Announcements.Announcements> serializationSuccess: |
260 | case SerializationSuccess<Announcements.Announcements> serializationSuccess: |
|
255 | return serializationSuccess.Result; |
261 | return serializationSuccess.Result; |
|
256 | default: |
262 | default: |
|
257 | return new Announcements.Announcements(); |
263 | return new Announcements.Announcements(); |
|
258 | } |
264 | } |
|
259 | } |
265 | } |
|
260 | |
266 | |
|
261 | private static async Task<global::Servers.Servers> LoadServers() |
267 | private static async Task<global::Servers.Servers> LoadServers() |
|
262 | { |
268 | { |
|
263 | if (!Directory.Exists(Constants.UserApplicationDirectory)) |
269 | if (!Directory.Exists(Constants.UserApplicationDirectory)) |
|
264 | { |
270 | { |
|
265 | Directory.CreateDirectory(Constants.UserApplicationDirectory); |
271 | Directory.CreateDirectory(Constants.UserApplicationDirectory); |
|
266 | } |
272 | } |
|
267 | |
273 | |
|
268 | var deserializationResult = |
274 | var deserializationResult = |
|
269 | await ServersSerialization.Deserialize<global::Servers.Servers>(Constants.ServersFile, |
275 | await ServersSerialization.Deserialize<global::Servers.Servers>(Constants.ServersFile, |
|
270 | "urn:winify-servers-schema", "Servers.xsd"); |
276 | "urn:winify-servers-schema", "Servers.xsd"); |
|
271 | |
277 | |
|
272 | switch (deserializationResult) |
278 | switch (deserializationResult) |
|
273 | { |
279 | { |
|
274 | case SerializationSuccess<global::Servers.Servers> serializationSuccess: |
280 | case SerializationSuccess<global::Servers.Servers> serializationSuccess: |
|
275 | // Decrypt password. |
281 | // Decrypt password. |
|
276 | var deviceId = Miscellaneous.GetMachineGuid(); |
282 | var deviceId = Miscellaneous.GetMachineGuid(); |
|
277 | var @protected = new global::Servers.Servers |
283 | var @protected = new global::Servers.Servers |
|
278 | { |
284 | { |
|
279 | Server = new BindingListWithCollectionChanged<Server>() |
285 | Server = new BindingListWithCollectionChanged<Server>() |
|
280 | }; |
286 | }; |
|
281 | foreach (var server in serializationSuccess.Result.Server) |
287 | foreach (var server in serializationSuccess.Result.Server) |
|
282 | { |
288 | { |
|
283 | var unarmored = Convert.FromBase64String(server.Password); |
289 | var unarmored = Convert.FromBase64String(server.Password); |
|
284 | var decrypted = Encoding.UTF8.GetString(AES.Decrypt(unarmored, deviceId)); |
290 | var decrypted = Encoding.UTF8.GetString(AES.Decrypt(unarmored, deviceId)); |
|
285 | |
291 | |
|
286 | @protected.Server.Add(new Server(server.Name, server.Url, server.Username, decrypted)); |
292 | @protected.Server.Add(new Server(server.Name, server.Url, server.Username, decrypted)); |
|
287 | } |
293 | } |
|
288 | |
294 | |
|
289 | return @protected; |
295 | return @protected; |
|
290 | |
296 | |
|
291 | default: |
297 | default: |
|
292 | return new global::Servers.Servers(); |
298 | return new global::Servers.Servers(); |
|
293 | } |
299 | } |
|
294 | } |
300 | } |
|
295 | |
301 | |
|
296 | #endregion |
302 | #endregion |
|
297 | } |
303 | } |
|
298 | } |
304 | } |
|
299 | |
305 | |
|
300 |
|
306 |
|
|
301 | |
307 | |
|
302 | |
308 | |
|
303 | |
309 | |