1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
/***************************************************************************
* Manager.cs
*
* Copyright (C) 2005 Novell
* Written by Aaron Bockover (aaron@aaronbock.net)
****************************************************************************/
/* THIS FILE IS LICENSED UNDER THE MIT LICENSE AS OUTLINED IMMEDIATELY BELOW:
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Reflection;
using System.Collections;
using DBus;
namespace NetworkManager
{
public enum State {
Unknown = 0,
Asleep,
Connecting,
Connected,
Disconnected
}
[Interface("org.freedesktop.NetworkManager")]
internal abstract class ManagerProxy
{
/* Unsupported methods:
getDialup
activateDialup
setActiveDevice
createWirelessNetwork
createTestDevice
removeTestDevice
*/
[Method] public abstract DeviceProxy [] getDevices();
[Method] public abstract uint state();
[Method] public abstract void sleep();
[Method] public abstract void wake();
[Method] public abstract bool getWirelessEnabled();
[Method] public abstract void setWirelessEnabled(bool enabled);
[Method] public abstract DeviceProxy getActiveDevice();
}
public class Manager : IEnumerable, IDisposable
{
private static readonly string PATH_NAME = "/org/freedesktop/NetworkManager";
private static readonly string INTERFACE_NAME = "org.freedesktop.NetworkManager";
private Service dbus_service;
private Connection dbus_connection;
private ManagerProxy manager;
#pragma warning disable 0067
public event EventHandler DeviceNoLongerActive;
public event EventHandler DeviceNowActive;
public event EventHandler DeviceActivating;
public event EventHandler DevicesChanged;
public event EventHandler DeviceActivationStage;
public event EventHandler DeviceIP4AddressChange;
public event EventHandler StateChange;
public event EventHandler WirelessNetworkDisappeared;
public event EventHandler WirelessNetworkAppeared;
public event EventHandler WirelessNetworkStrengthChanged;
#pragma warning restore 0067
public Manager()
{
dbus_connection = Bus.GetSystemBus();
dbus_service = Service.Get(dbus_connection, INTERFACE_NAME);
manager = (ManagerProxy)dbus_service.GetObject(typeof(ManagerProxy), PATH_NAME);
dbus_service.SignalCalled += OnSignalCalled;
}
public void Dispose()
{
// Major nasty hack to work around dbus-sharp bug: bad IL in object Finalizer
System.GC.SuppressFinalize(manager);
}
private void OnSignalCalled(Signal signal)
{
if(signal.PathName != PATH_NAME || signal.InterfaceName != INTERFACE_NAME) {
return;
}
InvokeEvent(signal.Name);
}
private void InvokeEvent(string nmSignalName)
{
/*
Ughh, this would be much nicer if it actually worked using reflection.
But noooo, EventInfo.GetRaiseMethod *always* returns null, so
I can't invoke the registered raise method through reflection, which
leaves me to do this lame switch/case hard coded event raising...
*/
switch(nmSignalName) {
case "DeviceNoLongerActive": InvokeEvent(DeviceNoLongerActive); break;
case "DeviceNowActive": InvokeEvent(DeviceNowActive); break;
case "DeviceActivating": InvokeEvent(DeviceActivating); break;
case "DeviceActivationStage": InvokeEvent(DeviceActivationStage); break;
case "DevicesChanged": InvokeEvent(DevicesChanged); break;
case "DeviceIP4AddressChange": InvokeEvent(DeviceIP4AddressChange); break;
case "WirelessNetworkDisappeared": InvokeEvent(WirelessNetworkDisappeared); break;
case "WirelessNetworkAppeared": InvokeEvent(WirelessNetworkAppeared); break;
case "WirelessNetworkStrengthChanged": InvokeEvent(WirelessNetworkStrengthChanged); break;
case "StateChange": InvokeEvent(StateChange); break;
}
/*EventInfo event_info = GetType().GetEvent(nmSignalName);
if(event_info == null) {
return;
}
MethodInfo method = event_info.GetRaiseMethod(true);
if(method != null) {
method.Invoke(this, new object [] { this, new EventArgs() });
}*/
}
private void InvokeEvent(EventHandler eventHandler)
{
EventHandler handler = eventHandler;
if(handler != null) {
handler(this, new EventArgs());
}
}
public IEnumerator GetEnumerator()
{
foreach(DeviceProxy device in manager.getDevices()) {
yield return new Device(device);
}
}
public State State {
get {
return (State)manager.state();
}
}
public Device [] Devices {
get {
ArrayList list = new ArrayList();
foreach(DeviceProxy device in manager.getDevices()) {
list.Add(new Device(device));
}
return list.ToArray(typeof(Device)) as Device [];
}
}
public void Wake()
{
manager.wake();
}
public void Sleep()
{
manager.sleep();
}
public bool WirelessEnabled {
get {
return manager.getWirelessEnabled();
}
set {
manager.setWirelessEnabled(value);
}
}
public Device ActiveDevice {
get {
foreach(Device device in this) {
if(device.IsLinkActive) {
return device;
}
}
return null;
}
}
}
}
|