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.
28 #include <glib/gi18n.h>
32 #include <libgnomeui/gnome-client.h>
35 #include "totem-scrsaver.h"
37 #include "ev-application.h"
38 #include "ev-document-factory.h"
39 #include "ev-file-helpers.h"
43 #include "ev-media-player-keys.h"
44 #endif /* ENABLE_DBUS */
47 #include <dbus/dbus-glib-bindings.h>
48 #include "ev-application-service.h"
51 static void ev_application_add_icon_path_for_screen (GdkScreen *screen);
53 struct _EvApplication {
54 GObject base_instance;
58 EggToolbarsModel *toolbars_model;
60 TotemScrsaver *scr_saver;
62 gchar *last_chooser_uri;
65 EvMediaPlayerKeys *keys;
66 #endif /* ENABLE_DBUS */
68 GtkPrintSettings *print_settings;
69 #if GTK_CHECK_VERSION (2, 11, 0)
70 gchar *print_settings_file;
74 struct _EvApplicationClass {
75 GObjectClass base_class;
78 G_DEFINE_TYPE (EvApplication, ev_application, G_TYPE_OBJECT);
80 #define APPLICATION_SERVICE_NAME "org.gnome.evince.ApplicationService"
84 ev_application_register_service (EvApplication *application)
86 static DBusGConnection *connection = NULL;
87 DBusGProxy *driver_proxy;
89 guint request_name_result;
92 g_warning ("Service already registered.");
96 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &err);
97 if (connection == NULL) {
98 g_warning ("Service registration failed.");
104 driver_proxy = dbus_g_proxy_new_for_name (connection,
107 DBUS_INTERFACE_DBUS);
109 if (!org_freedesktop_DBus_request_name (driver_proxy,
110 APPLICATION_SERVICE_NAME,
111 DBUS_NAME_FLAG_DO_NOT_QUEUE,
112 &request_name_result, &err)) {
113 g_warning ("Service registration failed.");
114 g_clear_error (&err);
117 g_object_unref (driver_proxy);
119 if (request_name_result == DBUS_REQUEST_NAME_REPLY_EXISTS) {
123 dbus_g_object_type_install_info (EV_TYPE_APPLICATION,
124 &dbus_glib_ev_application_object_info);
125 dbus_g_connection_register_g_object (connection,
126 "/org/gnome/evince/Evince",
127 G_OBJECT (application));
129 application->scr_saver = totem_scrsaver_new (connection);
133 #endif /* ENABLE_DBUS */
136 * ev_application_get_instance:
138 * Checks for #EvApplication instance, if it doesn't exist it does create it.
140 * Returns: an instance of the #EvApplication data.
143 ev_application_get_instance (void)
145 static EvApplication *instance;
148 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
156 removed_from_session (GnomeClient *client, EvApplication *application)
158 ev_application_shutdown (application);
162 save_session (GnomeClient *client, gint phase, GnomeSaveStyle save_style, gint shutdown,
163 GnomeInteractStyle interact_style, gint fast, EvApplication *application)
169 windows = ev_application_get_windows (application);
170 restart_argv = g_new (char *, g_list_length (windows) + 1);
171 restart_argv[argc++] = g_strdup ("evince");
173 for (l = windows; l != NULL; l = l->next) {
174 EvWindow *window = EV_WINDOW (l->data);
175 restart_argv[argc++] = g_strdup (ev_window_get_uri (window));
178 gnome_client_set_restart_command (client, argc, restart_argv);
180 for (k = 0; k < argc; k++) {
181 g_free (restart_argv[k]);
184 g_list_free (windows);
185 g_free (restart_argv);
191 init_session (EvApplication *application)
195 client = gnome_master_client ();
197 g_signal_connect (client, "save_yourself",
198 G_CALLBACK (save_session), application);
199 g_signal_connect (client, "die",
200 G_CALLBACK (removed_from_session), application);
205 * ev_display_open_if_needed:
206 * @name: the name of the display to be open if it's needed.
208 * Search among all the open displays if any of them have the same name as the
209 * passed name. If the display isn't found it tries the open it.
211 * Returns: a #GdkDisplay of the display with the passed name.
214 ev_display_open_if_needed (const gchar *name)
218 GdkDisplay *display = NULL;
220 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
222 for (l = displays; l != NULL; l = l->next) {
223 const gchar *display_name = gdk_display_get_name ((GdkDisplay *) l->data);
225 if (g_ascii_strcasecmp (display_name, name) == 0) {
231 g_slist_free (displays);
233 return display != NULL ? display : gdk_display_open (name);
237 * get_screen_from_args:
238 * @args: a #GHashTable with data passed to the application.
240 * Looks for the screen in the display available in the hash table passed to the
241 * application. If the display isn't opened, it's opened and the #GdkScreen
242 * assigned to the screen in that display returned.
244 * Returns: the #GdkScreen assigned to the screen on the display indicated by
245 * the data on the #GHashTable.
248 get_screen_from_args (GHashTable *args)
250 GValue *value = NULL;
251 GdkDisplay *display = NULL;
252 GdkScreen *screen = NULL;
254 g_assert (args != NULL);
256 value = g_hash_table_lookup (args, "display");
258 const gchar *display_name;
260 display_name = g_value_get_string (value);
261 display = ev_display_open_if_needed (display_name);
264 value = g_hash_table_lookup (args, "screen");
268 screen_number = g_value_get_int (value);
269 screen = gdk_display_get_screen (display, screen_number);
276 * get_window_run_mode_from_args:
277 * @args: a #GHashTable with data passed to the application.
279 * It does look if the mode option has been passed from command line, using it
280 * as the window run mode, otherwise the run mode will be the normal mode.
282 * Returns: The window run mode passed from command line or
283 * EV_WINDOW_MODE_NORMAL in other case.
285 static EvWindowRunMode
286 get_window_run_mode_from_args (GHashTable *args)
288 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
289 GValue *value = NULL;
291 g_assert (args != NULL);
293 value = g_hash_table_lookup (args, "mode");
295 mode = g_value_get_uint (value);
302 * get_destination_from_args:
303 * @args: a #GHashTable with data passed to the application.
305 * It does look for the page-label argument parsed from the command line and
306 * if it does exist, it returns an #EvLinkDest.
308 * Returns: An #EvLinkDest to page-label if it has been passed from the command
309 * line, NULL in other case.
312 get_destination_from_args (GHashTable *args)
314 EvLinkDest *dest = NULL;
315 GValue *value = NULL;
317 g_assert (args != NULL);
319 value = g_hash_table_lookup (args, "page-label");
321 const gchar *page_label;
323 page_label = g_value_get_string (value);
324 dest = ev_link_dest_new_page_label (page_label);
331 get_find_string_from_args (GHashTable *args)
333 GValue *value = NULL;
335 g_assert (args != NULL);
337 value = g_hash_table_lookup (args, "find-string");
339 return value ? g_value_get_string (value) : NULL;
343 * get_unlink_temp_file_from_args:
344 * @args: a #GHashTable with data passed to the application.
346 * It does look if the unlink-temp-file option has been passed from the command
347 * line returning it's boolean representation, otherwise it does return %FALSE.
349 * Returns: the boolean representation of the unlink-temp-file value or %FALSE
353 get_unlink_temp_file_from_args (GHashTable *args)
355 gboolean unlink_temp_file = FALSE;
356 GValue *value = NULL;
358 g_assert (args != NULL);
360 value = g_hash_table_lookup (args, "unlink-temp-file");
362 unlink_temp_file = g_value_get_boolean (value);
365 return unlink_temp_file;
369 get_print_settings_from_args (GHashTable *args)
371 const gchar *print_settings = NULL;
372 GValue *value = NULL;
374 g_assert (args != NULL);
376 value = g_hash_table_lookup (args, "print-settings");
378 print_settings = g_value_get_string (value);
381 return print_settings;
385 * ev_application_open_window:
386 * @application: The instance of the application.
387 * @args: A #GHashTable with the arguments data.
388 * @timestamp: Current time value.
389 * @error: The #GError facility.
391 * Creates a new window and if the args are available, it's not NULL, it gets
392 * the screen from them and assigns the just created window to it. At last it
398 ev_application_open_window (EvApplication *application,
403 GtkWidget *new_window = ev_window_new ();
404 GdkScreen *screen = NULL;
407 screen = get_screen_from_args (args);
411 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
413 ev_application_add_icon_path_for_screen (screen);
415 gtk_widget_show (new_window);
417 gtk_window_present_with_time (GTK_WINDOW (new_window),
423 * ev_application_get_empty_window:
424 * @application: The instance of the application.
425 * @screen: The screen where the empty window will be search.
427 * It does look if there is any empty window in the indicated screen.
429 * Returns: The first empty #EvWindow in the passed #GdkScreen or NULL in other
433 ev_application_get_empty_window (EvApplication *application,
436 EvWindow *empty_window = NULL;
437 GList *windows = ev_application_get_windows (application);
440 for (l = windows; l != NULL; l = l->next) {
441 EvWindow *window = EV_WINDOW (l->data);
443 if (ev_window_is_empty (window) &&
444 gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
445 empty_window = window;
450 g_list_free (windows);
456 * ev_application_get_uri_window:
457 * @application: The instance of the application.
458 * @uri: The uri to be opened.
460 * It looks in the list of the windows for the one with the document represented
461 * by the passed uri on it. If the window is empty or the document isn't present
462 * on any window, it will return NULL.
464 * Returns: The #EvWindow where the document represented by the passed uri is
465 * shown, NULL in other case.
468 ev_application_get_uri_window (EvApplication *application, const char *uri)
470 EvWindow *uri_window = NULL;
471 GList *windows = gtk_window_list_toplevels ();
474 g_return_val_if_fail (uri != NULL, NULL);
476 for (l = windows; l != NULL; l = l->next) {
477 if (EV_IS_WINDOW (l->data)) {
478 EvWindow *window = EV_WINDOW (l->data);
479 const char *window_uri = ev_window_get_uri (window);
481 if (window_uri && strcmp (window_uri, uri) == 0 && !ev_window_is_empty (window)) {
488 g_list_free (windows);
494 ev_application_add_icon_path_for_screen (GdkScreen *screen)
496 GtkIconTheme *icon_theme;
498 icon_theme = gtk_icon_theme_get_for_screen (screen);
503 gchar *ev_icons_path;
505 /* GtkIconTheme will then look in Evince custom hicolor dir
506 * for icons as well as the standard search paths
508 ev_icons_path = g_build_filename (DATADIR, "icons", NULL);
509 gtk_icon_theme_get_search_path (icon_theme, &path, &n_paths);
510 for (i = n_paths - 1; i >= 0; i--) {
511 if (g_ascii_strcasecmp (ev_icons_path, path[i]) == 0)
516 gtk_icon_theme_append_search_path (icon_theme,
519 g_free (ev_icons_path);
525 * ev_application_open_uri_at_dest:
526 * @application: The instance of the application.
527 * @uri: The uri to be opened.
528 * @screen: Thee screen where the link will be shown.
529 * @dest: The #EvLinkDest of the document.
530 * @mode: The run mode of the window.
531 * @unlink_temp_file: The unlink_temp_file option value.
532 * @timestamp: Current time value.
535 ev_application_open_uri_at_dest (EvApplication *application,
539 EvWindowRunMode mode,
540 const gchar *search_string,
541 gboolean unlink_temp_file,
542 const gchar *print_settings,
545 EvWindow *new_window;
547 g_return_if_fail (uri != NULL);
549 ev_application_add_icon_path_for_screen (screen);
551 new_window = ev_application_get_uri_window (application, uri);
553 if (new_window == NULL) {
554 new_window = ev_application_get_empty_window (application, screen);
557 if (new_window == NULL) {
558 new_window = EV_WINDOW (ev_window_new ());
562 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
564 /* We need to load uri before showing the window, so
565 we can restore window size without flickering */
566 ev_window_open_uri (new_window, uri, dest, mode, search_string,
567 unlink_temp_file, print_settings);
569 ev_document_fc_mutex_lock ();
570 gtk_widget_show (GTK_WIDGET (new_window));
571 ev_document_fc_mutex_unlock ();
573 gtk_window_present_with_time (GTK_WINDOW (new_window),
578 * ev_application_open_uri:
579 * @application: The instance of the application.
580 * @uri: The uri to be opened
581 * @args: A #GHashTable with the arguments data.
582 * @timestamp: Current time value.
583 * @error: The #GError facility.
586 ev_application_open_uri (EvApplication *application,
592 EvLinkDest *dest = NULL;
593 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
594 const gchar *search_string = NULL;
595 gboolean unlink_temp_file = FALSE;
596 const gchar *print_settings = NULL;
597 GdkScreen *screen = NULL;
600 screen = get_screen_from_args (args);
601 dest = get_destination_from_args (args);
602 mode = get_window_run_mode_from_args (args);
603 search_string = get_find_string_from_args (args);
604 unlink_temp_file = (mode == EV_WINDOW_MODE_PREVIEW &&
605 get_unlink_temp_file_from_args (args));
606 print_settings = get_print_settings_from_args (args);
609 ev_application_open_uri_at_dest (application, uri, screen,
610 dest, mode, search_string,
612 print_settings, timestamp);
615 g_object_unref (dest);
621 ev_application_open_uri_list (EvApplication *application,
628 for (l = uri_list; l != NULL; l = l->next) {
629 ev_application_open_uri_at_dest (application, (char *)l->data,
630 screen, NULL, 0, NULL,
631 FALSE, NULL, timestamp);
636 ev_application_shutdown (EvApplication *application)
638 if (application->toolbars_model) {
639 g_object_unref (application->toolbars_model);
640 g_free (application->toolbars_file);
641 application->toolbars_model = NULL;
642 application->toolbars_file = NULL;
645 #if GTK_CHECK_VERSION (2, 11, 0)
646 if (application->print_settings_file) {
647 if (application->print_settings) {
648 GError *error = NULL;
650 gtk_print_settings_to_file (application->print_settings,
651 application->print_settings_file,
654 g_warning ("%s", error->message);
655 g_error_free (error);
658 g_object_unref (application->print_settings);
659 application->print_settings = NULL;
662 g_free (application->print_settings_file);
663 application->print_settings_file = NULL;
665 #else /* ! GTK 2.11.0 */
666 if (application->print_settings) {
667 g_object_unref (application->print_settings);
668 application->print_settings = NULL;
670 #endif /* GTK 2.11.0 */
672 g_free (application->last_chooser_uri);
673 g_object_unref (application);
679 ev_application_class_init (EvApplicationClass *ev_application_class)
684 ev_application_init (EvApplication *ev_application)
689 init_session (ev_application);
692 ev_application->toolbars_model = egg_toolbars_model_new ();
694 ev_application->toolbars_file = g_build_filename
695 (ev_dot_dir (), "evince_toolbar.xml", NULL);
697 egg_toolbars_model_load_names (ev_application->toolbars_model,
698 DATADIR "/evince-toolbar.xml");
700 if (!egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
701 ev_application->toolbars_file)) {
702 egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
703 DATADIR"/evince-toolbar.xml");
706 /* Open item doesn't exist anymore,
707 * convert it to OpenRecent for compatibility
709 for (i = 0; i < egg_toolbars_model_n_items (ev_application->toolbars_model, 0); i++) {
712 item = egg_toolbars_model_item_nth (ev_application->toolbars_model, 0, i);
713 if (g_ascii_strcasecmp (item, "FileOpen") == 0) {
714 egg_toolbars_model_remove_item (ev_application->toolbars_model, 0, i);
715 egg_toolbars_model_add_item (ev_application->toolbars_model, 0, i,
717 ev_application_save_toolbars_model (ev_application);
722 egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
723 EGG_TB_MODEL_NOT_REMOVABLE);
726 ev_application->keys = ev_media_player_keys_new ();
727 #endif /* ENABLE_DBUS */
731 * ev_application_get_windows:
732 * @application: The instance of the application.
734 * It creates a list of the top level windows.
736 * Returns: A #GList of the top level windows.
739 ev_application_get_windows (EvApplication *application)
741 GList *l, *toplevels;
742 GList *windows = NULL;
744 toplevels = gtk_window_list_toplevels ();
746 for (l = toplevels; l != NULL; l = l->next) {
747 if (EV_IS_WINDOW (l->data)) {
748 windows = g_list_append (windows, l->data);
752 g_list_free (toplevels);
758 * ev_application_get_media_keys:
759 * @application: The instance of the application.
761 * It gives you access to the media player keys handler object.
763 * Returns: A #EvMediaPlayerKeys.
766 *ev_application_get_media_keys (EvApplication *application)
769 if (!application->keys)
771 return g_object_ref (G_OBJECT (application->keys));
774 #endif /* ENABLE_DBUS */
778 ev_application_get_toolbars_model (EvApplication *application)
780 return application->toolbars_model;
784 ev_application_save_toolbars_model (EvApplication *application)
786 egg_toolbars_model_save_toolbars (application->toolbars_model,
787 application->toolbars_file, "1.0");
791 ev_application_set_chooser_uri (EvApplication *application, const gchar *uri)
793 g_free (application->last_chooser_uri);
794 application->last_chooser_uri = g_strdup (uri);
798 ev_application_get_chooser_uri (EvApplication *application)
800 return application->last_chooser_uri;
804 ev_application_screensaver_enable (EvApplication *application)
806 if (application->scr_saver)
807 totem_scrsaver_enable (application->scr_saver);
811 ev_application_screensaver_disable (EvApplication *application)
813 if (application->scr_saver)
814 totem_scrsaver_disable (application->scr_saver);
818 ev_application_get_print_settings (EvApplication *application)
820 if (application->print_settings)
821 return application->print_settings;
823 #if GTK_CHECK_VERSION (2, 11, 0)
824 if (!application->print_settings_file) {
825 application->print_settings_file =
826 g_build_filename (ev_dot_dir (), "print-settings", NULL);
829 if (g_file_test (application->print_settings_file, G_FILE_TEST_IS_REGULAR)) {
830 GError *error = NULL;
832 application->print_settings =
833 gtk_print_settings_new_from_file (application->print_settings_file, &error);
836 g_warning ("%s", error->message);
837 g_error_free (error);
839 return application->print_settings;
842 #endif /* GTK 2.11.0 */
844 application->print_settings = gtk_print_settings_new ();
846 return application->print_settings;
850 ev_application_set_print_settings (EvApplication *application,
851 GtkPrintSettings *settings)
853 g_return_if_fail (GTK_IS_PRINT_SETTINGS (settings));
855 if (settings == application->print_settings)
858 if (application->print_settings)
859 g_object_unref (application->print_settings);
861 application->print_settings = g_object_ref (settings);