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 struct _EvApplication {
49 GObject base_instance;
53 EggToolbarsModel *toolbars_model;
55 #ifndef HAVE_GTK_RECENT
56 EggRecentModel *recent_model;
59 TotemScrsaver *scr_saver;
61 gchar *last_chooser_uri;
64 GtkPrintSettings *print_settings;
65 #if GTK_CHECK_VERSION (2, 11, 0)
66 gchar *print_settings_file;
71 struct _EvApplicationClass {
72 GObjectClass base_class;
75 G_DEFINE_TYPE (EvApplication, ev_application, G_TYPE_OBJECT);
77 #define APPLICATION_SERVICE_NAME "org.gnome.evince.ApplicationService"
81 ev_application_register_service (EvApplication *application)
83 static DBusGConnection *connection = NULL;
84 DBusGProxy *driver_proxy;
86 guint request_name_result;
89 g_warning ("Service already registered.");
93 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &err);
94 if (connection == NULL) {
95 g_warning ("Service registration failed.");
101 driver_proxy = dbus_g_proxy_new_for_name (connection,
104 DBUS_INTERFACE_DBUS);
106 if (!org_freedesktop_DBus_request_name (driver_proxy,
107 APPLICATION_SERVICE_NAME,
108 DBUS_NAME_FLAG_DO_NOT_QUEUE,
109 &request_name_result, &err)) {
110 g_warning ("Service registration failed.");
111 g_clear_error (&err);
114 g_object_unref (driver_proxy);
116 if (request_name_result == DBUS_REQUEST_NAME_REPLY_EXISTS) {
120 dbus_g_object_type_install_info (EV_TYPE_APPLICATION,
121 &dbus_glib_ev_application_object_info);
122 dbus_g_connection_register_g_object (connection,
123 "/org/gnome/evince/Evince",
124 G_OBJECT (application));
126 application->scr_saver = totem_scrsaver_new (connection);
130 #endif /* ENABLE_DBUS */
133 * ev_application_get_instance:
135 * Checks for #EvApplication instance, if it doesn't exist it does create it.
137 * Returns: an instance of the #EvApplication data.
140 ev_application_get_instance (void)
142 static EvApplication *instance;
145 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
153 removed_from_session (GnomeClient *client, EvApplication *application)
155 ev_application_shutdown (application);
159 save_session (GnomeClient *client, gint phase, GnomeSaveStyle save_style, gint shutdown,
160 GnomeInteractStyle interact_style, gint fast, EvApplication *application)
166 windows = ev_application_get_windows (application);
167 restart_argv = g_new (char *, g_list_length (windows) + 1);
168 restart_argv[argc++] = g_strdup ("evince");
170 for (l = windows; l != NULL; l = l->next) {
171 EvWindow *window = EV_WINDOW (l->data);
172 restart_argv[argc++] = g_strdup (ev_window_get_uri (window));
175 gnome_client_set_restart_command (client, argc, restart_argv);
177 for (k = 0; k < argc; k++) {
178 g_free (restart_argv[k]);
181 g_list_free (windows);
182 g_free (restart_argv);
188 init_session (EvApplication *application)
192 client = gnome_master_client ();
194 g_signal_connect (client, "save_yourself",
195 G_CALLBACK (save_session), application);
196 g_signal_connect (client, "die",
197 G_CALLBACK (removed_from_session), application);
202 * ev_display_open_if_needed:
203 * @name: the name of the display to be open if it's needed.
205 * Search among all the open displays if any of them have the same name as the
206 * passed name. If the display isn't found it tries the open it.
208 * Returns: a #GdkDisplay of the display with the passed name.
211 ev_display_open_if_needed (const gchar *name)
215 GdkDisplay *display = NULL;
217 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
219 for (l = displays; l != NULL; l = l->next) {
220 const gchar *display_name = gdk_display_get_name ((GdkDisplay *) l->data);
222 if (g_ascii_strcasecmp (display_name, name) == 0) {
228 g_slist_free (displays);
230 return display != NULL ? display : gdk_display_open (name);
234 * get_screen_from_args:
235 * @args: a #GHashTable with data passed to the application.
237 * Looks for the screen in the display available in the hash table passed to the
238 * application. If the display isn't opened, it's opened and the #GdkScreen
239 * assigned to the screen in that display returned.
241 * Returns: the #GdkScreen assigned to the screen on the display indicated by
242 * the data on the #GHashTable.
245 get_screen_from_args (GHashTable *args)
247 GValue *value = NULL;
248 GdkDisplay *display = NULL;
249 GdkScreen *screen = NULL;
251 g_assert (args != NULL);
253 value = g_hash_table_lookup (args, "display");
255 const gchar *display_name;
257 display_name = g_value_get_string (value);
258 display = ev_display_open_if_needed (display_name);
261 value = g_hash_table_lookup (args, "screen");
265 screen_number = g_value_get_int (value);
266 screen = gdk_display_get_screen (display, screen_number);
273 * get_window_run_mode_from_args:
274 * @args: a #GHashTable with data passed to the application.
276 * It does look if the mode option has been passed from command line, using it
277 * as the window run mode, otherwise the run mode will be the normal mode.
279 * Returns: The window run mode passed from command line or
280 * EV_WINDOW_MODE_NORMAL in other case.
282 static EvWindowRunMode
283 get_window_run_mode_from_args (GHashTable *args)
285 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
286 GValue *value = NULL;
288 g_assert (args != NULL);
290 value = g_hash_table_lookup (args, "mode");
292 mode = g_value_get_uint (value);
299 * get_destination_from_args:
300 * @args: a #GHashTable with data passed to the application.
302 * It does look for the page-label argument parsed from the command line and
303 * if it does exist, it returns an #EvLinkDest.
305 * Returns: An #EvLinkDest to page-label if it has been passed from the command
306 * line, NULL in other case.
309 get_destination_from_args (GHashTable *args)
311 EvLinkDest *dest = NULL;
312 GValue *value = NULL;
314 g_assert (args != NULL);
316 value = g_hash_table_lookup (args, "page-label");
318 const gchar *page_label;
320 page_label = g_value_get_string (value);
321 dest = ev_link_dest_new_page_label (page_label);
328 * get_unlink_temp_file_from_args:
329 * @args: a #GHashTable with data passed to the application.
331 * It does look if the unlink-temp-file option has been passed from the command
332 * line returning it's boolean representation, otherwise it does return %FALSE.
334 * Returns: the boolean representation of the unlink-temp-file value or %FALSE
338 get_unlink_temp_file_from_args (GHashTable *args)
340 gboolean unlink_temp_file = FALSE;
341 GValue *value = NULL;
343 g_assert (args != NULL);
345 value = g_hash_table_lookup (args, "unlink-temp-file");
347 unlink_temp_file = g_value_get_boolean (value);
350 return unlink_temp_file;
354 * ev_application_open_window:
355 * @application: The instance of the application.
356 * @args: A #GHashTable with the arguments data.
357 * @timestamp: Current time value.
358 * @error: The #GError facility.
360 * Creates a new window and if the args are available, it's not NULL, it gets
361 * the screen from them and assigns the just created window to it. At last it
367 ev_application_open_window (EvApplication *application,
372 GtkWidget *new_window = ev_window_new ();
373 GdkScreen *screen = NULL;
376 screen = get_screen_from_args (args);
380 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
383 gtk_widget_show (new_window);
385 gtk_window_present_with_time (GTK_WINDOW (new_window),
391 * ev_application_get_empty_window:
392 * @application: The instance of the application.
393 * @screen: The screen where the empty window will be search.
395 * It does look if there is any empty window in the indicated screen.
397 * Returns: The first empty #EvWindow in the passed #GdkScreen or NULL in other
401 ev_application_get_empty_window (EvApplication *application,
404 EvWindow *empty_window = NULL;
405 GList *windows = ev_application_get_windows (application);
408 for (l = windows; l != NULL; l = l->next) {
409 EvWindow *window = EV_WINDOW (l->data);
411 if (ev_window_is_empty (window) &&
412 gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
413 empty_window = window;
418 g_list_free (windows);
424 * ev_application_get_uri_window:
425 * @application: The instance of the application.
426 * @uri: The uri to be opened.
428 * It looks in the list of the windows for the one with the document represented
429 * by the passed uri on it. If the window is empty or the document isn't present
430 * on any window, it will return NULL.
432 * Returns: The #EvWindow where the document represented by the passed uri is
433 * shown, NULL in other case.
436 ev_application_get_uri_window (EvApplication *application, const char *uri)
438 EvWindow *uri_window = NULL;
439 GList *windows = gtk_window_list_toplevels ();
442 g_return_val_if_fail (uri != NULL, NULL);
444 for (l = windows; l != NULL; l = l->next) {
445 if (EV_IS_WINDOW (l->data)) {
446 EvWindow *window = EV_WINDOW (l->data);
447 const char *window_uri = ev_window_get_uri (window);
449 if (window_uri && strcmp (window_uri, uri) == 0 && !ev_window_is_empty (window)) {
456 g_list_free (windows);
462 * ev_application_open_uri_at_dest:
463 * @application: The instance of the application.
464 * @uri: The uri to be opened.
465 * @screen: Thee screen where the link will be shown.
466 * @dest: The #EvLinkDest of the document.
467 * @mode: The run mode of the window.
468 * @unlink_temp_file: The unlink_temp_file option value.
469 * @timestamp: Current time value.
472 ev_application_open_uri_at_dest (EvApplication *application,
476 EvWindowRunMode mode,
477 gboolean unlink_temp_file,
480 EvWindow *new_window;
481 GtkIconTheme *icon_theme;
483 g_return_if_fail (uri != NULL);
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);
510 new_window = ev_application_get_uri_window (application, uri);
512 if (new_window == NULL) {
513 new_window = ev_application_get_empty_window (application, screen);
516 if (new_window == NULL) {
517 new_window = EV_WINDOW (ev_window_new ());
521 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
523 /* We need to load uri before showing the window, so
524 we can restore window size without flickering */
525 ev_window_open_uri (new_window, uri, dest, mode, unlink_temp_file);
527 ev_document_fc_mutex_lock ();
528 gtk_widget_show (GTK_WIDGET (new_window));
529 ev_document_fc_mutex_unlock ();
531 gtk_window_present_with_time (GTK_WINDOW (new_window),
536 * ev_application_open_uri:
537 * @application: The instance of the application.
538 * @uri: The uri to be opened
539 * @args: A #GHashTable with the arguments data.
540 * @timestamp: Current time value.
541 * @error: The #GError facility.
544 ev_application_open_uri (EvApplication *application,
550 EvLinkDest *dest = NULL;
551 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
552 gboolean unlink_temp_file = FALSE;
553 GdkScreen *screen = NULL;
556 screen = get_screen_from_args (args);
557 dest = get_destination_from_args (args);
558 mode = get_window_run_mode_from_args (args);
559 unlink_temp_file = (mode == EV_WINDOW_MODE_PREVIEW &&
560 get_unlink_temp_file_from_args (args));
563 ev_application_open_uri_at_dest (application, uri, screen,
564 dest, mode, unlink_temp_file,
568 g_object_unref (dest);
574 ev_application_open_uri_list (EvApplication *application,
581 for (l = uri_list; l != NULL; l = l->next) {
582 ev_application_open_uri_at_dest (application, (char *)l->data,
583 screen, NULL, 0, FALSE,
589 ev_application_shutdown (EvApplication *application)
591 if (application->toolbars_model) {
592 g_object_unref (application->toolbars_model);
593 g_free (application->toolbars_file);
594 application->toolbars_model = NULL;
595 application->toolbars_file = NULL;
598 #ifndef HAVE_GTK_RECENT
599 if (application->recent_model) {
600 g_object_unref (application->recent_model);
601 application->recent_model = NULL;
605 #ifdef WITH_GTK_PRINT
606 #if GTK_CHECK_VERSION (2, 11, 0)
607 if (application->print_settings_file) {
608 if (application->print_settings) {
609 GError *error = NULL;
611 gtk_print_settings_to_file (application->print_settings,
612 application->print_settings_file,
615 g_warning (error->message);
616 g_error_free (error);
619 g_object_unref (application->print_settings);
620 application->print_settings = NULL;
623 g_free (application->print_settings_file);
624 application->print_settings_file = NULL;
626 #else /* ! GTK 2.11.0 */
627 if (application->print_settings) {
628 g_object_unref (application->print_settings);
629 application->print_settings = NULL;
631 #endif /* GTK 2.11.0 */
632 #endif /* WITH_GTK_PRINT */
634 g_free (application->last_chooser_uri);
635 g_object_unref (application);
641 ev_application_class_init (EvApplicationClass *ev_application_class)
646 ev_application_init (EvApplication *ev_application)
649 init_session (ev_application);
652 ev_application->toolbars_model = egg_toolbars_model_new ();
654 ev_application->toolbars_file = g_build_filename
655 (ev_dot_dir (), "evince_toolbar.xml", NULL);
657 egg_toolbars_model_load_names (ev_application->toolbars_model,
658 DATADIR "/evince-toolbar.xml");
660 if (!egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
661 ev_application->toolbars_file)) {
662 egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
663 DATADIR"/evince-toolbar.xml");
666 egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
667 EGG_TB_MODEL_NOT_REMOVABLE);
669 #ifndef HAVE_GTK_RECENT
670 ev_application->recent_model = egg_recent_model_new (EGG_RECENT_MODEL_SORT_MRU);
671 /* FIXME we should add a mime type filter but current eggrecent
672 has only a varargs style api which does not work well when
673 the list of mime types is dynamic */
674 egg_recent_model_set_limit (ev_application->recent_model, 5);
675 egg_recent_model_set_filter_groups (ev_application->recent_model,
677 #endif /* HAVE_GTK_RECENT */
681 * ev_application_get_windows:
682 * @application: The instance of the application.
684 * It creates a list of the top level windows.
686 * Returns: A #GList of the top level windows.
689 ev_application_get_windows (EvApplication *application)
691 GList *l, *toplevels;
692 GList *windows = NULL;
694 toplevels = gtk_window_list_toplevels ();
696 for (l = toplevels; l != NULL; l = l->next) {
697 if (EV_IS_WINDOW (l->data)) {
698 windows = g_list_append (windows, l->data);
702 g_list_free (toplevels);
708 ev_application_get_toolbars_model (EvApplication *application)
710 return application->toolbars_model;
713 #ifndef HAVE_GTK_RECENT
715 ev_application_get_recent_model (EvApplication *application)
717 return application->recent_model;
722 ev_application_save_toolbars_model (EvApplication *application)
724 egg_toolbars_model_save_toolbars (application->toolbars_model,
725 application->toolbars_file, "1.0");
729 ev_application_set_chooser_uri (EvApplication *application, const gchar *uri)
731 g_free (application->last_chooser_uri);
732 application->last_chooser_uri = g_strdup (uri);
736 ev_application_get_chooser_uri (EvApplication *application)
738 return application->last_chooser_uri;
742 ev_application_screensaver_enable (EvApplication *application)
744 if (application->scr_saver)
745 totem_scrsaver_enable (application->scr_saver);
749 ev_application_screensaver_disable (EvApplication *application)
751 if (application->scr_saver)
752 totem_scrsaver_disable (application->scr_saver);
755 #ifdef WITH_GTK_PRINT
757 ev_application_get_print_settings (EvApplication *application)
759 if (application->print_settings)
760 return application->print_settings;
762 #if GTK_CHECK_VERSION (2, 11, 0)
763 if (!application->print_settings_file) {
764 application->print_settings_file =
765 g_build_filename (ev_dot_dir (), "print-settings", NULL);
768 if (g_file_test (application->print_settings_file, G_FILE_TEST_IS_REGULAR)) {
769 GError *error = NULL;
771 application->print_settings =
772 gtk_print_settings_new_from_file (application->print_settings_file, &error);
775 g_warning (error->message);
776 g_error_free (error);
778 return application->print_settings;
781 #endif /* GTK 2.11.0 */
783 application->print_settings = gtk_print_settings_new ();
785 return application->print_settings;
789 ev_application_set_print_settings (EvApplication *application,
790 GtkPrintSettings *settings)
792 g_return_if_fail (GTK_IS_PRINT_SETTINGS (settings));
794 if (settings == application->print_settings)
797 if (application->print_settings)
798 g_object_unref (application->print_settings);
800 application->print_settings = g_object_ref (settings);
802 #endif /* WITH_GTK_PRINT */