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.
26 #include <glib/gi18n.h>
30 #include <libgnomeui/gnome-client.h>
33 #include "ev-application.h"
35 #include "ev-file-helpers.h"
36 #include "ev-document-factory.h"
38 #include "ev-media-player-keys.h"
39 #endif /* ENABLE_DBUS */
40 #include "totem-scrsaver.h"
43 #include "ev-application-service.h"
44 #include <dbus/dbus-glib-bindings.h>
49 static void ev_application_add_icon_path_for_screen (GdkScreen *screen);
51 struct _EvApplication {
52 GObject base_instance;
56 EggToolbarsModel *toolbars_model;
58 TotemScrsaver *scr_saver;
60 gchar *last_chooser_uri;
63 EvMediaPlayerKeys *keys;
64 #endif /* ENABLE_DBUS */
66 GtkPrintSettings *print_settings;
67 #if GTK_CHECK_VERSION (2, 11, 0)
68 gchar *print_settings_file;
72 struct _EvApplicationClass {
73 GObjectClass base_class;
76 G_DEFINE_TYPE (EvApplication, ev_application, G_TYPE_OBJECT);
78 #define APPLICATION_SERVICE_NAME "org.gnome.evince.ApplicationService"
82 ev_application_register_service (EvApplication *application)
84 static DBusGConnection *connection = NULL;
85 DBusGProxy *driver_proxy;
87 guint request_name_result;
90 g_warning ("Service already registered.");
94 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &err);
95 if (connection == NULL) {
96 g_warning ("Service registration failed.");
102 driver_proxy = dbus_g_proxy_new_for_name (connection,
105 DBUS_INTERFACE_DBUS);
107 if (!org_freedesktop_DBus_request_name (driver_proxy,
108 APPLICATION_SERVICE_NAME,
109 DBUS_NAME_FLAG_DO_NOT_QUEUE,
110 &request_name_result, &err)) {
111 g_warning ("Service registration failed.");
112 g_clear_error (&err);
115 g_object_unref (driver_proxy);
117 if (request_name_result == DBUS_REQUEST_NAME_REPLY_EXISTS) {
121 dbus_g_object_type_install_info (EV_TYPE_APPLICATION,
122 &dbus_glib_ev_application_object_info);
123 dbus_g_connection_register_g_object (connection,
124 "/org/gnome/evince/Evince",
125 G_OBJECT (application));
127 application->scr_saver = totem_scrsaver_new (connection);
131 #endif /* ENABLE_DBUS */
134 * ev_application_get_instance:
136 * Checks for #EvApplication instance, if it doesn't exist it does create it.
138 * Returns: an instance of the #EvApplication data.
141 ev_application_get_instance (void)
143 static EvApplication *instance;
146 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
154 removed_from_session (GnomeClient *client, EvApplication *application)
156 ev_application_shutdown (application);
160 save_session (GnomeClient *client, gint phase, GnomeSaveStyle save_style, gint shutdown,
161 GnomeInteractStyle interact_style, gint fast, EvApplication *application)
167 windows = ev_application_get_windows (application);
168 restart_argv = g_new (char *, g_list_length (windows) + 1);
169 restart_argv[argc++] = g_strdup ("evince");
171 for (l = windows; l != NULL; l = l->next) {
172 EvWindow *window = EV_WINDOW (l->data);
173 restart_argv[argc++] = g_strdup (ev_window_get_uri (window));
176 gnome_client_set_restart_command (client, argc, restart_argv);
178 for (k = 0; k < argc; k++) {
179 g_free (restart_argv[k]);
182 g_list_free (windows);
183 g_free (restart_argv);
189 init_session (EvApplication *application)
193 client = gnome_master_client ();
195 g_signal_connect (client, "save_yourself",
196 G_CALLBACK (save_session), application);
197 g_signal_connect (client, "die",
198 G_CALLBACK (removed_from_session), application);
203 * ev_display_open_if_needed:
204 * @name: the name of the display to be open if it's needed.
206 * Search among all the open displays if any of them have the same name as the
207 * passed name. If the display isn't found it tries the open it.
209 * Returns: a #GdkDisplay of the display with the passed name.
212 ev_display_open_if_needed (const gchar *name)
216 GdkDisplay *display = NULL;
218 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
220 for (l = displays; l != NULL; l = l->next) {
221 const gchar *display_name = gdk_display_get_name ((GdkDisplay *) l->data);
223 if (g_ascii_strcasecmp (display_name, name) == 0) {
229 g_slist_free (displays);
231 return display != NULL ? display : gdk_display_open (name);
235 * get_screen_from_args:
236 * @args: a #GHashTable with data passed to the application.
238 * Looks for the screen in the display available in the hash table passed to the
239 * application. If the display isn't opened, it's opened and the #GdkScreen
240 * assigned to the screen in that display returned.
242 * Returns: the #GdkScreen assigned to the screen on the display indicated by
243 * the data on the #GHashTable.
246 get_screen_from_args (GHashTable *args)
248 GValue *value = NULL;
249 GdkDisplay *display = NULL;
250 GdkScreen *screen = NULL;
252 g_assert (args != NULL);
254 value = g_hash_table_lookup (args, "display");
256 const gchar *display_name;
258 display_name = g_value_get_string (value);
259 display = ev_display_open_if_needed (display_name);
262 value = g_hash_table_lookup (args, "screen");
266 screen_number = g_value_get_int (value);
267 screen = gdk_display_get_screen (display, screen_number);
274 * get_window_run_mode_from_args:
275 * @args: a #GHashTable with data passed to the application.
277 * It does look if the mode option has been passed from command line, using it
278 * as the window run mode, otherwise the run mode will be the normal mode.
280 * Returns: The window run mode passed from command line or
281 * EV_WINDOW_MODE_NORMAL in other case.
283 static EvWindowRunMode
284 get_window_run_mode_from_args (GHashTable *args)
286 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
287 GValue *value = NULL;
289 g_assert (args != NULL);
291 value = g_hash_table_lookup (args, "mode");
293 mode = g_value_get_uint (value);
300 * get_destination_from_args:
301 * @args: a #GHashTable with data passed to the application.
303 * It does look for the page-label argument parsed from the command line and
304 * if it does exist, it returns an #EvLinkDest.
306 * Returns: An #EvLinkDest to page-label if it has been passed from the command
307 * line, NULL in other case.
310 get_destination_from_args (GHashTable *args)
312 EvLinkDest *dest = NULL;
313 GValue *value = NULL;
315 g_assert (args != NULL);
317 value = g_hash_table_lookup (args, "page-label");
319 const gchar *page_label;
321 page_label = g_value_get_string (value);
322 dest = ev_link_dest_new_page_label (page_label);
329 get_find_string_from_args (GHashTable *args)
331 GValue *value = NULL;
333 g_assert (args != NULL);
335 value = g_hash_table_lookup (args, "find-string");
337 return value ? g_value_get_string (value) : NULL;
341 * get_unlink_temp_file_from_args:
342 * @args: a #GHashTable with data passed to the application.
344 * It does look if the unlink-temp-file option has been passed from the command
345 * line returning it's boolean representation, otherwise it does return %FALSE.
347 * Returns: the boolean representation of the unlink-temp-file value or %FALSE
351 get_unlink_temp_file_from_args (GHashTable *args)
353 gboolean unlink_temp_file = FALSE;
354 GValue *value = NULL;
356 g_assert (args != NULL);
358 value = g_hash_table_lookup (args, "unlink-temp-file");
360 unlink_temp_file = g_value_get_boolean (value);
363 return unlink_temp_file;
367 get_print_settings_from_args (GHashTable *args)
369 const gchar *print_settings = NULL;
370 GValue *value = NULL;
372 g_assert (args != NULL);
374 value = g_hash_table_lookup (args, "print-settings");
376 print_settings = g_value_get_string (value);
379 return print_settings;
383 * ev_application_open_window:
384 * @application: The instance of the application.
385 * @args: A #GHashTable with the arguments data.
386 * @timestamp: Current time value.
387 * @error: The #GError facility.
389 * Creates a new window and if the args are available, it's not NULL, it gets
390 * the screen from them and assigns the just created window to it. At last it
396 ev_application_open_window (EvApplication *application,
401 GtkWidget *new_window = ev_window_new ();
402 GdkScreen *screen = NULL;
405 screen = get_screen_from_args (args);
409 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
411 ev_application_add_icon_path_for_screen (screen);
413 gtk_widget_show (new_window);
415 gtk_window_present_with_time (GTK_WINDOW (new_window),
421 * ev_application_get_empty_window:
422 * @application: The instance of the application.
423 * @screen: The screen where the empty window will be search.
425 * It does look if there is any empty window in the indicated screen.
427 * Returns: The first empty #EvWindow in the passed #GdkScreen or NULL in other
431 ev_application_get_empty_window (EvApplication *application,
434 EvWindow *empty_window = NULL;
435 GList *windows = ev_application_get_windows (application);
438 for (l = windows; l != NULL; l = l->next) {
439 EvWindow *window = EV_WINDOW (l->data);
441 if (ev_window_is_empty (window) &&
442 gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
443 empty_window = window;
448 g_list_free (windows);
454 * ev_application_get_uri_window:
455 * @application: The instance of the application.
456 * @uri: The uri to be opened.
458 * It looks in the list of the windows for the one with the document represented
459 * by the passed uri on it. If the window is empty or the document isn't present
460 * on any window, it will return NULL.
462 * Returns: The #EvWindow where the document represented by the passed uri is
463 * shown, NULL in other case.
466 ev_application_get_uri_window (EvApplication *application, const char *uri)
468 EvWindow *uri_window = NULL;
469 GList *windows = gtk_window_list_toplevels ();
472 g_return_val_if_fail (uri != NULL, NULL);
474 for (l = windows; l != NULL; l = l->next) {
475 if (EV_IS_WINDOW (l->data)) {
476 EvWindow *window = EV_WINDOW (l->data);
477 const char *window_uri = ev_window_get_uri (window);
479 if (window_uri && strcmp (window_uri, uri) == 0 && !ev_window_is_empty (window)) {
486 g_list_free (windows);
492 ev_application_add_icon_path_for_screen (GdkScreen *screen)
494 GtkIconTheme *icon_theme;
496 icon_theme = gtk_icon_theme_get_for_screen (screen);
501 gchar *ev_icons_path;
503 /* GtkIconTheme will then look in Evince custom hicolor dir
504 * for icons as well as the standard search paths
506 ev_icons_path = g_build_filename (DATADIR, "icons", NULL);
507 gtk_icon_theme_get_search_path (icon_theme, &path, &n_paths);
508 for (i = n_paths - 1; i >= 0; i--) {
509 if (g_ascii_strcasecmp (ev_icons_path, path[i]) == 0)
514 gtk_icon_theme_append_search_path (icon_theme,
517 g_free (ev_icons_path);
523 * ev_application_open_uri_at_dest:
524 * @application: The instance of the application.
525 * @uri: The uri to be opened.
526 * @screen: Thee screen where the link will be shown.
527 * @dest: The #EvLinkDest of the document.
528 * @mode: The run mode of the window.
529 * @unlink_temp_file: The unlink_temp_file option value.
530 * @timestamp: Current time value.
533 ev_application_open_uri_at_dest (EvApplication *application,
537 EvWindowRunMode mode,
538 const gchar *search_string,
539 gboolean unlink_temp_file,
540 const gchar *print_settings,
543 EvWindow *new_window;
545 g_return_if_fail (uri != NULL);
547 ev_application_add_icon_path_for_screen (screen);
549 new_window = ev_application_get_uri_window (application, uri);
551 if (new_window == NULL) {
552 new_window = ev_application_get_empty_window (application, screen);
555 if (new_window == NULL) {
556 new_window = EV_WINDOW (ev_window_new ());
560 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
562 /* We need to load uri before showing the window, so
563 we can restore window size without flickering */
564 ev_window_open_uri (new_window, uri, dest, mode, search_string,
565 unlink_temp_file, print_settings);
567 ev_document_fc_mutex_lock ();
568 gtk_widget_show (GTK_WIDGET (new_window));
569 ev_document_fc_mutex_unlock ();
571 gtk_window_present_with_time (GTK_WINDOW (new_window),
576 * ev_application_open_uri:
577 * @application: The instance of the application.
578 * @uri: The uri to be opened
579 * @args: A #GHashTable with the arguments data.
580 * @timestamp: Current time value.
581 * @error: The #GError facility.
584 ev_application_open_uri (EvApplication *application,
590 EvLinkDest *dest = NULL;
591 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
592 const gchar *search_string = NULL;
593 gboolean unlink_temp_file = FALSE;
594 const gchar *print_settings = NULL;
595 GdkScreen *screen = NULL;
598 screen = get_screen_from_args (args);
599 dest = get_destination_from_args (args);
600 mode = get_window_run_mode_from_args (args);
601 search_string = get_find_string_from_args (args);
602 unlink_temp_file = (mode == EV_WINDOW_MODE_PREVIEW &&
603 get_unlink_temp_file_from_args (args));
604 print_settings = get_print_settings_from_args (args);
607 ev_application_open_uri_at_dest (application, uri, screen,
608 dest, mode, search_string,
610 print_settings, timestamp);
613 g_object_unref (dest);
619 ev_application_open_uri_list (EvApplication *application,
626 for (l = uri_list; l != NULL; l = l->next) {
627 ev_application_open_uri_at_dest (application, (char *)l->data,
628 screen, NULL, 0, NULL,
629 FALSE, NULL, timestamp);
634 ev_application_shutdown (EvApplication *application)
636 if (application->toolbars_model) {
637 g_object_unref (application->toolbars_model);
638 g_free (application->toolbars_file);
639 application->toolbars_model = NULL;
640 application->toolbars_file = NULL;
643 #if GTK_CHECK_VERSION (2, 11, 0)
644 if (application->print_settings_file) {
645 if (application->print_settings) {
646 GError *error = NULL;
648 gtk_print_settings_to_file (application->print_settings,
649 application->print_settings_file,
652 g_warning ("%s", error->message);
653 g_error_free (error);
656 g_object_unref (application->print_settings);
657 application->print_settings = NULL;
660 g_free (application->print_settings_file);
661 application->print_settings_file = NULL;
663 #else /* ! GTK 2.11.0 */
664 if (application->print_settings) {
665 g_object_unref (application->print_settings);
666 application->print_settings = NULL;
668 #endif /* GTK 2.11.0 */
670 g_free (application->last_chooser_uri);
671 g_object_unref (application);
677 ev_application_class_init (EvApplicationClass *ev_application_class)
682 ev_application_init (EvApplication *ev_application)
687 init_session (ev_application);
690 ev_application->toolbars_model = egg_toolbars_model_new ();
692 ev_application->toolbars_file = g_build_filename
693 (ev_dot_dir (), "evince_toolbar.xml", NULL);
695 egg_toolbars_model_load_names (ev_application->toolbars_model,
696 DATADIR "/evince-toolbar.xml");
698 if (!egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
699 ev_application->toolbars_file)) {
700 egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
701 DATADIR"/evince-toolbar.xml");
704 /* Open item doesn't exist anymore,
705 * convert it to OpenRecent for compatibility
707 for (i = 0; i < egg_toolbars_model_n_items (ev_application->toolbars_model, 0); i++) {
710 item = egg_toolbars_model_item_nth (ev_application->toolbars_model, 0, i);
711 if (g_ascii_strcasecmp (item, "FileOpen") == 0) {
712 egg_toolbars_model_remove_item (ev_application->toolbars_model, 0, i);
713 egg_toolbars_model_add_item (ev_application->toolbars_model, 0, i,
715 ev_application_save_toolbars_model (ev_application);
720 egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
721 EGG_TB_MODEL_NOT_REMOVABLE);
724 ev_application->keys = ev_media_player_keys_new ();
725 #endif /* ENABLE_DBUS */
729 * ev_application_get_windows:
730 * @application: The instance of the application.
732 * It creates a list of the top level windows.
734 * Returns: A #GList of the top level windows.
737 ev_application_get_windows (EvApplication *application)
739 GList *l, *toplevels;
740 GList *windows = NULL;
742 toplevels = gtk_window_list_toplevels ();
744 for (l = toplevels; l != NULL; l = l->next) {
745 if (EV_IS_WINDOW (l->data)) {
746 windows = g_list_append (windows, l->data);
750 g_list_free (toplevels);
756 * ev_application_get_media_keys:
757 * @application: The instance of the application.
759 * It gives you access to the media player keys handler object.
761 * Returns: A #EvMediaPlayerKeys.
764 *ev_application_get_media_keys (EvApplication *application)
767 if (!application->keys)
769 return g_object_ref (G_OBJECT (application->keys));
772 #endif /* ENABLE_DBUS */
776 ev_application_get_toolbars_model (EvApplication *application)
778 return application->toolbars_model;
782 ev_application_save_toolbars_model (EvApplication *application)
784 egg_toolbars_model_save_toolbars (application->toolbars_model,
785 application->toolbars_file, "1.0");
789 ev_application_set_chooser_uri (EvApplication *application, const gchar *uri)
791 g_free (application->last_chooser_uri);
792 application->last_chooser_uri = g_strdup (uri);
796 ev_application_get_chooser_uri (EvApplication *application)
798 return application->last_chooser_uri;
802 ev_application_screensaver_enable (EvApplication *application)
804 if (application->scr_saver)
805 totem_scrsaver_enable (application->scr_saver);
809 ev_application_screensaver_disable (EvApplication *application)
811 if (application->scr_saver)
812 totem_scrsaver_disable (application->scr_saver);
816 ev_application_get_print_settings (EvApplication *application)
818 if (application->print_settings)
819 return application->print_settings;
821 #if GTK_CHECK_VERSION (2, 11, 0)
822 if (!application->print_settings_file) {
823 application->print_settings_file =
824 g_build_filename (ev_dot_dir (), "print-settings", NULL);
827 if (g_file_test (application->print_settings_file, G_FILE_TEST_IS_REGULAR)) {
828 GError *error = NULL;
830 application->print_settings =
831 gtk_print_settings_new_from_file (application->print_settings_file, &error);
834 g_warning ("%s", error->message);
835 g_error_free (error);
837 return application->print_settings;
840 #endif /* GTK 2.11.0 */
842 application->print_settings = gtk_print_settings_new ();
844 return application->print_settings;
848 ev_application_set_print_settings (EvApplication *application,
849 GtkPrintSettings *settings)
851 g_return_if_fail (GTK_IS_PRINT_SETTINGS (settings));
853 if (settings == application->print_settings)
856 if (application->print_settings)
857 g_object_unref (application->print_settings);
859 application->print_settings = g_object_ref (settings);