Winify – Diff between revs 24 and 25
?pathlinks?
Rev 24 | Rev 25 | |||
---|---|---|---|---|
Line 1... | Line 1... | |||
1 | using System; |
1 | using System; |
|
2 | using System.Collections.Specialized; |
2 | using System.Collections.Concurrent; |
|
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; |
- | ||
8 | using System.Text; |
7 | using System.Text; |
|
9 | using System.Threading; |
8 | using System.Threading; |
|
10 | using System.Threading.Tasks; |
9 | using System.Threading.Tasks; |
|
11 | using System.Windows.Forms; |
10 | using System.Windows.Forms; |
|
12 | using AutoUpdaterDotNET; |
11 | using AutoUpdaterDotNET; |
|
13 | using Serilog; |
12 | using Serilog; |
|
14 | using Servers; |
13 | using Servers; |
|
15 | using ToastNotifications; |
14 | using ToastNotifications; |
|
16 | using Winify.Gotify; |
15 | using Winify.Gotify; |
|
17 | using Winify.Properties; |
- | ||
18 | using Winify.Servers.Serialization; |
16 | using Winify.Servers.Serialization; |
|
- | 17 | using Winify.Settings; |
||
19 | using Winify.Utilities; |
18 | using Winify.Utilities; |
|
Line 20... | Line 19... | |||
20 | |
19 | |
|
21 | namespace Winify |
20 | namespace Winify |
|
22 | { |
21 | { |
|
23 | public partial class Form1 : Form |
22 | public partial class Form1 : Form |
|
24 | { |
23 | { |
|
Line 25... | Line -... | |||
25 | #region Private Delegates, Events, Enums, Properties, Indexers and Fields |
- | ||
26 | |
- | ||
27 | private readonly Announcements.Announcements _announcements; |
- | ||
28 | |
- | ||
29 | private readonly global::Servers.Servers _servers; |
24 | #region Private Delegates, Events, Enums, Properties, Indexers and Fields |
|
Line 30... | Line 25... | |||
30 | |
25 | |
|
Line 31... | Line 26... | |||
31 | private readonly TaskScheduler _uiTaskScheduler; |
26 | private readonly TaskScheduler _uiTaskScheduler; |
|
Line 32... | Line 27... | |||
32 | |
27 | |
|
Line 33... | Line 28... | |||
33 | private AboutForm _aboutForm; |
28 | private AboutForm _aboutForm; |
|
Line 51... | Line 46... | |||
51 | .CreateLogger(); |
46 | .CreateLogger(); |
|
Line 52... | Line 47... | |||
52 | |
47 | |
|
53 | // Upgrade settings if required. |
48 | // Upgrade settings if required. |
|
54 | if (!ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).HasFile) |
49 | if (!ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).HasFile) |
|
55 | { |
50 | { |
|
56 | if (Settings.Default.UpdateRequired) |
51 | if (Properties.Settings.Default.UpdateRequired) |
|
57 | { |
52 | { |
|
58 | Settings.Default.Upgrade(); |
53 | Properties.Settings.Default.Upgrade(); |
|
Line 59... | Line 54... | |||
59 | Settings.Default.Reload(); |
54 | Properties.Settings.Default.Reload(); |
|
60 | |
55 | |
|
Line 61... | Line 56... | |||
61 | Settings.Default.UpdateRequired = false; |
56 | Properties.Settings.Default.UpdateRequired = false; |
|
62 | Settings.Default.Save(); |
57 | Properties.Settings.Default.Save(); |
|
63 | |
58 | |
|
64 | mutex.ReleaseMutex(); |
59 | mutex.ReleaseMutex(); |
|
65 | Process.Start(Application.ExecutablePath); |
60 | Process.Start(Application.ExecutablePath); |
|
Line 66... | Line 61... | |||
66 | Environment.Exit(0); |
61 | Environment.Exit(0); |
|
67 | } |
62 | } |
|
68 | } |
63 | } |
|
69 | |
64 | |
|
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 | |
- | ||
75 | _servers = new global::Servers.Servers(); |
- | ||
76 | _servers.Server.CollectionChanged += Server_CollectionChanged; |
- | ||
Line -... | Line 65... | |||
- | 65 | // Bind to settings changed event. |
||
77 | _servers.Server.ListChanged += Server_ListChanged; |
66 | Properties.Settings.Default.SettingsLoaded += Default_SettingsLoaded; |
|
Line 78... | Line -... | |||
78 | _announcements = new Announcements.Announcements(); |
- | ||
79 | _announcements.Announcement.CollectionChanged += Announcements_CollectionChanged; |
- | ||
80 | _announcements.Announcement.ListChanged += Announcement_ListChanged; |
- | ||
81 | |
67 | Properties.Settings.Default.SettingsSaving += Default_SettingsSaving; |
|
82 | _uiTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext(); |
68 | Properties.Settings.Default.PropertyChanged += Default_PropertyChanged; |
|
83 | |
69 | |
|
Line 84... | Line 70... | |||
84 | _gotifyConnectionManager = new GotifyConnectionManager(_servers); |
70 | // Store UI thread context. |
|
85 | _gotifyConnectionManager.GotifyNotification += GotifyConnectionManager_GotifyNotification; |
- | ||
86 | |
- | ||
87 | LoadServers().ContinueWith(async task => |
- | ||
88 | { |
- | ||
Line 89... | Line -... | |||
89 | var restoredServers = await task; |
- | ||
90 | |
- | ||
91 | foreach (var server in restoredServers.Server) |
- | ||
92 | { |
- | ||
93 | _servers.Server.Add(server); |
71 | _uiTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext(); |
|
94 | } |
72 | |
|
- | 73 | LoadServers().ContinueWith(async task => |
||
- | 74 | { |
||
- | 75 | var restoredServers = await task; |
||
95 | }); |
76 | |
|
96 | |
77 | _gotifyConnections = new ConcurrentBag<GotifyConnection>(); |
|
97 | LoadAnnouncements().ContinueWith(async task => |
78 | |
|
Line 98... | Line 79... | |||
98 | { |
79 | foreach (var server in restoredServers.Server) |
|
99 | var restoreAnnouncements = await task; |
80 | { |
|
Line 114... | Line 95... | |||
114 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> |
95 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> |
|
115 | protected override void Dispose(bool disposing) |
96 | protected override void Dispose(bool disposing) |
|
116 | { |
97 | { |
|
117 | if (disposing && components != null) |
98 | if (disposing && components != null) |
|
118 | { |
99 | { |
|
119 | _servers.Server.CollectionChanged -= Server_CollectionChanged; |
- | ||
120 | _announcements.Announcement.CollectionChanged -= Announcements_CollectionChanged; |
- | ||
121 | |
- | ||
122 | Settings.Default.SettingsLoaded -= Default_SettingsLoaded; |
100 | Properties.Settings.Default.SettingsLoaded -= Default_SettingsLoaded; |
|
123 | Settings.Default.SettingsSaving -= Default_SettingsSaving; |
101 | Properties.Settings.Default.SettingsSaving -= Default_SettingsSaving; |
|
124 | Settings.Default.PropertyChanged -= Default_PropertyChanged; |
102 | Properties.Settings.Default.PropertyChanged -= Default_PropertyChanged; |
|
125 | |
- | ||
126 | _gotifyConnectionManager.GotifyNotification -= GotifyConnectionManager_GotifyNotification; |
- | ||
127 | _gotifyConnectionManager?.Dispose(); |
- | ||
128 | _gotifyConnectionManager = null; |
- | ||
Line 129... | Line 103... | |||
129 | |
103 | |
|
130 | components.Dispose(); |
104 | components.Dispose(); |
|
Line 131... | Line 105... | |||
131 | } |
105 | } |
|
Line 135... | Line 109... | |||
135 | |
109 | |
|
Line 136... | Line 110... | |||
136 | #endregion |
110 | #endregion |
|
Line 137... | Line 111... | |||
137 | |
111 | |
|
- | 112 | #region Event Handlers |
||
- | 113 | |
||
- | 114 | private static void Default_PropertyChanged(object sender, PropertyChangedEventArgs e) |
||
- | 115 | { |
||
- | 116 | Properties.Settings.Default.Save(); |
||
138 | #region Event Handlers |
117 | } |
|
139 | |
- | ||
140 | private async void Announcement_ListChanged(object sender, ListChangedEventArgs e) |
118 | |
|
Line 141... | Line 119... | |||
141 | { |
119 | private static void Default_SettingsSaving(object sender, CancelEventArgs e) |
|
142 | await SaveAnnouncements(); |
120 | { |
|
143 | } |
- | ||
144 | |
121 | } |
|
Line 145... | Line 122... | |||
145 | private async void Server_ListChanged(object sender, ListChangedEventArgs e) |
122 | |
|
146 | { |
123 | private static void Default_SettingsLoaded(object sender, SettingsLoadedEventArgs e) |
|
- | 124 | { |
||
- | 125 | } |
||
- | 126 | |
||
- | 127 | private async void SettingsToolStripMenuItem_Click(object sender, EventArgs e) |
||
- | 128 | { |
||
- | 129 | if (_settingsForm != null) |
||
147 | await SaveServers(); |
130 | { |
|
- | 131 | return; |
||
- | 132 | } |
||
- | 133 | |
||
- | 134 | var servers = await LoadServers(); |
||
- | 135 | var announcements = await LoadAnnouncements(); |
||
- | 136 | |
||
- | 137 | _settingsForm = new SettingsForm(servers, announcements); |
||
- | 138 | _settingsForm.Save += SettingsForm_Save; |
||
- | 139 | _settingsForm.Closing += SettingsForm_Closing; |
||
- | 140 | _settingsForm.Show(); |
||
- | 141 | } |
||
- | 142 | |
||
- | 143 | private async void SettingsForm_Save(object sender, SettingsSavedEventArgs e) |
||
- | 144 | { |
||
- | 145 | // Save the servers. |
||
- | 146 | await Task.WhenAll(SaveServers(e.Servers), SaveAnnouncements(e.Announcements)); |
||
- | 147 | |
||
- | 148 | // Update connections to gotify servers. |
||
- | 149 | while (_gotifyConnections.TryTake(out var gotifyConnection)) |
||
- | 150 | { |
||
- | 151 | gotifyConnection.GotifyNotification -= GotifyConnection_GotifyNotification; |
||
- | 152 | gotifyConnection.Stop(); |
||
- | 153 | gotifyConnection.Dispose(); |
||
- | 154 | gotifyConnection = null; |
||
- | 155 | } |
||
- | 156 | |
||
- | 157 | foreach (var server in e.Servers.Server) |
||
- | 158 | { |
||
148 | } |
159 | var gotifyConnection = new GotifyConnection(server); |
|
Line 149... | Line 160... | |||
149 | |
160 | gotifyConnection.GotifyNotification += GotifyConnection_GotifyNotification; |
|
150 | private async void Announcements_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) |
161 | gotifyConnection.Start(); |
|
151 | { |
162 | _gotifyConnections.Add(gotifyConnection); |
|
152 | await SaveAnnouncements(); |
163 | } |
|
- | 164 | } |
||
- | 165 | |
||
153 | } |
166 | private void GotifyConnection_GotifyNotification(object sender, GotifyNotificationEventArgs e) |
|
154 | |
167 | { |
|
155 | private void GotifyConnectionManager_GotifyNotification(object sender, GotifyNotificationEventArgs e) |
168 | Task.Factory.StartNew(async () => |
|
156 | { |
169 | { |
|
- | 170 | var announcements = await LoadAnnouncements(); |
||
157 | Task.Factory.StartNew(() => |
171 | |
|
158 | { |
172 | foreach (var announcement in announcements.Announcement) |
|
- | 173 | { |
||
159 | foreach (var announcement in _announcements.Announcement) |
174 | if (announcement.AppId == e.Notification.AppId) |
|
Line 160... | Line 175... | |||
160 | { |
175 | { |
|
Line 161... | Line 176... | |||
161 | if (announcement.AppId == e.Notification.AppId) |
176 | var configuredNotification = new Notification( |
|
162 | { |
177 | $"{e.Notification.Title} ({e.Notification.Server.Name}/{e.Notification.AppId})", |
|
163 | var configuredNotification = new Notification($"{e.Notification.Title} ({e.Notification.Server.Name}/{e.Notification.AppId})", |
178 | e.Notification.Message, announcement.LingerTime, e.Image, |
|
Line -... | Line 179... | |||
- | 179 | FormAnimator.AnimationMethod.Slide, |
||
164 | e.Notification.Message, announcement.LingerTime, e.Image, FormAnimator.AnimationMethod.Slide, |
180 | FormAnimator.AnimationDirection.Up); |
|
165 | FormAnimator.AnimationDirection.Up); |
181 | |
|
166 | |
182 | configuredNotification.Show(); |
|
Line 167... | Line 183... | |||
167 | configuredNotification.Show(); |
183 | |
|
168 | |
184 | return; |
|
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); |
- | ||
179 | } |
- | ||
180 | |
- | ||
181 | private async void Server_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) |
- | ||
182 | { |
- | ||
183 | await SaveServers(); |
- | ||
184 | } |
- | ||
185 | |
- | ||
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) |
185 | } |
|
Line 200... | Line 186... | |||
200 | { |
186 | } |
|
201 | if (_settingsForm != null) |
187 | |
|
202 | { |
188 | var notification = new Notification( |
|
203 | return; |
189 | $"{e.Notification.Title} ({e.Notification.Server.Name}/{e.Notification.AppId})", |
|
204 | } |
190 | e.Notification.Message, 5000, e.Image, FormAnimator.AnimationMethod.Slide, |
|
205 | |
191 | FormAnimator.AnimationDirection.Up); |
|
Line -... | Line 192... | |||
- | 192 | |
||
206 | _settingsForm = new SettingsForm(_servers, _announcements); |
193 | notification.Show(); |
|
207 | _settingsForm.Closing += SettingsForm_Closing; |
194 | }, CancellationToken.None, TaskCreationOptions.LongRunning, _uiTaskScheduler); |
|
208 | _settingsForm.Show(); |
195 | } |
|
209 | } |
196 | |
|
Line 258... | Line 245... | |||
258 | |
245 | |
|
Line 259... | Line 246... | |||
259 | #endregion |
246 | #endregion |
|
Line 260... | Line 247... | |||
260 | |
247 | |
|
261 | #region Private Methods |
248 | #region Private Methods |
|
262 | |
249 | |
|
263 | private async Task SaveAnnouncements() |
250 | private static async Task SaveAnnouncements(Announcements.Announcements announcements) |
|
264 | { |
251 | { |
|
265 | switch (await ServersSerialization.Serialize(_announcements, Constants.NotificationsFile, "Announcements", |
252 | switch (await ServersSerialization.Serialize(announcements, Constants.AnnouncementsFile, "Announcements", |
|
266 | "<!ATTLIST Announcements xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>")) |
253 | "<!ATTLIST Announcements xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>")) |
|
267 | { |
254 | { |
|
268 | case SerializationFailure serializationFailure: |
255 | case SerializationFailure serializationFailure: |
|
269 | Log.Warning(serializationFailure.Exception, "Unable to serialize announcements."); |
256 | Log.Warning(serializationFailure.Exception, "Unable to serialize announcements."); |
|
Line 270... | Line 257... | |||
270 | break; |
257 | break; |
|
271 | } |
258 | } |
|
272 | } |
259 | } |
|
273 | |
260 | |
|
274 | private async Task SaveServers() |
261 | private static async Task SaveServers(global::Servers.Servers servers) |
|
275 | { |
262 | { |
|
276 | // Encrypt password for all servers. |
263 | // Encrypt password for all servers. |
|
277 | var deviceId = Miscellaneous.GetMachineGuid(); |
264 | var deviceId = Miscellaneous.GetMachineGuid(); |
|
278 | var @protected = new global::Servers.Servers |
265 | var @protected = new global::Servers.Servers |
|
279 | { |
266 | { |
|
280 | Server = new BindingListWithCollectionChanged<Server>() |
267 | Server = new BindingListWithCollectionChanged<Server>() |
|
281 | }; |
268 | }; |
|
Line 282... | Line 269... | |||
282 | foreach (var server in _servers.Server) |
269 | foreach (var server in servers.Server) |
|
Line 302... | Line 289... | |||
302 | { |
289 | { |
|
303 | Directory.CreateDirectory(Constants.UserApplicationDirectory); |
290 | Directory.CreateDirectory(Constants.UserApplicationDirectory); |
|
304 | } |
291 | } |
|
Line 305... | Line 292... | |||
305 | |
292 | |
|
306 | var deserializationResult = |
293 | var deserializationResult = |
|
307 | await ServersSerialization.Deserialize<Announcements.Announcements>(Constants.NotificationsFile, |
294 | await ServersSerialization.Deserialize<Announcements.Announcements>(Constants.AnnouncementsFile, |
|
Line 308... | Line 295... | |||
308 | "urn:winify-announcements-schema", "Announcements.xsd"); |
295 | "urn:winify-announcements-schema", "Announcements.xsd"); |
|
309 | |
296 | |
|
310 | switch (deserializationResult) |
297 | switch (deserializationResult) |