Winify

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 6  →  ?path2? @ 7
/trunk/Winify/Gotify/GotifyConnectionManager.cs
@@ -1,62 +1,81 @@
using System.Collections.Generic;
using System;
using System.Collections.Concurrent;
using System.Collections.Specialized;
using System.Linq;
using Servers;
 
namespace Winify.Gotify
{
public class GotifyConnectionManager
public class GotifyConnectionManager : IDisposable
{
#region Private Delegates, Events, Enums, Properties, Indexers and Fields
 
private readonly List<GotifyConnection> _gotifyConnections;
private readonly ConcurrentDictionary<string, GotifyConnection> _gotifyConnections;
 
private readonly Servers.Servers _servers;
 
#endregion
 
#region Constructors, Destructors and Finalizers
 
public GotifyConnectionManager()
private GotifyConnectionManager()
{
_gotifyConnections = new List<GotifyConnection>();
_gotifyConnections = new ConcurrentDictionary<string, GotifyConnection>();
}
 
#endregion
public GotifyConnectionManager(Servers.Servers servers) : this()
{
_servers = servers;
 
#region Event Handlers
_servers.Server.CollectionChanged += Server_CollectionChanged;
}
 
private static void Connection_GotifyNotification(object sender, GotifyNotificationEventArgs e)
public void Dispose()
{
var notification = new NotificationForm(e.Image, e.Notification.Title, e.Notification.Message, 5000);
notification.ShowDialog();
_servers.Server.CollectionChanged -= Server_CollectionChanged;
}
 
#endregion
 
#region Public Methods
#region Event Handlers
 
public void UpdateServers(Servers.Servers servers)
private void Server_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
// Update connections.
var removeConnections = new List<GotifyConnection>();
foreach (var gotifyConnection in _gotifyConnections)
if (e.NewItems != null)
{
gotifyConnection.GotifyNotification -= Connection_GotifyNotification;
gotifyConnection.Stop();
gotifyConnection.Dispose();
removeConnections.Add(gotifyConnection);
foreach (var server in e.NewItems.OfType<Server>())
{
var connection = new GotifyConnection();
connection.GotifyNotification += Connection_GotifyNotification;
 
connection.Start(server.Username, server.Password, server.Host, server.Port);
_gotifyConnections.TryAdd(server.Name, connection);
}
}
 
foreach (var connection in removeConnections)
if (e.OldItems != null)
{
_gotifyConnections.Remove(connection);
}
foreach (var server in e.OldItems.OfType<Server>())
{
if (!_gotifyConnections.TryRemove(server.Name, out var gotifyConnection))
{
continue;
}
 
foreach (var server in servers.Server)
{
var connection = new GotifyConnection();
connection.Start(server.Username, server.Password, server.Host, server.Port);
connection.GotifyNotification += Connection_GotifyNotification;
_gotifyConnections.Add(connection);
gotifyConnection.GotifyNotification -= Connection_GotifyNotification;
 
gotifyConnection.Stop();
gotifyConnection.Dispose();
}
}
}
 
private static void Connection_GotifyNotification(object sender, GotifyNotificationEventArgs e)
{
var notification = new NotificationForm(e.Image, e.Notification.Title, e.Notification.Message, 5000);
notification.ShowDialog();
}
 
#endregion
}
}