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;
71 DBusGConnection *connection;
72 EvMediaPlayerKeys *keys;
75 TotemScrsaver *scr_saver;
78 EggSMClient *smclient;
81 gchar *filechooser_open_uri;
82 gchar *filechooser_save_uri;
85 struct _EvApplicationClass {
86 GObjectClass base_class;
89 static EvApplication *instance;
91 G_DEFINE_TYPE (EvApplication, ev_application, G_TYPE_OBJECT);
94 #define APPLICATION_DBUS_OBJECT_PATH "/org/gnome/evince/Evince"
95 #define APPLICATION_DBUS_INTERFACE "org.gnome.evince.Application"
99 * ev_application_get_instance:
101 * Checks for #EvApplication instance, if it doesn't exist it does create it.
103 * Returns: an instance of the #EvApplication data.
106 ev_application_get_instance (void)
109 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
117 ev_application_load_session (EvApplication *application)
119 GKeyFile *state_file;
123 if (egg_sm_client_is_resumed (application->smclient)) {
124 state_file = egg_sm_client_get_state_file (application->smclient);
128 #endif /* WITH_SMCLIENT */
131 uri = g_key_file_get_string (state_file, "Evince", "uri", NULL);
135 ev_application_open_uri_at_dest (application, uri,
136 gdk_screen_get_default (),
140 g_key_file_free (state_file);
148 smclient_save_state_cb (EggSMClient *client,
149 GKeyFile *state_file,
150 EvApplication *application)
152 if (!application->uri)
155 g_key_file_set_string (state_file, "Evince", "uri", application->uri);
159 smclient_quit_cb (EggSMClient *client,
160 EvApplication *application)
162 ev_application_shutdown (application);
164 #endif /* WITH_SMCLIENT */
167 ev_application_init_session (EvApplication *application)
170 application->smclient = egg_sm_client_get ();
171 g_signal_connect (application->smclient, "save_state",
172 G_CALLBACK (smclient_save_state_cb),
174 g_signal_connect (application->smclient, "quit",
175 G_CALLBACK (smclient_quit_cb),
181 * ev_display_open_if_needed:
182 * @name: the name of the display to be open if it's needed.
184 * Search among all the open displays if any of them have the same name as the
185 * passed name. If the display isn't found it tries the open it.
187 * Returns: a #GdkDisplay of the display with the passed name.
190 ev_display_open_if_needed (const gchar *name)
194 GdkDisplay *display = NULL;
196 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
198 for (l = displays; l != NULL; l = l->next) {
199 const gchar *display_name = gdk_display_get_name ((GdkDisplay *) l->data);
201 if (g_ascii_strcasecmp (display_name, name) == 0) {
207 g_slist_free (displays);
209 return display != NULL ? display : gdk_display_open (name);
213 * get_screen_from_args:
214 * @args: a #GHashTable with data passed to the application.
216 * Looks for the screen in the display available in the hash table passed to the
217 * application. If the display isn't opened, it's opened and the #GdkScreen
218 * assigned to the screen in that display returned.
220 * Returns: the #GdkScreen assigned to the screen on the display indicated by
221 * the data on the #GHashTable.
224 get_screen_from_args (GHashTable *args)
226 GValue *value = NULL;
227 GdkDisplay *display = NULL;
228 GdkScreen *screen = NULL;
230 g_assert (args != NULL);
232 value = g_hash_table_lookup (args, "display");
234 const gchar *display_name;
236 display_name = g_value_get_string (value);
237 display = ev_display_open_if_needed (display_name);
240 value = g_hash_table_lookup (args, "screen");
244 screen_number = g_value_get_int (value);
245 screen = gdk_display_get_screen (display, screen_number);
252 * get_window_run_mode_from_args:
253 * @args: a #GHashTable with data passed to the application.
255 * It does look if the mode option has been passed from command line, using it
256 * as the window run mode, otherwise the run mode will be the normal mode.
258 * Returns: The window run mode passed from command line or
259 * EV_WINDOW_MODE_NORMAL in other case.
261 static EvWindowRunMode
262 get_window_run_mode_from_args (GHashTable *args)
264 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
265 GValue *value = NULL;
267 g_assert (args != NULL);
269 value = g_hash_table_lookup (args, "mode");
271 mode = g_value_get_uint (value);
278 * get_destination_from_args:
279 * @args: a #GHashTable with data passed to the application.
281 * It does look for the page-label argument parsed from the command line and
282 * if it does exist, it returns an #EvLinkDest.
284 * Returns: An #EvLinkDest to page-label if it has been passed from the command
285 * line, NULL in other case.
288 get_destination_from_args (GHashTable *args)
290 EvLinkDest *dest = NULL;
291 GValue *value = NULL;
293 g_assert (args != NULL);
295 value = g_hash_table_lookup (args, "page-label");
297 const gchar *page_label;
299 page_label = g_value_get_string (value);
300 dest = ev_link_dest_new_page_label (page_label);
307 get_find_string_from_args (GHashTable *args)
309 GValue *value = NULL;
311 g_assert (args != NULL);
313 value = g_hash_table_lookup (args, "find-string");
315 return value ? g_value_get_string (value) : NULL;
319 value_free (GValue *value)
321 g_value_unset (value);
326 build_args (GdkScreen *screen,
328 EvWindowRunMode mode,
329 const gchar *search_string)
334 const gchar *display_name;
337 args = g_hash_table_new_full (g_str_hash,
339 (GDestroyNotify)g_free,
340 (GDestroyNotify)value_free);
343 display = gdk_screen_get_display (screen);
344 display_name = gdk_display_get_name (display);
345 value = g_new0 (GValue, 1);
346 g_value_init (value, G_TYPE_STRING);
347 g_value_set_string (value, display_name);
348 g_hash_table_insert (args, g_strdup ("display"), value);
351 screen_number = gdk_screen_get_number (screen);
352 value = g_new0 (GValue, 1);
353 g_value_init (value, G_TYPE_INT);
354 g_value_set_int (value, screen_number);
355 g_hash_table_insert (args, g_strdup ("screen"), value);
359 value = g_new0 (GValue, 1);
360 g_value_init (value, G_TYPE_STRING);
361 g_value_set_string (value, ev_link_dest_get_page_label (dest));
363 g_hash_table_insert (args, g_strdup ("page-label"), value);
368 value = g_new0 (GValue, 1);
369 g_value_init (value, G_TYPE_STRING);
370 g_value_set_string (value, search_string);
372 g_hash_table_insert (args, g_strdup ("find-string"), value);
376 if (mode != EV_WINDOW_MODE_NORMAL) {
377 value = g_new0 (GValue, 1);
378 g_value_init (value, G_TYPE_UINT);
379 g_value_set_uint (value, mode);
381 g_hash_table_insert (args, g_strdup ("mode"), value);
388 child_setup (gpointer user_data)
392 startup_id = g_strdup_printf ("_TIME%lu",
393 (unsigned long)GPOINTER_TO_INT (user_data));
394 g_setenv ("DESKTOP_STARTUP_ID", startup_id, TRUE);
399 ev_spawn (const char *uri,
402 EvWindowRunMode mode,
403 const gchar *search_string,
410 GError *error = NULL;
412 argv[arg++] = g_build_filename (BINDIR, "evince", NULL);
416 const gchar *page_label;
418 page_label = ev_link_dest_get_page_label (dest);
420 argv[arg++] = g_strdup_printf ("--page-label=%s", page_label);
422 argv[arg++] = g_strdup_printf ("--page-label=%d",
423 ev_link_dest_get_page (dest));
428 argv[arg++] = g_strdup_printf ("--find=%s", search_string);
433 case EV_WINDOW_MODE_FULLSCREEN:
434 argv[arg++] = g_strdup ("-f");
436 case EV_WINDOW_MODE_PRESENTATION:
437 argv[arg++] = g_strdup ("-s");
443 argv[arg++] = (gchar *)uri;
446 res = gdk_spawn_on_screen (screen, NULL /* wd */, argv, NULL /* env */,
449 GINT_TO_POINTER(timestamp),
452 g_warning ("Error launching evince %s: %s\n", uri, error->message);
453 g_error_free (error);
456 for (i = 0; i < arg - 1; i++) {
462 ev_application_get_windows (EvApplication *application)
464 GList *l, *toplevels;
465 GList *windows = NULL;
467 toplevels = gtk_window_list_toplevels ();
469 for (l = toplevels; l != NULL; l = l->next) {
470 if (EV_IS_WINDOW (l->data)) {
471 windows = g_list_append (windows, l->data);
475 g_list_free (toplevels);
481 ev_application_get_empty_window (EvApplication *application,
484 EvWindow *empty_window = NULL;
485 GList *windows = ev_application_get_windows (application);
488 for (l = windows; l != NULL; l = l->next) {
489 EvWindow *window = EV_WINDOW (l->data);
491 if (ev_window_is_empty (window) &&
492 gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
493 empty_window = window;
498 g_list_free (windows);
506 ev_application_register_uri (EvApplication *application,
513 gboolean retval = TRUE;
514 GError *error = NULL;
516 if (!application->connection)
519 proxy = dbus_g_proxy_new_for_name (application->connection,
520 "org.gnome.evince.Daemon",
521 "/org/gnome/evince/Daemon",
522 "org.gnome.evince.Daemon");
523 if (!dbus_g_proxy_call (proxy, "RegisterDocument", &error,
526 G_TYPE_STRING, &owner,
528 g_warning ("Error registering document: %s\n", error->message);
529 g_error_free (error);
530 g_object_unref (proxy);
534 g_object_unref (proxy);
537 /* Already registered */
538 proxy = dbus_g_proxy_new_for_name_owner (application->connection,
540 APPLICATION_DBUS_OBJECT_PATH,
541 APPLICATION_DBUS_INTERFACE,
544 if (!dbus_g_proxy_call (proxy, "OpenURI", &error,
546 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
547 G_TYPE_UINT, timestamp,
550 g_warning ("%s", error->message);
551 g_error_free (error);
553 g_object_unref (proxy);
555 g_warning ("Error creating proxy: %s\n", error->message);
556 g_error_free (error);
559 /* Do not continue opening this document */
569 ev_application_unregister_uri (EvApplication *application,
573 GError *error = NULL;
575 if (!application->connection)
578 proxy = dbus_g_proxy_new_for_name (application->connection,
579 "org.gnome.evince.Daemon",
580 "/org/gnome/evince/Daemon",
581 "org.gnome.evince.Daemon");
582 if (!dbus_g_proxy_call (proxy, "UnregisterDocument", &error,
586 g_warning ("Error unregistering document: %s\n", error->message);
587 g_error_free (error);
590 g_object_unref (proxy);
592 #endif /* ENABLE_DBUS */
595 ev_application_open_uri_in_window (EvApplication *application,
600 EvWindowRunMode mode,
601 const gchar *search_string,
605 ev_stock_icons_set_screen (screen);
606 gtk_window_set_screen (GTK_WINDOW (ev_window), screen);
609 /* We need to load uri before showing the window, so
610 we can restore window size without flickering */
611 ev_window_open_uri (ev_window, uri, dest, mode, search_string);
613 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (ev_window)))
614 gtk_widget_realize (GTK_WIDGET (ev_window));
616 #ifdef GDK_WINDOWING_X11
618 timestamp = gdk_x11_get_server_time (GTK_WIDGET (ev_window)->window);
619 gdk_x11_window_set_user_time (GTK_WIDGET (ev_window)->window, timestamp);
621 ev_document_fc_mutex_lock ();
622 gtk_window_present (GTK_WINDOW (ev_window));
623 ev_document_fc_mutex_unlock ();
625 ev_document_fc_mutex_lock ();
626 gtk_window_present_with_time (GTK_WINDOW (ev_window), timestamp);
627 ev_document_fc_mutex_unlock ();
628 #endif /* GDK_WINDOWING_X11 */
632 * ev_application_open_uri_at_dest:
633 * @application: The instance of the application.
634 * @uri: The uri to be opened.
635 * @screen: Thee screen where the link will be shown.
636 * @dest: The #EvLinkDest of the document.
637 * @mode: The run mode of the window.
638 * @timestamp: Current time value.
641 ev_application_open_uri_at_dest (EvApplication *application,
645 EvWindowRunMode mode,
646 const gchar *search_string,
651 g_return_if_fail (uri != NULL);
653 if (application->uri && strcmp (application->uri, uri) != 0) {
654 /* spawn a new evince process */
655 ev_spawn (uri, screen, dest, mode, search_string, timestamp);
659 GHashTable *args = build_args (screen, dest, mode, search_string);
662 /* Register the uri or send OpenURI to
663 * remote instance if already registered
665 ret = ev_application_register_uri (application, uri, args, timestamp);
666 g_hash_table_destroy (args);
669 #endif /* ENABLE_DBUS */
671 ev_window = ev_application_get_empty_window (application, screen);
673 ev_window = EV_WINDOW (ev_window_new ());
676 application->uri = g_strdup (uri);
678 ev_application_open_uri_in_window (application, uri, ev_window,
685 * ev_application_open_window:
686 * @application: The instance of the application.
687 * @timestamp: Current time value.
689 * Creates a new window
692 ev_application_open_window (EvApplication *application,
696 GtkWidget *new_window = ev_window_new ();
699 ev_stock_icons_set_screen (screen);
700 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
703 if (!GTK_WIDGET_REALIZED (new_window))
704 gtk_widget_realize (new_window);
706 #ifdef GDK_WINDOWING_X11
708 timestamp = gdk_x11_get_server_time (new_window->window);
709 gdk_x11_window_set_user_time (new_window->window, timestamp);
711 gtk_window_present (GTK_WINDOW (new_window));
713 gtk_window_present_with_time (GTK_WINDOW (new_window), timestamp);
714 #endif /* GDK_WINDOWING_X11 */
718 * ev_application_open_uri:
719 * @application: The instance of the application.
720 * @uri: The uri to be opened
721 * @args: A #GHashTable with the arguments data.
722 * @timestamp: Current time value.
723 * @error: The #GError facility.
726 ev_application_open_uri (EvApplication *application,
733 EvLinkDest *dest = NULL;
734 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
735 const gchar *search_string = NULL;
736 GdkScreen *screen = NULL;
738 g_assert (application->uri != NULL);
740 /* FIXME: we don't need uri anymore,
741 * maybe this method should be renamed
742 * as reload, refresh or something like that
744 if (!application->uri || strcmp (application->uri, uri)) {
745 g_warning ("Invalid uri: %s, expected %s\n",
746 uri, application->uri);
751 screen = get_screen_from_args (args);
752 dest = get_destination_from_args (args);
753 mode = get_window_run_mode_from_args (args);
754 search_string = get_find_string_from_args (args);
757 windows = ev_application_get_windows (application);
758 for (l = windows; l != NULL; l = g_list_next (l)) {
759 EvWindow *ev_window = EV_WINDOW (l->data);
761 ev_application_open_uri_in_window (application, uri, ev_window,
766 g_list_free (windows);
769 g_object_unref (dest);
775 ev_application_open_uri_list (EvApplication *application,
782 for (l = uri_list; l != NULL; l = l->next) {
783 ev_application_open_uri_at_dest (application, (char *)l->data,
784 screen, NULL, 0, NULL,
790 ev_application_shutdown (EvApplication *application)
792 if (application->uri) {
794 ev_application_unregister_uri (application,
797 g_free (application->uri);
798 application->uri = NULL;
801 if (application->accel_map_file) {
802 gtk_accel_map_save (application->accel_map_file);
803 g_free (application->accel_map_file);
804 application->accel_map_file = NULL;
808 if (application->keys) {
809 g_object_unref (application->keys);
810 application->keys = NULL;
812 #endif /* ENABLE_DBUS */
814 g_free (application->dot_dir);
815 application->dot_dir = NULL;
816 g_free (application->data_dir);
817 application->data_dir = NULL;
818 g_free (application->filechooser_open_uri);
819 application->filechooser_open_uri = NULL;
820 g_free (application->filechooser_save_uri);
821 application->filechooser_save_uri = NULL;
823 g_object_unref (application);
830 ev_application_class_init (EvApplicationClass *ev_application_class)
833 dbus_g_object_type_install_info (EV_TYPE_APPLICATION,
834 &dbus_glib_ev_application_object_info);
839 ev_application_init (EvApplication *ev_application)
841 const gchar *home_dir;
842 GError *error = NULL;
844 ev_application->dot_dir = g_build_filename (g_get_home_dir (),
849 /* FIXME: why make this fatal? */
850 if (!ev_dir_ensure_exists (ev_application->dot_dir, 0700))
857 dir = g_win32_get_package_installation_directory_of_module (NULL);
858 ev_application->data_dir = g_build_filename (dir, "share", "evince", NULL);
862 ev_application->data_dir = g_strdup (DATADIR);
865 ev_application_init_session (ev_application);
867 home_dir = g_get_home_dir ();
869 ev_application->accel_map_file = g_build_filename (home_dir,
874 gtk_accel_map_load (ev_application->accel_map_file);
878 ev_application->connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
879 if (ev_application->connection) {
880 dbus_g_connection_register_g_object (ev_application->connection,
881 APPLICATION_DBUS_OBJECT_PATH,
882 G_OBJECT (ev_application));
883 ev_application->scr_saver = totem_scrsaver_new (ev_application->connection);
885 g_warning ("Error connection to DBus: %s\n", error->message);
886 g_error_free (error);
888 ev_application->keys = ev_media_player_keys_new ();
889 #endif /* ENABLE_DBUS */
893 ev_application_has_window (EvApplication *application)
895 GList *windows = ev_application_get_windows (application);
896 gboolean retval = windows != NULL;
898 g_list_free (windows);
904 ev_application_get_uri (EvApplication *application)
906 return application->uri;
910 * ev_application_get_media_keys:
911 * @application: The instance of the application.
913 * It gives you access to the media player keys handler object.
915 * Returns: A #EvMediaPlayerKeys.
918 ev_application_get_media_keys (EvApplication *application)
921 return G_OBJECT (application->keys);
924 #endif /* ENABLE_DBUS */
928 ev_application_set_filechooser_uri (EvApplication *application,
929 GtkFileChooserAction action,
932 if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
933 g_free (application->filechooser_open_uri);
934 application->filechooser_open_uri = g_strdup (uri);
935 } else if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
936 g_free (application->filechooser_save_uri);
937 application->filechooser_save_uri = g_strdup (uri);
942 ev_application_get_filechooser_uri (EvApplication *application,
943 GtkFileChooserAction action)
945 if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
946 if (application->filechooser_open_uri)
947 return application->filechooser_open_uri;
948 } else if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
949 if (application->filechooser_save_uri)
950 return application->filechooser_save_uri;
957 ev_application_screensaver_enable (EvApplication *application)
959 if (application->scr_saver)
960 totem_scrsaver_enable (application->scr_saver);
964 ev_application_screensaver_disable (EvApplication *application)
966 if (application->scr_saver)
967 totem_scrsaver_disable (application->scr_saver);
971 ev_application_get_dot_dir (EvApplication *application)
973 return application->dot_dir;
977 ev_application_get_data_dir (EvApplication *application)
979 return application->data_dir;