gc-backend.c 2.15 KB
Newer Older
Iain Holmes's avatar
Iain Holmes committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
/*
 * Geoclue
 * gc-backend.c - A backend object that handles the basic D-Bus required for
 *                a backend.
 *
 * Author: Iain Holmes <iain@openedhand.com>
 */

#include <config.h>

#include <glib-object.h>

#include <dbus/dbus-glib-bindings.h>
#include <dbus/dbus.h>

#include <geoclue/gc-backend.h>

G_DEFINE_ABSTRACT_TYPE (GcBackend, gc_backend, G_TYPE_OBJECT)

static void
finalize (GObject *object)
{
	((GObjectClass *) gc_backend_parent_class)->finalize (object);
}

static void
dispose (GObject *object)
{
	((GObjectClass *) gc_backend_parent_class)->dispose (object);
}

static void
gc_backend_class_init (GcBackendClass *klass)
{
	GObjectClass *o_class = (GObjectClass *) klass;

	o_class->finalize = finalize;
	o_class->dispose = dispose;
}

static void
gc_backend_init (GcBackend *backend)
{
	GError *error = NULL;

	backend->connection = dbus_g_bus_get (GEOCLUE_DBUS_BUS, &error);
	if (backend->connection == NULL) {
		g_warning ("%s was unable to create a connection to D-Bus: %s",
			   G_OBJECT_TYPE_NAME (backend), error->message);
		g_error_free (error);
	}
}

/**
 * gc_backend_set_details:
 * @backend: A #GcBackend object
 * @service: The service that the object implements
 * @path: The path that the object should be registered at
 *
 * Requests ownership of the @service name, and if that succeeds registers
 * @backend at @path.
 */
void
gc_backend_set_details (GcBackend  *backend,
			const char *service,
			const char *path)
{
	GError *error = NULL;
	DBusGProxy *driver;
	guint request_ret;

	g_return_if_fail (GC_IS_BACKEND (backend));
	g_return_if_fail (backend->connection != NULL);
	g_return_if_fail (service != NULL);
	g_return_if_fail (path != NULL);

	driver = dbus_g_proxy_new_for_name (backend->connection,
					    DBUS_SERVICE_DBUS,
					    DBUS_PATH_DBUS,
					    DBUS_INTERFACE_DBUS);

	if (!org_freedesktop_DBus_request_name (driver, service, 0,
						&request_ret, &error)) {
		g_warning ("%s was unable to register service %s: %s", 
			   G_OBJECT_TYPE_NAME (backend), service, 
			   error->message);
		g_error_free (error);
		return;
	}

	dbus_g_connection_register_g_object (backend->connection, 
					     path, G_OBJECT (backend));
}