Winify – Blame information for rev 8

Subversion Repositories:
Rev:
Rev Author Line No. Line
7 office 1 using System;
2 using System.Collections.Concurrent;
3 using System.Collections.Specialized;
4 using System.Linq;
5 using Servers;
4 office 6  
7 namespace Winify.Gotify
8 {
7 office 9 public class GotifyConnectionManager : IDisposable
4 office 10 {
11 #region Private Delegates, Events, Enums, Properties, Indexers and Fields
12  
7 office 13 private readonly ConcurrentDictionary<string, GotifyConnection> _gotifyConnections;
4 office 14  
8 office 15 private readonly global::Servers.Servers _servers;
7 office 16  
4 office 17 #endregion
18  
19 #region Constructors, Destructors and Finalizers
20  
7 office 21 private GotifyConnectionManager()
4 office 22 {
7 office 23 _gotifyConnections = new ConcurrentDictionary<string, GotifyConnection>();
4 office 24 }
25  
8 office 26 public GotifyConnectionManager(global::Servers.Servers servers) : this()
7 office 27 {
28 _servers = servers;
4 office 29  
7 office 30 _servers.Server.CollectionChanged += Server_CollectionChanged;
31 }
4 office 32  
7 office 33 public void Dispose()
4 office 34 {
7 office 35 _servers.Server.CollectionChanged -= Server_CollectionChanged;
4 office 36 }
37  
38 #endregion
39  
7 office 40 #region Event Handlers
4 office 41  
7 office 42 private void Server_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
4 office 43 {
7 office 44 if (e.NewItems != null)
4 office 45 {
7 office 46 foreach (var server in e.NewItems.OfType<Server>())
47 {
48 var connection = new GotifyConnection();
49 connection.GotifyNotification += Connection_GotifyNotification;
50  
51 connection.Start(server.Username, server.Password, server.Host, server.Port);
52 _gotifyConnections.TryAdd(server.Name, connection);
53 }
4 office 54 }
55  
7 office 56 if (e.OldItems != null)
4 office 57 {
7 office 58 foreach (var server in e.OldItems.OfType<Server>())
59 {
60 if (!_gotifyConnections.TryRemove(server.Name, out var gotifyConnection))
61 {
62 continue;
63 }
4 office 64  
7 office 65 gotifyConnection.GotifyNotification -= Connection_GotifyNotification;
66  
67 gotifyConnection.Stop();
68 gotifyConnection.Dispose();
69 }
4 office 70 }
71 }
72  
7 office 73 private static void Connection_GotifyNotification(object sender, GotifyNotificationEventArgs e)
74 {
75 var notification = new NotificationForm(e.Image, e.Notification.Title, e.Notification.Message, 5000);
76 notification.ShowDialog();
77 }
78  
4 office 79 #endregion
80 }
81 }