Debian Codegraph @

glib/gio/tests/fake-service-name.c

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