Winify – Diff between revs 22 and 24

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