StephaneLenclud@123: //
StephaneLenclud@123: // Copyright (C) 2014-2015 Stéphane Lenclud.
StephaneLenclud@123: //
StephaneLenclud@123: // This file is part of SharpDisplayManager.
StephaneLenclud@123: //
StephaneLenclud@123: // SharpDisplayManager is free software: you can redistribute it and/or modify
StephaneLenclud@123: // it under the terms of the GNU General Public License as published by
StephaneLenclud@123: // the Free Software Foundation, either version 3 of the License, or
StephaneLenclud@123: // (at your option) any later version.
StephaneLenclud@123: //
StephaneLenclud@123: // SharpDisplayManager is distributed in the hope that it will be useful,
StephaneLenclud@123: // but WITHOUT ANY WARRANTY; without even the implied warranty of
StephaneLenclud@123: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
StephaneLenclud@123: // GNU General Public License for more details.
StephaneLenclud@123: //
StephaneLenclud@123: // You should have received a copy of the GNU General Public License
StephaneLenclud@123: // along with SharpDisplayManager.  If not, see <http://www.gnu.org/licenses/>.
StephaneLenclud@123: //
StephaneLenclud@123: 
StephaneLenclud@123: using System;
StephaneLenclud@117: using System.Collections.Generic;
StephaneLenclud@117: using System.Linq;
StephaneLenclud@117: using System.Text;
StephaneLenclud@117: using System.Threading.Tasks;
StephaneLenclud@117: using System.Net.NetworkInformation;
StephaneLenclud@117: using System.Runtime.InteropServices;
StephaneLenclud@117: using System.Runtime.InteropServices.ComTypes;
StephaneLenclud@117: using System.Diagnostics;
StephaneLenclud@117: using NETWORKLIST;
StephaneLenclud@117: 
StephaneLenclud@117: namespace SharpDisplayManager
StephaneLenclud@117: {
StephaneLenclud@117: 	public class NetworkManager: INetworkListManagerEvents, IDisposable
StephaneLenclud@117:     {
StephaneLenclud@117: 		public delegate void OnConnectivityChangedDelegate(NetworkManager aNetworkManager, NLM_CONNECTIVITY aConnectivity);
StephaneLenclud@117: 		public event OnConnectivityChangedDelegate OnConnectivityChanged;
StephaneLenclud@117: 		
StephaneLenclud@117: 		private int iCookie = 0;
StephaneLenclud@117:         private IConnectionPoint iConnectionPoint;
StephaneLenclud@117:         private INetworkListManager iNetworkListManager;
StephaneLenclud@117: 
StephaneLenclud@117: 
StephaneLenclud@117: 		public NetworkManager()
StephaneLenclud@117: 		{
StephaneLenclud@117: 			iNetworkListManager = new NetworkListManager();
StephaneLenclud@117: 			ConnectToNetworkListManagerEvents();
StephaneLenclud@117: 		}
StephaneLenclud@117: 
StephaneLenclud@117: 		public void Dispose()
StephaneLenclud@117: 		{
StephaneLenclud@117: 			//Not sure why this is not working form here
StephaneLenclud@117: 			//Possibly because something is doing automatically before we get there
StephaneLenclud@117: 			//DisconnectFromNetworkListManagerEvents();
StephaneLenclud@117: 		}
StephaneLenclud@117: 
StephaneLenclud@117: 
StephaneLenclud@117: 		public INetworkListManager NetworkListManager
StephaneLenclud@117: 		{
StephaneLenclud@117: 			get { return iNetworkListManager; }
StephaneLenclud@117: 		}
StephaneLenclud@117: 
StephaneLenclud@117: 		public void ConnectivityChanged(NLM_CONNECTIVITY newConnectivity)
StephaneLenclud@117: 		{
StephaneLenclud@117: 			//Fire our event
StephaneLenclud@117: 			OnConnectivityChanged(this, newConnectivity);
StephaneLenclud@117: 		}
StephaneLenclud@117: 
StephaneLenclud@117: 		public void ConnectToNetworkListManagerEvents()
StephaneLenclud@117: 		{
StephaneLenclud@117: 			Debug.WriteLine("Subscribing to INetworkListManagerEvents");
StephaneLenclud@117: 			IConnectionPointContainer icpc = (IConnectionPointContainer)iNetworkListManager;
StephaneLenclud@117: 			//similar event subscription can be used for INetworkEvents and INetworkConnectionEvents
StephaneLenclud@117: 			Guid tempGuid = typeof(INetworkListManagerEvents).GUID;
StephaneLenclud@117: 			icpc.FindConnectionPoint(ref tempGuid, out iConnectionPoint);
StephaneLenclud@117: 			iConnectionPoint.Advise(this, out iCookie);
StephaneLenclud@117: 			
StephaneLenclud@117: 		}
StephaneLenclud@117: 
StephaneLenclud@117: 		public void DisconnectFromNetworkListManagerEvents()
StephaneLenclud@117: 		{
StephaneLenclud@117: 			Debug.WriteLine("Un-subscribing to INetworkListManagerEvents");
StephaneLenclud@117: 			iConnectionPoint.Unadvise(iCookie);
StephaneLenclud@117: 		} 
StephaneLenclud@117: 	}
StephaneLenclud@117: }