PageRenderTime 92ms CodeModel.GetById 40ms app.highlight 10ms RepoModel.GetById 40ms app.codeStats 0ms

/hotot/view.py

https://code.google.com/p/hotot/
Python | 100 lines | 86 code | 9 blank | 5 comment | 3 complexity | 024854480d39de7b77efae1d6db36aca MD5 | raw file
  1#!/usr/bin/env python
  2# -*- coding:utf8 -*-
  3import gtk
  4gtk.gdk.threads_init() ## fix issue 24
  5import webkit
  6import agent
  7import config
  8from webkit import WebView
  9import utils
 10import json
 11import gobject
 12
 13try: import i18n
 14except: from gettext import gettext as _
 15
 16class MainView(WebView):
 17    def __init__(self):
 18        WebView.__init__(self)
 19        self.load_finish_flag = False
 20        self.set_property('can-focus', True)
 21        self.set_property('can-default', True)
 22        self.set_full_content_zoom(1)
 23        self.clipbord = gtk.Clipboard()
 24
 25        settings = self.get_settings()
 26        try:
 27            settings.set_property('enable-universal-access-from-file-uris', True)
 28            settings.set_property('javascript-can-access-clipboard', True)
 29            settings.set_property('enable-default-context-menu', True)
 30            settings.set_property('enable-page-cache', True)
 31            settings.set_property('tab-key-cycles-through-elements', True)
 32            settings.set_property('enable-file-access-from-file-uris', True)
 33            settings.set_property('enable-spell-checking', False)
 34            settings.set_property('enable-caret-browsing', False)
 35        except:
 36            print 'Error: settings property was not set.'
 37        webkit.set_web_database_directory_path(config.DB_DIR)
 38        webkit.set_default_web_database_quota(1024**3L)
 39        ## bind events
 40        self.connect('navigation-requested', self.on_navigation_requested);
 41        self.connect('new-window-policy-decision-requested', self.on_new_window_requested);
 42        self.connect('script-alert', self.on_script_alert);
 43        self.connect('load-finished', self.on_load_finish);
 44        self.connect("hovering-over-link", self.on_over_link);
 45        templatefile = utils.get_ui_object(config.TEMPLATE)
 46        template = open(templatefile, 'rb').read()
 47        self.load_html_string(template, 'file://' + templatefile)
 48
 49    def on_navigation_requested(self, view, webframe, request):
 50        return self.handle_uri(request.get_uri())
 51
 52    def on_new_window_requested(self, view, frame, request, decision, u_data):
 53        return self.handle_uri(request.get_uri())
 54
 55    def handle_uri(self, uri):
 56        if uri.startswith('file://'):
 57            return False
 58        elif uri.startswith('hotot:'):
 59            self.on_hotot_action(uri)
 60            return True
 61        elif uri.startswith('about:'):
 62            return True
 63        else:
 64            utils.open_webbrowser(uri)
 65        return True
 66
 67    def on_script_alert(self, view, webframe, message):
 68        if message.startswith('hotot:'):
 69            self.on_hotot_action(message)
 70            return True
 71        return False
 72
 73    def on_hotot_action(self, uri):
 74        if uri.startswith('hotot:'):
 75            agent.crack_hotot(uri[6:])
 76        return True
 77
 78    def on_load_finish(self, view, webframe):
 79        self.load_finish_flag = True;
 80        agent.webv = self
 81        # overlay extra variables of web part
 82        variables = {
 83              'platform': 'Linux'
 84            , 'conf_dir': config.CONF_DIR
 85            , 'cache_dir': config.CACHE_DIR
 86            , 'avatar_cache_dir': config.AVATAR_CACHE_DIR
 87            , 'extra_fonts': utils.get_extra_fonts()
 88            , 'extra_exts': utils.get_extra_exts()
 89            , 'locale': utils.get_locale()
 90        };
 91        # and then, notify web part i am ready to work :)
 92        gobject.idle_add(view.execute_script, '''
 93            overlay_variables(%s);
 94            globals.load_flags = 1;
 95            ''' % json.dumps(variables))
 96
 97    def on_over_link(self, view, alt, href):
 98        href = href or ""
 99        if not alt and not href.startswith('file:'):
100            self.parent.set_tooltip_text(href)