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_find_string_from_args (GHashTable *args)
328 GValue *value = NULL;
330 g_assert (args != NULL);
332 value = g_hash_table_lookup (args, "find-string");
334 return value ? g_value_get_string (value) : NULL;
338 * get_unlink_temp_file_from_args:
339 * @args: a #GHashTable with data passed to the application.
341 * It does look if the unlink-temp-file option has been passed from the command
342 * line returning it's boolean representation, otherwise it does return %FALSE.
344 * Returns: the boolean representation of the unlink-temp-file value or %FALSE
348 get_unlink_temp_file_from_args (GHashTable *args)
350 gboolean unlink_temp_file = FALSE;
351 GValue *value = NULL;
353 g_assert (args != NULL);
355 value = g_hash_table_lookup (args, "unlink-temp-file");
357 unlink_temp_file = g_value_get_boolean (value);
360 return unlink_temp_file;
364 get_print_settings_from_args (GHashTable *args)
366 const gchar *print_settings = NULL;
367 GValue *value = NULL;
369 g_assert (args != NULL);
371 value = g_hash_table_lookup (args, "print-settings");
373 print_settings = g_value_get_string (value);
376 return print_settings;
380 * ev_application_open_window:
381 * @application: The instance of the application.
382 * @args: A #GHashTable with the arguments data.
383 * @timestamp: Current time value.
384 * @error: The #GError facility.
386 * Creates a new window and if the args are available, it's not NULL, it gets
387 * the screen from them and assigns the just created window to it. At last it
393 ev_application_open_window (EvApplication *application,
398 GtkWidget *new_window = ev_window_new ();
399 GdkScreen *screen = NULL;
402 screen = get_screen_from_args (args);
406 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
408 ev_application_add_icon_path_for_screen (screen);
410 gtk_widget_show (new_window);
412 gtk_window_present_with_time (GTK_WINDOW (new_window),
418 * ev_application_get_empty_window:
419 * @application: The instance of the application.
420 * @screen: The screen where the empty window will be search.
422 * It does look if there is any empty window in the indicated screen.
424 * Returns: The first empty #EvWindow in the passed #GdkScreen or NULL in other
428 ev_application_get_empty_window (EvApplication *application,
431 EvWindow *empty_window = NULL;
432 GList *windows = ev_application_get_windows (application);
435 for (l = windows; l != NULL; l = l->next) {
436 EvWindow *window = EV_WINDOW (l->data);
438 if (ev_window_is_empty (window) &&
439 gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
440 empty_window = window;
445 g_list_free (windows);
451 * ev_application_get_uri_window:
452 * @application: The instance of the application.
453 * @uri: The uri to be opened.
455 * It looks in the list of the windows for the one with the document represented
456 * by the passed uri on it. If the window is empty or the document isn't present
457 * on any window, it will return NULL.
459 * Returns: The #EvWindow where the document represented by the passed uri is
460 * shown, NULL in other case.
463 ev_application_get_uri_window (EvApplication *application, const char *uri)
465 EvWindow *uri_window = NULL;
466 GList *windows = gtk_window_list_toplevels ();
469 g_return_val_if_fail (uri != NULL, NULL);
471 for (l = windows; l != NULL; l = l->next) {
472 if (EV_IS_WINDOW (l->data)) {
473 EvWindow *window = EV_WINDOW (l->data);
474 const char *window_uri = ev_window_get_uri (window);
476 if (window_uri && strcmp (window_uri, uri) == 0 && !ev_window_is_empty (window)) {
483 g_list_free (windows);
489 ev_application_add_icon_path_for_screen (GdkScreen *screen)
491 GtkIconTheme *icon_theme;
493 icon_theme = gtk_icon_theme_get_for_screen (screen);
498 gchar *ev_icons_path;
500 /* GtkIconTheme will then look in Evince custom hicolor dir
501 * for icons as well as the standard search paths
503 ev_icons_path = g_build_filename (DATADIR, "icons", NULL);
504 gtk_icon_theme_get_search_path (icon_theme, &path, &n_paths);
505 for (i = n_paths - 1; i >= 0; i--) {
506 if (g_ascii_strcasecmp (ev_icons_path, path[i]) == 0)
511 gtk_icon_theme_append_search_path (icon_theme,
514 g_free (ev_icons_path);
520 * ev_application_open_uri_at_dest:
521 * @application: The instance of the application.
522 * @uri: The uri to be opened.
523 * @screen: Thee screen where the link will be shown.
524 * @dest: The #EvLinkDest of the document.
525 * @mode: The run mode of the window.
526 * @unlink_temp_file: The unlink_temp_file option value.
527 * @timestamp: Current time value.
530 ev_application_open_uri_at_dest (EvApplication *application,
534 EvWindowRunMode mode,
535 const gchar *search_string,
536 gboolean unlink_temp_file,
537 const gchar *print_settings,
540 EvWindow *new_window;
542 g_return_if_fail (uri != NULL);
544 ev_application_add_icon_path_for_screen (screen);
546 new_window = ev_application_get_uri_window (application, uri);
548 if (new_window == NULL) {
549 new_window = ev_application_get_empty_window (application, screen);
552 if (new_window == NULL) {
553 new_window = EV_WINDOW (ev_window_new ());
557 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
559 /* We need to load uri before showing the window, so
560 we can restore window size without flickering */
561 ev_window_open_uri (new_window, uri, dest, mode, search_string,
562 unlink_temp_file, print_settings);
564 ev_document_fc_mutex_lock ();
565 gtk_widget_show (GTK_WIDGET (new_window));
566 ev_document_fc_mutex_unlock ();
568 gtk_window_present_with_time (GTK_WINDOW (new_window),
573 * ev_application_open_uri:
574 * @application: The instance of the application.
575 * @uri: The uri to be opened
576 * @args: A #GHashTable with the arguments data.
577 * @timestamp: Current time value.
578 * @error: The #GError facility.
581 ev_application_open_uri (EvApplication *application,
587 EvLinkDest *dest = NULL;
588 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
589 const gchar *search_string = NULL;
590 gboolean unlink_temp_file = FALSE;
591 const gchar *print_settings = NULL;
592 GdkScreen *screen = NULL;
595 screen = get_screen_from_args (args);
596 dest = get_destination_from_args (args);
597 mode = get_window_run_mode_from_args (args);
598 search_string = get_find_string_from_args (args);
599 unlink_temp_file = (mode == EV_WINDOW_MODE_PREVIEW &&
600 get_unlink_temp_file_from_args (args));
601 print_settings = get_print_settings_from_args (args);
604 ev_application_open_uri_at_dest (application, uri, screen,
605 dest, mode, search_string,
607 print_settings, timestamp);
610 g_object_unref (dest);
616 ev_application_open_uri_list (EvApplication *application,
623 for (l = uri_list; l != NULL; l = l->next) {
624 ev_application_open_uri_at_dest (application, (char *)l->data,
625 screen, NULL, 0, NULL,
626 FALSE, NULL, timestamp);
631 ev_application_shutdown (EvApplication *application)
633 if (application->toolbars_model) {
634 g_object_unref (application->toolbars_model);
635 g_free (application->toolbars_file);
636 application->toolbars_model = NULL;
637 application->toolbars_file = NULL;
640 #ifdef WITH_GTK_PRINT
641 #if GTK_CHECK_VERSION (2, 11, 0)
642 if (application->print_settings_file) {
643 if (application->print_settings) {
644 GError *error = NULL;
646 gtk_print_settings_to_file (application->print_settings,
647 application->print_settings_file,
650 g_warning (error->message);
651 g_error_free (error);
654 g_object_unref (application->print_settings);
655 application->print_settings = NULL;
658 g_free (application->print_settings_file);
659 application->print_settings_file = NULL;
661 #else /* ! GTK 2.11.0 */
662 if (application->print_settings) {
663 g_object_unref (application->print_settings);
664 application->print_settings = NULL;
666 #endif /* GTK 2.11.0 */
667 #endif /* WITH_GTK_PRINT */
669 g_free (application->last_chooser_uri);
670 g_object_unref (application);
676 ev_application_class_init (EvApplicationClass *ev_application_class)
681 ev_application_init (EvApplication *ev_application)
686 init_session (ev_application);
689 ev_application->toolbars_model = egg_toolbars_model_new ();
691 ev_application->toolbars_file = g_build_filename
692 (ev_dot_dir (), "evince_toolbar.xml", NULL);
694 egg_toolbars_model_load_names (ev_application->toolbars_model,
695 DATADIR "/evince-toolbar.xml");
697 if (!egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
698 ev_application->toolbars_file)) {
699 egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
700 DATADIR"/evince-toolbar.xml");
703 /* Open item doesn't exist anymore,
704 * convert it to OpenRecent for compatibility
706 for (i = 0; i < egg_toolbars_model_n_items (ev_application->toolbars_model, 0); i++) {
709 item = egg_toolbars_model_item_nth (ev_application->toolbars_model, 0, i);
710 if (g_ascii_strcasecmp (item, "FileOpen") == 0) {
711 egg_toolbars_model_remove_item (ev_application->toolbars_model, 0, i);
712 egg_toolbars_model_add_item (ev_application->toolbars_model, 0, i,
714 ev_application_save_toolbars_model (ev_application);
719 egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
720 EGG_TB_MODEL_NOT_REMOVABLE);
724 * ev_application_get_windows:
725 * @application: The instance of the application.
727 * It creates a list of the top level windows.
729 * Returns: A #GList of the top level windows.
732 ev_application_get_windows (EvApplication *application)
734 GList *l, *toplevels;
735 GList *windows = NULL;
737 toplevels = gtk_window_list_toplevels ();
739 for (l = toplevels; l != NULL; l = l->next) {
740 if (EV_IS_WINDOW (l->data)) {
741 windows = g_list_append (windows, l->data);
745 g_list_free (toplevels);
751 ev_application_get_toolbars_model (EvApplication *application)
753 return application->toolbars_model;
757 ev_application_save_toolbars_model (EvApplication *application)
759 egg_toolbars_model_save_toolbars (application->toolbars_model,
760 application->toolbars_file, "1.0");
764 ev_application_set_chooser_uri (EvApplication *application, const gchar *uri)
766 g_free (application->last_chooser_uri);
767 application->last_chooser_uri = g_strdup (uri);
771 ev_application_get_chooser_uri (EvApplication *application)
773 return application->last_chooser_uri;
777 ev_application_screensaver_enable (EvApplication *application)
779 if (application->scr_saver)
780 totem_scrsaver_enable (application->scr_saver);
784 ev_application_screensaver_disable (EvApplication *application)
786 if (application->scr_saver)
787 totem_scrsaver_disable (application->scr_saver);
790 #ifdef WITH_GTK_PRINT
792 ev_application_get_print_settings (EvApplication *application)
794 if (application->print_settings)
795 return application->print_settings;
797 #if GTK_CHECK_VERSION (2, 11, 0)
798 if (!application->print_settings_file) {
799 application->print_settings_file =
800 g_build_filename (ev_dot_dir (), "print-settings", NULL);
803 if (g_file_test (application->print_settings_file, G_FILE_TEST_IS_REGULAR)) {
804 GError *error = NULL;
806 application->print_settings =
807 gtk_print_settings_new_from_file (application->print_settings_file, &error);
810 g_warning (error->message);
811 g_error_free (error);
813 return application->print_settings;
816 #endif /* GTK 2.11.0 */
818 application->print_settings = gtk_print_settings_new ();
820 return application->print_settings;
824 ev_application_set_print_settings (EvApplication *application,
825 GtkPrintSettings *settings)
827 g_return_if_fail (GTK_IS_PRINT_SETTINGS (settings));
829 if (settings == application->print_settings)
832 if (application->print_settings)
833 g_object_unref (application->print_settings);
835 application->print_settings = g_object_ref (settings);
837 #endif /* WITH_GTK_PRINT */