projects/xwrited

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 Apr 27 23:12:06 2014 +0200 (2014-04-27)
parents 0907cc7064d4
children 6e2d746b06ae
files Makefile xwrited-unique.c
line diff
     1.1 --- a/Makefile	Sun Apr 27 23:12:06 2014 +0200
     1.2 +++ b/Makefile	Sun Apr 27 23:12:06 2014 +0200
     1.3 @@ -53,7 +53,7 @@
     1.4  
     1.5  OS_NAME :=	$(shell uname -s)
     1.6  
     1.7 -PKGCONFIG_LIBS :=	dbus-1 glib-2.0 dbus-glib-1 libnotify
     1.8 +PKGCONFIG_LIBS :=	dbus-1 glib-2.0 libnotify
     1.9  EXTRA_LIBS :=
    1.10  
    1.11  ifeq ($(OS_NAME),Linux)
    1.12 @@ -66,6 +66,12 @@
    1.13      OBJS_UTMP :=	xwrited-utmp-utmpx.o
    1.14  endif
    1.15  
    1.16 +ifeq ($(shell pkg-config --exists 'glib-2.0 >= 2.25.5' && printf "true"), true)
    1.17 +    CPPFLAGS_EXTRA :=	-DHAVE_GLIB_GDBUS
    1.18 +else
    1.19 +    PKGCONFIG_LIBS +=	dbus-glib-1
    1.20 +endif
    1.21 +
    1.22  OBJS =		main.o xwrited-debug.o xwrited-unique.o $(OBJS_UTMP)
    1.23  AUTOSTART_FILE = $(PACKAGE).desktop
    1.24  MOFILES :=	$(patsubst %.po,%.mo,$(wildcard po/*.po))
     2.1 --- a/xwrited-unique.c	Sun Apr 27 23:12:06 2014 +0200
     2.2 +++ b/xwrited-unique.c	Sun Apr 27 23:12:06 2014 +0200
     2.3 @@ -1,5 +1,5 @@
     2.4  /*
     2.5 - * Copyright (C) 2011 Guido Berhoerster <guido+xwrited@berhoerster.name>
     2.6 + * Copyright (C) 2014 Guido Berhoerster <guido+xwrited@berhoerster.name>
     2.7   *
     2.8   * Permission is hereby granted, free of charge, to any person obtaining
     2.9   * a copy of this software and associated documentation files (the
    2.10 @@ -24,7 +24,11 @@
    2.11  #define	_XOPEN_SOURCE	600
    2.12  
    2.13  #include <glib.h>
    2.14 +#ifdef	HAVE_GLIB_GDBUS
    2.15 +#include <gio/gio.h>
    2.16 +#else
    2.17  #include <dbus/dbus-glib.h>
    2.18 +#endif /* HAVE_GLIB_GDBUS */
    2.19  #include <dbus/dbus.h>
    2.20  
    2.21  #include "xwrited-unique.h"
    2.22 @@ -35,8 +39,12 @@
    2.23      XWRITED_TYPE_UNIQUE, XWritedUniquePrivate))
    2.24  
    2.25  struct	_XWritedUniquePrivate {
    2.26 +#ifdef HAVE_GLIB_GDBUS
    2.27 +	GDBusProxy	*session_bus_proxy;
    2.28 +#else
    2.29  	DBusGConnection	*session_bus;
    2.30  	DBusGProxy	*session_bus_proxy;
    2.31 +#endif /* HAVE_GLIB_GDBUS */
    2.32  	gchar		*name;
    2.33  	gboolean	is_unique;
    2.34  };
    2.35 @@ -52,10 +60,28 @@
    2.36  {
    2.37  	guint32	request_name_response;
    2.38  	GError	*error = NULL;
    2.39 +#ifdef HAVE_GLIB_GDBUS
    2.40 +	GVariant *result;
    2.41 +
    2.42 +	g_return_val_if_fail(self->priv->session_bus_proxy != NULL, FALSE);
    2.43 +
    2.44 +	result = g_dbus_proxy_call_sync(self->priv->session_bus_proxy,
    2.45 +	    "RequestName", g_variant_new("(su)", self->priv->name,
    2.46 +	    DBUS_NAME_FLAG_DO_NOT_QUEUE), G_DBUS_CALL_FLAGS_NONE, -1, NULL,
    2.47 +	    &error);
    2.48 +	if (result == NULL) {
    2.49 +		g_warning("failed to acquire service name \"%s\": %s",
    2.50 +		    self->priv->name, error->message);
    2.51 +		g_error_free(error);
    2.52 +		return (FALSE);
    2.53 +	}
    2.54 +
    2.55 +	g_variant_get(result, "(u)", &request_name_response);
    2.56 +	g_variant_unref(result);
    2.57 +#else
    2.58  
    2.59  	g_return_val_if_fail(self->priv->session_bus != NULL, FALSE);
    2.60  	g_return_val_if_fail(self->priv->session_bus_proxy != NULL, FALSE);
    2.61 -
    2.62  	if (dbus_g_proxy_call(self->priv->session_bus_proxy, "RequestName",
    2.63  	    &error, G_TYPE_STRING, self->priv->name, G_TYPE_UINT,
    2.64  	    DBUS_NAME_FLAG_DO_NOT_QUEUE, G_TYPE_INVALID, G_TYPE_UINT,
    2.65 @@ -65,6 +91,7 @@
    2.66  		g_error_free(error);
    2.67  		return (FALSE);
    2.68  	}
    2.69 +#endif /* HAVE_GLIB_GDBUS */
    2.70  
    2.71  	switch (request_name_response) {
    2.72  	case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
    2.73 @@ -127,6 +154,18 @@
    2.74  	gobject = parent_class->constructor(gtype, n_params, params);
    2.75  	app = XWRITED_UNIQUE(gobject);
    2.76  
    2.77 +#ifdef HAVE_GLIB_GDBUS
    2.78 +	app->priv->session_bus_proxy =
    2.79 +	    g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION,
    2.80 +		G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
    2.81 +		G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, NULL, DBUS_SERVICE_DBUS,
    2.82 +		DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, NULL, &error);
    2.83 +	if (app->priv->session_bus_proxy == NULL) {
    2.84 +		g_warning("failed to create DBus proxy: %s", error->message);
    2.85 +		g_error_free(error);
    2.86 +		goto out;
    2.87 +	}
    2.88 +#else
    2.89  	app->priv->session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
    2.90  	if (app->priv->session_bus == NULL) {
    2.91  		g_warning("failed to connect to DBus session bus: %s",
    2.92 @@ -142,6 +181,7 @@
    2.93  		g_warning("failed to create DBus proxy");
    2.94  		goto out;
    2.95  	}
    2.96 +#endif /* HAVE_GLIB_GDBUS */
    2.97  
    2.98  	if (request_name(app)) {
    2.99  		app->priv->is_unique = TRUE;
   2.100 @@ -161,6 +201,13 @@
   2.101  		self->priv->session_bus_proxy = NULL;
   2.102  	}
   2.103  
   2.104 +#ifndef HAVE_GLIB_GDBUS
   2.105 +	if (self->priv->session_bus != NULL) {
   2.106 +		dbus_g_connection_unref(self->priv->session_bus);
   2.107 +		self->priv->session_bus = NULL;
   2.108 +	}
   2.109 +
   2.110 +#endif /* !HAVE_GLIB_GDBUS */
   2.111  	G_OBJECT_CLASS(xwrited_unique_parent_class)->dispose(gobject);
   2.112  }
   2.113  
   2.114 @@ -208,6 +255,9 @@
   2.115  	self->priv = XWRITED_UNIQUE_GET_PRIVATE(self);
   2.116  
   2.117  	self->priv->is_unique = FALSE;
   2.118 +#ifndef HAVE_GLIB_GDBUS
   2.119 +	self->priv->session_bus = NULL;
   2.120 +#endif /* !HAVE_GLIB_GDBUS */
   2.121  	self->priv->session_bus_proxy = NULL;
   2.122  }
   2.123  
   2.124 @@ -219,7 +269,11 @@
   2.125  	g_return_val_if_fail(name != NULL, NULL);
   2.126  
   2.127  	app = g_object_new(XWRITED_TYPE_UNIQUE, "name", name, NULL);
   2.128 -	if (app->priv->session_bus_proxy == NULL) {
   2.129 +	if (
   2.130 +#ifndef HAVE_GLIB_GDBUS
   2.131 +	    app->priv->session_bus == NULL ||
   2.132 +#endif /* !HAVE_GLIB_GDBUS */
   2.133 +	    app->priv->session_bus_proxy == NULL) {
   2.134  		g_object_unref(app);
   2.135  		return (NULL);
   2.136  	}