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
|
#! /usr/bin/env python
# Copyright (C) 2009 Sayamindu Dasgupta <sayamindu@laptop.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import os
import logging
import gobject
import dbus
import time
from dbus.mainloop.glib import DBusGMainLoop
_logger = logging.getLogger('get-ia-books-activity')
class DeviceManager(gobject.GObject):
__gsignals__ = {
'device-added': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE,
([])),
'device-removed': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE,
([]))
}
def __init__(self):
gobject.GObject.__init__(self)
self._devices = []
self._bus = dbus.SystemBus ()
self._hal_obj = self._bus.get_object ('org.freedesktop.Hal', '/org/freedesktop/Hal/Manager')
self._hal_mgr = dbus.Interface (self._hal_obj, 'org.freedesktop.Hal.Manager')
self._populate_devices()
self._hal_mgr.connect_to_signal("DeviceAdded", self.__device_added)
self._hal_mgr.connect_to_signal("DeviceRemoved", self.__device_removed)
def _populate_devices(self):
udis = self._hal_mgr.FindDeviceByCapability ('volume')
for udi in udis:
self.__device_added(udi)
def _is_removable_volume(self, dev):
# Apart from determining if this is a removable volume,
# this also tries to find if there is a catalog.xml in the
# root
if not dev.QueryCapability('volume'):
return False
parent_udi = dev.GetProperty('info.parent')
parent_dev_obj = self._bus.get_object('org.freedesktop.Hal', parent_udi)
parent = dbus.Interface(parent_dev_obj, 'org.freedesktop.Hal.Device')
if not parent.GetProperty('storage.removable'):
return False
time.sleep(1) #XXX: Ugly Hack, needed in some situations
mount_point = dev.GetProperty('volume.mount_point')
return os.path.exists(os.path.join(mount_point, 'catalog.xml'))
def __device_added(self, udi):
dev_obj = self._bus.get_object ('org.freedesktop.Hal', udi)
# get an interface to the device
dev = dbus.Interface (dev_obj, 'org.freedesktop.Hal.Device')
if self._is_removable_volume(dev):
self._devices.append((udi, dev))
self.emit('device-added')
_logger.debug('DeviceManager: Device was added %s' % str(udi))
def __device_removed(self, udi):
for device in self._devices:
if udi in device:
self._devices.remove(device)
self.emit('device-removed')
_logger.debug('DeviceManager: Device was removed %s' % str(udi))
def get_devices(self):
return self._devices
if __name__ == '__main__':
DBusGMainLoop(set_as_default=True)
dm = DeviceManager()
#print dm.get_devices()[0][1].GetProperty('volume.mount_point'), dm.get_devices()[0][1].GetProperty('volume.label')
loop = gobject.MainLoop()
loop.run()
|