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 */
52 struct _EvApplication {
53 GObject base_instance;
61 GDBusConnection *connection;
62 guint registration_id;
63 EvMediaPlayerKeys *keys;
64 gboolean doc_registered;
67 TotemScrsaver *scr_saver;
70 EggSMClient *smclient;
73 gchar *filechooser_open_uri;
74 gchar *filechooser_save_uri;
77 struct _EvApplicationClass {
78 GObjectClass base_class;
81 static EvApplication *instance;
83 G_DEFINE_TYPE (EvApplication, ev_application, G_TYPE_OBJECT);
86 #define APPLICATION_DBUS_OBJECT_PATH "/org/gnome/evince/Evince"
87 #define APPLICATION_DBUS_INTERFACE "org.gnome.evince.Application"
89 #define EVINCE_DAEMON_SERVICE "org.gnome.evince.Daemon"
90 #define EVINCE_DAEMON_OBJECT_PATH "/org/gnome/evince/Daemon"
91 #define EVINCE_DAEMON_INTERFACE "org.gnome.evince.Daemon"
94 static const gchar *userdir = NULL;
96 static void _ev_application_open_uri_at_dest (EvApplication *application,
100 EvWindowRunMode mode,
101 const gchar *search_string,
103 static void ev_application_open_uri_in_window (EvApplication *application,
108 EvWindowRunMode mode,
109 const gchar *search_string,
113 * ev_application_get_instance:
115 * Checks for #EvApplication instance, if it doesn't exist it does create it.
117 * Returns: an instance of the #EvApplication data.
120 ev_application_get_instance (void)
123 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
131 ev_application_load_session (EvApplication *application)
133 GKeyFile *state_file;
137 if (egg_sm_client_is_resumed (application->smclient)) {
138 state_file = egg_sm_client_get_state_file (application->smclient);
142 #endif /* WITH_SMCLIENT */
145 uri = g_key_file_get_string (state_file, "Evince", "uri", NULL);
149 ev_application_open_uri_at_dest (application, uri,
150 gdk_screen_get_default (),
154 g_key_file_free (state_file);
162 smclient_save_state_cb (EggSMClient *client,
163 GKeyFile *state_file,
164 EvApplication *application)
166 if (!application->uri)
169 g_key_file_set_string (state_file, "Evince", "uri", application->uri);
173 smclient_quit_cb (EggSMClient *client,
174 EvApplication *application)
176 ev_application_shutdown (application);
178 #endif /* WITH_SMCLIENT */
181 ev_application_init_session (EvApplication *application)
184 application->smclient = egg_sm_client_get ();
185 g_signal_connect (application->smclient, "save_state",
186 G_CALLBACK (smclient_save_state_cb),
188 g_signal_connect (application->smclient, "quit",
189 G_CALLBACK (smclient_quit_cb),
195 * ev_display_open_if_needed:
196 * @name: the name of the display to be open if it's needed.
198 * Search among all the open displays if any of them have the same name as the
199 * passed name. If the display isn't found it tries the open it.
201 * Returns: a #GdkDisplay of the display with the passed name.
204 ev_display_open_if_needed (const gchar *name)
208 GdkDisplay *display = NULL;
210 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
212 for (l = displays; l != NULL; l = l->next) {
213 const gchar *display_name = gdk_display_get_name ((GdkDisplay *) l->data);
215 if (g_ascii_strcasecmp (display_name, name) == 0) {
221 g_slist_free (displays);
223 return display != NULL ? display : gdk_display_open (name);
227 child_setup (gpointer user_data)
231 startup_id = g_strdup_printf ("_TIME%lu",
232 (unsigned long)GPOINTER_TO_INT (user_data));
233 g_setenv ("DESKTOP_STARTUP_ID", startup_id, TRUE);
238 ev_spawn (const char *uri,
241 EvWindowRunMode mode,
242 const gchar *search_string,
249 GError *error = NULL;
255 dir = g_win32_get_package_installation_directory_of_module (NULL);
256 argv[arg++] = g_build_filename (dir, "bin", "evince", NULL);
260 argv[arg++] = g_build_filename (BINDIR, "evince", NULL);
265 const gchar *page_label;
267 page_label = ev_link_dest_get_page_label (dest);
269 argv[arg++] = g_strdup_printf ("--page-label=%s", page_label);
271 argv[arg++] = g_strdup_printf ("--page-label=%d",
272 ev_link_dest_get_page (dest));
277 argv[arg++] = g_strdup_printf ("--find=%s", search_string);
282 case EV_WINDOW_MODE_FULLSCREEN:
283 argv[arg++] = g_strdup ("-f");
285 case EV_WINDOW_MODE_PRESENTATION:
286 argv[arg++] = g_strdup ("-s");
292 argv[arg++] = (gchar *)uri;
295 res = gdk_spawn_on_screen (screen, NULL /* wd */, argv, NULL /* env */,
298 GINT_TO_POINTER(timestamp),
301 g_warning ("Error launching evince %s: %s\n", uri, error->message);
302 g_error_free (error);
305 for (i = 0; i < arg - 1; i++) {
311 ev_application_get_windows (EvApplication *application)
313 GList *l, *toplevels;
314 GList *windows = NULL;
316 toplevels = gtk_window_list_toplevels ();
318 for (l = toplevels; l != NULL; l = l->next) {
319 if (EV_IS_WINDOW (l->data)) {
320 windows = g_list_append (windows, l->data);
324 g_list_free (toplevels);
330 ev_application_get_empty_window (EvApplication *application,
333 EvWindow *empty_window = NULL;
334 GList *windows = ev_application_get_windows (application);
337 for (l = windows; l != NULL; l = l->next) {
338 EvWindow *window = EV_WINDOW (l->data);
340 if (ev_window_is_empty (window) &&
341 gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
342 empty_window = window;
347 g_list_free (windows);
358 EvWindowRunMode mode;
359 gchar *search_string;
364 ev_register_doc_data_free (EvRegisterDocData *data)
370 if (data->search_string)
371 g_free (data->search_string);
373 g_object_unref (data->dest);
379 on_reload_cb (GObject *source_object,
383 GDBusConnection *connection = G_DBUS_CONNECTION (source_object);
385 GError *error = NULL;
387 value = g_dbus_connection_call_finish (connection, res, &error);
389 g_warning ("Failed to Reload: %s", error->message);
390 g_error_free (error);
392 g_variant_unref (value);
394 ev_application_shutdown (EV_APP);
398 on_register_uri_cb (GObject *source_object,
402 GDBusConnection *connection = G_DBUS_CONNECTION (source_object);
403 EvRegisterDocData *data = (EvRegisterDocData *)user_data;
404 EvApplication *application = EV_APP;
407 GVariantBuilder builder;
408 GError *error = NULL;
410 value = g_dbus_connection_call_finish (connection, res, &error);
412 g_warning ("Error registering document: %s\n", error->message);
413 g_error_free (error);
415 _ev_application_open_uri_at_dest (application,
422 ev_register_doc_data_free (data);
427 g_variant_get (value, "(&s)", &owner);
429 /* This means that the document wasn't already registered; go
430 * ahead with opening it.
432 if (owner[0] == '\0') {
433 g_variant_unref (value);
435 application->doc_registered = TRUE;
437 _ev_application_open_uri_at_dest (application,
444 ev_register_doc_data_free (data);
449 /* Already registered */
450 g_variant_builder_init (&builder, G_VARIANT_TYPE ("(a{sv}u)"));
451 g_variant_builder_open (&builder, G_VARIANT_TYPE ("a{sv}"));
452 g_variant_builder_add (&builder, "{sv}",
454 g_variant_new_string (gdk_display_get_name (gdk_screen_get_display (data->screen))));
455 g_variant_builder_add (&builder, "{sv}",
457 g_variant_new_int32 (gdk_screen_get_number (data->screen)));
459 g_variant_builder_add (&builder, "{sv}",
461 g_variant_new_string (ev_link_dest_get_page_label (data->dest)));
463 if (data->search_string) {
464 g_variant_builder_add (&builder, "{sv}",
466 g_variant_new_string (data->search_string));
468 if (data->mode != EV_WINDOW_MODE_NORMAL) {
469 g_variant_builder_add (&builder, "{sv}",
471 g_variant_new_uint32 (data->mode));
473 g_variant_builder_close (&builder);
475 g_variant_builder_add (&builder, "u", data->timestamp);
477 g_dbus_connection_call (connection,
479 APPLICATION_DBUS_OBJECT_PATH,
480 APPLICATION_DBUS_INTERFACE,
482 g_variant_builder_end (&builder),
484 G_DBUS_CALL_FLAGS_NONE,
489 g_variant_unref (value);
490 ev_register_doc_data_free (data);
494 * ev_application_register_uri:
503 * Registers @uri with evince-daemon.
507 ev_application_register_uri (EvApplication *application,
511 EvWindowRunMode mode,
512 const gchar *search_string,
515 EvRegisterDocData *data;
517 if (!application->connection)
520 if (application->doc_registered) {
521 /* Already registered, reload */
524 windows = ev_application_get_windows (application);
525 for (l = windows; l != NULL; l = g_list_next (l)) {
526 EvWindow *ev_window = EV_WINDOW (l->data);
528 ev_application_open_uri_in_window (application, uri, ev_window,
533 g_list_free (windows);
538 data = g_new (EvRegisterDocData, 1);
539 data->uri = g_strdup (uri);
540 data->screen = screen;
541 data->dest = dest ? g_object_ref (dest) : NULL;
543 data->search_string = search_string ? g_strdup (search_string) : NULL;
544 data->timestamp = timestamp;
546 g_dbus_connection_call (application->connection,
547 EVINCE_DAEMON_SERVICE,
548 EVINCE_DAEMON_OBJECT_PATH,
549 EVINCE_DAEMON_INTERFACE,
551 g_variant_new ("(s)", uri),
552 G_VARIANT_TYPE ("(s)"),
553 G_DBUS_CALL_FLAGS_NONE,
561 ev_application_unregister_uri (EvApplication *application,
565 GError *error = NULL;
567 if (!application->doc_registered)
570 /* This is called from ev_application_shutdown(),
571 * so it's safe to use the sync api
573 value = g_dbus_connection_call_sync (
574 application->connection,
575 EVINCE_DAEMON_SERVICE,
576 EVINCE_DAEMON_OBJECT_PATH,
577 EVINCE_DAEMON_INTERFACE,
578 "UnregisterDocument",
579 g_variant_new ("(s)", uri),
581 G_DBUS_CALL_FLAGS_NO_AUTO_START,
586 g_warning ("Error unregistering document: %s\n", error->message);
587 g_error_free (error);
589 g_variant_unref (value);
592 #endif /* ENABLE_DBUS */
595 ev_application_open_uri_in_window (EvApplication *application,
600 EvWindowRunMode mode,
601 const gchar *search_string,
604 #ifdef GDK_WINDOWING_X11
605 GdkWindow *gdk_window;
609 ev_stock_icons_set_screen (screen);
610 gtk_window_set_screen (GTK_WINDOW (ev_window), screen);
613 /* We need to load uri before showing the window, so
614 we can restore window size without flickering */
615 ev_window_open_uri (ev_window, uri, dest, mode, search_string);
617 if (!gtk_widget_get_realized (GTK_WIDGET (ev_window)))
618 gtk_widget_realize (GTK_WIDGET (ev_window));
620 #ifdef GDK_WINDOWING_X11
621 gdk_window = gtk_widget_get_window (GTK_WIDGET (ev_window));
624 timestamp = gdk_x11_get_server_time (gdk_window);
625 gdk_x11_window_set_user_time (gdk_window, timestamp);
627 gtk_window_present (GTK_WINDOW (ev_window));
629 gtk_window_present_with_time (GTK_WINDOW (ev_window), timestamp);
630 #endif /* GDK_WINDOWING_X11 */
634 _ev_application_open_uri_at_dest (EvApplication *application,
638 EvWindowRunMode mode,
639 const gchar *search_string,
644 ev_window = ev_application_get_empty_window (application, screen);
646 ev_window = EV_WINDOW (ev_window_new ());
648 ev_application_open_uri_in_window (application, uri, ev_window,
655 * ev_application_open_uri_at_dest:
656 * @application: The instance of the application.
657 * @uri: The uri to be opened.
658 * @screen: Thee screen where the link will be shown.
659 * @dest: The #EvLinkDest of the document.
660 * @mode: The run mode of the window.
661 * @timestamp: Current time value.
664 ev_application_open_uri_at_dest (EvApplication *application,
668 EvWindowRunMode mode,
669 const gchar *search_string,
672 g_return_if_fail (uri != NULL);
674 if (application->uri && strcmp (application->uri, uri) != 0) {
675 /* spawn a new evince process */
676 ev_spawn (uri, screen, dest, mode, search_string, timestamp);
678 } else if (!application->uri) {
679 application->uri = g_strdup (uri);
683 /* Register the uri or send Reload to
684 * remote instance if already registered
686 ev_application_register_uri (application, uri, screen, dest, mode, search_string, timestamp);
688 _ev_application_open_uri_at_dest (application, uri, screen, dest, mode, search_string, timestamp);
689 #endif /* ENABLE_DBUS */
693 * ev_application_open_window:
694 * @application: The instance of the application.
695 * @timestamp: Current time value.
697 * Creates a new window
700 ev_application_open_window (EvApplication *application,
704 GtkWidget *new_window = ev_window_new ();
705 #ifdef GDK_WINDOWING_X11
706 GdkWindow *gdk_window;
710 ev_stock_icons_set_screen (screen);
711 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
714 if (!gtk_widget_get_realized (new_window))
715 gtk_widget_realize (new_window);
717 #ifdef GDK_WINDOWING_X11
718 gdk_window = gtk_widget_get_window (GTK_WIDGET (new_window));
721 timestamp = gdk_x11_get_server_time (gdk_window);
722 gdk_x11_window_set_user_time (gdk_window, timestamp);
724 gtk_window_present (GTK_WINDOW (new_window));
726 gtk_window_present_with_time (GTK_WINDOW (new_window), timestamp);
727 #endif /* GDK_WINDOWING_X11 */
732 method_call_cb (GDBusConnection *connection,
734 const gchar *object_path,
735 const gchar *interface_name,
736 const gchar *method_name,
737 GVariant *parameters,
738 GDBusMethodInvocation *invocation,
741 EvApplication *application = EV_APPLICATION (user_data);
747 GdkDisplay *display = NULL;
748 int screen_number = 0;
749 EvLinkDest *dest = NULL;
750 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
751 const gchar *search_string = NULL;
752 GdkScreen *screen = NULL;
754 if (g_strcmp0 (method_name, "Reload") != 0)
757 g_variant_get (parameters, "(a{sv}u)", &iter, ×tamp);
759 while (g_variant_iter_loop (iter, "{&sv}", &key, &value)) {
760 if (strcmp (key, "display") == 0 && g_variant_classify (value) == G_VARIANT_CLASS_STRING) {
761 display = ev_display_open_if_needed (g_variant_get_string (value, NULL));
762 } else if (strcmp (key, "screen") == 0 && g_variant_classify (value) == G_VARIANT_CLASS_STRING) {
763 screen_number = g_variant_get_int32 (value);
764 } else if (strcmp (key, "mode") == 0 && g_variant_classify (value) == G_VARIANT_CLASS_UINT32) {
765 mode = g_variant_get_uint32 (value);
766 } else if (strcmp (key, "page-label") == 0 && g_variant_classify (value) == G_VARIANT_CLASS_STRING) {
767 dest = ev_link_dest_new_page_label (g_variant_get_string (value, NULL));
768 } else if (strcmp (key, "find-string") == 0 && g_variant_classify (value) == G_VARIANT_CLASS_STRING) {
769 search_string = g_variant_get_string (value, NULL);
772 g_variant_iter_free (iter);
774 if (display != NULL &&
775 screen_number >= 0 &&
776 screen_number < gdk_display_get_n_screens (display))
777 screen = gdk_display_get_screen (display, screen_number);
779 screen = gdk_screen_get_default ();
781 windows = ev_application_get_windows (application);
782 for (l = windows; l != NULL; l = g_list_next (l)) {
783 EvWindow *ev_window = EV_WINDOW (l->data);
785 ev_application_open_uri_in_window (application, application->uri,
791 g_list_free (windows);
794 g_object_unref (dest);
796 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
799 static const char introspection_xml[] =
801 "<interface name='org.gnome.evince.Application'>"
802 "<method name='Reload'>"
803 "<arg type='a{sv}' name='args' direction='in'/>"
804 "<arg type='u' name='timestamp' direction='in'/>"
809 static const GDBusInterfaceVTable interface_vtable = {
815 static GDBusNodeInfo *introspection_data;
816 #endif /* ENABLE_DBUS */
819 ev_application_open_uri_list (EvApplication *application,
826 for (l = uri_list; l != NULL; l = l->next) {
827 ev_application_open_uri_at_dest (application, (char *)l->data,
828 screen, NULL, 0, NULL,
834 ev_application_accel_map_save (EvApplication *application)
836 gchar *accel_map_file;
841 accel_map_file = g_build_filename (userdir, "accels",
844 accel_map_file = g_build_filename (g_get_home_dir (),
849 tmp_filename = g_strdup_printf ("%s.XXXXXX", accel_map_file);
851 fd = g_mkstemp (tmp_filename);
853 g_free (accel_map_file);
854 g_free (tmp_filename);
858 gtk_accel_map_save_fd (fd);
861 if (g_rename (tmp_filename, accel_map_file) == -1) {
863 g_unlink (tmp_filename);
866 g_free (accel_map_file);
867 g_free (tmp_filename);
871 ev_application_accel_map_load (EvApplication *application)
873 gchar *accel_map_file;
876 accel_map_file = g_build_filename (userdir, "accels",
879 accel_map_file = g_build_filename (g_get_home_dir (),
884 gtk_accel_map_load (accel_map_file);
885 g_free (accel_map_file);
889 ev_application_shutdown (EvApplication *application)
891 if (application->uri) {
893 ev_application_unregister_uri (application,
896 g_free (application->uri);
897 application->uri = NULL;
900 ev_application_accel_map_save (application);
902 g_object_unref (application->scr_saver);
903 application->scr_saver = NULL;
906 if (application->keys) {
907 g_object_unref (application->keys);
908 application->keys = NULL;
910 if (application->registration_id != 0) {
911 g_dbus_connection_unregister_object (application->connection,
912 application->registration_id);
913 application->registration_id = 0;
915 if (application->connection != NULL) {
916 g_object_unref (application->connection);
917 application->connection = NULL;
919 if (introspection_data) {
920 g_dbus_node_info_ref (introspection_data);
921 introspection_data = NULL;
923 #endif /* ENABLE_DBUS */
925 g_free (application->dot_dir);
926 application->dot_dir = NULL;
927 g_free (application->data_dir);
928 application->data_dir = NULL;
929 g_free (application->filechooser_open_uri);
930 application->filechooser_open_uri = NULL;
931 g_free (application->filechooser_save_uri);
932 application->filechooser_save_uri = NULL;
934 g_object_unref (application);
941 ev_application_class_init (EvApplicationClass *ev_application_class)
946 ev_application_init (EvApplication *ev_application)
948 GError *error = NULL;
950 userdir = g_getenv ("GNOME22_USER_DIR");
952 ev_application->dot_dir = g_build_filename (userdir, "evince", NULL);
954 ev_application->dot_dir = g_build_filename (g_get_home_dir (),
963 dir = g_win32_get_package_installation_directory_of_module (NULL);
964 ev_application->data_dir = g_build_filename (dir, "share", "evince", NULL);
968 ev_application->data_dir = g_strdup (DATADIR);
971 ev_application_init_session (ev_application);
973 ev_application_accel_map_load (ev_application);
976 ev_application->connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
977 if (ev_application->connection != NULL) {
978 introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
979 g_assert (introspection_data != NULL);
981 ev_application->registration_id =
982 g_dbus_connection_register_object (ev_application->connection,
983 APPLICATION_DBUS_OBJECT_PATH,
984 introspection_data->interfaces[0],
986 ev_application, NULL,
988 if (ev_application->registration_id == 0) {
989 g_printerr ("Failed to register bus object: %s\n", error->message);
990 g_error_free (error);
993 g_printerr ("Failed to get bus connection: %s\n", error->message);
994 g_error_free (error);
997 ev_application->keys = ev_media_player_keys_new ();
998 #endif /* ENABLE_DBUS */
1000 ev_application->scr_saver = totem_scrsaver_new ();
1001 g_object_set (ev_application->scr_saver,
1002 "reason", _("Running in presentation mode"),
1007 ev_application_has_window (EvApplication *application)
1009 GList *windows = ev_application_get_windows (application);
1010 gboolean retval = windows != NULL;
1012 g_list_free (windows);
1018 ev_application_get_uri (EvApplication *application)
1020 return application->uri;
1024 * ev_application_get_media_keys:
1025 * @application: The instance of the application.
1027 * It gives you access to the media player keys handler object.
1029 * Returns: A #EvMediaPlayerKeys.
1032 ev_application_get_media_keys (EvApplication *application)
1035 return G_OBJECT (application->keys);
1038 #endif /* ENABLE_DBUS */
1042 ev_application_set_filechooser_uri (EvApplication *application,
1043 GtkFileChooserAction action,
1046 if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
1047 g_free (application->filechooser_open_uri);
1048 application->filechooser_open_uri = g_strdup (uri);
1049 } else if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
1050 g_free (application->filechooser_save_uri);
1051 application->filechooser_save_uri = g_strdup (uri);
1056 ev_application_get_filechooser_uri (EvApplication *application,
1057 GtkFileChooserAction action)
1059 if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
1060 if (application->filechooser_open_uri)
1061 return application->filechooser_open_uri;
1062 } else if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
1063 if (application->filechooser_save_uri)
1064 return application->filechooser_save_uri;
1071 ev_application_screensaver_enable (EvApplication *application)
1073 totem_scrsaver_enable (application->scr_saver);
1077 ev_application_screensaver_disable (EvApplication *application)
1079 totem_scrsaver_disable (application->scr_saver);
1083 ev_application_get_dot_dir (EvApplication *application,
1087 g_mkdir_with_parents (application->dot_dir, 0700);
1089 return application->dot_dir;
1093 ev_application_get_data_dir (EvApplication *application)
1095 return application->data_dir;