5
// Aaron Bockover <abockover@novell.com>
7
// Copyright (C) 2006-2008 Novell, Inc.
9
// Permission is hereby granted, free of charge, to any person obtaining
10
// a copy of this software and associated documentation files (the
11
// "Software"), to deal in the Software without restriction, including
12
// without limitation the rights to use, copy, modify, merge, publish,
13
// distribute, sublicense, and/or sell copies of the Software, and to
14
// permit persons to whom the Software is furnished to do so, subject to
15
// the following conditions:
17
// The above copyright notice and this permission notice shall be
18
// included in all copies or substantial portions of the Software.
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30
using System.Collections;
31
using System.Collections.Generic;
34
using org.freedesktop.DBus;
38
internal delegate void DBusDeviceAddedHandler(string udi);
39
internal delegate void DBusDeviceRemovedHandler(string udi);
40
internal delegate void DBusNewCapabilityHandler(string udi, string capability);
42
[Interface("org.freedesktop.Hal.Manager")]
43
internal interface IManager
45
event DBusDeviceAddedHandler DeviceAdded;
46
event DBusDeviceRemovedHandler DeviceRemoved;
47
event DBusNewCapabilityHandler NewCapability;
49
string [] GetAllDevices();
50
bool DeviceExists(string udi);
51
string [] FindDeviceStringMatch(string key, string value);
52
string [] FindDeviceByCapability(string capability);
55
public class DeviceArgs : EventArgs
59
public DeviceArgs(string udi)
69
public class DeviceAddedArgs : DeviceArgs
71
private Device device;
73
public DeviceAddedArgs(string udi) : base(udi)
77
public Device Device {
80
device = new Device(Udi);
88
public class DeviceRemovedArgs : DeviceArgs
90
public DeviceRemovedArgs(string udi) : base(udi)
95
public class NewCapabilityArgs : DeviceArgs
97
private string capability;
99
public NewCapabilityArgs(string udi, string capability) : base(udi)
101
this.capability = capability;
104
public string Capability {
105
get { return capability; }
109
public delegate void DeviceAddedHandler(object o, DeviceAddedArgs args);
110
public delegate void DeviceRemovedHandler(object o, DeviceRemovedArgs args);
111
public delegate void NewCapabilityHandler(object o, NewCapabilityArgs args);
113
public class Manager : IEnumerable<string>
115
private IManager manager;
117
public event DeviceAddedHandler DeviceAdded;
118
public event DeviceRemovedHandler DeviceRemoved;
119
public event NewCapabilityHandler NewCapability;
123
if(!Bus.System.NameHasOwner("org.freedesktop.Hal")) {
125
var reply = Bus.System.StartServiceByName ("org.freedesktop.Hal");
126
if (reply != StartReply.Success && reply != StartReply.AlreadyRunning) {
127
throw new ApplicationException("Could not start org.freedesktop.Hal");
130
// If still not started, we're done
131
if(!Bus.System.NameHasOwner("org.freedesktop.Hal")) {
132
throw new ApplicationException("Could not find org.freedesktop.Hal");
136
manager = Bus.System.GetObject<IManager>("org.freedesktop.Hal",
137
new ObjectPath("/org/freedesktop/Hal/Manager"));
139
if(manager == null) {
140
throw new ApplicationException("The /org/freedesktop/Hal/Manager object could not be located on the DBUs interface org.freedesktop.Hal");
143
manager.DeviceAdded += OnDeviceAdded;
144
manager.DeviceRemoved += OnDeviceRemoved;
145
manager.NewCapability += OnNewCapability;
148
protected virtual void OnDeviceAdded(string udi)
150
if(DeviceAdded != null) {
151
DeviceAdded(this, new DeviceAddedArgs(udi));
155
protected virtual void OnDeviceRemoved(string udi)
157
if(DeviceRemoved != null) {
158
DeviceRemoved(this, new DeviceRemovedArgs(udi));
162
protected virtual void OnNewCapability(string udi, string capability)
164
if(NewCapability != null)
165
NewCapability(this, new NewCapabilityArgs(udi, capability));
168
public bool DeviceExists(string udi)
170
return manager.DeviceExists(udi);
173
public string [] FindDeviceByStringMatch(string key, string value)
175
return manager.FindDeviceStringMatch(key, value);
178
public string [] FindDeviceByCapability(string capability)
180
return manager.FindDeviceByCapability(capability);
183
public Device [] FindDeviceByCapabilityAsDevice(string capability)
185
return Device.UdisToDevices(FindDeviceByCapability(capability));
188
public Device [] FindDeviceByStringMatchAsDevice(string key, string value)
190
return Device.UdisToDevices(FindDeviceByStringMatch(key, value));
193
public string [] GetAllDevices()
195
return manager.GetAllDevices();
198
public IEnumerator<string> GetEnumerator()
200
foreach(string device in GetAllDevices()) {
205
IEnumerator IEnumerable.GetEnumerator()
207
return GetEnumerator();