#example of how to use the Master provider
import dbus, gobject

from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)


def on_address_changed(timestamp, address, accuracy):
	# 0 accuracy is no data
	if accuracy[0] == 0:
		return   
	print address
	print timestamp
	print accuracy
		
def idle():
    on_address_changed(*address.GetAddress())
    return False
	
bus = dbus.SessionBus()
#The provider used is the Master
master = bus.get_object('org.freedesktop.Geoclue.Master', 
						'/org/freedesktop/Geoclue/Master')
							   
#Create the client (MasterClient)
client = bus.get_object('org.freedesktop.Geoclue.Master', master.Create())

#set the accuracy for the client
#SetRequirements     (in  'i'accuracy_level,
#                     in  'i'time,
#                     in  'b'require_updates,
#                     in  'i'allowed_resources)
#accuracy_level - 0 to 7 (NONE, COUNTRY, REGION, LOCALITY, POSTALCODE, STREET, DETAILED)
#allowed_resources - 0 (NONE), 1 << 0 (NETWORK), 1 << 1 (CELL), 1 << 2 (GPS), ALL ((1 << 10) -1)

#client.SetRequirements(LOCALITY, 0, True, NETWORK)
client.SetRequirements(3, 0, True, 1)
	
#get address
address = dbus.Interface(client, dbus_interface='org.freedesktop.Geoclue.Address')
address.connect_to_signal("AddressChanged", on_address_changed)
client.AddressStart()
	
gobject.idle_add(idle)
	
loop = gobject.MainLoop()
loop.run()


