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