PageRenderTime 215ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/hotot/view.py

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