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.
24 #include "ev-application.h"
26 #include "ev-file-helpers.h"
27 #include "ev-document-factory.h"
29 #include "ev-media-player-keys.h"
30 #endif /* ENABLE_DBUS */
31 #include "totem-scrsaver.h"
34 #include <glib/gi18n.h>
35 #include <glib-object.h>
36 #include <gtk/gtkfilechooserdialog.h>
37 #include <gtk/gtkstock.h>
38 #include <gtk/gtkwidget.h>
39 #include <gtk/gtkmain.h>
42 #include <libgnomeui/gnome-client.h>
46 #include "ev-application-service.h"
47 #include <dbus/dbus-glib-bindings.h>
52 static void ev_application_add_icon_path_for_screen (GdkScreen *screen);
54 struct _EvApplication {
55 GObject base_instance;
59 EggToolbarsModel *toolbars_model;
61 TotemScrsaver *scr_saver;
63 gchar *last_chooser_uri;
66 EvMediaPlayerKeys *keys;
67 #endif /* ENABLE_DBUS */
69 GtkPrintSettings *print_settings;
70 #if GTK_CHECK_VERSION (2, 11, 0)
71 gchar *print_settings_file;
75 struct _EvApplicationClass {
76 GObjectClass base_class;
79 G_DEFINE_TYPE (EvApplication, ev_application, G_TYPE_OBJECT);
81 #define APPLICATION_SERVICE_NAME "org.gnome.evince.ApplicationService"
85 ev_application_register_service (EvApplication *application)
87 static DBusGConnection *connection = NULL;
88 DBusGProxy *driver_proxy;
90 guint request_name_result;
93 g_warning ("Service already registered.");
97 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &err);
98 if (connection == NULL) {
99 g_warning ("Service registration failed.");
105 driver_proxy = dbus_g_proxy_new_for_name (connection,
108 DBUS_INTERFACE_DBUS);
110 if (!org_freedesktop_DBus_request_name (driver_proxy,
111 APPLICATION_SERVICE_NAME,
112 DBUS_NAME_FLAG_DO_NOT_QUEUE,
113 &request_name_result, &err)) {
114 g_warning ("Service registration failed.");
115 g_clear_error (&err);
118 g_object_unref (driver_proxy);
120 if (request_name_result == DBUS_REQUEST_NAME_REPLY_EXISTS) {
124 dbus_g_object_type_install_info (EV_TYPE_APPLICATION,
125 &dbus_glib_ev_application_object_info);
126 dbus_g_connection_register_g_object (connection,
127 "/org/gnome/evince/Evince",
128 G_OBJECT (application));
130 application->scr_saver = totem_scrsaver_new (connection);
134 #endif /* ENABLE_DBUS */
137 * ev_application_get_instance:
139 * Checks for #EvApplication instance, if it doesn't exist it does create it.
141 * Returns: an instance of the #EvApplication data.
144 ev_application_get_instance (void)
146 static EvApplication *instance;
149 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
157 removed_from_session (GnomeClient *client, EvApplication *application)
159 ev_application_shutdown (application);
163 save_session (GnomeClient *client, gint phase, GnomeSaveStyle save_style, gint shutdown,
164 GnomeInteractStyle interact_style, gint fast, EvApplication *application)
170 windows = ev_application_get_windows (application);
171 restart_argv = g_new (char *, g_list_length (windows) + 1);
172 restart_argv[argc++] = g_strdup ("evince");
174 for (l = windows; l != NULL; l = l->next) {
175 EvWindow *window = EV_WINDOW (l->data);
176 restart_argv[argc++] = g_strdup (ev_window_get_uri (window));
179 gnome_client_set_restart_command (client, argc, restart_argv);
181 for (k = 0; k < argc; k++) {
182 g_free (restart_argv[k]);
185 g_list_free (windows);
186 g_free (restart_argv);
192 init_session (EvApplication *application)
196 client = gnome_master_client ();
198 g_signal_connect (client, "save_yourself",
199 G_CALLBACK (save_session), application);
200 g_signal_connect (client, "die",
201 G_CALLBACK (removed_from_session), application);
206 * ev_display_open_if_needed:
207 * @name: the name of the display to be open if it's needed.
209 * Search among all the open displays if any of them have the same name as the
210 * passed name. If the display isn't found it tries the open it.
212 * Returns: a #GdkDisplay of the display with the passed name.
215 ev_display_open_if_needed (const gchar *name)
219 GdkDisplay *display = NULL;
221 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
223 for (l = displays; l != NULL; l = l->next) {
224 const gchar *display_name = gdk_display_get_name ((GdkDisplay *) l->data);
226 if (g_ascii_strcasecmp (display_name, name) == 0) {
232 g_slist_free (displays);
234 return display != NULL ? display : gdk_display_open (name);
238 * get_screen_from_args:
239 * @args: a #GHashTable with data passed to the application.
241 * Looks for the screen in the display available in the hash table passed to the
242 * application. If the display isn't opened, it's opened and the #GdkScreen
243 * assigned to the screen in that display returned.
245 * Returns: the #GdkScreen assigned to the screen on the display indicated by
246 * the data on the #GHashTable.
249 get_screen_from_args (GHashTable *args)
251 GValue *value = NULL;
252 GdkDisplay *display = NULL;
253 GdkScreen *screen = NULL;
255 g_assert (args != NULL);
257 value = g_hash_table_lookup (args, "display");
259 const gchar *display_name;
261 display_name = g_value_get_string (value);
262 display = ev_display_open_if_needed (display_name);
265 value = g_hash_table_lookup (args, "screen");
269 screen_number = g_value_get_int (value);
270 screen = gdk_display_get_screen (display, screen_number);
277 * get_window_run_mode_from_args:
278 * @args: a #GHashTable with data passed to the application.
280 * It does look if the mode option has been passed from command line, using it
281 * as the window run mode, otherwise the run mode will be the normal mode.
283 * Returns: The window run mode passed from command line or
284 * EV_WINDOW_MODE_NORMAL in other case.
286 static EvWindowRunMode
287 get_window_run_mode_from_args (GHashTable *args)
289 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
290 GValue *value = NULL;
292 g_assert (args != NULL);
294 value = g_hash_table_lookup (args, "mode");
296 mode = g_value_get_uint (value);
303 * get_destination_from_args:
304 * @args: a #GHashTable with data passed to the application.
306 * It does look for the page-label argument parsed from the command line and
307 * if it does exist, it returns an #EvLinkDest.
309 * Returns: An #EvLinkDest to page-label if it has been passed from the command
310 * line, NULL in other case.
313 get_destination_from_args (GHashTable *args)
315 EvLinkDest *dest = NULL;
316 GValue *value = NULL;
318 g_assert (args != NULL);
320 value = g_hash_table_lookup (args, "page-label");
322 const gchar *page_label;
324 page_label = g_value_get_string (value);
325 dest = ev_link_dest_new_page_label (page_label);
332 get_find_string_from_args (GHashTable *args)
334 GValue *value = NULL;
336 g_assert (args != NULL);
338 value = g_hash_table_lookup (args, "find-string");
340 return value ? g_value_get_string (value) : NULL;
344 * get_unlink_temp_file_from_args:
345 * @args: a #GHashTable with data passed to the application.
347 * It does look if the unlink-temp-file option has been passed from the command
348 * line returning it's boolean representation, otherwise it does return %FALSE.
350 * Returns: the boolean representation of the unlink-temp-file value or %FALSE
354 get_unlink_temp_file_from_args (GHashTable *args)
356 gboolean unlink_temp_file = FALSE;
357 GValue *value = NULL;
359 g_assert (args != NULL);
361 value = g_hash_table_lookup (args, "unlink-temp-file");
363 unlink_temp_file = g_value_get_boolean (value);
366 return unlink_temp_file;
370 get_print_settings_from_args (GHashTable *args)
372 const gchar *print_settings = NULL;
373 GValue *value = NULL;
375 g_assert (args != NULL);
377 value = g_hash_table_lookup (args, "print-settings");
379 print_settings = g_value_get_string (value);
382 return print_settings;
386 * ev_application_open_window:
387 * @application: The instance of the application.
388 * @args: A #GHashTable with the arguments data.
389 * @timestamp: Current time value.
390 * @error: The #GError facility.
392 * Creates a new window and if the args are available, it's not NULL, it gets
393 * the screen from them and assigns the just created window to it. At last it
399 ev_application_open_window (EvApplication *application,
404 GtkWidget *new_window = ev_window_new ();
405 GdkScreen *screen = NULL;
408 screen = get_screen_from_args (args);
412 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
414 ev_application_add_icon_path_for_screen (screen);
416 gtk_widget_show (new_window);
418 gtk_window_present_with_time (GTK_WINDOW (new_window),
424 * ev_application_get_empty_window:
425 * @application: The instance of the application.
426 * @screen: The screen where the empty window will be search.
428 * It does look if there is any empty window in the indicated screen.
430 * Returns: The first empty #EvWindow in the passed #GdkScreen or NULL in other
434 ev_application_get_empty_window (EvApplication *application,
437 EvWindow *empty_window = NULL;
438 GList *windows = ev_application_get_windows (application);
441 for (l = windows; l != NULL; l = l->next) {
442 EvWindow *window = EV_WINDOW (l->data);
444 if (ev_window_is_empty (window) &&
445 gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
446 empty_window = window;
451 g_list_free (windows);
457 * ev_application_get_uri_window:
458 * @application: The instance of the application.
459 * @uri: The uri to be opened.
461 * It looks in the list of the windows for the one with the document represented
462 * by the passed uri on it. If the window is empty or the document isn't present
463 * on any window, it will return NULL.
465 * Returns: The #EvWindow where the document represented by the passed uri is
466 * shown, NULL in other case.
469 ev_application_get_uri_window (EvApplication *application, const char *uri)
471 EvWindow *uri_window = NULL;
472 GList *windows = gtk_window_list_toplevels ();
475 g_return_val_if_fail (uri != NULL, NULL);
477 for (l = windows; l != NULL; l = l->next) {
478 if (EV_IS_WINDOW (l->data)) {
479 EvWindow *window = EV_WINDOW (l->data);
480 const char *window_uri = ev_window_get_uri (window);
482 if (window_uri && strcmp (window_uri, uri) == 0 && !ev_window_is_empty (window)) {
489 g_list_free (windows);
495 ev_application_add_icon_path_for_screen (GdkScreen *screen)
497 GtkIconTheme *icon_theme;
499 icon_theme = gtk_icon_theme_get_for_screen (screen);
504 gchar *ev_icons_path;
506 /* GtkIconTheme will then look in Evince custom hicolor dir
507 * for icons as well as the standard search paths
509 ev_icons_path = g_build_filename (DATADIR, "icons", NULL);
510 gtk_icon_theme_get_search_path (icon_theme, &path, &n_paths);
511 for (i = n_paths - 1; i >= 0; i--) {
512 if (g_ascii_strcasecmp (ev_icons_path, path[i]) == 0)
517 gtk_icon_theme_append_search_path (icon_theme,
520 g_free (ev_icons_path);
526 * ev_application_open_uri_at_dest:
527 * @application: The instance of the application.
528 * @uri: The uri to be opened.
529 * @screen: Thee screen where the link will be shown.
530 * @dest: The #EvLinkDest of the document.
531 * @mode: The run mode of the window.
532 * @unlink_temp_file: The unlink_temp_file option value.
533 * @timestamp: Current time value.
536 ev_application_open_uri_at_dest (EvApplication *application,
540 EvWindowRunMode mode,
541 const gchar *search_string,
542 gboolean unlink_temp_file,
543 const gchar *print_settings,
546 EvWindow *new_window;
548 g_return_if_fail (uri != NULL);
550 ev_application_add_icon_path_for_screen (screen);
552 new_window = ev_application_get_uri_window (application, uri);
554 if (new_window == NULL) {
555 new_window = ev_application_get_empty_window (application, screen);
558 if (new_window == NULL) {
559 new_window = EV_WINDOW (ev_window_new ());
563 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
565 /* We need to load uri before showing the window, so
566 we can restore window size without flickering */
567 ev_window_open_uri (new_window, uri, dest, mode, search_string,
568 unlink_temp_file, print_settings);
570 ev_document_fc_mutex_lock ();
571 gtk_widget_show (GTK_WIDGET (new_window));
572 ev_document_fc_mutex_unlock ();
574 gtk_window_present_with_time (GTK_WINDOW (new_window),
579 * ev_application_open_uri:
580 * @application: The instance of the application.
581 * @uri: The uri to be opened
582 * @args: A #GHashTable with the arguments data.
583 * @timestamp: Current time value.
584 * @error: The #GError facility.
587 ev_application_open_uri (EvApplication *application,
593 EvLinkDest *dest = NULL;
594 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
595 const gchar *search_string = NULL;
596 gboolean unlink_temp_file = FALSE;
597 const gchar *print_settings = NULL;
598 GdkScreen *screen = NULL;
601 screen = get_screen_from_args (args);
602 dest = get_destination_from_args (args);
603 mode = get_window_run_mode_from_args (args);
604 search_string = get_find_string_from_args (args);
605 unlink_temp_file = (mode == EV_WINDOW_MODE_PREVIEW &&
606 get_unlink_temp_file_from_args (args));
607 print_settings = get_print_settings_from_args (args);
610 ev_application_open_uri_at_dest (application, uri, screen,
611 dest, mode, search_string,
613 print_settings, timestamp);
616 g_object_unref (dest);
622 ev_application_open_uri_list (EvApplication *application,
629 for (l = uri_list; l != NULL; l = l->next) {
630 ev_application_open_uri_at_dest (application, (char *)l->data,
631 screen, NULL, 0, NULL,
632 FALSE, NULL, timestamp);
637 ev_application_shutdown (EvApplication *application)
639 if (application->toolbars_model) {
640 g_object_unref (application->toolbars_model);
641 g_free (application->toolbars_file);
642 application->toolbars_model = NULL;
643 application->toolbars_file = NULL;
646 #if GTK_CHECK_VERSION (2, 11, 0)
647 if (application->print_settings_file) {
648 if (application->print_settings) {
649 GError *error = NULL;
651 gtk_print_settings_to_file (application->print_settings,
652 application->print_settings_file,
655 g_warning ("%s", error->message);
656 g_error_free (error);
659 g_object_unref (application->print_settings);
660 application->print_settings = NULL;
663 g_free (application->print_settings_file);
664 application->print_settings_file = NULL;
666 #else /* ! GTK 2.11.0 */
667 if (application->print_settings) {
668 g_object_unref (application->print_settings);
669 application->print_settings = NULL;
671 #endif /* GTK 2.11.0 */
673 g_free (application->last_chooser_uri);
674 g_object_unref (application);
680 ev_application_class_init (EvApplicationClass *ev_application_class)
685 ev_application_init (EvApplication *ev_application)
690 init_session (ev_application);
693 ev_application->toolbars_model = egg_toolbars_model_new ();
695 ev_application->toolbars_file = g_build_filename
696 (ev_dot_dir (), "evince_toolbar.xml", NULL);
698 egg_toolbars_model_load_names (ev_application->toolbars_model,
699 DATADIR "/evince-toolbar.xml");
701 if (!egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
702 ev_application->toolbars_file)) {
703 egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
704 DATADIR"/evince-toolbar.xml");
707 /* Open item doesn't exist anymore,
708 * convert it to OpenRecent for compatibility
710 for (i = 0; i < egg_toolbars_model_n_items (ev_application->toolbars_model, 0); i++) {
713 item = egg_toolbars_model_item_nth (ev_application->toolbars_model, 0, i);
714 if (g_ascii_strcasecmp (item, "FileOpen") == 0) {
715 egg_toolbars_model_remove_item (ev_application->toolbars_model, 0, i);
716 egg_toolbars_model_add_item (ev_application->toolbars_model, 0, i,
718 ev_application_save_toolbars_model (ev_application);
723 egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
724 EGG_TB_MODEL_NOT_REMOVABLE);
727 ev_application->keys = ev_media_player_keys_new ();
728 #endif /* ENABLE_DBUS */
732 * ev_application_get_windows:
733 * @application: The instance of the application.
735 * It creates a list of the top level windows.
737 * Returns: A #GList of the top level windows.
740 ev_application_get_windows (EvApplication *application)
742 GList *l, *toplevels;
743 GList *windows = NULL;
745 toplevels = gtk_window_list_toplevels ();
747 for (l = toplevels; l != NULL; l = l->next) {
748 if (EV_IS_WINDOW (l->data)) {
749 windows = g_list_append (windows, l->data);
753 g_list_free (toplevels);
759 * ev_application_get_media_keys:
760 * @application: The instance of the application.
762 * It gives you access to the media player keys handler object.
764 * Returns: A #EvMediaPlayerKeys.
767 *ev_application_get_media_keys (EvApplication *application)
770 if (!application->keys)
772 return g_object_ref (G_OBJECT (application->keys));
775 #endif /* ENABLE_DBUS */
779 ev_application_get_toolbars_model (EvApplication *application)
781 return application->toolbars_model;
785 ev_application_save_toolbars_model (EvApplication *application)
787 egg_toolbars_model_save_toolbars (application->toolbars_model,
788 application->toolbars_file, "1.0");
792 ev_application_set_chooser_uri (EvApplication *application, const gchar *uri)
794 g_free (application->last_chooser_uri);
795 application->last_chooser_uri = g_strdup (uri);
799 ev_application_get_chooser_uri (EvApplication *application)
801 return application->last_chooser_uri;
805 ev_application_screensaver_enable (EvApplication *application)
807 if (application->scr_saver)
808 totem_scrsaver_enable (application->scr_saver);
812 ev_application_screensaver_disable (EvApplication *application)
814 if (application->scr_saver)
815 totem_scrsaver_disable (application->scr_saver);
819 ev_application_get_print_settings (EvApplication *application)
821 if (application->print_settings)
822 return application->print_settings;
824 #if GTK_CHECK_VERSION (2, 11, 0)
825 if (!application->print_settings_file) {
826 application->print_settings_file =
827 g_build_filename (ev_dot_dir (), "print-settings", NULL);
830 if (g_file_test (application->print_settings_file, G_FILE_TEST_IS_REGULAR)) {
831 GError *error = NULL;
833 application->print_settings =
834 gtk_print_settings_new_from_file (application->print_settings_file, &error);
837 g_warning ("%s", error->message);
838 g_error_free (error);
840 return application->print_settings;
843 #endif /* GTK 2.11.0 */
845 application->print_settings = gtk_print_settings_new ();
847 return application->print_settings;
851 ev_application_set_print_settings (EvApplication *application,
852 GtkPrintSettings *settings)
854 g_return_if_fail (GTK_IS_PRINT_SETTINGS (settings));
856 if (settings == application->print_settings)
859 if (application->print_settings)
860 g_object_unref (application->print_settings);
862 application->print_settings = g_object_ref (settings);