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 * ev_application_open_window:
357 * @application: The instance of the application.
358 * @args: A #GHashTable with the arguments data.
359 * @timestamp: Current time value.
360 * @error: The #GError facility.
362 * Creates a new window and if the args are available, it's not NULL, it gets
363 * the screen from them and assigns the just created window to it. At last it
369 ev_application_open_window (EvApplication *application,
374 GtkWidget *new_window = ev_window_new ();
375 GdkScreen *screen = NULL;
378 screen = get_screen_from_args (args);
382 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
384 ev_application_add_icon_path_for_screen (screen);
386 gtk_widget_show (new_window);
388 gtk_window_present_with_time (GTK_WINDOW (new_window),
394 * ev_application_get_empty_window:
395 * @application: The instance of the application.
396 * @screen: The screen where the empty window will be search.
398 * It does look if there is any empty window in the indicated screen.
400 * Returns: The first empty #EvWindow in the passed #GdkScreen or NULL in other
404 ev_application_get_empty_window (EvApplication *application,
407 EvWindow *empty_window = NULL;
408 GList *windows = ev_application_get_windows (application);
411 for (l = windows; l != NULL; l = l->next) {
412 EvWindow *window = EV_WINDOW (l->data);
414 if (ev_window_is_empty (window) &&
415 gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
416 empty_window = window;
421 g_list_free (windows);
427 * ev_application_get_uri_window:
428 * @application: The instance of the application.
429 * @uri: The uri to be opened.
431 * It looks in the list of the windows for the one with the document represented
432 * by the passed uri on it. If the window is empty or the document isn't present
433 * on any window, it will return NULL.
435 * Returns: The #EvWindow where the document represented by the passed uri is
436 * shown, NULL in other case.
439 ev_application_get_uri_window (EvApplication *application, const char *uri)
441 EvWindow *uri_window = NULL;
442 GList *windows = gtk_window_list_toplevels ();
445 g_return_val_if_fail (uri != NULL, NULL);
447 for (l = windows; l != NULL; l = l->next) {
448 if (EV_IS_WINDOW (l->data)) {
449 EvWindow *window = EV_WINDOW (l->data);
450 const char *window_uri = ev_window_get_uri (window);
452 if (window_uri && strcmp (window_uri, uri) == 0 && !ev_window_is_empty (window)) {
459 g_list_free (windows);
465 ev_application_add_icon_path_for_screen (GdkScreen *screen)
467 GtkIconTheme *icon_theme;
469 icon_theme = gtk_icon_theme_get_for_screen (screen);
474 gchar *ev_icons_path;
476 /* GtkIconTheme will then look in Evince custom hicolor dir
477 * for icons as well as the standard search paths
479 ev_icons_path = g_build_filename (DATADIR, "icons", NULL);
480 gtk_icon_theme_get_search_path (icon_theme, &path, &n_paths);
481 for (i = n_paths - 1; i >= 0; i--) {
482 if (g_ascii_strcasecmp (ev_icons_path, path[i]) == 0)
487 gtk_icon_theme_append_search_path (icon_theme,
490 g_free (ev_icons_path);
496 * ev_application_open_uri_at_dest:
497 * @application: The instance of the application.
498 * @uri: The uri to be opened.
499 * @screen: Thee screen where the link will be shown.
500 * @dest: The #EvLinkDest of the document.
501 * @mode: The run mode of the window.
502 * @unlink_temp_file: The unlink_temp_file option value.
503 * @timestamp: Current time value.
506 ev_application_open_uri_at_dest (EvApplication *application,
510 EvWindowRunMode mode,
511 gboolean unlink_temp_file,
514 EvWindow *new_window;
516 g_return_if_fail (uri != NULL);
518 ev_application_add_icon_path_for_screen (screen);
520 new_window = ev_application_get_uri_window (application, uri);
522 if (new_window == NULL) {
523 new_window = ev_application_get_empty_window (application, screen);
526 if (new_window == NULL) {
527 new_window = EV_WINDOW (ev_window_new ());
531 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
533 /* We need to load uri before showing the window, so
534 we can restore window size without flickering */
535 ev_window_open_uri (new_window, uri, dest, mode, unlink_temp_file);
537 ev_document_fc_mutex_lock ();
538 gtk_widget_show (GTK_WIDGET (new_window));
539 ev_document_fc_mutex_unlock ();
541 gtk_window_present_with_time (GTK_WINDOW (new_window),
546 * ev_application_open_uri:
547 * @application: The instance of the application.
548 * @uri: The uri to be opened
549 * @args: A #GHashTable with the arguments data.
550 * @timestamp: Current time value.
551 * @error: The #GError facility.
554 ev_application_open_uri (EvApplication *application,
560 EvLinkDest *dest = NULL;
561 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
562 gboolean unlink_temp_file = FALSE;
563 GdkScreen *screen = NULL;
566 screen = get_screen_from_args (args);
567 dest = get_destination_from_args (args);
568 mode = get_window_run_mode_from_args (args);
569 unlink_temp_file = (mode == EV_WINDOW_MODE_PREVIEW &&
570 get_unlink_temp_file_from_args (args));
573 ev_application_open_uri_at_dest (application, uri, screen,
574 dest, mode, unlink_temp_file,
578 g_object_unref (dest);
584 ev_application_open_uri_list (EvApplication *application,
591 for (l = uri_list; l != NULL; l = l->next) {
592 ev_application_open_uri_at_dest (application, (char *)l->data,
593 screen, NULL, 0, FALSE,
599 ev_application_shutdown (EvApplication *application)
601 if (application->toolbars_model) {
602 g_object_unref (application->toolbars_model);
603 g_free (application->toolbars_file);
604 application->toolbars_model = NULL;
605 application->toolbars_file = NULL;
608 #ifndef HAVE_GTK_RECENT
609 if (application->recent_model) {
610 g_object_unref (application->recent_model);
611 application->recent_model = NULL;
615 #ifdef WITH_GTK_PRINT
616 #if GTK_CHECK_VERSION (2, 11, 0)
617 if (application->print_settings_file) {
618 if (application->print_settings) {
619 GError *error = NULL;
621 gtk_print_settings_to_file (application->print_settings,
622 application->print_settings_file,
625 g_warning (error->message);
626 g_error_free (error);
629 g_object_unref (application->print_settings);
630 application->print_settings = NULL;
633 g_free (application->print_settings_file);
634 application->print_settings_file = NULL;
636 #else /* ! GTK 2.11.0 */
637 if (application->print_settings) {
638 g_object_unref (application->print_settings);
639 application->print_settings = NULL;
641 #endif /* GTK 2.11.0 */
642 #endif /* WITH_GTK_PRINT */
644 g_free (application->last_chooser_uri);
645 g_object_unref (application);
651 ev_application_class_init (EvApplicationClass *ev_application_class)
656 ev_application_init (EvApplication *ev_application)
659 init_session (ev_application);
662 ev_application->toolbars_model = egg_toolbars_model_new ();
664 ev_application->toolbars_file = g_build_filename
665 (ev_dot_dir (), "evince_toolbar.xml", NULL);
667 egg_toolbars_model_load_names (ev_application->toolbars_model,
668 DATADIR "/evince-toolbar.xml");
670 if (!egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
671 ev_application->toolbars_file)) {
672 egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
673 DATADIR"/evince-toolbar.xml");
676 egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
677 EGG_TB_MODEL_NOT_REMOVABLE);
679 #ifndef HAVE_GTK_RECENT
680 ev_application->recent_model = egg_recent_model_new (EGG_RECENT_MODEL_SORT_MRU);
681 /* FIXME we should add a mime type filter but current eggrecent
682 has only a varargs style api which does not work well when
683 the list of mime types is dynamic */
684 egg_recent_model_set_limit (ev_application->recent_model, 5);
685 egg_recent_model_set_filter_groups (ev_application->recent_model,
687 #endif /* HAVE_GTK_RECENT */
691 * ev_application_get_windows:
692 * @application: The instance of the application.
694 * It creates a list of the top level windows.
696 * Returns: A #GList of the top level windows.
699 ev_application_get_windows (EvApplication *application)
701 GList *l, *toplevels;
702 GList *windows = NULL;
704 toplevels = gtk_window_list_toplevels ();
706 for (l = toplevels; l != NULL; l = l->next) {
707 if (EV_IS_WINDOW (l->data)) {
708 windows = g_list_append (windows, l->data);
712 g_list_free (toplevels);
718 ev_application_get_toolbars_model (EvApplication *application)
720 return application->toolbars_model;
723 #ifndef HAVE_GTK_RECENT
725 ev_application_get_recent_model (EvApplication *application)
727 return application->recent_model;
732 ev_application_save_toolbars_model (EvApplication *application)
734 egg_toolbars_model_save_toolbars (application->toolbars_model,
735 application->toolbars_file, "1.0");
739 ev_application_set_chooser_uri (EvApplication *application, const gchar *uri)
741 g_free (application->last_chooser_uri);
742 application->last_chooser_uri = g_strdup (uri);
746 ev_application_get_chooser_uri (EvApplication *application)
748 return application->last_chooser_uri;
752 ev_application_screensaver_enable (EvApplication *application)
754 if (application->scr_saver)
755 totem_scrsaver_enable (application->scr_saver);
759 ev_application_screensaver_disable (EvApplication *application)
761 if (application->scr_saver)
762 totem_scrsaver_disable (application->scr_saver);
765 #ifdef WITH_GTK_PRINT
767 ev_application_get_print_settings (EvApplication *application)
769 if (application->print_settings)
770 return application->print_settings;
772 #if GTK_CHECK_VERSION (2, 11, 0)
773 if (!application->print_settings_file) {
774 application->print_settings_file =
775 g_build_filename (ev_dot_dir (), "print-settings", NULL);
778 if (g_file_test (application->print_settings_file, G_FILE_TEST_IS_REGULAR)) {
779 GError *error = NULL;
781 application->print_settings =
782 gtk_print_settings_new_from_file (application->print_settings_file, &error);
785 g_warning (error->message);
786 g_error_free (error);
788 return application->print_settings;
791 #endif /* GTK 2.11.0 */
793 application->print_settings = gtk_print_settings_new ();
795 return application->print_settings;
799 ev_application_set_print_settings (EvApplication *application,
800 GtkPrintSettings *settings)
802 g_return_if_fail (GTK_IS_PRINT_SETTINGS (settings));
804 if (settings == application->print_settings)
807 if (application->print_settings)
808 g_object_unref (application->print_settings);
810 application->print_settings = g_object_ref (settings);
812 #endif /* WITH_GTK_PRINT */