comparison xwrited-unique.c @ 2:c53bcdabbba7

Make use of GDBus for glib >= 2.25.5 From glib 2.25.5 on GDBus is available and the D-Bus GLib bindings are no longer needed.
author Guido Berhoerster <guido+xwrited@berhoerster.name>
date Sun, 27 Apr 2014 23:12:06 +0200
parents 52694b49dcc4
children 4a5330979433
comparison
equal deleted inserted replaced
1:0907cc7064d4 2:c53bcdabbba7
1 /* 1 /*
2 * Copyright (C) 2011 Guido Berhoerster <guido+xwrited@berhoerster.name> 2 * Copyright (C) 2014 Guido Berhoerster <guido+xwrited@berhoerster.name>
3 * 3 *
4 * Permission is hereby granted, free of charge, to any person obtaining 4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the 5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including 6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish, 7 * without limitation the rights to use, copy, modify, merge, publish,
22 */ 22 */
23 23
24 #define _XOPEN_SOURCE 600 24 #define _XOPEN_SOURCE 600
25 25
26 #include <glib.h> 26 #include <glib.h>
27 #ifdef HAVE_GLIB_GDBUS
28 #include <gio/gio.h>
29 #else
27 #include <dbus/dbus-glib.h> 30 #include <dbus/dbus-glib.h>
31 #endif /* HAVE_GLIB_GDBUS */
28 #include <dbus/dbus.h> 32 #include <dbus/dbus.h>
29 33
30 #include "xwrited-unique.h" 34 #include "xwrited-unique.h"
31 35
32 G_DEFINE_TYPE(XWritedUnique, xwrited_unique, G_TYPE_OBJECT) 36 G_DEFINE_TYPE(XWritedUnique, xwrited_unique, G_TYPE_OBJECT)
33 37
34 #define XWRITED_UNIQUE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), \ 38 #define XWRITED_UNIQUE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), \
35 XWRITED_TYPE_UNIQUE, XWritedUniquePrivate)) 39 XWRITED_TYPE_UNIQUE, XWritedUniquePrivate))
36 40
37 struct _XWritedUniquePrivate { 41 struct _XWritedUniquePrivate {
42 #ifdef HAVE_GLIB_GDBUS
43 GDBusProxy *session_bus_proxy;
44 #else
38 DBusGConnection *session_bus; 45 DBusGConnection *session_bus;
39 DBusGProxy *session_bus_proxy; 46 DBusGProxy *session_bus_proxy;
47 #endif /* HAVE_GLIB_GDBUS */
40 gchar *name; 48 gchar *name;
41 gboolean is_unique; 49 gboolean is_unique;
42 }; 50 };
43 51
44 enum { 52 enum {
50 static gboolean 58 static gboolean
51 request_name(XWritedUnique *self) 59 request_name(XWritedUnique *self)
52 { 60 {
53 guint32 request_name_response; 61 guint32 request_name_response;
54 GError *error = NULL; 62 GError *error = NULL;
63 #ifdef HAVE_GLIB_GDBUS
64 GVariant *result;
65
66 g_return_val_if_fail(self->priv->session_bus_proxy != NULL, FALSE);
67
68 result = g_dbus_proxy_call_sync(self->priv->session_bus_proxy,
69 "RequestName", g_variant_new("(su)", self->priv->name,
70 DBUS_NAME_FLAG_DO_NOT_QUEUE), G_DBUS_CALL_FLAGS_NONE, -1, NULL,
71 &error);
72 if (result == NULL) {
73 g_warning("failed to acquire service name \"%s\": %s",
74 self->priv->name, error->message);
75 g_error_free(error);
76 return (FALSE);
77 }
78
79 g_variant_get(result, "(u)", &request_name_response);
80 g_variant_unref(result);
81 #else
55 82
56 g_return_val_if_fail(self->priv->session_bus != NULL, FALSE); 83 g_return_val_if_fail(self->priv->session_bus != NULL, FALSE);
57 g_return_val_if_fail(self->priv->session_bus_proxy != NULL, FALSE); 84 g_return_val_if_fail(self->priv->session_bus_proxy != NULL, FALSE);
58
59 if (dbus_g_proxy_call(self->priv->session_bus_proxy, "RequestName", 85 if (dbus_g_proxy_call(self->priv->session_bus_proxy, "RequestName",
60 &error, G_TYPE_STRING, self->priv->name, G_TYPE_UINT, 86 &error, G_TYPE_STRING, self->priv->name, G_TYPE_UINT,
61 DBUS_NAME_FLAG_DO_NOT_QUEUE, G_TYPE_INVALID, G_TYPE_UINT, 87 DBUS_NAME_FLAG_DO_NOT_QUEUE, G_TYPE_INVALID, G_TYPE_UINT,
62 &request_name_response, G_TYPE_INVALID) == 0) { 88 &request_name_response, G_TYPE_INVALID) == 0) {
63 g_warning("failed to acquire service name \"%s\": %s", 89 g_warning("failed to acquire service name \"%s\": %s",
64 self->priv->name, error->message); 90 self->priv->name, error->message);
65 g_error_free(error); 91 g_error_free(error);
66 return (FALSE); 92 return (FALSE);
67 } 93 }
94 #endif /* HAVE_GLIB_GDBUS */
68 95
69 switch (request_name_response) { 96 switch (request_name_response) {
70 case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER: 97 case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
71 return (TRUE); 98 return (TRUE);
72 case DBUS_REQUEST_NAME_REPLY_EXISTS: 99 case DBUS_REQUEST_NAME_REPLY_EXISTS:
125 152
126 parent_class = G_OBJECT_CLASS(xwrited_unique_parent_class); 153 parent_class = G_OBJECT_CLASS(xwrited_unique_parent_class);
127 gobject = parent_class->constructor(gtype, n_params, params); 154 gobject = parent_class->constructor(gtype, n_params, params);
128 app = XWRITED_UNIQUE(gobject); 155 app = XWRITED_UNIQUE(gobject);
129 156
157 #ifdef HAVE_GLIB_GDBUS
158 app->priv->session_bus_proxy =
159 g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION,
160 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
161 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, NULL, DBUS_SERVICE_DBUS,
162 DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, NULL, &error);
163 if (app->priv->session_bus_proxy == NULL) {
164 g_warning("failed to create DBus proxy: %s", error->message);
165 g_error_free(error);
166 goto out;
167 }
168 #else
130 app->priv->session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error); 169 app->priv->session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
131 if (app->priv->session_bus == NULL) { 170 if (app->priv->session_bus == NULL) {
132 g_warning("failed to connect to DBus session bus: %s", 171 g_warning("failed to connect to DBus session bus: %s",
133 error->message); 172 error->message);
134 g_error_free(error); 173 g_error_free(error);
140 DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS); 179 DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);
141 if (app->priv->session_bus_proxy == NULL) { 180 if (app->priv->session_bus_proxy == NULL) {
142 g_warning("failed to create DBus proxy"); 181 g_warning("failed to create DBus proxy");
143 goto out; 182 goto out;
144 } 183 }
184 #endif /* HAVE_GLIB_GDBUS */
145 185
146 if (request_name(app)) { 186 if (request_name(app)) {
147 app->priv->is_unique = TRUE; 187 app->priv->is_unique = TRUE;
148 } 188 }
149 189
159 if (self->priv->session_bus_proxy != NULL) { 199 if (self->priv->session_bus_proxy != NULL) {
160 g_object_unref(self->priv->session_bus_proxy); 200 g_object_unref(self->priv->session_bus_proxy);
161 self->priv->session_bus_proxy = NULL; 201 self->priv->session_bus_proxy = NULL;
162 } 202 }
163 203
204 #ifndef HAVE_GLIB_GDBUS
205 if (self->priv->session_bus != NULL) {
206 dbus_g_connection_unref(self->priv->session_bus);
207 self->priv->session_bus = NULL;
208 }
209
210 #endif /* !HAVE_GLIB_GDBUS */
164 G_OBJECT_CLASS(xwrited_unique_parent_class)->dispose(gobject); 211 G_OBJECT_CLASS(xwrited_unique_parent_class)->dispose(gobject);
165 } 212 }
166 213
167 static void 214 static void
168 xwrited_unique_finalize(GObject *gobject) 215 xwrited_unique_finalize(GObject *gobject)
206 xwrited_unique_init(XWritedUnique *self) 253 xwrited_unique_init(XWritedUnique *self)
207 { 254 {
208 self->priv = XWRITED_UNIQUE_GET_PRIVATE(self); 255 self->priv = XWRITED_UNIQUE_GET_PRIVATE(self);
209 256
210 self->priv->is_unique = FALSE; 257 self->priv->is_unique = FALSE;
258 #ifndef HAVE_GLIB_GDBUS
259 self->priv->session_bus = NULL;
260 #endif /* !HAVE_GLIB_GDBUS */
211 self->priv->session_bus_proxy = NULL; 261 self->priv->session_bus_proxy = NULL;
212 } 262 }
213 263
214 XWritedUnique * 264 XWritedUnique *
215 xwrited_unique_new(const gchar *name) 265 xwrited_unique_new(const gchar *name)
217 XWritedUnique *app; 267 XWritedUnique *app;
218 268
219 g_return_val_if_fail(name != NULL, NULL); 269 g_return_val_if_fail(name != NULL, NULL);
220 270
221 app = g_object_new(XWRITED_TYPE_UNIQUE, "name", name, NULL); 271 app = g_object_new(XWRITED_TYPE_UNIQUE, "name", name, NULL);
222 if (app->priv->session_bus_proxy == NULL) { 272 if (
273 #ifndef HAVE_GLIB_GDBUS
274 app->priv->session_bus == NULL ||
275 #endif /* !HAVE_GLIB_GDBUS */
276 app->priv->session_bus_proxy == NULL) {
223 g_object_unref(app); 277 g_object_unref(app);
224 return (NULL); 278 return (NULL);
225 } 279 }
226 280
227 return (app); 281 return (app);