# 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 . import urllib import logging from gettext import gettext as _ from gi.repository import Gtk from gi.repository import Gdk from gi.repository import GLib from gi.repository import WebKit from gi.repository import GObject from sugar_network import webui_port from sugar3 import wm from sugar3.graphics.window import Window from sugar3.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.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) scrolled.set_shadow_type(Gtk.ShadowType.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(Gdk.Cursor(Gdk.CursorType.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))