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
|
# Copyright (C) 2012 Aleksey Lim
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
import urllib
import logging
from gettext import gettext as _
import gtk
import glib
import webkit
import gobject
from sugar_network import webui_port
from sugar import wm
from sugar.graphics.window import Window
from sugar.activity.activityfactory import create_activity_id
from jarabe.view.palettes import BasePalette
from jarabe.plugins.sn import SN_BROWSER_NAME, get_client
_WEBKIT_LOAD_PROVISIONAL = 0
_WEBKIT_LOAD_FINISHED = 2
_WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT = 3
_WEBKIT_LOAD_FAILED = 4
_RETRY_TIMEOUT = 1
_logger = logging.getLogger('plugins.sn.browser')
class Browser(Window):
def __init__(self):
Window.__init__(self)
self._initial_load = True
self._reveal = False
self.set_title(_('Sugar Network'))
self.connect('realize', self.__realize_cb)
self.iconify()
scrolled = gtk.ScrolledWindow()
scrolled.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
scrolled.set_shadow_type(gtk.SHADOW_NONE)
self.set_canvas(scrolled)
# pylint: disable-msg=E1101
self._webkit = webkit.WebView()
scrolled.add(self._webkit)
self.show_all()
self._webkit.connect('notify::load-status', self.__load_status_cb)
get_client().connect('event', self.__Event_cb)
gobject.timeout_add_seconds(_RETRY_TIMEOUT, self._open)
def open_report(self, **kwargs):
self._open('_report', **kwargs)
self._reveal = True
def _open(self, *args, **kwargs):
url = '/'.join(('http://127.0.0.1:%s' % webui_port.value,) + args)
query = urllib.urlencode(kwargs)
if query:
url += '?' + query
_logger.debug('Open %s', url)
self._webkit.open(url)
def __load_status_cb(self, widget, pspec):
_logger.debug('Webkit status changed to %r',
self._webkit.props.load_status)
if self._webkit.props.load_status in (_WEBKIT_LOAD_FINISHED,
_WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT):
self._initial_load = False
self.get_window().set_cursor(None)
if self._reveal:
self.reveal()
self._reveal = False
elif self._webkit.props.load_status == _WEBKIT_LOAD_FAILED:
if self._initial_load:
gobject.timeout_add_seconds(_RETRY_TIMEOUT, self._open)
else:
self.get_window().set_cursor(None)
elif self._webkit.props.load_status == _WEBKIT_LOAD_PROVISIONAL:
self.get_window().set_cursor(gtk.gdk.Cursor(gtk.gdk.WATCH))
def __realize_cb(self, window):
wm.set_bundle_id(window.window, SN_BROWSER_NAME)
wm.set_activity_id(window.window, create_activity_id())
self.disconnect_by_func(self.__realize_cb)
def __Event_cb(self, event, data):
if event in ('mount', 'umount'):
self._open()
class Palette(BasePalette):
def __init__(self, activity):
self._activity = activity
BasePalette.__init__(self, activity)
def setup_palette(self):
title = self._activity.get_title()
self.set_primary_text(glib.markup_escape_text(title))
|