~kelemeng/banshee/bug743928

« back to all changes in this revision

Viewing changes to src/Backends/Banshee.Hal/Hal/Manager.cs

  • Committer: Bazaar Package Importer
  • Author(s): Chow Loong Jin
  • Date: 2011-05-14 22:25:36 UTC
  • mfrom: (6.3.15 experimental)
  • Revision ID: james.westby@ubuntu.com-20110514222536-u1x7ikxdqkmfvyuz
Tags: 2.1.0-1ubuntu1
* [2396c18] Merge from Debian Unstable, remaining changes:
  + Enable SoundMenu and Disable NotificationArea by default
  + Disable boo and karma extensions
  + Enable and recommnd u1ms and soundmenu extensions
  + Move desktop file for Meego UI to /usr/share/une/applications
  + Change the url for the Amazon store redirector
  + Create the U1MS widget earlier and bump libu1 requirement
* [9d7c600] Drop upstreamed u1ms-initialize-earlier patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
// Manager.cs
3
 
//
4
 
// Author:
5
 
//   Aaron Bockover <abockover@novell.com>
6
 
//
7
 
// Copyright (C) 2006-2008 Novell, Inc.
8
 
//
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:
16
 
//
17
 
// The above copyright notice and this permission notice shall be
18
 
// included in all copies or substantial portions of the Software.
19
 
//
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.
27
 
//
28
 
 
29
 
using System;
30
 
using System.Collections;
31
 
using System.Collections.Generic;
32
 
 
33
 
using NDesk.DBus;
34
 
using org.freedesktop.DBus;
35
 
 
36
 
namespace Hal
37
 
{
38
 
    internal delegate void DBusDeviceAddedHandler(string udi);
39
 
    internal delegate void DBusDeviceRemovedHandler(string udi);
40
 
    internal delegate void DBusNewCapabilityHandler(string udi, string capability);
41
 
 
42
 
    [Interface("org.freedesktop.Hal.Manager")]
43
 
    internal interface IManager
44
 
    {
45
 
        event DBusDeviceAddedHandler DeviceAdded;
46
 
        event DBusDeviceRemovedHandler DeviceRemoved;
47
 
        event DBusNewCapabilityHandler NewCapability;
48
 
 
49
 
        string [] GetAllDevices();
50
 
        bool DeviceExists(string udi);
51
 
        string [] FindDeviceStringMatch(string key, string value);
52
 
        string [] FindDeviceByCapability(string capability);
53
 
    }
54
 
 
55
 
    public class DeviceArgs : EventArgs
56
 
    {
57
 
        private string udi;
58
 
 
59
 
        public DeviceArgs(string udi)
60
 
        {
61
 
            this.udi = udi;
62
 
        }
63
 
 
64
 
        public string Udi {
65
 
            get { return udi; }
66
 
        }
67
 
    }
68
 
 
69
 
    public class DeviceAddedArgs : DeviceArgs
70
 
    {
71
 
        private Device device;
72
 
 
73
 
        public DeviceAddedArgs(string udi) : base(udi)
74
 
        {
75
 
        }
76
 
 
77
 
        public Device Device {
78
 
            get {
79
 
                if(device == null) {
80
 
                    device = new Device(Udi);
81
 
                }
82
 
 
83
 
                return device;
84
 
            }
85
 
        }
86
 
    }
87
 
 
88
 
    public class DeviceRemovedArgs : DeviceArgs
89
 
    {
90
 
        public DeviceRemovedArgs(string udi) : base(udi)
91
 
        {
92
 
        }
93
 
    }
94
 
 
95
 
    public class NewCapabilityArgs : DeviceArgs
96
 
    {
97
 
        private string capability;
98
 
 
99
 
        public NewCapabilityArgs(string udi, string capability) : base(udi)
100
 
        {
101
 
            this.capability = capability;
102
 
        }
103
 
 
104
 
        public string Capability {
105
 
            get { return capability; }
106
 
        }
107
 
    }
108
 
 
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);
112
 
 
113
 
    public class Manager : IEnumerable<string>
114
 
    {
115
 
        private IManager manager;
116
 
 
117
 
        public event DeviceAddedHandler DeviceAdded;
118
 
        public event DeviceRemovedHandler DeviceRemoved;
119
 
        public event NewCapabilityHandler NewCapability;
120
 
 
121
 
        public Manager()
122
 
        {
123
 
            if(!Bus.System.NameHasOwner("org.freedesktop.Hal")) {
124
 
                // try to start it
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");
128
 
                }
129
 
 
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");
133
 
                }
134
 
            }
135
 
 
136
 
            manager = Bus.System.GetObject<IManager>("org.freedesktop.Hal",
137
 
                new ObjectPath("/org/freedesktop/Hal/Manager"));
138
 
 
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");
141
 
            }
142
 
 
143
 
            manager.DeviceAdded += OnDeviceAdded;
144
 
            manager.DeviceRemoved += OnDeviceRemoved;
145
 
            manager.NewCapability += OnNewCapability;
146
 
        }
147
 
 
148
 
        protected virtual void OnDeviceAdded(string udi)
149
 
        {
150
 
            if(DeviceAdded != null) {
151
 
                DeviceAdded(this, new DeviceAddedArgs(udi));
152
 
            }
153
 
        }
154
 
 
155
 
        protected virtual void OnDeviceRemoved(string udi)
156
 
        {
157
 
            if(DeviceRemoved != null) {
158
 
                DeviceRemoved(this, new DeviceRemovedArgs(udi));
159
 
            }
160
 
        }
161
 
 
162
 
        protected virtual void OnNewCapability(string udi, string capability)
163
 
        {
164
 
            if(NewCapability != null)
165
 
                NewCapability(this, new NewCapabilityArgs(udi, capability));
166
 
        }
167
 
 
168
 
        public bool DeviceExists(string udi)
169
 
        {
170
 
            return manager.DeviceExists(udi);
171
 
        }
172
 
 
173
 
        public string [] FindDeviceByStringMatch(string key, string value)
174
 
        {
175
 
            return manager.FindDeviceStringMatch(key, value);
176
 
        }
177
 
 
178
 
        public string [] FindDeviceByCapability(string capability)
179
 
        {
180
 
            return manager.FindDeviceByCapability(capability);
181
 
        }
182
 
 
183
 
        public Device [] FindDeviceByCapabilityAsDevice(string capability)
184
 
        {
185
 
            return Device.UdisToDevices(FindDeviceByCapability(capability));
186
 
        }
187
 
 
188
 
        public Device [] FindDeviceByStringMatchAsDevice(string key, string value)
189
 
        {
190
 
            return Device.UdisToDevices(FindDeviceByStringMatch(key, value));
191
 
        }
192
 
 
193
 
        public string [] GetAllDevices()
194
 
        {
195
 
            return manager.GetAllDevices();
196
 
        }
197
 
 
198
 
        public IEnumerator<string> GetEnumerator()
199
 
        {
200
 
            foreach(string device in GetAllDevices()) {
201
 
                yield return device;
202
 
            }
203
 
        }
204
 
 
205
 
        IEnumerator IEnumerable.GetEnumerator()
206
 
        {
207
 
            return GetEnumerator();
208
 
        }
209
 
    }
210
 
}