From c16957faf5c864a7f5db046e6c200c7daf8acdc3 Mon Sep 17 00:00:00 2001
From: Manuel Kaufmann <humitos@gmail.com>
Date: Tue, 26 Jun 2012 12:55:03 -0300
Subject: [PATCH Get Books] Use Gio instead dbus to monitor pen drives
Replace the use of dbus by Gio to monitor the (dis)connection of pen
drives and show/hide them in the ComboBox used to search books
Signed-off-by: Manuel Kaufmann <humitos@gmail.com>
---
GetIABooksActivity.py | 2 +-
devicemanager.py | 88 ++++++++++++++++---------------------------------
opds.py | 2 +-
3 files changed, 30 insertions(+), 62 deletions(-)
diff --git a/GetIABooksActivity.py b/GetIABooksActivity.py
index b340798..8d20e70 100644
a
|
b
|
class GetIABooksActivity(activity.Activity): |
791 | 791 | search_text, query_language) |
792 | 792 | else: |
793 | 793 | self.queryresults = opds.LocalVolumeQueryResult(self.source, |
794 | | search_text) |
| 794 | search_text, query_language) |
795 | 795 | |
796 | 796 | self.show_message(_('Performing lookup, please wait...')) |
797 | 797 | self.get_window().set_cursor(Gdk.Cursor(Gdk.CursorType.WATCH)) |
diff --git a/devicemanager.py b/devicemanager.py
index e721a49..c86c8eb 100644
a
|
b
|
|
18 | 18 | |
19 | 19 | import os |
20 | 20 | import logging |
21 | | import dbus |
22 | 21 | |
23 | 22 | from gi.repository import GObject |
24 | | |
25 | | UDISK_DEVICE_PATH = 'org.freedesktop.UDisks2.Device' |
| 23 | from gi.repository import Gio |
26 | 24 | |
27 | 25 | |
28 | 26 | class DeviceManager(GObject.GObject): |
… |
… |
class DeviceManager(GObject.GObject): |
37 | 35 | GObject.GObject.__init__(self) |
38 | 36 | |
39 | 37 | self._devices = {} |
40 | | self._bus = dbus.SystemBus() |
41 | 38 | |
42 | | try: |
43 | | self._udisk_proxy = self._bus.get_object('org.freedesktop.UDisks2', |
44 | | '/org/freedesktop/UDisks2') |
45 | | self._udisk_iface = dbus.Interface(self._udisk_proxy, |
46 | | 'org.freedesktop.UDisks2') |
47 | | self._populate_devices() |
48 | | self._udisk_iface.connect_to_signal('DeviceChanged', |
49 | | self.__device_changed_cb) |
50 | | except dbus.exceptions.DBusException, e: |
51 | | logging.error('Exception initializing UDisks: %s', e) |
| 39 | self.volume_monitor = Gio.VolumeMonitor.get() |
| 40 | self.volume_monitor.connect('mount-added', self._mount_added_cb) |
| 41 | self.volume_monitor.connect('mount-removed', self._mount_removed_cb) |
| 42 | |
| 43 | self._populate_devices() |
52 | 44 | |
53 | 45 | def _populate_devices(self): |
54 | | for device in self._udisk_proxy.EnumerateDevices(): |
55 | | props = self._get_props_from_device(device) |
56 | | if props is not None: |
57 | | logging.debug('Device mounted in %s', props['mount_path']) |
58 | | props['have_catalog'] = self._have_catalog(props) |
59 | | self._devices[device] = props |
| 46 | for mount in self.volume_monitor.get_mounts(): |
| 47 | props = self._get_props_from_device(mount) |
| 48 | if mount.can_eject() and props['have_catalog']: |
| 49 | self._devices[mount] = props |
60 | 50 | |
61 | | def _get_props_from_device(self, device): |
62 | | # http://hal.freedesktop.org/docs/udisks/Device.html |
63 | | device_obj = self._bus.get_object('org.freedesktop.UDisks2', device) |
64 | | device_props = dbus.Interface(device_obj, dbus.PROPERTIES_IFACE) |
| 51 | def _get_props_from_device(self, mount): |
65 | 52 | props = {} |
66 | | props['mounted'] = bool(device_props.Get(UDISK_DEVICE_PATH, |
67 | | 'DeviceIsMounted')) |
68 | | if props['mounted']: |
69 | | props['mount_path'] = str(device_props.Get(UDISK_DEVICE_PATH, |
70 | | 'DeviceMountPaths')[0]) |
71 | | props['removable'] = bool(device_props.Get(UDISK_DEVICE_PATH, |
72 | | 'DriveCanDetach')) |
73 | | props['label'] = str(device_props.Get(UDISK_DEVICE_PATH, |
74 | | 'IdLabel')) |
75 | | props['size'] = int(device_props.Get(UDISK_DEVICE_PATH, |
76 | | 'DeviceSize')) |
| 53 | props['removable'] = True |
| 54 | props['mounted'] = True |
| 55 | props['mount_path'] = mount.get_default_location().get_path() |
| 56 | props['label'] = mount.get_name() |
| 57 | # FIXME: get the proper size here |
| 58 | props['size'] = 0 |
| 59 | props['have_catalog'] = os.path.exists(\ |
| 60 | os.path.join(props['mount_path'], 'catalog.xml')) |
77 | 61 | return props |
78 | 62 | |
79 | | def _have_catalog(self, props): |
80 | | # Apart from determining if this is a removable volume, |
81 | | # this also tries to find if there is a catalog.xml in the |
82 | | # root |
83 | | if not props['removable']: |
84 | | return False |
85 | | mount_point = props['mount_path'] |
86 | | return os.path.exists(os.path.join(mount_point, 'catalog.xml')) |
87 | | |
88 | | def __device_changed_cb(self, device): |
| 63 | def _mount_added_cb(self, volume_monitor, device): |
89 | 64 | props = self._get_props_from_device(device) |
90 | | if props is not None: |
91 | | self._devices[device] = props |
92 | | have_catalog = self._have_catalog(props) |
93 | | props['have_catalog'] = have_catalog |
94 | | if have_catalog: |
95 | | self.emit('device-changed') |
96 | | logging.error('Device was added %s' % props) |
97 | | else: |
98 | | if device in self._devices: |
99 | | props = self._devices[device] |
100 | | need_notify = props['have_catalog'] |
101 | | logging.error('Device was removed %s', props) |
102 | | del self._devices[device] |
103 | | if need_notify: |
104 | | self.emit('device-changed') |
| 65 | self._devices[device] = props |
| 66 | logging.debug('Device added: %s', props) |
| 67 | if props['have_catalog']: |
| 68 | self.emit('device-changed') |
| 69 | |
| 70 | def _mount_removed_cb(self, volume_monitor, device): |
| 71 | del self._devices[device] |
| 72 | self.emit('device-changed') |
105 | 73 | |
106 | 74 | def get_devices(self): |
107 | 75 | return self._devices |
diff --git a/opds.py b/opds.py
index 1cd6d62..ced6964 100644
a
|
b
|
class LocalVolumeQueryResult(QueryResult): |
323 | 323 | |
324 | 324 | def __init__(self, path, queryterm, language): |
325 | 325 | configuration = {'query_uri': os.path.join(path, 'catalog.xml')} |
326 | | QueryResult.__init__(self, configuration, queryterm) |
| 326 | QueryResult.__init__(self, configuration, queryterm, language) |
327 | 327 | |
328 | 328 | def is_local(self): |
329 | 329 | return True |