1 /* this file is part of evince, a gnome document viewer
3 * Copyright (C) 2004 Martin Kretzschmar
4 * Copyright © 2010 Christian Persch
7 * Martin Kretzschmar <martink@gnome.org>
9 * Evince is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * Evince is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 #include <glib/gi18n.h>
31 #include <glib/gstdio.h>
33 #ifdef GDK_WINDOWING_X11
38 #include "totem-scrsaver.h"
41 #include "eggsmclient.h"
44 #include "ev-application.h"
45 #include "ev-file-helpers.h"
46 #include "ev-stock-icons.h"
49 #include "ev-media-player-keys.h"
50 #endif /* ENABLE_DBUS */
55 struct _EvApplication {
56 GObject base_instance;
64 GDBusConnection *connection;
65 guint registration_id;
66 EvMediaPlayerKeys *keys;
69 TotemScrsaver *scr_saver;
72 EggSMClient *smclient;
75 gchar *filechooser_open_uri;
76 gchar *filechooser_save_uri;
79 struct _EvApplicationClass {
80 GObjectClass base_class;
83 static EvApplication *instance;
85 G_DEFINE_TYPE (EvApplication, ev_application, G_TYPE_OBJECT);
88 #define APPLICATION_DBUS_OBJECT_PATH "/org/gnome/evince/Evince"
89 #define APPLICATION_DBUS_INTERFACE "org.gnome.evince.Application"
92 static const gchar *userdir = NULL;
95 * ev_application_get_instance:
97 * Checks for #EvApplication instance, if it doesn't exist it does create it.
99 * Returns: an instance of the #EvApplication data.
102 ev_application_get_instance (void)
105 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
113 ev_application_load_session (EvApplication *application)
115 GKeyFile *state_file;
119 if (egg_sm_client_is_resumed (application->smclient)) {
120 state_file = egg_sm_client_get_state_file (application->smclient);
124 #endif /* WITH_SMCLIENT */
127 uri = g_key_file_get_string (state_file, "Evince", "uri", NULL);
131 ev_application_open_uri_at_dest (application, uri,
132 gdk_screen_get_default (),
136 g_key_file_free (state_file);
144 smclient_save_state_cb (EggSMClient *client,
145 GKeyFile *state_file,
146 EvApplication *application)
148 if (!application->uri)
151 g_key_file_set_string (state_file, "Evince", "uri", application->uri);
155 smclient_quit_cb (EggSMClient *client,
156 EvApplication *application)
158 ev_application_shutdown (application);
160 #endif /* WITH_SMCLIENT */
163 ev_application_init_session (EvApplication *application)
166 application->smclient = egg_sm_client_get ();
167 g_signal_connect (application->smclient, "save_state",
168 G_CALLBACK (smclient_save_state_cb),
170 g_signal_connect (application->smclient, "quit",
171 G_CALLBACK (smclient_quit_cb),
177 * ev_display_open_if_needed:
178 * @name: the name of the display to be open if it's needed.
180 * Search among all the open displays if any of them have the same name as the
181 * passed name. If the display isn't found it tries the open it.
183 * Returns: a #GdkDisplay of the display with the passed name.
186 ev_display_open_if_needed (const gchar *name)
190 GdkDisplay *display = NULL;
192 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
194 for (l = displays; l != NULL; l = l->next) {
195 const gchar *display_name = gdk_display_get_name ((GdkDisplay *) l->data);
197 if (g_ascii_strcasecmp (display_name, name) == 0) {
203 g_slist_free (displays);
205 return display != NULL ? display : gdk_display_open (name);
209 child_setup (gpointer user_data)
213 startup_id = g_strdup_printf ("_TIME%lu",
214 (unsigned long)GPOINTER_TO_INT (user_data));
215 g_setenv ("DESKTOP_STARTUP_ID", startup_id, TRUE);
220 ev_spawn (const char *uri,
223 EvWindowRunMode mode,
224 const gchar *search_string,
231 GError *error = NULL;
237 dir = g_win32_get_package_installation_directory_of_module (NULL);
238 argv[arg++] = g_build_filename (dir, "bin", "evince", NULL);
242 argv[arg++] = g_build_filename (BINDIR, "evince", NULL);
247 const gchar *page_label;
249 page_label = ev_link_dest_get_page_label (dest);
251 argv[arg++] = g_strdup_printf ("--page-label=%s", page_label);
253 argv[arg++] = g_strdup_printf ("--page-label=%d",
254 ev_link_dest_get_page (dest));
259 argv[arg++] = g_strdup_printf ("--find=%s", search_string);
264 case EV_WINDOW_MODE_FULLSCREEN:
265 argv[arg++] = g_strdup ("-f");
267 case EV_WINDOW_MODE_PRESENTATION:
268 argv[arg++] = g_strdup ("-s");
274 argv[arg++] = (gchar *)uri;
277 res = gdk_spawn_on_screen (screen, NULL /* wd */, argv, NULL /* env */,
280 GINT_TO_POINTER(timestamp),
283 g_warning ("Error launching evince %s: %s\n", uri, error->message);
284 g_error_free (error);
287 for (i = 0; i < arg - 1; i++) {
293 ev_application_get_windows (EvApplication *application)
295 GList *l, *toplevels;
296 GList *windows = NULL;
298 toplevels = gtk_window_list_toplevels ();
300 for (l = toplevels; l != NULL; l = l->next) {
301 if (EV_IS_WINDOW (l->data)) {
302 windows = g_list_append (windows, l->data);
306 g_list_free (toplevels);
312 ev_application_get_empty_window (EvApplication *application,
315 EvWindow *empty_window = NULL;
316 GList *windows = ev_application_get_windows (application);
319 for (l = windows; l != NULL; l = l->next) {
320 EvWindow *window = EV_WINDOW (l->data);
322 if (ev_window_is_empty (window) &&
323 gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
324 empty_window = window;
329 g_list_free (windows);
337 * ev_application_register_uri:
346 * Registers @uri with evince-daemon.
348 * Returns: %TRUE to continue by opening @uri in this instance,
349 * or %FALSE if the request was forwarded to an existing evince
353 ev_application_register_uri (EvApplication *application,
357 EvWindowRunMode mode,
358 const gchar *search_string,
361 GVariant *value, *value2;
363 GVariantBuilder builder;
364 GError *error = NULL;
366 if (!application->connection)
369 /* FIXME: Don't make sync dbus calls, they block the UI! */
370 value = g_dbus_connection_call_sync
371 (application->connection,
372 "org.gnome.evince.Daemon",
373 "/org/gnome/evince/Daemon",
374 "org.gnome.evince.Daemon",
376 g_variant_new ("(s)", uri),
377 G_VARIANT_TYPE ("(s)"),
378 G_DBUS_CALL_FLAGS_NONE,
383 g_warning ("Error registering document: %s\n", error->message);
384 g_error_free (error);
388 g_variant_get (value, "(&s)", &owner);
390 /* This means that the document wasn't already registered; go
391 * ahead with opening it.
393 if (owner[0] == '\0') {
394 g_variant_unref (value);
398 /* Already registered */
399 g_variant_builder_init (&builder, G_VARIANT_TYPE ("(sa{sv}u)"));
400 g_variant_builder_add (&builder, "s", uri);
402 g_variant_builder_open (&builder, G_VARIANT_TYPE ("a{sv}"));
403 g_variant_builder_add (&builder, "{sv}",
405 g_variant_new_string (gdk_display_get_name (gdk_screen_get_display (screen))));
406 g_variant_builder_add (&builder, "{sv}",
408 g_variant_new_int32 (gdk_screen_get_number (screen)));
410 g_variant_builder_add (&builder, "{sv}",
412 g_variant_new_string (ev_link_dest_get_page_label (dest)));
415 g_variant_builder_add (&builder, "{sv}",
417 g_variant_new_string (search_string));
419 if (mode != EV_WINDOW_MODE_NORMAL) {
420 g_variant_builder_add (&builder, "{sv}",
422 g_variant_new_uint32 (mode));
424 g_variant_builder_close (&builder);
426 g_variant_builder_add (&builder, "u", timestamp);
428 value2 = g_dbus_connection_call_sync
429 (application->connection,
431 APPLICATION_DBUS_OBJECT_PATH,
432 APPLICATION_DBUS_INTERFACE,
434 g_variant_builder_end (&builder),
436 G_DBUS_CALL_FLAGS_NONE,
440 if (value2 == NULL) {
441 g_warning ("Failed to OpenURI: %s", error->message);
442 g_error_free (error);
446 g_variant_unref (value);
447 g_variant_unref (value2);
449 /* Do not continue opening this document */
454 ev_application_unregister_uri (EvApplication *application,
458 GError *error = NULL;
460 if (!application->connection)
463 /* FIXME: Don't make sync dbus calls, they block the UI! */
464 value = g_dbus_connection_call_sync
465 (application->connection,
466 "org.gnome.evince.Daemon",
467 "/org/gnome/evince/Daemon",
468 "org.gnome.evince.Daemon",
469 "UnregisterDocument",
470 g_variant_new ("(s)", uri),
472 G_DBUS_CALL_FLAGS_NO_AUTO_START,
477 g_warning ("Error unregistering document: %s\n", error->message);
478 g_error_free (error);
480 g_variant_unref (value);
483 #endif /* ENABLE_DBUS */
486 ev_application_open_uri_in_window (EvApplication *application,
491 EvWindowRunMode mode,
492 const gchar *search_string,
495 #ifdef GDK_WINDOWING_X11
496 GdkWindow *gdk_window;
500 ev_stock_icons_set_screen (screen);
501 gtk_window_set_screen (GTK_WINDOW (ev_window), screen);
504 /* We need to load uri before showing the window, so
505 we can restore window size without flickering */
506 ev_window_open_uri (ev_window, uri, dest, mode, search_string);
508 if (!gtk_widget_get_realized (GTK_WIDGET (ev_window)))
509 gtk_widget_realize (GTK_WIDGET (ev_window));
511 #ifdef GDK_WINDOWING_X11
512 gdk_window = gtk_widget_get_window (GTK_WIDGET (ev_window));
515 timestamp = gdk_x11_get_server_time (gdk_window);
516 gdk_x11_window_set_user_time (gdk_window, timestamp);
518 gtk_window_present (GTK_WINDOW (ev_window));
520 gtk_window_present_with_time (GTK_WINDOW (ev_window), timestamp);
521 #endif /* GDK_WINDOWING_X11 */
525 * ev_application_open_uri_at_dest:
526 * @application: The instance of the application.
527 * @uri: The uri to be opened.
528 * @screen: Thee screen where the link will be shown.
529 * @dest: The #EvLinkDest of the document.
530 * @mode: The run mode of the window.
531 * @timestamp: Current time value.
534 ev_application_open_uri_at_dest (EvApplication *application,
538 EvWindowRunMode mode,
539 const gchar *search_string,
544 g_return_if_fail (uri != NULL);
546 if (application->uri && strcmp (application->uri, uri) != 0) {
547 /* spawn a new evince process */
548 ev_spawn (uri, screen, dest, mode, search_string, timestamp);
554 /* Register the uri or send OpenURI to
555 * remote instance if already registered
557 ret = ev_application_register_uri (application, uri, screen, dest, mode, search_string, timestamp);
560 #endif /* ENABLE_DBUS */
562 ev_window = ev_application_get_empty_window (application, screen);
564 ev_window = EV_WINDOW (ev_window_new ());
567 application->uri = g_strdup (uri);
569 ev_application_open_uri_in_window (application, uri, ev_window,
576 * ev_application_open_window:
577 * @application: The instance of the application.
578 * @timestamp: Current time value.
580 * Creates a new window
583 ev_application_open_window (EvApplication *application,
587 GtkWidget *new_window = ev_window_new ();
588 #ifdef GDK_WINDOWING_X11
589 GdkWindow *gdk_window;
593 ev_stock_icons_set_screen (screen);
594 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
597 if (!gtk_widget_get_realized (new_window))
598 gtk_widget_realize (new_window);
600 #ifdef GDK_WINDOWING_X11
601 gdk_window = gtk_widget_get_window (GTK_WIDGET (new_window));
604 timestamp = gdk_x11_get_server_time (gdk_window);
605 gdk_x11_window_set_user_time (gdk_window, timestamp);
607 gtk_window_present (GTK_WINDOW (new_window));
609 gtk_window_present_with_time (GTK_WINDOW (new_window), timestamp);
610 #endif /* GDK_WINDOWING_X11 */
614 method_call_cb (GDBusConnection *connection,
616 const gchar *object_path,
617 const gchar *interface_name,
618 const gchar *method_name,
619 GVariant *parameters,
620 GDBusMethodInvocation *invocation,
623 EvApplication *application = EV_APPLICATION (user_data);
630 GdkDisplay *display = NULL;
631 int screen_number = 0;
632 EvLinkDest *dest = NULL;
633 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
634 const gchar *search_string = NULL;
635 GdkScreen *screen = NULL;
637 if (g_strcmp0 (method_name, "OpenURI") != 0)
640 g_variant_get (parameters, "(&sa{sv}u)", &uri, &iter, ×tamp);
642 /* FIXME: we don't need uri anymore,
643 * maybe this method should be renamed
644 * as reload, refresh or something like that
646 if (g_strcmp0 (application->uri, uri) != 0) {
647 g_dbus_method_invocation_return_error (invocation,
649 G_DBUS_ERROR_INVALID_ARGS,
650 "Unexpected URI \"%s\"",
652 g_variant_iter_free (iter);
656 while (g_variant_iter_loop (iter, "{&sv}", &key, &value)) {
657 if (strcmp (key, "display") == 0 && g_variant_classify (value) == G_VARIANT_CLASS_STRING) {
658 display = ev_display_open_if_needed (g_variant_get_string (value, NULL));
659 } else if (strcmp (key, "screen") == 0 && g_variant_classify (value) == G_VARIANT_CLASS_STRING) {
660 screen_number = g_variant_get_int32 (value);
661 } else if (strcmp (key, "mode") == 0 && g_variant_classify (value) == G_VARIANT_CLASS_UINT32) {
662 mode = g_variant_get_uint32 (value);
663 } else if (strcmp (key, "page-label") == 0 && g_variant_classify (value) == G_VARIANT_CLASS_STRING) {
664 dest = ev_link_dest_new_page_label (g_variant_get_string (value, NULL));
665 } else if (strcmp (key, "find-string") == 0 && g_variant_classify (value) == G_VARIANT_CLASS_STRING) {
666 search_string = g_variant_get_string (value, NULL);
669 g_variant_iter_free (iter);
671 if (display != NULL &&
672 screen_number >= 0 &&
673 screen_number < gdk_display_get_n_screens (display))
674 screen = gdk_display_get_screen (display, screen_number);
676 screen = gdk_screen_get_default ();
678 windows = ev_application_get_windows (application);
679 for (l = windows; l != NULL; l = g_list_next (l)) {
680 EvWindow *ev_window = EV_WINDOW (l->data);
682 ev_application_open_uri_in_window (application, uri, ev_window,
687 g_list_free (windows);
690 g_object_unref (dest);
692 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
696 ev_application_open_uri_list (EvApplication *application,
703 for (l = uri_list; l != NULL; l = l->next) {
704 ev_application_open_uri_at_dest (application, (char *)l->data,
705 screen, NULL, 0, NULL,
711 ev_application_accel_map_save (EvApplication *application)
713 gchar *accel_map_file;
718 accel_map_file = g_build_filename (userdir, "accels",
721 accel_map_file = g_build_filename (g_get_home_dir (),
726 tmp_filename = g_strdup_printf ("%s.XXXXXX", accel_map_file);
728 fd = g_mkstemp (tmp_filename);
730 g_free (accel_map_file);
731 g_free (tmp_filename);
735 gtk_accel_map_save_fd (fd);
738 if (g_rename (tmp_filename, accel_map_file) == -1) {
740 g_unlink (tmp_filename);
743 g_free (accel_map_file);
744 g_free (tmp_filename);
748 ev_application_accel_map_load (EvApplication *application)
750 gchar *accel_map_file;
753 accel_map_file = g_build_filename (userdir, "accels",
756 accel_map_file = g_build_filename (g_get_home_dir (),
761 gtk_accel_map_load (accel_map_file);
762 g_free (accel_map_file);
766 ev_application_shutdown (EvApplication *application)
768 if (application->uri) {
770 ev_application_unregister_uri (application,
773 g_free (application->uri);
774 application->uri = NULL;
777 ev_application_accel_map_save (application);
779 g_object_unref (application->scr_saver);
780 application->scr_saver = NULL;
783 if (application->keys) {
784 g_object_unref (application->keys);
785 application->keys = NULL;
787 if (application->registration_id != 0) {
788 g_dbus_connection_unregister_object (application->connection,
789 application->registration_id);
790 application->registration_id = 0;
792 if (application->connection != NULL) {
793 g_object_unref (application->connection);
794 application->connection = NULL;
796 #endif /* ENABLE_DBUS */
798 g_free (application->dot_dir);
799 application->dot_dir = NULL;
800 g_free (application->data_dir);
801 application->data_dir = NULL;
802 g_free (application->filechooser_open_uri);
803 application->filechooser_open_uri = NULL;
804 g_free (application->filechooser_save_uri);
805 application->filechooser_save_uri = NULL;
807 g_object_unref (application);
814 ev_application_class_init (EvApplicationClass *ev_application_class)
819 ev_application_init (EvApplication *ev_application)
821 GError *error = NULL;
823 userdir = g_getenv ("GNOME22_USER_DIR");
825 ev_application->dot_dir = g_build_filename (userdir, "evince", NULL);
827 ev_application->dot_dir = g_build_filename (g_get_home_dir (),
836 dir = g_win32_get_package_installation_directory_of_module (NULL);
837 ev_application->data_dir = g_build_filename (dir, "share", "evince", NULL);
841 ev_application->data_dir = g_strdup (DATADIR);
844 ev_application_init_session (ev_application);
846 ev_application_accel_map_load (ev_application);
850 static const char introspection_xml[] =
852 "<interface name='org.gnome.evince.Daemon'>"
853 "<method name='OpenURI'>"
854 "<arg type='s' name='uri' direction='in'/>"
855 "<arg type='a{sv}' name='args' direction='in'/>"
856 "<arg type='u' name='timestamp' direction='in'/>"
861 static const GDBusInterfaceVTable interface_vtable = {
867 GDBusNodeInfo *introspection_data;
869 ev_application->connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
870 if (ev_application->connection != NULL) {
871 introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
872 g_assert (introspection_data != NULL);
875 ev_application->registration_id =
876 g_dbus_connection_register_object (ev_application->connection,
877 APPLICATION_DBUS_OBJECT_PATH,
878 introspection_data->interfaces[0],
880 ev_application, NULL,
882 if (ev_application->registration_id == 0) {
883 g_printerr ("Failed to register bus object: %s\n", error->message);
884 g_error_free (error);
888 g_printerr ("Failed to get bus connection: %s\n", error->message);
889 g_error_free (error);
892 ev_application->keys = ev_media_player_keys_new ();
894 ev_application->scr_saver = totem_scrsaver_new ();
897 ev_application->scr_saver = totem_scrsaver_new ();
898 #endif /* ENABLE_DBUS */
902 ev_application_has_window (EvApplication *application)
904 GList *windows = ev_application_get_windows (application);
905 gboolean retval = windows != NULL;
907 g_list_free (windows);
913 ev_application_get_uri (EvApplication *application)
915 return application->uri;
919 * ev_application_get_media_keys:
920 * @application: The instance of the application.
922 * It gives you access to the media player keys handler object.
924 * Returns: A #EvMediaPlayerKeys.
927 ev_application_get_media_keys (EvApplication *application)
930 return G_OBJECT (application->keys);
933 #endif /* ENABLE_DBUS */
937 ev_application_set_filechooser_uri (EvApplication *application,
938 GtkFileChooserAction action,
941 if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
942 g_free (application->filechooser_open_uri);
943 application->filechooser_open_uri = g_strdup (uri);
944 } else if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
945 g_free (application->filechooser_save_uri);
946 application->filechooser_save_uri = g_strdup (uri);
951 ev_application_get_filechooser_uri (EvApplication *application,
952 GtkFileChooserAction action)
954 if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
955 if (application->filechooser_open_uri)
956 return application->filechooser_open_uri;
957 } else if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
958 if (application->filechooser_save_uri)
959 return application->filechooser_save_uri;
966 ev_application_screensaver_enable (EvApplication *application)
968 totem_scrsaver_enable (application->scr_saver);
972 ev_application_screensaver_disable (EvApplication *application)
974 totem_scrsaver_disable (application->scr_saver);
978 ev_application_get_dot_dir (EvApplication *application,
982 g_mkdir_with_parents (application->dot_dir, 0700);
984 return application->dot_dir;
988 ev_application_get_data_dir (EvApplication *application)
990 return application->data_dir;