diff options
author | Christian Persch <[email protected]> | 2012-06-11 22:00:31 +0200 |
---|---|---|
committer | raveit65 <[email protected]> | 2018-03-21 06:56:34 +0100 |
commit | 131b29ed88185f63421a9e5dae1e0352159232d3 (patch) | |
tree | d1077b09f3649a38232140f24654d6b7828a25dd /shell/ev-daemon.c | |
parent | f44500b8d0b8113e3fb475e5e9fb1c685ca5653a (diff) | |
download | atril-131b29ed88185f63421a9e5dae1e0352159232d3.tar.bz2 atril-131b29ed88185f63421a9e5dae1e0352159232d3.tar.xz |
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
Diffstat (limited to 'shell/ev-daemon.c')
-rw-r--r-- | shell/ev-daemon.c | 304 |
1 files changed, 144 insertions, 160 deletions
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 <fcntl.h> #include <unistd.h> +#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[] = - "<node>" - "<interface name='org.mate.atril.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 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); |