From 131b29ed88185f63421a9e5dae1e0352159232d3 Mon Sep 17 00:00:00 2001 From: Christian Persch Date: Mon, 11 Jun 2012 22:00:31 +0200 Subject: daemon: Use gdbus-codegen for the org.mate.atril.Daemon interface origin commit: https://git.gnome.org/browse/evince/commit/?h=gnome-3-6&id=1aac15e --- shell/Makefile.am | 26 +++- shell/ev-daemon-gdbus.xml | 19 +++ shell/ev-daemon.c | 304 ++++++++++++++++++++++------------------------ 3 files changed, 185 insertions(+), 164 deletions(-) create mode 100644 shell/ev-daemon-gdbus.xml diff --git a/shell/Makefile.am b/shell/Makefile.am index 7f044db4..a8d40f10 100644 --- a/shell/Makefile.am +++ b/shell/Makefile.am @@ -120,12 +120,21 @@ atril_LDADD= \ BUILT_SOURCES = ev-marshal.h ev-marshal.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 atrild_SOURCES= \ - ev-daemon.c + ev-daemon.c \ + $(NULL) +nodist_atrild_SOURCES = \ + ev-daemon-gdbus-generated.c \ + ev-daemon-gdbus-generated.h + $(NULL) atrild_CFLAGS= \ -DATRILDATADIR=\"$(pkgdatadir)\" \ @@ -144,7 +153,8 @@ endif EXTRA_DIST = \ ev-marshal.list \ - 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 @@ -161,6 +171,14 @@ ev-gdbus-generated.c ev-gdbus-generated.h: ev-gdbus.xml Makefile --generate-c-code ev-gdbus-generated \ $< -DISTCLEANFILES = +ev-daemon-gdbus-generated.c ev-daemon-gdbus-generated.h: ev-daemon-gdbus.xml Makefile + $(AM_V_GEN) $(GDBUS_CODEGEN) \ + --interface-prefix=org.mate.atril \ + --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 00000000..dc65f831 --- /dev/null +++ b/shell/ev-daemon-gdbus.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + diff --git a/shell/ev-daemon.c b/shell/ev-daemon.c index 897b16e7..ea7f66ef 100644 --- a/shell/ev-daemon.c +++ b/shell/ev-daemon.c @@ -31,6 +31,8 @@ #include #include +#include "ev-daemon-gdbus-generated.h" + #define EV_DBUS_DAEMON_NAME "org.mate.atril.Daemon" #define EV_DBUS_DAEMON_INTERFACE_NAME "org.mate.atril.Daemon" #define EV_DBUS_DAEMON_OBJECT_PATH "/org/mate/atril/Daemon" @@ -210,162 +212,145 @@ 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; + GDBusConnection *connection; + const char *sender; + EvDoc *doc; - if (g_strcmp0 (method_name, "RegisterDocument") == 0) { - EvDoc *doc; - const gchar *uri; + doc = ev_daemon_find_doc (uri); + if (doc != NULL) { + LOG ("RegisterDocument found owner '%s' for URI '%s'\n", doc->dbus_name, uri); + ev_daemon_complete_register_document (object, invocation, doc->dbus_name); - g_variant_get (parameters, "(&s)", &uri); + return TRUE; + } - 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_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); + + doc->loaded_id = g_dbus_connection_signal_subscribe (connection, + doc->dbus_name, + EV_DBUS_WINDOW_INTERFACE_NAME, + "DocumentLoaded", + NULL, + NULL, + 0, + (GDBusSignalCallback) document_loaded_cb, + doc, + NULL); + doc->watch_id = g_bus_watch_name_on_connection (connection, + sender, + G_BUS_NAME_WATCHER_FLAGS_NONE, + name_appeared_cb, + name_vanished_cb, + user_data, NULL); + + LOG ("RegisterDocument registered owner '%s' for URI '%s'\n", doc->dbus_name, uri); + ev_daemon_docs = g_list_prepend (ev_daemon_docs, doc); + + ev_daemon_complete_register_document (object, invocation, ""); + + return TRUE; +} - ev_daemon_stop_killtimer (); - - doc = g_new (EvDoc, 1); - doc->dbus_name = g_strdup (sender); - doc->uri = g_strdup (uri); - - doc->loaded_id = g_dbus_connection_signal_subscribe (connection, - doc->dbus_name, - EV_DBUS_WINDOW_INTERFACE_NAME, - "DocumentLoaded", - NULL, - NULL, - 0, - (GDBusSignalCallback) document_loaded_cb, - doc, - NULL); - doc->watch_id = g_bus_watch_name_on_connection (connection, - sender, - G_BUS_NAME_WATCHER_FLAGS_NONE, - name_appeared_cb, - name_vanished_cb, - user_data, NULL); - - 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; - - g_variant_get (parameters, "(&s)", &uri); - - LOG ("UnregisterDocument URI '%s'\n", uri); - - doc = ev_daemon_find_doc (uri); - if (doc == NULL) { - LOG ("UnregisterDocument URI was not registered!\n"); - g_dbus_method_invocation_return_error_literal (invocation, - G_DBUS_ERROR, - G_DBUS_ERROR_INVALID_ARGS, - "URI not registered"); - return; - } +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); + + doc = ev_daemon_find_doc (uri); + if (doc == NULL) { + LOG ("UnregisterDocument URI was not registered!\n"); + g_dbus_method_invocation_return_error_literal (invocation, + G_DBUS_ERROR, + G_DBUS_ERROR_INVALID_ARGS, + "URI not registered"); + return TRUE; + } - if (strcmp (doc->dbus_name, sender) != 0) { - LOG ("UnregisterDocument called by non-owner (owner '%s' sender '%s')\n", - doc->dbus_name, sender); + 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); - g_dbus_method_invocation_return_error_literal (invocation, - G_DBUS_ERROR, - G_DBUS_ERROR_BAD_ADDRESS, - "Only owner can call this method"); - return; - } + g_dbus_method_invocation_return_error_literal (invocation, + G_DBUS_ERROR, + G_DBUS_ERROR_BAD_ADDRESS, + "Only owner can call this method"); + return TRUE; + } - ev_daemon_docs = g_list_remove (ev_daemon_docs, doc); - ev_doc_free (doc); - ev_daemon_maybe_start_killtimer (user_data); + 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; - - g_variant_get (parameters, "(&sb)", &uri, &spawn); - - 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; - } - - if (spawn) { - GList *uri_invocations; - gboolean ret_val = TRUE; - - uri_invocations = g_hash_table_lookup (pending_invocations, uri); - - if (uri_invocations == NULL) { - /* Only spawn once. */ - ret_val = spawn_atril (uri); - } - - if (ret_val) { - /* Only defer DBUS answer if atril was succesfully spawned */ - uri_invocations = g_list_prepend (uri_invocations, invocation); - g_hash_table_insert (pending_invocations, - g_strdup (uri), - uri_invocations); - return; - } - } - - LOG ("FindDocument URI '%s' was not registered!\n", uri); - g_dbus_method_invocation_return_value (invocation, - g_variant_new ("(s)","")); - } + ev_daemon_complete_unregister_document (object, invocation); + + return TRUE; } -static const char introspection_xml[] = - "" - "" - "" - "" - "" - "" - "" - "" - "" - "" - "" - "" - "" - "" - "" - ""; - -static const GDBusInterfaceVTable interface_vtable = { - method_call_cb, - NULL, - NULL -}; - -static GDBusNodeInfo *introspection_data; +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) { + ev_daemon_complete_find_document (object, invocation, doc->dbus_name); + + return TRUE; + } + + if (spawn) { + GList *uri_invocations; + gboolean ret_val = TRUE; + + uri_invocations = g_hash_table_lookup (pending_invocations, uri); + + if (uri_invocations == NULL) { + /* Only spawn once. */ + ret_val = spawn_atril (uri); + } + + if (ret_val) { + /* Only defer DBUS answer if atril was succesfully spawned */ + uri_invocations = g_list_prepend (uri_invocations, invocation); + g_hash_table_insert (pending_invocations, + g_strdup (uri), + uri_invocations); + return TRUE; + } + } + + LOG ("FindDocument URI '%s' was not registered!\n", uri); + // FIXME: shouldn't this return an error then? + ev_daemon_complete_find_document (object, invocation, ""); + + return TRUE; +} static void bus_acquired_cb (GDBusConnection *connection, @@ -373,26 +358,27 @@ bus_acquired_cb (GDBusConnection *connection, 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 @@ -444,8 +430,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); -- cgit v1.2.1