Winify – Blame information for rev 15

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