[evince/wip/app: 3/19] daemon: Use gdbus-codegen for the org.gnome.evince.Daemon interface



commit f6777ebc78658937a03eb2cbd8b8362e7d2ca198
Author: Christian Persch <chpe gnome org>
Date:   Mon Jun 11 22:00:31 2012 +0200

    daemon: Use gdbus-codegen for the org.gnome.evince.Daemon interface

 shell/Makefile.am         |   21 ++++++-
 shell/ev-daemon-gdbus.xml |   19 ++++++
 shell/ev-daemon.c         |  139 +++++++++++++++++++--------------------------
 3 files changed, 97 insertions(+), 82 deletions(-)
---
diff --git a/shell/Makefile.am b/shell/Makefile.am
index 4b5e277..c43a824 100644
--- a/shell/Makefile.am
+++ b/shell/Makefile.am
@@ -138,13 +138,21 @@ BUILT_SOURCES = \
 	ev-resources.c
 
 if ENABLE_DBUS
-BUILT_SOURCES += ev-gdbus-generated.c ev-gdbus-generated.h
+BUILT_SOURCES += \
+	ev-gdbus-generated.c \
+	ev-gdbus-generated.h \
+	ev-daemon-gdbus-generated.c \
+	ev-daemon-gdbus-generated.h
 endif
 
 if ENABLE_DBUS
 evinced_SOURCES = \
 	ev-daemon.c \
 	$(NULL)
+nodist_evinced_SOURCES = \
+	ev-daemon-gdbus-generated.c \
+	ev-daemon-gdbus-generated.h
+	$(NULL)
 
 evinced_CFLAGS=				\
 	-I$(top_srcdir)						\
@@ -164,7 +172,8 @@ EXTRA_DIST = \
 	evince-ui.xml \
 	evince-toolbar.xml \
 	evince.gresource.xml \
-	ev-gdbus.xml
+	ev-gdbus.xml \
+	ev-daemon-gdbus.xml
 
 ev-marshal.h: $(srcdir)/ev-marshal.list
 	$(AM_V_GEN)$(GLIB_GENMARSHAL) --prefix=ev_marshal $(srcdir)/ev-marshal.list --header > ev-marshal.h
@@ -184,6 +193,14 @@ ev-gdbus-generated.c ev-gdbus-generated.h: ev-gdbus.xml Makefile
 			--generate-c-code ev-gdbus-generated \
 			$<
 
+ev-daemon-gdbus-generated.c ev-daemon-gdbus-generated.h: ev-daemon-gdbus.xml Makefile
+	$(AM_V_GEN) $(GDBUS_CODEGEN) \
+			--interface-prefix=org.gnome.evince \
+			--c-namespace=Ev \
+			--c-generate-object-manager \
+			--generate-c-code ev-daemon-gdbus-generated \
+			$<
+
 DISTCLEANFILES = $(BUILT_SOURCES)
 
 -include $(top_srcdir)/git.mk
diff --git a/shell/ev-daemon-gdbus.xml b/shell/ev-daemon-gdbus.xml
new file mode 100644
index 0000000..6b4f9d1
--- /dev/null
+++ b/shell/ev-daemon-gdbus.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Introspection 0.1//EN"
+                      "http://www.freedesktop.org/software/dbus/introspection.dtd";>
+<node>
+  <interface name="org.gnome.evince.Daemon">
+    <method name="RegisterDocument">
+      <arg type="s" name="uri" direction="in"/>
+      <arg type="s" name="owner" direction="out"/>
+    </method>
+    <method name="UnregisterDocument">
+      <arg type="s" name="uri" direction="in"/>
+    </method>
+    <method name="FindDocument">
+      <arg type="s" name="uri" direction="in"/>
+      <arg type="b" name="spawn" direction="in"/>
+      <arg type="s" name="owner" direction="out"/>
+    </method>
+  </interface>
+</node>
diff --git a/shell/ev-daemon.c b/shell/ev-daemon.c
index c0f0238..06792ec 100644
--- a/shell/ev-daemon.c
+++ b/shell/ev-daemon.c
@@ -30,6 +30,8 @@
 #include <fcntl.h>
 #include <unistd.h>
 
