From 37960fc0c7fad5ecd5cd78d8c39d9634a94d6c0b Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Thu, 13 Jun 2019 17:37:59 -0700 Subject: [PATCH] libsysprof-ui: add helper to check for sysprof-3 support --- src/libsysprof-ui/meson.build | 2 + src/libsysprof-ui/sysprof-check.c | 106 ++++++++++++++++++++++++++++++ src/libsysprof-ui/sysprof-check.h | 37 +++++++++++ src/libsysprof-ui/sysprof-ui.h | 1 + 4 files changed, 146 insertions(+) create mode 100644 src/libsysprof-ui/sysprof-check.c create mode 100644 src/libsysprof-ui/sysprof-check.h diff --git a/src/libsysprof-ui/meson.build b/src/libsysprof-ui/meson.build index da811e93..5a919ac5 100644 --- a/src/libsysprof-ui/meson.build +++ b/src/libsysprof-ui/meson.build @@ -5,6 +5,7 @@ libsysprof_ui_public_sources = [ 'sysprof-capture-view.c', 'sysprof-callgraph-aid.c', 'sysprof-callgraph-view.c', + 'sysprof-check.c', 'sysprof-color-cycle.c', 'sysprof-cpu-aid.c', 'sysprof-cpu-visualizer-row.c', @@ -56,6 +57,7 @@ libsysprof_ui_public_headers = [ 'sysprof-callgraph-aid.h', 'sysprof-callgraph-view.h', 'sysprof-cell-renderer-percent.h', + 'sysprof-check.h', 'sysprof-cpu-aid.h', 'sysprof-cpu-visualizer-row.h', 'sysprof-display.h', diff --git a/src/libsysprof-ui/sysprof-check.c b/src/libsysprof-ui/sysprof-check.c new file mode 100644 index 00000000..d7025790 --- /dev/null +++ b/src/libsysprof-ui/sysprof-check.c @@ -0,0 +1,106 @@ +/* sysprof-check.c + * + * Copyright 2019 Christian Hergert + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#define G_LOG_DOMAIN "sysprof-check" + +#include "config.h" + +#include "sysprof-check.h" + +static void +sysprof_check_supported_ping_cb (GObject *object, + GAsyncResult *result, + gpointer user_data) +{ + GDBusConnection *bus = (GDBusConnection *)object; + g_autoptr(GVariant) reply = NULL; + g_autoptr(GError) error = NULL; + g_autoptr(GTask) task = user_data; + + g_assert (G_IS_DBUS_CONNECTION (bus)); + g_assert (G_IS_ASYNC_RESULT (result)); + g_assert (G_IS_TASK (task)); + + if (!(reply = g_dbus_connection_call_finish (bus, result, &error))) + g_task_return_error (task, g_steal_pointer (&error)); + else + g_task_return_boolean (task, TRUE); +} + +static void +sysprof_check_supported_bus_cb (GObject *object, + GAsyncResult *result, + gpointer user_data) +{ + g_autoptr(GDBusConnection) bus = NULL; + g_autoptr(GError) error = NULL; + g_autoptr(GTask) task = user_data; + + g_assert (G_IS_ASYNC_RESULT (result)); + g_assert (G_IS_TASK (task)); + + if (!(bus = g_bus_get_finish (result, &error))) + g_task_return_error (task, g_steal_pointer (&error)); + else + g_dbus_connection_call (bus, + "org.gnome.Sysprof3", + "/org/gnome/Sysprof3", + "org.freedesktop.DBus.Peer", + "Ping", + g_variant_new ("()"), + NULL, + G_DBUS_CALL_FLAGS_NONE, + -1, + g_task_get_cancellable (task), + sysprof_check_supported_ping_cb, + g_object_ref (task)); +} + +void +sysprof_check_supported_async (GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data) +{ + g_autoptr(GTask) task = NULL; + + g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable)); + + task = g_task_new (NULL, cancellable, callback, user_data); + g_task_set_source_tag (task, sysprof_check_supported_async); + + /* Get access to the System D-Bus and check to see if we can ping the + * service that is found at org.gnome.Sysprof3. + */ + + g_bus_get (G_BUS_TYPE_SYSTEM, + cancellable, + sysprof_check_supported_bus_cb, + g_steal_pointer (&task)); + +} + +gboolean +sysprof_check_supported_finish (GAsyncResult *result, + GError **error) +{ + g_return_val_if_fail (G_IS_TASK (result), FALSE); + + return g_task_propagate_boolean (G_TASK (result), error); +} diff --git a/src/libsysprof-ui/sysprof-check.h b/src/libsysprof-ui/sysprof-check.h new file mode 100644 index 00000000..bb8f4e7c --- /dev/null +++ b/src/libsysprof-ui/sysprof-check.h @@ -0,0 +1,37 @@ +/* sysprof-check.h + * + * Copyright 2019 Christian Hergert + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#pragma once + +#include + +#include "sysprof-version-macros.h" + +G_BEGIN_DECLS + +SYSPROF_AVAILABLE_IN_ALL +void sysprof_check_supported_async (GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data); +SYSPROF_AVAILABLE_IN_ALL +gboolean sysprof_check_supported_finish (GAsyncResult *result, + GError **error); + +G_END_DECLS diff --git a/src/libsysprof-ui/sysprof-ui.h b/src/libsysprof-ui/sysprof-ui.h index 49d97869..4f020d5b 100644 --- a/src/libsysprof-ui/sysprof-ui.h +++ b/src/libsysprof-ui/sysprof-ui.h @@ -32,6 +32,7 @@ G_BEGIN_DECLS # include "sysprof-capture-view.h" # include "sysprof-callgraph-aid.h" # include "sysprof-cell-renderer-percent.h" +# include "sysprof-check.h" # include "sysprof-cpu-aid.h" # include "sysprof-cpu-visualizer-row.h" # include "sysprof-display.h"