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_DBUS_CALL_FLAGS_NONE,
382 g_warning ("Error registering document: %s\n", error->message);
383 g_error_free (error);
387 g_variant_get (value, "(&s)", &owner);
389 /* This means that the document wasn't already registered; go
390 * ahead with opening it.
392 if (owner[0] == '\0') {
393 g_variant_unref (value);
397 /* Already registered */
398 g_variant_builder_init (&builder, G_VARIANT_TYPE ("(sa{sv}u)"));
399 g_variant_builder_add (&builder, "s", uri);
401 g_variant_builder_open (&builder, G_VARIANT_TYPE ("a{sv}"));
402 g_variant_builder_add (&builder, "{sv}",
404 g_variant_new_string (gdk_display_get_name (gdk_screen_get_display (screen))));
405 g_variant_builder_add (&builder, "{sv}",
407 g_variant_new_int32 (gdk_screen_get_number (screen)));
409 g_variant_builder_add (&builder, "{sv}",
411 g_variant_new_string (ev_link_dest_get_page_label (dest)));
414 g_variant_builder_add (&builder, "{sv}",
416 g_variant_new_string (search_string));
418 if (mode != EV_WINDOW_MODE_NORMAL) {
419 g_variant_builder_add (&builder, "{sv}",
421 g_variant_new_uint32 (mode));
423 g_variant_builder_close (&builder);
425 g_variant_builder_add (&builder, "u", timestamp);
427 value2 = g_dbus_connection_call_sync
428 (application->connection,
430 APPLICATION_DBUS_OBJECT_PATH,
431 APPLICATION_DBUS_INTERFACE,
433 g_variant_builder_end (&builder),
434 G_DBUS_CALL_FLAGS_NONE,
438 if (value2 == NULL) {
439 g_warning ("Failed to OpenURI: %s", error->message);
440 g_error_free (error);
444 g_variant_unref (value);
445 g_variant_unref (value2);
447 /* Do not continue opening this document */
452 ev_application_unregister_uri (EvApplication *application,
456 GError *error = NULL;
458 if (!application->connection)
461 /* FIXME: Don't make sync dbus calls, they block the UI! */
462 value = g_dbus_connection_call_sync
463 (application->connection,
464 "org.gnome.evince.Daemon",
465 "/org/gnome/evince/Daemon",
466 "org.gnome.evince.Daemon",
467 "UnregisterDocument",
468 g_variant_new ("(s)", uri),
469 G_DBUS_CALL_FLAGS_NO_AUTO_START,
474 g_warning ("Error unregistering document: %s\n", error->message);
475 g_error_free (error);
477 g_variant_unref (value);
480 #endif /* ENABLE_DBUS */
483 ev_application_open_uri_in_window (EvApplication *application,
488 EvWindowRunMode mode,
489 const gchar *search_string,
492 #ifdef GDK_WINDOWING_X11
493 GdkWindow *gdk_window;
497 ev_stock_icons_set_screen (screen);
498 gtk_window_set_screen (GTK_WINDOW (ev_window), screen);
501 /* We need to load uri before showing the window, so
502 we can restore window size without flickering */
503 ev_window_open_uri (ev_window, uri, dest, mode, search_string);
505 if (!gtk_widget_get_realized (GTK_WIDGET (ev_window)))
506 gtk_widget_realize (GTK_WIDGET (ev_window));
508 #ifdef GDK_WINDOWING_X11
509 gdk_window = gtk_widget_get_window (GTK_WIDGET (ev_window));
512 timestamp = gdk_x11_get_server_time (gdk_window);
513 gdk_x11_window_set_user_time (gdk_window, timestamp);
515 gtk_window_present (GTK_WINDOW (ev_window));
517 gtk_window_present_with_time (GTK_WINDOW (ev_window), timestamp);
518 #endif /* GDK_WINDOWING_X11 */
522 * ev_application_open_uri_at_dest:
523 * @application: The instance of the application.
524 * @uri: The uri to be opened.
525 * @screen: Thee screen where the link will be shown.
526 * @dest: The #EvLinkDest of the document.
527 * @mode: The run mode of the window.
528 * @timestamp: Current time value.
531 ev_application_open_uri_at_dest (EvApplication *application,
535 EvWindowRunMode mode,
536 const gchar *search_string,
541 g_return_if_fail (uri != NULL);
543 if (application->uri && strcmp (application->uri, uri) != 0) {
544 /* spawn a new evince process */
545 ev_spawn (uri, screen, dest, mode, search_string, timestamp);
551 /* Register the uri or send OpenURI to
552 * remote instance if already registered
554 ret = ev_application_register_uri (application, uri, screen, dest, mode, search_string, timestamp);
557 #endif /* ENABLE_DBUS */
559 ev_window = ev_application_get_empty_window (application, screen);
561 ev_window = EV_WINDOW (ev_window_new ());
564 application->uri = g_strdup (uri);
566 ev_application_open_uri_in_window (application, uri, ev_window,
573 * ev_application_open_window:
574 * @application: The instance of the application.
575 * @timestamp: Current time value.
577 * Creates a new window
580 ev_application_open_window (EvApplication *application,
584 GtkWidget *new_window = ev_window_new ();
585 #ifdef GDK_WINDOWING_X11
586 GdkWindow *gdk_window;
590 ev_stock_icons_set_screen (screen);
591 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
594 if (!gtk_widget_get_realized (new_window))
595 gtk_widget_realize (new_window);
597 #ifdef GDK_WINDOWING_X11
598 gdk_window = gtk_widget_get_window (GTK_WIDGET (new_window));
601 timestamp = gdk_x11_get_server_time (gdk_window);
602 gdk_x11_window_set_user_time (gdk_window, timestamp);
604 gtk_window_present (GTK_WINDOW (new_window));
606 gtk_window_present_with_time (GTK_WINDOW (new_window), timestamp);
607 #endif /* GDK_WINDOWING_X11 */
611 method_call_cb (GDBusConnection *connection,
613 const gchar *object_path,
614 const gchar *interface_name,
615 const gchar *method_name,
616 GVariant *parameters,
617 GDBusMethodInvocation *invocation,
620 EvApplication *application = EV_APPLICATION (user_data);
627 GdkDisplay *display = NULL;
628 int screen_number = 0;
629 EvLinkDest *dest = NULL;
630 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
631 const gchar *search_string = NULL;
632 GdkScreen *screen = NULL;
634 if (g_strcmp0 (method_name, "OpenURI") != 0)
637 g_variant_get (parameters, "(&sa{sv}u)", &uri, &iter, ×tamp);
639 /* FIXME: we don't need uri anymore,
640 * maybe this method should be renamed
641 * as reload, refresh or something like that
643 if (g_strcmp0 (application->uri, uri) != 0) {
644 g_dbus_method_invocation_return_error (invocation,
646 G_DBUS_ERROR_INVALID_ARGS,
647 "Unexpected URI \"%s\"",
649 g_variant_iter_free (iter);
653 while (g_variant_iter_loop (iter, "{&sv}", &key, &value)) {
654 if (strcmp (key, "display") == 0 && g_variant_classify (value) == G_VARIANT_CLASS_STRING) {
655 display = ev_display_open_if_needed (g_variant_get_string (value, NULL));
656 } else if (strcmp (key, "screen") == 0 && g_variant_classify (value) == G_VARIANT_CLASS_STRING) {
657 screen_number = g_variant_get_int32 (value);
658 } else if (strcmp (key, "mode") == 0 && g_variant_classify (value) == G_VARIANT_CLASS_UINT32) {
659 mode = g_variant_get_uint32 (value);
660 } else if (strcmp (key, "page-label") == 0 && g_variant_classify (value) == G_VARIANT_CLASS_STRING) {
661 dest = ev_link_dest_new_page_label (g_variant_get_string (value, NULL));
662 } else if (strcmp (key, "find-string") == 0 && g_variant_classify (value) == G_VARIANT_CLASS_STRING) {
663 search_string = g_variant_get_string (value, NULL);
666 g_variant_iter_free (iter);
668 if (display != NULL &&
669 screen_number >= 0 &&
670 screen_number < gdk_display_get_n_screens (display))
671 screen = gdk_display_get_screen (display, screen_number);
673 screen = gdk_screen_get_default ();
675 windows = ev_application_get_windows (application);
676 for (l = windows; l != NULL; l = g_list_next (l)) {
677 EvWindow *ev_window = EV_WINDOW (l->data);
679 ev_application_open_uri_in_window (application, uri, ev_window,
684 g_list_free (windows);
687 g_object_unref (dest);
689 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
693 ev_application_open_uri_list (EvApplication *application,
700 for (l = uri_list; l != NULL; l = l->next) {
701 ev_application_open_uri_at_dest (application, (char *)l->data,
702 screen, NULL, 0, NULL,
708 ev_application_accel_map_save (EvApplication *application)
710 gchar *accel_map_file;
715 accel_map_file = g_build_filename (userdir, "accels",
718 accel_map_file = g_build_filename (g_get_home_dir (),
723 tmp_filename = g_strdup_printf ("%s.XXXXXX", accel_map_file);
725 fd = g_mkstemp (tmp_filename);
727 g_free (accel_map_file);
728 g_free (tmp_filename);
732 gtk_accel_map_save_fd (fd);
735 if (g_rename (tmp_filename, accel_map_file) == -1) {
737 g_unlink (tmp_filename);
740 g_free (accel_map_file);
741 g_free (tmp_filename);
745 ev_application_accel_map_load (EvApplication *application)
747 gchar *accel_map_file;
750 accel_map_file = g_build_filename (userdir, "accels",
753 accel_map_file = g_build_filename (g_get_home_dir (),
758 gtk_accel_map_load (accel_map_file);
759 g_free (accel_map_file);
763 ev_application_shutdown (EvApplication *application)
765 if (application->uri) {
767 ev_application_unregister_uri (application,
770 g_free (application->uri);
771 application->uri = NULL;
774 ev_application_accel_map_save (application);
776 g_object_unref (application->scr_saver);
777 application->scr_saver = NULL;
780 if (application->keys) {
781 g_object_unref (application->keys);
782 application->keys = NULL;
784 if (application->registration_id != 0) {
785 g_dbus_connection_unregister_object (application->connection,
786 application->registration_id);
787 application->registration_id = 0;
789 if (application->connection != NULL) {
790 g_object_unref (application->connection);
791 application->connection = NULL;
793 #endif /* ENABLE_DBUS */
795 g_free (application->dot_dir);
796 application->dot_dir = NULL;
797 g_free (application->data_dir);
798 application->data_dir = NULL;
799 g_free (application->filechooser_open_uri);
800 application->filechooser_open_uri = NULL;
801 g_free (application->filechooser_save_uri);
802 application->filechooser_save_uri = NULL;
804 g_object_unref (application);
811 ev_application_class_init (EvApplicationClass *ev_application_class)
816 ev_application_init (EvApplication *ev_application)
818 GError *error = NULL;
820 userdir = g_getenv ("GNOME22_USER_DIR");
822 ev_application->dot_dir = g_build_filename (userdir, "evince", NULL);
824 ev_application->dot_dir = g_build_filename (g_get_home_dir (),
833 dir = g_win32_get_package_installation_directory_of_module (NULL);
834 ev_application->data_dir = g_build_filename (dir, "share", "evince", NULL);
838 ev_application->data_dir = g_strdup (DATADIR);
841 ev_application_init_session (ev_application);
843 ev_application_accel_map_load (ev_application);
847 static const char introspection_xml[] =
849 "<interface name='org.gnome.evince.Daemon'>"
850 "<method name='OpenURI'>"
851 "<arg type='s' name='uri' direction='in'/>"
852 "<arg type='a{sv}' name='args' direction='in'/>"
853 "<arg type='u' name='timestamp' direction='in'/>"
858 static const GDBusInterfaceVTable interface_vtable = {
864 GDBusNodeInfo *introspection_data;
866 ev_application->connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
867 if (ev_application->connection != NULL) {
868 introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
869 g_assert (introspection_data != NULL);
872 ev_application->registration_id =
873 g_dbus_connection_register_object (ev_application->connection,
874 APPLICATION_DBUS_OBJECT_PATH,
875 introspection_data->interfaces[0],
877 ev_application, NULL,
879 if (ev_application->registration_id == 0) {
880 g_printerr ("Failed to register bus object: %s\n", error->message);
881 g_error_free (error);
885 g_printerr ("Failed to get bus connection: %s\n", error->message);
886 g_error_free (error);
889 ev_application->keys = ev_media_player_keys_new (ev_application->connection);
891 ev_application->scr_saver = totem_scrsaver_new (ev_application->connection);
894 ev_application->scr_saver = totem_scrsaver_new (NULL);
895 #endif /* ENABLE_DBUS */
899 ev_application_has_window (EvApplication *application)
901 GList *windows = ev_application_get_windows (application);
902 gboolean retval = windows != NULL;
904 g_list_free (windows);
910 ev_application_get_uri (EvApplication *application)
912 return application->uri;
916 * ev_application_get_media_keys:
917 * @application: The instance of the application.
919 * It gives you access to the media player keys handler object.
921 * Returns: A #EvMediaPlayerKeys.
924 ev_application_get_media_keys (EvApplication *application)
927 return G_OBJECT (application->keys);
930 #endif /* ENABLE_DBUS */
934 ev_application_set_filechooser_uri (EvApplication *application,
935 GtkFileChooserAction action,
938 if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
939 g_free (application->filechooser_open_uri);
940 application->filechooser_open_uri = g_strdup (uri);
941 } else if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
942 g_free (application->filechooser_save_uri);
943 application->filechooser_save_uri = g_strdup (uri);
948 ev_application_get_filechooser_uri (EvApplication *application,
949 GtkFileChooserAction action)
951 if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
952 if (application->filechooser_open_uri)
953 return application->filechooser_open_uri;
954 } else if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
955 if (application->filechooser_save_uri)
956 return application->filechooser_save_uri;
963 ev_application_screensaver_enable (EvApplication *application)
965 totem_scrsaver_enable (application->scr_saver);
969 ev_application_screensaver_disable (EvApplication *application)
971 totem_scrsaver_disable (application->scr_saver);
975 ev_application_get_dot_dir (EvApplication *application,
979 g_mkdir_with_parents (application->dot_dir, 0700);
981 return application->dot_dir;
985 ev_application_get_data_dir (EvApplication *application)
987 return application->data_dir;