3 # version 2.2 by steven676 and xiojason
5 # simple python-dbus service that proxies osso_browser events to Tear
6 # based on code from http://paste.lisp.org/display/45824
8 # Copyright (C) 2009 Jason Simpson
9 # Copyright (C) 2009 Steven Luo
11 # This program is free software; you can redistribute it and/or
12 # modify it under the terms of the GNU General Public License
13 # as published by the Free Software Foundation; either version 2
14 # of the License, or (at your option) any later version.
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 # On Maemo systems, a copy of the GPL can be found in
26 # /usr/share/common-licenses/GPL .
35 # Set default configuration values
36 # These can be overriden by the config file $HOME/.config/browser-proxy
37 def setconfigdefaults():
38 global continuous_mode, default_browser, other_browser_cmd
40 # continuous_mode: 0 -- close after handling a request; 1 -- run
41 # continuously in the background
43 # default_browser: "tear", "microb", "fennec", "midori", or "other"
44 default_browser = "tear"
45 # If default browser is "other", what program to run (%s will be replaced
47 other_browser_cmd = ""
49 class BrowserLauncher:
50 def LaunchTear(self, uri):
51 # We should be able to just call the D-Bus service to open Tear ...
52 # but if Tear's not open, that causes D-Bus to start Tear and then
53 # pass it the OpenAddress call, which results in two browser windows.
54 # Properly fixing this probably requires Tear to provide a D-Bus method
55 # that opens an address in an existing window, but for now work around
56 # by just opening Tear via the command line if it's not running.
57 if os.system("pidof tear > /dev/null") == 0:
58 dbus.SessionBus().get_object('com.nokia.tear', '/com/nokia/tear').OpenAddress(uri, dbus_interface='com.nokia.Tear')
59 if continuous_mode == 0:
64 # parent process doesn't need to do anything
68 os.execl('/usr/bin/tear', '/usr/bin/tear', uri)
70 def LaunchMicroB(self, uri):
71 if os.system("pidof /usr/sbin/browserd > /dev/null") != 0:
73 os.system("/usr/sbin/browserd -d")
76 dbus.SessionBus().release_name("com.nokia.osso_browser")
77 if uri == "new_window":
78 # exec maemo-invoker directly instead of relying on the
79 # /usr/bin/browser symlink, since /usr/bin/browser may have been
80 # replaced with a shell script calling us via D-Bus
81 os.spawnl(os.P_WAIT, '/usr/bin/maemo-invoker', '/usr/bin/browser')
83 os.spawnl(os.P_WAIT, '/usr/bin/maemo-invoker', '/usr/bin/browser', '--url', uri)
85 os.system("kill `pidof /usr/sbin/browserd`")
87 dbus.SessionBus().request_name("com.nokia.osso_browser")
91 def LaunchOtherBrowser(self, uri):
92 if uri == "new_window":
95 # URI must be quoted and quotes in the URI must be URL-escaped
96 # to prevent the shell from interpreting any part of the URI
97 command = other_browser_cmd % "'%s'" % uri.replace("'", "%27")
99 # Couldn't substitute URI in, just launch the browser
100 print "other_browser_cmd should have a %s placeholder for the URI"
101 command = other_browser_cmd
103 subprocess.Popen(command, shell=True)
104 if continuous_mode == 0:
107 def UpdateDefaultBrowser(self):
108 global other_browser_cmd
109 if default_browser == "tear":
110 self.LaunchBrowser = self.LaunchTear
111 elif default_browser == "microb":
112 self.LaunchBrowser = self.LaunchMicroB
113 elif default_browser == "fennec":
114 # Cheat and reuse LaunchOtherBrowser, since we don't appear to
115 # need to do anything special
116 # TODO: does Fennec have a D-Bus API or signaling mechanism?
117 # Invoking the fennec binary appears to be somewhat expensive
118 other_browser_cmd = "fennec '%s'"
119 self.LaunchBrowser = self.LaunchOtherBrowser
120 elif default_browser == "midori":
121 other_browser_cmd = "midori '%s'"
122 self.LaunchBrowser = self.LaunchOtherBrowser
123 elif default_browser == "other":
124 if other_browser_cmd != "":
125 self.LaunchBrowser = self.LaunchOtherBrowser
127 print "default_browser is 'other', but no other_browser_cmd set -- using default"
128 self.LaunchBrowser = self.LaunchTear
130 print "Unknown default_browser %s, using default" % default_browser
131 self.LaunchBrowser = self.LaunchTear
134 self.UpdateDefaultBrowser()
136 class ProxyBrowserService(dbus.service.Object):
138 bus_name = dbus.service.BusName('com.nokia.osso_browser', bus=dbus.SessionBus())
139 dbus.service.Object.__init__(self, bus_name, '/com/nokia/osso_browser')
140 dbus.service.Object.__init__(self, bus_name, '/com/nokia/osso_browser/request')
142 def OpenAddress(self, uri):
146 print "prefixing apparent local path with file://"
147 uri = "file://" + uri
149 launcher.LaunchBrowser(uri)
151 @dbus.service.method(dbus_interface='com.nokia.osso_browser', in_signature='s')
152 def load_url(self, uri):
154 self.OpenAddress(uri)
156 @dbus.service.method(dbus_interface='com.nokia.osso_browser', in_signature='s')
157 def mime_open(self, uri):
159 self.OpenAddress(uri)
161 @dbus.service.method(dbus_interface='com.nokia.osso_browser', in_signature='s')
162 def open_new_window(self, uri):
163 print "open_new_window"
164 self.OpenAddress(uri)
166 @dbus.service.method(dbus_interface='com.nokia.osso_browser')
167 def top_application(self):
168 print "top_application"
169 launcher.LaunchMicroB("new_window")
171 # This method is an "undocumented", non-standard extension to the
172 # osso_browser D-Bus API, intended solely to allow a wrapper script
173 # replacing /usr/bin/browser to implement --url
174 @dbus.service.method(dbus_interface='com.nokia.osso_browser')
175 def switchboard_launch_microb(self, uri="new_window"):
176 print "switchboard_launch_microb"
177 launcher.LaunchMicroB(uri)
179 def readconfigfile(signalnum=None, frame=None):
180 # reset configuration to defaults
183 # read configuration from the config file, if available
185 execfile(os.getenv("HOME", "/home/user") + "/.config/browser-proxy", globals())
186 launcher.UpdateDefaultBrowser()
188 # No valid config file available
193 launcher = BrowserLauncher()
194 pbrowser = ProxyBrowserService()
200 def waitforzombies(signalnum, frame):
202 while os.waitpid(-1, os.WNOHANG) != (0, 0):
206 signal.signal(signal.SIGCHLD, waitforzombies)
207 signal.signal(signal.SIGHUP, readconfigfile)
209 loop = gobject.MainLoop()