Python, Maemo, WLAN & webbrowser

Just scratched an itch with a couple of lines of Python code, which launches a browser if my cellphone (Nokia N900, Maemo 5) connects to a specific WLAN. The WLAN names to look for, as well as the URL that is opened in the browser are specified in gconf.
The itch is about WLANs with login pages/portals and is reported in Maemo bug tracker as Bug 7678.

I put the source on but also “inlined” here (spaces might get truncated by WordPress):


This is a quick hack that launches a browser when the device connects to an IAP with a
name that is present in gconf key 

There's no fancy GUI. To set the IAP names to listen for, run:
gconftool-2 -s --type=list --list-type=string "/apps/wlan-browser-launcher/iap-names" "[limbergwendt2,CSWLAN]"

To change the URL that opens, run
gconftool-2 -s --type=string "/apps/wlan-browser-launcher/url" ""

All gconf values are re-read on each connect signal.

Based on Conic examples on PyMaemo wiki by Lauro Moura [lauro].

import conic
import dbus
import gobject
import gnome.gconf
import dbus.glib
import webbrowser

GCONF_KEY_NAMES = "/apps/wlan-browser-launcher/iap-names"
GCONF_KEY_URL = "/apps/wlan-browser-launcher/url"

iap_id_to_name = {}
names_to_look_for = []

def load_iap_id_to_name(connection):
    global iap_id_to_name
    iap_id_to_name = {}
    iaps = connection.get_all_iaps()
    for iap in iaps:
        iap_id = iap.get_id()
        iap_name = iap.get_name()
        iap_id_to_name[iap_id] = iap_name

def load_names_to_look_for():
    global names_to_look_for, url_to_open
    gc = gnome.gconf.client_get_default()
    names_to_look_for = gc.get_list(GCONF_KEY_NAMES, gnome.gconf.VALUE_STRING)
    url_to_open = gc.get_string(GCONF_KEY_URL)
    print "Looking for IAP with these names: %s" % names_to_look_for

def connection_cb(connection, event):
    global iap_id_to_name, names_to_look_for, url_to_open
    status = event.get_status()
    iap_id = event.get_iap_id()
    if status == conic.STATUS_CONNECTED:
        iap_name = 'UNRECOGNIZED'
        if (iap_id_to_name.has_key(iap_id)):
            iap_name = iap_id_to_name[iap_id]
        print "CONNECTED (%s, %s, %i)" % (iap_id, iap_name, status)
        if (iap_name in names_to_look_for):
            if (not url_to_open):
                print "No URL setup for gconf key %s: not launching browser" % GCONF_KEY_URL
                print "Opening browser (pointing it to %s) for IAP %s" % (url_to_open, iap_name)

def start():
    connection = conic.Connection()
    connection.connect("connection-event", connection_cb)
    connection.set_property("automatic-connection-events", True)
    return connection
def stop(connection, loop):
    connection.set_property("automatic-connection-events", False)

def check_browser():
     gc = gnome.gconf.client_get_default()
     if (gc.get_string('/desktop/gnome/url-handlers/http/command') == 'epiphany %s'):
         raise Exception('Browser in gconf invalid (see NB#136012). Installation error.')

if __name__ == "__main__":
    loop = gobject.MainLoop()
    bus = dbus.SystemBus(private=True)
    connection = start()
    stop(connection, loop)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.