diff options
Diffstat (limited to 'src/location-geoclue2.c')
-rw-r--r-- | src/location-geoclue2.c | 166 |
1 files changed, 50 insertions, 116 deletions
diff --git a/src/location-geoclue2.c b/src/location-geoclue2.c index a60867b..b1489db 100644 --- a/src/location-geoclue2.c +++ b/src/location-geoclue2.c @@ -55,9 +55,7 @@ static void mark_error(struct location_state *state) { g_mutex_lock(&state->lock); - state->error = 1; - g_mutex_unlock(&state->lock); pipeutils_signal(state->pipe_fd_write); @@ -65,9 +63,7 @@ mark_error(struct location_state *state) /* Handle position change callbacks */ static void -geoclue_client_signal_cb(GDBusProxy *client, gchar *sender_name, - gchar *signal_name, GVariant *parameters, - gpointer user_data) +geoclue_client_signal_cb(GDBusProxy *client, gchar *sender_name, gchar *signal_name, GVariant *parameters, gpointer user_data) { struct location_state *state = user_data; const gchar *location_path; @@ -78,26 +74,19 @@ geoclue_client_signal_cb(GDBusProxy *client, gchar *sender_name, (void) sender_name; /* Only handle LocationUpdated signals */ - if (g_strcmp0(signal_name, "LocationUpdated") != 0) { + if (g_strcmp0(signal_name, "LocationUpdated")) return; - } /* Obtain location path */ g_variant_get_child(parameters, 1, "&o", &location_path); /* Obtain location */ error = NULL; - location = g_dbus_proxy_new_sync( - g_dbus_proxy_get_connection(client), - G_DBUS_PROXY_FLAGS_NONE, - NULL, - "org.freedesktop.GeoClue2", - location_path, - "org.freedesktop.GeoClue2.Location", - NULL, &error); - if (location == NULL) { - g_printerr(_("Unable to obtain location: %s.\n"), - error->message); + location = g_dbus_proxy_new_sync(g_dbus_proxy_get_connection(client), G_DBUS_PROXY_FLAGS_NONE, + NULL, "org.freedesktop.GeoClue2", location_path, + "org.freedesktop.GeoClue2.Location", NULL, &error); + if (!location) { + weprintf(_("Unable to obtain location: %s.\n"), error->message); g_error_free(error); mark_error(state); return; @@ -131,23 +120,18 @@ on_name_appeared(GDBusConnection *conn, const gchar *name, GDBusProxy *geoclue_manager; GError *error; GVariant *ret_v; + gchar *dbus_error; (void) name; (void) name_owner; /* Obtain GeoClue Manager */ error = NULL; - geoclue_manager = g_dbus_proxy_new_sync( - conn, - G_DBUS_PROXY_FLAGS_NONE, - NULL, - "org.freedesktop.GeoClue2", - "/org/freedesktop/GeoClue2/Manager", - "org.freedesktop.GeoClue2.Manager", - NULL, &error); - if (geoclue_manager == NULL) { - g_printerr(_("Unable to obtain GeoClue Manager: %s.\n"), - error->message); + geoclue_manager = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE, NULL, + "org.freedesktop.GeoClue2", "/org/freedesktop/GeoClue2/Manager", + "org.freedesktop.GeoClue2.Manager", NULL, &error); + if (!geoclue_manager) { + weprintf(_("Unable to obtain GeoClue Manager: %s.\n"), error->message); g_error_free(error); mark_error(state); return; @@ -155,15 +139,10 @@ on_name_appeared(GDBusConnection *conn, const gchar *name, /* Obtain GeoClue Client path */ error = NULL; - client_path_v = - g_dbus_proxy_call_sync(geoclue_manager, - "GetClient", - NULL, - G_DBUS_CALL_FLAGS_NONE, - -1, NULL, &error); - if (client_path_v == NULL) { - g_printerr(_("Unable to obtain GeoClue client path: %s.\n"), - error->message); + client_path_v = g_dbus_proxy_call_sync(geoclue_manager, "GetClient", NULL, + G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); + if (!client_path_v) { + weprintf(_("Unable to obtain GeoClue client path: %s.\n"), error->message); g_error_free(error); g_object_unref(geoclue_manager); mark_error(state); @@ -174,17 +153,10 @@ on_name_appeared(GDBusConnection *conn, const gchar *name, /* Obtain GeoClue client */ error = NULL; - geoclue_client = g_dbus_proxy_new_sync( - conn, - G_DBUS_PROXY_FLAGS_NONE, - NULL, - "org.freedesktop.GeoClue2", - client_path, - "org.freedesktop.GeoClue2.Client", - NULL, &error); - if (geoclue_client == NULL) { - g_printerr(_("Unable to obtain GeoClue Client: %s.\n"), - error->message); + geoclue_client = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE, NULL, "org.freedesktop.GeoClue2", + client_path, "org.freedesktop.GeoClue2.Client", NULL, &error); + if (!geoclue_client) { + weprintf(_("Unable to obtain GeoClue Client: %s.\n"), error->message); g_error_free(error); g_variant_unref(client_path_v); g_object_unref(geoclue_manager); @@ -196,16 +168,11 @@ on_name_appeared(GDBusConnection *conn, const gchar *name, /* Set desktop id (basename of the .desktop file) */ error = NULL; - ret_v = g_dbus_proxy_call_sync( - geoclue_client, - "org.freedesktop.DBus.Properties.Set", - g_variant_new("(ssv)", - "org.freedesktop.GeoClue2.Client", - "DesktopId", - g_variant_new("s", "redshift")), - G_DBUS_CALL_FLAGS_NONE, - -1, NULL, &error); - if (ret_v == NULL) { + ret_v = g_dbus_proxy_call_sync(geoclue_client, "org.freedesktop.DBus.Properties.Set", + g_variant_new("(ssv)", "org.freedesktop.GeoClue2.Client", + "DesktopId", g_variant_new("s", "redshift")), + G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); + if (!ret_v) { /* Ignore this error for now. The property is not available in early versions of GeoClue2. */ } else { @@ -214,18 +181,12 @@ on_name_appeared(GDBusConnection *conn, const gchar *name, /* Set distance threshold */ error = NULL; - ret_v = g_dbus_proxy_call_sync( - geoclue_client, - "org.freedesktop.DBus.Properties.Set", - g_variant_new("(ssv)", - "org.freedesktop.GeoClue2.Client", - "DistanceThreshold", - g_variant_new("u", 50000)), - G_DBUS_CALL_FLAGS_NONE, - -1, NULL, &error); - if (ret_v == NULL) { - g_printerr(_("Unable to set distance threshold: %s.\n"), - error->message); + ret_v = g_dbus_proxy_call_sync(geoclue_client, "org.freedesktop.DBus.Properties.Set", + g_variant_new("(ssv)", "org.freedesktop.GeoClue2.Client", + "DistanceThreshold", g_variant_new("u", 50000)), + G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); + if (!ret_v) { + weprintf(_("Unable to set distance threshold: %s.\n"), error->message); g_error_free(error); g_object_unref(geoclue_client); g_object_unref(geoclue_manager); @@ -236,26 +197,17 @@ on_name_appeared(GDBusConnection *conn, const gchar *name, g_variant_unref(ret_v); /* Attach signal callback to client */ - g_signal_connect(geoclue_client, "g-signal", - G_CALLBACK(geoclue_client_signal_cb), - user_data); + g_signal_connect(geoclue_client, "g-signal", G_CALLBACK(geoclue_client_signal_cb), user_data); /* Start GeoClue client */ error = NULL; - ret_v = g_dbus_proxy_call_sync(geoclue_client, - "Start", - NULL, - G_DBUS_CALL_FLAGS_NONE, - -1, NULL, &error); - if (ret_v == NULL) { - g_printerr(_("Unable to start GeoClue client: %s.\n"), - error->message); + ret_v = g_dbus_proxy_call_sync(geoclue_client, "Start", NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); + if (!ret_v) { + weprintf(_("Unable to start GeoClue client: %s.\n"), error->message); if (g_dbus_error_is_remote_error(error)) { - gchar *dbus_error = g_dbus_error_get_remote_error( - error); - if (g_strcmp0(dbus_error, DBUS_ACCESS_ERROR) == 0) { + dbus_error = g_dbus_error_get_remote_error( error); + if (!g_strcmp0(dbus_error, DBUS_ACCESS_ERROR)) print_denial_message(); - } g_free(dbus_error); } g_error_free(error); @@ -270,8 +222,7 @@ on_name_appeared(GDBusConnection *conn, const gchar *name, /* Callback when GeoClue disappears from the bus */ static void -on_name_vanished(GDBusConnection *connection, const gchar *name, - gpointer user_data) +on_name_vanished(GDBusConnection *connection, const gchar *name, gpointer user_data) { struct location_state *state = user_data; @@ -279,9 +230,7 @@ on_name_vanished(GDBusConnection *connection, const gchar *name, (void) name; g_mutex_lock(&state->lock); - state->available = 0; - g_mutex_unlock(&state->lock); pipeutils_signal(state->pipe_fd_write); @@ -314,13 +263,9 @@ run_geoclue2_loop(void *state_) g_main_context_push_thread_default(context); state->loop = g_main_loop_new(context, FALSE); - watcher_id = g_bus_watch_name( - G_BUS_TYPE_SYSTEM, - "org.freedesktop.GeoClue2", - G_BUS_NAME_WATCHER_FLAGS_AUTO_START, - on_name_appeared, - on_name_vanished, - state, NULL); + watcher_id = g_bus_watch_name(G_BUS_TYPE_SYSTEM, "org.freedesktop.GeoClue2", + G_BUS_NAME_WATCHER_FLAGS_AUTO_START, + on_name_appeared, on_name_vanished, state, NULL); /* Listen for closure of pipe */ pipe_channel = g_io_channel_unix_new(state->pipe_fd_write); @@ -348,8 +293,7 @@ location_geoclue2_init(struct location_state **state) #if !GLIB_CHECK_VERSION(2, 35, 0) g_type_init(); #endif - *state = malloc(sizeof(**state)); - if (*state == NULL) return -1; + *state = emalloc(sizeof(**state)); return 0; } @@ -367,7 +311,7 @@ location_geoclue2_start(struct location_state *state) state->location.lon = 0; if (pipeutils_create_nonblocking(pipefds)) { - fputs(_("Failed to start GeoClue2 provider!\n"), stderr); + weprintf(_("Failed to start GeoClue2 provider!\n")); return -1; } @@ -385,14 +329,12 @@ location_geoclue2_start(struct location_state *state) static void location_geoclue2_free(struct location_state *state) { - if (state->pipe_fd_read != -1) { + if (state->pipe_fd_read != -1) close(state->pipe_fd_read); - } /* Closing the pipe should cause the thread to exit. */ g_thread_join(state->thread); state->thread = NULL; - g_mutex_clear(&state->lock); free(state); @@ -401,18 +343,16 @@ location_geoclue2_free(struct location_state *state) static void location_geoclue2_print_help(FILE *f) { - fputs(_("Use the location as discovered by a GeoClue2 provider.\n"), - f); + fputs(_("Use the location as discovered by a GeoClue2 provider.\n"), f); fputs("\n", f); } static int -location_geoclue2_set_option(struct location_state *state, - const char *key, const char *value) +location_geoclue2_set_option(struct location_state *state, const char *key, const char *value) { (void) state; (void) value; - fprintf(stderr, _("Unknown method parameter: `%s'.\n"), key); + weprintf(_("Unknown method parameter: `%s'.\n"), key); return -1; } @@ -423,25 +363,19 @@ location_geoclue2_get_fd(struct location_state *state) } static int -location_geoclue2_handle( - struct location_state *state, - struct location *location, int *available) +location_geoclue2_handle(struct location_state *state, struct location *location, int *available) { int error; pipeutils_handle_signal(state->pipe_fd_read); g_mutex_lock(&state->lock); - error = state->error; *location = state->location; *available = state->available; - g_mutex_unlock(&state->lock); - if (error) return -1; - - return 0; + return error ? -1 : 0; } |