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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
29 #include <glib/gi18n.h>
31 #ifdef GDK_WINDOWING_X11
35 #include "totem-scrsaver.h"
38 #include "eggsmclient.h"
41 #include "ev-application.h"
42 #include "ev-document-factory.h"
43 #include "ev-file-helpers.h"
45 #include "ev-stock-icons.h"
48 #include "ev-media-player-keys.h"
49 #endif /* ENABLE_DBUS */
52 #include <dbus/dbus-glib-bindings.h>
53 static gboolean ev_application_open_uri (EvApplication *application,
58 #include "ev-application-service.h"
61 struct _EvApplication {
62 GObject base_instance;
68 gchar *accel_map_file;
72 DBusGConnection *connection;
73 EvMediaPlayerKeys *keys;
76 EggToolbarsModel *toolbars_model;
78 TotemScrsaver *scr_saver;
81 EggSMClient *smclient;
84 gchar *filechooser_open_uri;
85 gchar *filechooser_save_uri;
88 struct _EvApplicationClass {
89 GObjectClass base_class;
92 static EvApplication *instance;
94 G_DEFINE_TYPE (EvApplication, ev_application, G_TYPE_OBJECT);
97 #define APPLICATION_DBUS_OBJECT_PATH "/org/gnome/evince/Evince"
98 #define APPLICATION_DBUS_INTERFACE "org.gnome.evince.Application"
102 * ev_application_get_instance:
104 * Checks for #EvApplication instance, if it doesn't exist it does create it.
106 * Returns: an instance of the #EvApplication data.
109 ev_application_get_instance (void)
112 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
120 ev_application_load_session (EvApplication *application)
122 GKeyFile *state_file;
126 if (egg_sm_client_is_resumed (application->smclient)) {
127 state_file = egg_sm_client_get_state_file (application->smclient);
131 #endif /* WITH_SMCLIENT */
134 uri = g_key_file_get_string (state_file, "Evince", "uri", NULL);
138 ev_application_open_uri_at_dest (application, uri,
139 gdk_screen_get_default (),
143 g_key_file_free (state_file);
151 smclient_save_state_cb (EggSMClient *client,
152 GKeyFile *state_file,
153 EvApplication *application)
155 if (!application->uri)
158 g_key_file_set_string (state_file, "Evince", "uri", application->uri);
162 smclient_quit_cb (EggSMClient *client,
163 EvApplication *application)
165 ev_application_shutdown (application);
167 #endif /* WITH_SMCLIENT */
170 ev_application_init_session (EvApplication *application)
173 application->smclient = egg_sm_client_get ();
174 g_signal_connect (application->smclient, "save_state",
175 G_CALLBACK (smclient_save_state_cb),
177 g_signal_connect (application->smclient, "quit",
178 G_CALLBACK (smclient_quit_cb),
184 * ev_display_open_if_needed:
185 * @name: the name of the display to be open if it's needed.
187 * Search among all the open displays if any of them have the same name as the
188 * passed name. If the display isn't found it tries the open it.
190 * Returns: a #GdkDisplay of the display with the passed name.
193 ev_display_open_if_needed (const gchar *name)
197 GdkDisplay *display = NULL;
199 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
201 for (l = displays; l != NULL; l = l->next) {
202 const gchar *display_name = gdk_display_get_name ((GdkDisplay *) l->data);
204 if (g_ascii_strcasecmp (display_name, name) == 0) {
210 g_slist_free (displays);
212 return display != NULL ? display : gdk_display_open (name);
216 * get_screen_from_args:
217 * @args: a #GHashTable with data passed to the application.
219 * Looks for the screen in the display available in the hash table passed to the
220 * application. If the display isn't opened, it's opened and the #GdkScreen
221 * assigned to the screen in that display returned.
223 * Returns: the #GdkScreen assigned to the screen on the display indicated by
224 * the data on the #GHashTable.
227 get_screen_from_args (GHashTable *args)
229 GValue *value = NULL;
230 GdkDisplay *display = NULL;
231 GdkScreen *screen = NULL;
233 g_assert (args != NULL);
235 value = g_hash_table_lookup (args, "display");
237 const gchar *display_name;
239 display_name = g_value_get_string (value);
240 display = ev_display_open_if_needed (display_name);
243 value = g_hash_table_lookup (args, "screen");
247 screen_number = g_value_get_int (value);
248 screen = gdk_display_get_screen (display, screen_number);
255 * get_window_run_mode_from_args:
256 * @args: a #GHashTable with data passed to the application.
258 * It does look if the mode option has been passed from command line, using it
259 * as the window run mode, otherwise the run mode will be the normal mode.
261 * Returns: The window run mode passed from command line or
262 * EV_WINDOW_MODE_NORMAL in other case.
264 static EvWindowRunMode
265 get_window_run_mode_from_args (GHashTable *args)
267 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
268 GValue *value = NULL;
270 g_assert (args != NULL);
272 value = g_hash_table_lookup (args, "mode");
274 mode = g_value_get_uint (value);
281 * get_destination_from_args:
282 * @args: a #GHashTable with data passed to the application.
284 * It does look for the page-label argument parsed from the command line and
285 * if it does exist, it returns an #EvLinkDest.
287 * Returns: An #EvLinkDest to page-label if it has been passed from the command
288 * line, NULL in other case.
291 get_destination_from_args (GHashTable *args)
293 EvLinkDest *dest = NULL;
294 GValue *value = NULL;
296 g_assert (args != NULL);
298 value = g_hash_table_lookup (args, "page-label");
300 const gchar *page_label;
302 page_label = g_value_get_string (value);
303 dest = ev_link_dest_new_page_label (page_label);
310 get_find_string_from_args (GHashTable *args)
312 GValue *value = NULL;
314 g_assert (args != NULL);
316 value = g_hash_table_lookup (args, "find-string");
318 return value ? g_value_get_string (value) : NULL;
322 value_free (GValue *value)
324 g_value_unset (value);
329 build_args (GdkScreen *screen,
331 EvWindowRunMode mode,
332 const gchar *search_string)
337 const gchar *display_name;
340 args = g_hash_table_new_full (g_str_hash,
342 (GDestroyNotify)g_free,
343 (GDestroyNotify)value_free);
346 display = gdk_screen_get_display (screen);
347 display_name = gdk_display_get_name (display);
348 value = g_new0 (GValue, 1);
349 g_value_init (value, G_TYPE_STRING);
350 g_value_set_string (value, display_name);
351 g_hash_table_insert (args, g_strdup ("display"), value);
354 screen_number = gdk_screen_get_number (screen);
355 value = g_new0 (GValue, 1);
356 g_value_init (value, G_TYPE_INT);
357 g_value_set_int (value, screen_number);
358 g_hash_table_insert (args, g_strdup ("screen"), value);
362 value = g_new0 (GValue, 1);
363 g_value_init (value, G_TYPE_STRING);
364 g_value_set_string (value, ev_link_dest_get_page_label (dest));
366 g_hash_table_insert (args, g_strdup ("page-label"), value);
371 value = g_new0 (GValue, 1);
372 g_value_init (value, G_TYPE_STRING);
373 g_value_set_string (value, search_string);
375 g_hash_table_insert (args, g_strdup ("find-string"), value);
379 if (mode != EV_WINDOW_MODE_NORMAL) {
380 value = g_new0 (GValue, 1);
381 g_value_init (value, G_TYPE_UINT);
382 g_value_set_uint (value, mode);
384 g_hash_table_insert (args, g_strdup ("mode"), value);
391 child_setup (gpointer user_data)
395 startup_id = g_strdup_printf ("_TIME%lu",
396 (unsigned long)GPOINTER_TO_INT (user_data));
397 g_setenv ("DESKTOP_STARTUP_ID", startup_id, TRUE);
402 ev_spawn (const char *uri,
405 EvWindowRunMode mode,
406 const gchar *search_string,
413 GError *error = NULL;
415 argv[arg++] = g_build_filename (BINDIR, "evince", NULL);
419 const gchar *page_label;
421 page_label = ev_link_dest_get_page_label (dest);
423 argv[arg++] = g_strdup_printf ("--page-label=%s", page_label);
425 argv[arg++] = g_strdup_printf ("--page-label=%d",
426 ev_link_dest_get_page (dest));
431 argv[arg++] = g_strdup_printf ("--find=%s", search_string);
436 case EV_WINDOW_MODE_FULLSCREEN:
437 argv[arg++] = g_strdup ("-f");
439 case EV_WINDOW_MODE_PRESENTATION:
440 argv[arg++] = g_strdup ("-s");
446 argv[arg++] = (gchar *)uri;
449 res = gdk_spawn_on_screen (screen, NULL /* wd */, argv, NULL /* env */,
452 GINT_TO_POINTER(timestamp),
455 g_warning ("Error launching evince %s: %s\n", uri, error->message);
456 g_error_free (error);
459 for (i = 0; i < arg - 1; i++) {
465 ev_application_get_windows (EvApplication *application)
467 GList *l, *toplevels;
468 GList *windows = NULL;
470 toplevels = gtk_window_list_toplevels ();
472 for (l = toplevels; l != NULL; l = l->next) {
473 if (EV_IS_WINDOW (l->data)) {
474 windows = g_list_append (windows, l->data);
478 g_list_free (toplevels);
484 ev_application_get_empty_window (EvApplication *application,
487 EvWindow *empty_window = NULL;
488 GList *windows = ev_application_get_windows (application);
491 for (l = windows; l != NULL; l = l->next) {
492 EvWindow *window = EV_WINDOW (l->data);
494 if (ev_window_is_empty (window) &&
495 gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
496 empty_window = window;
501 g_list_free (windows);
509 ev_application_register_uri (EvApplication *application,
516 gboolean retval = TRUE;
517 GError *error = NULL;
519 if (!application->connection)
522 proxy = dbus_g_proxy_new_for_name (application->connection,
523 "org.gnome.evince.Daemon",
524 "/org/gnome/evince/Daemon",
525 "org.gnome.evince.Daemon");
526 if (!dbus_g_proxy_call (proxy, "RegisterDocument", &error,
529 G_TYPE_STRING, &owner,
531 g_warning ("Error registering document: %s\n", error->message);
532 g_error_free (error);
533 g_object_unref (proxy);
537 g_object_unref (proxy);
540 /* Already registered */
541 proxy = dbus_g_proxy_new_for_name_owner (application->connection,
543 APPLICATION_DBUS_OBJECT_PATH,
544 APPLICATION_DBUS_INTERFACE,
547 if (!dbus_g_proxy_call (proxy, "OpenURI", &error,
549 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
550 G_TYPE_UINT, timestamp,
553 g_warning ("%s", error->message);
554 g_error_free (error);
556 g_object_unref (proxy);
558 g_warning ("Error creating proxy: %s\n", error->message);
559 g_error_free (error);
562 /* Do not continue opening this document */
572 ev_application_unregister_uri (EvApplication *application,
576 GError *error = NULL;
578 if (!application->connection)
581 proxy = dbus_g_proxy_new_for_name (application->connection,
582 "org.gnome.evince.Daemon",
583 "/org/gnome/evince/Daemon",
584 "org.gnome.evince.Daemon");
585 if (!dbus_g_proxy_call (proxy, "UnregisterDocument", &error,
589 g_warning ("Error unregistering document: %s\n", error->message);
590 g_error_free (error);
593 g_object_unref (proxy);
595 #endif /* ENABLE_DBUS */
598 ev_application_open_uri_in_window (EvApplication *application,
603 EvWindowRunMode mode,
604 const gchar *search_string,
608 ev_stock_icons_set_screen (screen);
609 gtk_window_set_screen (GTK_WINDOW (ev_window), screen);
612 /* We need to load uri before showing the window, so
613 we can restore window size without flickering */
614 ev_window_open_uri (ev_window, uri, dest, mode, search_string);
616 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (ev_window)))
617 gtk_widget_realize (GTK_WIDGET (ev_window));
619 #ifdef GDK_WINDOWING_X11
621 timestamp = gdk_x11_get_server_time (GTK_WIDGET (ev_window)->window);
622 gdk_x11_window_set_user_time (GTK_WIDGET (ev_window)->window, timestamp);
624 ev_document_fc_mutex_lock ();
625 gtk_window_present (GTK_WINDOW (ev_window));
626 ev_document_fc_mutex_unlock ();
628 ev_document_fc_mutex_lock ();
629 gtk_window_present_with_time (GTK_WINDOW (ev_window), timestamp);
630 ev_document_fc_mutex_unlock ();
631 #endif /* GDK_WINDOWING_X11 */
635 * ev_application_open_uri_at_dest:
636 * @application: The instance of the application.
637 * @uri: The uri to be opened.
638 * @screen: Thee screen where the link will be shown.
639 * @dest: The #EvLinkDest of the document.
640 * @mode: The run mode of the window.
641 * @timestamp: Current time value.
644 ev_application_open_uri_at_dest (EvApplication *application,
648 EvWindowRunMode mode,
649 const gchar *search_string,
654 g_return_if_fail (uri != NULL);
656 if (application->uri && strcmp (application->uri, uri) != 0) {
657 /* spawn a new evince process */
658 ev_spawn (uri, screen, dest, mode, search_string, timestamp);
662 GHashTable *args = build_args (screen, dest, mode, search_string);
665 /* Register the uri or send OpenURI to
666 * remote instance if already registered
668 ret = ev_application_register_uri (application, uri, args, timestamp);
669 g_hash_table_destroy (args);
672 #endif /* ENABLE_DBUS */
674 ev_window = ev_application_get_empty_window (application, screen);
676 ev_window = EV_WINDOW (ev_window_new ());
679 application->uri = g_strdup (uri);
681 ev_application_open_uri_in_window (application, uri, ev_window,
688 * ev_application_open_window:
689 * @application: The instance of the application.
690 * @timestamp: Current time value.
692 * Creates a new window
695 ev_application_open_window (EvApplication *application,
699 GtkWidget *new_window = ev_window_new ();
702 ev_stock_icons_set_screen (screen);
703 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
706 if (!GTK_WIDGET_REALIZED (new_window))
707 gtk_widget_realize (new_window);
709 #ifdef GDK_WINDOWING_X11
711 timestamp = gdk_x11_get_server_time (new_window->window);
712 gdk_x11_window_set_user_time (new_window->window, timestamp);
714 gtk_window_present (GTK_WINDOW (new_window));
716 gtk_window_present_with_time (GTK_WINDOW (new_window), timestamp);
717 #endif /* GDK_WINDOWING_X11 */
721 * ev_application_open_uri:
722 * @application: The instance of the application.
723 * @uri: The uri to be opened
724 * @args: A #GHashTable with the arguments data.
725 * @timestamp: Current time value.
726 * @error: The #GError facility.
729 ev_application_open_uri (EvApplication *application,
736 EvLinkDest *dest = NULL;
737 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
738 const gchar *search_string = NULL;
739 GdkScreen *screen = NULL;
741 g_assert (application->uri != NULL);
743 /* FIXME: we don't need uri anymore,
744 * maybe this method should be renamed
745 * as reload, refresh or something like that
747 if (!application->uri || strcmp (application->uri, uri)) {
748 g_warning ("Invalid uri: %s, expected %s\n",
749 uri, application->uri);
754 screen = get_screen_from_args (args);
755 dest = get_destination_from_args (args);
756 mode = get_window_run_mode_from_args (args);
757 search_string = get_find_string_from_args (args);
760 windows = ev_application_get_windows (application);
761 for (l = windows; l != NULL; l = g_list_next (l)) {
762 EvWindow *ev_window = EV_WINDOW (l->data);
764 ev_application_open_uri_in_window (application, uri, ev_window,
769 g_list_free (windows);
772 g_object_unref (dest);
778 ev_application_open_uri_list (EvApplication *application,
785 for (l = uri_list; l != NULL; l = l->next) {
786 ev_application_open_uri_at_dest (application, (char *)l->data,
787 screen, NULL, 0, NULL,
793 ev_application_shutdown (EvApplication *application)
795 if (application->uri) {
797 ev_application_unregister_uri (application,
800 g_free (application->uri);
801 application->uri = NULL;
804 if (application->accel_map_file) {
805 gtk_accel_map_save (application->accel_map_file);
806 g_free (application->accel_map_file);
807 application->accel_map_file = NULL;
810 if (application->toolbars_model) {
811 g_object_unref (application->toolbars_model);
812 g_free (application->toolbars_file);
813 application->toolbars_model = NULL;
814 application->toolbars_file = NULL;
818 if (application->keys) {
819 g_object_unref (application->keys);
820 application->keys = NULL;
822 #endif /* ENABLE_DBUS */
824 g_free (application->dot_dir);
825 application->dot_dir = NULL;
826 g_free (application->data_dir);
827 application->data_dir = NULL;
828 g_free (application->filechooser_open_uri);
829 application->filechooser_open_uri = NULL;
830 g_free (application->filechooser_save_uri);
831 application->filechooser_save_uri = NULL;
833 g_object_unref (application);
840 ev_application_class_init (EvApplicationClass *ev_application_class)
843 dbus_g_object_type_install_info (EV_TYPE_APPLICATION,
844 &dbus_glib_ev_application_object_info);
849 ev_application_init (EvApplication *ev_application)
852 const gchar *home_dir;
854 GError *error = NULL;
856 ev_application->dot_dir = g_build_filename (g_get_home_dir (),
861 /* FIXME: why make this fatal? */
862 if (!ev_dir_ensure_exists (ev_application->dot_dir, 0700))
869 dir = g_win32_get_package_installation_directory_of_module (NULL);
870 ev_application->data_dir = g_build_filename (dir, "share", "evince", NULL);
874 ev_application->data_dir = g_strdup (DATADIR);
877 ev_application_init_session (ev_application);
879 home_dir = g_get_home_dir ();
881 ev_application->accel_map_file = g_build_filename (home_dir,
886 gtk_accel_map_load (ev_application->accel_map_file);
889 ev_application->toolbars_model = egg_toolbars_model_new ();
891 ev_application->toolbars_file = g_build_filename
892 (ev_application->dot_dir, "evince_toolbar.xml", NULL);
894 toolbar_path = g_build_filename (ev_application->data_dir,
895 "evince-toolbar.xml", NULL);
896 egg_toolbars_model_load_names (ev_application->toolbars_model,
899 if (!egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
900 ev_application->toolbars_file)) {
901 egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
904 g_free (toolbar_path);
906 /* Open item doesn't exist anymore,
907 * convert it to OpenRecent for compatibility
909 for (i = 0; i < egg_toolbars_model_n_items (ev_application->toolbars_model, 0); i++) {
912 item = egg_toolbars_model_item_nth (ev_application->toolbars_model, 0, i);
913 if (g_ascii_strcasecmp (item, "FileOpen") == 0) {
914 egg_toolbars_model_remove_item (ev_application->toolbars_model, 0, i);
915 egg_toolbars_model_add_item (ev_application->toolbars_model, 0, i,
917 ev_application_save_toolbars_model (ev_application);
922 egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
923 EGG_TB_MODEL_NOT_REMOVABLE);
926 ev_application->connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
927 if (ev_application->connection) {
928 dbus_g_connection_register_g_object (ev_application->connection,
929 APPLICATION_DBUS_OBJECT_PATH,
930 G_OBJECT (ev_application));
931 ev_application->scr_saver = totem_scrsaver_new (ev_application->connection);
933 g_warning ("Error connection to DBus: %s\n", error->message);
934 g_error_free (error);
936 ev_application->keys = ev_media_player_keys_new ();
937 #endif /* ENABLE_DBUS */
941 ev_application_has_window (EvApplication *application)
943 GList *windows = ev_application_get_windows (application);
944 gboolean retval = windows != NULL;
946 g_list_free (windows);
952 ev_application_get_uri (EvApplication *application)
954 return application->uri;
958 * ev_application_get_media_keys:
959 * @application: The instance of the application.
961 * It gives you access to the media player keys handler object.
963 * Returns: A #EvMediaPlayerKeys.
966 ev_application_get_media_keys (EvApplication *application)
969 return G_OBJECT (application->keys);
972 #endif /* ENABLE_DBUS */
976 ev_application_get_toolbars_model (EvApplication *application)
978 return application->toolbars_model;
982 ev_application_save_toolbars_model (EvApplication *application)
984 egg_toolbars_model_save_toolbars (application->toolbars_model,
985 application->toolbars_file, "1.0");
989 ev_application_set_filechooser_uri (EvApplication *application,
990 GtkFileChooserAction action,
993 if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
994 g_free (application->filechooser_open_uri);
995 application->filechooser_open_uri = g_strdup (uri);
996 } else if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
997 g_free (application->filechooser_save_uri);
998 application->filechooser_save_uri = g_strdup (uri);
1003 ev_application_get_filechooser_uri (EvApplication *application,
1004 GtkFileChooserAction action)
1006 if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
1007 if (application->filechooser_open_uri)
1008 return application->filechooser_open_uri;
1009 } else if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
1010 if (application->filechooser_save_uri)
1011 return application->filechooser_save_uri;
1018 ev_application_screensaver_enable (EvApplication *application)
1020 if (application->scr_saver)
1021 totem_scrsaver_enable (application->scr_saver);
1025 ev_application_screensaver_disable (EvApplication *application)
1027 if (application->scr_saver)
1028 totem_scrsaver_disable (application->scr_saver);
1032 ev_application_get_dot_dir (EvApplication *application)
1034 return application->dot_dir;
1038 ev_application_get_data_dir (EvApplication *application)
1040 return application->data_dir;