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.
28 #include <glib/gi18n.h>
32 #include "totem-scrsaver.h"
33 #include "eggsmclient.h"
35 #include "ev-application.h"
36 #include "ev-document-factory.h"
37 #include "ev-file-helpers.h"
41 #include "ev-media-player-keys.h"
42 #endif /* ENABLE_DBUS */
45 #include <dbus/dbus-glib-bindings.h>
46 #include "ev-application-service.h"
49 static void ev_application_add_icon_path_for_screen (GdkScreen *screen);
50 static void ev_application_save_print_settings (EvApplication *application);
52 struct _EvApplication {
53 GObject base_instance;
55 gchar *accel_map_file;
59 EggToolbarsModel *toolbars_model;
61 TotemScrsaver *scr_saver;
63 EggSMClient *smclient;
65 gchar *last_chooser_uri;
68 EvMediaPlayerKeys *keys;
69 #endif /* ENABLE_DBUS */
71 GtkPrintSettings *print_settings;
72 GtkPageSetup *page_setup;
73 GKeyFile *print_settings_file;
76 struct _EvApplicationClass {
77 GObjectClass base_class;
80 G_DEFINE_TYPE (EvApplication, ev_application, G_TYPE_OBJECT);
82 #define APPLICATION_SERVICE_NAME "org.gnome.evince.ApplicationService"
84 #define EV_PRINT_SETTINGS_FILE "print-settings"
85 #define EV_PRINT_SETTINGS_GROUP "Print Settings"
86 #define EV_PAGE_SETUP_GROUP "Page Setup"
90 ev_application_register_service (EvApplication *application)
92 static DBusGConnection *connection = NULL;
93 DBusGProxy *driver_proxy;
95 guint request_name_result;
98 g_warning ("Service already registered.");
102 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &err);
103 if (connection == NULL) {
104 g_warning ("Service registration failed.");
110 driver_proxy = dbus_g_proxy_new_for_name (connection,
113 DBUS_INTERFACE_DBUS);
115 if (!org_freedesktop_DBus_request_name (driver_proxy,
116 APPLICATION_SERVICE_NAME,
117 DBUS_NAME_FLAG_DO_NOT_QUEUE,
118 &request_name_result, &err)) {
119 g_warning ("Service registration failed.");
120 g_clear_error (&err);
123 g_object_unref (driver_proxy);
125 if (request_name_result == DBUS_REQUEST_NAME_REPLY_EXISTS) {
129 dbus_g_object_type_install_info (EV_TYPE_APPLICATION,
130 &dbus_glib_ev_application_object_info);
131 dbus_g_connection_register_g_object (connection,
132 "/org/gnome/evince/Evince",
133 G_OBJECT (application));
135 application->scr_saver = totem_scrsaver_new (connection);
139 #endif /* ENABLE_DBUS */
142 * ev_application_get_instance:
144 * Checks for #EvApplication instance, if it doesn't exist it does create it.
146 * Returns: an instance of the #EvApplication data.
149 ev_application_get_instance (void)
151 static EvApplication *instance;
154 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
162 ev_application_load_session (EvApplication *application)
164 GKeyFile *state_file;
167 if (!egg_sm_client_is_resumed (application->smclient))
170 state_file = egg_sm_client_get_state_file (application->smclient);
174 uri_list = g_key_file_get_string_list (state_file,
181 for (i = 0; uri_list[i]; i++) {
182 if (g_ascii_strcasecmp (uri_list[i], "empty-window") == 0)
183 ev_application_open_window (application, NULL, GDK_CURRENT_TIME, NULL);
185 ev_application_open_uri (application, uri_list[i], NULL, GDK_CURRENT_TIME, NULL);
187 g_strfreev (uri_list);
189 g_key_file_free (state_file);
195 smclient_save_state_cb (EggSMClient *client,
196 GKeyFile *state_file,
197 EvApplication *application)
201 const gchar **uri_list;
202 const gchar *empty = "empty-window";
204 windows = ev_application_get_windows (application);
208 uri_list = g_new (const gchar *, g_list_length (windows));
209 for (l = windows, i = 0; l != NULL; l = g_list_next (l), i++) {
210 EvWindow *window = EV_WINDOW (l->data);
212 if (ev_window_is_empty (window))
215 uri_list[i] = ev_window_get_uri (window);
217 g_key_file_set_string_list (state_file,
220 (const char **)uri_list,
226 smclient_quit_cb (EggSMClient *client,
227 EvApplication *application)
229 ev_application_shutdown (application);
233 ev_application_init_session (EvApplication *application)
235 application->smclient = egg_sm_client_get ();
236 g_signal_connect (application->smclient, "save_state",
237 G_CALLBACK (smclient_save_state_cb),
239 g_signal_connect (application->smclient, "quit",
240 G_CALLBACK (smclient_quit_cb),
245 * ev_display_open_if_needed:
246 * @name: the name of the display to be open if it's needed.
248 * Search among all the open displays if any of them have the same name as the
249 * passed name. If the display isn't found it tries the open it.
251 * Returns: a #GdkDisplay of the display with the passed name.
254 ev_display_open_if_needed (const gchar *name)
258 GdkDisplay *display = NULL;
260 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
262 for (l = displays; l != NULL; l = l->next) {
263 const gchar *display_name = gdk_display_get_name ((GdkDisplay *) l->data);
265 if (g_ascii_strcasecmp (display_name, name) == 0) {
271 g_slist_free (displays);
273 return display != NULL ? display : gdk_display_open (name);
277 * get_screen_from_args:
278 * @args: a #GHashTable with data passed to the application.
280 * Looks for the screen in the display available in the hash table passed to the
281 * application. If the display isn't opened, it's opened and the #GdkScreen
282 * assigned to the screen in that display returned.
284 * Returns: the #GdkScreen assigned to the screen on the display indicated by
285 * the data on the #GHashTable.
288 get_screen_from_args (GHashTable *args)
290 GValue *value = NULL;
291 GdkDisplay *display = NULL;
292 GdkScreen *screen = NULL;
294 g_assert (args != NULL);
296 value = g_hash_table_lookup (args, "display");
298 const gchar *display_name;
300 display_name = g_value_get_string (value);
301 display = ev_display_open_if_needed (display_name);
304 value = g_hash_table_lookup (args, "screen");
308 screen_number = g_value_get_int (value);
309 screen = gdk_display_get_screen (display, screen_number);
316 * get_window_run_mode_from_args:
317 * @args: a #GHashTable with data passed to the application.
319 * It does look if the mode option has been passed from command line, using it
320 * as the window run mode, otherwise the run mode will be the normal mode.
322 * Returns: The window run mode passed from command line or
323 * EV_WINDOW_MODE_NORMAL in other case.
325 static EvWindowRunMode
326 get_window_run_mode_from_args (GHashTable *args)
328 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
329 GValue *value = NULL;
331 g_assert (args != NULL);
333 value = g_hash_table_lookup (args, "mode");
335 mode = g_value_get_uint (value);
342 * get_destination_from_args:
343 * @args: a #GHashTable with data passed to the application.
345 * It does look for the page-label argument parsed from the command line and
346 * if it does exist, it returns an #EvLinkDest.
348 * Returns: An #EvLinkDest to page-label if it has been passed from the command
349 * line, NULL in other case.
352 get_destination_from_args (GHashTable *args)
354 EvLinkDest *dest = NULL;
355 GValue *value = NULL;
357 g_assert (args != NULL);
359 value = g_hash_table_lookup (args, "page-label");
361 const gchar *page_label;
363 page_label = g_value_get_string (value);
364 dest = ev_link_dest_new_page_label (page_label);
371 get_find_string_from_args (GHashTable *args)
373 GValue *value = NULL;
375 g_assert (args != NULL);
377 value = g_hash_table_lookup (args, "find-string");
379 return value ? g_value_get_string (value) : NULL;
383 * get_unlink_temp_file_from_args:
384 * @args: a #GHashTable with data passed to the application.
386 * It does look if the unlink-temp-file option has been passed from the command
387 * line returning it's boolean representation, otherwise it does return %FALSE.
389 * Returns: the boolean representation of the unlink-temp-file value or %FALSE
393 get_unlink_temp_file_from_args (GHashTable *args)
395 gboolean unlink_temp_file = FALSE;
396 GValue *value = NULL;
398 g_assert (args != NULL);
400 value = g_hash_table_lookup (args, "unlink-temp-file");
402 unlink_temp_file = g_value_get_boolean (value);
405 return unlink_temp_file;
409 get_print_settings_from_args (GHashTable *args)
411 const gchar *print_settings = NULL;
412 GValue *value = NULL;
414 g_assert (args != NULL);
416 value = g_hash_table_lookup (args, "print-settings");
418 print_settings = g_value_get_string (value);
421 return print_settings;
425 * ev_application_open_window:
426 * @application: The instance of the application.
427 * @args: A #GHashTable with the arguments data.
428 * @timestamp: Current time value.
429 * @error: The #GError facility.
431 * Creates a new window and if the args are available, it's not NULL, it gets
432 * the screen from them and assigns the just created window to it. At last it
438 ev_application_open_window (EvApplication *application,
443 GtkWidget *new_window = ev_window_new ();
444 GdkScreen *screen = NULL;
447 screen = get_screen_from_args (args);
451 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
453 ev_application_add_icon_path_for_screen (screen);
455 if (!GTK_WIDGET_REALIZED (new_window))
456 gtk_widget_realize (new_window);
459 timestamp = gdk_x11_get_server_time (GTK_WIDGET (new_window)->window);
460 gdk_x11_window_set_user_time (GTK_WIDGET (new_window)->window, timestamp);
462 gtk_window_present (GTK_WINDOW (new_window));
468 * ev_application_get_empty_window:
469 * @application: The instance of the application.
470 * @screen: The screen where the empty window will be search.
472 * It does look if there is any empty window in the indicated screen.
474 * Returns: The first empty #EvWindow in the passed #GdkScreen or NULL in other
478 ev_application_get_empty_window (EvApplication *application,
481 EvWindow *empty_window = NULL;
482 GList *windows = ev_application_get_windows (application);
485 for (l = windows; l != NULL; l = l->next) {
486 EvWindow *window = EV_WINDOW (l->data);
488 if (ev_window_is_empty (window) &&
489 gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
490 empty_window = window;
495 g_list_free (windows);
501 * ev_application_get_uri_window:
502 * @application: The instance of the application.
503 * @uri: The uri to be opened.
505 * It looks in the list of the windows for the one with the document represented
506 * by the passed uri on it. If the window is empty or the document isn't present
507 * on any window, it will return NULL.
509 * Returns: The #EvWindow where the document represented by the passed uri is
510 * shown, NULL in other case.
513 ev_application_get_uri_window (EvApplication *application, const char *uri)
515 EvWindow *uri_window = NULL;
516 GList *windows = gtk_window_list_toplevels ();
519 g_return_val_if_fail (uri != NULL, NULL);
521 for (l = windows; l != NULL; l = l->next) {
522 if (EV_IS_WINDOW (l->data)) {
523 EvWindow *window = EV_WINDOW (l->data);
524 const char *window_uri = ev_window_get_uri (window);
526 if (window_uri && strcmp (window_uri, uri) == 0 && !ev_window_is_empty (window)) {
533 g_list_free (windows);
539 ev_application_add_icon_path_for_screen (GdkScreen *screen)
541 GtkIconTheme *icon_theme;
543 icon_theme = screen ? gtk_icon_theme_get_for_screen (screen) : gtk_icon_theme_get_default ();
548 gchar *ev_icons_path;
550 /* GtkIconTheme will then look in Evince custom hicolor dir
551 * for icons as well as the standard search paths
553 ev_icons_path = g_build_filename (DATADIR, "icons", NULL);
554 gtk_icon_theme_get_search_path (icon_theme, &path, &n_paths);
555 for (i = n_paths - 1; i >= 0; i--) {
556 if (g_ascii_strcasecmp (ev_icons_path, path[i]) == 0)
561 gtk_icon_theme_append_search_path (icon_theme,
564 g_free (ev_icons_path);
570 * ev_application_open_uri_at_dest:
571 * @application: The instance of the application.
572 * @uri: The uri to be opened.
573 * @screen: Thee screen where the link will be shown.
574 * @dest: The #EvLinkDest of the document.
575 * @mode: The run mode of the window.
576 * @unlink_temp_file: The unlink_temp_file option value.
577 * @timestamp: Current time value.
580 ev_application_open_uri_at_dest (EvApplication *application,
584 EvWindowRunMode mode,
585 const gchar *search_string,
586 gboolean unlink_temp_file,
587 const gchar *print_settings,
590 EvWindow *new_window;
592 g_return_if_fail (uri != NULL);
594 ev_application_add_icon_path_for_screen (screen);
596 new_window = ev_application_get_uri_window (application, uri);
598 if (new_window == NULL) {
599 new_window = ev_application_get_empty_window (application, screen);
602 if (new_window == NULL) {
603 new_window = EV_WINDOW (ev_window_new ());
607 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
609 /* We need to load uri before showing the window, so
610 we can restore window size without flickering */
611 ev_window_open_uri (new_window, uri, dest, mode, search_string,
612 unlink_temp_file, print_settings);
614 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (new_window)))
615 gtk_widget_realize (GTK_WIDGET (new_window));
618 timestamp = gdk_x11_get_server_time (GTK_WIDGET (new_window)->window);
619 gdk_x11_window_set_user_time (GTK_WIDGET (new_window)->window, timestamp);
621 ev_document_fc_mutex_lock ();
622 gtk_window_present (GTK_WINDOW (new_window));
623 ev_document_fc_mutex_unlock ();
627 * ev_application_open_uri:
628 * @application: The instance of the application.
629 * @uri: The uri to be opened
630 * @args: A #GHashTable with the arguments data.
631 * @timestamp: Current time value.
632 * @error: The #GError facility.
635 ev_application_open_uri (EvApplication *application,
641 EvLinkDest *dest = NULL;
642 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
643 const gchar *search_string = NULL;
644 gboolean unlink_temp_file = FALSE;
645 const gchar *print_settings = NULL;
646 GdkScreen *screen = NULL;
649 screen = get_screen_from_args (args);
650 dest = get_destination_from_args (args);
651 mode = get_window_run_mode_from_args (args);
652 search_string = get_find_string_from_args (args);
653 unlink_temp_file = (mode == EV_WINDOW_MODE_PREVIEW &&
654 get_unlink_temp_file_from_args (args));
655 print_settings = get_print_settings_from_args (args);
658 ev_application_open_uri_at_dest (application, uri, screen,
659 dest, mode, search_string,
661 print_settings, timestamp);
664 g_object_unref (dest);
670 ev_application_open_uri_list (EvApplication *application,
677 for (l = uri_list; l != NULL; l = l->next) {
678 ev_application_open_uri_at_dest (application, (char *)l->data,
679 screen, NULL, 0, NULL,
680 FALSE, NULL, timestamp);
685 ev_application_shutdown (EvApplication *application)
687 if (application->accel_map_file) {
688 gtk_accel_map_save (application->accel_map_file);
689 g_free (application->accel_map_file);
690 application->accel_map_file = NULL;
693 if (application->toolbars_model) {
694 g_object_unref (application->toolbars_model);
695 g_free (application->toolbars_file);
696 application->toolbars_model = NULL;
697 application->toolbars_file = NULL;
700 ev_application_save_print_settings (application);
702 if (application->print_settings_file) {
703 g_key_file_free (application->print_settings_file);
704 application->print_settings_file = NULL;
707 if (application->print_settings) {
708 g_object_unref (application->print_settings);
709 application->print_settings = NULL;
712 if (application->page_setup) {
713 g_object_unref (application->page_setup);
714 application->page_setup = NULL;
718 if (application->keys) {
719 g_object_unref (application->keys);
720 application->keys = NULL;
722 #endif /* ENABLE_DBUS */
724 g_free (application->last_chooser_uri);
725 g_object_unref (application);
731 ev_application_class_init (EvApplicationClass *ev_application_class)
736 ev_application_init (EvApplication *ev_application)
739 const gchar *home_dir;
741 ev_application_init_session (ev_application);
743 home_dir = g_get_home_dir ();
745 ev_application->accel_map_file = g_build_filename (home_dir,
750 gtk_accel_map_load (ev_application->accel_map_file);
753 ev_application->toolbars_model = egg_toolbars_model_new ();
755 ev_application->toolbars_file = g_build_filename
756 (ev_dot_dir (), "evince_toolbar.xml", NULL);
758 egg_toolbars_model_load_names (ev_application->toolbars_model,
759 DATADIR "/evince-toolbar.xml");
761 if (!egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
762 ev_application->toolbars_file)) {
763 egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
764 DATADIR"/evince-toolbar.xml");
767 /* Open item doesn't exist anymore,
768 * convert it to OpenRecent for compatibility
770 for (i = 0; i < egg_toolbars_model_n_items (ev_application->toolbars_model, 0); i++) {
773 item = egg_toolbars_model_item_nth (ev_application->toolbars_model, 0, i);
774 if (g_ascii_strcasecmp (item, "FileOpen") == 0) {
775 egg_toolbars_model_remove_item (ev_application->toolbars_model, 0, i);
776 egg_toolbars_model_add_item (ev_application->toolbars_model, 0, i,
778 ev_application_save_toolbars_model (ev_application);
783 egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
784 EGG_TB_MODEL_NOT_REMOVABLE);
787 ev_application->keys = ev_media_player_keys_new ();
788 #endif /* ENABLE_DBUS */
792 * ev_application_get_windows:
793 * @application: The instance of the application.
795 * It creates a list of the top level windows.
797 * Returns: A #GList of the top level windows.
800 ev_application_get_windows (EvApplication *application)
802 GList *l, *toplevels;
803 GList *windows = NULL;
805 toplevels = gtk_window_list_toplevels ();
807 for (l = toplevels; l != NULL; l = l->next) {
808 if (EV_IS_WINDOW (l->data)) {
809 windows = g_list_append (windows, l->data);
813 g_list_free (toplevels);
819 * ev_application_get_media_keys:
820 * @application: The instance of the application.
822 * It gives you access to the media player keys handler object.
824 * Returns: A #EvMediaPlayerKeys.
827 ev_application_get_media_keys (EvApplication *application)
830 return G_OBJECT (application->keys);
833 #endif /* ENABLE_DBUS */
837 ev_application_get_toolbars_model (EvApplication *application)
839 return application->toolbars_model;
843 ev_application_save_toolbars_model (EvApplication *application)
845 egg_toolbars_model_save_toolbars (application->toolbars_model,
846 application->toolbars_file, "1.0");
850 ev_application_set_chooser_uri (EvApplication *application, const gchar *uri)
852 g_free (application->last_chooser_uri);
853 application->last_chooser_uri = g_strdup (uri);
857 ev_application_get_chooser_uri (EvApplication *application)
859 return application->last_chooser_uri;
863 ev_application_screensaver_enable (EvApplication *application)
865 if (application->scr_saver)
866 totem_scrsaver_enable (application->scr_saver);
870 ev_application_screensaver_disable (EvApplication *application)
872 if (application->scr_saver)
873 totem_scrsaver_disable (application->scr_saver);
877 ev_application_get_print_settings_file (EvApplication *application)
881 if (application->print_settings_file)
882 return application->print_settings_file;
884 application->print_settings_file = g_key_file_new ();
886 filename = g_build_filename (ev_dot_dir (), EV_PRINT_SETTINGS_FILE, NULL);
887 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR)) {
888 GError *error = NULL;
890 g_key_file_load_from_file (application->print_settings_file,
892 G_KEY_FILE_KEEP_COMMENTS |
893 G_KEY_FILE_KEEP_TRANSLATIONS,
896 g_warning ("%s", error->message);
897 g_error_free (error);
902 return application->print_settings_file;
906 ev_application_save_print_settings (EvApplication *application)
912 GError *error = NULL;
914 if (!application->print_settings && !application->page_setup)
917 key_file = ev_application_get_print_settings_file (application);
918 if (application->print_settings)
919 gtk_print_settings_to_key_file (application->print_settings,
921 EV_PRINT_SETTINGS_GROUP);
922 if (application->page_setup)
923 gtk_page_setup_to_key_file (application->page_setup,
925 EV_PAGE_SETUP_GROUP);
927 filename = g_build_filename (ev_dot_dir (), EV_PRINT_SETTINGS_FILE, NULL);
928 data = g_key_file_to_data (key_file, (gsize *)&data_length, NULL);
929 g_file_set_contents (filename, data, data_length, &error);
931 g_warning ("%s", error->message);
932 g_error_free (error);
939 ev_application_get_print_settings (EvApplication *application)
942 GtkPrintSettings *print_settings;
944 if (application->print_settings)
945 return application->print_settings;
947 key_file = ev_application_get_print_settings_file (application);
948 print_settings = g_key_file_has_group (key_file, EV_PRINT_SETTINGS_GROUP) ?
949 gtk_print_settings_new_from_key_file (key_file, EV_PRINT_SETTINGS_GROUP, NULL) :
950 gtk_print_settings_new ();
952 application->print_settings = print_settings ? print_settings : gtk_print_settings_new ();
954 return application->print_settings;
958 ev_application_set_print_settings (EvApplication *application,
959 GtkPrintSettings *settings)
963 g_return_if_fail (GTK_IS_PRINT_SETTINGS (settings));
965 if (settings == application->print_settings)
968 key_file = ev_application_get_print_settings_file (application);
970 if (application->print_settings)
971 g_object_unref (application->print_settings);
973 application->print_settings = g_object_ref (settings);
974 gtk_print_settings_to_key_file (settings, key_file, EV_PRINT_SETTINGS_GROUP);
978 ev_application_get_page_setup (EvApplication *application)
981 GtkPageSetup *page_setup;
983 if (application->page_setup)
984 return application->page_setup;
986 key_file = ev_application_get_print_settings_file (application);
987 page_setup = g_key_file_has_group (key_file, EV_PAGE_SETUP_GROUP) ?
988 gtk_page_setup_new_from_key_file (key_file, EV_PAGE_SETUP_GROUP, NULL) :
989 gtk_page_setup_new ();
991 application->page_setup = page_setup ? page_setup : gtk_page_setup_new ();
993 return application->page_setup;
997 ev_application_set_page_setup (EvApplication *application,
998 GtkPageSetup *page_setup)
1002 g_return_if_fail (GTK_IS_PAGE_SETUP (page_setup));
1004 if (page_setup == application->page_setup)
1007 key_file = ev_application_get_print_settings_file (application);
1009 if (application->page_setup)
1010 g_object_unref (application->page_setup);
1012 application->page_setup = g_object_ref (page_setup);
1013 gtk_page_setup_to_key_file (page_setup, key_file, EV_PAGE_SETUP_GROUP);