geoclue-address.c 8.25 KB
Newer Older
1 2 3 4 5
/*
 * Geoclue
 * geoclue-address.c - Client API for accessing GcIfaceAddress
 *
 * Author: Iain Holmes <iain@openedhand.com>
6
 * Copyright 2007 by Garmin Ltd. or its subsidiaries
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
23 24
 */

25 26 27 28 29
/**
 * SECTION:geoclue-address
 * @short_description: Geoclue address client API
 *
 * #GeoclueAddress contains Address-related methods and signals. 
30 31
 * It is part of the Geoclue public C client API which uses D-Bus 
 * to communicate with the actual provider.
32
 * 
Jussi Kukkonen's avatar
Jussi Kukkonen committed
33
 * After a #GeoclueAddress is created with geoclue_address_new() or 
34
 * geoclue_master_client_create_address(), the 
Jussi Kukkonen's avatar
Jussi Kukkonen committed
35 36
 * geoclue_address_get_address() and geoclue_address_get_address_async() methods 
 * and the address-changed signal can be used to obtain the current address. 
37 38 39 40 41
 * 
 * Address #GHashTable keys are defined in 
 * <ulink url="geoclue-types.html">geoclue-types.h</ulink>. See also 
 * convenience functions in 
 * <ulink url="geoclue-address-details.html">geoclue-address-details.h</ulink>.
42 43
 */

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
#include <geoclue/geoclue-address.h>
#include <geoclue/geoclue-marshal.h>

#include "gc-iface-address-bindings.h"

typedef struct _GeoclueAddressPrivate {
	int dummy;
} GeoclueAddressPrivate;

enum {
	ADDRESS_CHANGED,
	LAST_SIGNAL
};

static guint32 signals[LAST_SIGNAL] = {0, };

#define GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GEOCLUE_TYPE_ADDRESS, GeoclueAddressPrivate))

G_DEFINE_TYPE (GeoclueAddress, geoclue_address, GEOCLUE_TYPE_PROVIDER);

static void
finalize (GObject *object)
{
	G_OBJECT_CLASS (geoclue_address_parent_class)->finalize (object);
}

static void
dispose (GObject *object)
{
	G_OBJECT_CLASS (geoclue_address_parent_class)->dispose (object);
}

static void
address_changed (DBusGProxy      *proxy,
		 int              timestamp,
79 80
		 GHashTable      *details,
		 GeoclueAccuracy *accuracy,
81 82 83
		 GeoclueAddress *address)
{
	g_signal_emit (address, signals[ADDRESS_CHANGED], 0, 
84
		       timestamp, details, accuracy);
85 86 87 88 89 90 91 92 93 94 95 96 97
}

static GObject *
constructor (GType                  type,
	     guint                  n_props,
	     GObjectConstructParam *props)
{
	GObject *object;
	GeoclueProvider *provider;

	object = G_OBJECT_CLASS (geoclue_address_parent_class)->constructor
		(type, n_props, props);
	provider = GEOCLUE_PROVIDER (object);
Jussi Kukkonen's avatar
Jussi Kukkonen committed
98
	
99
	dbus_g_proxy_add_signal (provider->proxy, "AddressChanged",
100
				 G_TYPE_INT, 
101
				 DBUS_TYPE_G_STRING_STRING_HASHTABLE,
102
				 GEOCLUE_ACCURACY_TYPE,
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
				 G_TYPE_INVALID);
	dbus_g_proxy_connect_signal (provider->proxy, "AddressChanged",
				     G_CALLBACK (address_changed),
				     object, NULL);

	return object;
}

static void
geoclue_address_class_init (GeoclueAddressClass *klass)
{
	GObjectClass *o_class = (GObjectClass *) klass;

	o_class->finalize = finalize;
	o_class->dispose = dispose;
	o_class->constructor = constructor;

	g_type_class_add_private (klass, sizeof (GeoclueAddressPrivate));
Jussi Kukkonen's avatar
Jussi Kukkonen committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134
	
	/**
	 * GeoclueAddress::address-changed:
	 * @address: the #GeoclueAddress object emitting the signal
	 * @timestamp: Time of address measurement (Unix timestamp)
	 * @details: Address details as #GHashTable.
	 * @accuracy: Accuracy of measurement as #GeoclueAccuracy
	 * 
	 * The address-changed signal is emitted each time the address changes. 
	 * See <ulink url="geoclue-types.html">geoclue-types.h</ulink> for the possible 
	 * GEOCLUE_ADDRESS_KEY_* keys used in @details.
	 * 
	 * Note that not all providers support signals.
	 */
135 136 137 138 139 140 141 142 143
	signals[ADDRESS_CHANGED] = g_signal_new ("address-changed",
						 G_TYPE_FROM_CLASS (klass),
						 G_SIGNAL_RUN_FIRST |
						 G_SIGNAL_NO_RECURSE,
						 G_STRUCT_OFFSET (GeoclueAddressClass, address_changed), 
						 NULL, NULL,
						 geoclue_marshal_VOID__INT_BOXED_BOXED,
						 G_TYPE_NONE, 3,
						 G_TYPE_INT, 
144 145
						 G_TYPE_POINTER,
						 G_TYPE_POINTER);
146 147 148 149 150 151 152
}

