#!/usr/bin/python
# browser-proxy.py
# version 1.4 by xiojason
# simple python-dbus service that proxies osso_browser events to Fennec
# based on code from http://paste.lisp.org/display/45824
#
# installation:
# 1. place this file in /home/user as browser-proxy.py
# 2. make executable (chmod +x browser-proxy.py)
# 3. change /usr/share/dbus-1/services/com.nokia.osso_browser.service to read:
# [D-BUS Service]
# Name=com.nokia.osso_browser
# Exec=/home/user/browser-proxy.py
# 4. disable tablet-browser-service (aka browserd) (optional, saves memory)
# 5. you may wish to reboot.
#
# notes:
# - opening urls will be a bit slow. it takes some time to relay the messages to Tear
# - modifying the osso_browser.service file prevents MicroB/browserd from working.
#   if you wish to restore MicroB/browserd, change the service file's contents back to:
# [D-BUS Service]
# Name=com.nokia.osso_browser
# Exec=/usr/bin/browser
#   and re-enable tablet-browser-service again.
#
# revision history:
# 1.0
# 1.1 -> fixed wrong capitalization in dbus message
# 1.2 -> removed return values, added osso_browser/request namespace (fixes Pidgin)
# 1.3 -> sniff for local paths, prefix with file:// (fixes feedcircuit)
# 1.4 -> can now manually launch MicroB/browser -- while open, it will be used instead
#     -- without --print-reply, the initial launching message seems to get lost
# 1.5 -> removed dbus-launch that caused other software to hang. Used subprocess to 
#        spawn new processes instead of replacing the current process, which DBUS wasn't
#        taking to well.

import sys
import os
import gtk
import dbus
import dbus.service
import dbus.glib
import subprocess


def log(message):
    outf = open("/tmp/bp.log", "a")
    outf.write(message)
    outf.write("\n")
    outf.close()


class ProxyBrowserService(dbus.service.Object):
    def __init__(self):
        bus_name = dbus.service.BusName('com.nokia.osso_browser', bus=dbus.SessionBus())
        dbus.service.Object.__init__(self, bus_name, '/com/nokia/osso_browser')
        dbus.service.Object.__init__(self, bus_name, '/com/nokia/osso_browser/request')

    def OpenAddress(self, uri):
        log(uri)
        if uri[0] == '/':
            print "prefixing apparent local path with file://"
            uri = "file://" + uri

        subprocess.Popen(['/usr/bin/fennec', uri])



    @dbus.service.method(dbus_interface='com.nokia.osso_browser', in_signature='s')
    def load_url(self, uri):
        log("load_url")
        self.OpenAddress(uri)

    @dbus.service.method(dbus_interface='com.nokia.osso_browser', in_signature='s')
    def open_new_window(self, uri):
        log("open_new_window")
        self.OpenAddress(uri)

    @dbus.service.method(dbus_interface='com.nokia.osso_browser')
    def top_application(self):
        log("top_application")        
        if os.system("lsof /usr/sbin/browserd") != 0:
            os.system("/usr/sbin/browserd -d")
        os.execl('/usr/bin/browser', '/usr/bin/browser')

pbrowser = ProxyBrowserService()
log("before main")
try:
    gtk.main()
except:
    log("error")
    log(repr(sys.exc_info()))
log("after main")
