#!/usr/bin/python2.5
#

__author__    = "Henri Bergius <henri.bergius@iki.fi>"
__version__   = "0.1.0"
__date__      = "2007-02-11"
__copyright__ = "Copyright (c) 2007 %s. All rights reserved." % __author__
__licence__   = "GPL"

import dbus
import osso
import gtk
import maemoplazer
import time
import sys

# This does the magick
def icd_status_changed(interface, method, arguments, user_data):
    print "DEBUG: icd_status_changed called"
    print "  interface: %s" % interface
    print "  method: %s" % method
    print "  arguments: %s" % arguments
    print "  user_data: %s" % user_data
    # Only plaze us if this is a new WLAN connection
    if (   method != 'WLAN_INFRA'
        or arguments != 'CONNECTED'):
        return
    sys.stderr.write("DEBUG: new WLAN connection found, call plazer in ")
    for d in (3,2,1):
        sys.stderr.write("%d " % d)
        time.sleep(1)
    print "now"
    try:
        maemoplazer.run()
    except:
        # DBUS traps exceptions during callbacks and prints very little of them
        print "got exception, trying to print traceback"
        import traceback
        traceback.print_exc()
        print "traceback printed (i hope)"
    return

# Register listener
bus = dbus.SystemBus()
bus.add_signal_receiver(icd_status_changed, 'status_changed', 'com.nokia.icd', 'com.nokia.icd', '/com/nokia/icd')

# We seem to need this to make the receiver work correctly
osso_c = osso.Context("maemoplazer_watcher", "0.0.1", False)

print "Listener activated"
# This will loop infinitely
gtk.main()
