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;
63 GtkPrintSettings *print_settings;
64 #if GTK_CHECK_VERSION (2, 11, 0)
65 gchar *print_settings_file;
70 struct _EvApplicationClass {
71 GObjectClass base_class;
74 G_DEFINE_TYPE (EvApplication, ev_application, G_TYPE_OBJECT);
76 #define APPLICATION_SERVICE_NAME "org.gnome.evince.ApplicationService"
80 ev_application_register_service (EvApplication *application)
82 static DBusGConnection *connection = NULL;
83 DBusGProxy *driver_proxy;
85 guint request_name_result;
88 g_warning ("Service already registered.");
92 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &err);
93 if (connection == NULL) {
94 g_warning ("Service registration failed.");
100 driver_proxy = dbus_g_proxy_new_for_name (connection,
103 DBUS_INTERFACE_DBUS);
105 if (!org_freedesktop_DBus_request_name (driver_proxy,
106 APPLICATION_SERVICE_NAME,
107 DBUS_NAME_FLAG_DO_NOT_QUEUE,
108 &request_name_result, &err)) {
109 g_warning ("Service registration failed.");
110 g_clear_error (&err);
113 g_object_unref (driver_proxy);
115 if (request_name_result == DBUS_REQUEST_NAME_REPLY_EXISTS) {
119 dbus_g_object_type_install_info (EV_TYPE_APPLICATION,
120 &dbus_glib_ev_application_object_info);
121 dbus_g_connection_register_g_object (connection,
122 "/org/gnome/evince/Evince",
123 G_OBJECT (application));
125 application->scr_saver = totem_scrsaver_new (connection);
129 #endif /* ENABLE_DBUS */
132 * ev_application_get_instance:
134 * Checks for #EvApplication instance, if it doesn't exist it does create it.
136 * Returns: an instance of the #EvApplication data.
139 ev_application_get_instance (void)
141 static EvApplication *instance;
144 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
152 removed_from_session (GnomeClient *client, EvApplication *application)
154 ev_application_shutdown (application);
158 save_session (GnomeClient *client, gint phase, GnomeSaveStyle save_style, gint shutdown,
159 GnomeInteractStyle interact_style, gint fast, EvApplication *application)
165 windows = ev_application_get_windows (application);
166 restart_argv = g_new (char *, g_list_length (windows) + 1);
167 restart_argv[argc++] = g_strdup ("evince");
169 for (l = windows; l != NULL; l = l->next) {
170 EvWindow *window = EV_WINDOW (l->data);
171 restart_argv[argc++] = g_strdup (ev_window_get_uri (window));
174 gnome_client_set_restart_command (client, argc, restart_argv);
176 for (k = 0; k < argc; k++) {
177 g_free (restart_argv[k]);
180 g_list_free (windows);
181 g_free (restart_argv);
187 init_session (EvApplication *application)
191 client = gnome_master_client ();
193 g_signal_connect (client, "save_yourself",
194 G_CALLBACK (save_session), application);
195 g_signal_connect (client, "die",
196 G_CALLBACK (removed_from_session), application);
201 * ev_display_open_if_needed:
202 * @name: the name of the display to be open if it's needed.
204 * Search among all the open displays if any of them have the same name as the
205 * passed name. If the display isn't found it tries the open it.
207 * Returns: a #GdkDisplay of the display with the passed name.
210 ev_display_open_if_needed (const gchar *name)
214 GdkDisplay *display = NULL;
216 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
218 for (l = displays; l != NULL; l = l->next) {
219 const gchar *display_name = gdk_display_get_name ((GdkDisplay *) l->data);
221 if (g_ascii_strcasecmp (display_name, name) == 0) {
227 g_slist_free (displays);
229 return display != NULL ? display : gdk_display_open (name);
233 * get_screen_from_args:
234 * @args: a #GHashTable with data passed to the application.
236 * Looks for the screen in the display available in the hash table passed to the
237 * application. If the display isn't opened, it's opened and the #GdkScreen
238 * assigned to the screen in that display returned.
240 * Returns: the #GdkScreen assigned to the screen on the display indicated by
241 * the data on the #GHashTable.
244 get_screen_from_args (GHashTable *args)
246 GValue *value = NULL;
247 GdkDisplay *display = NULL;
248 GdkScreen *screen = NULL;
250 g_assert (args != NULL);
252 value = g_hash_table_lookup (args, "display");
254 const gchar *display_name;
256 display_name = g_value_get_string (value);
257 display = ev_display_open_if_needed (display_name);
260 value = g_hash_table_lookup (args, "screen");
264 screen_number = g_value_get_int (value);
265 screen = gdk_display_get_screen (display, screen_number);
272 * get_window_run_mode_from_args:
273 * @args: a #GHashTable with data passed to the application.
275 * It does look if the mode option has been passed from command line, using it
276 * as the window run mode, otherwise the run mode will be the normal mode.
278 * Returns: The window run mode passed from command line or
279 * EV_WINDOW_MODE_NORMAL in other case.
281 static EvWindowRunMode
282 get_window_run_mode_from_args (GHashTable *args)
284 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
285 GValue *value = NULL;
287 g_assert (args != NULL);
289 value = g_hash_table_lookup (args, "mode");
291 mode = g_value_get_uint (value);
298 * get_destination_from_args:
299 * @args: a #GHashTable with data passed to the application.
301 * It does look for the page-label argument parsed from the command line and
302 * if it does exist, it returns an #EvLinkDest.
304 * Returns: An #EvLinkDest to page-label if it has been passed from the command
305 * line, NULL in other case.
308 get_destination_from_args (GHashTable *args)
310 EvLinkDest *dest = NULL;
311 GValue *value = NULL;
313 g_assert (args != NULL);
315 value = g_hash_table_lookup (args, "page-label");
317 const gchar *page_label;
319 page_label = g_value_get_string (value);
320 dest = ev_link_dest_new_page_label (page_label);
327 get_find_string_from_args (GHashTable *args)
329 GValue *value = NULL;
331 g_assert (args != NULL);
333 value = g_hash_table_lookup (args, "find-string");
335 return value ? g_value_get_string (value) : NULL;
339 * get_unlink_temp_file_from_args:
340 * @args: a #GHashTable with data passed to the application.
342 * It does look if the unlink-temp-file option has been passed from the command
343 * line returning it's boolean representation, otherwise it does return %FALSE.
345 * Returns: the boolean representation of the unlink-temp-file value or %FALSE
349 get_unlink_temp_file_from_args (GHashTable *args)
351 gboolean unlink_temp_file = FALSE;
352 GValue *value = NULL;
354 g_assert (args != NULL);
356 value = g_hash_table_lookup (args, "unlink-temp-file");
358 unlink_temp_file = g_value_get_boolean (value);
361 return unlink_temp_file;
365 get_print_settings_from_args (GHashTable *args)
367 const gchar *print_settings = NULL;
368 GValue *value = NULL;
370 g_assert (args != NULL);
372 value = g_hash_table_lookup (args, "print-settings");
374 print_settings = g_value_get_string (value);
377 return print_settings;
381 * ev_application_open_window:
382 * @application: The instance of the application.
383 * @args: A #GHashTable with the arguments data.
384 * @timestamp: Current time value.
385 * @error: The #GError facility.
387 * Creates a new window and if the args are available, it's not NULL, it gets
388 * the screen from them and assigns the just created window to it. At last it
394 ev_application_open_window (EvApplication *application,
399 GtkWidget *new_window = ev_window_new ();
400 GdkScreen *screen = NULL;
403 screen = get_screen_from_args (args);
407 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
409 ev_application_add_icon_path_for_screen (screen);
411 gtk_widget_show (new_window);
413 gtk_window_present_with_time (GTK_WINDOW (new_window),
419 * ev_application_get_empty_window:
420 * @application: The instance of the application.
421 * @screen: The screen where the empty window will be search.
423 * It does look if there is any empty window in the indicated screen.
425 * Returns: The first empty #EvWindow in the passed #GdkScreen or NULL in other
429 ev_application_get_empty_window (EvApplication *application,
432 EvWindow *empty_window = NULL;
433 GList *windows = ev_application_get_windows (application);
436 for (l = windows; l != NULL; l = l->next) {
437 EvWindow *window = EV_WINDOW (l->data);
439 if (ev_window_is_empty (window) &&
440 gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
441 empty_window = window;
446 g_list_free (windows);
452 * ev_application_get_uri_window:
453 * @application: The instance of the application.
454 * @uri: The uri to be opened.
456 * It looks in the list of the windows for the one with the document represented
457 * by the passed uri on it. If the window is empty or the document isn't present
458 * on any window, it will return NULL.
460 * Returns: The #EvWindow where the document represented by the passed uri is
461 * shown, NULL in other case.
464 ev_application_get_uri_window (EvApplication *application, const char *uri)
466 EvWindow *uri_window = NULL;
467 GList *windows = gtk_window_list_toplevels ();
470 g_return_val_if_fail (uri != NULL, NULL);
472 for (l = windows; l != NULL; l = l->next) {
473 if (EV_IS_WINDOW (l->data)) {
474 EvWindow *window = EV_WINDOW (l->data);
475 const char *window_uri = ev_window_get_uri (window);
477 if (window_uri && strcmp (window_uri, uri) == 0 && !ev_window_is_empty (window)) {
484 g_list_free (windows);
490 ev_application_add_icon_path_for_screen (GdkScreen *screen)
492 GtkIconTheme *icon_theme;
494 icon_theme = gtk_icon_theme_get_for_screen (screen);
499 gchar *ev_icons_path;
501 /* GtkIconTheme will then look in Evince custom hicolor dir
502 * for icons as well as the standard search paths
504 ev_icons_path = g_build_filename (DATADIR, "icons", NULL);
505 gtk_icon_theme_get_search_path (icon_theme, &path, &n_paths);
506 for (i = n_paths - 1; i >= 0; i--) {
507 if (g_ascii_strcasecmp (ev_icons_path, path[i]) == 0)
512 gtk_icon_theme_append_search_path (icon_theme,
515 g_free (ev_icons_path);
521 * ev_application_open_uri_at_dest:
522 * @application: The instance of the application.
523 * @uri: The uri to be opened.
524 * @screen: Thee screen where the link will be shown.
525 * @dest: The #EvLinkDest of the document.
526 * @mode: The run mode of the window.
527 * @unlink_temp_file: The unlink_temp_file option value.
528 * @timestamp: Current time value.
531 ev_application_open_uri_at_dest (EvApplication *application,
535 EvWindowRunMode mode,
536 const gchar *search_string,
537 gboolean unlink_temp_file,
538 const gchar *print_settings,
541 EvWindow *new_window;
543 g_return_if_fail (uri != NULL);
545 ev_application_add_icon_path_for_screen (screen);
547 new_window = ev_application_get_uri_window (application, uri);
549 if (new_window == NULL) {
550 new_window = ev_application_get_empty_window (application, screen);
553 if (new_window == NULL) {
554 new_window = EV_WINDOW (ev_window_new ());
558 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
560 /* We need to load uri before showing the window, so
561 we can restore window size without flickering */
562 ev_window_open_uri (new_window, uri, dest, mode, search_string,
563 unlink_temp_file, print_settings);
565 ev_document_fc_mutex_lock ();
566 gtk_widget_show (GTK_WIDGET (new_window));
567 ev_document_fc_mutex_unlock ();
569 gtk_window_present_with_time (GTK_WINDOW (new_window),
574 * ev_application_open_uri:
575 * @application: The instance of the application.
576 * @uri: The uri to be opened
577 * @args: A #GHashTable with the arguments data.
578 * @timestamp: Current time value.
579 * @error: The #GError facility.
582 ev_application_open_uri (EvApplication *application,
588 EvLinkDest *dest = NULL;
589 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
590 const gchar *search_string = NULL;
591 gboolean unlink_temp_file = FALSE;
592 const gchar *print_settings = NULL;
593 GdkScreen *screen = NULL;
596 screen = get_screen_from_args (args);
597 dest = get_destination_from_args (args);
598 mode = get_window_run_mode_from_args (args);
599 search_string = get_find_string_from_args (args);
600 unlink_temp_file = (mode == EV_WINDOW_MODE_PREVIEW &&
601 get_unlink_temp_file_from_args (args));
602 print_settings = get_print_settings_from_args (args);
605 ev_application_open_uri_at_dest (application, uri, screen,
606 dest, mode, search_string,
608 print_settings, timestamp);
611 g_object_unref (dest);
617 ev_application_open_uri_list (EvApplication *application,
624 for (l = uri_list; l != NULL; l = l->next) {
625 ev_application_open_uri_at_dest (application, (char *)l->data,
626 screen, NULL, 0, NULL,
627 FALSE, NULL, timestamp);
632 ev_application_shutdown (EvApplication *application)
634 if (application->toolbars_model) {
635 g_object_unref (application->toolbars_model);
636 g_free (application->toolbars_file);
637 application->toolbars_model = NULL;
638 application->toolbars_file = NULL;
641 #ifdef WITH_GTK_PRINT
642 #if GTK_CHECK_VERSION (2, 11, 0)
643 if (application->print_settings_file) {
644 if (application->print_settings) {
645 GError *error = NULL;
647 gtk_print_settings_to_file (application->print_settings,
648 application->print_settings_file,
651 g_warning (error->message);
652 g_error_free (error);
655 g_object_unref (application->print_settings);
656 application->print_settings = NULL;
659 g_free (application->print_settings_file);
660 application->print_settings_file = NULL;
662 #else /* ! GTK 2.11.0 */
663 if (application->print_settings) {
664 g_object_unref (application->print_settings);
665 application->print_settings = NULL;
667 #endif /* GTK 2.11.0 */
668 #endif /* WITH_GTK_PRINT */
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);
725 * ev_application_get_windows:
726 * @application: The instance of the application.
728 * It creates a list of the top level windows.
730 * Returns: A #GList of the top level windows.
733 ev_application_get_windows (EvApplication *application)
735 GList *l, *toplevels;
736 GList *windows = NULL;
738 toplevels = gtk_window_list_toplevels ();
740 for (l = toplevels; l != NULL; l = l->next) {
741 if (EV_IS_WINDOW (l->data)) {
742 windows = g_list_append (windows, l->data);
746 g_list_free (toplevels);
752 ev_application_get_toolbars_model (EvApplication *application)
754 return application->toolbars_model;
758 ev_application_save_toolbars_model (EvApplication *application)
760 egg_toolbars_model_save_toolbars (application->toolbars_model,
761 application->toolbars_file, "1.0");
765 ev_application_set_chooser_uri (EvApplication *application, const gchar *uri)
767 g_free (application->last_chooser_uri);
768 application->last_chooser_uri = g_strdup (uri);
772 ev_application_get_chooser_uri (EvApplication *application)
774 return application->last_chooser_uri;
778 ev_application_screensaver_enable (EvApplication *application)
780 if (application->scr_saver)
781 totem_scrsaver_enable (application->scr_saver);
785 ev_application_screensaver_disable (EvApplication *application)
787 if (application->scr_saver)
788 totem_scrsaver_disable (application->scr_saver);
791 #ifdef WITH_GTK_PRINT
793 ev_application_get_print_settings (EvApplication *application)
795 if (application->print_settings)
796 return application->print_settings;
798 #if GTK_CHECK_VERSION (2, 11, 0)
799 if (!application->print_settings_file) {
800 application->print_settings_file =
801 g_build_filename (ev_dot_dir (), "print-settings", NULL);
804 if (g_file_test (application->print_settings_file, G_FILE_TEST_IS_REGULAR)) {
805 GError *error = NULL;
807 application->print_settings =
808 gtk_print_settings_new_from_file (application->print_settings_file, &error);
811 g_warning (error->message);
812 g_error_free (error);
814 return application->print_settings;
817 #endif /* GTK 2.11.0 */
819 application->print_settings = gtk_print_settings_new ();
821 return application->print_settings;
825 ev_application_set_print_settings (EvApplication *application,
826 GtkPrintSettings *settings)
828 g_return_if_fail (GTK_IS_PRINT_SETTINGS (settings));
830 if (settings == application->print_settings)
833 if (application->print_settings)
834 g_object_unref (application->print_settings);
836 application->print_settings = g_object_ref (settings);
838 #endif /* WITH_GTK_PRINT */