#!/usr/bin/python
import dbus
import dbus.glib
import gobject
import time
import os
import sys
import pygtk
import gtk
import pango

def dbg(s):
    print("%s %s" % (time.strftime("%H:%M:%S"), s))

class UI:
    def __init__(self, loop):
        self.loop = loop

        bus = dbus.SystemBus()
        self.gsm_device_obj = bus.get_object("org.freesmartphone.ogsmd", "/org/freesmartphone/GSM/Device")
        self.gsm_call_iface = dbus.Interface(self.gsm_device_obj, 'org.freesmartphone.GSM.Call')

        self.window = w = gtk.Window(gtk.WINDOW_TOPLEVEL)
        w.set_default_size(480, 300)
        w.set_border_width(10)
        w.connect("delete_event", (lambda w,e: False))
        w.connect("destroy", lambda d: self.loop.quit())

        vbox = gtk.VBox(False, 0)
        w.add(vbox)

        l = gtk.Label("Ready")
        l.modify_font(pango.FontDescription("lucida sans unicode 20"))
        self.status = l
        vbox.add(self.status)
        
        j = 0
        for i in "123456789*0#ABCD":
            if j % 3 == 0:
                hbox = gtk.HBox(True, 0)
                vbox.pack_start(hbox)
            b = gtk.Button(i)
            b.set_focus_on_click(False)
            b.set_property("can-focus", False)
            b.get_child().modify_font(pango.FontDescription("lucida sans unicode 40"))
            b.connect("pressed", self.cbSend, i)
            hbox.add(b)
            j += 1

        if False:
            hbox = gtk.HBox(True, 0)
            vbox.pack_start(hbox)
            b = gtk.Button("Quit")
            b.get_child().modify_font(pango.FontDescription("lucida sans unicode 40"))
            b.connect("pressed", self.cbQuit)
            hbox.add(b)

        w.show_all()

    def cbSend(self, w, code):
        dbg("cbSend %s" % code)
        self.status.set_text((self.status.get_text() + code)[-15:])
        self.gsm_call_iface.SendDtmf(code,
                                     reply_handler = self.cbSendDtmfReply,
                                     error_handler = self.cbSendDtmfError)
    def cbSendDtmfError(self, e):
        dbg("cbSendDtmfError %s" % e)
        self.status.set_text(("... %s" % e)[-15:])

    def cbSendDtmfReply(self):
        dbg("cbSendDtmfReply")

    def cbQuit(self, w):
        dbg("cbQuit")
        self.loop.quit()

if __name__ == "__main__":
    loop = gobject.MainLoop()
    UI(loop)
    loop.run()
