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 #ifndef HAVE_GTK_RECENT
58 EggRecentModel *recent_model;
61 TotemScrsaver *scr_saver;
63 gchar *last_chooser_uri;
66 GtkPrintSettings *print_settings;
67 #if GTK_CHECK_VERSION (2, 11, 0)
68 gchar *print_settings_file;
73 struct _EvApplicationClass {
74 GObjectClass base_class;
77 G_DEFINE_TYPE (EvApplication, ev_application, G_TYPE_OBJECT);
79 #define APPLICATION_SERVICE_NAME "org.gnome.evince.ApplicationService"
83 ev_application_register_service (EvApplication *application)
85 static DBusGConnection *connection = NULL;
86 DBusGProxy *driver_proxy;
88 guint request_name_result;
91 g_warning ("Service already registered.");
95 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &err);
96 if (connection == NULL) {
97 g_warning ("Service registration failed.");
103 driver_proxy = dbus_g_proxy_new_for_name (connection,
106 DBUS_INTERFACE_DBUS);
108 if (!org_freedesktop_DBus_request_name (driver_proxy,
109 APPLICATION_SERVICE_NAME,
110 DBUS_NAME_FLAG_DO_NOT_QUEUE,
111 &request_name_result, &err)) {
112 g_warning ("Service registration failed.");
113 g_clear_error (&err);
116 g_object_unref (driver_proxy);
118 if (request_name_result == DBUS_REQUEST_NAME_REPLY_EXISTS) {
122 dbus_g_object_type_install_info (EV_TYPE_APPLICATION,
123 &dbus_glib_ev_application_object_info);
124 dbus_g_connection_register_g_object (connection,
125 "/org/gnome/evince/Evince",
126 G_OBJECT (application));
128 application->scr_saver = totem_scrsaver_new (connection);
132 #endif /* ENABLE_DBUS */
135 * ev_application_get_instance:
137 * Checks for #EvApplication instance, if it doesn't exist it does create it.
139 * Returns: an instance of the #EvApplication data.
142 ev_application_get_instance (void)
144 static EvApplication *instance;
147 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
155 removed_from_session (GnomeClient *client, EvApplication *application)
157 ev_application_shutdown (application);
161 save_session (GnomeClient *client, gint phase, GnomeSaveStyle save_style, gint shutdown,
162 GnomeInteractStyle interact_style, gint fast, EvApplication *application)
168 windows = ev_application_get_windows (application);
169 restart_argv = g_new (char *, g_list_length (windows) + 1);
170 restart_argv[argc++] = g_strdup ("evince");
172 for (l = windows; l != NULL; l = l->next) {
173 EvWindow *window = EV_WINDOW (l->data);
174 restart_argv[argc++] = g_strdup (ev_window_get_uri (window));
177 gnome_client_set_restart_command (client, argc, restart_argv);
179 for (k = 0; k < argc; k++) {
180 g_free (restart_argv[k]);
183 g_list_free (windows);
184 g_free (restart_argv);
190 init_session (EvApplication *application)
194 client = gnome_master_client ();
196 g_signal_connect (client, "save_yourself",
197 G_CALLBACK (save_session), application);
198 g_signal_connect (client, "die",
199 G_CALLBACK (removed_from_session), application);
204 * ev_display_open_if_needed:
205 * @name: the name of the display to be open if it's needed.
207 * Search among all the open displays if any of them have the same name as the
208 * passed name. If the display isn't found it tries the open it.
210 * Returns: a #GdkDisplay of the display with the passed name.
213 ev_display_open_if_needed (const gchar *name)
217 GdkDisplay *display = NULL;
219 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
221 for (l = displays; l != NULL; l = l->next) {
222 const gchar *display_name = gdk_display_get_name ((GdkDisplay *) l->data);
224 if (g_ascii_strcasecmp (display_name, name) == 0) {
230 g_slist_free (displays);
232 return display != NULL ? display : gdk_display_open (name);
236 * get_screen_from_args:
237 * @args: a #GHashTable with data passed to the application.
239 * Looks for the screen in the display available in the hash table passed to the
240 * application. If the display isn't opened, it's opened and the #GdkScreen
241 * assigned to the screen in that display returned.
243 * Returns: the #GdkScreen assigned to the screen on the display indicated by
244 * the data on the #GHashTable.
247 get_screen_from_args (GHashTable *args)
249 GValue *value = NULL;
250 GdkDisplay *display = NULL;
251 GdkScreen *screen = NULL;
253 g_assert (args != NULL);
255 value = g_hash_table_lookup (args, "display");
257 const gchar *display_name;
259 display_name = g_value_get_string (value);
260 display = ev_display_open_if_needed (display_name);
263 value = g_hash_table_lookup (args, "screen");
267 screen_number = g_value_get_int (value);
268 screen = gdk_display_get_screen (display, screen_number);
275 * get_window_run_mode_from_args:
276 * @args: a #GHashTable with data passed to the application.
278 * It does look if the mode option has been passed from command line, using it
279 * as the window run mode, otherwise the run mode will be the normal mode.
281 * Returns: The window run mode passed from command line or
282 * EV_WINDOW_MODE_NORMAL in other case.
284 static EvWindowRunMode
285 get_window_run_mode_from_args (GHashTable *args)
287 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
288 GValue *value = NULL;
290 g_assert (args != NULL);
292 value = g_hash_table_lookup (args, "mode");
294 mode = g_value_get_uint (value);
301 * get_destination_from_args:
302 * @args: a #GHashTable with data passed to the application.
304 * It does look for the page-label argument parsed from the command line and
305 * if it does exist, it returns an #EvLinkDest.
307 * Returns: An #EvLinkDest to page-label if it has been passed from the command
308 * line, NULL in other case.
311 get_destination_from_args (GHashTable *args)
313 EvLinkDest *dest = NULL;
314 GValue *value = NULL;
316 g_assert (args != NULL);
318 value = g_hash_table_lookup (args, "page-label");
320 const gchar *page_label;
322 page_label = g_value_get_string (value);
323 dest = ev_link_dest_new_page_label (page_label);
330 * get_unlink_temp_file_from_args:
331 * @args: a #GHashTable with data passed to the application.
333 * It does look if the unlink-temp-file option has been passed from the command
334 * line returning it's boolean representation, otherwise it does return %FALSE.
336 * Returns: the boolean representation of the unlink-temp-file value or %FALSE
340 get_unlink_temp_file_from_args (GHashTable *args)
342 gboolean unlink_temp_file = FALSE;
343 GValue *value = NULL;
345 g_assert (args != NULL);
347 value = g_hash_table_lookup (args, "unlink-temp-file");
349 unlink_temp_file = g_value_get_boolean (value);
352 return unlink_temp_file;
356 get_print_settings_from_args (GHashTable *args)
358 const gchar *print_settings = NULL;
359 GValue *value = NULL;
361 g_assert (args != NULL);
363 value = g_hash_table_lookup (args, "print-settings");
365 print_settings = g_value_get_string (value);
368 return print_settings;
372 * ev_application_open_window:
373 * @application: The instance of the application.
374 * @args: A #GHashTable with the arguments data.
375 * @timestamp: Current time value.
376 * @error: The #GError facility.
378 * Creates a new window and if the args are available, it's not NULL, it gets
379 * the screen from them and assigns the just created window to it. At last it
385 ev_application_open_window (EvApplication *application,
390 GtkWidget *new_window = ev_window_new ();
391 GdkScreen *screen = NULL;
394 screen = get_screen_from_args (args);
398 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
400 ev_application_add_icon_path_for_screen (screen);
402 gtk_widget_show (new_window);
404 gtk_window_present_with_time (GTK_WINDOW (new_window),
410 * ev_application_get_empty_window:
411 * @application: The instance of the application.
412 * @screen: The screen where the empty window will be search.
414 * It does look if there is any empty window in the indicated screen.
416 * Returns: The first empty #EvWindow in the passed #GdkScreen or NULL in other
420 ev_application_get_empty_window (EvApplication *application,
423 EvWindow *empty_window = NULL;
424 GList *windows = ev_application_get_windows (application);
427 for (l = windows; l != NULL; l = l->next) {
428 EvWindow *window = EV_WINDOW (l->data);
430 if (ev_window_is_empty (window) &&
431 gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
432 empty_window = window;
437 g_list_free (windows);
443 * ev_application_get_uri_window:
444 * @application: The instance of the application.
445 * @uri: The uri to be opened.
447 * It looks in the list of the windows for the one with the document represented
448 * by the passed uri on it. If the window is empty or the document isn't present
449 * on any window, it will return NULL.
451 * Returns: The #EvWindow where the document represented by the passed uri is
452 * shown, NULL in other case.
455 ev_application_get_uri_window (EvApplication *application, const char *uri)
457 EvWindow *uri_window = NULL;
458 GList *windows = gtk_window_list_toplevels ();
461 g_return_val_if_fail (uri != NULL, NULL);
463 for (l = windows; l != NULL; l = l->next) {
464 if (EV_IS_WINDOW (l->data)) {
465 EvWindow *window = EV_WINDOW (l->data);
466 const char *window_uri = ev_window_get_uri (window);
468 if (window_uri && strcmp (window_uri, uri) == 0 && !ev_window_is_empty (window)) {
475 g_list_free (windows);
481 ev_application_add_icon_path_for_screen (GdkScreen *screen)
483 GtkIconTheme *icon_theme;
485 icon_theme = gtk_icon_theme_get_for_screen (screen);
490 gchar *ev_icons_path;
492 /* GtkIconTheme will then look in Evince custom hicolor dir
493 * for icons as well as the standard search paths
495 ev_icons_path = g_build_filename (DATADIR, "icons", NULL);
496 gtk_icon_theme_get_search_path (icon_theme, &path, &n_paths);
497 for (i = n_paths - 1; i >= 0; i--) {
498 if (g_ascii_strcasecmp (ev_icons_path, path[i]) == 0)
503 gtk_icon_theme_append_search_path (icon_theme,
506 g_free (ev_icons_path);
512 * ev_application_open_uri_at_dest:
513 * @application: The instance of the application.
514 * @uri: The uri to be opened.
515 * @screen: Thee screen where the link will be shown.
516 * @dest: The #EvLinkDest of the document.
517 * @mode: The run mode of the window.
518 * @unlink_temp_file: The unlink_temp_file option value.
519 * @timestamp: Current time value.
522 ev_application_open_uri_at_dest (EvApplication *application,
526 EvWindowRunMode mode,
527 gboolean unlink_temp_file,
528 const gchar *print_settings,
531 EvWindow *new_window;
533 g_return_if_fail (uri != NULL);
535 ev_application_add_icon_path_for_screen (screen);
537 new_window = ev_application_get_uri_window (application, uri);
539 if (new_window == NULL) {
540 new_window = ev_application_get_empty_window (application, screen);
543 if (new_window == NULL) {
544 new_window = EV_WINDOW (ev_window_new ());
548 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
550 /* We need to load uri before showing the window, so
551 we can restore window size without flickering */
552 ev_window_open_uri (new_window, uri, dest, mode,
553 unlink_temp_file, print_settings);
555 ev_document_fc_mutex_lock ();
556 gtk_widget_show (GTK_WIDGET (new_window));
557 ev_document_fc_mutex_unlock ();
559 gtk_window_present_with_time (GTK_WINDOW (new_window),
564 * ev_application_open_uri:
565 * @application: The instance of the application.
566 * @uri: The uri to be opened
567 * @args: A #GHashTable with the arguments data.
568 * @timestamp: Current time value.
569 * @error: The #GError facility.
572 ev_application_open_uri (EvApplication *application,
578 EvLinkDest *dest = NULL;
579 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
580 gboolean unlink_temp_file = FALSE;
581 const gchar *print_settings = NULL;
582 GdkScreen *screen = NULL;
585 screen = get_screen_from_args (args);
586 dest = get_destination_from_args (args);
587 mode = get_window_run_mode_from_args (args);
588 unlink_temp_file = (mode == EV_WINDOW_MODE_PREVIEW &&
589 get_unlink_temp_file_from_args (args));
590 print_settings = get_print_settings_from_args (args);
593 ev_application_open_uri_at_dest (application, uri, screen,
594 dest, mode, unlink_temp_file,
595 print_settings, timestamp);
598 g_object_unref (dest);
604 ev_application_open_uri_list (EvApplication *application,
611 for (l = uri_list; l != NULL; l = l->next) {
612 ev_application_open_uri_at_dest (application, (char *)l->data,
613 screen, NULL, 0, FALSE,
619 ev_application_shutdown (EvApplication *application)
621 if (application->toolbars_model) {
622 g_object_unref (application->toolbars_model);
623 g_free (application->toolbars_file);
624 application->toolbars_model = NULL;
625 application->toolbars_file = NULL;
628 #ifndef HAVE_GTK_RECENT
629 if (application->recent_model) {
630 g_object_unref (application->recent_model);
631 application->recent_model = NULL;
635 #ifdef WITH_GTK_PRINT
636 #if GTK_CHECK_VERSION (2, 11, 0)
637 if (application->print_settings_file) {
638 if (application->print_settings) {
639 GError *error = NULL;
641 gtk_print_settings_to_file (application->print_settings,
642 application->print_settings_file,
645 g_warning (error->message);
646 g_error_free (error);
649 g_object_unref (application->print_settings);
650 application->print_settings = NULL;
653 g_free (application->print_settings_file);
654 application->print_settings_file = NULL;
656 #else /* ! GTK 2.11.0 */
657 if (application->print_settings) {
658 g_object_unref (application->print_settings);
659 application->print_settings = NULL;
661 #endif /* GTK 2.11.0 */
662 #endif /* WITH_GTK_PRINT */
664 g_free (application->last_chooser_uri);
665 g_object_unref (application);
671 ev_application_class_init (EvApplicationClass *ev_application_class)
676 ev_application_init (EvApplication *ev_application)
679 init_session (ev_application);
682 ev_application->toolbars_model = egg_toolbars_model_new ();
684 ev_application->toolbars_file = g_build_filename
685 (ev_dot_dir (), "evince_toolbar.xml", NULL);
687 egg_toolbars_model_load_names (ev_application->toolbars_model,
688 DATADIR "/evince-toolbar.xml");
690 if (!egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
691 ev_application->toolbars_file)) {
692 egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
693 DATADIR"/evince-toolbar.xml");
696 egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
697 EGG_TB_MODEL_NOT_REMOVABLE);
699 #ifndef HAVE_GTK_RECENT
700 ev_application->recent_model = egg_recent_model_new (EGG_RECENT_MODEL_SORT_MRU);
701 /* FIXME we should add a mime type filter but current eggrecent
702 has only a varargs style api which does not work well when
703 the list of mime types is dynamic */
704 egg_recent_model_set_limit (ev_application->recent_model, 5);
705 egg_recent_model_set_filter_groups (ev_application->recent_model,
707 #endif /* HAVE_GTK_RECENT */
711 * ev_application_get_windows:
712 * @application: The instance of the application.
714 * It creates a list of the top level windows.
716 * Returns: A #GList of the top level windows.
719 ev_application_get_windows (EvApplication *application)
721 GList *l, *toplevels;
722 GList *windows = NULL;
724 toplevels = gtk_window_list_toplevels ();
726 for (l = toplevels; l != NULL; l = l->next) {
727 if (EV_IS_WINDOW (l->data)) {
728 windows = g_list_append (windows, l->data);
732 g_list_free (toplevels);
738 ev_application_get_toolbars_model (EvApplication *application)
740 return application->toolbars_model;
743 #ifndef HAVE_GTK_RECENT
745 ev_application_get_recent_model (EvApplication *application)
747 return application->recent_model;
752 ev_application_save_toolbars_model (EvApplication *application)
754 egg_toolbars_model_save_toolbars (application->toolbars_model,
755 application->toolbars_file, "1.0");
759 ev_application_set_chooser_uri (EvApplication *application, const gchar *uri)
761 g_free (application->last_chooser_uri);
762 application->last_chooser_uri = g_strdup (uri);
766 ev_application_get_chooser_uri (EvApplication *application)
768 return application->last_chooser_uri;
772 ev_application_screensaver_enable (EvApplication *application)
774 if (application->scr_saver)
775 totem_scrsaver_enable (application->scr_saver);
779 ev_application_screensaver_disable (EvApplication *application)
781 if (application->scr_saver)
782 totem_scrsaver_disable (application->scr_saver);
785 #ifdef WITH_GTK_PRINT
787 ev_application_get_print_settings (EvApplication *application)
789 if (application->print_settings)
790 return application->print_settings;
792 #if GTK_CHECK_VERSION (2, 11, 0)
793 if (!application->print_settings_file) {
794 application->print_settings_file =
795 g_build_filename (ev_dot_dir (), "print-settings", NULL);
798 if (g_file_test (application->print_settings_file, G_FILE_TEST_IS_REGULAR)) {
799 GError *error = NULL;
801 application->print_settings =
802 gtk_print_settings_new_from_file (application->print_settings_file, &error);
805 g_warning (error->message);
806 g_error_free (error);
808 return application->print_settings;
811 #endif /* GTK 2.11.0 */
813 application->print_settings = gtk_print_settings_new ();
815 return application->print_settings;
819 ev_application_set_print_settings (EvApplication *application,
820 GtkPrintSettings *settings)
822 g_return_if_fail (GTK_IS_PRINT_SETTINGS (settings));
824 if (settings == application->print_settings)
827 if (application->print_settings)
828 g_object_unref (application->print_settings);
830 application->print_settings = g_object_ref (settings);
832 #endif /* WITH_GTK_PRINT */