Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/bookmarklettoolbar.py
blob: 18d93e93ed1c9c266810d5160ee55caeb2c2a432 (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
# Copyright (C) 2009, Lucian Branescu Mihaila
#
# 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

from gettext import gettext as _
import os
import gtk
import logging
import gobject

from sugar.graphics.toolbutton import ToolButton
from sugar.graphics.palette import Palette

import bookmarklets

# HACK until we have toolbox.get_toolbars()
_TOOLBAR_BROWSE = 2
_TOOLBAR_BOOKMARKLETS = 4

class BookmarkletButton(ToolButton):
    def __init__(self, toolbar, name, uri):
        self._name = name
        self._uri = uri
        self._toolbar = toolbar
        self._browser = toolbar._activity._browser
    
        # set up the button
        ToolButton.__init__(self, 'bookmarklet')
        self.connect('clicked', self._clicked_cb)
        toolbar.insert(self, -1)
        
        # and its palette
        palette = Palette(name, text_maxlen=50)
        self.set_palette(palette)
        
        menu_item = gtk.MenuItem(_('Remove'))
        menu_item.connect('activate', self._remove_cb)
        palette.menu.append(menu_item)
        menu_item.show()
        
        self.show()
            
    def animate(self):          
        gobject.timeout_add(500, self.set_icon, 'bookmarklet-thick')
        gobject.timeout_add(800, self.set_icon, 'bookmarklet')
        
    def flash(self):
        gobject.timeout_add(500, self.set_icon, 'bookmarklet-inverted')
        gobject.timeout_add(800, self.set_icon, 'bookmarklet')
                    
    def _clicked_cb(self, button):
        self._browser.load_uri(self._uri)

    def _remove_cb(self, widget):
        bookmarklets.get_store().remove(self._name)
        self.destroy()
        
    def destroy(self):
        del self._toolbar.bookmarklets[self._name]        
        
        if len(self._toolbar.bookmarklets) == 0:
            self._toolbar.destroy()
            
        ToolButton.destroy(self)
        
class BookmarkletToolbar(gtk.Toolbar):
    def __init__(self, activity):
        gtk.Toolbar.__init__(self)
        
        self._activity = activity
        self._browser = self._activity._browser

        self.bookmarklets = {}
            
    def add_bookmarklet(self, name):
        url = bookmarklets.get_store().get(name)
        self.bookmarklets[name] = BookmarkletButton(self, name, url)
        
    def destroy(self):
        self._activity.toolbox.remove_toolbar(_TOOLBAR_BOOKMARKLETS)
        self._activity.toolbox.set_current_toolbar(_TOOLBAR_BROWSE)
        
        gtk.Toolbar.destroy(self)