Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/volumestoolbar.py
blob: 6de0fc853a0025c4332d42954dc248fccb086b8f (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
# 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 logging
from gettext import gettext as _

import gobject
import gtk

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

import volumesmanager

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)

        gobject.idle_add(self._set_up_volumes)

    def _set_up_volumes(self):
        volumes_manager = volumesmanager.get_volumes_manager()
        volumes_manager.connect('volume-added', self._volume_added_cb)
        volumes_manager.connect('volume-removed', self._volume_removed_cb)
        for volume in volumes_manager.get_volumes():
            self._add_button(volume)

    def _volume_added_cb(self, volumes_manager, volume):
        self._add_button(volume)

    def _volume_removed_cb(self, volumes_manager, volume):
        self._remove_button(volume)

    def _add_button(self, volume):
        logging.debug('VolumeToolbar._add_button: %r' % volume.name)

        if self.get_children():
            group = self.get_children()[0]
        else:
            group = None

        palette = Palette(volume.name)

        button = VolumeButton(volume, group)
        button.set_palette(palette)
        button.connect('toggled', self._button_toggled_cb, volume)
        self.insert(button, -1)
        button.show()

        if volume.can_unmount:
            menu_item = gtk.MenuItem(_('Unmount'))
            menu_item.connect('activate', self._unmount_activated_cb, volume)
            palette.menu.append(menu_item)
            menu_item.show()
        
        if len(self.get_children()) > 1:
            self.show()

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

    def _unmount_activated_cb(self, menu_item, volume):
        logging.debug('VolumesToolbar._unmount_activated_cb: %r', volume.udi)
        volume.unmount()

    def _remove_button(self, volume):
        for button in self.get_children():
            if button.volume.id == volume.id:
                logging.debug('_remove_button: removing button for udi: %r' % volume.udi)
                self.remove(button)
                self.get_children()[0].props.active = True
                
                if len(self.get_children()) < 2:
                    self.hide()
                return
        logging.debug('_remove_button: couldn''t find a button for udi: %r' % volume.udi)

class VolumeButton(RadioToolButton):
    def __init__(self, volume, group):
        RadioToolButton.__init__(self, volume.icon_name, group, volume.icon_color)
        self.volume = volume
        self.drag_dest_set(gtk.DEST_DEFAULT_ALL,
                           [('journal-object-id', 0, 0)],
                           gtk.gdk.ACTION_COPY)
        self.connect('drag-data-received', self._drag_data_received_cb)

    def _drag_data_received_cb(self, widget, drag_context, x, y, selection_data, info, timestamp):
        jobject = datastore.get(selection_data.data)
        datastore.copy(jobject, self.volume.id)