Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/volumestoolbar.py
blob: 5974f9046ea958d6cc907db8635f4b6f9f61dc44 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# Copyright (C) 2007, One Laptop Per Child
#
# 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
from gettext import gettext as _

import gobject
import gtk
import dbus

from sugar.activity import activity
from sugar.datastore import datastore
from sugar.graphics import color
from sugar.graphics import units
from sugar.graphics.radiotoolbutton import RadioToolButton
from sugar.graphics.palette import Palette

HAL_SERVICE_NAME = 'org.freedesktop.Hal'
HAL_MANAGER_PATH = '/org/freedesktop/Hal/Manager'
HAL_MANAGER_IFACE = 'org.freedesktop.Hal.Manager'
HAL_DEVICE_IFACE = 'org.freedesktop.Hal.Device'
HAL_VOLUME_IFACE = 'org.freedesktop.Hal.Device.Volume'

class VolumesToolbar(gtk.Toolbar):
    __gtype_name__ = 'VolumesToolbar'

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

    def __init__(self):
        gtk.Toolbar.__init__(self)

        # Internal flash is not in HAL
        internal_flash = datastore.mounts()[0]
        self._add_button(internal_flash['id'], _('Journal'), 'activity-journal', None)

        bus = dbus.SystemBus()
        proxy = bus.get_object(HAL_SERVICE_NAME, HAL_MANAGER_PATH)
        hal_manager = dbus.Interface(proxy, HAL_MANAGER_IFACE)
        hal_manager.connect_to_signal('DeviceAdded', self._hal_device_added_cb)

        gobject.idle_add(self._add_current_hal_devices)

    def _add_current_hal_devices(self):
        bus = dbus.SystemBus()
        proxy = bus.get_object(HAL_SERVICE_NAME, HAL_MANAGER_PATH)
        hal_manager = dbus.Interface(proxy, HAL_MANAGER_IFACE)
        for udi in hal_manager.FindDeviceByCapability('volume'):
            self._add_hal_device(udi)

    def _hal_device_added_cb(self, udi):
        bus = dbus.SystemBus()
        device_object = bus.get_object(HAL_SERVICE_NAME, udi)
        device = dbus.Interface(device_object, HAL_DEVICE_IFACE)
        if device.QueryCapability('volume'):
            self._add_hal_device(udi)

    def _add_hal_device(self, udi):
        bus = dbus.SystemBus()
        device_object = bus.get_object(HAL_SERVICE_NAME, udi)
        device = dbus.Interface(device_object, HAL_DEVICE_IFACE)

        # Only mount volumes with a filesystem.
        if device.GetProperty('volume.fsusage') != 'filesystem':
            return
        # Don't mount root.        
        if device.GetProperty('volume.mount_point') == '/':
            return
        
        label = device.GetProperty('volume.label')
        if device.GetProperty('volume.is_mounted'):
            mount_point = device.GetProperty('volume.mount_point')
        else:
            fs_type = device.GetProperty('volume.fstype')
            valid_options = device.GetProperty('volume.mount.valid_options')
            options = []

            if 'uid=' in valid_options:
                options.append('uid=%i' % os.getuid())

            mount_point = label
            if not mount_point:
                mount_point = device.GetProperty('volume.uuid')

            volume = dbus.Interface(device_object, HAL_VOLUME_IFACE)
            volume.Mount(mount_point, fs_type, options)
            mount_point = os.path.join('/media', mount_point)

        mount_id = None
        ds_mounts = datastore.mounts()
        for ds_mount in ds_mounts:
            if ds_mount['uri'] == mount_point:
                mount_id = ds_mount['id']

        if mount_id is None:
            mount_id = datastore.mount('inplace:' + mount_point,
                                       dict(title=mount_point))
            if not mount_id:
                self._unmount_device(uid)
                raise RuntimeError('datastore.mount(%r, %r) failed.' % (
                        'inplace:' + mount_point,
                        dict(title=mount_point)))
        
        storage_udi = device.GetProperty('block.storage_device')
        obj = bus.get_object(HAL_SERVICE_NAME, storage_udi)
        storage_device = dbus.Interface(obj, HAL_DEVICE_IFACE)
        
        storage_bus = storage_device.GetProperty('storage.bus')
        storage_drive_type = storage_device.GetProperty('storage.drive_type')
        if storage_drive_type == 'sd_mmc':
            icon_name = 'media-flash-sd-mmc'
        else:
            icon_name = 'media-flash-usb'
        
        logging.debug('mounted volume %s' % mount_point)
        self._add_button(mount_id, label, icon_name, udi)

    def _add_button(self, volume_id, volume_name, icon_name, udi):
        if self.get_children():
            group = self.get_children()[0]
        else:
            group = None

        palette = Palette(volume_name)
        palette.props.position = Palette.TOP

        button = RadioToolButton(icon_name, group=group)
        button.set_palette(palette)
        button.connect('toggled', self._button_toggled_cb, volume_id)
        self.insert(button, -1)
        button.show()

        if udi and volume_id:
            menu_item = gtk.MenuItem(_('Unmount'))
            menu_item.connect('activate', self._unmount_activated_cb, udi, volume_id, button)
            palette.append_menu_item(menu_item)
            menu_item.show()
        
        if len(self.get_children()) > 1:
            self.show()

    def _button_toggled_cb(self, button, volume_id):
        if button.props.active:
            self.emit('volume-changed', volume_id)

    def _unmount_activated_cb(self, menu_item, udi, volume_id, button):
        datastore.unmount(volume_id)
        self._unmount_device(udi)
        self.remove(button)
        self.get_children()[0].props.active = True
        
        if len(self.get_children()) < 2:
            self.hide()

    def _unmount_device(self, udi):
        bus = dbus.SystemBus()
        device_object = bus.get_object(HAL_SERVICE_NAME, udi)
        volume = dbus.Interface(device_object, HAL_VOLUME_IFACE)
        volume.Unmount([])