Winify – Blame information for rev 20

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  
15 office 25 private readonly Announcements.Announcements _notifications;
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;
15 office 77 _notifications = new Announcements.Announcements();
78 _notifications.Announcement.CollectionChanged += Announcements_CollectionChanged;
1 office 79  
14 office 80 _taskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
11 office 81  
14 office 82 _notificationManager = new NotificationManager(_taskScheduler);
83  
7 office 84 _gotifyConnectionManager = new GotifyConnectionManager(_servers);
11 office 85 _gotifyConnectionManager.GotifyNotification += GotifyConnectionManager_GotifyNotification;
7 office 86  
14 office 87 LoadServers().ContinueWith(async task =>
4 office 88 {
14 office 89 var restoredServers = await task;
4 office 90  
7 office 91 foreach (var server in restoredServers.Server)
92 {
93 _servers.Server.Add(server);
94 }
4 office 95 });
15 office 96  
97 LoadAnnouncements().ContinueWith(async task =>
98 {
99 var restoreAnnouncements = await task;
100  
101 foreach (var announcement in restoreAnnouncements.Announcement)
102 {
103 _notifications.Announcement.Add(announcement);
104 }
105 });
19 office 106  
107 // Start application update.
108 AutoUpdater.Start("http://winify.grimore.org/update/winify.xml");
1 office 109 }
110  
111 /// <summary>
112 /// Clean up any resources being used.
113 /// </summary>
114 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
115 protected override void Dispose(bool disposing)
116 {
117 if (disposing && components != null)
118 {
7 office 119 _servers.Server.CollectionChanged -= Server_CollectionChanged;
15 office 120 _notifications.Announcement.CollectionChanged -= Announcements_CollectionChanged;
7 office 121  
122 Settings.Default.SettingsLoaded -= Default_SettingsLoaded;
123 Settings.Default.SettingsSaving -= Default_SettingsSaving;
124 Settings.Default.PropertyChanged -= Default_PropertyChanged;
125  
11 office 126 _gotifyConnectionManager.GotifyNotification -= GotifyConnectionManager_GotifyNotification;
7 office 127 _gotifyConnectionManager?.Dispose();
128 _gotifyConnectionManager = null;
129  
11 office 130 _notificationManager?.Dispose();
131 _notificationManager = null;
132  
1 office 133 components.Dispose();
134 }
135  
136 base.Dispose(disposing);
137 }
138  
139 #endregion
140  
141 #region Event Handlers
142  
15 office 143 private async void Announcements_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
144 {
145 switch (await ServersSerialization.Serialize(_notifications, Constants.NotificationsFile, "Announcements",
146 "<!ATTLIST Announcements xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>"))
147 {
148 case SerializationFailure serializationFailure:
18 office 149 Log.Warning(serializationFailure.Exception, "Unable to serialize announcements.");
15 office 150 break;
151 }
152 }
153  
11 office 154 private void GotifyConnectionManager_GotifyNotification(object sender, GotifyNotificationEventArgs e)
155 {
15 office 156 _notificationManager.ShowNotification(e, _notifications);
11 office 157 }
158  
7 office 159 private async void Server_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
160 {
15 office 161 // Encrypt password for all servers.
162 var deviceId = Miscellaneous.GetMachineGuid();
163 var @protected = new global::Servers.Servers
7 office 164 {
15 office 165 Server = new BindingListWithCollectionChanged<Server>()
166 };
167 foreach (var server in _servers.Server)
168 {
169 var encrypted = AES.Encrypt(Encoding.UTF8.GetBytes(server.Password), deviceId);
170 var armored = Convert.ToBase64String(encrypted);
171  
172 @protected.Server.Add(new Server(server.Name, server.Url, server.Username, armored));
173 }
174  
175 switch (await ServersSerialization.Serialize(@protected, Constants.ServersFile, "Servers",
176 "<!ATTLIST Servers xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>"))
177 {
178 case SerializationFailure serializationFailure:
18 office 179 Log.Warning(serializationFailure.Exception, "Unable to serialize servers.");
7 office 180 break;
181 }
182 }
183  
1 office 184 private static void Default_PropertyChanged(object sender, PropertyChangedEventArgs e)
185 {
186 Settings.Default.Save();
187 }
188  
189 private static void Default_SettingsSaving(object sender, CancelEventArgs e)
190 {
191 }
192  
193 private static void Default_SettingsLoaded(object sender, SettingsLoadedEventArgs e)
194 {
195 }
196  
197 private void SettingsToolStripMenuItem_Click(object sender, EventArgs e)
198 {
199 if (_settingsForm != null)
200 {
201 return;
202 }
203  
15 office 204 _settingsForm = new SettingsForm(_servers, _notifications);
1 office 205 _settingsForm.Closing += SettingsForm_Closing;
206 _settingsForm.Show();
207 }
208  
209 private void SettingsForm_Closing(object sender, CancelEventArgs e)
210 {
5 office 211 if (_settingsForm == null)
1 office 212 {
213 return;
214 }
6 office 215  
1 office 216 _settingsForm.Closing -= SettingsForm_Closing;
217 _settingsForm.Dispose();
218 _settingsForm = null;
219 }
220  
221 private void AboutToolStripMenuItem_Click(object sender, EventArgs e)
222 {
223 if (_aboutForm != null)
224 {
225 return;
226 }
227  
228 _aboutForm = new AboutForm();
229 _aboutForm.Closing += AboutForm_Closing;
230 _aboutForm.Show();
231 }
232  
233 private void AboutForm_Closing(object sender, CancelEventArgs e)
234 {
235 if (_aboutForm == null)
236 {
237 return;
238 }
239  
240 _aboutForm.Closing -= AboutForm_Closing;
241 _aboutForm.Dispose();
242 _aboutForm = null;
243 }
244  
245 private void QuitToolStripMenuItem_Click(object sender, EventArgs e)
246 {
17 office 247 Close();
248  
249 Environment.Exit(0);
1 office 250 }
251  
9 office 252 private void UpdateToolStripMenuItem_Click(object sender, EventArgs e)
253 {
254 AutoUpdater.Start("http://winify.grimore.org/update/winify.xml");
255 }
256  
1 office 257 #endregion
4 office 258  
259 #region Private Methods
260  
15 office 261 private static async Task<Announcements.Announcements> LoadAnnouncements()
262 {
263 if (!Directory.Exists(Constants.UserApplicationDirectory))
264 {
265 Directory.CreateDirectory(Constants.UserApplicationDirectory);
266 }
267  
268 var deserializationResult =
269 await ServersSerialization.Deserialize<Announcements.Announcements>(Constants.NotificationsFile,
270 "urn:winify-announcements-schema", "Announcements.xsd");
271  
272 switch (deserializationResult)
273 {
274 case SerializationSuccess<Announcements.Announcements> serializationSuccess:
275 return serializationSuccess.Result;
276 default:
277 return new Announcements.Announcements();
278 }
279 }
280  
8 office 281 private static async Task<global::Servers.Servers> LoadServers()
4 office 282 {
283 if (!Directory.Exists(Constants.UserApplicationDirectory))
284 {
285 Directory.CreateDirectory(Constants.UserApplicationDirectory);
286 }
287  
15 office 288 var deserializationResult =
289 await ServersSerialization.Deserialize<global::Servers.Servers>(Constants.ServersFile,
290 "urn:winify-servers-schema", "Servers.xsd");
4 office 291  
292 switch (deserializationResult)
293 {
15 office 294 case SerializationSuccess<global::Servers.Servers> serializationSuccess:
295 // Decrypt password.
296 var deviceId = Miscellaneous.GetMachineGuid();
297 var @protected = new global::Servers.Servers
298 {
299 Server = new BindingListWithCollectionChanged<Server>()
300 };
301 foreach (var server in serializationSuccess.Result.Server)
302 {
303 var unarmored = Convert.FromBase64String(server.Password);
304 var decrypted = Encoding.UTF8.GetString(AES.Decrypt(unarmored, deviceId));
4 office 305  
15 office 306 @protected.Server.Add(new Server(server.Name, server.Url, server.Username, decrypted));
307 }
308  
309 return @protected;
310  
4 office 311 default:
8 office 312 return new global::Servers.Servers();
4 office 313 }
314 }
315  
316 #endregion
1 office 317 }
318 }