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 */
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;
70 DBusGConnection *connection;
71 EvMediaPlayerKeys *keys;
74 TotemScrsaver *scr_saver;
77 EggSMClient *smclient;
80 gchar *filechooser_open_uri;
81 gchar *filechooser_save_uri;
84 struct _EvApplicationClass {
85 GObjectClass base_class;
88 static EvApplication *instance;
90 G_DEFINE_TYPE (EvApplication, ev_application, G_TYPE_OBJECT);
93 #define APPLICATION_DBUS_OBJECT_PATH "/org/gnome/evince/Evince"
94 #define APPLICATION_DBUS_INTERFACE "org.gnome.evince.Application"
97 static const gchar *userdir = NULL;
100 * ev_application_get_instance:
102 * Checks for #EvApplication instance, if it doesn't exist it does create it.
104 * Returns: an instance of the #EvApplication data.
107 ev_application_get_instance (void)
110 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
118 ev_application_load_session (EvApplication *application)
120 GKeyFile *state_file;
124 if (egg_sm_client_is_resumed (application->smclient)) {
125 state_file = egg_sm_client_get_state_file (application->smclient);
129 #endif /* WITH_SMCLIENT */
132 uri = g_key_file_get_string (state_file, "Evince", "uri", NULL);
136 ev_application_open_uri_at_dest (application, uri,
137 gdk_screen_get_default (),
141 g_key_file_free (state_file);
149 smclient_save_state_cb (EggSMClient *client,
150 GKeyFile *state_file,
151 EvApplication *application)
153 if (!application->uri)
156 g_key_file_set_string (state_file, "Evince", "uri", application->uri);
160 smclient_quit_cb (EggSMClient *client,
161 EvApplication *application)
163 ev_application_shutdown (application);
165 #endif /* WITH_SMCLIENT */
168 ev_application_init_session (EvApplication *application)
171 application->smclient = egg_sm_client_get ();
172 g_signal_connect (application->smclient, "save_state",
173 G_CALLBACK (smclient_save_state_cb),
175 g_signal_connect (application->smclient, "quit",
176 G_CALLBACK (smclient_quit_cb),
182 * ev_display_open_if_needed:
183 * @name: the name of the display to be open if it's needed.
185 * Search among all the open displays if any of them have the same name as the
186 * passed name. If the display isn't found it tries the open it.
188 * Returns: a #GdkDisplay of the display with the passed name.
191 ev_display_open_if_needed (const gchar *name)
195 GdkDisplay *display = NULL;
197 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
199 for (l = displays; l != NULL; l = l->next) {
200 const gchar *display_name = gdk_display_get_name ((GdkDisplay *) l->data);
202 if (g_ascii_strcasecmp (display_name, name) == 0) {
208 g_slist_free (displays);
210 return display != NULL ? display : gdk_display_open (name);
214 * get_screen_from_args:
215 * @args: a #GHashTable with data passed to the application.
217 * Looks for the screen in the display available in the hash table passed to the
218 * application. If the display isn't opened, it's opened and the #GdkScreen
219 * assigned to the screen in that display returned.
221 * Returns: the #GdkScreen assigned to the screen on the display indicated by
222 * the data on the #GHashTable.
225 get_screen_from_args (GHashTable *args)
227 GValue *value = NULL;
228 GdkDisplay *display = NULL;
229 GdkScreen *screen = NULL;
231 g_assert (args != NULL);
233 value = g_hash_table_lookup (args, "display");
235 const gchar *display_name;
237 display_name = g_value_get_string (value);
238 display = ev_display_open_if_needed (display_name);
241 value = g_hash_table_lookup (args, "screen");
245 screen_number = g_value_get_int (value);
246 screen = gdk_display_get_screen (display, screen_number);
253 * get_window_run_mode_from_args:
254 * @args: a #GHashTable with data passed to the application.
256 * It does look if the mode option has been passed from command line, using it
257 * as the window run mode, otherwise the run mode will be the normal mode.
259 * Returns: The window run mode passed from command line or
260 * EV_WINDOW_MODE_NORMAL in other case.
262 static EvWindowRunMode
263 get_window_run_mode_from_args (GHashTable *args)
265 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
266 GValue *value = NULL;
268 g_assert (args != NULL);
270 value = g_hash_table_lookup (args, "mode");
272 mode = g_value_get_uint (value);
279 * get_destination_from_args:
280 * @args: a #GHashTable with data passed to the application.
282 * It does look for the page-label argument parsed from the command line and
283 * if it does exist, it returns an #EvLinkDest.
285 * Returns: An #EvLinkDest to page-label if it has been passed from the command
286 * line, NULL in other case.
289 get_destination_from_args (GHashTable *args)
291 EvLinkDest *dest = NULL;
292 GValue *value = NULL;
294 g_assert (args != NULL);
296 value = g_hash_table_lookup (args, "page-label");
298 const gchar *page_label;
300 page_label = g_value_get_string (value);
301 dest = ev_link_dest_new_page_label (page_label);
308 get_find_string_from_args (GHashTable *args)
310 GValue *value = NULL;
312 g_assert (args != NULL);
314 value = g_hash_table_lookup (args, "find-string");
316 return value ? g_value_get_string (value) : NULL;
320 value_free (GValue *value)
322 g_value_unset (value);
327 build_args (GdkScreen *screen,
329 EvWindowRunMode mode,
330 const gchar *search_string)
335 const gchar *display_name;
338 args = g_hash_table_new_full (g_str_hash,
340 (GDestroyNotify)g_free,
341 (GDestroyNotify)value_free);
344 display = gdk_screen_get_display (screen);
345 display_name = gdk_display_get_name (display);
346 value = g_new0 (GValue, 1);
347 g_value_init (value, G_TYPE_STRING);
348 g_value_set_string (value, display_name);
349 g_hash_table_insert (args, g_strdup ("display"), value);
352 screen_number = gdk_screen_get_number (screen);
353 value = g_new0 (GValue, 1);
354 g_value_init (value, G_TYPE_INT);
355 g_value_set_int (value, screen_number);
356 g_hash_table_insert (args, g_strdup ("screen"), value);
360 value = g_new0 (GValue, 1);
361 g_value_init (value, G_TYPE_STRING);
362 g_value_set_string (value, ev_link_dest_get_page_label (dest));
364 g_hash_table_insert (args, g_strdup ("page-label"), value);
369 value = g_new0 (GValue, 1);
370 g_value_init (value, G_TYPE_STRING);
371 g_value_set_string (value, search_string);
373 g_hash_table_insert (args, g_strdup ("find-string"), value);
377 if (mode != EV_WINDOW_MODE_NORMAL) {
378 value = g_new0 (GValue, 1);
379 g_value_init (value, G_TYPE_UINT);
380 g_value_set_uint (value, mode);
382 g_hash_table_insert (args, g_strdup ("mode"), value);
389 child_setup (gpointer user_data)
393 startup_id = g_strdup_printf ("_TIME%lu",
394 (unsigned long)GPOINTER_TO_INT (user_data));
395 g_setenv ("DESKTOP_STARTUP_ID", startup_id, TRUE);
400 ev_spawn (const char *uri,
403 EvWindowRunMode mode,
404 const gchar *search_string,
411 GError *error = NULL;
417 dir = g_win32_get_package_installation_directory_of_module (NULL);
418 argv[arg++] = g_build_filename (dir, "bin", "evince", NULL);
422 argv[arg++] = g_build_filename (BINDIR, "evince", NULL);
427 const gchar *page_label;
429 page_label = ev_link_dest_get_page_label (dest);
431 argv[arg++] = g_strdup_printf ("--page-label=%s", page_label);
433 argv[arg++] = g_strdup_printf ("--page-label=%d",
434 ev_link_dest_get_page (dest));
439 argv[arg++] = g_strdup_printf ("--find=%s", search_string);
444 case EV_WINDOW_MODE_FULLSCREEN:
445 argv[arg++] = g_strdup ("-f");
447 case EV_WINDOW_MODE_PRESENTATION:
448 argv[arg++] = g_strdup ("-s");
454 argv[arg++] = (gchar *)uri;
457 res = gdk_spawn_on_screen (screen, NULL /* wd */, argv, NULL /* env */,
460 GINT_TO_POINTER(timestamp),
463 g_warning ("Error launching evince %s: %s\n", uri, error->message);
464 g_error_free (error);
467 for (i = 0; i < arg - 1; i++) {
473 ev_application_get_windows (EvApplication *application)
475 GList *l, *toplevels;
476 GList *windows = NULL;
478 toplevels = gtk_window_list_toplevels ();
480 for (l = toplevels; l != NULL; l = l->next) {
481 if (EV_IS_WINDOW (l->data)) {
482 windows = g_list_append (windows, l->data);
486 g_list_free (toplevels);
492 ev_application_get_empty_window (EvApplication *application,
495 EvWindow *empty_window = NULL;
496 GList *windows = ev_application_get_windows (application);
499 for (l = windows; l != NULL; l = l->next) {
500 EvWindow *window = EV_WINDOW (l->data);
502 if (ev_window_is_empty (window) &&
503 gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
504 empty_window = window;
509 g_list_free (windows);
517 ev_application_register_uri (EvApplication *application,
524 gboolean retval = TRUE;
525 GError *error = NULL;
527 if (!application->connection)
530 proxy = dbus_g_proxy_new_for_name (application->connection,
531 "org.gnome.evince.Daemon",
532 "/org/gnome/evince/Daemon",
533 "org.gnome.evince.Daemon");
534 if (!dbus_g_proxy_call (proxy, "RegisterDocument", &error,
537 G_TYPE_STRING, &owner,
539 g_warning ("Error registering document: %s\n", error->message);
540 g_error_free (error);
541 g_object_unref (proxy);
545 g_object_unref (proxy);
548 /* Already registered */
549 proxy = dbus_g_proxy_new_for_name_owner (application->connection,
551 APPLICATION_DBUS_OBJECT_PATH,
552 APPLICATION_DBUS_INTERFACE,
555 if (!dbus_g_proxy_call (proxy, "OpenURI", &error,
557 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
558 G_TYPE_UINT, timestamp,
561 g_warning ("%s", error->message);
562 g_error_free (error);
564 g_object_unref (proxy);
566 g_warning ("Error creating proxy: %s\n", error->message);
567 g_error_free (error);
570 /* Do not continue opening this document */
580 ev_application_unregister_uri (EvApplication *application,
584 GError *error = NULL;
586 if (!application->connection)
589 proxy = dbus_g_proxy_new_for_name (application->connection,
590 "org.gnome.evince.Daemon",
591 "/org/gnome/evince/Daemon",
592 "org.gnome.evince.Daemon");
593 if (!dbus_g_proxy_call (proxy, "UnregisterDocument", &error,
597 g_warning ("Error unregistering document: %s\n", error->message);
598 g_error_free (error);
601 g_object_unref (proxy);
603 #endif /* ENABLE_DBUS */
606 ev_application_open_uri_in_window (EvApplication *application,
611 EvWindowRunMode mode,
612 const gchar *search_string,
615 #ifdef GDK_WINDOWING_X11
616 GdkWindow *gdk_window;
620 ev_stock_icons_set_screen (screen);
621 gtk_window_set_screen (GTK_WINDOW (ev_window), screen);
624 /* We need to load uri before showing the window, so
625 we can restore window size without flickering */
626 ev_window_open_uri (ev_window, uri, dest, mode, search_string);
628 if (!gtk_widget_get_realized (GTK_WIDGET (ev_window)))
629 gtk_widget_realize (GTK_WIDGET (ev_window));
631 #ifdef GDK_WINDOWING_X11
632 gdk_window = gtk_widget_get_window (GTK_WIDGET (ev_window));
635 timestamp = gdk_x11_get_server_time (gdk_window);
636 gdk_x11_window_set_user_time (gdk_window, timestamp);
638 ev_document_fc_mutex_lock ();
639 gtk_window_present (GTK_WINDOW (ev_window));
640 ev_document_fc_mutex_unlock ();
642 ev_document_fc_mutex_lock ();
643 gtk_window_present_with_time (GTK_WINDOW (ev_window), timestamp);
644 ev_document_fc_mutex_unlock ();
645 #endif /* GDK_WINDOWING_X11 */
649 * ev_application_open_uri_at_dest:
650 * @application: The instance of the application.
651 * @uri: The uri to be opened.
652 * @screen: Thee screen where the link will be shown.
653 * @dest: The #EvLinkDest of the document.
654 * @mode: The run mode of the window.
655 * @timestamp: Current time value.
658 ev_application_open_uri_at_dest (EvApplication *application,
662 EvWindowRunMode mode,
663 const gchar *search_string,
668 g_return_if_fail (uri != NULL);
670 if (application->uri && strcmp (application->uri, uri) != 0) {
671 /* spawn a new evince process */
672 ev_spawn (uri, screen, dest, mode, search_string, timestamp);
676 GHashTable *args = build_args (screen, dest, mode, search_string);
679 /* Register the uri or send OpenURI to
680 * remote instance if already registered
682 ret = ev_application_register_uri (application, uri, args, timestamp);
683 g_hash_table_destroy (args);
686 #endif /* ENABLE_DBUS */
688 ev_window = ev_application_get_empty_window (application, screen);
690 ev_window = EV_WINDOW (ev_window_new ());
693 application->uri = g_strdup (uri);
695 ev_application_open_uri_in_window (application, uri, ev_window,
702 * ev_application_open_window:
703 * @application: The instance of the application.
704 * @timestamp: Current time value.
706 * Creates a new window
709 ev_application_open_window (EvApplication *application,
713 GtkWidget *new_window = ev_window_new ();
714 #ifdef GDK_WINDOWING_X11
715 GdkWindow *gdk_window;
719 ev_stock_icons_set_screen (screen);
720 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
723 if (!gtk_widget_get_realized (new_window))
724 gtk_widget_realize (new_window);
726 #ifdef GDK_WINDOWING_X11
727 gdk_window = gtk_widget_get_window (GTK_WIDGET (new_window));
730 timestamp = gdk_x11_get_server_time (gdk_window);
731 gdk_x11_window_set_user_time (gdk_window, timestamp);
733 gtk_window_present (GTK_WINDOW (new_window));
735 gtk_window_present_with_time (GTK_WINDOW (new_window), timestamp);
736 #endif /* GDK_WINDOWING_X11 */
740 * ev_application_open_uri:
741 * @application: The instance of the application.
742 * @uri: The uri to be opened
743 * @args: A #GHashTable with the arguments data.
744 * @timestamp: Current time value.
745 * @error: The #GError facility.
748 ev_application_open_uri (EvApplication *application,
755 EvLinkDest *dest = NULL;
756 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
757 const gchar *search_string = NULL;
758 GdkScreen *screen = NULL;
760 g_assert (application->uri != NULL);
762 /* FIXME: we don't need uri anymore,
763 * maybe this method should be renamed
764 * as reload, refresh or something like that
766 if (!application->uri || strcmp (application->uri, uri)) {
767 g_warning ("Invalid uri: %s, expected %s\n",
768 uri, application->uri);
773 screen = get_screen_from_args (args);
774 dest = get_destination_from_args (args);
775 mode = get_window_run_mode_from_args (args);
776 search_string = get_find_string_from_args (args);
779 windows = ev_application_get_windows (application);
780 for (l = windows; l != NULL; l = g_list_next (l)) {
781 EvWindow *ev_window = EV_WINDOW (l->data);
783 ev_application_open_uri_in_window (application, uri, ev_window,
788 g_list_free (windows);
791 g_object_unref (dest);
797 ev_application_open_uri_list (EvApplication *application,
804 for (l = uri_list; l != NULL; l = l->next) {
805 ev_application_open_uri_at_dest (application, (char *)l->data,
806 screen, NULL, 0, NULL,
812 ev_application_accel_map_save (EvApplication *application)
814 gchar *accel_map_file;
819 accel_map_file = g_build_filename (userdir, "accels",
822 accel_map_file = g_build_filename (g_get_home_dir (),
827 tmp_filename = g_strdup_printf ("%s.XXXXXX", accel_map_file);
829 fd = g_mkstemp (tmp_filename);
831 g_free (accel_map_file);
832 g_free (tmp_filename);
836 gtk_accel_map_save_fd (fd);
839 if (g_rename (tmp_filename, accel_map_file) == -1) {
841 g_unlink (tmp_filename);
844 g_free (accel_map_file);
845 g_free (tmp_filename);
849 ev_application_accel_map_load (EvApplication *application)
851 gchar *accel_map_file;
854 accel_map_file = g_build_filename (userdir, "accels",
857 accel_map_file = g_build_filename (g_get_home_dir (),
862 gtk_accel_map_load (accel_map_file);
863 g_free (accel_map_file);
867 ev_application_shutdown (EvApplication *application)
869 if (application->uri) {
871 ev_application_unregister_uri (application,
874 g_free (application->uri);
875 application->uri = NULL;
878 ev_application_accel_map_save (application);
880 g_object_unref (application->scr_saver);
881 application->scr_saver = NULL;
884 if (application->keys) {
885 g_object_unref (application->keys);
886 application->keys = NULL;
888 #endif /* ENABLE_DBUS */
890 g_free (application->dot_dir);
891 application->dot_dir = NULL;
892 g_free (application->data_dir);
893 application->data_dir = NULL;
894 g_free (application->filechooser_open_uri);
895 application->filechooser_open_uri = NULL;
896 g_free (application->filechooser_save_uri);
897 application->filechooser_save_uri = NULL;
899 g_object_unref (application);
906 ev_application_class_init (EvApplicationClass *ev_application_class)
909 dbus_g_object_type_install_info (EV_TYPE_APPLICATION,
910 &dbus_glib_ev_application_object_info);
915 ev_application_init (EvApplication *ev_application)
917 GError *error = NULL;
919 userdir = g_getenv ("GNOME22_USER_DIR");
921 ev_application->dot_dir = g_build_filename (userdir, "evince", NULL);
923 ev_application->dot_dir = g_build_filename (g_get_home_dir (),
932 dir = g_win32_get_package_installation_directory_of_module (NULL);
933 ev_application->data_dir = g_build_filename (dir, "share", "evince", NULL);
937 ev_application->data_dir = g_strdup (DATADIR);
940 ev_application_init_session (ev_application);
942 ev_application_accel_map_load (ev_application);
944 ev_application->scr_saver = totem_scrsaver_new ();
947 ev_application->connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
948 if (ev_application->connection) {
949 dbus_g_connection_register_g_object (ev_application->connection,
950 APPLICATION_DBUS_OBJECT_PATH,
951 G_OBJECT (ev_application));
953 g_warning ("Error connection to DBus: %s\n", error->message);
954 g_error_free (error);
956 ev_application->keys = ev_media_player_keys_new ();
957 #endif /* ENABLE_DBUS */
961 ev_application_has_window (EvApplication *application)
963 GList *windows = ev_application_get_windows (application);
964 gboolean retval = windows != NULL;
966 g_list_free (windows);
972 ev_application_get_uri (EvApplication *application)
974 return application->uri;
978 * ev_application_get_media_keys:
979 * @application: The instance of the application.
981 * It gives you access to the media player keys handler object.
983 * Returns: A #EvMediaPlayerKeys.
986 ev_application_get_media_keys (EvApplication *application)
989 return G_OBJECT (application->keys);
992 #endif /* ENABLE_DBUS */
996 ev_application_set_filechooser_uri (EvApplication *application,
997 GtkFileChooserAction action,
1000 if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
1001 g_free (application->filechooser_open_uri);
1002 application->filechooser_open_uri = g_strdup (uri);
1003 } else if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
1004 g_free (application->filechooser_save_uri);
1005 application->filechooser_save_uri = g_strdup (uri);
1010 ev_application_get_filechooser_uri (EvApplication *application,
1011 GtkFileChooserAction action)
1013 if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
1014 if (application->filechooser_open_uri)
1015 return application->filechooser_open_uri;
1016 } else if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
1017 if (application->filechooser_save_uri)
1018 return application->filechooser_save_uri;
1025 ev_application_screensaver_enable (EvApplication *application)
1027 totem_scrsaver_enable (application->scr_saver);
1031 ev_application_screensaver_disable (EvApplication *application)
1033 totem_scrsaver_disable (application->scr_saver);
1037 ev_application_get_dot_dir (EvApplication *application,
1041 g_mkdir_with_parents (application->dot_dir, 0700);
1043 return application->dot_dir;
1047 ev_application_get_data_dir (EvApplication *application)
1049 return application->data_dir;