static void
geoclue_address_init (GeoclueAddress *address)
{
}

153 154 155 156 157 158 159 160 161
/**
 * geoclue_address_new:
 * @service: D-Bus service name
 * @path: D-Bus path name
 *
 * Creates a #GeoclueAddress with given D-Bus service name and path.
 * 
 * Return value: Pointer to a new #GeoclueAddress
 */
162 163 164 165 166 167 168 169 170 171 172
GeoclueAddress *
geoclue_address_new (const char *service,
		     const char *path)
{
	return g_object_new (GEOCLUE_TYPE_ADDRESS,
			     "service", service,
			     "path", path,
			     "interface", GEOCLUE_ADDRESS_INTERFACE_NAME,
			     NULL);
}

173 174 175
/**
 * geoclue_address_get_address:
 * @address: A #GeoclueAddress object
Jussi Kukkonen's avatar
Jussi Kukkonen committed
176
 * @timestamp: Pointer to returned time of address measurement (Unix timestamp) or %NULL
177 178 179
 * @details: Pointer to returned #GHashTable with address details or %NULL
 * @accuracy: Pointer to returned #GeoclueAccuracy or NULL
 * @error: Pointer to returned #Gerror or %NULL
180 181
 * 
 * Obtains the current address. @timestamp will contain the time of 
182 183 184 185 186
 * the actual address measurement. @accuracy is the estimated of the
 * accuracy of the current address information (see #GeoclueAccuracy 
 * for more details). @details is a hashtable with the address data, 
 * see <ulink url="geoclue-types.html">geoclue-types.h</ulink> for the 
 * hashtable keys.
187 188 189 190
 * 
 * If the caller is not interested in some values, the pointers can be 
 * left %NULL.
 * 
191
 * Return value: %TRUE if there is no @error
192
 */
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
gboolean
geoclue_address_get_address (GeoclueAddress   *address,
			     int              *timestamp,
			     GHashTable      **details,
			     GeoclueAccuracy **accuracy,
			     GError          **error)
{
	GeoclueProvider *provider = GEOCLUE_PROVIDER (address);

	if (!org_freedesktop_Geoclue_Address_get_address (provider->proxy,
							  timestamp, details,
							  accuracy, error)) {
		return FALSE;
	}

	return TRUE;
}
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234


typedef struct _GeoclueAddressAsyncData {
	GeoclueAddress *address;
	GCallback callback;
	gpointer userdata;
} GeoclueAddressAsyncData;

static void
get_address_async_callback (DBusGProxy              *proxy, 
			    int                      timestamp,
			    GHashTable              *details,
			    GeoclueAccuracy         *accuracy,
			    GError                  *error,
			    GeoclueAddressAsyncData *data)
{
	(*(GeoclueAddressCallback)data->callback) (data->address,
	                                           timestamp,
	                                           details,
	                                           accuracy,
	                                           error,
	                                           data->userdata);
	g_free (data);
}

Jussi Kukkonen's avatar
Jussi Kukkonen committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
/**
 * GeoclueAddressCallback:
 * @address: the #GeoclueAddress object emitting the signal
 * @timestamp: Time of address measurement (Unix timestamp)
 * @details: Address details as #GHashTable.
 * @accuracy: Accuracy of measurement as #GeoclueAccuracy
 * @error: Error as #Gerror (may be %NULL)
 * @userdata: User data pointer set in geoclue_position_get_position_async()
 * 
 * Callback function for geoclue_address_get_address_async().
 */

/**
 * geoclue_address_get_address_async:
 * @address: A #GeoclueAddress object
 * @callback: A #GeoclueAddressCallback function that should be called when return values are available
 * @userdata: pointer for user specified data
 * 
 * Function returns (essentially) immediately and calls @callback when current address
 * is available or when D-Bus timeouts.
 */
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
void 
geoclue_address_get_address_async (GeoclueAddress         *address,
				   GeoclueAddressCallback  callback,
				   gpointer                userdata)
{
	GeoclueProvider *provider = GEOCLUE_PROVIDER (address);
	GeoclueAddressAsyncData *data;
	
	data = g_new (GeoclueAddressAsyncData, 1);
	data->address = address;
	data->callback = G_CALLBACK (callback);
	data->userdata = userdata;
	
	org_freedesktop_Geoclue_Address_get_address_async
			(provider->proxy,
			 (org_freedesktop_Geoclue_Address_get_address_reply) get_address_async_callback,
			 data);
}