2 * this file is part of evince, a gnome document viewer
4 * Copyright (C) 2009 Carlos Garcia Campos <carlosgc@gnome.org>
5 * Copyright © 2010 Christian Persch
7 * Evince is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * Evince is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include <glib/gstdio.h>
28 #include <sys/types.h>
34 #define EV_DBUS_DAEMON_NAME "org.gnome.evince.Daemon"
35 #define EV_DBUS_DAEMON_INTERFACE_NAME "org.gnome.evince.Daemon"
36 #define EV_DBUS_DAEMON_OBJECT_PATH "/org/gnome/evince/Daemon"
38 #define EV_DBUS_WINDOW_INTERFACE_NAME "org.gnome.evince.Window"
40 #define DAEMON_TIMEOUT (30) /* seconds */
42 #define LOG g_printerr
44 static GList *ev_daemon_docs = NULL;
45 static guint kill_timer_id;
46 static GHashTable *pending_invocations = NULL;
56 ev_doc_free (EvDoc *doc)
61 g_free (doc->dbus_name);
64 g_bus_unwatch_name (doc->watch_id);
70 ev_daemon_find_doc (const gchar *uri)
74 for (l = ev_daemon_docs; l != NULL; l = l->next) {
75 EvDoc *doc = (EvDoc *)l->data;
77 if (strcmp (doc->uri, uri) == 0)
85 ev_daemon_stop_killtimer (void)
87 if (kill_timer_id != 0)
88 g_source_remove (kill_timer_id);
93 ev_daemon_shutdown (gpointer user_data)
95 GMainLoop *loop = (GMainLoop *) user_data;
97 LOG ("Timeout; exiting daemon.\n");
99 if (g_main_loop_is_running (loop))
100 g_main_loop_quit (loop);
106 ev_daemon_maybe_start_killtimer (gpointer data)
108 ev_daemon_stop_killtimer ();
109 if (ev_daemon_docs != NULL)
112 kill_timer_id = g_timeout_add_seconds (DAEMON_TIMEOUT,
113 (GSourceFunc) ev_daemon_shutdown,
118 convert_metadata (const gchar *metadata)
123 GFileAttributeInfoList *namespaces;
124 gboolean supported = FALSE;
125 GError *error = NULL;
128 /* If metadata is not supported for a local file
129 * is likely because and old gvfs version is running.
131 file = g_file_new_for_path (metadata);
132 namespaces = g_file_query_writable_namespaces (file, NULL, NULL);
136 for (i = 0; i < namespaces->n_infos; i++) {
137 if (strcmp (namespaces->infos[i].name, "metadata") == 0) {
142 g_file_attribute_info_list_unref (namespaces);
145 g_warning ("GVFS metadata not supported. "
146 "Evince will run without metadata support.\n");
147 g_object_unref (file);
150 g_object_unref (file);
152 argv[0] = g_build_filename (LIBEXECDIR, "evince-convert-metadata", NULL);
153 argv[1] = (char *) metadata;
156 retval = g_spawn_sync (NULL /* wd */, argv, NULL /* env */,
157 0, NULL, NULL, NULL, NULL,
158 &exit_status, &error);
162 g_printerr ("Error migrating metadata: %s\n", error->message);
163 g_error_free (error);
166 return retval && WIFEXITED (exit_status) && WEXITSTATUS (exit_status) == 0;
170 ev_migrate_metadata (void)
175 const gchar *userdir;
177 userdir = g_getenv ("GNOME22_USER_DIR");
179 dot_dir = g_build_filename (userdir, "evince", NULL);
181 dot_dir = g_build_filename (g_get_home_dir (),
187 updated = g_build_filename (dot_dir, "migrated-to-gvfs", NULL);
188 if (g_file_test (updated, G_FILE_TEST_EXISTS)) {
189 /* Already migrated */
195 metadata = g_build_filename (dot_dir, "ev-metadata.xml", NULL);
196 if (g_file_test (metadata, G_FILE_TEST_EXISTS)) {
197 if (convert_metadata (metadata)) {
200 fd = g_creat (updated, 0600);
213 spawn_evince (const gchar *uri)
217 GError *error = NULL;
219 /* TODO Check that the uri exists */
220 argv[0] = g_build_filename (BINDIR, "evince", NULL);
221 argv[1] = (gchar *) uri;
224 retval = g_spawn_async (NULL /* wd */, argv, NULL /* env */,
225 0, NULL, NULL, NULL, &error);
227 g_printerr ("Error spawning evince for uri %s: %s\n", uri, error->message);
228 g_error_free (error);
236 name_appeared_cb (GDBusConnection *connection,
238 const gchar *name_owner,
241 LOG ("Watch name'%s' appeared with owner '%s'\n", name, name_owner);
245 name_vanished_cb (GDBusConnection *connection,
251 LOG ("Watch name'%s' disappeared\n", name);
253 for (l = ev_daemon_docs; l != NULL; l = l->next) {
254 EvDoc *doc = (EvDoc *) l->data;
256 if (strcmp (doc->dbus_name, name) != 0)
259 LOG ("Watch found URI '%s' for name; removing\n", doc->uri);
261 ev_daemon_docs = g_list_delete_link (ev_daemon_docs, l);
264 ev_daemon_maybe_start_killtimer (user_data);
270 process_pending_invocations (const gchar *uri,
271 const gchar *dbus_name)
274 GList *uri_invocations;
276 LOG ("RegisterDocument process pending invocations for URI %s\n", uri);
277 uri_invocations = g_hash_table_lookup (pending_invocations, uri);
279 for (l = uri_invocations; l != NULL; l = l->next) {
280 GDBusMethodInvocation *invocation;
282 invocation = (GDBusMethodInvocation *)l->data;
283 g_dbus_method_invocation_return_value (invocation,
284 g_variant_new ("(s)", dbus_name));
287 g_list_free (uri_invocations);
288 g_hash_table_remove (pending_invocations, uri);
292 document_loaded_cb (GDBusConnection *connection,
293 const gchar *sender_name,
294 const gchar *object_path,
295 const gchar *interface_name,
296 const gchar *signal_name,
297 GVariant *parameters,
302 g_variant_get (parameters, "(&s)", &uri);
303 if (strcmp (uri, doc->uri) == 0)
304 process_pending_invocations (uri, sender_name);
305 g_dbus_connection_signal_unsubscribe (connection, doc->loaded_id);
309 method_call_cb (GDBusConnection *connection,
311 const gchar *object_path,
312 const gchar *interface_name,
313 const gchar *method_name,
314 GVariant *parameters,
315 GDBusMethodInvocation *invocation,
318 if (g_strcmp0 (interface_name, EV_DBUS_DAEMON_INTERFACE_NAME) != 0)
321 if (g_strcmp0 (method_name, "RegisterDocument") == 0) {
325 g_variant_get (parameters, "(&s)", &uri);
327 doc = ev_daemon_find_doc (uri);
329 LOG ("RegisterDocument found owner '%s' for URI '%s'\n", doc->dbus_name, uri);
330 g_dbus_method_invocation_return_value (invocation,
331 g_variant_new ("(s)", doc->dbus_name));
335 ev_daemon_stop_killtimer ();
337 doc = g_new (EvDoc, 1);
338 doc->dbus_name = g_strdup (sender);
339 doc->uri = g_strdup (uri);
341 doc->loaded_id = g_dbus_connection_signal_subscribe (connection,
343 EV_DBUS_WINDOW_INTERFACE_NAME,
348 (GDBusSignalCallback) document_loaded_cb,
351 doc->watch_id = g_bus_watch_name_on_connection (connection,
353 G_BUS_NAME_WATCHER_FLAGS_NONE,
358 LOG ("RegisterDocument registered owner '%s' for URI '%s'\n", doc->dbus_name, uri);
359 ev_daemon_docs = g_list_prepend (ev_daemon_docs, doc);
361 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", ""));
362 } else if (g_strcmp0 (method_name, "UnregisterDocument") == 0) {
366 g_variant_get (parameters, "(&s)", &uri);
368 LOG ("UnregisterDocument URI '%s'\n", uri);
370 doc = ev_daemon_find_doc (uri);
372 LOG ("UnregisterDocument URI was not registered!\n");
373 g_dbus_method_invocation_return_error_literal (invocation,
375 G_DBUS_ERROR_INVALID_ARGS,
376 "URI not registered");
380 if (strcmp (doc->dbus_name, sender) != 0) {
381 LOG ("UnregisterDocument called by non-owner (owner '%s' sender '%s')\n",
382 doc->dbus_name, sender);
384 g_dbus_method_invocation_return_error_literal (invocation,
386 G_DBUS_ERROR_BAD_ADDRESS,
387 "Only owner can call this method");
391 ev_daemon_docs = g_list_remove (ev_daemon_docs, doc);
393 ev_daemon_maybe_start_killtimer (user_data);
395 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
396 } else if (g_strcmp0 (method_name, "FindDocument") == 0) {
401 g_variant_get (parameters, "(&sb)", &uri, &spawn);
403 LOG ("FindDocument URI '%s' \n", uri);
405 doc = ev_daemon_find_doc (uri);
407 g_dbus_method_invocation_return_value (invocation,
408 g_variant_new ("(s)", doc->dbus_name));
413 GList *uri_invocations;
414 gboolean ret_val = TRUE;
416 uri_invocations = g_hash_table_lookup (pending_invocations, uri);
418 if (uri_invocations == NULL) {
419 /* Only spawn once. */
420 ret_val = spawn_evince (uri);
424 /* Only defer DBUS answer if evince was succesfully spawned */
425 uri_invocations = g_list_prepend (uri_invocations, invocation);
426 g_hash_table_insert (pending_invocations,
433 LOG ("FindDocument URI '%s' was not registered!\n", uri);
434 g_dbus_method_invocation_return_value (invocation,
435 g_variant_new ("(s)",""));
439 static const char introspection_xml[] =
441 "<interface name='org.gnome.evince.Daemon'>"
442 "<method name='RegisterDocument'>"
443 "<arg type='s' name='uri' direction='in'/>"
444 "<arg type='s' name='owner' direction='out'/>"
446 "<method name='UnregisterDocument'>"
447 "<arg type='s' name='uri' direction='in'/>"
449 "<method name='FindDocument'>"
450 "<arg type='s' name='uri' direction='in'/>"
451 "<arg type='b' name='spawn' direction='in'/>"
452 "<arg type='s' name='owner' direction='out'/>"
457 static const GDBusInterfaceVTable interface_vtable = {
463 static GDBusNodeInfo *introspection_data;
466 bus_acquired_cb (GDBusConnection *connection,
470 GMainLoop *loop = (GMainLoop *) user_data;
471 guint registration_id;
472 GError *error = NULL;
474 if (!introspection_data)
475 introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
477 registration_id = g_dbus_connection_register_object (connection,
478 EV_DBUS_DAEMON_OBJECT_PATH,
479 introspection_data->interfaces[0],
481 g_main_loop_ref (loop),
482 (GDestroyNotify) g_main_loop_unref,
484 if (registration_id == 0) {
485 g_printerr ("Failed to register object: %s\n", error->message);
486 g_error_free (error);
488 if (g_main_loop_is_running (loop))
489 g_main_loop_quit (loop);
494 name_acquired_cb (GDBusConnection *connection,
498 ev_migrate_metadata ();
500 ev_daemon_maybe_start_killtimer (user_data);
504 name_lost_cb (GDBusConnection *connection,
508 GMainLoop *loop = (GMainLoop *) user_data;
510 /* Failed to acquire the name; exit daemon */
511 if (g_main_loop_is_running (loop))
512 g_main_loop_quit (loop);
516 main (gint argc, gchar **argv)
521 g_set_prgname ("evince-daemon");
525 loop = g_main_loop_new (NULL, FALSE);
527 pending_invocations = g_hash_table_new_full (g_str_hash,
529 (GDestroyNotify)g_free,
532 owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
534 G_BUS_NAME_OWNER_FLAGS_NONE,
538 g_main_loop_ref (loop),
539 (GDestroyNotify) g_main_loop_unref);
541 g_main_loop_run (loop);
543 g_bus_unown_name (owner_id);
545 g_main_loop_unref (loop);
546 if (introspection_data)
547 g_dbus_node_info_unref (introspection_data);
548 g_list_foreach (ev_daemon_docs, (GFunc)ev_doc_free, NULL);
549 g_list_free (ev_daemon_docs);
550 g_hash_table_destroy (pending_invocations);