summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZeeshan Ali (Khattak) <zeeshanak@gnome.org>2015-10-06 18:35:49 +0100
committerZeeshan Ali (Khattak) <zeeshanak@gnome.org>2015-10-09 14:44:41 +0100
commitc9ef691d1756b17d3456f230818aba642c521b98 (patch)
treec6b77c79761f6bae83349e438d13bcc91cff8c6b
parentaf336ae4682c6f698474c73a4c19037cdb1cb13d (diff)
demo: Adapt where-am-i to use convenience API
This reduces the code by 50%, thus nicely demonstrating how easy the new API makes it to get location. https://bugs.freedesktop.org/show_bug.cgi?id=68658
-rw-r--r--demo/Makefile.am12
-rw-r--r--demo/where-am-i.c340
2 files changed, 73 insertions, 279 deletions
diff --git a/demo/Makefile.am b/demo/Makefile.am
index adddfd4..036be6c 100644
--- a/demo/Makefile.am
+++ b/demo/Makefile.am
@@ -1,12 +1,20 @@
demodir = $(libexecdir)/geoclue-2.0/demos
+if BUILD_LIBGEOCLUE
demo_PROGRAMS = where-am-i
+else
+demo_PROGRAMS =
+endif
where_am_i_SOURCES = where-am-i.c
where_am_i_CFLAGS = $(GEOCLUE_CFLAGS) \
$(WARN_CFLAGS) \
- -DLOCALEDIR="\"$(datadir)/locale\""
-where_am_i_LDADD = $(GEOCLUE_LIBS)
+ -DLOCALEDIR="\"$(datadir)/locale\"" \
+ -I$(top_builddir)/libgeoclue \
+ -I$(top_builddir)/src/public-api
+where_am_i_LDADD = $(GEOCLUE_LIBS) \
+ -L$(top_builddir)/libgeoclue \
+ -lgeoclue-2
appsdir = $(datadir)/applications
apps_DATA = geoclue-where-am-i.desktop \
diff --git a/demo/where-am-i.c b/demo/where-am-i.c
index 76e3fb1..8720f5a 100644
--- a/demo/where-am-i.c
+++ b/demo/where-am-i.c
@@ -23,17 +23,9 @@
#include <config.h>
#include <stdlib.h>
-#include <glib.h>
#include <locale.h>
#include <glib/gi18n.h>
-#include <gio/gio.h>
-
-typedef enum {
- GCLUE_ACCURACY_LEVEL_COUNTRY = 1,
- GCLUE_ACCURACY_LEVEL_CITY = 4,
- GCLUE_ACCURACY_LEVEL_STREET = 6,
- GCLUE_ACCURACY_LEVEL_EXACT = 8,
-} GClueAccuracyLevel;
+#include <geoclue.h>
/* Commandline options */
static gint timeout = 30; /* seconds */
@@ -63,323 +55,119 @@ static GOptionEntry entries[] =
{ NULL }
};
-GDBusProxy *manager;
+GClueClient *client = NULL;
GMainLoop *main_loop;
static gboolean
on_location_timeout (gpointer user_data)
{
- GDBusProxy *client = G_DBUS_PROXY (user_data);
-
- g_object_unref (client);
- g_object_unref (manager);
+ g_clear_object (&client);
g_main_loop_quit (main_loop);
return FALSE;
}
static void
-on_location_proxy_ready (GObject *source_object,
- GAsyncResult *res,
- gpointer user_data)
+print_location (GClueLocation *location)
{
- GDBusProxy *location = G_DBUS_PROXY (source_object);
- GVariant *value;
- gdouble latitude, longitude, accuracy, altitude, speed, heading;
+ gdouble altitude, speed, heading;
const char *desc;
- gsize desc_len;
- GError *error = NULL;
-
- location = g_dbus_proxy_new_for_bus_finish (res, &error);
- if (error != NULL) {
- g_critical ("Failed to connect to GeoClue2 service: %s", error->message);
-
- exit (-7);
- }
-
- value = g_dbus_proxy_get_cached_property (location, "Latitude");
- latitude = g_variant_get_double (value);
- value = g_dbus_proxy_get_cached_property (location, "Longitude");
- longitude = g_variant_get_double (value);
- value = g_dbus_proxy_get_cached_property (location, "Accuracy");
- accuracy = g_variant_get_double (value);
- value = g_dbus_proxy_get_cached_property (location, "Altitude");
- altitude = g_variant_get_double (value);
- value = g_dbus_proxy_get_cached_property (location, "Speed");
- speed = g_variant_get_double (value);
- value = g_dbus_proxy_get_cached_property (location, "Heading");
- heading = g_variant_get_double (value);
g_print ("\nNew location:\n");
g_print ("Latitude: %f°\nLongitude: %f°\nAccuracy: %f meters\n",
- latitude,
- longitude,
- accuracy);
+ gclue_location_get_latitude (location),
+ gclue_location_get_longitude (location),
+ gclue_location_get_accuracy (location));
+
+ altitude = gclue_location_get_altitude (location);
if (altitude != -G_MAXDOUBLE)
g_print ("Altitude: %f meters\n", altitude);
+ speed = gclue_location_get_speed (location);
if (speed >= 0)
g_print ("Speed: %f meters/second\n", speed);
+ heading = gclue_location_get_heading (location);
if (heading >= 0)
g_print ("Heading: %f°\n", heading);
- value = g_dbus_proxy_get_cached_property (location, "Description");
- desc = g_variant_get_string (value, &desc_len);
- if (desc_len > 0)
+ desc = gclue_location_get_description (location);
+ if (strlen (desc) > 0)
g_print ("Description: %s\n", desc);
-
- g_object_unref (location);
-}
-
-static void
-on_client_props_changed (GDBusProxy *client,
- GVariant *changed_properties,
- GStrv invalidated_properties,
- gpointer user_data)
-{
- GVariantIter *iter;
- const gchar *key;
- GVariant *value;
-
- if (g_variant_n_children (changed_properties) <= 0)
- return;
-
- g_variant_get (changed_properties, "a{sv}", &iter);
- while (g_variant_iter_loop (iter, "{&sv}", &key, &value)) {
-
- if (g_strcmp0 (key, "Active") != 0)
- continue;
-
- if (!g_variant_get_boolean (value)) {
- g_print ("Geolocation disabled. Quiting..\n");
- on_location_timeout (client);
- }
- }
- g_variant_iter_free (iter);
-}
-
-static void
-on_client_signal (GDBusProxy *client,
- gchar *sender_name,
- gchar *signal_name,
- GVariant *parameters,
- gpointer user_data)
-{
- char *location_path;
- if (g_strcmp0 (signal_name, "LocationUpdated") != 0)
- return;
-
- g_assert (g_variant_n_children (parameters) > 1);
- g_variant_get_child (parameters, 1, "&o", &location_path);
-
- g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
- G_DBUS_PROXY_FLAGS_NONE,
- NULL,
- "org.freedesktop.GeoClue2",
- location_path,
- "org.freedesktop.GeoClue2.Location",
- NULL,
- on_location_proxy_ready,
- user_data);
}
static void
-on_start_ready (GObject *source_object,
- GAsyncResult *res,
- gpointer user_data)
-{
- GDBusProxy *client = G_DBUS_PROXY (source_object);
- GVariant *results;
- GError *error = NULL;
-
- results = g_dbus_proxy_call_finish (client, res, &error);
- if (results == NULL) {
- g_critical ("Failed to start GeoClue2 client: %s", error->message);
-
- exit (-6);
- }
-
- g_variant_unref (results);
-}
-
-static void
-on_client_proxy_ready (GObject *source_object,
- GAsyncResult *res,
- gpointer user_data)
+on_location_proxy_ready (GObject *source_object,
+ GAsyncResult *res,
+ gpointer user_data)
{
- GDBusProxy *client;
+ GClueLocation *location;
GError *error = NULL;
- client = g_dbus_proxy_new_for_bus_finish (res, &error);
+ location = gclue_location_proxy_new_for_bus_finish (res, &error);
if (error != NULL) {
g_critical ("Failed to connect to GeoClue2 service: %s", error->message);
- exit (-5);
- }
-
- g_signal_connect (client, "g-signal",
- G_CALLBACK (on_client_signal), user_data);
- g_signal_connect (client, "g-properties-changed",
- G_CALLBACK (on_client_props_changed), user_data);
-
- g_dbus_proxy_call (client,
- "Start",
- NULL,
- G_DBUS_CALL_FLAGS_NONE,
- -1,
- NULL,
- on_start_ready,
- user_data);
-
- g_timeout_add_seconds (timeout, on_location_timeout, client);
-}
-
-static void
-on_set_accuracy_level_ready (GObject *source_object,
- GAsyncResult *res,
- gpointer user_data)
-{
- GDBusProxy *client_props = G_DBUS_PROXY (source_object);
- GVariant *results;
- GError *error = NULL;
-
- results = g_dbus_proxy_call_finish (client_props, res, &error);
- if (results == NULL) {
- g_critical ("Failed to start GeoClue2 client: %s", error->message);
-
- exit (-8);
+ exit (-2);
}
- g_variant_unref (results);
- g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
- G_DBUS_PROXY_FLAGS_NONE,
- NULL,
- "org.freedesktop.GeoClue2",
- g_dbus_proxy_get_object_path (client_props),
- "org.freedesktop.GeoClue2.Client",
- NULL,
- on_client_proxy_ready,
- user_data);
+ print_location (location);
+ g_object_unref (location);
}
static void
-on_set_desktop_id_ready (GObject *source_object,
- GAsyncResult *res,
- gpointer user_data)
+on_location_updated (GClueClient *client,
+ const char *old_location,
+ const char *new_location,
+ gpointer user_data)
{
- GDBusProxy *client_props = G_DBUS_PROXY (source_object);
- GVariant *results;
- GVariant *level;
- GError *error = NULL;
-
- results = g_dbus_proxy_call_finish (client_props, res, &error);
- if (results == NULL) {
- g_critical ("Failed to start GeoClue2 client: %s", error->message);
-
- exit (-4);
- }
- g_variant_unref (results);
-
- level = g_variant_new ("u", accuracy_level);
-
- g_dbus_proxy_call (client_props,
- "Set",
- g_variant_new ("(ssv)",
- "org.freedesktop.GeoClue2.Client",
- "RequestedAccuracyLevel",
- level),
- G_DBUS_CALL_FLAGS_NONE,
- -1,
- NULL,
- on_set_accuracy_level_ready,
- user_data);
+ gclue_location_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
+ G_DBUS_PROXY_FLAGS_NONE,
+ "org.freedesktop.GeoClue2",
+ new_location,
+ NULL,
+ on_location_proxy_ready,
+ NULL);
}
static void
-on_client_props_proxy_ready (GObject *source_object,
- GAsyncResult *res,
- gpointer user_data)
+on_client_active_notify (GClueClient *client,
+ GParamSpec *pspec,
+ gpointer user_data)
{
- GDBusProxy *client_props;
- GVariant *desktop_id;
- GError *error = NULL;
-
- client_props = g_dbus_proxy_new_for_bus_finish (res, &error);
- if (error != NULL) {
- g_critical ("Failed to connect to GeoClue2 service: %s", error->message);
-
- exit (-3);
- }
-
- desktop_id = g_variant_new ("s", "geoclue-where-am-i");
+ if (gclue_client_get_active (client))
+ return;
- g_dbus_proxy_call (client_props,
- "Set",
- g_variant_new ("(ssv)",
- "org.freedesktop.GeoClue2.Client",
- "DesktopId",
- desktop_id),
- G_DBUS_CALL_FLAGS_NONE,
- -1,
- NULL,
- on_set_desktop_id_ready,
- user_data);
+ g_print ("Geolocation disabled. Quiting..\n");
+ on_location_timeout (NULL);
}
static void
-on_get_client_ready (GObject *source_object,
+on_location_fetched (GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
- GVariant *results;
- const char *client_path;
- GError *error = NULL;
-
- results = g_dbus_proxy_call_finish (manager, res, &error);
- if (results == NULL) {
- g_critical ("Failed to connect to GeoClue2 service: %s", error->message);
-
- exit (-2);
- }
-
- g_assert (g_variant_n_children (results) > 0);
- g_variant_get_child (results, 0, "&o", &client_path);
-
- g_print ("Client object: %s\n", client_path);
-
- g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
- G_DBUS_PROXY_FLAGS_NONE,
- NULL,
- "org.freedesktop.GeoClue2",
- client_path,
- "org.freedesktop.DBus.Properties",
- NULL,
- on_client_props_proxy_ready,
- manager);
- g_variant_unref (results);
-}
-
-static void
-on_manager_proxy_ready (GObject *source_object,
- GAsyncResult *res,
- gpointer user_data)
-{
+ GClueLocation *location;
GError *error = NULL;
- manager = g_dbus_proxy_new_for_bus_finish (res, &error);
+ location = gclue_fetch_location_finish (&client, res, &error);
if (error != NULL) {
g_critical ("Failed to connect to GeoClue2 service: %s", error->message);
exit (-1);
}
+ g_print ("Client object: %s\n",
+ g_dbus_proxy_get_object_path (G_DBUS_PROXY (client)));
- g_dbus_proxy_call (manager,
- "GetClient",
- NULL,
- G_DBUS_CALL_FLAGS_NONE,
- -1,
- NULL,
- on_get_client_ready,
- NULL);
+ print_location (location);
+ g_object_unref (location);
+
+ g_signal_connect (client,
+ "location-updated",
+ G_CALLBACK (on_location_updated),
+ NULL);
+ g_signal_connect (client,
+ "notify::active",
+ G_CALLBACK (on_client_active_notify),
+ NULL);
}
gint
@@ -401,15 +189,13 @@ main (gint argc, gchar *argv[])
}
g_option_context_free (context);
- g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
- G_DBUS_PROXY_FLAGS_NONE,
- NULL,
- "org.freedesktop.GeoClue2",
- "/org/freedesktop/GeoClue2/Manager",
- "org.freedesktop.GeoClue2.Manager",
- NULL,
- on_manager_proxy_ready,
- NULL);
+ g_timeout_add_seconds (timeout, on_location_timeout, NULL);
+
+ gclue_fetch_location ("geoclue-where-am-i",
+ accuracy_level,
+ NULL,
+ on_location_fetched,
+ NULL);
main_loop = g_main_loop_new (NULL, FALSE);
g_main_loop_run (main_loop);