/trunk/Winify/Form1.cs |
@@ -1,8 +1,12 @@ |
using System; |
using System.ComponentModel; |
using System.Configuration; |
using System.Diagnostics; |
using System.IO; |
using System.Threading.Tasks; |
using System.Windows.Forms; |
using AutoUpdaterDotNET; |
using Winify.Connections; |
using Winify.Gotify; |
using Winify.Properties; |
|
@@ -12,12 +16,12 @@ |
{ |
#region Private Delegates, Events, Enums, Properties, Indexers and Fields |
|
private readonly GotifyConnectionManager _gotifyConnectionManager; |
|
private AboutForm _aboutForm; |
|
private GotifyConnection _gotifyConnection; |
private Servers _servers; |
|
private NotificationForm _notificationForm; |
|
private SettingsForm _settingsForm; |
|
#endregion |
@@ -41,12 +45,14 @@ |
Settings.Default.SettingsSaving += Default_SettingsSaving; |
Settings.Default.PropertyChanged += Default_PropertyChanged; |
|
Settings.Default.PropertyChanged += Default_PropertyChanged1; |
_gotifyConnectionManager = new GotifyConnectionManager(); |
|
_gotifyConnection = new GotifyConnection(); |
_gotifyConnection.GotifyNotification += GotifyConnection_GotifyNotification; |
_gotifyConnection.Start(Settings.Default.Username, Settings.Default.Password, Settings.Default.Host, |
Settings.Default.Port); |
Task.Run(async () => |
{ |
_servers = await LoadServers(); |
|
_gotifyConnectionManager.UpdateServers(_servers); |
}); |
} |
|
/// <summary> |
@@ -57,8 +63,6 @@ |
{ |
if (disposing && components != null) |
{ |
_gotifyConnection.GotifyNotification -= GotifyConnection_GotifyNotification; |
|
components.Dispose(); |
} |
|
@@ -69,28 +73,6 @@ |
|
#region Event Handlers |
|
private void GotifyConnection_GotifyNotification(object sender, GotifyNotificationEventArgs e) |
{ |
_notificationForm = new NotificationForm(e.Image, e.Notification.Title, e.Notification.Message, 5000); |
_notificationForm.Show(); |
} |
|
private void Default_PropertyChanged1(object sender, PropertyChangedEventArgs e) |
{ |
if (_gotifyConnection != null) |
{ |
_gotifyConnection.GotifyNotification -= GotifyConnection_GotifyNotification; |
_gotifyConnection.Stop(); |
_gotifyConnection.Dispose(); |
_gotifyConnection = null; |
} |
|
_gotifyConnection = new GotifyConnection(); |
_gotifyConnection.GotifyNotification += GotifyConnection_GotifyNotification; |
_gotifyConnection.Start(Settings.Default.Username, Settings.Default.Password, Settings.Default.Host, |
Settings.Default.Port); |
} |
|
private static void Default_PropertyChanged(object sender, PropertyChangedEventArgs e) |
{ |
Settings.Default.Save(); |
@@ -111,12 +93,27 @@ |
return; |
} |
|
_settingsForm = new SettingsForm(); |
_settingsForm = new SettingsForm(_servers); |
_settingsForm.Closing += SettingsForm_Closing; |
; |
_settingsForm.ServersUpdated += SettingsForm_ServersUpdated; |
_settingsForm.Show(); |
} |
|
private async void SettingsForm_ServersUpdated(object sender, ServersUpdatedEventArgs e) |
{ |
_servers = e.Servers; |
|
switch (await ServersSerialization.Serialize(e.Servers, Constants.ServersFile)) |
{ |
case SerializationSuccess _: |
_gotifyConnectionManager.UpdateServers(e.Servers); |
break; |
case SerializationFailure serializationFailure: |
Debug.WriteLine(serializationFailure.Exception.Message); |
break; |
} |
} |
|
private void SettingsForm_Closing(object sender, CancelEventArgs e) |
{ |
if (_aboutForm == null) |
@@ -159,5 +156,28 @@ |
} |
|
#endregion |
|
#region Private Methods |
|
private static async Task<Servers> LoadServers() |
{ |
if (!Directory.Exists(Constants.UserApplicationDirectory)) |
{ |
Directory.CreateDirectory(Constants.UserApplicationDirectory); |
} |
|
var deserializationResult = await ServersSerialization.Deserialize(Constants.ServersFile); |
|
switch (deserializationResult) |
{ |
case SerializationSuccess serializationSuccess: |
return serializationSuccess.Servers; |
|
default: |
return new Servers(); |
} |
} |
|
#endregion |
} |
} |