+#include "ev-daemon-gdbus-generated.h"
+
 #define EV_DBUS_DAEMON_NAME             "org.gnome.evince.Daemon"
 #define EV_DBUS_DAEMON_INTERFACE_NAME   "org.gnome.evince.Daemon"
 #define EV_DBUS_DAEMON_OBJECT_PATH      "/org/gnome/evince/Daemon"
@@ -209,35 +211,28 @@ document_loaded_cb (GDBusConnection *connection,
 	g_dbus_connection_signal_unsubscribe (connection, doc->loaded_id);
 }
 
-static void
-method_call_cb (GDBusConnection       *connection,
-                const gchar           *sender,
-                const gchar           *object_path,
-                const gchar           *interface_name,
-                const gchar           *method_name,
-                GVariant              *parameters,
-                GDBusMethodInvocation *invocation,
-                gpointer               user_data)
+static gboolean
+handle_register_document_cb (EvDaemon *object,
+                             GDBusMethodInvocation *invocation,
+                             const gchar *uri,
+                             gpointer user_data)
 {
-        if (g_strcmp0 (interface_name, EV_DBUS_DAEMON_INTERFACE_NAME) != 0)
-                return;
-
-        if (g_strcmp0 (method_name, "RegisterDocument") == 0) {
+                GDBusConnection *connection;
+                const char *sender;
                 EvDoc       *doc;
-                const gchar *uri;
-
-                g_variant_get (parameters, "(&s)", &uri);
 
                 doc = ev_daemon_find_doc (uri);
                 if (doc != NULL) {
                         LOG ("RegisterDocument found owner '%s' for URI '%s'\n", doc->dbus_name, uri);
-                        g_dbus_method_invocation_return_value (invocation,
-                                                               g_variant_new ("(s)", doc->dbus_name));
-                        return;
+                        ev_daemon_complete_register_document (object, invocation, doc->dbus_name);
+                        return TRUE;
                 }
 
                 ev_daemon_stop_killtimer ();
 
+                sender = g_dbus_method_invocation_get_sender (invocation);
+                connection = g_dbus_method_invocation_get_connection (invocation);
+
                 doc = g_new (EvDoc, 1);
                 doc->dbus_name = g_strdup (sender);
                 doc->uri = g_strdup (uri);
@@ -262,12 +257,18 @@ method_call_cb (GDBusConnection       *connection,
                 LOG ("RegisterDocument registered owner '%s' for URI '%s'\n", doc->dbus_name, uri);
                 ev_daemon_docs = g_list_prepend (ev_daemon_docs, doc);
 
-                g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", ""));
-        } else if (g_strcmp0 (method_name, "UnregisterDocument") == 0) {
-                EvDoc *doc;
-                const gchar *uri;
+                ev_daemon_complete_register_document (object, invocation, "");
+                return TRUE;
+}
 
-                g_variant_get (parameters, "(&s)", &uri);
+static gboolean
+handle_unregister_document_cb (EvDaemon *object,
+                               GDBusMethodInvocation *invocation,
+                               const gchar *uri,
+                               gpointer user_data)
+{
+                EvDoc *doc;
+                const char *sender;
 
                 LOG ("UnregisterDocument URI '%s'\n", uri);
 
@@ -278,9 +279,10 @@ method_call_cb (GDBusConnection       *connection,
                                                                        G_DBUS_ERROR,
                                                                        G_DBUS_ERROR_INVALID_ARGS,
                                                                        "URI not registered");
-                        return;
+                        return TRUE;
                 }
 
+                sender = g_dbus_method_invocation_get_sender (invocation);
                 if (strcmp (doc->dbus_name, sender) != 0) {
                         LOG ("UnregisterDocument called by non-owner (owner '%s' sender '%s')\n",
                              doc->dbus_name, sender);
@@ -289,28 +291,32 @@ method_call_cb (GDBusConnection       *connection,
                                                                        G_DBUS_ERROR,
                                                                        G_DBUS_ERROR_BAD_ADDRESS,
                                                                        "Only owner can call this method");
-                        return;
+                        return TRUE;
                 }
 
                 ev_daemon_docs = g_list_remove (ev_daemon_docs, doc);
                 ev_doc_free (doc);
                 ev_daemon_maybe_start_killtimer (user_data);
 
-                g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
-	} else if (g_strcmp0 (method_name, "FindDocument") == 0) {
-		EvDoc *doc;
-		const gchar *uri;
-		gboolean spawn;
+                ev_daemon_complete_unregister_document (object, invocation);
+                return TRUE;
+}
 
-		g_variant_get (parameters, "(&sb)",  &uri, &spawn);
+static gboolean 
+handle_find_document_cb (EvDaemon *object,
+                         GDBusMethodInvocation *invocation,
+                         const gchar *uri,
+                         gboolean spawn,
+                         gpointer user_data)
+{
+		EvDoc *doc;
 
 		LOG ("FindDocument URI '%s' \n", uri);
 
 		doc = ev_daemon_find_doc (uri);
 		if (doc != NULL) {
-			g_dbus_method_invocation_return_value (invocation,
-							       g_variant_new ("(s)", doc->dbus_name));
-			return;
+                        ev_daemon_complete_find_document (object, invocation, doc->dbus_name);
+                        return TRUE;
 		}
 
 		if (spawn) {
@@ -330,68 +336,43 @@ method_call_cb (GDBusConnection       *connection,
 				g_hash_table_insert (pending_invocations,
 						     g_strdup (uri),
 						     uri_invocations);
-				return;
+                                return TRUE;
 			}
 		}
 
 		LOG ("FindDocument URI '%s' was not registered!\n", uri);
-		g_dbus_method_invocation_return_value (invocation,
-						       g_variant_new ("(s)",""));
-	}
+                // FIXME shouldn't this return an error then?
+                ev_daemon_complete_find_document (object, invocation, "");
+                return TRUE;
 }
 
