1/*
2 * Copyright (C) 2021 Frederic Martinsons
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 *
17 * Authors: Frederic Martinsons <frederic.martinsons@gmail.com>
18 */
19
20/* A dummy service which just own a dbus name and implement a method to quit*/
21
22#include <gio/gio.h>
23#include <glib.h>
24
25static GDBusNodeInfo *introspection_data = NULL;
26static GMainLoop *loop = NULL;
27static const gchar introspection_xml[] =
28 "<node>"
29 " <interface name='org.gtk.GDBus.FakeService'>"
30 " <method name='Quit'/>"
31 " </interface>"
32 "</node>";
33
34static void
35incoming_method_call (GDBusConnection *connection,
36 const gchar *sender,
37 const gchar *object_path,
38 const gchar *interface_name,
39 const gchar *method_name,
40 GVariant *parameters,
41 GDBusMethodInvocation *invocation,
42 gpointer user_data)
43{
44 if (g_strcmp0 (str1: method_name, str2: "Quit") == 0)
45 {
46 g_dbus_method_invocation_return_value (invocation, NULL);
47 g_main_loop_quit (loop);
48 }
49}
50
51static const GDBusInterfaceVTable interface_vtable = {
52 incoming_method_call,
53 NULL,
54 NULL
55};
56
57static void
58on_bus_acquired (GDBusConnection *connection,
59 const gchar *name,
60 gpointer user_data)
61{
62 guint registration_id;
63 GError *error = NULL;
64 g_test_message (format: "Acquired a message bus connection");
65
66 registration_id = g_dbus_connection_register_object (connection,
67 object_path: "/org/gtk/GDBus/FakeService",
68 interface_info: introspection_data->interfaces[0],
69 vtable: &interface_vtable,
70 NULL, /* user_data */
71 NULL, /* user_data_free_func */
72 error: &error);
73 g_assert_no_error (error);
74 g_assert (registration_id > 0);
75}
76
77static void
78on_name_acquired (GDBusConnection *connection,
79 const gchar *name,
80 gpointer user_data)
81{
82 g_test_message (format: "Acquired the name %s", name);
83}
84
85static void
86on_name_lost (GDBusConnection *connection,
87 const gchar *name,
88 gpointer user_data)
89{
90 g_test_message (format: "Lost the name %s", name);
91}
92
93gint
94main (gint argc, gchar *argv[])
95{
96 guint id;
97
98 loop = g_main_loop_new (NULL, FALSE);
99 introspection_data = g_dbus_node_info_new_for_xml (xml_data: introspection_xml, NULL);
100 g_assert (introspection_data != NULL);
101
102 id = g_bus_own_name (bus_type: G_BUS_TYPE_SESSION,
103 name: "org.gtk.GDBus.FakeService",
104 flags: G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
105 G_BUS_NAME_OWNER_FLAGS_REPLACE,
106 bus_acquired_handler: on_bus_acquired,
107 name_acquired_handler: on_name_acquired,
108 name_lost_handler: on_name_lost,
109 user_data: loop,
110 NULL);
111
112 g_main_loop_run (loop);
113
114 g_bus_unown_name (owner_id: id);
115 g_main_loop_unref (loop);
116 g_dbus_node_info_unref (info: introspection_data);
117
118 return 0;
119}
120

source code of gtk/subprojects/glib/gio/tests/fake-service-name.c