Winify – Blame information for rev
?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; |
||
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 | |||
67 | Close(); |
||
68 | Environment.Exit(0); |
||
69 | } |
||
1 | office | 70 | } |
71 | |||
72 | // Bind to settings changed event. |
||
73 | Settings.Default.SettingsLoaded += Default_SettingsLoaded; |
||
74 | Settings.Default.SettingsSaving += Default_SettingsSaving; |
||
75 | Settings.Default.PropertyChanged += Default_PropertyChanged; |
||
76 | |||
8 | office | 77 | _servers = new global::Servers.Servers(); |
7 | office | 78 | _servers.Server.CollectionChanged += Server_CollectionChanged; |
15 | office | 79 | _notifications = new Announcements.Announcements(); |
80 | _notifications.Announcement.CollectionChanged += Announcements_CollectionChanged; |
||
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 | { |
||
105 | _notifications.Announcement.Add(announcement); |
||
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; |
15 | office | 122 | _notifications.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 | |||
15 | office | 145 | private async void Announcements_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) |
146 | { |
||
147 | switch (await ServersSerialization.Serialize(_notifications, Constants.NotificationsFile, "Announcements", |
||
148 | "<!ATTLIST Announcements xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>")) |
||
149 | { |
||
150 | case SerializationFailure serializationFailure: |
||
18 | office | 151 | Log.Warning(serializationFailure.Exception, "Unable to serialize announcements."); |
15 | office | 152 | break; |
153 | } |
||
154 | } |
||
155 | |||
11 | office | 156 | private void GotifyConnectionManager_GotifyNotification(object sender, GotifyNotificationEventArgs e) |
157 | { |
||
15 | office | 158 | _notificationManager.ShowNotification(e, _notifications); |
11 | office | 159 | } |
160 | |||
7 | office | 161 | private async void Server_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) |
162 | { |
||
15 | office | 163 | // Encrypt password for all servers. |
164 | var deviceId = Miscellaneous.GetMachineGuid(); |
||
165 | var @protected = new global::Servers.Servers |
||
7 | office | 166 | { |
15 | office | 167 | Server = new BindingListWithCollectionChanged<Server>() |
168 | }; |
||
169 | foreach (var server in _servers.Server) |
||
170 | { |
||
171 | var encrypted = AES.Encrypt(Encoding.UTF8.GetBytes(server.Password), deviceId); |
||
172 | var armored = Convert.ToBase64String(encrypted); |
||
173 | |||
174 | @protected.Server.Add(new Server(server.Name, server.Url, server.Username, armored)); |
||
175 | } |
||
176 | |||
177 | switch (await ServersSerialization.Serialize(@protected, Constants.ServersFile, "Servers", |
||
178 | "<!ATTLIST Servers xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>")) |
||
179 | { |
||
180 | case SerializationFailure serializationFailure: |
||
18 | office | 181 | Log.Warning(serializationFailure.Exception, "Unable to serialize servers."); |
7 | office | 182 | break; |
183 | } |
||
184 | } |
||
185 | |||
1 | office | 186 | private static void Default_PropertyChanged(object sender, PropertyChangedEventArgs e) |
187 | { |
||
188 | Settings.Default.Save(); |
||
189 | } |
||
190 | |||
191 | private static void Default_SettingsSaving(object sender, CancelEventArgs e) |
||
192 | { |
||
193 | } |
||
194 | |||
195 | private static void Default_SettingsLoaded(object sender, SettingsLoadedEventArgs e) |
||
196 | { |
||
197 | } |
||
198 | |||
199 | private void SettingsToolStripMenuItem_Click(object sender, EventArgs e) |
||
200 | { |
||
201 | if (_settingsForm != null) |
||
202 | { |
||
203 | return; |
||
204 | } |
||
205 | |||
15 | office | 206 | _settingsForm = new SettingsForm(_servers, _notifications); |
1 | office | 207 | _settingsForm.Closing += SettingsForm_Closing; |
208 | _settingsForm.Show(); |
||
209 | } |
||
210 | |||
211 | private void SettingsForm_Closing(object sender, CancelEventArgs e) |
||
212 | { |
||
5 | office | 213 | if (_settingsForm == null) |
1 | office | 214 | { |
215 | return; |
||
216 | } |
||
6 | office | 217 | |
1 | office | 218 | _settingsForm.Closing -= SettingsForm_Closing; |
219 | _settingsForm.Dispose(); |
||
220 | _settingsForm = null; |
||
221 | } |
||
222 | |||
223 | private void AboutToolStripMenuItem_Click(object sender, EventArgs e) |
||
224 | { |
||
225 | if (_aboutForm != null) |
||
226 | { |
||
227 | return; |
||
228 | } |
||
229 | |||
230 | _aboutForm = new AboutForm(); |
||
231 | _aboutForm.Closing += AboutForm_Closing; |
||
232 | _aboutForm.Show(); |
||
233 | } |
||
234 | |||
235 | private void AboutForm_Closing(object sender, CancelEventArgs e) |
||
236 | { |
||
237 | if (_aboutForm == null) |
||
238 | { |
||
239 | return; |
||
240 | } |
||
241 | |||
242 | _aboutForm.Closing -= AboutForm_Closing; |
||
243 | _aboutForm.Dispose(); |
||
244 | _aboutForm = null; |
||
245 | } |
||
246 | |||
247 | private void QuitToolStripMenuItem_Click(object sender, EventArgs e) |
||
248 | { |
||
17 | office | 249 | Close(); |
250 | |||
251 | Environment.Exit(0); |
||
1 | office | 252 | } |
253 | |||
9 | office | 254 | private void UpdateToolStripMenuItem_Click(object sender, EventArgs e) |
255 | { |
||
256 | AutoUpdater.Start("http://winify.grimore.org/update/winify.xml"); |
||
257 | } |
||
258 | |||
1 | office | 259 | #endregion |
4 | office | 260 | |
261 | #region Private Methods |
||
262 | |||
15 | office | 263 | private static async Task<Announcements.Announcements> LoadAnnouncements() |
264 | { |
||
265 | if (!Directory.Exists(Constants.UserApplicationDirectory)) |
||
266 | { |
||
267 | Directory.CreateDirectory(Constants.UserApplicationDirectory); |
||
268 | } |
||
269 | |||
270 | var deserializationResult = |
||
271 | await ServersSerialization.Deserialize<Announcements.Announcements>(Constants.NotificationsFile, |
||
272 | "urn:winify-announcements-schema", "Announcements.xsd"); |
||
273 | |||
274 | switch (deserializationResult) |
||
275 | { |
||
276 | case SerializationSuccess<Announcements.Announcements> serializationSuccess: |
||
277 | return serializationSuccess.Result; |
||
278 | default: |
||
279 | return new Announcements.Announcements(); |
||
280 | } |
||
281 | } |
||
282 | |||
8 | office | 283 | private static async Task<global::Servers.Servers> LoadServers() |
4 | office | 284 | { |
285 | if (!Directory.Exists(Constants.UserApplicationDirectory)) |
||
286 | { |
||
287 | Directory.CreateDirectory(Constants.UserApplicationDirectory); |
||
288 | } |
||
289 | |||
15 | office | 290 | var deserializationResult = |
291 | await ServersSerialization.Deserialize<global::Servers.Servers>(Constants.ServersFile, |
||
292 | "urn:winify-servers-schema", "Servers.xsd"); |
||
4 | office | 293 | |
294 | switch (deserializationResult) |
||
295 | { |
||
15 | office | 296 | case SerializationSuccess<global::Servers.Servers> serializationSuccess: |
297 | // Decrypt password. |
||
298 | var deviceId = Miscellaneous.GetMachineGuid(); |
||
299 | var @protected = new global::Servers.Servers |
||
300 | { |
||
301 | Server = new BindingListWithCollectionChanged<Server>() |
||
302 | }; |
||
303 | foreach (var server in serializationSuccess.Result.Server) |
||
304 | { |
||
305 | var unarmored = Convert.FromBase64String(server.Password); |
||
306 | var decrypted = Encoding.UTF8.GetString(AES.Decrypt(unarmored, deviceId)); |
||
4 | office | 307 | |
15 | office | 308 | @protected.Server.Add(new Server(server.Name, server.Url, server.Username, decrypted)); |
309 | } |
||
310 | |||
311 | return @protected; |
||
312 | |||
4 | office | 313 | default: |
8 | office | 314 | return new global::Servers.Servers(); |
4 | office | 315 | } |
316 | } |
||
317 | |||
318 | #endregion |
||
1 | office | 319 | } |
320 | } |