1
#include "test_service_gdbus_wrapper.h"
5
const char * gdbus_wrapper_interface =
6
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
8
" <interface name=\"com.canonical.gdbus_wrapper\">\n"
9
"<!-- Properties -->\n"
12
"<!-- Functions -->\n"
13
" <method name=\"TestMethod\">\n"
15
" <arg type=\"s\" name=\"query\" direction=\"in\" />\n"
17
" <arg type=\"s\" name=\"target\" direction=\"out\" />\n"
21
" <signal name=\"TestSignal\">\n"
22
" <arg type=\"s\" name=\"target\" direction=\"out\" />\n"
25
"<!-- End of interesting stuff -->\n"
30
static void bus_got_cb (GObject *object, GAsyncResult * res, gpointer user_data);
31
static void bus_method (GDBusConnection *connection,
33
const gchar *object_path,
34
const gchar *interface_name,
35
const gchar *method_name,
37
GDBusMethodInvocation *invocation,
40
G_DEFINE_TYPE(ServiceGDBusWrapper, service_gdbus_wrapper, G_TYPE_OBJECT);
41
static GDBusNodeInfo * node_info = NULL;
42
static GDBusInterfaceInfo * iface_info = NULL;
43
static GDBusInterfaceVTable bus_vtable = {
44
method_call: bus_method,
50
struct _ServiceGDBusWrapperPrivate
52
GDBusConnection * bus;
53
GCancellable * bus_lookup;
54
guint bus_registration;
58
service_gdbus_wrapper_dispose(GObject* object)
60
ServiceGDBusWrapper* self = SERVICE_GDBUS_WRAPPER(object);
61
if (self->priv->bus_lookup != NULL) {
62
g_cancellable_cancel(self->priv->bus_lookup);
63
g_object_unref(self->priv->bus_lookup);
64
self->priv->bus_lookup = NULL;
67
if (self->priv->bus_registration != 0) {
68
g_dbus_connection_unregister_object(self->priv->bus, self->priv->bus_registration);
69
self->priv->bus_registration = 0;
72
if (self->priv->bus != NULL) {
73
g_object_unref(self->priv->bus);
74
self->priv->bus = NULL;
80
service_gdbus_wrapper_class_init(ServiceGDBusWrapperClass* klass)
82
G_OBJECT_CLASS(klass)->dispose = service_gdbus_wrapper_dispose;
83
g_type_class_add_private (klass, sizeof (ServiceGDBusWrapperPrivate));
85
if (node_info == NULL)
87
GError * error = NULL;
89
node_info = g_dbus_node_info_new_for_xml(gdbus_wrapper_interface, &error);
92
g_error("Unable to parse GDBUS_WRAPPER interface: %s", error->message);
97
if (node_info != NULL && iface_info == NULL)
99
iface_info = g_dbus_node_info_lookup_interface(node_info,"com.canonical.gdbus_wrapper");
100
if (iface_info == NULL)
102
g_error("Unable to find interface 'com.canonical.gdbus_wrapper'");
109
service_gdbus_wrapper_init(ServiceGDBusWrapper* self)
111
self->priv = G_TYPE_INSTANCE_GET_PRIVATE(self, SERVICE_TYPE_GDBUS_WRAPPER, ServiceGDBusWrapperPrivate);
112
self->priv->bus = NULL;
113
self->priv->bus_lookup = NULL;
114
self->priv->bus_registration = 0;
116
self->priv->bus_lookup = g_cancellable_new();
117
g_bus_get(G_BUS_TYPE_SESSION, self->priv->bus_lookup, bus_got_cb, self);
122
service_gdbus_wrapper_new()
124
return g_object_new(SERVICE_TYPE_GDBUS_WRAPPER, NULL);
128
bus_got_cb (GObject *object, GAsyncResult * res, gpointer user_data)
130
GError * error = NULL;
131
ServiceGDBusWrapper * self = SERVICE_GDBUS_WRAPPER(user_data);
132
GDBusConnection * bus;
134
bus = g_bus_get_finish(res, &error);
136
g_critical("Unable to get bus: %s", error->message);
141
self->priv->bus = bus;
143
/* Register object */
144
self->priv->bus_registration = g_dbus_connection_register_object(bus,
145
/* path */ "/com/canonical/gdbus_wrapper",
146
/* interface */ iface_info,
147
/* vtable */ &bus_vtable,
153
g_critical ("Unable to create bus connection object, %s", error->message);
162
bus_method (GDBusConnection *connection, const gchar *sender, const gchar *object_path, const gchar *interface_name, const gchar *method_name, GVariant *parameters, GDBusMethodInvocation *invocation, gpointer user_data)
164
if (g_strcmp0(method_name, "TestMethod") == 0)
166
GVariant * ret = NULL;
167
GVariant * sig_data = NULL;
168
GVariant * tmp = NULL;
169
gchar * query = NULL;
170
GError * error = NULL;
171
g_variant_get(parameters, "(s)", &query);
173
tmp = g_variant_new_string(query);
174
ret = g_variant_new_tuple(&tmp, 1);
175
g_dbus_method_invocation_return_value(invocation, ret);
178
// emit a signal with the same string as passed in every time this method is called
180
tmp = g_variant_new_string(query);
181
sig_data = g_variant_new_tuple(&tmp, 1);
182
g_dbus_connection_emit_signal(connection,
183
NULL, /* destination bus, we don't care */
184
"/com/canonical/gdbus_wrapper", /* object path */
185
"com.canonical.gdbus_wrapper", /* interface name */
186
"TestSignal", /* Signal name */
187
sig_data, /* parameter */
192
g_critical ("could not emit signal TestSignal with data %s", query);