Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/webtoolbar.py
blob: 1a1cfc70a3579d9806733ae87cead45346f8d356 (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
# Copyright (C) 2006, Red Hat, Inc.
#
# 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 hippo
import gtk

from addressentry import AddressEntry

from sugar.graphics.toolbar import Toolbar
from sugar.graphics.iconbutton import IconButton

class WebToolbar(Toolbar):
    def __init__(self, embed):
        Toolbar.__init__(self)
        
        self._save = IconButton(icon_name='theme:stock-save')
        self._save.connect("activated", self._save_cb)
        self.append(self._save)
        
        self._back = IconButton(icon_name='theme:stock-back')
        self._back.props.active = False
        self._back.connect("activated", self._go_back_cb)
        self.append(self._back)

        self._forward = IconButton(icon_name='theme:stock-forward')
        self._forward.props.active = False
        self._forward.connect("activated", self._go_forward_cb)
        self.append(self._forward)

        self._stop_and_reload = IconButton(icon_name='theme:stock-close')
        self._stop_and_reload.connect("activated", self._stop_and_reload_cb)
        self.append(self._stop_and_reload)

        self._entry = AddressEntry()
        self._entry.connect("activated", self._entry_activate_cb)
        self.append(self._entry, hippo.PACK_EXPAND)

        self._post = IconButton(icon_name='theme:stock-add')
        self._post.props.active = False
        self._post.connect("activated", self._post_cb)
        self.append(self._post)

        self._embed = embed
        self._embed.connect("notify::progress", self._progress_changed_cb)
        self._embed.connect("notify::loading", self._loading_changed_cb)
        self._embed.connect("notify::address", self._address_changed_cb)
        self._embed.connect("notify::title", self._title_changed_cb)
        self._embed.connect("notify::can-go-back",
                            self._can_go_back_changed_cb)
        self._embed.connect("notify::can-go-forward",
                            self._can_go_forward_changed_cb)

        self._update_stop_and_reload_icon()

    def set_links_controller(self, links_controller):
        self._links_controller = links_controller
        self._post.props.active = True

    def _update_stop_and_reload_icon(self):
        if self._embed.props.loading:
            self._stop_and_reload.props.icon_name = 'theme:stock-close'
        else:
            self._stop_and_reload.props.icon_name = 'theme:stock-continue'

    def _progress_changed_cb(self, embed, spec):
        self._entry.props.progress = embed.props.progress

    def _loading_changed_cb(self, embed, spec):
        self._update_stop_and_reload_icon()

    def _address_changed_cb(self, embed, spec):
        self._entry.props.address = embed.props.address

    def _title_changed_cb(self, embed, spec):
        self._entry.props.title = embed.props.title

    def _can_go_back_changed_cb(self, embed, spec):
        self._back.props.active = embed.props.can_go_back

    def _can_go_forward_changed_cb(self, embed, spec):
        self._forward.props.active = embed.props.can_go_forward

    def _entry_activate_cb(self, entry):
        self._embed.load_url(entry.props.text)
        self._embed.grab_focus()

    def _go_back_cb(self, button):
        self._embed.go_back()
    
    def _go_forward_cb(self, button):
        self._embed.go_forward()

    def _stop_and_reload_cb(self, button):
        if self._embed.props.loading:
            self._embed.stop_load()
        else:
            self._embed.reload(0)

    def _post_cb(self, button):
        title = self._embed.get_title()
        address = self._embed.get_location()
        self._links_controller.post_link(title, address)

    def _save_cb(self, button):
        chooser = gtk.FileChooserDialog(title=None,
                                        action=gtk.FILE_CHOOSER_ACTION_SAVE,
                                        buttons=(gtk.STOCK_CANCEL,
                                                 gtk.RESPONSE_CANCEL,
                                                 gtk.STOCK_SAVE,
                                                 gtk.RESPONSE_OK))
        chooser.set_default_response(gtk.RESPONSE_OK)
        chooser.set_current_folder(os.path.expanduser('~'))
        response = chooser.run()

        if response == gtk.RESPONSE_OK:
            self._embed.save_document(chooser.get_filename())

        chooser.destroy()