Winify – Blame information for rev 21

Subversion Repositories:
Rev:
Rev Author Line No. Line
7 office 1 using System;
2 using System.Collections.Concurrent;
3 using System.Collections.Specialized;
21 office 4 using System.ComponentModel;
7 office 5 using System.Linq;
6 using Servers;
4 office 7  
8 namespace Winify.Gotify
9 {
7 office 10 public class GotifyConnectionManager : IDisposable
4 office 11 {
11 office 12 #region Public Events & Delegates
13  
14 public event EventHandler<GotifyNotificationEventArgs> GotifyNotification;
15  
16 #endregion
17  
4 office 18 #region Private Delegates, Events, Enums, Properties, Indexers and Fields
19  
7 office 20 private readonly ConcurrentDictionary<string, GotifyConnection> _gotifyConnections;
4 office 21  
8 office 22 private readonly global::Servers.Servers _servers;
7 office 23  
4 office 24 #endregion
25  
26 #region Constructors, Destructors and Finalizers
27  
7 office 28 private GotifyConnectionManager()
4 office 29 {
7 office 30 _gotifyConnections = new ConcurrentDictionary<string, GotifyConnection>();
4 office 31 }
32  
8 office 33 public GotifyConnectionManager(global::Servers.Servers servers) : this()
7 office 34 {
35 _servers = servers;
4 office 36  
7 office 37 _servers.Server.CollectionChanged += Server_CollectionChanged;
21 office 38 _servers.Server.ListChanged += Server_ListChanged;
7 office 39 }
4 office 40  
7 office 41 public void Dispose()
4 office 42 {
7 office 43 _servers.Server.CollectionChanged -= Server_CollectionChanged;
21 office 44 _servers.Server.ListChanged -= Server_ListChanged;
4 office 45 }
46  
47 #endregion
48  
7 office 49 #region Event Handlers
4 office 50  
21 office 51 private void Server_ListChanged(object sender, ListChangedEventArgs e)
52 {
53 if (e.ListChangedType != ListChangedType.ItemChanged)
54 {
55 return;
56 }
57  
58 var server = _servers.Server[e.NewIndex];
59  
60 if (RemoveConnection(server))
61 {
62 CreateConnection(server);
63 }
64 }
65  
7 office 66 private void Server_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
4 office 67 {
7 office 68 if (e.NewItems != null)
4 office 69 {
7 office 70 foreach (var server in e.NewItems.OfType<Server>())
71 {
21 office 72 CreateConnection(server);
7 office 73 }
4 office 74 }
75  
7 office 76 if (e.OldItems != null)
4 office 77 {
7 office 78 foreach (var server in e.OldItems.OfType<Server>())
79 {
21 office 80 RemoveConnection(server);
7 office 81 }
4 office 82 }
83 }
84  
11 office 85 private void Connection_GotifyNotification(object sender, GotifyNotificationEventArgs e)
7 office 86 {
11 office 87 GotifyNotification?.Invoke(sender, e);
7 office 88 }
89  
4 office 90 #endregion
21 office 91  
92 #region Private Methods
93  
94 private bool RemoveConnection(Server server)
95 {
96 if (!_gotifyConnections.TryRemove(server.Url, out var gotifyConnection))
97 {
98 return false;
99 }
100  
101 gotifyConnection.GotifyNotification -= Connection_GotifyNotification;
102  
103 gotifyConnection.Stop();
104 gotifyConnection.Dispose();
105 gotifyConnection = null;
106  
107 return true;
108 }
109  
110 private void CreateConnection(Server server)
111 {
112 var connection = new GotifyConnection();
113 connection.GotifyNotification += Connection_GotifyNotification;
114  
115 connection.Start(server.Username, server.Password, server.Url);
116 _gotifyConnections.TryAdd(server.Url, connection);
117 }
118  
119 #endregion
4 office 120 }
121 }