• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
25 static GDBusNodeInfo *introspection_data = NULL;
26 static GMainLoop *loop = NULL;
27 static const gchar introspection_xml[] =
28     "<node>"
29     "    <interface name='org.gtk.GDBus.FakeService'>"
30     "        <method name='Quit'/>"
31     "    </interface>"
32     "</node>";
33 
34 static void
incoming_method_call(GDBusConnection * connection,const gchar * sender,const gchar * object_path,const gchar * interface_name,const gchar * method_name,GVariant * parameters,GDBusMethodInvocation * invocation,gpointer user_data)35 incoming_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 (method_name, "Quit") == 0)
45     {
46       g_dbus_method_invocation_return_value (invocation, NULL);
47       g_main_loop_quit (loop);
48     }
49 }
50 
51 static const GDBusInterfaceVTable interface_vtable = {
52   incoming_method_call,
53   NULL,
54   NULL
55 };
56 
57 static void
on_bus_acquired(GDBusConnection * connection,const gchar * name,gpointer user_data)58 on_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 ("Acquired a message bus connection");
65 
66   registration_id = g_dbus_connection_register_object (connection,
67                                                        "/org/gtk/GDBus/FakeService",
68                                                        introspection_data->interfaces[0],
69                                                        &interface_vtable,
70                                                        NULL, /* user_data */
71                                                        NULL, /* user_data_free_func */
72                                                        &error);
73   g_assert_no_error (error);
74   g_assert (registration_id > 0);
75 }
76 
77 static void
on_name_acquired(GDBusConnection * connection,const gchar * name,gpointer user_data)78 on_name_acquired (GDBusConnection *connection,
79                   const gchar     *name,
80                   gpointer         user_data)
81 {
82   g_test_message ("Acquired the name %s", name);
83 }
84 
85 static void
on_name_lost(GDBusConnection * connection,const gchar * name,gpointer user_data)86 on_name_lost (GDBusConnection *connection,
87               const gchar     *name,
88               gpointer         user_data)
89 {
90   g_test_message ("Lost the name %s", name);
91 }
92 
93 gint
main(gint argc,gchar * argv[])94 main (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 (introspection_xml, NULL);
100   g_assert (introspection_data != NULL);
101 
102   id = g_bus_own_name (G_BUS_TYPE_SESSION,
103                        "org.gtk.GDBus.FakeService",
104                        G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
105                            G_BUS_NAME_OWNER_FLAGS_REPLACE,
106                        on_bus_acquired,
107                        on_name_acquired,
108                        on_name_lost,
109                        loop,
110                        NULL);
111 
112   g_main_loop_run (loop);
113 
114   g_bus_unown_name (id);
115   g_main_loop_unref (loop);
116   g_dbus_node_info_unref (introspection_data);
117 
118   return 0;
119 }
120