Package conduit :: Package platform :: Module WebBrowserMozilla
[hide private]

Source Code for Module conduit.platform.WebBrowserMozilla

  1  import os.path 
  2  import gtkmozembed 
  3   
  4  import logging 
  5  log = logging.getLogger("WebBrowser") 
  6   
  7  import conduit.platform 
  8  import conduit.utils.Singleton as Singleton 
  9   
10 -class _MozConfig(Singleton.Singleton):
11 """ 12 A Singleton whose only responsibilty is to configure gtkmozembed to 13 use the correct profile path. Gtkmozembed only allows its profile 14 path to be set once 15 """ 16 17 DEFAULT_PROFILE = 'default' 18
19 - def __init__(self, **kwargs):
20 self._profile = kwargs.get('profile', self.DEFAULT_PROFILE) 21 self._profileDir = kwargs.get('profileDir', self._get_profile_subdir()) 22 23 log.info("Configuring Mozilla profile dir") 24 25 self._create_prefs_js() 26 gtkmozembed.set_profile_path(self._profileDir, self._profile)
27
28 - def _get_profile_subdir(self):
29 """ 30 Some webbrowsers need a profile dir. Make it if 31 it doesnt exist 32 """ 33 subdir = os.path.join(conduit.USER_DIR, 'mozilla') 34 profdir = os.path.join(subdir, self._profile) 35 if not os.access(profdir, os.F_OK): 36 os.makedirs(profdir) 37 return subdir
38
39 - def _create_prefs_js(self):
40 """ 41 Create the file prefs.js in the mozilla profile directory. This 42 file does things like turn off the warning when navigating to https pages. 43 """ 44 prefsContent = """\ 45 # Mozilla User Preferences 46 user_pref("security.warn_entering_secure", false); 47 user_pref("security.warn_entering_weak", false); 48 user_pref("security.warn_viewing_mixed", false); 49 user_pref("security.warn_leaving_secure", false); 50 user_pref("security.warn_submit_insecure", false); 51 user_pref("security.warn_entering_secure.show_once", false); 52 user_pref("security.warn_entering_weak.show_once", false); 53 user_pref("security.warn_viewing_mixed.show_once", false); 54 user_pref("security.warn_leaving_secure.show_once", false); 55 user_pref("security.warn_submit_insecure.show_once", false); 56 user_pref("security.enable_java", false); 57 user_pref("browser.xul.error_pages.enabled", false); 58 user_pref("general.useragent.vendor", "%s"); 59 user_pref("general.useragent.vendorSub", "%s"); 60 user_pref("general.useragent.vendorComment", "%s"); 61 """ % ("Conduit",conduit.VERSION,"http://www.conduit-project.org") 62 63 if conduit.GLOBALS.settings.proxy_enabled(): 64 log.info("Setting mozilla proxy details") 65 host,port,user,password = conduit.GLOBALS.settings.get_proxy() 66 prefsContent += """\ 67 user_pref("network.proxy.type", 1); 68 user_pref("network.proxy.http", "%s"); 69 user_pref("network.proxy.http_port", %d); 70 user_pref("network.proxy.ssl", "%s"); 71 user_pref("network.proxy.ssl_port", %s); 72 user_pref("network.proxy.share_proxy_settings", true); 73 """ % (host,port,host,port) 74 75 prefsPath = os.path.join(self._profileDir,self._profile,'prefs.js') 76 f = open(prefsPath, "wt") 77 f.write(prefsContent) 78 f.close()
79
80 -class WebBrowserImpl(conduit.platform.WebBrowser):
81 """ 82 Wraps the GTK embeddable Mozilla in the WebBrowser interface 83 """
84 - def __init__(self, **kwargs):
85 conduit.platform.WebBrowser.__init__(self) 86 87 #lazy import and other hoops necessary because 88 self._mozconfig = _MozConfig(**kwargs) 89 90 self.url_load_request = False # flag to break load_url recursion 91 self.location = "" 92 93 self.moz = gtkmozembed.MozEmbed() 94 self.moz.connect("link-message", self._signal_link_message) 95 self.moz.connect("open-uri", self._signal_open_uri) 96 self.moz.connect("location", self._signal_location) 97 self.moz.connect("progress", self._signal_progress) 98 self.moz.connect("net-start", self._signal_net_start) 99 self.moz.connect("net-stop", self._signal_net_stop)
100
101 - def widget(self):
102 return self.moz
103
104 - def load_url(self, str):
105 self.url_load_request = True # don't handle open-uri signal 106 self.moz.load_url(str) # emits open-uri signal 107 self.url_load_request = False # handle open-uri again
108
109 - def stop_load(self):
110 self.moz.stop_load()
111 114
115 - def _signal_open_uri(self, object, uri):
116 if self.url_load_request: 117 return False # proceed as requested 118 else: 119 return self.emit("open_uri", uri)
120
121 - def _signal_location(self, object):
122 self.location_changed(self.moz.get_location())
123
124 - def location_changed(self, location):
125 self.location = location 126 self.emit("location_changed",self.location)
127
128 - def _signal_progress(self, object, cur, maxim):
129 if maxim < 1: 130 self.emit("loading_progress", -1.0) 131 else: 132 self.emit("loading_progress", (cur/maxim))
133
134 - def _signal_net_start(self, object):
135 self.emit("loading_started")
136
137 - def _signal_net_stop(self, object):
138 self.emit("loading_finished")
139