X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=blobdiff_plain;f=shell%2Fev-daemon.c;h=b136369dfe8142e07f359d5758097469ba11213a;hb=2297bff1e7d745f0f837d44feeda03244368d7f1;hp=fcf472d8d26ceb017f60e70d2c6b7b86f058407b;hpb=166131c9257f2640843aef37f2febafc8f7da9ea;p=evince.git diff --git a/shell/ev-daemon.c b/shell/ev-daemon.c index fcf472d8..b136369d 100644 --- a/shell/ev-daemon.c +++ b/shell/ev-daemon.c @@ -37,6 +37,8 @@ #define DAEMON_TIMEOUT (30) /* seconds */ +#define LOG g_printerr + static GList *ev_daemon_docs = NULL; static guint kill_timer_id; @@ -88,6 +90,8 @@ ev_daemon_shutdown (gpointer user_data) { GMainLoop *loop = (GMainLoop *) user_data; + LOG ("Timeout; exiting daemon.\n"); + if (g_main_loop_is_running (loop)) g_main_loop_quit (loop); @@ -207,6 +211,7 @@ name_appeared_cb (GDBusConnection *connection, const gchar *name_owner, gpointer user_data) { + LOG ("Watch name'%s' appeared with owner '%s'\n", name, name_owner); } static void @@ -216,12 +221,16 @@ name_vanished_cb (GDBusConnection *connection, { GList *l; + LOG ("Watch name'%s' disappeared\n", name); + for (l = ev_daemon_docs; l != NULL; l = l->next) { EvDoc *doc = (EvDoc *) l->data; if (strcmp (doc->dbus_name, name) != 0) continue; + LOG ("Watch found URI '%s' for name; removing\n", doc->uri); + ev_daemon_docs = g_list_delete_link (ev_daemon_docs, l); ev_doc_free (doc); @@ -246,43 +255,45 @@ method_call_cb (GDBusConnection *connection, if (g_strcmp0 (method_name, "RegisterDocument") == 0) { EvDoc *doc; const gchar *uri; - const gchar *owner = NULL; g_variant_get (parameters, "(&s)", &uri); doc = ev_daemon_find_doc (uri); - if (doc) { - /* Already registered */ - owner = doc->dbus_name; - } else { - ev_daemon_stop_killtimer (); - - doc = g_new (EvDoc, 1); - doc->dbus_name = g_strdup (sender); - doc->uri = g_strdup (uri); - - doc->watch_id = g_bus_watch_name (G_BUS_TYPE_STARTER, - sender, - G_BUS_NAME_WATCHER_FLAGS_NONE, - name_appeared_cb, - name_vanished_cb, - user_data, NULL); - - ev_daemon_docs = g_list_prepend (ev_daemon_docs, doc); + 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 (); - g_dbus_method_invocation_return_value (invocation, - g_variant_new_string (owner)); - return; + doc = g_new (EvDoc, 1); + doc->dbus_name = g_strdup (sender); + doc->uri = g_strdup (uri); + + 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, @@ -291,6 +302,9 @@ method_call_cb (GDBusConnection *connection, } 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, @@ -303,10 +317,58 @@ method_call_cb (GDBusConnection *connection, ev_daemon_maybe_start_killtimer (user_data); g_dbus_method_invocation_return_value (invocation, g_variant_new ("()")); - return; } } +static const char introspection_xml[] = + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + ""; + +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; + 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); + g_error_free (error); + + if (g_main_loop_is_running (loop)) + g_main_loop_quit (loop); + } +} + static void name_acquired_cb (GDBusConnection *connection, const gchar *name, @@ -329,77 +391,34 @@ name_lost_cb (GDBusConnection *connection, g_main_loop_quit (loop); } -static const char introspection_xml[] = - "" - "" - "" - "" - "" - "" - "" - "" - "" - "" - ""; - -static const GDBusInterfaceVTable interface_vtable = { - method_call_cb, - NULL, - NULL -}; - gint main (gint argc, gchar **argv) { - GDBusConnection *connection; GMainLoop *loop; - GError *error = NULL; - guint registration_id, owner_id; - GDBusNodeInfo *introspection_data; - - g_type_init (); + guint owner_id; - connection = g_bus_get_sync (G_BUS_TYPE_STARTER, NULL, &error); - if (connection == NULL) { - g_printerr ("Failed to get bus connection: %s\n", error->message); - g_error_free (error); - return 1; - } + g_set_prgname ("evince-daemon"); - introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL); - g_assert (introspection_data != NULL); + g_type_init (); loop = g_main_loop_new (NULL, FALSE); - registration_id = g_dbus_connection_register_object (connection, - EV_DBUS_DAEMON_OBJECT_PATH, - EV_DBUS_DAEMON_NAME, - 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); - g_error_free (error); - g_object_unref (connection); - return 1; - } - - owner_id = g_bus_own_name_on_connection (connection, - EV_DBUS_DAEMON_NAME, - G_BUS_NAME_OWNER_FLAGS_NONE, - name_acquired_cb, - name_lost_cb, - g_main_loop_ref (loop), - (GDestroyNotify) g_main_loop_unref); + owner_id = g_bus_own_name (G_BUS_TYPE_SESSION, + EV_DBUS_DAEMON_NAME, + G_BUS_NAME_OWNER_FLAGS_NONE, + bus_acquired_cb, + name_acquired_cb, + name_lost_cb, + g_main_loop_ref (loop), + (GDestroyNotify) g_main_loop_unref); g_main_loop_run (loop); g_bus_unown_name (owner_id); g_main_loop_unref (loop); - g_dbus_node_info_unref (introspection_data); + 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);