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>
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"
98 * ev_application_get_instance:
100 * Checks for #EvApplication instance, if it doesn't exist it does create it.
102 * Returns: an instance of the #EvApplication data.
105 ev_application_get_instance (void)
108 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
116 ev_application_load_session (EvApplication *application)
118 GKeyFile *state_file;
122 if (egg_sm_client_is_resumed (application->smclient)) {
123 state_file = egg_sm_client_get_state_file (application->smclient);
127 #endif /* WITH_SMCLIENT */
130 uri = g_key_file_get_string (state_file, "Evince", "uri", NULL);
134 ev_application_open_uri_at_dest (application, uri,
135 gdk_screen_get_default (),
139 g_key_file_free (state_file);
147 smclient_save_state_cb (EggSMClient *client,
148 GKeyFile *state_file,
149 EvApplication *application)
151 if (!application->uri)
154 g_key_file_set_string (state_file, "Evince", "uri", application->uri);
158 smclient_quit_cb (EggSMClient *client,
159 EvApplication *application)
161 ev_application_shutdown (application);
163 #endif /* WITH_SMCLIENT */
166 ev_application_init_session (EvApplication *application)
169 application->smclient = egg_sm_client_get ();
170 g_signal_connect (application->smclient, "save_state",
171 G_CALLBACK (smclient_save_state_cb),
173 g_signal_connect (application->smclient, "quit",
174 G_CALLBACK (smclient_quit_cb),
180 * ev_display_open_if_needed:
181 * @name: the name of the display to be open if it's needed.
183 * Search among all the open displays if any of them have the same name as the
184 * passed name. If the display isn't found it tries the open it.
186 * Returns: a #GdkDisplay of the display with the passed name.
189 ev_display_open_if_needed (const gchar *name)
193 GdkDisplay *display = NULL;
195 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
197 for (l = displays; l != NULL; l = l->next) {
198 const gchar *display_name = gdk_display_get_name ((GdkDisplay *) l->data);
200 if (g_ascii_strcasecmp (display_name, name) == 0) {
206 g_slist_free (displays);
208 return display != NULL ? display : gdk_display_open (name);
212 * get_screen_from_args:
213 * @args: a #GHashTable with data passed to the application.
215 * Looks for the screen in the display available in the hash table passed to the
216 * application. If the display isn't opened, it's opened and the #GdkScreen
217 * assigned to the screen in that display returned.
219 * Returns: the #GdkScreen assigned to the screen on the display indicated by
220 * the data on the #GHashTable.
223 get_screen_from_args (GHashTable *args)
225 GValue *value = NULL;
226 GdkDisplay *display = NULL;
227 GdkScreen *screen = NULL;
229 g_assert (args != NULL);
231 value = g_hash_table_lookup (args, "display");
233 const gchar *display_name;
235 display_name = g_value_get_string (value);
236 display = ev_display_open_if_needed (display_name);
239 value = g_hash_table_lookup (args, "screen");
243 screen_number = g_value_get_int (value);
244 screen = gdk_display_get_screen (display, screen_number);
251 * get_window_run_mode_from_args:
252 * @args: a #GHashTable with data passed to the application.
254 * It does look if the mode option has been passed from command line, using it
255 * as the window run mode, otherwise the run mode will be the normal mode.
257 * Returns: The window run mode passed from command line or
258 * EV_WINDOW_MODE_NORMAL in other case.
260 static EvWindowRunMode
261 get_window_run_mode_from_args (GHashTable *args)
263 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
264 GValue *value = NULL;
266 g_assert (args != NULL);
268 value = g_hash_table_lookup (args, "mode");
270 mode = g_value_get_uint (value);
277 * get_destination_from_args:
278 * @args: a #GHashTable with data passed to the application.
280 * It does look for the page-label argument parsed from the command line and
281 * if it does exist, it returns an #EvLinkDest.
283 * Returns: An #EvLinkDest to page-label if it has been passed from the command
284 * line, NULL in other case.
287 get_destination_from_args (GHashTable *args)
289 EvLinkDest *dest = NULL;
290 GValue *value = NULL;
292 g_assert (args != NULL);
294 value = g_hash_table_lookup (args, "page-label");
296 const gchar *page_label;
298 page_label = g_value_get_string (value);
299 dest = ev_link_dest_new_page_label (page_label);
306 get_find_string_from_args (GHashTable *args)
308 GValue *value = NULL;
310 g_assert (args != NULL);
312 value = g_hash_table_lookup (args, "find-string");
314 return value ? g_value_get_string (value) : NULL;
318 value_free (GValue *value)
320 g_value_unset (value);
325 build_args (GdkScreen *screen,
327 EvWindowRunMode mode,
328 const gchar *search_string)
333 const gchar *display_name;
336 args = g_hash_table_new_full (g_str_hash,
338 (GDestroyNotify)g_free,
339 (GDestroyNotify)value_free);
342 display = gdk_screen_get_display (screen);
343 display_name = gdk_display_get_name (display);
344 value = g_new0 (GValue, 1);
345 g_value_init (value, G_TYPE_STRING);
346 g_value_set_string (value, display_name);
347 g_hash_table_insert (args, g_strdup ("display"), value);
350 screen_number = gdk_screen_get_number (screen);
351 value = g_new0 (GValue, 1);
352 g_value_init (value, G_TYPE_INT);
353 g_value_set_int (value, screen_number);
354 g_hash_table_insert (args, g_strdup ("screen"), value);
358 value = g_new0 (GValue, 1);
359 g_value_init (value, G_TYPE_STRING);
360 g_value_set_string (value, ev_link_dest_get_page_label (dest));
362 g_hash_table_insert (args, g_strdup ("page-label"), value);
367 value = g_new0 (GValue, 1);
368 g_value_init (value, G_TYPE_STRING);
369 g_value_set_string (value, search_string);
371 g_hash_table_insert (args, g_strdup ("find-string"), value);
375 if (mode != EV_WINDOW_MODE_NORMAL) {
376 value = g_new0 (GValue, 1);
377 g_value_init (value, G_TYPE_UINT);
378 g_value_set_uint (value, mode);
380 g_hash_table_insert (args, g_strdup ("mode"), value);
387 child_setup (gpointer user_data)
391 startup_id = g_strdup_printf ("_TIME%lu",
392 (unsigned long)GPOINTER_TO_INT (user_data));
393 g_setenv ("DESKTOP_STARTUP_ID", startup_id, TRUE);
398 ev_spawn (const char *uri,
401 EvWindowRunMode mode,
402 const gchar *search_string,
409 GError *error = NULL;
415 dir = g_win32_get_package_installation_directory_of_module (NULL);
416 argv[arg++] = g_build_filename (dir, "bin", "evince", NULL);
420 argv[arg++] = g_build_filename (BINDIR, "evince", NULL);
425 const gchar *page_label;
427 page_label = ev_link_dest_get_page_label (dest);
429 argv[arg++] = g_strdup_printf ("--page-label=%s", page_label);
431 argv[arg++] = g_strdup_printf ("--page-label=%d",
432 ev_link_dest_get_page (dest));
437 argv[arg++] = g_strdup_printf ("--find=%s", search_string);
442 case EV_WINDOW_MODE_FULLSCREEN:
443 argv[arg++] = g_strdup ("-f");
445 case EV_WINDOW_MODE_PRESENTATION:
446 argv[arg++] = g_strdup ("-s");
452 argv[arg++] = (gchar *)uri;
455 res = gdk_spawn_on_screen (screen, NULL /* wd */, argv, NULL /* env */,
458 GINT_TO_POINTER(timestamp),
461 g_warning ("Error launching evince %s: %s\n", uri, error->message);
462 g_error_free (error);
465 for (i = 0; i < arg - 1; i++) {
471 ev_application_get_windows (EvApplication *application)
473 GList *l, *toplevels;
474 GList *windows = NULL;
476 toplevels = gtk_window_list_toplevels ();
478 for (l = toplevels; l != NULL; l = l->next) {
479 if (EV_IS_WINDOW (l->data)) {
480 windows = g_list_append (windows, l->data);
484 g_list_free (toplevels);
490 ev_application_get_empty_window (EvApplication *application,
493 EvWindow *empty_window = NULL;
494 GList *windows = ev_application_get_windows (application);
497 for (l = windows; l != NULL; l = l->next) {
498 EvWindow *window = EV_WINDOW (l->data);
500 if (ev_window_is_empty (window) &&
501 gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
502 empty_window = window;
507 g_list_free (windows);
515 ev_application_register_uri (EvApplication *application,
522 gboolean retval = TRUE;
523 GError *error = NULL;
525 if (!application->connection)
528 proxy = dbus_g_proxy_new_for_name (application->connection,
529 "org.gnome.evince.Daemon",
530 "/org/gnome/evince/Daemon",
531 "org.gnome.evince.Daemon");
532 if (!dbus_g_proxy_call (proxy, "RegisterDocument", &error,
535 G_TYPE_STRING, &owner,
537 g_warning ("Error registering document: %s\n", error->message);
538 g_error_free (error);
539 g_object_unref (proxy);
543 g_object_unref (proxy);
546 /* Already registered */
547 proxy = dbus_g_proxy_new_for_name_owner (application->connection,
549 APPLICATION_DBUS_OBJECT_PATH,
550 APPLICATION_DBUS_INTERFACE,
553 if (!dbus_g_proxy_call (proxy, "OpenURI", &error,
555 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
556 G_TYPE_UINT, timestamp,
559 g_warning ("%s", error->message);
560 g_error_free (error);
562 g_object_unref (proxy);
564 g_warning ("Error creating proxy: %s\n", error->message);
565 g_error_free (error);
568 /* Do not continue opening this document */
578 ev_application_unregister_uri (EvApplication *application,
582 GError *error = NULL;
584 if (!application->connection)
587 proxy = dbus_g_proxy_new_for_name (application->connection,
588 "org.gnome.evince.Daemon",
589 "/org/gnome/evince/Daemon",
590 "org.gnome.evince.Daemon");
591 if (!dbus_g_proxy_call (proxy, "UnregisterDocument", &error,
595 g_warning ("Error unregistering document: %s\n", error->message);
596 g_error_free (error);
599 g_object_unref (proxy);
601 #endif /* ENABLE_DBUS */
604 ev_application_open_uri_in_window (EvApplication *application,
609 EvWindowRunMode mode,
610 const gchar *search_string,
614 ev_stock_icons_set_screen (screen);
615 gtk_window_set_screen (GTK_WINDOW (ev_window), screen);
618 /* We need to load uri before showing the window, so
619 we can restore window size without flickering */
620 ev_window_open_uri (ev_window, uri, dest, mode, search_string);
622 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (ev_window)))
623 gtk_widget_realize (GTK_WIDGET (ev_window));
625 #ifdef GDK_WINDOWING_X11
627 timestamp = gdk_x11_get_server_time (GTK_WIDGET (ev_window)->window);
628 gdk_x11_window_set_user_time (GTK_WIDGET (ev_window)->window, timestamp);
630 ev_document_fc_mutex_lock ();
631 gtk_window_present (GTK_WINDOW (ev_window));
632 ev_document_fc_mutex_unlock ();
634 ev_document_fc_mutex_lock ();
635 gtk_window_present_with_time (GTK_WINDOW (ev_window), timestamp);
636 ev_document_fc_mutex_unlock ();
637 #endif /* GDK_WINDOWING_X11 */
641 * ev_application_open_uri_at_dest:
642 * @application: The instance of the application.
643 * @uri: The uri to be opened.
644 * @screen: Thee screen where the link will be shown.
645 * @dest: The #EvLinkDest of the document.
646 * @mode: The run mode of the window.
647 * @timestamp: Current time value.
650 ev_application_open_uri_at_dest (EvApplication *application,
654 EvWindowRunMode mode,
655 const gchar *search_string,
660 g_return_if_fail (uri != NULL);
662 if (application->uri && strcmp (application->uri, uri) != 0) {
663 /* spawn a new evince process */
664 ev_spawn (uri, screen, dest, mode, search_string, timestamp);
668 GHashTable *args = build_args (screen, dest, mode, search_string);
671 /* Register the uri or send OpenURI to
672 * remote instance if already registered
674 ret = ev_application_register_uri (application, uri, args, timestamp);
675 g_hash_table_destroy (args);
678 #endif /* ENABLE_DBUS */
680 ev_window = ev_application_get_empty_window (application, screen);
682 ev_window = EV_WINDOW (ev_window_new ());
685 application->uri = g_strdup (uri);
687 ev_application_open_uri_in_window (application, uri, ev_window,
694 * ev_application_open_window:
695 * @application: The instance of the application.
696 * @timestamp: Current time value.
698 * Creates a new window
701 ev_application_open_window (EvApplication *application,
705 GtkWidget *new_window = ev_window_new ();
708 ev_stock_icons_set_screen (screen);
709 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
712 if (!GTK_WIDGET_REALIZED (new_window))
713 gtk_widget_realize (new_window);
715 #ifdef GDK_WINDOWING_X11
717 timestamp = gdk_x11_get_server_time (new_window->window);
718 gdk_x11_window_set_user_time (new_window->window, timestamp);
720 gtk_window_present (GTK_WINDOW (new_window));
722 gtk_window_present_with_time (GTK_WINDOW (new_window), timestamp);
723 #endif /* GDK_WINDOWING_X11 */
727 * ev_application_open_uri:
728 * @application: The instance of the application.
729 * @uri: The uri to be opened
730 * @args: A #GHashTable with the arguments data.
731 * @timestamp: Current time value.
732 * @error: The #GError facility.
735 ev_application_open_uri (EvApplication *application,
742 EvLinkDest *dest = NULL;
743 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
744 const gchar *search_string = NULL;
745 GdkScreen *screen = NULL;
747 g_assert (application->uri != NULL);
749 /* FIXME: we don't need uri anymore,
750 * maybe this method should be renamed
751 * as reload, refresh or something like that
753 if (!application->uri || strcmp (application->uri, uri)) {
754 g_warning ("Invalid uri: %s, expected %s\n",
755 uri, application->uri);
760 screen = get_screen_from_args (args);
761 dest = get_destination_from_args (args);
762 mode = get_window_run_mode_from_args (args);
763 search_string = get_find_string_from_args (args);
766 windows = ev_application_get_windows (application);
767 for (l = windows; l != NULL; l = g_list_next (l)) {
768 EvWindow *ev_window = EV_WINDOW (l->data);
770 ev_application_open_uri_in_window (application, uri, ev_window,
775 g_list_free (windows);
778 g_object_unref (dest);
784 ev_application_open_uri_list (EvApplication *application,
791 for (l = uri_list; l != NULL; l = l->next) {
792 ev_application_open_uri_at_dest (application, (char *)l->data,
793 screen, NULL, 0, NULL,
799 ev_application_accel_map_save (EvApplication *application)
801 gchar *accel_map_file;
805 accel_map_file = g_build_filename (g_get_home_dir (),
809 tmp_filename = g_strdup_printf ("%s.XXXXXX", accel_map_file);
811 fd = g_mkstemp (tmp_filename);
813 g_free (accel_map_file);
814 g_free (tmp_filename);
818 gtk_accel_map_save_fd (fd);
821 if (g_rename (tmp_filename, accel_map_file) == -1) {
823 g_unlink (tmp_filename);
826 g_free (accel_map_file);
827 g_free (tmp_filename);
831 ev_application_accel_map_load (EvApplication *application)
833 gchar *accel_map_file;
835 accel_map_file = g_build_filename (g_get_home_dir (),
838 gtk_accel_map_load (accel_map_file);
839 g_free (accel_map_file);
843 ev_application_shutdown (EvApplication *application)
845 if (application->uri) {
847 ev_application_unregister_uri (application,
850 g_free (application->uri);
851 application->uri = NULL;
854 ev_application_accel_map_save (application);
856 g_object_unref (application->scr_saver);
857 application->scr_saver = NULL;
860 if (application->keys) {
861 g_object_unref (application->keys);
862 application->keys = NULL;
864 #endif /* ENABLE_DBUS */
866 g_free (application->dot_dir);
867 application->dot_dir = NULL;
868 g_free (application->data_dir);
869 application->data_dir = NULL;
870 g_free (application->filechooser_open_uri);
871 application->filechooser_open_uri = NULL;
872 g_free (application->filechooser_save_uri);
873 application->filechooser_save_uri = NULL;
875 g_object_unref (application);
882 ev_application_class_init (EvApplicationClass *ev_application_class)
885 dbus_g_object_type_install_info (EV_TYPE_APPLICATION,
886 &dbus_glib_ev_application_object_info);
891 ev_application_init (EvApplication *ev_application)
893 GError *error = NULL;
895 ev_application->dot_dir = g_build_filename (g_get_home_dir (),
904 dir = g_win32_get_package_installation_directory_of_module (NULL);
905 ev_application->data_dir = g_build_filename (dir, "share", "evince", NULL);
909 ev_application->data_dir = g_strdup (DATADIR);
912 ev_application_init_session (ev_application);
914 ev_application_accel_map_load (ev_application);
916 ev_application->scr_saver = totem_scrsaver_new ();
919 ev_application->connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
920 if (ev_application->connection) {
921 dbus_g_connection_register_g_object (ev_application->connection,
922 APPLICATION_DBUS_OBJECT_PATH,
923 G_OBJECT (ev_application));
925 g_warning ("Error connection to DBus: %s\n", error->message);
926 g_error_free (error);
928 ev_application->keys = ev_media_player_keys_new ();
929 #endif /* ENABLE_DBUS */
933 ev_application_has_window (EvApplication *application)
935 GList *windows = ev_application_get_windows (application);
936 gboolean retval = windows != NULL;
938 g_list_free (windows);
944 ev_application_get_uri (EvApplication *application)
946 return application->uri;
950 * ev_application_get_media_keys:
951 * @application: The instance of the application.
953 * It gives you access to the media player keys handler object.
955 * Returns: A #EvMediaPlayerKeys.
958 ev_application_get_media_keys (EvApplication *application)
961 return G_OBJECT (application->keys);
964 #endif /* ENABLE_DBUS */
968 ev_application_set_filechooser_uri (EvApplication *application,
969 GtkFileChooserAction action,
972 if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
973 g_free (application->filechooser_open_uri);
974 application->filechooser_open_uri = g_strdup (uri);
975 } else if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
976 g_free (application->filechooser_save_uri);
977 application->filechooser_save_uri = g_strdup (uri);
982 ev_application_get_filechooser_uri (EvApplication *application,
983 GtkFileChooserAction action)
985 if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
986 if (application->filechooser_open_uri)
987 return application->filechooser_open_uri;
988 } else if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
989 if (application->filechooser_save_uri)
990 return application->filechooser_save_uri;
997 ev_application_screensaver_enable (EvApplication *application)
999 totem_scrsaver_enable (application->scr_saver);
1003 ev_application_screensaver_disable (EvApplication *application)
1005 totem_scrsaver_disable (application->scr_saver);
1009 ev_application_get_dot_dir (EvApplication *application,
1013 g_mkdir_with_parents (application->dot_dir, 0700);
1015 return application->dot_dir;
1019 ev_application_get_data_dir (EvApplication *application)
1021 return application->data_dir;