1 /* this file is part of evince, a gnome document viewer
3 * Copyright (C) 2004 Martin Kretzschmar
6 * Martin Kretzschmar <martink@gnome.org>
8 * Evince is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * Evince is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 #include <glib/gi18n.h>
30 #include <glib/gstdio.h>
32 #ifdef GDK_WINDOWING_X11
37 #include "totem-scrsaver.h"
40 #include "eggsmclient.h"
43 #include "ev-application.h"
44 #include "ev-file-helpers.h"
45 #include "ev-stock-icons.h"
48 #include "ev-media-player-keys.h"
49 #endif /* ENABLE_DBUS */
54 struct _EvApplication {
55 GObject base_instance;
63 GDBusConnection *connection;
64 guint registration_id;
65 EvMediaPlayerKeys *keys;
68 TotemScrsaver *scr_saver;
71 EggSMClient *smclient;
74 gchar *filechooser_open_uri;
75 gchar *filechooser_save_uri;
78 struct _EvApplicationClass {
79 GObjectClass base_class;
82 static EvApplication *instance;
84 G_DEFINE_TYPE (EvApplication, ev_application, G_TYPE_OBJECT);
87 #define APPLICATION_DBUS_OBJECT_PATH "/org/gnome/evince/Evince"
88 #define APPLICATION_DBUS_INTERFACE "org.gnome.evince.Application"
91 static const gchar *userdir = NULL;
94 * ev_application_get_instance:
96 * Checks for #EvApplication instance, if it doesn't exist it does create it.
98 * Returns: an instance of the #EvApplication data.
101 ev_application_get_instance (void)
104 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
112 ev_application_load_session (EvApplication *application)
114 GKeyFile *state_file;
118 if (egg_sm_client_is_resumed (application->smclient)) {
119 state_file = egg_sm_client_get_state_file (application->smclient);
123 #endif /* WITH_SMCLIENT */
126 uri = g_key_file_get_string (state_file, "Evince", "uri", NULL);
130 ev_application_open_uri_at_dest (application, uri,
131 gdk_screen_get_default (),
135 g_key_file_free (state_file);
143 smclient_save_state_cb (EggSMClient *client,
144 GKeyFile *state_file,
145 EvApplication *application)
147 if (!application->uri)
150 g_key_file_set_string (state_file, "Evince", "uri", application->uri);
154 smclient_quit_cb (EggSMClient *client,
155 EvApplication *application)
157 ev_application_shutdown (application);
159 #endif /* WITH_SMCLIENT */
162 ev_application_init_session (EvApplication *application)
165 application->smclient = egg_sm_client_get ();
166 g_signal_connect (application->smclient, "save_state",
167 G_CALLBACK (smclient_save_state_cb),
169 g_signal_connect (application->smclient, "quit",
170 G_CALLBACK (smclient_quit_cb),
176 * ev_display_open_if_needed:
177 * @name: the name of the display to be open if it's needed.
179 * Search among all the open displays if any of them have the same name as the
180 * passed name. If the display isn't found it tries the open it.
182 * Returns: a #GdkDisplay of the display with the passed name.
185 ev_display_open_if_needed (const gchar *name)
189 GdkDisplay *display = NULL;
191 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
193 for (l = displays; l != NULL; l = l->next) {
194 const gchar *display_name = gdk_display_get_name ((GdkDisplay *) l->data);
196 if (g_ascii_strcasecmp (display_name, name) == 0) {
202 g_slist_free (displays);
204 return display != NULL ? display : gdk_display_open (name);
208 child_setup (gpointer user_data)
212 startup_id = g_strdup_printf ("_TIME%lu",
213 (unsigned long)GPOINTER_TO_INT (user_data));
214 g_setenv ("DESKTOP_STARTUP_ID", startup_id, TRUE);
219 ev_spawn (const char *uri,
222 EvWindowRunMode mode,
223 const gchar *search_string,
230 GError *error = NULL;
236 dir = g_win32_get_package_installation_directory_of_module (NULL);
237 argv[arg++] = g_build_filename (dir, "bin", "evince", NULL);
241 argv[arg++] = g_build_filename (BINDIR, "evince", NULL);
246 const gchar *page_label;
248 page_label = ev_link_dest_get_page_label (dest);
250 argv[arg++] = g_strdup_printf ("--page-label=%s", page_label);
252 argv[arg++] = g_strdup_printf ("--page-label=%d",
253 ev_link_dest_get_page (dest));
258 argv[arg++] = g_strdup_printf ("--find=%s", search_string);
263 case EV_WINDOW_MODE_FULLSCREEN:
264 argv[arg++] = g_strdup ("-f");
266 case EV_WINDOW_MODE_PRESENTATION:
267 argv[arg++] = g_strdup ("-s");
273 argv[arg++] = (gchar *)uri;
276 res = gdk_spawn_on_screen (screen, NULL /* wd */, argv, NULL /* env */,
279 GINT_TO_POINTER(timestamp),
282 g_warning ("Error launching evince %s: %s\n", uri, error->message);
283 g_error_free (error);
286 for (i = 0; i < arg - 1; i++) {
292 ev_application_get_windows (EvApplication *application)
294 GList *l, *toplevels;
295 GList *windows = NULL;
297 toplevels = gtk_window_list_toplevels ();
299 for (l = toplevels; l != NULL; l = l->next) {
300 if (EV_IS_WINDOW (l->data)) {
301 windows = g_list_append (windows, l->data);
305 g_list_free (toplevels);
311 ev_application_get_empty_window (EvApplication *application,
314 EvWindow *empty_window = NULL;
315 GList *windows = ev_application_get_windows (application);
318 for (l = windows; l != NULL; l = l->next) {
319 EvWindow *window = EV_WINDOW (l->data);
321 if (ev_window_is_empty (window) &&
322 gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
323 empty_window = window;
328 g_list_free (windows);
336 ev_application_register_uri (EvApplication *application,
340 EvWindowRunMode mode,
341 const gchar *search_string,
346 GVariantBuilder builder;
347 GError *error = NULL;
349 if (!application->connection)
352 /* FIXME: Don't make sync dbus calls, they block the UI! */
353 value = g_dbus_connection_invoke_method_sync
354 (application->connection,
355 "org.gnome.evince.Daemon",
356 "/org/gnome/evince/Daemon",
357 "org.gnome.evince.Daemon",
359 g_variant_new ("(s)", uri),
360 G_DBUS_INVOKE_METHOD_FLAGS_NONE,
365 g_warning ("Error registering document: %s\n", error->message);
366 g_error_free (error);
370 g_variant_get (value, "(&s)", &owner);
372 if (owner[0] != ':') {
373 g_variant_unref (value);
377 /* Already registered */
378 g_variant_builder_init (&builder, G_VARIANT_TYPE ("(sa{sv}u)"));
379 g_variant_builder_add (&builder, "s", uri);
381 g_variant_builder_open (&builder, G_VARIANT_TYPE ("a{sv}"));
382 g_variant_builder_add (&builder, "{sv}",
384 g_variant_new_string (gdk_display_get_name (gdk_screen_get_display (screen))));
385 g_variant_builder_add (&builder, "{sv}",
387 g_variant_new_int32 (gdk_screen_get_number (screen)));
389 g_variant_builder_add (&builder, "{sv}",
391 g_variant_new_string (ev_link_dest_get_page_label (dest)));
394 g_variant_builder_add (&builder, "{sv}",
396 g_variant_new_string (search_string));
398 if (mode != EV_WINDOW_MODE_NORMAL) {
399 g_variant_builder_add (&builder, "{sv}",
401 g_variant_new_uint32 (mode));
403 g_variant_builder_close (&builder);
405 g_variant_builder_add (&builder, "u", timestamp);
407 value = g_dbus_connection_invoke_method_sync
408 (application->connection,
410 APPLICATION_DBUS_OBJECT_PATH,
411 APPLICATION_DBUS_INTERFACE,
413 g_variant_builder_end (&builder),
414 G_DBUS_INVOKE_METHOD_FLAGS_NONE,
419 g_warning ("%s", error->message);
420 g_error_free (error);
423 g_variant_unref (value);
425 /* Do not continue opening this document */
430 ev_application_unregister_uri (EvApplication *application,
434 GError *error = NULL;
436 if (!application->connection)
439 /* FIXME: Don't make sync dbus calls, they block the UI! */
440 value = g_dbus_connection_invoke_method_sync
441 (application->connection,
442 "org.gnome.evince.Daemon",
443 "/org/gnome/evince/Daemon",
444 "org.gnome.evince.Daemon",
445 "UnregisterDocument",
446 g_variant_new ("(s)", uri),
447 G_DBUS_INVOKE_METHOD_FLAGS_NONE,
452 g_warning ("Error unregistering document: %s\n", error->message);
453 g_error_free (error);
455 g_variant_unref (value);
458 #endif /* ENABLE_DBUS */
461 ev_application_open_uri_in_window (EvApplication *application,
466 EvWindowRunMode mode,
467 const gchar *search_string,
470 #ifdef GDK_WINDOWING_X11
471 GdkWindow *gdk_window;
475 ev_stock_icons_set_screen (screen);
476 gtk_window_set_screen (GTK_WINDOW (ev_window), screen);
479 /* We need to load uri before showing the window, so
480 we can restore window size without flickering */
481 ev_window_open_uri (ev_window, uri, dest, mode, search_string);
483 if (!gtk_widget_get_realized (GTK_WIDGET (ev_window)))
484 gtk_widget_realize (GTK_WIDGET (ev_window));
486 #ifdef GDK_WINDOWING_X11
487 gdk_window = gtk_widget_get_window (GTK_WIDGET (ev_window));
490 timestamp = gdk_x11_get_server_time (gdk_window);
491 gdk_x11_window_set_user_time (gdk_window, timestamp);
493 gtk_window_present (GTK_WINDOW (ev_window));
495 gtk_window_present_with_time (GTK_WINDOW (ev_window), timestamp);
496 #endif /* GDK_WINDOWING_X11 */
500 * ev_application_open_uri_at_dest:
501 * @application: The instance of the application.
502 * @uri: The uri to be opened.
503 * @screen: Thee screen where the link will be shown.
504 * @dest: The #EvLinkDest of the document.
505 * @mode: The run mode of the window.
506 * @timestamp: Current time value.
509 ev_application_open_uri_at_dest (EvApplication *application,
513 EvWindowRunMode mode,
514 const gchar *search_string,
519 g_return_if_fail (uri != NULL);
521 if (application->uri && strcmp (application->uri, uri) != 0) {
522 /* spawn a new evince process */
523 ev_spawn (uri, screen, dest, mode, search_string, timestamp);
529 /* Register the uri or send OpenURI to
530 * remote instance if already registered
532 ret = ev_application_register_uri (application, uri, screen, dest, mode, search_string, timestamp);
535 #endif /* ENABLE_DBUS */
537 ev_window = ev_application_get_empty_window (application, screen);
539 ev_window = EV_WINDOW (ev_window_new ());
542 application->uri = g_strdup (uri);
544 ev_application_open_uri_in_window (application, uri, ev_window,
551 * ev_application_open_window:
552 * @application: The instance of the application.
553 * @timestamp: Current time value.
555 * Creates a new window
558 ev_application_open_window (EvApplication *application,
562 GtkWidget *new_window = ev_window_new ();
563 #ifdef GDK_WINDOWING_X11
564 GdkWindow *gdk_window;
568 ev_stock_icons_set_screen (screen);
569 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
572 if (!gtk_widget_get_realized (new_window))
573 gtk_widget_realize (new_window);
575 #ifdef GDK_WINDOWING_X11
576 gdk_window = gtk_widget_get_window (GTK_WIDGET (new_window));
579 timestamp = gdk_x11_get_server_time (gdk_window);
580 gdk_x11_window_set_user_time (gdk_window, timestamp);
582 gtk_window_present (GTK_WINDOW (new_window));
584 gtk_window_present_with_time (GTK_WINDOW (new_window), timestamp);
585 #endif /* GDK_WINDOWING_X11 */
589 method_call_cb (GDBusConnection *connection,
591 const gchar *object_path,
592 const gchar *interface_name,
593 const gchar *method_name,
594 GVariant *parameters,
595 GDBusMethodInvocation *invocation,
598 EvApplication *application = EV_APPLICATION (user_data);
605 GdkDisplay *display = NULL;
606 int screen_number = 0;
607 EvLinkDest *dest = NULL;
608 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
609 const gchar *search_string = NULL;
610 GdkScreen *screen = NULL;
612 if (g_strcmp0 (method_name, "OpenURI") != 0)
615 g_variant_get (parameters, "(&sa{sv}u)", &uri, &iter, ×tamp);
617 /* FIXME: we don't need uri anymore,
618 * maybe this method should be renamed
619 * as reload, refresh or something like that
621 if (g_strcmp0 (application->uri, uri) != 0) {
622 g_dbus_method_invocation_return_error (invocation,
624 G_DBUS_ERROR_INVALID_ARGS,
625 "Unexpected URI \"%s\"",
627 g_variant_iter_free (iter);
631 while (g_variant_iter_loop (iter, "{sv}", &key, &value)) {
632 if (strcmp (key, "display") == 0 && g_variant_classify (value) == G_VARIANT_CLASS_STRING) {
633 display = ev_display_open_if_needed (g_variant_get_string (value, NULL));
634 } else if (strcmp (key, "screen") == 0 && g_variant_classify (value) == G_VARIANT_CLASS_STRING) {
635 screen_number = g_variant_get_int32 (value);
636 } else if (strcmp (key, "mode") == 0 && g_variant_classify (value) == G_VARIANT_CLASS_UINT32) {
637 mode = g_variant_get_uint32 (value);
638 } else if (strcmp (key, "page-label") == 0 && g_variant_classify (value) == G_VARIANT_CLASS_STRING) {
639 dest = ev_link_dest_new_page_label (g_variant_get_string (value, NULL));
640 } else if (strcmp (key, "find-string") == 0 && g_variant_classify (value) == G_VARIANT_CLASS_STRING) {
641 search_string = g_variant_get_string (value, NULL);
644 g_variant_iter_free (iter);
647 screen = gdk_display_get_screen (display, screen_number);
649 screen = gdk_screen_get_default ();
651 windows = ev_application_get_windows (application);
652 for (l = windows; l != NULL; l = g_list_next (l)) {
653 EvWindow *ev_window = EV_WINDOW (l->data);
655 ev_application_open_uri_in_window (application, uri, ev_window,
660 g_list_free (windows);
663 g_object_unref (dest);
665 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
669 ev_application_open_uri_list (EvApplication *application,
676 for (l = uri_list; l != NULL; l = l->next) {
677 ev_application_open_uri_at_dest (application, (char *)l->data,
678 screen, NULL, 0, NULL,
684 ev_application_accel_map_save (EvApplication *application)
686 gchar *accel_map_file;
691 accel_map_file = g_build_filename (userdir, "accels",
694 accel_map_file = g_build_filename (g_get_home_dir (),
699 tmp_filename = g_strdup_printf ("%s.XXXXXX", accel_map_file);
701 fd = g_mkstemp (tmp_filename);
703 g_free (accel_map_file);
704 g_free (tmp_filename);
708 gtk_accel_map_save_fd (fd);
711 if (g_rename (tmp_filename, accel_map_file) == -1) {
713 g_unlink (tmp_filename);
716 g_free (accel_map_file);
717 g_free (tmp_filename);
721 ev_application_accel_map_load (EvApplication *application)
723 gchar *accel_map_file;
726 accel_map_file = g_build_filename (userdir, "accels",
729 accel_map_file = g_build_filename (g_get_home_dir (),
734 gtk_accel_map_load (accel_map_file);
735 g_free (accel_map_file);
739 ev_application_shutdown (EvApplication *application)
741 if (application->uri) {
743 ev_application_unregister_uri (application,
746 g_free (application->uri);
747 application->uri = NULL;
750 ev_application_accel_map_save (application);
752 g_object_unref (application->scr_saver);
753 application->scr_saver = NULL;
756 if (application->keys) {
757 g_object_unref (application->keys);
758 application->keys = NULL;
760 if (application->registration_id != 0) {
761 g_dbus_connection_unregister_object (application->connection,
762 application->registration_id);
763 application->registration_id = 0;
765 if (application->connection != NULL) {
766 g_object_unref (application->connection);
767 application->connection = NULL;
769 #endif /* ENABLE_DBUS */
771 g_free (application->dot_dir);
772 application->dot_dir = NULL;
773 g_free (application->data_dir);
774 application->data_dir = NULL;
775 g_free (application->filechooser_open_uri);
776 application->filechooser_open_uri = NULL;
777 g_free (application->filechooser_save_uri);
778 application->filechooser_save_uri = NULL;
780 g_object_unref (application);
787 ev_application_class_init (EvApplicationClass *ev_application_class)
792 ev_application_init (EvApplication *ev_application)
794 GError *error = NULL;
796 userdir = g_getenv ("GNOME22_USER_DIR");
798 ev_application->dot_dir = g_build_filename (userdir, "evince", NULL);
800 ev_application->dot_dir = g_build_filename (g_get_home_dir (),
809 dir = g_win32_get_package_installation_directory_of_module (NULL);
810 ev_application->data_dir = g_build_filename (dir, "share", "evince", NULL);
814 ev_application->data_dir = g_strdup (DATADIR);
817 ev_application_init_session (ev_application);
819 ev_application_accel_map_load (ev_application);
821 ev_application->scr_saver = totem_scrsaver_new ();
825 static const char introspection_xml[] =
827 "<interface name='org.gnome.evince.Daemon'>"
828 "<method name='OpenURI'>"
829 "<arg type='s' name='uri' direction='in'/>"
830 "<arg type='a{sv}' name='args' direction='in'/>"
831 "<arg type='u' name='timestamp' direction='in'/>"
836 static const GDBusInterfaceVTable interface_vtable = {
842 GDBusNodeInfo *introspection_data;
844 ev_application->connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
845 if (ev_application->connection != NULL) {
846 introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
847 g_assert (introspection_data != NULL);
850 ev_application->registration_id =
851 g_dbus_connection_register_object (ev_application->connection,
852 APPLICATION_DBUS_OBJECT_PATH,
853 APPLICATION_DBUS_INTERFACE,
854 introspection_data->interfaces[0],
856 ev_application, NULL,
858 if (ev_application->registration_id == 0) {
859 g_printerr ("Failed to register bus object: %s\n", error->message);
860 g_error_free (error);
864 g_printerr ("Failed to get bus connection: %s\n", error->message);
865 g_error_free (error);
868 ev_application->keys = ev_media_player_keys_new ();
870 #endif /* ENABLE_DBUS */
874 ev_application_has_window (EvApplication *application)
876 GList *windows = ev_application_get_windows (application);
877 gboolean retval = windows != NULL;
879 g_list_free (windows);
885 ev_application_get_uri (EvApplication *application)
887 return application->uri;
891 * ev_application_get_media_keys:
892 * @application: The instance of the application.
894 * It gives you access to the media player keys handler object.
896 * Returns: A #EvMediaPlayerKeys.
899 ev_application_get_media_keys (EvApplication *application)
902 return G_OBJECT (application->keys);
905 #endif /* ENABLE_DBUS */
909 ev_application_set_filechooser_uri (EvApplication *application,
910 GtkFileChooserAction action,
913 if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
914 g_free (application->filechooser_open_uri);
915 application->filechooser_open_uri = g_strdup (uri);
916 } else if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
917 g_free (application->filechooser_save_uri);
918 application->filechooser_save_uri = g_strdup (uri);
923 ev_application_get_filechooser_uri (EvApplication *application,
924 GtkFileChooserAction action)
926 if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
927 if (application->filechooser_open_uri)
928 return application->filechooser_open_uri;
929 } else if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
930 if (application->filechooser_save_uri)
931 return application->filechooser_save_uri;
938 ev_application_screensaver_enable (EvApplication *application)
940 totem_scrsaver_enable (application->scr_saver);
944 ev_application_screensaver_disable (EvApplication *application)
946 totem_scrsaver_disable (application->scr_saver);
950 ev_application_get_dot_dir (EvApplication *application,
954 g_mkdir_with_parents (application->dot_dir, 0700);
956 return application->dot_dir;
960 ev_application_get_data_dir (EvApplication *application)
962 return application->data_dir;