+static void
+name_appeared_cb (GDBusConnection *connection,
+ const gchar *name,
+ const gchar *name_owner,
+ gpointer user_data)
+{
+ LOG ("Watch name'%s' appeared with owner '%s'\n", name, name_owner);
+}
+
+static void
+name_vanished_cb (GDBusConnection *connection,
+ const gchar *name,
+ gpointer user_data)
+{
+ 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);
+
+ ev_daemon_maybe_start_killtimer (user_data);
+ return;
+ }
+}
+
+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)
+{
+ if (g_strcmp0 (interface_name, EV_DBUS_DAEMON_INTERFACE_NAME) != 0)
+ return;
+
+ if (g_strcmp0 (method_name, "RegisterDocument") == 0) {
+ 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_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_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;
+ }
+
+ 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;
+ }
+
+ 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 ("()"));
+ }
+}
+
+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>"
+ "</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;
+ 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,
+ gpointer user_data)
+{
+ ev_migrate_metadata ();
+
+ ev_daemon_maybe_start_killtimer (user_data);
+}
+
+static void
+name_lost_cb (GDBusConnection *connection,
+ const gchar *name,
+ gpointer user_data)
+{
+ GMainLoop *loop = (GMainLoop *) user_data;
+
+ /* Failed to acquire the name; exit daemon */
+ if (g_main_loop_is_running (loop))
+ g_main_loop_quit (loop);
+}
+