Winify – Blame information for rev 21

Subversion Repositories:
Rev:
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;
19 office 5 using System.Diagnostics;
4 office 6 using System.IO;
15 office 7 using System.Text;
19 office 8 using System.Threading;
4 office 9 using System.Threading.Tasks;
1 office 10 using System.Windows.Forms;
11 using AutoUpdaterDotNET;
18 office 12 using Serilog;
15 office 13 using Servers;
1 office 14 using Winify.Gotify;
15 using Winify.Properties;
8 office 16 using Winify.Servers.Serialization;
15 office 17 using Winify.Utilities;
1 office 18  
19 namespace Winify
20 {
21 public partial class Form1 : Form
22 {
23 #region Private Delegates, Events, Enums, Properties, Indexers and Fields
24  
21 office 25 private readonly Announcements.Announcements _announcements;
15 office 26  
8 office 27 private readonly global::Servers.Servers _servers;
4 office 28  
14 office 29 private readonly TaskScheduler _taskScheduler;
30  
1 office 31 private AboutForm _aboutForm;
32  
7 office 33 private GotifyConnectionManager _gotifyConnectionManager;
1 office 34  
11 office 35 private NotificationManager _notificationManager;
36  
1 office 37 private SettingsForm _settingsForm;
38  
39 #endregion
40  
41 #region Constructors, Destructors and Finalizers
42  
19 office 43 public Form1(Mutex mutex)
1 office 44 {
45 InitializeComponent();
46  
18 office 47 Log.Logger = new LoggerConfiguration()
48 .MinimumLevel.Debug()
49 .WriteTo.File(Path.Combine(Constants.UserApplicationDirectory, "Logs", $"{Constants.AssemblyName}.log"),
50 rollingInterval: RollingInterval.Day)
51 .CreateLogger();
52  
1 office 53 // Upgrade settings if required.
54 if (!ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).HasFile)
55 {
19 office 56 if (Settings.Default.UpdateRequired)
57 {
58 Settings.Default.Upgrade();
59 Settings.Default.Reload();
60  
61 Settings.Default.UpdateRequired = false;
62 Settings.Default.Save();
63  
64 mutex.ReleaseMutex();
65 Process.Start(Application.ExecutablePath);
66 Environment.Exit(0);
67 }
1 office 68 }
69  
70 // Bind to settings changed event.
71 Settings.Default.SettingsLoaded += Default_SettingsLoaded;
72 Settings.Default.SettingsSaving += Default_SettingsSaving;
73 Settings.Default.PropertyChanged += Default_PropertyChanged;
74  
8 office 75 _servers = new global::Servers.Servers();
7 office 76 _servers.Server.CollectionChanged += Server_CollectionChanged;
21 office 77 _servers.Server.ListChanged += Server_ListChanged;
78 _announcements = new Announcements.Announcements();
79 _announcements.Announcement.CollectionChanged += Announcements_CollectionChanged;
80 _announcements.Announcement.ListChanged += Announcement_ListChanged;
1 office 81  
14 office 82 _taskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
11 office 83  
14 office 84 _notificationManager = new NotificationManager(_taskScheduler);
85  
7 office 86 _gotifyConnectionManager = new GotifyConnectionManager(_servers);
11 office 87 _gotifyConnectionManager.GotifyNotification += GotifyConnectionManager_GotifyNotification;
7 office 88  
14 office 89 LoadServers().ContinueWith(async task =>
4 office 90 {
14 office 91 var restoredServers = await task;
4 office 92  
7 office 93 foreach (var server in restoredServers.Server)
94 {
95 _servers.Server.Add(server);
96 }
4 office 97 });
15 office 98  
99 LoadAnnouncements().ContinueWith(async task =>
100 {
101 var restoreAnnouncements = await task;
102  
103 foreach (var announcement in restoreAnnouncements.Announcement)
104 {
21 office 105 _announcements.Announcement.Add(announcement);
15 office 106 }
107 });
19 office 108  
109 // Start application update.
110 AutoUpdater.Start("http://winify.grimore.org/update/winify.xml");
1 office 111 }
112  
113 /// <summary>
114 /// Clean up any resources being used.
115 /// </summary>
116 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
117 protected override void Dispose(bool disposing)
118 {
119 if (disposing && components != null)
120 {
7 office 121 _servers.Server.CollectionChanged -= Server_CollectionChanged;
21 office 122 _announcements.Announcement.CollectionChanged -= Announcements_CollectionChanged;
7 office 123  
124 Settings.Default.SettingsLoaded -= Default_SettingsLoaded;
125 Settings.Default.SettingsSaving -= Default_SettingsSaving;
126 Settings.Default.PropertyChanged -= Default_PropertyChanged;
127  
11 office 128 _gotifyConnectionManager.GotifyNotification -= GotifyConnectionManager_GotifyNotification;
7 office 129 _gotifyConnectionManager?.Dispose();
130 _gotifyConnectionManager = null;
131  
11 office 132 _notificationManager?.Dispose();
133 _notificationManager = null;
134  
1 office 135 components.Dispose();
136 }
137  
138 base.Dispose(disposing);
139 }
140  
141 #endregion
142  
143 #region Event Handlers
144  
21 office 145 private async void Announcement_ListChanged(object sender, ListChangedEventArgs e)
146 {
147 await SaveAnnouncements();
148 }
149  
150 private async void Server_ListChanged(object sender, ListChangedEventArgs e)
151 {
152 await SaveServers();
153 }
154  
15 office 155 private async void Announcements_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
156 {
21 office 157 await SaveAnnouncements();
15 office 158 }
159  
11 office 160 private void GotifyConnectionManager_GotifyNotification(object sender, GotifyNotificationEventArgs e)
161 {
21 office 162 _notificationManager.ShowNotification(e, _announcements);
11 office 163 }
164  
7 office 165 private async void Server_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
166 {
21 office 167 await SaveServers();
7 office 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  
21 office 190 _settingsForm = new SettingsForm(_servers, _announcements);
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  
21 office 247 private async Task SaveAnnouncements()
248 {
249 switch (await ServersSerialization.Serialize(_announcements, Constants.NotificationsFile, "Announcements",
250 "<!ATTLIST Announcements xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>"))
251 {
252 case SerializationFailure serializationFailure:
253 Log.Warning(serializationFailure.Exception, "Unable to serialize announcements.");
254 break;
255 }
256 }
257  
258 private async Task SaveServers()
259 {
260 // Encrypt password for all servers.
261 var deviceId = Miscellaneous.GetMachineGuid();
262 var @protected = new global::Servers.Servers
263 {
264 Server = new BindingListWithCollectionChanged<Server>()
265 };
266 foreach (var server in _servers.Server)
267 {
268 var encrypted = AES.Encrypt(Encoding.UTF8.GetBytes(server.Password), deviceId);
269 var armored = Convert.ToBase64String(encrypted);
270  
271 @protected.Server.Add(new Server(server.Name, server.Url, server.Username, armored));
272 }
273  
274 switch (await ServersSerialization.Serialize(@protected, Constants.ServersFile, "Servers",
275 "<!ATTLIST Servers xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>"))
276 {
277 case SerializationFailure serializationFailure:
278 Log.Warning(serializationFailure.Exception, "Unable to serialize servers.");
279 break;
280 }
281 }
282  
15 office 283 private static async Task<Announcements.Announcements> LoadAnnouncements()
284 {
285 if (!Directory.Exists(Constants.UserApplicationDirectory))
286 {
287 Directory.CreateDirectory(Constants.UserApplicationDirectory);
288 }
289  
290 var deserializationResult =
291 await ServersSerialization.Deserialize<Announcements.Announcements>(Constants.NotificationsFile,
292 "urn:winify-announcements-schema", "Announcements.xsd");
293  
294 switch (deserializationResult)
295 {
296 case SerializationSuccess<Announcements.Announcements> serializationSuccess:
297 return serializationSuccess.Result;
21 office 298 case SerializationFailure serializationFailure:
299 Log.Warning(serializationFailure.Exception, "Unable to load announcements.");
300 return new Announcements.Announcements();
15 office 301 default:
302 return new Announcements.Announcements();
303 }
304 }
305  
8 office 306 private static async Task<global::Servers.Servers> LoadServers()
4 office 307 {
308 if (!Directory.Exists(Constants.UserApplicationDirectory))
309 {
310 Directory.CreateDirectory(Constants.UserApplicationDirectory);
311 }
312  
15 office 313 var deserializationResult =
314 await ServersSerialization.Deserialize<global::Servers.Servers>(Constants.ServersFile,
315 "urn:winify-servers-schema", "Servers.xsd");
4 office 316  
317 switch (deserializationResult)
318 {
15 office 319 case SerializationSuccess<global::Servers.Servers> serializationSuccess:
320 // Decrypt password.
321 var deviceId = Miscellaneous.GetMachineGuid();
322 var @protected = new global::Servers.Servers
323 {
324 Server = new BindingListWithCollectionChanged<Server>()
325 };
326 foreach (var server in serializationSuccess.Result.Server)
327 {
328 var unarmored = Convert.FromBase64String(server.Password);
329 var decrypted = Encoding.UTF8.GetString(AES.Decrypt(unarmored, deviceId));
4 office 330  
15 office 331 @protected.Server.Add(new Server(server.Name, server.Url, server.Username, decrypted));
332 }
333  
334 return @protected;
335  
21 office 336 case SerializationFailure serializationFailure:
337 Log.Warning(serializationFailure.Exception, "Unable to load servers.");
338 return new global::Servers.Servers();
339  
4 office 340 default:
8 office 341 return new global::Servers.Servers();
4 office 342 }
343 }
344  
345 #endregion
1 office 346 }
347 }