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

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_device_iface = dbus.Interface(self.gsm_device_obj, 'org.freesmartphone.GSM.Device')
        self.gsm_sim_iface = dbus.Interface(self.gsm_device_obj, 'org.freesmartphone.GSM.SIM')
        self.gsm_network_iface = dbus.Interface(self.gsm_device_obj, 'org.freesmartphone.GSM.Network')
        self.gsm_call_iface = dbus.Interface(self.gsm_device_obj, 'org.freesmartphone.GSM.Call')
        self.gsm_test_iface = dbus.Interface(self.gsm_device_obj, 'org.freesmartphone.GSM.Test')

        self.gsm_sim_iface.RetrievePhonebook("contacts",
                                             reply_handler = self.cbPhonebookReply,
                                             error_handler = lambda e: (dbg("cbPhonebookError %s" % e), self.loop.quit()))

    def cbPhonebookReply(self, result):
        #dbg("cbPhonebookReply %s" % result)
        for i in result:
            print(repr(i))
        self.loop.quit()

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

