[gnome-keyring/dbus-api] [daemon] Refactor dbus code into seperate files.



commit 46bef5367df6814c3146c2f3304738a27552ab3e
Author: Stef Walter <stef memberwebs com>
Date:   Sat Aug 15 14:40:10 2009 +0000

    [daemon] Refactor dbus code into seperate files.
    
    Since dbus is used for several things in the daemon, refactor DBus
    code into a separate component, with each task in a separate file.

 configure.in                       |    1 +
 daemon/Makefile.am                 |    3 +-
 daemon/dbus/Makefile.am            |   22 ++
 daemon/dbus/gkd-dbus-environment.c |   97 ++++++++
 daemon/dbus/gkd-dbus-private.h     |   42 ++++
 daemon/dbus/gkd-dbus-service.c     |  161 +++++++++++++
 daemon/dbus/gkd-dbus-session.c     |  226 ++++++++++++++++++
 daemon/dbus/gkd-dbus.c             |   99 ++++++++
 daemon/dbus/gkd-dbus.h             |   29 +++
 daemon/gkr-daemon-dbus.c           |  462 ------------------------------------
 daemon/gkr-daemon.c                |    4 +-
 daemon/gkr-daemon.h                |    2 -
 12 files changed, 682 insertions(+), 466 deletions(-)
---
diff --git a/configure.in b/configure.in
index 3317e6f..ab782ef 100644
--- a/configure.in
+++ b/configure.in
@@ -527,6 +527,7 @@ Makefile
 daemon/Makefile
 daemon/gnome-keyring-daemon.desktop.in
 daemon/data/Makefile
+daemon/dbus/Makefile
 daemon/keyrings/Makefile
 daemon/keyrings/tests/Makefile
 daemon/pkcs11/Makefile
diff --git a/daemon/Makefile.am b/daemon/Makefile.am
index 79df344..4de2940 100644
--- a/daemon/Makefile.am
+++ b/daemon/Makefile.am
@@ -3,6 +3,7 @@ SUBDIRS = \
 	ui \
 	keyrings \
 	pkcs11 \
+	dbus \
 	data
 
 bin_PROGRAMS= \
@@ -24,12 +25,12 @@ INCLUDES=	\
 
 gnome_keyring_daemon_SOURCES = \
 	gkr-daemon.c gkr-daemon.h \
-	gkr-daemon-dbus.c \
 	gkr-daemon-io.c \
 	gkr-daemon-ops.c
 
 gnome_keyring_daemon_LDADD = \
 	$(top_builddir)/daemon/pkcs11/libgkr-pkcs11.la \
+	$(top_builddir)/daemon/dbus/libgkr-dbus.la \
 	$(top_builddir)/daemon/keyrings/libgkr-keyrings.la \
 	$(top_builddir)/daemon/ui/libgkr-ui.la \
 	$(top_builddir)/daemon/util/libgkr-daemon-util.la \
