Package advene :: Package gui :: Package views :: Module html
[hide private]
[frames] | no frames]

Source Code for Module advene.gui.views.html

  1  # 
  2  # Advene: Annotate Digital Videos, Exchange on the NEt 
  3  # Copyright (C) 2008 Olivier Aubert <olivier.aubert@liris.cnrs.fr> 
  4  # 
  5  # Advene is free software; you can redistribute it and/or modify 
  6  # it under the terms of the GNU General Public License as published by 
  7  # the Free Software Foundation; either version 2 of the License, or 
  8  # (at your option) any later version. 
  9  # 
 10  # Advene is distributed in the hope that it will be useful, 
 11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13  # GNU General Public License for more details. 
 14  # 
 15  # You should have received a copy of the GNU General Public License 
 16  # along with Advene; if not, write to the Free Software 
 17  # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 18  # 
 19  """HTML viewer component. 
 20   
 21  FIXME: add navigation buttons (back, history) 
 22  """ 
 23   
 24  import gtk 
 25  import urllib 
 26   
 27  engine=None 
 28  try: 
 29      import gtkmozembed 
 30      gtkmozembed.set_comp_path('') 
 31      engine='mozembed' 
 32  except ImportError: 
 33      try: 
 34          import gtkhtml2 
 35          engine='gtkhtml2' 
 36      except ImportError: 
 37          pass 
 38   
 39  from gettext import gettext as _ 
 40  from advene.gui.views import AdhocView 
 41   
42 -class gtkhtml_wrapper:
43 - def __init__(self, controller=None, notify=None):
44 self.controller=controller 45 self.notify=notify 46 self.history = [] 47 self.current = "" 48 self.widget = self.build_widget()
49
50 - def refresh(self, *p):
51 self.set_url(self.current) 52 return True
53
54 - def back(self, *p):
55 if len(self.history) <= 1: 56 self.log(_("Cannot go back: first item in history")) 57 else: 58 # Current URL 59 u=self.history.pop() 60 # Previous one 61 self.set_url(self.history[-1]) 62 return True
63
64 - def set_url(self, url):
65 self.update_history(url) 66 d=self.component.document 67 d.clear() 68 69 u=urllib.urlopen(url) 70 71 d.open_stream(u.info().type) 72 for l in u: 73 d.write_stream (l) 74 75 u.close() 76 d.close_stream() 77 78 self.current=url 79 if self.notify: 80 self.notify(url=url) 81 return True
82
83 - def get_url(self):
84 return self.current
85
86 - def update_history(self, url):
87 if not self.history: 88 self.history.append(url) 89 elif self.history[-1] != url: 90 self.history.append(url) 91 return
92
93 - def build_widget(self):
94 w=gtk.ScrolledWindow() 95 w.set_policy (gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) 96 97 c=gtkhtml2.View() 98 c.document=gtkhtml2.Document() 99 100 def request_url(document, url, stream): 101 print "request url", url, stream 102 pass
103 104 def link_clicked(document, link): 105 u=self.get_url() 106 if u: 107 url=urllib.basejoin(u, link) 108 else: 109 url=link 110 self.set_url(url) 111 return True
112 113 c.document.connect('link-clicked', link_clicked) 114 c.document.connect('request-url', request_url) 115 116 c.get_vadjustment().set_value(0) 117 w.set_hadjustment(c.get_hadjustment()) 118 w.set_vadjustment(c.get_vadjustment()) 119 c.document.clear() 120 c.set_document(c.document) 121 w.add(c) 122 self.component=c 123 return w 124
125 -class mozembed_wrapper:
126 - def __init__(self, controller=None, notify=None):
127 self.controller=controller 128 self.notify=notify 129 self.widget=self.build_widget()
130
131 - def refresh(self, *p):
132 self.component.reload(0) 133 return True
134
135 - def back(self, *p):
136 self.component.go_back() 137 return True
138
139 - def set_url(self, url):
140 self.component.load_url(url) 141 return True
142
143 - def get_url(self):
144 return self.component.get_location()
145
146 - def build_widget(self):
147 w=gtkmozembed.MozEmbed() 148 # A profile must be initialized, cf 149 # http://www.async.com.br/faq/pygtk/index.py?req=show&file=faq19.018.htp 150 gtkmozembed.set_profile_path("/tmp", "foobar") 151 152 def update_location(c): 153 if self.notify: 154 self.notify(url=self.get_url()) 155 return False
156 157 def update_label(c): 158 if self.notify: 159 self.notify(label=c.get_link_message()) 160 return False
161 162 w.connect('location', update_location) 163 w.connect('link-message', update_label) 164 self.component=w 165 return w 166
167 -class HTMLView(AdhocView):
168 _engine = engine 169 view_name = _("HTML Viewer") 170 view_id = 'htmlview' 171 tooltip = _("Embedded HTML widget")
172 - def __init__ (self, controller=None, url=None):
173 super(HTMLView, self).