Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/devicemanager.py
blob: 736312c202cd80ca466f778295d8f64d5a83a547 (plain)
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
#! /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

UDISK_DEVICE_PATH = 'org.freedesktop.UDisks.Device'


class DeviceManager(gobject.GObject):

    __gsignals__ = {
        'device-changed': (gobject.SIGNAL_RUN_FIRST,
                          gobject.TYPE_NONE,
                          ([])),
    }

    def __init__(self):
        gobject.GObject.__init__(self)

        self._devices = {}
        self._bus = dbus.SystemBus()

        self._udisk_proxy = self._bus.get_object('org.freedesktop.UDisks',
                       '/org/freedesktop/UDisks')
        self._udisk_iface = dbus.Interface(self._udisk_proxy,
                        'org.freedesktop.UDisks')
        try:
            self._populate_devices()

            self._udisk_iface.connect_to_signal('DeviceChanged',
                    self.__device_changed_cb)
        except dbus.exceptions.DBusException, e:
            logging.error('Exception initializing UDisks: %s', e)

    def _populate_devices(self):
        for device in self._udisk_proxy.EnumerateDevices():
            props = self._get_props_from_device(device)
            if props is not None:
                logging.debug('Device mounted in %s', props['mount_path'])
                props['have_catalog'] = self._have_catalog(props)
                self._devices[device] = props

    def _get_props_from_device(self, device):
        # http://hal.freedesktop.org/docs/udisks/Device.html
        device_obj = self._bus.get_object('org.freedesktop.UDisks', device)
        device_props = dbus.Interface(device_obj, dbus.PROPERTIES_IFACE)
        props = {}
        props['mounted'] = bool(device_props.Get(UDISK_DEVICE_PATH,
                'DeviceIsMounted'))
        if props['mounted']:
            props['mount_path'] = str(device_props.Get(UDISK_DEVICE_PATH,
                    'DeviceMountPaths')[0])
            props['removable'] = bool(device_props.Get(UDISK_DEVICE_PATH,
                    'DriveCanDetach'))
            props['label'] = str(device_props.Get(UDISK_DEVICE_PATH,
                    'IdLabel'))
            props['size'] = int(device_props.Get(UDISK_DEVICE_PATH,
                    'DeviceSize'))
            return props
        return None

    def _have_catalog(self, props):
        # 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 props['removable']:
            return False
        mount_point = props['mount_path']
        return os.path.exists(os.path.join(mount_point, 'catalog.xml'))

    def __device_changed_cb(self, device):
        props = self._get_props_from_device(device)
        if props is not None:
            self._devices[device] = props
            have_catalog = self._have_catalog(props)
            props['have_catalog'] = have_catalog
            if have_catalog:
                self.emit('device-changed')
            logging.debug('Device was added %s' % props)
        else:
            if device in self._devices:
                props = self._devices[device]
                need_notify = props['have_catalog']
                logging.debug('Device was removed %s', props)
                del self._devices[device]
                if need_notify:
                    self.emit('device-changed')

    def get_devices(self):
        return self._devices