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.
23 #include "ev-application.h"
25 #include "ev-file-helpers.h"
26 #include "ev-document-factory.h"
27 #include "totem-scrsaver.h"
30 #include <glib/gi18n.h>
31 #include <glib-object.h>
32 #include <gtk/gtkfilechooserdialog.h>
33 #include <gtk/gtkstock.h>
34 #include <gtk/gtkwidget.h>
35 #include <gtk/gtkmain.h>
38 #include <libgnomeui/gnome-client.h>
42 #include "ev-application-service.h"
43 #include <dbus/dbus-glib-bindings.h>
48 static void ev_application_add_icon_path_for_screen (GdkScreen *screen);
50 struct _EvApplication {
51 GObject base_instance;
55 EggToolbarsModel *toolbars_model;
57 TotemScrsaver *scr_saver;
59 gchar *last_chooser_uri;
62 GtkPrintSettings *print_settings;
63 #if GTK_CHECK_VERSION (2, 11, 0)
64 gchar *print_settings_file;
69 struct _EvApplicationClass {
70 GObjectClass base_class;
73 G_DEFINE_TYPE (EvApplication, ev_application, G_TYPE_OBJECT);
75 #define APPLICATION_SERVICE_NAME "org.gnome.evince.ApplicationService"
79 ev_application_register_service (EvApplication *application)
81 static DBusGConnection *connection = NULL;
82 DBusGProxy *driver_proxy;
84 guint request_name_result;
87 g_warning ("Service already registered.");
91 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &err);
92 if (connection == NULL) {
93 g_warning ("Service registration failed.");
99 driver_proxy = dbus_g_proxy_new_for_name (connection,
102 DBUS_INTERFACE_DBUS);
104 if (!org_freedesktop_DBus_request_name (driver_proxy,
105 APPLICATION_SERVICE_NAME,
106 DBUS_NAME_FLAG_DO_NOT_QUEUE,
107 &request_name_result, &err)) {
108 g_warning ("Service registration failed.");
109 g_clear_error (&err);
112 g_object_unref (driver_proxy);
114 if (request_name_result == DBUS_REQUEST_NAME_REPLY_EXISTS) {
118 dbus_g_object_type_install_info (EV_TYPE_APPLICATION,
119 &dbus_glib_ev_application_object_info);
120 dbus_g_connection_register_g_object (connection,
121 "/org/gnome/evince/Evince",
122 G_OBJECT (application));
124 application->scr_saver = totem_scrsaver_new (connection);
128 #endif /* ENABLE_DBUS */
131 * ev_application_get_instance:
133 * Checks for #EvApplication instance, if it doesn't exist it does create it.
135 * Returns: an instance of the #EvApplication data.
138 ev_application_get_instance (void)
140 static EvApplication *instance;
143 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
151 removed_from_session (GnomeClient *client, EvApplication *application)
153 ev_application_shutdown (application);
157 save_session (GnomeClient *client, gint phase, GnomeSaveStyle save_style, gint shutdown,
158 GnomeInteractStyle interact_style, gint fast, EvApplication *application)
164 windows = ev_application_get_windows (application);
165 restart_argv = g_new (char *, g_list_length (windows) + 1);
166 restart_argv[argc++] = g_strdup ("evince");
168 for (l = windows; l != NULL; l = l->next) {
169 EvWindow *window = EV_WINDOW (l->data);
170 restart_argv[argc++] = g_strdup (ev_window_get_uri (window));
173 gnome_client_set_restart_command (client, argc, restart_argv);
175 for (k = 0; k < argc; k++) {
176 g_free (restart_argv[k]);
179 g_list_free (windows);
180 g_free (restart_argv);
186 init_session (EvApplication *application)
190 client = gnome_master_client ();
192 g_signal_connect (client, "save_yourself",
193 G_CALLBACK (save_session), application);
194 g_signal_connect (client, "die",
195 G_CALLBACK (removed_from_session), application);
200 * ev_display_open_if_needed:
201 * @name: the name of the display to be open if it's needed.
203 * Search among all the open displays if any of them have the same name as the
204 * passed name. If the display isn't found it tries the open it.
206 * Returns: a #GdkDisplay of the display with the passed name.
209 ev_display_open_if_needed (const gchar *name)
213 GdkDisplay *display = NULL;
215 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
217 for (l = displays; l != NULL; l = l->next) {
218 const gchar *display_name = gdk_display_get_name ((GdkDisplay *) l->data);
220 if (g_ascii_strcasecmp (display_name, name) == 0) {
226 g_slist_free (displays);
228 return display != NULL ? display : gdk_display_open (name);
232 * get_screen_from_args:
233 * @args: a #GHashTable with data passed to the application.
235 * Looks for the screen in the display available in the hash table passed to the
236 * application. If the display isn't opened, it's opened and the #GdkScreen
237 * assigned to the screen in that display returned.
239 * Returns: the #GdkScreen assigned to the screen on the display indicated by
240 * the data on the #GHashTable.
243 get_screen_from_args (GHashTable *args)
245 GValue *value = NULL;
246 GdkDisplay *display = NULL;
247 GdkScreen *screen = NULL;
249 g_assert (args != NULL);
251 value = g_hash_table_lookup (args, "display");
253 const gchar *display_name;
255 display_name = g_value_get_string (value);
256 display = ev_display_open_if_needed (display_name);
259 value = g_hash_table_lookup (args, "screen");
263 screen_number = g_value_get_int (value);
264 screen = gdk_display_get_screen (display, screen_number);
271 * get_window_run_mode_from_args:
272 * @args: a #GHashTable with data passed to the application.
274 * It does look if the mode option has been passed from command line, using it
275 * as the window run mode, otherwise the run mode will be the normal mode.
277 * Returns: The window run mode passed from command line or
278 * EV_WINDOW_MODE_NORMAL in other case.
280 static EvWindowRunMode
281 get_window_run_mode_from_args (GHashTable *args)
283 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
284 GValue *value = NULL;
286 g_assert (args != NULL);
288 value = g_hash_table_lookup (args, "mode");
290 mode = g_value_get_uint (value);
297 * get_destination_from_args:
298 * @args: a #GHashTable with data passed to the application.
300 * It does look for the page-label argument parsed from the command line and
301 * if it does exist, it returns an #EvLinkDest.
303 * Returns: An #EvLinkDest to page-label if it has been passed from the command
304 * line, NULL in other case.
307 get_destination_from_args (GHashTable *args)
309 EvLinkDest *dest = NULL;
310 GValue *value = NULL;
312 g_assert (args != NULL);
314 value = g_hash_table_lookup (args, "page-label");
316 const gchar *page_label;
318 page_label = g_value_get_string (value);
319 dest = ev_link_dest_new_page_label (page_label);
326 * get_unlink_temp_file_from_args:
327 * @args: a #GHashTable with data passed to the application.
329 * It does look if the unlink-temp-file option has been passed from the command
330 * line returning it's boolean representation, otherwise it does return %FALSE.
332 * Returns: the boolean representation of the unlink-temp-file value or %FALSE
336 get_unlink_temp_file_from_args (GHashTable *args)
338 gboolean unlink_temp_file = FALSE;
339 GValue *value = NULL;
341 g_assert (args != NULL);
343 value = g_hash_table_lookup (args, "unlink-temp-file");
345 unlink_temp_file = g_value_get_boolean (value);
348 return unlink_temp_file;
352 get_print_settings_from_args (GHashTable *args)
354 const gchar *print_settings = NULL;
355 GValue *value = NULL;
357 g_assert (args != NULL);
359 value = g_hash_table_lookup (args, "print-settings");
361 print_settings = g_value_get_string (value);
364 return print_settings;
368 * ev_application_open_window:
369 * @application: The instance of the application.
370 * @args: A #GHashTable with the arguments data.
371 * @timestamp: Current time value.
372 * @error: The #GError facility.
374 * Creates a new window and if the args are available, it's not NULL, it gets
375 * the screen from them and assigns the just created window to it. At last it
381 ev_application_open_window (EvApplication *application,
386 GtkWidget *new_window = ev_window_new ();
387 GdkScreen *screen = NULL;
390 screen = get_screen_from_args (args);
394 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
396 ev_application_add_icon_path_for_screen (screen);
398 gtk_widget_show (new_window);
400 gtk_window_present_with_time (GTK_WINDOW (new_window),
406 * ev_application_get_empty_window:
407 * @application: The instance of the application.
408 * @screen: The screen where the empty window will be search.
410 * It does look if there is any empty window in the indicated screen.
412 * Returns: The first empty #EvWindow in the passed #GdkScreen or NULL in other
416 ev_application_get_empty_window (EvApplication *application,
419 EvWindow *empty_window = NULL;
420 GList *windows = ev_application_get_windows (application);
423 for (l = windows; l != NULL; l = l->next) {
424 EvWindow *window = EV_WINDOW (l->data);
426 if (ev_window_is_empty (window) &&
427 gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
428 empty_window = window;
433 g_list_free (windows);
439 * ev_application_get_uri_window:
440 * @application: The instance of the application.
441 * @uri: The uri to be opened.
443 * It looks in the list of the windows for the one with the document represented
444 * by the passed uri on it. If the window is empty or the document isn't present
445 * on any window, it will return NULL.
447 * Returns: The #EvWindow where the document represented by the passed uri is
448 * shown, NULL in other case.
451 ev_application_get_uri_window (EvApplication *application, const char *uri)
453 EvWindow *uri_window = NULL;
454 GList *windows = gtk_window_list_toplevels ();
457 g_return_val_if_fail (uri != NULL, NULL);
459 for (l = windows; l != NULL; l = l->next) {
460 if (EV_IS_WINDOW (l->data)) {
461 EvWindow *window = EV_WINDOW (l->data);
462 const char *window_uri = ev_window_get_uri (window);
464 if (window_uri && strcmp (window_uri, uri) == 0 && !ev_window_is_empty (window)) {
471 g_list_free (windows);
477 ev_application_add_icon_path_for_screen (GdkScreen *screen)
479 GtkIconTheme *icon_theme;
481 icon_theme = gtk_icon_theme_get_for_screen (screen);
486 gchar *ev_icons_path;
488 /* GtkIconTheme will then look in Evince custom hicolor dir
489 * for icons as well as the standard search paths
491 ev_icons_path = g_build_filename (DATADIR, "icons", NULL);
492 gtk_icon_theme_get_search_path (icon_theme, &path, &n_paths);
493 for (i = n_paths - 1; i >= 0; i--) {
494 if (g_ascii_strcasecmp (ev_icons_path, path[i]) == 0)
499 gtk_icon_theme_append_search_path (icon_theme,
502 g_free (ev_icons_path);
508 * ev_application_open_uri_at_dest:
509 * @application: The instance of the application.
510 * @uri: The uri to be opened.
511 * @screen: Thee screen where the link will be shown.
512 * @dest: The #EvLinkDest of the document.
513 * @mode: The run mode of the window.
514 * @unlink_temp_file: The unlink_temp_file option value.
515 * @timestamp: Current time value.
518 ev_application_open_uri_at_dest (EvApplication *application,
522 EvWindowRunMode mode,
523 gboolean unlink_temp_file,
524 const gchar *print_settings,
527 EvWindow *new_window;
529 g_return_if_fail (uri != NULL);
531 ev_application_add_icon_path_for_screen (screen);
533 new_window = ev_application_get_uri_window (application, uri);
535 if (new_window == NULL) {
536 new_window = ev_application_get_empty_window (application, screen);
539 if (new_window == NULL) {
540 new_window = EV_WINDOW (ev_window_new ());
544 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
546 /* We need to load uri before showing the window, so
547 we can restore window size without flickering */
548 ev_window_open_uri (new_window, uri, dest, mode,
549 unlink_temp_file, print_settings);
551 ev_document_fc_mutex_lock ();
552 gtk_widget_show (GTK_WIDGET (new_window));
553 ev_document_fc_mutex_unlock ();
555 gtk_window_present_with_time (GTK_WINDOW (new_window),
560 * ev_application_open_uri:
561 * @application: The instance of the application.
562 * @uri: The uri to be opened
563 * @args: A #GHashTable with the arguments data.
564 * @timestamp: Current time value.
565 * @error: The #GError facility.
568 ev_application_open_uri (EvApplication *application,
574 EvLinkDest *dest = NULL;
575 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
576 gboolean unlink_temp_file = FALSE;
577 const gchar *print_settings = NULL;
578 GdkScreen *screen = NULL;
581 screen = get_screen_from_args (args);
582 dest = get_destination_from_args (args);
583 mode = get_window_run_mode_from_args (args);
584 unlink_temp_file = (mode == EV_WINDOW_MODE_PREVIEW &&
585 get_unlink_temp_file_from_args (args));
586 print_settings = get_print_settings_from_args (args);
589 ev_application_open_uri_at_dest (application, uri, screen,
590 dest, mode, unlink_temp_file,
591 print_settings, timestamp);
594 g_object_unref (dest);
600 ev_application_open_uri_list (EvApplication *application,
607 for (l = uri_list; l != NULL; l = l->next) {
608 ev_application_open_uri_at_dest (application, (char *)l->data,
609 screen, NULL, 0, FALSE,
615 ev_application_shutdown (EvApplication *application)
617 if (application->toolbars_model) {
618 g_object_unref (application->toolbars_model);
619 g_free (application->toolbars_file);
620 application->toolbars_model = NULL;
621 application->toolbars_file = NULL;
624 #ifdef WITH_GTK_PRINT
625 #if GTK_CHECK_VERSION (2, 11, 0)
626 if (application->print_settings_file) {
627 if (application->print_settings) {
628 GError *error = NULL;
630 gtk_print_settings_to_file (application->print_settings,
631 application->print_settings_file,
634 g_warning (error->message);
635 g_error_free (error);
638 g_object_unref (application->print_settings);
639 application->print_settings = NULL;
642 g_free (application->print_settings_file);
643 application->print_settings_file = NULL;
645 #else /* ! GTK 2.11.0 */
646 if (application->print_settings) {
647 g_object_unref (application->print_settings);
648 application->print_settings = NULL;
650 #endif /* GTK 2.11.0 */
651 #endif /* WITH_GTK_PRINT */
653 g_free (application->last_chooser_uri);
654 g_object_unref (application);
660 ev_application_class_init (EvApplicationClass *ev_application_class)
665 ev_application_init (EvApplication *ev_application)
668 init_session (ev_application);
671 ev_application->toolbars_model = egg_toolbars_model_new ();
673 ev_application->toolbars_file = g_build_filename
674 (ev_dot_dir (), "evince_toolbar.xml", NULL);
676 egg_toolbars_model_load_names (ev_application->toolbars_model,
677 DATADIR "/evince-toolbar.xml");
679 if (!egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
680 ev_application->toolbars_file)) {
681 egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
682 DATADIR"/evince-toolbar.xml");
685 egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
686 EGG_TB_MODEL_NOT_REMOVABLE);
690 * ev_application_get_windows:
691 * @application: The instance of the application.
693 * It creates a list of the top level windows.
695 * Returns: A #GList of the top level windows.
698 ev_application_get_windows (EvApplication *application)
700 GList *l, *toplevels;
701 GList *windows = NULL;
703 toplevels = gtk_window_list_toplevels ();
705 for (l = toplevels; l != NULL; l = l->next) {
706 if (EV_IS_WINDOW (l->data)) {
707 windows = g_list_append (windows, l->data);
711 g_list_free (toplevels);
717 ev_application_get_toolbars_model (EvApplication *application)
719 return application->toolbars_model;
723 ev_application_save_toolbars_model (EvApplication *application)
725 egg_toolbars_model_save_toolbars (application->toolbars_model,
726 application->toolbars_file, "1.0");
730 ev_application_set_chooser_uri (EvApplication *application, const gchar *uri)
732 g_free (application->last_chooser_uri);
733 application->last_chooser_uri = g_strdup (uri);
737 ev_application_get_chooser_uri (EvApplication *application)
739 return application->last_chooser_uri;
743 ev_application_screensaver_enable (EvApplication *application)
745 if (application->scr_saver)
746 totem_scrsaver_enable (application->scr_saver);
750 ev_application_screensaver_disable (EvApplication *application)
752 if (application->scr_saver)
753 totem_scrsaver_disable (application->scr_saver);
756 #ifdef WITH_GTK_PRINT
758 ev_application_get_print_settings (EvApplication *application)
760 if (application->print_settings)
761 return application->print_settings;
763 #if GTK_CHECK_VERSION (2, 11, 0)
764 if (!application->print_settings_file) {
765 application->print_settings_file =
766 g_build_filename (ev_dot_dir (), "print-settings", NULL);
769 if (g_file_test (application->print_settings_file, G_FILE_TEST_IS_REGULAR)) {
770 GError *error = NULL;
772 application->print_settings =
773 gtk_print_settings_new_from_file (application->print_settings_file, &error);
776 g_warning (error->message);
777 g_error_free (error);
779 return application->print_settings;
782 #endif /* GTK 2.11.0 */
784 application->print_settings = gtk_print_settings_new ();
786 return application->print_settings;
790 ev_application_set_print_settings (EvApplication *application,
791 GtkPrintSettings *settings)
793 g_return_if_fail (GTK_IS_PRINT_SETTINGS (settings));
795 if (settings == application->print_settings)
798 if (application->print_settings)
799 g_object_unref (application->print_settings);
801 application->print_settings = g_object_ref (settings);
803 #endif /* WITH_GTK_PRINT */