changeset 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 0907cc7064d4
children 6e2d746b06ae
files Makefile xwrited-unique.c
diffstat 2 files changed, 64 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Sun Apr 27 23:12:06 2014 +0200
+++ b/Makefile	Sun Apr 27 23:12:06 2014 +0200
@@ -53,7 +53,7 @@
 
 OS_NAME :=	$(shell uname -s)
 
-PKGCONFIG_LIBS :=	dbus-1 glib-2.0 dbus-glib-1 libnotify
+PKGCONFIG_LIBS :=	dbus-1 glib-2.0 libnotify
 EXTRA_LIBS :=
 
 ifeq ($(OS_NAME),Linux)
@@ -66,6 +66,12 @@
     OBJS_UTMP :=	xwrited-utmp-utmpx.o
 endif
 
+ifeq ($(shell pkg-config --exists 'glib-2.0 >= 2.25.5' && printf "true"), true)
+    CPPFLAGS_EXTRA :=	-DHAVE_GLIB_GDBUS
+else
+    PKGCONFIG_LIBS +=	dbus-glib-1
+endif
+
 OBJS =		main.o xwrited-debug.o xwrited-unique.o $(OBJS_UTMP)
 AUTOSTART_FILE = $(PACKAGE).desktop
 MOFILES :=	$(patsubst %.po,%.mo,$(wildcard po/*.po))
--- a/xwrited-unique.c	Sun Apr 27 23:12:06 2014 +0200
+++ b/xwrited-unique.c	Sun Apr 27 23:12:06 2014 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2011 Guido Berhoerster <guido+xwrited@berhoerster.name>
+ * Copyright (C) 2014 Guido Berhoerster <guido+xwrited@berhoerster.name>
  *
  * Permission is hereby granted, free of charge, to any person obtaining
  * a copy of this software and associated documentation files (the
@@ -24,7 +24,11 @@
 #define	_XOPEN_SOURCE	600
 
 #include <glib.h>
+#ifdef	HAVE_GLIB_GDBUS
+#include <gio/gio.h>
+#else
 #include <dbus/dbus-glib.h>
+#endif /* HAVE_GLIB_GDBUS */
 #include <dbus/dbus.h>
 
 #include "xwrited-unique.h"
@@ -35,8 +39,12 @@
     XWRITED_TYPE_UNIQUE, XWritedUniquePrivate))
 
 struct	_XWritedUniquePrivate {
+#ifdef HAVE_GLIB_GDBUS
+	GDBusProxy	*session_bus_proxy;
+#else
 	DBusGConnection	*session_bus;
 	DBusGProxy	*session_bus_proxy;
+#endif /* HAVE_GLIB_GDBUS */
 	gchar		*name;
 	gboolean	is_unique;
 };
@@ -52,10 +60,28 @@
 {
 	guint32	request_name_response;
 	GError	*error = NULL;
+#ifdef HAVE_GLIB_GDBUS
+	GVariant *result;
+
+	g_return_val_if_fail(self->priv->session_bus_proxy != NULL, FALSE);
+
+	result = g_dbus_proxy_call_sync(self->priv->session_bus_proxy,
+	    "RequestName", g_variant_new("(su)", self->priv->name,
+	    DBUS_NAME_FLAG_DO_NOT_QUEUE), G_DBUS_CALL_FLAGS_NONE, -1, NULL,
+	    &error);
+	if (result == NULL) {
+		g_warning("failed to acquire service name \"%s\": %s",
+		    self->priv->name, error->message);
+		g_error_free(error);
+		return (FALSE);
+	}
+
+	g_variant_get(result, "(u)", &request_name_response);
+	g_variant_unref(result);
+#else
 
 	g_return_val_if_fail(self->priv->session_bus != NULL, FALSE);
 	g_return_val_if_fail(self->priv->session_bus_proxy != NULL, FALSE);
-
 	if (dbus_g_proxy_call(self->priv->session_bus_proxy, "RequestName",
 	    &error, G_TYPE_STRING, self->priv->name, G_TYPE_UINT,
 	    DBUS_NAME_FLAG_DO_NOT_QUEUE, G_TYPE_INVALID, G_TYPE_UINT,
@@ -65,6 +91,7 @@
 		g_error_free(error);
 		return (FALSE);
 	}
+#endif /* HAVE_GLIB_GDBUS */
 
 	switch (request_name_response) {
 	case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
@@ -127,6 +154,18 @@
 	gobject = parent_class->constructor(gtype, n_params, params);
 	app = XWRITED_UNIQUE(gobject);
 
+#ifdef HAVE_GLIB_GDBUS
+	app->priv->session_bus_proxy =
+	    g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION,
+		G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
+		G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, NULL, DBUS_SERVICE_DBUS,
+		DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, NULL, &error);
+	if (app->priv->session_bus_proxy == NULL) {
+		g_warning("failed to create DBus proxy: %s", error->message);
+		g_error_free(error);
+		goto out;
+	}
+#else
 	app->priv->session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
 	if (app->priv->session_bus == NULL) {
 		g_warning("failed to connect to DBus session bus: %s",
@@ -142,6 +181,7 @@
 		g_warning("failed to create DBus proxy");
 		goto out;
 	}
+#endif /* HAVE_GLIB_GDBUS */
 
 	if (request_name(app)) {
 		app->priv->is_unique = TRUE;
@@ -161,6 +201,13 @@
 		self->priv->session_bus_proxy = NULL;
 	}
 
+#ifndef HAVE_GLIB_GDBUS
+	if (self->priv->session_bus != NULL) {
+		dbus_g_connection_unref(self->priv->session_bus);
+		self->priv->session_bus = NULL;
+	}
+
+#endif /* !HAVE_GLIB_GDBUS */
 	G_OBJECT_CLASS(xwrited_unique_parent_class)->dispose(gobject);
 }
 
@@ -208,6 +255,9 @@
 	self->priv = XWRITED_UNIQUE_GET_PRIVATE(self);
 
 	self->priv->is_unique = FALSE;
+#ifndef HAVE_GLIB_GDBUS
+	self->priv->session_bus = NULL;
+#endif /* !HAVE_GLIB_GDBUS */
 	self->priv->session_bus_proxy = NULL;
 }
 
@@ -219,7 +269,11 @@
 	g_return_val_if_fail(name != NULL, NULL);
 
 	app = g_object_new(XWRITED_TYPE_UNIQUE, "name", name, NULL);
-	if (app->priv->session_bus_proxy == NULL) {
+	if (
+#ifndef HAVE_GLIB_GDBUS
+	    app->priv->session_bus == NULL ||
+#endif /* !HAVE_GLIB_GDBUS */
+	    app->priv->session_bus_proxy == NULL) {
 		g_object_unref(app);
 		return (NULL);
 	}