Winify – Rev 24

Subversion Repositories:
Rev:
using System;
using System.Collections.Concurrent;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using Servers;

namespace Winify.Gotify
{
    public class GotifyConnectionManager : IDisposable
    {
        #region Public Events & Delegates

        public event EventHandler<GotifyNotificationEventArgs> GotifyNotification;

        #endregion

        #region Private Delegates, Events, Enums, Properties, Indexers and Fields

        private readonly ConcurrentDictionary<string, GotifyConnection> _gotifyConnections;

        private readonly global::Servers.Servers _servers;

        #endregion

        #region Constructors, Destructors and Finalizers

        private GotifyConnectionManager()
        {
            _gotifyConnections = new ConcurrentDictionary<string, GotifyConnection>();
        }

        public GotifyConnectionManager(global::Servers.Servers servers) : this()
        {
            _servers = servers;

            _servers.Server.CollectionChanged += Server_CollectionChanged;
            _servers.Server.ListChanged += Server_ListChanged;
        }

        public void Dispose()
        {
            _servers.Server.CollectionChanged -= Server_CollectionChanged;
            _servers.Server.ListChanged -= Server_ListChanged;
        }

        #endregion

        #region Event Handlers

        private void Server_ListChanged(object sender, ListChangedEventArgs e)
        {
            if (e.ListChangedType != ListChangedType.ItemChanged)
            {
                return;
            }

            var server = _servers.Server[e.NewIndex];

            if (RemoveConnection(server))
            {
                CreateConnection(server);
            }
        }

        private void Server_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                foreach (var server in e.NewItems.OfType<Server>())
                {
                    CreateConnection(server);
                }
            }

            if (e.OldItems != null)
            {
                foreach (var server in e.OldItems.OfType<Server>())
                {
                    RemoveConnection(server);
                }
            }
        }

        private void Connection_GotifyNotification(object sender, GotifyNotificationEventArgs e)
        {
            GotifyNotification?.Invoke(sender, e);
        }

        #endregion

        #region Private Methods

        private bool RemoveConnection(Server server)
        {
            if (!_gotifyConnections.TryRemove(server.Url, out var gotifyConnection))
            {
                return false;
            }

            gotifyConnection.GotifyNotification -= Connection_GotifyNotification;

            gotifyConnection.Stop();
            gotifyConnection.Dispose();
            gotifyConnection = null;

            return true;
        }

        private void CreateConnection(Server server)
        {
            var connection = new GotifyConnection(server);
            connection.GotifyNotification += Connection_GotifyNotification;

            connection.Start(server.Username, server.Password, server.Url);
            _gotifyConnections.TryAdd(server.Url, connection);
        }

        #endregion
    }
}

Generated by GNU Enscript 1.6.5.90.