-static const char introspection_xml[] =
-  "<node>"
-    "<interface name='org.gnome.evince.Daemon'>"
-      "<method name='RegisterDocument'>"
-        "<arg type='s' name='uri' direction='in'/>"
-        "<arg type='s' name='owner' direction='out'/>"
-      "</method>"
-      "<method name='UnregisterDocument'>"
-        "<arg type='s' name='uri' direction='in'/>"
-      "</method>"
-      "<method name='FindDocument'>"
-        "<arg type='s' name='uri' direction='in'/>"
-        "<arg type='b' name='spawn' direction='in'/>"
-        "<arg type='s' name='owner' direction='out'/>"
-      "</method>"
-    "</interface>"
-  "</node>";
-
-static const GDBusInterfaceVTable interface_vtable = {
-  method_call_cb,
-  NULL,
-  NULL
-};
-
-static GDBusNodeInfo *introspection_data;
-
 static void
 bus_acquired_cb (GDBusConnection *connection,
 		 const gchar     *name,
 		 gpointer         user_data)
 {
 	GMainLoop *loop = (GMainLoop *) user_data;
-	guint      registration_id;
+        EvDaemon *skeleton;
 	GError    *error = NULL;
 
-	if (!introspection_data)
-		introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
-
-	registration_id = g_dbus_connection_register_object (connection,
-							     EV_DBUS_DAEMON_OBJECT_PATH,
-							     introspection_data->interfaces[0],
-							     &interface_vtable,
-							     g_main_loop_ref (loop),
-							     (GDestroyNotify) g_main_loop_unref,
-							     &error);
-	if (registration_id == 0) {
-		g_printerr ("Failed to register object: %s\n", error->message);
+        skeleton = ev_daemon_skeleton_new ();
+        if (!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (skeleton),
+                                               connection,
+                                               EV_DBUS_DAEMON_OBJECT_PATH,
+                                               &error)) {
+                g_printerr ("Failed to export object: %s\n", error->message);
 		g_error_free (error);
 
 		if (g_main_loop_is_running (loop))
 			g_main_loop_quit (loop);
 	}
+
+        g_signal_connect (skeleton, "handle-register-document",
+                          G_CALLBACK (handle_register_document_cb), loop);
+        g_signal_connect (skeleton, "handle-unregister-document",
+                          G_CALLBACK (handle_unregister_document_cb), loop);
+        g_signal_connect (skeleton, "handle-find-document",
+                          G_CALLBACK (handle_find_document_cb), loop);
 }
 
 static void
@@ -445,8 +426,6 @@ main (gint argc, gchar **argv)
         g_bus_unown_name (owner_id);
 
         g_main_loop_unref (loop);
-	if (introspection_data)
-		g_dbus_node_info_unref (introspection_data);
         g_list_foreach (ev_daemon_docs, (GFunc)ev_doc_free, NULL);
         g_list_free (ev_daemon_docs);
         g_hash_table_destroy (pending_invocations);



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