diff --git a/daemon/dbus/Makefile.am b/daemon/dbus/Makefile.am
new file mode 100644
index 0000000..9bae59a
--- /dev/null
+++ b/daemon/dbus/Makefile.am
@@ -0,0 +1,22 @@
+
+INCLUDES = \
+	-I$(top_srcdir) 				\
+	-I$(top_srcdir)/daemon 				\
+	-I$(top_builddir) 				\
+	$(DAEMON_CFLAGS)				\
+	$(GOBJECT_CFLAGS) 				\
+	$(GLIB_CFLAGS)
+
+noinst_LTLIBRARIES = libgkr-dbus.la
+
+libgkr_dbus_la_SOURCES = \
+	gkd-dbus.c gkd-dbus.h \
+	gkd-dbus-environment.c \
+	gkd-dbus-private.h \
+	gkd-dbus-service.c \
+	gkd-dbus-session.c
+
+libgkr_dbus_la_LIBADD = \
+	$(GLIB_LIBS) \
+	$(GOBJECT_LIBS)
+
diff --git a/daemon/dbus/gkd-dbus-environment.c b/daemon/dbus/gkd-dbus-environment.c
new file mode 100644
index 0000000..99bbe7f
--- /dev/null
+++ b/daemon/dbus/gkd-dbus-environment.c
@@ -0,0 +1,97 @@
+/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
+/* gkd-dbus-session.c - daemon registering environment variables with session
+
+   Copyright (C) 2007, 2009, Stefan Walter
+
+   The Gnome Keyring Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   The Gnome Keyring Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with the Gnome Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.
+
+   Author: Stef Walter <stef memberwebs com>
+*/
+
+#include "config.h"
+
+#include "gkd-dbus-private.h"
+
+#include "util/gkr-daemon-util.h"
+
+#include <dbus/dbus.h>
+
+#include <string.h>
+
+#define SERVICE_SESSION_MANAGER	"org.gnome.SessionManager"
+#define PATH_SESSION_MANAGER	"/org/gnome/SessionManager"
+#define IFACE_SESSION_MANAGER   "org.gnome.SessionManager"
+
+void
+gkd_dbus_environment_cleanup (DBusConnection *conn)
+{
+	/* Nothing to do here */
+}
+
+void
+gkd_dbus_environment_init (DBusConnection *conn)
+{
+	DBusMessageIter args;
+	DBusMessage *msg;
+	DBusMessage *reply;
+	DBusError derr = { 0 };
+	const gchar **envp;
+	const gchar *value;
+	gchar *name;
+
+	/*
+	 * The list of all environment variables registered by
+	 * various components in the daemon.
+	 */
+	envp = gkr_daemon_util_get_environment ();
+
+	for (; *envp; ++envp) {
+
+		/* Find the value part of the environment variable */
+		value = strchr (*envp, '=');
+		if (!value)
+			continue;
+
+		name = g_strndup (*envp, value - *envp);
+		++value;
+
+		msg = dbus_message_new_method_call (SERVICE_SESSION_MANAGER,
+		                                    PATH_SESSION_MANAGER,
+		                                    IFACE_SESSION_MANAGER,
+		                                    "Setenv");
+		g_return_if_fail (msg);
+
+		dbus_message_iter_init_append (msg, &args);
+		if (!dbus_message_iter_append_basic (&args, DBUS_TYPE_STRING, &name) ||
+		    !dbus_message_iter_append_basic (&args, DBUS_TYPE_STRING, &value))
+			g_return_if_reached ();
+
+		g_free (name);
+		value = name = NULL;
+
+		/* Send message and get a handle for a reply */
+		reply = dbus_connection_send_with_reply_and_block (conn, msg, 1000, &derr);
+		dbus_message_unref (msg);
+
+		if (!reply) {
+			g_message ("couldn't set environment variable in session: %s", derr.message);
+			dbus_error_free (&derr);
+			return;
+		}
+
+		dbus_message_unref (reply);
+	}
+}
diff --git a/daemon/dbus/gkd-dbus-private.h b/daemon/dbus/gkd-dbus-private.h
new file mode 100644
index 0000000..32fc5a4
--- /dev/null
+++ b/daemon/dbus/gkd-dbus-private.h
@@ -0,0 +1,42 @@
+/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
+/* gkd-dbus-private.h - header bits for dbus components
+
+   Copyright (C) 2009, Stefan Walter
+
+   The Gnome Keyring Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   The Gnome Keyring Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with the Gnome Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.
+
+   Author: Stef Walter <stef memberwebs com>
+*/
+
+#ifndef GKD_DBUS_PRIVATE_H
+#define GKD_DBUS_PRIVATE_H
+
+#include <glib.h>
+#include <dbus/dbus.h>
+
+/* DBus environment variables sent to session */
+void   gkd_dbus_environment_init        (DBusConnection *conn);
+void   gkd_dbus_environment_cleanup     (DBusConnection *conn);
+
+/* The gnome-keyring Dbus service, very simple */
+void   gkd_dbus_service_init            (DBusConnection *conn);
+void   gkd_dbus_service_cleanup         (DBusConnection *conn);
+
+/* DBus desktop session interaction */
+void   gkd_dbus_session_init            (DBusConnection *conn);
+void   gkd_dbus_session_cleanup         (DBusConnection *conn);
+
+#endif /* GKD_DBUS_PRIVATE_H */
diff --git a/daemon/dbus/gkd-dbus-service.c b/daemon/dbus/gkd-dbus-service.c
new file mode 100644
index 0000000..563a299
--- /dev/null
+++ b/daemon/dbus/gkd-dbus-service.c
@@ -0,0 +1,161 @@
+/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
+/* gkd-dbus-service.c - gnome-keyring dbus service
+
+   Copyright (C) 2007, 2009, Stefan Walter
+
+   The Gnome Keyring Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   The Gnome Keyring Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with the Gnome Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.
+
+   Author: Stef Walter <stef memberwebs com>
+*/
+
+#include "config.h"
+
+#include "gkd-dbus-private.h"
+
+#include "gkr-daemon.h"
+#include "util/gkr-daemon-util.h"
+
+#include <dbus/dbus.h>
+
+#include <string.h>
+
+static gboolean object_registered = FALSE;
+
+#define GNOME_KEYRING_DAEMON_SERVICE    "org.gnome.keyring"
+#define GNOME_KEYRING_DAEMON_PATH       "/org/gnome/keyring/daemon"
+#define GNOME_KEYRING_DAEMON_INTERFACE  "org.gnome.keyring.Daemon"
+
+static DBusHandlerResult
+message_handler_cb (DBusConnection *conn, DBusMessage *message, void *user_data)
+{
+	/*
+	 * Here we handle the requests to our own gnome-keyring DBus interfaces
+	 */
+
+	DBusMessageIter args;
+	DBusMessage *reply = NULL;
+
+	/* GetSocketPath */
+	if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_CALL &&
+	    dbus_message_is_method_call (message, GNOME_KEYRING_DAEMON_INTERFACE, "GetSocketPath") &&
+	    g_str_equal (dbus_message_get_signature (message), "")) {
+
+		const gchar *socket_path = gkr_daemon_io_get_socket_path ();
+		g_return_val_if_fail (socket_path, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
+
+		/* Setup the result */
+		reply = dbus_message_new_method_return (message);
+		dbus_message_iter_init_append (reply, &args);
+		if (!dbus_message_iter_append_basic (&args, DBUS_TYPE_STRING, &socket_path))
+			g_return_val_if_reached (DBUS_HANDLER_RESULT_NEED_MEMORY);
+
+	/* GetEnvironment */
+	} else if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_CALL &&
+	           dbus_message_is_method_call (message, GNOME_KEYRING_DAEMON_INTERFACE, "GetEnvironment") &&
+	           g_str_equal (dbus_message_get_signature (message), "")) {
+
+		const gchar **env;
+		DBusMessageIter items, entry;
+		gchar **parts;
+
+		env = gkr_daemon_util_get_environment ();
+		g_return_val_if_fail (env, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
+
+		/* Setup the result */
+		reply = dbus_message_new_method_return (message);
+		dbus_message_iter_init_append (reply, &args);
+		if (!dbus_message_iter_open_container (&args, DBUS_TYPE_ARRAY, "{ss}", &items))
+			g_return_val_if_reached (DBUS_HANDLER_RESULT_NEED_MEMORY);
+		while (*env) {
+			parts = g_strsplit (*env, "=", 2);
+			g_return_val_if_fail (parts && parts[0] && parts[1], DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
+			if (!dbus_message_iter_open_container (&items, DBUS_TYPE_DICT_ENTRY, NULL, &entry) ||
+			    !dbus_message_iter_append_basic (&entry, DBUS_TYPE_STRING, &parts[0]) ||
+			    !dbus_message_iter_append_basic (&entry, DBUS_TYPE_STRING, &parts[1]) ||
+			    !dbus_message_iter_close_container (&items, &entry))
+				g_return_val_if_reached (DBUS_HANDLER_RESULT_NEED_MEMORY);
+			++env;
+		}
+		if (!dbus_message_iter_close_container (&args, &items))
+			g_return_val_if_reached (DBUS_HANDLER_RESULT_NEED_MEMORY);
+
+	/* Unknown call */
+	} else {
+		return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+	}
+
+	/* Send the reply */
+	if (!dbus_connection_send (conn, reply, NULL))
+		g_return_val_if_reached (DBUS_HANDLER_RESULT_NEED_MEMORY);
+	dbus_connection_flush (conn);
+
+	return DBUS_HANDLER_RESULT_HANDLED;
+}
+
+static DBusObjectPathVTable object_vtable  = {
+	NULL,
+	message_handler_cb,
+	NULL,
+};
+
+void
+gkd_dbus_service_init (DBusConnection *conn)
+{
+	dbus_uint32_t res = 0;
+	DBusError derr = { 0 };
+
+	dbus_error_init (&derr);
+
+	/* Try and grab our name */
+	res = dbus_bus_request_name (conn, GNOME_KEYRING_DAEMON_SERVICE, 0, &derr);
+	if (dbus_error_is_set (&derr)) {
+		g_message ("couldn't request name on session bus: %s", derr.message);
+		dbus_error_free (&derr);
+	}
+
+	switch (res) {
+	/* We acquired the service name */
+	case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
+		break;
+	/* We already acquired the service name. Odd */
+	case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
+		g_return_if_reached ();
+		break;
+	/* Another daemon is running */
+	case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
+	case DBUS_REQUEST_NAME_REPLY_EXISTS:
+		g_message ("another gnome-keyring-daemon is running");
+		break;
+	default:
+		g_return_if_reached ();
+		break;
+	};
+
+	/* Now register the object */
+	if (dbus_connection_register_object_path (conn, GNOME_KEYRING_DAEMON_PATH, 
+	                                          &object_vtable, NULL))
+		object_registered = TRUE;
+	else
+		g_message ("couldn't register dbus object path");
+}
+
+void
+gkd_dbus_service_cleanup (DBusConnection *conn)
+{
+	if (object_registered)
+		dbus_connection_unregister_object_path (conn, GNOME_KEYRING_DAEMON_PATH);
+	object_registered = FALSE;
+}
diff --git a/daemon/dbus/gkd-dbus-session.c b/daemon/dbus/gkd-dbus-session.c
new file mode 100644
index 0000000..2370999
--- /dev/null
+++ b/daemon/dbus/gkd-dbus-session.c
@@ -0,0 +1,226 @@
+/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
+/* gkd-dbus-session.c - daemon registering with the session
+
+   Copyright (C) 2007, 2009, Stefan Walter
+
+   The Gnome Keyring Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   The Gnome Keyring Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with the Gnome Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.
+
+   Author: Stef Walter <stef memberwebs com>
+*/
+
+#include "config.h"
+
+#include "gkd-dbus-private.h"
+
+#include "gkr-daemon.h"
+
+#include <dbus/dbus.h>
+
+#include <string.h>
+
+#define SERVICE_SESSION_MANAGER	"org.gnome.SessionManager"
+#define PATH_SESSION_MANAGER	"/org/gnome/SessionManager"
+#define IFACE_SESSION_MANAGER   "org.gnome.SessionManager"
+#define IFACE_SESSION_CLIENT    "org.gnome.SessionManager.Client"
+#define IFACE_SESSION_PRIVATE   "org.gnome.SessionManager.ClientPrivate"
+
+static gchar *client_session_path = NULL;
+static gchar *client_session_rule = NULL;
+
+static void
+send_end_session_response (DBusConnection *conn)
+{
+	DBusMessageIter args;
+	DBusMessage *msg;
+	DBusMessage *reply;
+	DBusError derr = { 0 };
+	const gchar *reason = "";
+	dbus_bool_t is_ok = TRUE;
+
+	g_return_if_fail (client_session_path);
+
+	msg = dbus_message_new_method_call (SERVICE_SESSION_MANAGER,
+	                                    client_session_path,
+	                                    IFACE_SESSION_PRIVATE,
+	                                    "EndSessionResponse");
+	g_return_if_fail (msg);
+
+	dbus_message_iter_init_append (msg, &args);
+	if (!dbus_message_iter_append_basic (&args, DBUS_TYPE_BOOLEAN, &is_ok) ||
+	    !dbus_message_iter_append_basic (&args, DBUS_TYPE_STRING, &reason))
+		g_return_if_reached ();
+
+	reply = dbus_connection_send_with_reply_and_block (conn, msg, 1000, &derr);
+	dbus_message_unref (msg);
+
+	if (!reply) {
+		g_message ("dbus failure responding to ending session: %s", derr.message);
+		return;
+	}
+
+	dbus_message_unref (reply);
+}
+
+static void
+unregister_daemon_in_session (DBusConnection *conn)
+{
+	DBusMessageIter args;
+	DBusMessage *msg;
+	DBusMessage *reply;
+	DBusError derr = { 0 };
+
+	if (client_session_rule) {
+		dbus_bus_remove_match (conn, client_session_rule, NULL);
+		g_free (client_session_rule);
+		client_session_rule = NULL;
+	}
+
+	if (!client_session_path)
+		return;
+
+	msg = dbus_message_new_method_call (SERVICE_SESSION_MANAGER,
+	                                    PATH_SESSION_MANAGER,
+	                                    IFACE_SESSION_MANAGER,
+	                                    "UnregisterClient");
+	g_return_if_fail (msg);
+
+	dbus_message_iter_init_append (msg, &args);
+	if (!dbus_message_iter_append_basic (&args, DBUS_TYPE_OBJECT_PATH, &client_session_path))
+		g_return_if_reached ();
+
+	reply = dbus_connection_send_with_reply_and_block (conn, msg, 1000, &derr);
+	dbus_message_unref (msg);
+
+	if (!reply) {
+		g_message ("dbus failure unregistering from session: %s", derr.message);
+		return;
+	}
+
+	dbus_message_unref (reply);
+
+	g_free (client_session_path);
+	client_session_path = NULL;
+}
+
+static DBusHandlerResult
+signal_filter (DBusConnection *conn, DBusMessage *msg, void *user_data)
+{
+	/* Quit the daemon when the session is over */
+	if (dbus_message_is_signal (msg, IFACE_SESSION_PRIVATE, "Stop")) {
+		unregister_daemon_in_session (conn);
+		gkr_daemon_quit ();
+		return DBUS_HANDLER_RESULT_HANDLED;
+	} else if (dbus_message_is_signal (msg, IFACE_SESSION_PRIVATE, "QueryEndSession")) {
+		send_end_session_response (conn);
+		return DBUS_HANDLER_RESULT_HANDLED;
+	} else if (dbus_message_is_signal (msg, IFACE_SESSION_PRIVATE, "EndSession")) {
+		send_end_session_response (conn);
+		unregister_daemon_in_session (conn);
+		gkr_daemon_quit ();
+		return DBUS_HANDLER_RESULT_HANDLED;
+	}
+
+	return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+void
+gkd_dbus_session_cleanup (DBusConnection *conn)
+{
+	unregister_daemon_in_session (conn);
+
+	g_free (client_session_path);
+	client_session_path = NULL;
+
+	g_free (client_session_rule);
+	client_session_rule = NULL;
+}
+
+/*
+ * Here we register our desktop autostart id gnome-session style
+ * session manager via DBus.
+ */
+void
+gkd_dbus_session_init (DBusConnection *conn)
+{
+	DBusMessageIter args;
+	DBusMessage *msg;
+	DBusMessage *reply;
+	DBusError derr = { 0 };
+	const gchar *app_id = "gnome-keyring-daemon";
+	const gchar *client_id;
+
+	client_id = g_getenv ("DESKTOP_AUTOSTART_ID");
+	if (!client_id)
+		return;
+
+	msg = dbus_message_new_method_call (SERVICE_SESSION_MANAGER,
+	                                    PATH_SESSION_MANAGER,
+	                                    IFACE_SESSION_MANAGER,
+	                                    "RegisterClient");
+	g_return_if_fail (msg);
+
+	dbus_message_iter_init_append (msg, &args);
+	if (!dbus_message_iter_append_basic (&args, DBUS_TYPE_STRING, &app_id) ||
+	    !dbus_message_iter_append_basic (&args, DBUS_TYPE_STRING, &client_id))
+		g_return_if_reached ();
+
+	/* Send message and get a handle for a reply */
+	reply = dbus_connection_send_with_reply_and_block (conn, msg, 1000, &derr);
+	dbus_message_unref (msg);
+
+	if (!reply) {
+		g_message ("couldn't register in session: %s", derr.message);
+		dbus_error_free (&derr);
+		return;
+	}
+
+	/* Get out our client path */
+	if (!dbus_message_iter_init (reply, &args) ||
+	    dbus_message_iter_get_arg_type (&args) != DBUS_TYPE_OBJECT_PATH) {
+		g_message ("invalid register response from session");
+	} else {
+		dbus_message_iter_get_basic (&args, &client_session_path);
+		client_session_path = g_strdup (client_session_path);
+	}
+
+	dbus_message_unref (reply);
+
+	/*
+	 * Unset DESKTOP_AUTOSTART_ID in order to avoid child processes to
+	 * use the same client id.
+	 */
+	g_unsetenv ("DESKTOP_AUTOSTART_ID");
+
+	/*
+	 * Now we register for DBus signals on that client session path
+	 * These are fired specifically for us.
+	 */
+	client_session_rule = g_strdup_printf("type='signal',"
+	                                      "interface='org.gnome.SessionManager.ClientPrivate',"
+	                                      "path='%s'",
+	                                      client_session_path);
+	dbus_bus_add_match (conn, client_session_rule, &derr);
+
+	if(dbus_error_is_set(&derr)) {
+		g_message ("couldn't listen for signals in session: %s", derr.message);
+		dbus_error_free (&derr);
+		g_free (client_session_rule);
+		client_session_rule = NULL;
+		return;
+	}
+
+	dbus_connection_add_filter (conn, signal_filter, NULL, NULL);
+}
diff --git a/daemon/dbus/gkd-dbus.c b/daemon/dbus/gkd-dbus.c
new file mode 100644
index 0000000..f902a5d
--- /dev/null
+++ b/daemon/dbus/gkd-dbus.c
@@ -0,0 +1,99 @@
+/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
+/* gkd-dbus.c - hook into dbus, call other bits
+
+   Copyright (C) 2007, 2009, Stefan Walter
+
+   The Gnome Keyring Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   The Gnome Keyring Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with the Gnome Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.
+
+   Author: Stef Walter <stef memberwebs com>
+*/
+
+#include "config.h"
+
+#include "gkd-dbus.h"
+#include "gkd-dbus-private.h"
+
+#include "egg/egg-cleanup.h"
+#include "egg/egg-dbus.h"
+
+#include <glib.h>
+
+#include <dbus/dbus.h>
+
+static DBusConnection *dbus_conn = NULL;
+static gboolean dbus_do_session = TRUE;
+
+static void
+daemon_dbus_cleanup (gpointer unused)
+{
+	if (!dbus_conn)
+		return;
+
+	if (dbus_do_session) {
+		gkd_dbus_session_cleanup (dbus_conn);
+		gkd_dbus_environment_cleanup (dbus_conn);
+	}
+
+	gkd_dbus_service_cleanup (dbus_conn);
+
+	egg_dbus_disconnect_from_mainloop (dbus_conn, NULL);
+	dbus_connection_unref (dbus_conn);
+	dbus_conn = NULL;
+}
+
+void
+gkd_dbus_setup (void)
+{
+	DBusError derr = { 0 };
+
+	if (dbus_conn)
+		return;
+
+#ifdef WITH_TESTS
+	{
+		/* If running as a test, don't do certain DBUS stuff */
+		const gchar *env = g_getenv ("GNOME_KEYRING_TEST_PATH");
+		if (env && env[0])
+			dbus_do_session = FALSE;
+	}
+#endif
+
+	dbus_error_init (&derr);
+
+	/* Get the dbus bus and hook up */
+	dbus_conn = dbus_bus_get (DBUS_BUS_SESSION, &derr);
+	if (!dbus_conn) {
+		g_message ("couldn't connect to dbus session bus: %s", derr.message);
+		dbus_error_free (&derr);
+		return;
+	}
+
+	egg_cleanup_register (daemon_dbus_cleanup, NULL);
+
+	egg_dbus_connect_with_mainloop (dbus_conn, NULL);
+
+	/* Make sure dbus doesn't kill our app */
+	dbus_connection_set_exit_on_disconnect (dbus_conn, FALSE);
+
+	/* Gnome Keyring service */
+	gkd_dbus_service_init (dbus_conn);
+
+	/* Session stuff */
+	if (dbus_do_session) {
+		gkd_dbus_environment_init (dbus_conn);
+		gkd_dbus_session_init (dbus_conn);
+	}
+}
diff --git a/daemon/dbus/gkd-dbus.h b/daemon/dbus/gkd-dbus.h
new file mode 100644
index 0000000..77283e2
--- /dev/null
+++ b/daemon/dbus/gkd-dbus.h
@@ -0,0 +1,29 @@
+/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
+/* gkd-dbus.h - header for dbus component
+
+   Copyright (C) 2009, Stefan Walter
+
+   The Gnome Keyring Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   The Gnome Keyring Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with the Gnome Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.
+
+   Author: Stef Walter <stef memberwebs com>
+*/
+
+#ifndef GKD_DBUS_H
+#define GKD_DBUS_H
+
+void   gkd_dbus_setup (void);
+
+#endif /* GKD_DBUS_H */
diff --git a/daemon/gkr-daemon.c b/daemon/gkr-daemon.c
index 39a8c61..25fa6e0 100644
--- a/daemon/gkr-daemon.c
+++ b/daemon/gkr-daemon.c
@@ -24,6 +24,8 @@
 
 #include "gkr-daemon.h"
 
+#include "dbus/gkd-dbus.h"
+
 #include "egg/egg-cleanup.h"
 #include "egg/egg-libgcrypt.h"
 #include "egg/egg-secure-memory.h"
@@ -634,7 +636,7 @@ gkr_daemon_complete_initialization_steps (void)
 			return FALSE;
 	}
 	
-	gkr_daemon_dbus_setup ();
+	gkd_dbus_setup ();
 	
 	initialization_completed = TRUE;
 	return TRUE;
diff --git a/daemon/gkr-daemon.h b/daemon/gkr-daemon.h
index f37fd7e..76dae72 100644
--- a/daemon/gkr-daemon.h
+++ b/daemon/gkr-daemon.h
@@ -53,6 +53,4 @@ gboolean       gkr_daemon_io_create_master_socket (void);
 
 const gchar*   gkr_daemon_io_get_socket_path      (void);
 
-void           gkr_daemon_dbus_setup              (void);
-
 #endif /* GNOME_KEYRING_DAEMON_H */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]