diff --git a/src/DataViews/GeoView.py b/src/DataViews/GeoView.py index 0b001ff01..973ddec7e 100644 --- a/src/DataViews/GeoView.py +++ b/src/DataViews/GeoView.py @@ -4,6 +4,7 @@ # Gramps - a GTK+/GNOME based genealogy program # # Copyright (C) 2007-2008 Serge Noiraud +# Copyright (C) 2008 Benny Malengier # # 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 @@ -61,11 +62,10 @@ LOG = logging.getLogger(".GeoView") # #------------------------------------------------------------------------- import gen.lib -import ViewManager import PageView import Utils -import Errors import Config +from const import TEMP_DIR from BasicUtils import name_displayer as _nd #------------------------------------------------------------------------- @@ -73,23 +73,40 @@ from BasicUtils import name_displayer as _nd # Web interfaces # #------------------------------------------------------------------------- -WebKit = 0 + +NOWEB = 0 +WEBKIT = 1 +MOZIL = 2 + +WebKit = NOWEB try: import webkit - WebKit = 1 -except: - pass -try: - import gtkmozembed - WebKit = 2 + LOG.info("Using Webkit for HtmlView") + WebKit = WEBKIT except: pass + +if WebKit == NOWEB: + try: + import gtkmozembed + LOG.info("Using GtkMozembed for HtmlView") + WebKit = MOZIL + except: + pass + #no interfaces present, raise Error so that options for GeoView do not show if WebKit == 0 : Config.set(Config.GEOVIEW, False) LOG.warning(_("GeoView not enabled, no html plugin for GTK found.")) raise ImportError, 'No GTK html plugin found' +#------------------------------------------------------------------------- +# +# Gramps Modules +# +#------------------------------------------------------------------------- + + #------------------------------------------------------------------------- # # Constants @@ -97,8 +114,251 @@ if WebKit == 0 : #------------------------------------------------------------------------- # I think we should set the two following variable in const.py # They are used only with gtkmozembed. -MOZEMBED_PATH = "/tmp/" -MOZEMBED_SUBPATH = "browser-gramps" +MOZEMBED_PATH = TEMP_DIR +MOZEMBED_SUBPATH = Utils.get_empty_tempdir('mozembed_gramps') + +#------------------------------------------------------------------------- +# +# Renderer +# +#------------------------------------------------------------------------- +class Renderer(): + """ + Renderer renders the webpage. Several backend implementations are + possible + """ + def __init__(self): + self.window = None + + def get_window(self): + """ + Returns a container class with the widget that contains browser + window + """ + return self.window + + def show_all(self): + self.window.show_all() + + def open(self, url): + """ open the webpage at url + """ + raise NotImplementedError + + if (self.browser == 1): + self.m.open("file://"+htmlfile) + elif (self.browser == 2): + self.m.load_url("file://"+htmlfile) + elif (self.browser == 3): + self.m.openURL ("file://"+htmlfile); + +#------------------------------------------------------------------------- +# +# Renderer with WebKit +# +#------------------------------------------------------------------------- +class RendererWebkit(Renderer): + """ + Implementation of Renderer with Webkit + """ + def __init__(self): + Renderer.__init__(self) + self.window = webkit.WebView() + + def open(self, url): + self.window.open(url) + +class RendererMozilla(Renderer): + """ + Implementation of Renderer with gtkmozembed + """ + def __init__(self): + Renderer.__init__(self) + if hasattr(gtkmozembed, 'set_profile_path'): + set_profile_path = gtkmozembed.set_profile_path + else: + set_profile_path = gtkmozembed.gtk_moz_embed_set_profile_path + set_profile_path(MOZEMBED_PATH, MOZEMBED_SUBPATH) + self.__set_mozembed_proxy() + self.window = gtkmozembed.MozEmbed() + self.window.set_size_request(800, 600) + + def open(self, url): + self.window.load_url(url) + + def __set_mozembed_proxy(self): + """ + Try to see if we have some proxy environment variable. + http_proxy in our case. + The standard format is : http://[user:password@]proxy:port/ + """ + try: + proxy = os.environ['http_proxy'] + data = "" + if proxy: + host_port = None + parts = urlparse.urlparse(proxy) + if not parts[0] or parts[0] == 'http': + host_port = parts[1] + t = host_port.split(':') + host = t[0].strip() + if host: + try: + port = int(t[1]) + except: + user = host + u = t[1].split('@') + password = u[0] + host = u[1] + port = int(t[2]) + + if port and host: + port = str(port) + data += 'user_pref("network.proxy.type", 1);\r\n' + data += 'user_pref("network.proxy.http", "'+host+'");\r\n' + data += 'user_pref("network.proxy.http_port", '+port+');\r\n' + data += 'user_pref("network.proxy.no_proxies_on", '\ + '"127.0.0.1,localhost,localhost.localdomain");\r\n' + data += 'user_pref("network.proxy.share_proxy_settings", true);\r\n' + data += 'user_pref("network.http.proxy.pipelining", true);\r\n' + data += 'user_pref("network.http.proxy.keep-alive", true);\r\n' + data += 'user_pref("network.http.proxy.version", 1.1);\r\n' + data += 'user_pref("network.http.sendRefererHeader, 0);\r\n' + fd = file(MOZEMBED_PATH+MOZEMBED_SUBPATH+"/prefs.js","w+") + fd.write(data) + fd.close() + except: + try: # trying to remove pref.js in case of proxy change. + os.remove(MOZEMBED_PATH+MOZEMBED_SUBPATH+"/prefs.js") + except: + pass + pass # We don't use a proxy or the http_proxy variable is not set. + +#------------------------------------------------------------------------- +# +# HtmlView +# +#------------------------------------------------------------------------- +class HtmlView(PageView.PageView): + """ + HtmlView is a view showing a top widget with controls, and a bottom part + with an embedded webbrowser showing a given URL + """ + + def __init__(self, dbstate, uistate): + PageView.PageView.__init__(self, _('HtmlView'), dbstate, uistate) + + self.dbstate = dbstate + + def build_widget(self): + """ + Builds the interface and returns a gtk.Container type that + contains the interface. This containter will be inserted into + a gtk.Notebook page. + """ + global WebKit + + self.box = gtk.VBox(False, 4) + #top widget at the top + self.box.pack_start(self.top_widget(), False, False,0 ) + #web page under it in a scrolled window + self.table = gtk.Table(1, 1, False) + frame = gtk.ScrolledWindow(None, None) + frame.set_shadow_type(gtk.SHADOW_NONE) + frame.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) + frame.add_with_viewport(self.table) + self.table.get_parent().set_shadow_type(gtk.SHADOW_NONE) + self.table.set_row_spacings(1) + self.table.set_col_spacings(0) + + gobject.threads_init() + self.count = 0 + + if (WebKit == WEBKIT) : + # We use webkit + self.renderer = RendererWebkit() + elif (WebKit == MOZIL) : + # We use gtkmozembed + self.renderer = RendererMozilla() + + self.table.add(self.renderer.get_window()) + self.box.pack_start(frame, True, True, 0) + + #load a welcome html page + urlhelp = self.create_start_page() + self.renderer.open(urlhelp) + self.renderer.show_all() + + return self.box + + def top_widget(self): + """ + The default class gives a widget where user can type an url + """ + hbox = gtk.HBox(False,4) + self.urlfield = gtk.Entry() + self.urlfield.connect('activate', self._on_activate); + hbox.pack_start(self.urlfield, True, True, 4) + button = gtk.Button(stock=gtk.STOCK_APPLY) + button.connect('clicked', self._on_activate) + hbox.pack_start(button, False, False, 4) + return hbox + + def _on_activate(self, object): + url = self.urlfield.get_text() + if url.find('://') == -1: + url = 'http://'+ url + self.renderer.open(url) + + def build_tree(self): + """ + Rebuilds the current display. Called from ViewManager + """ + pass #htmlview is build on click and startup + + def get_stock(self): + """ + Returns the name of the stock icon to use for the display. + This assumes that this icon has already been registered with + GNOME as a stock icon. + """ + return 'gramps-geo' + + def define_actions(self): + pass + + def create_start_page(self): + """ + This command creates a default start page, and returns the URL of this + page. + """ + tmpdir = Utils.get_empty_tempdir('htmlview') + data = """ + + +
+ +