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"
28 #include "totem-scrsaver.h"
31 #include <glib/gi18n.h>
32 #include <glib-object.h>
33 #include <gtk/gtkfilechooserdialog.h>
34 #include <gtk/gtkstock.h>
35 #include <gtk/gtkwidget.h>
36 #include <gtk/gtkmain.h>
39 #include <libgnomeui/gnome-client.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;
62 GtkPrintSettings *print_settings;
63 #if GTK_CHECK_VERSION (2, 11, 0)
64 gchar *print_settings_file;
68 struct _EvApplicationClass {
69 GObjectClass base_class;
72 G_DEFINE_TYPE (EvApplication, ev_application, G_TYPE_OBJECT);
74 #define APPLICATION_SERVICE_NAME "org.gnome.evince.ApplicationService"
78 ev_application_register_service (EvApplication *application)
80 static DBusGConnection *connection = NULL;
81 DBusGProxy *driver_proxy;
83 guint request_name_result;
86 g_warning ("Service already registered.");
90 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &err);
91 if (connection == NULL) {
92 g_warning ("Service registration failed.");
98 driver_proxy = dbus_g_proxy_new_for_name (connection,
101 DBUS_INTERFACE_DBUS);
103 if (!org_freedesktop_DBus_request_name (driver_proxy,
104 APPLICATION_SERVICE_NAME,
105 DBUS_NAME_FLAG_DO_NOT_QUEUE,
106 &request_name_result, &err)) {
107 g_warning ("Service registration failed.");
108 g_clear_error (&err);
111 g_object_unref (driver_proxy);
113 if (request_name_result == DBUS_REQUEST_NAME_REPLY_EXISTS) {
117 dbus_g_object_type_install_info (EV_TYPE_APPLICATION,
118 &dbus_glib_ev_application_object_info);
119 dbus_g_connection_register_g_object (connection,
120 "/org/gnome/evince/Evince",
121 G_OBJECT (application));
123 application->scr_saver = totem_scrsaver_new (connection);
127 #endif /* ENABLE_DBUS */
130 * ev_application_get_instance:
132 * Checks for #EvApplication instance, if it doesn't exist it does create it.
134 * Returns: an instance of the #EvApplication data.
137 ev_application_get_instance (void)
139 static EvApplication *instance;
142 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
150 removed_from_session (GnomeClient *client, EvApplication *application)
152 ev_application_shutdown (application);
156 save_session (GnomeClient *client, gint phase, GnomeSaveStyle save_style, gint shutdown,
157 GnomeInteractStyle interact_style, gint fast, EvApplication *application)
163 windows = ev_application_get_windows (application);
164 restart_argv = g_new (char *, g_list_length (windows) + 1);
165 restart_argv[argc++] = g_strdup ("evince");
167 for (l = windows; l != NULL; l = l->next) {
168 EvWindow *window = EV_WINDOW (l->data);
169 restart_argv[argc++] = g_strdup (ev_window_get_uri (window));
172 gnome_client_set_restart_command (client, argc, restart_argv);
174 for (k = 0; k < argc; k++) {
175 g_free (restart_argv[k]);
178 g_list_free (windows);
179 g_free (restart_argv);
185 init_session (EvApplication *application)
189 client = gnome_master_client ();
191 g_signal_connect (client, "save_yourself",
192 G_CALLBACK (save_session), application);
193 g_signal_connect (client, "die",
194 G_CALLBACK (removed_from_session), application);
199 * ev_display_open_if_needed:
200 * @name: the name of the display to be open if it's needed.
202 * Search among all the open displays if any of them have the same name as the
203 * passed name. If the display isn't found it tries the open it.
205 * Returns: a #GdkDisplay of the display with the passed name.
208 ev_display_open_if_needed (const gchar *name)
212 GdkDisplay *display = NULL;
214 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
216 for (l = displays; l != NULL; l = l->next) {
217 const gchar *display_name = gdk_display_get_name ((GdkDisplay *) l->data);
219 if (g_ascii_strcasecmp (display_name, name) == 0) {
225 g_slist_free (displays);
227 return display != NULL ? display : gdk_display_open (name);
231 * get_screen_from_args:
232 * @args: a #GHashTable with data passed to the application.
234 * Looks for the screen in the display available in the hash table passed to the
235 * application. If the display isn't opened, it's opened and the #GdkScreen
236 * assigned to the screen in that display returned.
238 * Returns: the #GdkScreen assigned to the screen on the display indicated by
239 * the data on the #GHashTable.
242 get_screen_from_args (GHashTable *args)
244 GValue *value = NULL;
245 GdkDisplay *display = NULL;
246 GdkScreen *screen = NULL;
248 g_assert (args != NULL);
250 value = g_hash_table_lookup (args, "display");
252 const gchar *display_name;
254 display_name = g_value_get_string (value);
255 display = ev_display_open_if_needed (display_name);
258 value = g_hash_table_lookup (args, "screen");
262 screen_number = g_value_get_int (value);
263 screen = gdk_display_get_screen (display, screen_number);
270 * get_window_run_mode_from_args:
271 * @args: a #GHashTable with data passed to the application.
273 * It does look if the mode option has been passed from command line, using it
274 * as the window run mode, otherwise the run mode will be the normal mode.
276 * Returns: The window run mode passed from command line or
277 * EV_WINDOW_MODE_NORMAL in other case.
279 static EvWindowRunMode
280 get_window_run_mode_from_args (GHashTable *args)
282 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
283 GValue *value = NULL;
285 g_assert (args != NULL);
287 value = g_hash_table_lookup (args, "mode");
289 mode = g_value_get_uint (value);
296 * get_destination_from_args:
297 * @args: a #GHashTable with data passed to the application.
299 * It does look for the page-label argument parsed from the command line and
300 * if it does exist, it returns an #EvLinkDest.
302 * Returns: An #EvLinkDest to page-label if it has been passed from the command
303 * line, NULL in other case.
306 get_destination_from_args (GHashTable *args)
308 EvLinkDest *dest = NULL;
309 GValue *value = NULL;
311 g_assert (args != NULL);
313 value = g_hash_table_lookup (args, "page-label");
315 const gchar *page_label;
317 page_label = g_value_get_string (value);
318 dest = ev_link_dest_new_page_label (page_label);
325 get_find_string_from_args (GHashTable *args)
327 GValue *value = NULL;
329 g_assert (args != NULL);
331 value = g_hash_table_lookup (args, "find-string");
333 return value ? g_value_get_string (value) : NULL;
337 * get_unlink_temp_file_from_args:
338 * @args: a #GHashTable with data passed to the application.
340 * It does look if the unlink-temp-file option has been passed from the command
341 * line returning it's boolean representation, otherwise it does return %FALSE.
343 * Returns: the boolean representation of the unlink-temp-file value or %FALSE
347 get_unlink_temp_file_from_args (GHashTable *args)
349 gboolean unlink_temp_file = FALSE;
350 GValue *value = NULL;
352 g_assert (args != NULL);
354 value = g_hash_table_lookup (args, "unlink-temp-file");
356 unlink_temp_file = g_value_get_boolean (value);
359 return unlink_temp_file;
363 get_print_settings_from_args (GHashTable *args)
365 const gchar *print_settings = NULL;
366 GValue *value = NULL;
368 g_assert (args != NULL);
370 value = g_hash_table_lookup (args, "print-settings");
372 print_settings = g_value_get_string (value);
375 return print_settings;
379 * ev_application_open_window:
380 * @application: The instance of the application.
381 * @args: A #GHashTable with the arguments data.
382 * @timestamp: Current time value.
383 * @error: The #GError facility.
385 * Creates a new window and if the args are available, it's not NULL, it gets
386 * the screen from them and assigns the just created window to it. At last it
392 ev_application_open_window (EvApplication *application,
397 GtkWidget *new_window = ev_window_new ();
398 GdkScreen *screen = NULL;
401 screen = get_screen_from_args (args);
405 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
407 ev_application_add_icon_path_for_screen (screen);
409 gtk_widget_show (new_window);
411 gtk_window_present_with_time (GTK_WINDOW (new_window),
417 * ev_application_get_empty_window:
418 * @application: The instance of the application.
419 * @screen: The screen where the empty window will be search.
421 * It does look if there is any empty window in the indicated screen.
423 * Returns: The first empty #EvWindow in the passed #GdkScreen or NULL in other
427 ev_application_get_empty_window (EvApplication *application,
430 EvWindow *empty_window = NULL;
431 GList *windows = ev_application_get_windows (application);
434 for (l = windows; l != NULL; l = l->next) {
435 EvWindow *window = EV_WINDOW (l->data);
437 if (ev_window_is_empty (window) &&
438 gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
439 empty_window = window;
444 g_list_free (windows);
450 * ev_application_get_uri_window:
451 * @application: The instance of the application.
452 * @uri: The uri to be opened.
454 * It looks in the list of the windows for the one with the document represented
455 * by the passed uri on it. If the window is empty or the document isn't present
456 * on any window, it will return NULL.
458 * Returns: The #EvWindow where the document represented by the passed uri is
459 * shown, NULL in other case.
462 ev_application_get_uri_window (EvApplication *application, const char *uri)
464 EvWindow *uri_window = NULL;
465 GList *windows = gtk_window_list_toplevels ();
468 g_return_val_if_fail (uri != NULL, NULL);
470 for (l = windows; l != NULL; l = l->next) {
471 if (EV_IS_WINDOW (l->data)) {
472 EvWindow *window = EV_WINDOW (l->data);
473 const char *window_uri = ev_window_get_uri (window);
475 if (window_uri && strcmp (window_uri, uri) == 0 && !ev_window_is_empty (window)) {
482 g_list_free (windows);
488 ev_application_add_icon_path_for_screen (GdkScreen *screen)
490 GtkIconTheme *icon_theme;
492 icon_theme = gtk_icon_theme_get_for_screen (screen);
497 gchar *ev_icons_path;
499 /* GtkIconTheme will then look in Evince custom hicolor dir
500 * for icons as well as the standard search paths
502 ev_icons_path = g_build_filename (DATADIR, "icons", NULL);
503 gtk_icon_theme_get_search_path (icon_theme, &path, &n_paths);
504 for (i = n_paths - 1; i >= 0; i--) {
505 if (g_ascii_strcasecmp (ev_icons_path, path[i]) == 0)
510 gtk_icon_theme_append_search_path (icon_theme,
513 g_free (ev_icons_path);
519 * ev_application_open_uri_at_dest:
520 * @application: The instance of the application.
521 * @uri: The uri to be opened.
522 * @screen: Thee screen where the link will be shown.
523 * @dest: The #EvLinkDest of the document.
524 * @mode: The run mode of the window.
525 * @unlink_temp_file: The unlink_temp_file option value.
526 * @timestamp: Current time value.
529 ev_application_open_uri_at_dest (EvApplication *application,
533 EvWindowRunMode mode,
534 const gchar *search_string,
535 gboolean unlink_temp_file,
536 const gchar *print_settings,
539 EvWindow *new_window;
541 g_return_if_fail (uri != NULL);
543 ev_application_add_icon_path_for_screen (screen);
545 new_window = ev_application_get_uri_window (application, uri);
547 if (new_window == NULL) {
548 new_window = ev_application_get_empty_window (application, screen);
551 if (new_window == NULL) {
552 new_window = EV_WINDOW (ev_window_new ());
556 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
558 /* We need to load uri before showing the window, so
559 we can restore window size without flickering */
560 ev_window_open_uri (new_window, uri, dest, mode, search_string,
561 unlink_temp_file, print_settings);
563 ev_document_fc_mutex_lock ();
564 gtk_widget_show (GTK_WIDGET (new_window));
565 ev_document_fc_mutex_unlock ();
567 gtk_window_present_with_time (GTK_WINDOW (new_window),
572 * ev_application_open_uri:
573 * @application: The instance of the application.
574 * @uri: The uri to be opened
575 * @args: A #GHashTable with the arguments data.
576 * @timestamp: Current time value.
577 * @error: The #GError facility.
580 ev_application_open_uri (EvApplication *application,
586 EvLinkDest *dest = NULL;
587 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
588 const gchar *search_string = NULL;
589 gboolean unlink_temp_file = FALSE;
590 const gchar *print_settings = NULL;
591 GdkScreen *screen = NULL;
594 screen = get_screen_from_args (args);
595 dest = get_destination_from_args (args);
596 mode = get_window_run_mode_from_args (args);
597 search_string = get_find_string_from_args (args);
598 unlink_temp_file = (mode == EV_WINDOW_MODE_PREVIEW &&
599 get_unlink_temp_file_from_args (args));
600 print_settings = get_print_settings_from_args (args);
603 ev_application_open_uri_at_dest (application, uri, screen,
604 dest, mode, search_string,
606 print_settings, timestamp);
609 g_object_unref (dest);
615 ev_application_open_uri_list (EvApplication *application,
622 for (l = uri_list; l != NULL; l = l->next) {
623 ev_application_open_uri_at_dest (application, (char *)l->data,
624 screen, NULL, 0, NULL,
625 FALSE, NULL, timestamp);
630 ev_application_shutdown (EvApplication *application)
632 if (application->toolbars_model) {
633 g_object_unref (application->toolbars_model);
634 g_free (application->toolbars_file);
635 application->toolbars_model = NULL;
636 application->toolbars_file = NULL;
639 #if GTK_CHECK_VERSION (2, 11, 0)
640 if (application->print_settings_file) {
641 if (application->print_settings) {
642 GError *error = NULL;
644 gtk_print_settings_to_file (application->print_settings,
645 application->print_settings_file,
648 g_warning (error->message);
649 g_error_free (error);
652 g_object_unref (application->print_settings);
653 application->print_settings = NULL;
656 g_free (application->print_settings_file);
657 application->print_settings_file = NULL;
659 #else /* ! GTK 2.11.0 */
660 if (application->print_settings) {
661 g_object_unref (application->print_settings);
662 application->print_settings = NULL;
664 #endif /* GTK 2.11.0 */
666 g_free (application->last_chooser_uri);
667 g_object_unref (application);
673 ev_application_class_init (EvApplicationClass *ev_application_class)
678 ev_application_init (EvApplication *ev_application)
683 init_session (ev_application);
686 ev_application->toolbars_model = egg_toolbars_model_new ();
688 ev_application->toolbars_file = g_build_filename
689 (ev_dot_dir (), "evince_toolbar.xml", NULL);
691 egg_toolbars_model_load_names (ev_application->toolbars_model,
692 DATADIR "/evince-toolbar.xml");
694 if (!egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
695 ev_application->toolbars_file)) {
696 egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
697 DATADIR"/evince-toolbar.xml");
700 /* Open item doesn't exist anymore,
701 * convert it to OpenRecent for compatibility
703 for (i = 0; i < egg_toolbars_model_n_items (ev_application->toolbars_model, 0); i++) {
706 item = egg_toolbars_model_item_nth (ev_application->toolbars_model, 0, i);
707 if (g_ascii_strcasecmp (item, "FileOpen") == 0) {
708 egg_toolbars_model_remove_item (ev_application->toolbars_model, 0, i);
709 egg_toolbars_model_add_item (ev_application->toolbars_model, 0, i,
711 ev_application_save_toolbars_model (ev_application);
716 egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
717 EGG_TB_MODEL_NOT_REMOVABLE);
721 * ev_application_get_windows:
722 * @application: The instance of the application.
724 * It creates a list of the top level windows.
726 * Returns: A #GList of the top level windows.
729 ev_application_get_windows (EvApplication *application)
731 GList *l, *toplevels;
732 GList *windows = NULL;
734 toplevels = gtk_window_list_toplevels ();
736 for (l = toplevels; l != NULL; l = l->next) {
737 if (EV_IS_WINDOW (l->data)) {
738 windows = g_list_append (windows, l->data);
742 g_list_free (toplevels);
748 ev_application_get_toolbars_model (EvApplication *application)
750 return application->toolbars_model;
754 ev_application_save_toolbars_model (EvApplication *application)
756 egg_toolbars_model_save_toolbars (application->toolbars_model,
757 application->toolbars_file, "1.0");
761 ev_application_set_chooser_uri (EvApplication *application, const gchar *uri)
763 g_free (application->last_chooser_uri);
764 application->last_chooser_uri = g_strdup (uri);
768 ev_application_get_chooser_uri (EvApplication *application)
770 return application->last_chooser_uri;
774 ev_application_screensaver_enable (EvApplication *application)
776 if (application->scr_saver)
777 totem_scrsaver_enable (application->scr_saver);
781 ev_application_screensaver_disable (EvApplication *application)
783 if (application->scr_saver)
784 totem_scrsaver_disable (application->scr_saver);
788 ev_application_get_print_settings (EvApplication *application)
790 if (application->print_settings)
791 return application->print_settings;
793 #if GTK_CHECK_VERSION (2, 11, 0)
794 if (!application->print_settings_file) {
795 application->print_settings_file =
796 g_build_filename (ev_dot_dir (), "print-settings", NULL);
799 if (g_file_test (application->print_settings_file, G_FILE_TEST_IS_REGULAR)) {
800 GError *error = NULL;
802 application->print_settings =
803 gtk_print_settings_new_from_file (application->print_settings_file, &error);
806 g_warning (error->message);
807 g_error_free (error);
809 return application->print_settings;
812 #endif /* GTK 2.11.0 */
814 application->print_settings = gtk_print_settings_new ();
816 return application->print_settings;
820 ev_application_set_print_settings (EvApplication *application,
821 GtkPrintSettings *settings)
823 g_return_if_fail (GTK_IS_PRINT_SETTINGS (settings));
825 if (settings == application->print_settings)
828 if (application->print_settings)
829 g_object_unref (application->print_settings);
831 application->print_settings = g_object_ref (settings);