/trunk/Winify/Form1.cs |
@@ -1,10 +1,9 @@ |
using System; |
using System.Collections.Specialized; |
using System.Collections.Concurrent; |
using System.ComponentModel; |
using System.Configuration; |
using System.Diagnostics; |
using System.IO; |
using System.Linq; |
using System.Text; |
using System.Threading; |
using System.Threading.Tasks; |
@@ -14,8 +13,8 @@ |
using Servers; |
using ToastNotifications; |
using Winify.Gotify; |
using Winify.Properties; |
using Winify.Servers.Serialization; |
using Winify.Settings; |
using Winify.Utilities; |
|
namespace Winify |
@@ -24,15 +23,11 @@ |
{ |
#region Private Delegates, Events, Enums, Properties, Indexers and Fields |
|
private readonly Announcements.Announcements _announcements; |
|
private readonly global::Servers.Servers _servers; |
|
private readonly TaskScheduler _uiTaskScheduler; |
|
private AboutForm _aboutForm; |
|
private GotifyConnectionManager _gotifyConnectionManager; |
private ConcurrentBag<GotifyConnection> _gotifyConnections; |
|
private SettingsForm _settingsForm; |
|
@@ -53,13 +48,13 @@ |
// Upgrade settings if required. |
if (!ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).HasFile) |
{ |
if (Settings.Default.UpdateRequired) |
if (Properties.Settings.Default.UpdateRequired) |
{ |
Settings.Default.Upgrade(); |
Settings.Default.Reload(); |
Properties.Settings.Default.Upgrade(); |
Properties.Settings.Default.Reload(); |
|
Settings.Default.UpdateRequired = false; |
Settings.Default.Save(); |
Properties.Settings.Default.UpdateRequired = false; |
Properties.Settings.Default.Save(); |
|
mutex.ReleaseMutex(); |
Process.Start(Application.ExecutablePath); |
@@ -68,42 +63,28 @@ |
} |
|
// Bind to settings changed event. |
Settings.Default.SettingsLoaded += Default_SettingsLoaded; |
Settings.Default.SettingsSaving += Default_SettingsSaving; |
Settings.Default.PropertyChanged += Default_PropertyChanged; |
Properties.Settings.Default.SettingsLoaded += Default_SettingsLoaded; |
Properties.Settings.Default.SettingsSaving += Default_SettingsSaving; |
Properties.Settings.Default.PropertyChanged += Default_PropertyChanged; |
|
_servers = new global::Servers.Servers(); |
_servers.Server.CollectionChanged += Server_CollectionChanged; |
_servers.Server.ListChanged += Server_ListChanged; |
_announcements = new Announcements.Announcements(); |
_announcements.Announcement.CollectionChanged += Announcements_CollectionChanged; |
_announcements.Announcement.ListChanged += Announcement_ListChanged; |
|
// Store UI thread context. |
_uiTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext(); |
|
_gotifyConnectionManager = new GotifyConnectionManager(_servers); |
_gotifyConnectionManager.GotifyNotification += GotifyConnectionManager_GotifyNotification; |
|
LoadServers().ContinueWith(async task => |
{ |
var restoredServers = await task; |
|
_gotifyConnections = new ConcurrentBag<GotifyConnection>(); |
|
foreach (var server in restoredServers.Server) |
{ |
_servers.Server.Add(server); |
var gotifyConnection = new GotifyConnection(server); |
gotifyConnection.GotifyNotification += GotifyConnection_GotifyNotification; |
gotifyConnection.Start(); |
_gotifyConnections.Add(gotifyConnection); |
} |
}); |
|
LoadAnnouncements().ContinueWith(async task => |
{ |
var restoreAnnouncements = await task; |
|
foreach (var announcement in restoreAnnouncements.Announcement) |
{ |
_announcements.Announcement.Add(announcement); |
} |
}); |
|
// Start application update. |
AutoUpdater.Start("http://winify.grimore.org/update/winify.xml"); |
} |
@@ -116,17 +97,10 @@ |
{ |
if (disposing && components != null) |
{ |
_servers.Server.CollectionChanged -= Server_CollectionChanged; |
_announcements.Announcement.CollectionChanged -= Announcements_CollectionChanged; |
Properties.Settings.Default.SettingsLoaded -= Default_SettingsLoaded; |
Properties.Settings.Default.SettingsSaving -= Default_SettingsSaving; |
Properties.Settings.Default.PropertyChanged -= Default_PropertyChanged; |
|
Settings.Default.SettingsLoaded -= Default_SettingsLoaded; |
Settings.Default.SettingsSaving -= Default_SettingsSaving; |
Settings.Default.PropertyChanged -= Default_PropertyChanged; |
|
_gotifyConnectionManager.GotifyNotification -= GotifyConnectionManager_GotifyNotification; |
_gotifyConnectionManager?.Dispose(); |
_gotifyConnectionManager = null; |
|
components.Dispose(); |
} |
|
@@ -137,31 +111,72 @@ |
|
#region Event Handlers |
|
private async void Announcement_ListChanged(object sender, ListChangedEventArgs e) |
private static void Default_PropertyChanged(object sender, PropertyChangedEventArgs e) |
{ |
await SaveAnnouncements(); |
Properties.Settings.Default.Save(); |
} |
|
private async void Server_ListChanged(object sender, ListChangedEventArgs e) |
private static void Default_SettingsSaving(object sender, CancelEventArgs e) |
{ |
await SaveServers(); |
} |
|
private async void Announcements_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) |
private static void Default_SettingsLoaded(object sender, SettingsLoadedEventArgs e) |
{ |
await SaveAnnouncements(); |
} |
|
private void GotifyConnectionManager_GotifyNotification(object sender, GotifyNotificationEventArgs e) |
private async void SettingsToolStripMenuItem_Click(object sender, EventArgs e) |
{ |
Task.Factory.StartNew(() => |
if (_settingsForm != null) |
{ |
foreach (var announcement in _announcements.Announcement) |
return; |
} |
|
var servers = await LoadServers(); |
var announcements = await LoadAnnouncements(); |
|
_settingsForm = new SettingsForm(servers, announcements); |
_settingsForm.Save += SettingsForm_Save; |
_settingsForm.Closing += SettingsForm_Closing; |
_settingsForm.Show(); |
} |
|
private async void SettingsForm_Save(object sender, SettingsSavedEventArgs e) |
{ |
// Save the servers. |
await Task.WhenAll(SaveServers(e.Servers), SaveAnnouncements(e.Announcements)); |
|
// Update connections to gotify servers. |
while (_gotifyConnections.TryTake(out var gotifyConnection)) |
{ |
gotifyConnection.GotifyNotification -= GotifyConnection_GotifyNotification; |
gotifyConnection.Stop(); |
gotifyConnection.Dispose(); |
gotifyConnection = null; |
} |
|
foreach (var server in e.Servers.Server) |
{ |
var gotifyConnection = new GotifyConnection(server); |
gotifyConnection.GotifyNotification += GotifyConnection_GotifyNotification; |
gotifyConnection.Start(); |
_gotifyConnections.Add(gotifyConnection); |
} |
} |
|
private void GotifyConnection_GotifyNotification(object sender, GotifyNotificationEventArgs e) |
{ |
Task.Factory.StartNew(async () => |
{ |
var announcements = await LoadAnnouncements(); |
|
foreach (var announcement in announcements.Announcement) |
{ |
if (announcement.AppId == e.Notification.AppId) |
{ |
var configuredNotification = new Notification($"{e.Notification.Title} ({e.Notification.Server.Name}/{e.Notification.AppId})", |
e.Notification.Message, announcement.LingerTime, e.Image, FormAnimator.AnimationMethod.Slide, |
var configuredNotification = new Notification( |
$"{e.Notification.Title} ({e.Notification.Server.Name}/{e.Notification.AppId})", |
e.Notification.Message, announcement.LingerTime, e.Image, |
FormAnimator.AnimationMethod.Slide, |
FormAnimator.AnimationDirection.Up); |
|
configuredNotification.Show(); |
@@ -170,44 +185,15 @@ |
} |
} |
|
var notification = new Notification($"{e.Notification.Title} ({e.Notification.Server.Name}/{e.Notification.AppId})", |
var notification = new Notification( |
$"{e.Notification.Title} ({e.Notification.Server.Name}/{e.Notification.AppId})", |
e.Notification.Message, 5000, e.Image, FormAnimator.AnimationMethod.Slide, |
FormAnimator.AnimationDirection.Up); |
|
notification.Show(); |
}, CancellationToken.None, TaskCreationOptions.None, _uiTaskScheduler); |
}, CancellationToken.None, TaskCreationOptions.LongRunning, _uiTaskScheduler); |
} |
|
private async void Server_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) |
{ |
await SaveServers(); |
} |
|
private static void Default_PropertyChanged(object sender, PropertyChangedEventArgs e) |
{ |
Settings.Default.Save(); |
} |
|
private static void Default_SettingsSaving(object sender, CancelEventArgs e) |
{ |
} |
|
private static void Default_SettingsLoaded(object sender, SettingsLoadedEventArgs e) |
{ |
} |
|
private void SettingsToolStripMenuItem_Click(object sender, EventArgs e) |
{ |
if (_settingsForm != null) |
{ |
return; |
} |
|
_settingsForm = new SettingsForm(_servers, _announcements); |
_settingsForm.Closing += SettingsForm_Closing; |
_settingsForm.Show(); |
} |
|
private void SettingsForm_Closing(object sender, CancelEventArgs e) |
{ |
if (_settingsForm == null) |
@@ -215,6 +201,7 @@ |
return; |
} |
|
_settingsForm.Save -= SettingsForm_Save; |
_settingsForm.Closing -= SettingsForm_Closing; |
_settingsForm.Dispose(); |
_settingsForm = null; |
@@ -260,9 +247,9 @@ |
|
#region Private Methods |
|
private async Task SaveAnnouncements() |
private static async Task SaveAnnouncements(Announcements.Announcements announcements) |
{ |
switch (await ServersSerialization.Serialize(_announcements, Constants.NotificationsFile, "Announcements", |
switch (await ServersSerialization.Serialize(announcements, Constants.AnnouncementsFile, "Announcements", |
"<!ATTLIST Announcements xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>")) |
{ |
case SerializationFailure serializationFailure: |
@@ -271,7 +258,7 @@ |
} |
} |
|
private async Task SaveServers() |
private static async Task SaveServers(global::Servers.Servers servers) |
{ |
// Encrypt password for all servers. |
var deviceId = Miscellaneous.GetMachineGuid(); |
@@ -279,7 +266,7 @@ |
{ |
Server = new BindingListWithCollectionChanged<Server>() |
}; |
foreach (var server in _servers.Server) |
foreach (var server in servers.Server) |
{ |
var encrypted = AES.Encrypt(Encoding.UTF8.GetBytes(server.Password), deviceId); |
var armored = Convert.ToBase64String(encrypted); |
@@ -304,7 +291,7 @@ |
} |
|
var deserializationResult = |
await ServersSerialization.Deserialize<Announcements.Announcements>(Constants.NotificationsFile, |
await ServersSerialization.Deserialize<Announcements.Announcements>(Constants.AnnouncementsFile, |
"urn:winify-announcements-schema", "Announcements.xsd"); |
|
switch (deserializationResult) |