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 DAEMON_TIMEOUT (30) /* seconds */
40 #define LOG g_printerr
42 static GList *ev_daemon_docs = NULL;
43 static guint kill_timer_id;
52 ev_doc_free (EvDoc *doc)
57 g_free (doc->dbus_name);
60 g_bus_unwatch_name (doc->watch_id);
66 ev_daemon_find_doc (const gchar *uri)
70 for (l = ev_daemon_docs; l != NULL; l = l->next) {
71 EvDoc *doc = (EvDoc *)l->data;
73 if (strcmp (doc->uri, uri) == 0)
81 ev_daemon_stop_killtimer (void)
83 if (kill_timer_id != 0)
84 g_source_remove (kill_timer_id);
89 ev_daemon_shutdown (gpointer user_data)
91 GMainLoop *loop = (GMainLoop *) user_data;
93 LOG ("Timeout; exiting daemon.\n");
95 if (g_main_loop_is_running (loop))
96 g_main_loop_quit (loop);
102 ev_daemon_maybe_start_killtimer (gpointer data)
104 ev_daemon_stop_killtimer ();
105 if (ev_daemon_docs != NULL)
108 kill_timer_id = g_timeout_add_seconds (DAEMON_TIMEOUT,
109 (GSourceFunc) ev_daemon_shutdown,
114 convert_metadata (const gchar *metadata)
119 GFileAttributeInfoList *namespaces;
120 gboolean supported = FALSE;
121 GError *error = NULL;
124 /* If metadata is not supported for a local file
125 * is likely because and old gvfs version is running.
127 file = g_file_new_for_path (metadata);
128 namespaces = g_file_query_writable_namespaces (file, NULL, NULL);
132 for (i = 0; i < namespaces->n_infos; i++) {
133 if (strcmp (namespaces->infos[i].name, "metadata") == 0) {
138 g_file_attribute_info_list_unref (namespaces);
141 g_warning ("GVFS metadata not supported. "
142 "Evince will run without metadata support.\n");
143 g_object_unref (file);
146 g_object_unref (file);
148 argv[0] = g_build_filename (LIBEXECDIR, "evince-convert-metadata", NULL);
149 argv[1] = (char *) metadata;
152 retval = g_spawn_sync (NULL /* wd */, argv, NULL /* env */,
153 0, NULL, NULL, NULL, NULL,
154 &exit_status, &error);
158 g_printerr ("Error migrating metadata: %s\n", error->message);
159 g_error_free (error);
162 return retval && WIFEXITED (exit_status) && WEXITSTATUS (exit_status) == 0;
166 ev_migrate_metadata (void)
171 const gchar *userdir;
173 userdir = g_getenv ("GNOME22_USER_DIR");
175 dot_dir = g_build_filename (userdir, "evince", NULL);
177 dot_dir = g_build_filename (g_get_home_dir (),
183 updated = g_build_filename (dot_dir, "migrated-to-gvfs", NULL);
184 if (g_file_test (updated, G_FILE_TEST_EXISTS)) {
185 /* Already migrated */
191 metadata = g_build_filename (dot_dir, "ev-metadata.xml", NULL);
192 if (g_file_test (metadata, G_FILE_TEST_EXISTS)) {
193 if (convert_metadata (metadata)) {
196 fd = g_creat (updated, 0600);
209 name_appeared_cb (GDBusConnection *connection,
211 const gchar *name_owner,
217 name_vanished_cb (GDBusConnection *connection,
223 for (l = ev_daemon_docs; l != NULL; l = l->next) {
224 EvDoc *doc = (EvDoc *) l->data;
226 if (strcmp (doc->dbus_name, name) != 0)
229 ev_daemon_docs = g_list_delete_link (ev_daemon_docs, l);
232 ev_daemon_maybe_start_killtimer (user_data);
238 method_call_cb (GDBusConnection *connection,
240 const gchar *object_path,
241 const gchar *interface_name,
242 const gchar *method_name,
243 GVariant *parameters,
244 GDBusMethodInvocation *invocation,
247 if (g_strcmp0 (interface_name, EV_DBUS_DAEMON_INTERFACE_NAME) != 0)
250 if (g_strcmp0 (method_name, "RegisterDocument") == 0) {
254 g_variant_get (parameters, "(&s)", &uri);
256 doc = ev_daemon_find_doc (uri);
258 ev_daemon_stop_killtimer ();
260 doc = g_new (EvDoc, 1);
261 doc->dbus_name = g_strdup (sender);
262 doc->uri = g_strdup (uri);
264 doc->watch_id = g_bus_watch_name (G_BUS_TYPE_STARTER,
266 G_BUS_NAME_WATCHER_FLAGS_NONE,
271 LOG ("RegisterDocument registered owner '%s' for URI '%s'\n", doc->dbus_name, uri);
272 ev_daemon_docs = g_list_prepend (ev_daemon_docs, doc);
274 LOG ("RegisterDocument found owner '%s' for URI '%s'\n", doc->dbus_name, uri);
277 g_dbus_method_invocation_return_value (invocation,
278 g_variant_new ("(s)", doc->dbus_name));
281 } else if (g_strcmp0 (method_name, "UnregisterDocument") == 0) {
285 g_variant_get (parameters, "(&s)", &uri);
287 LOG ("UnregisterDocument URI '%s'\n", uri);
289 doc = ev_daemon_find_doc (uri);
291 LOG ("UnregisterDocument URI was not registered!\n");
292 g_dbus_method_invocation_return_error_literal (invocation,
294 G_DBUS_ERROR_INVALID_ARGS,
295 "URI not registered");
299 if (strcmp (doc->dbus_name, sender) != 0) {
300 LOG ("UnregisterDocument called by non-owner (owner '%s' sender '%s')\n",
301 doc->dbus_name, sender);
303 g_dbus_method_invocation_return_error_literal (invocation,
305 G_DBUS_ERROR_BAD_ADDRESS,
306 "Only owner can call this method");
310 ev_daemon_docs = g_list_remove (ev_daemon_docs, doc);
312 ev_daemon_maybe_start_killtimer (user_data);
314 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
320 name_acquired_cb (GDBusConnection *connection,
324 ev_migrate_metadata ();
326 ev_daemon_maybe_start_killtimer (user_data);
330 name_lost_cb (GDBusConnection *connection,
334 GMainLoop *loop = (GMainLoop *) user_data;
336 /* Failed to acquire the name; exit daemon */
337 if (g_main_loop_is_running (loop))
338 g_main_loop_quit (loop);
341 static const char introspection_xml[] =
343 "<interface name='org.gnome.evince.Daemon'>"
344 "<method name='RegisterDocument'>"
345 "<arg type='s' name='uri' direction='in'/>"
346 "<arg type='s' name='owner' direction='out'/>"
348 "<method name='UnregisterDocument'>"
349 "<arg type='s' name='uri' direction='in'/>"
354 static const GDBusInterfaceVTable interface_vtable = {
361 main (gint argc, gchar **argv)
363 GDBusConnection *connection;
365 GError *error = NULL;
366 guint registration_id, owner_id;
367 GDBusNodeInfo *introspection_data;
369 g_set_prgname ("evince-daemon");
373 connection = g_bus_get_sync (G_BUS_TYPE_STARTER, NULL, &error);
374 if (connection == NULL) {
375 g_printerr ("Failed to get bus connection: %s\n", error->message);
376 g_error_free (error);
380 introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
381 g_assert (introspection_data != NULL);
383 loop = g_main_loop_new (NULL, FALSE);
385 registration_id = g_dbus_connection_register_object (connection,
386 EV_DBUS_DAEMON_OBJECT_PATH,
388 introspection_data->interfaces[0],
390 g_main_loop_ref (loop),
391 (GDestroyNotify) g_main_loop_unref,
393 if (registration_id == 0) {
394 g_printerr ("Failed to register object: %s\n", error->message);
395 g_error_free (error);
396 g_object_unref (connection);
400 owner_id = g_bus_own_name_on_connection (connection,
402 G_BUS_NAME_OWNER_FLAGS_NONE,
405 g_main_loop_ref (loop),
406 (GDestroyNotify) g_main_loop_unref);
408 g_main_loop_run (loop);
410 g_bus_unown_name (owner_id);
412 g_main_loop_unref (loop);
413 g_dbus_node_info_unref (introspection_data);
414 g_list_foreach (ev_daemon_docs, (GFunc)ev_doc_free, NULL);
415 g_list_free (ev_daemon_docs);
416 g_object_unref (connection);