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"
34 #include "eggdesktopfile.h"
36 #include "ev-application.h"
37 #include "ev-document-factory.h"
38 #include "ev-file-helpers.h"
42 #include "ev-media-player-keys.h"
43 #endif /* ENABLE_DBUS */
46 #include <dbus/dbus-glib-bindings.h>
47 #include "ev-application-service.h"
50 static void ev_application_add_icon_path_for_screen (GdkScreen *screen);
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 #if GTK_CHECK_VERSION (2, 11, 0)
73 gchar *print_settings_file;
77 struct _EvApplicationClass {
78 GObjectClass base_class;
81 G_DEFINE_TYPE (EvApplication, ev_application, G_TYPE_OBJECT);
83 #define APPLICATION_SERVICE_NAME "org.gnome.evince.ApplicationService"
87 ev_application_register_service (EvApplication *application)
89 static DBusGConnection *connection = NULL;
90 DBusGProxy *driver_proxy;
92 guint request_name_result;
95 g_warning ("Service already registered.");
99 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &err);
100 if (connection == NULL) {
101 g_warning ("Service registration failed.");
107 driver_proxy = dbus_g_proxy_new_for_name (connection,
110 DBUS_INTERFACE_DBUS);
112 if (!org_freedesktop_DBus_request_name (driver_proxy,
113 APPLICATION_SERVICE_NAME,
114 DBUS_NAME_FLAG_DO_NOT_QUEUE,
115 &request_name_result, &err)) {
116 g_warning ("Service registration failed.");
117 g_clear_error (&err);
120 g_object_unref (driver_proxy);
122 if (request_name_result == DBUS_REQUEST_NAME_REPLY_EXISTS) {
126 dbus_g_object_type_install_info (EV_TYPE_APPLICATION,
127 &dbus_glib_ev_application_object_info);
128 dbus_g_connection_register_g_object (connection,
129 "/org/gnome/evince/Evince",
130 G_OBJECT (application));
132 application->scr_saver = totem_scrsaver_new (connection);
136 #endif /* ENABLE_DBUS */
139 * ev_application_get_instance:
141 * Checks for #EvApplication instance, if it doesn't exist it does create it.
143 * Returns: an instance of the #EvApplication data.
146 ev_application_get_instance (void)
148 static EvApplication *instance;
151 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
159 ev_application_load_session (EvApplication *application)
161 GKeyFile *state_file;
164 if (!egg_sm_client_is_resumed (application->smclient))
167 state_file = egg_sm_client_get_state_file (application->smclient);
171 uri_list = g_key_file_get_string_list (state_file,
178 for (i = 0; uri_list[i]; i++) {
179 if (g_ascii_strcasecmp (uri_list[i], "empty-window") == 0)
180 ev_application_open_window (application, NULL, GDK_CURRENT_TIME, NULL);
182 ev_application_open_uri (application, uri_list[i], NULL, GDK_CURRENT_TIME, NULL);
184 g_strfreev (uri_list);
186 g_key_file_free (state_file);
192 smclient_save_state_cb (EggSMClient *client,
193 GKeyFile *state_file,
194 EvApplication *application)
198 const gchar **uri_list;
199 const gchar *empty = "empty-window";
201 windows = ev_application_get_windows (application);
205 uri_list = g_new (const gchar *, g_list_length (windows));
206 for (l = windows, i = 0; l != NULL; l = g_list_next (l), i++) {
207 EvWindow *window = EV_WINDOW (l->data);
209 if (ev_window_is_empty (window))
212 uri_list[i] = ev_window_get_uri (window);
214 g_key_file_set_string_list (state_file,
217 (const char **)uri_list,
223 smclient_quit_cb (EggSMClient *client,
224 EvApplication *application)
226 ev_application_shutdown (application);
230 ev_application_init_session (EvApplication *application)
232 egg_set_desktop_file (GNOMEDATADIR "/applications/evince.desktop");
234 application->smclient = egg_sm_client_get ();
235 g_signal_connect (application->smclient, "save_state",
236 G_CALLBACK (smclient_save_state_cb),
238 g_signal_connect (application->smclient, "quit",
239 G_CALLBACK (smclient_quit_cb),
244 * ev_display_open_if_needed:
245 * @name: the name of the display to be open if it's needed.
247 * Search among all the open displays if any of them have the same name as the
248 * passed name. If the display isn't found it tries the open it.
250 * Returns: a #GdkDisplay of the display with the passed name.
253 ev_display_open_if_needed (const gchar *name)
257 GdkDisplay *display = NULL;
259 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
261 for (l = displays; l != NULL; l = l->next) {
262 const gchar *display_name = gdk_display_get_name ((GdkDisplay *) l->data);
264 if (g_ascii_strcasecmp (display_name, name) == 0) {
270 g_slist_free (displays);
272 return display != NULL ? display : gdk_display_open (name);
276 * get_screen_from_args:
277 * @args: a #GHashTable with data passed to the application.
279 * Looks for the screen in the display available in the hash table passed to the
280 * application. If the display isn't opened, it's opened and the #GdkScreen
281 * assigned to the screen in that display returned.
283 * Returns: the #GdkScreen assigned to the screen on the display indicated by
284 * the data on the #GHashTable.
287 get_screen_from_args (GHashTable *args)
289 GValue *value = NULL;
290 GdkDisplay *display = NULL;
291 GdkScreen *screen = NULL;
293 g_assert (args != NULL);
295 value = g_hash_table_lookup (args, "display");
297 const gchar *display_name;
299 display_name = g_value_get_string (value);
300 display = ev_display_open_if_needed (display_name);
303 value = g_hash_table_lookup (args, "screen");
307 screen_number = g_value_get_int (value);
308 screen = gdk_display_get_screen (display, screen_number);
315 * get_window_run_mode_from_args:
316 * @args: a #GHashTable with data passed to the application.
318 * It does look if the mode option has been passed from command line, using it
319 * as the window run mode, otherwise the run mode will be the normal mode.
321 * Returns: The window run mode passed from command line or
322 * EV_WINDOW_MODE_NORMAL in other case.
324 static EvWindowRunMode
325 get_window_run_mode_from_args (GHashTable *args)
327 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
328 GValue *value = NULL;
330 g_assert (args != NULL);
332 value = g_hash_table_lookup (args, "mode");
334 mode = g_value_get_uint (value);
341 * get_destination_from_args:
342 * @args: a #GHashTable with data passed to the application.
344 * It does look for the page-label argument parsed from the command line and
345 * if it does exist, it returns an #EvLinkDest.
347 * Returns: An #EvLinkDest to page-label if it has been passed from the command
348 * line, NULL in other case.
351 get_destination_from_args (GHashTable *args)
353 EvLinkDest *dest = NULL;
354 GValue *value = NULL;
356 g_assert (args != NULL);
358 value = g_hash_table_lookup (args, "page-label");
360 const gchar *page_label;
362 page_label = g_value_get_string (value);
363 dest = ev_link_dest_new_page_label (page_label);
370 get_find_string_from_args (GHashTable *args)
372 GValue *value = NULL;
374 g_assert (args != NULL);
376 value = g_hash_table_lookup (args, "find-string");
378 return value ? g_value_get_string (value) : NULL;
382 * get_unlink_temp_file_from_args:
383 * @args: a #GHashTable with data passed to the application.
385 * It does look if the unlink-temp-file option has been passed from the command
386 * line returning it's boolean representation, otherwise it does return %FALSE.
388 * Returns: the boolean representation of the unlink-temp-file value or %FALSE
392 get_unlink_temp_file_from_args (GHashTable *args)
394 gboolean unlink_temp_file = FALSE;
395 GValue *value = NULL;
397 g_assert (args != NULL);
399 value = g_hash_table_lookup (args, "unlink-temp-file");
401 unlink_temp_file = g_value_get_boolean (value);
404 return unlink_temp_file;
408 get_print_settings_from_args (GHashTable *args)
410 const gchar *print_settings = NULL;
411 GValue *value = NULL;
413 g_assert (args != NULL);
415 value = g_hash_table_lookup (args, "print-settings");
417 print_settings = g_value_get_string (value);
420 return print_settings;
424 * ev_application_open_window:
425 * @application: The instance of the application.
426 * @args: A #GHashTable with the arguments data.
427 * @timestamp: Current time value.
428 * @error: The #GError facility.
430 * Creates a new window and if the args are available, it's not NULL, it gets
431 * the screen from them and assigns the just created window to it. At last it
437 ev_application_open_window (EvApplication *application,
442 GtkWidget *new_window = ev_window_new ();
443 GdkScreen *screen = NULL;
446 screen = get_screen_from_args (args);
450 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
452 ev_application_add_icon_path_for_screen (screen);
454 if (!GTK_WIDGET_REALIZED (new_window))
455 gtk_widget_realize (new_window);
458 timestamp = gdk_x11_get_server_time (GTK_WIDGET (new_window)->window);
459 gdk_x11_window_set_user_time (GTK_WIDGET (new_window)->window, timestamp);
461 gtk_window_present (GTK_WINDOW (new_window));
467 * ev_application_get_empty_window:
468 * @application: The instance of the application.
469 * @screen: The screen where the empty window will be search.
471 * It does look if there is any empty window in the indicated screen.
473 * Returns: The first empty #EvWindow in the passed #GdkScreen or NULL in other
477 ev_application_get_empty_window (EvApplication *application,
480 EvWindow *empty_window = NULL;
481 GList *windows = ev_application_get_windows (application);
484 for (l = windows; l != NULL; l = l->next) {
485 EvWindow *window = EV_WINDOW (l->data);
487 if (ev_window_is_empty (window) &&
488 gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
489 empty_window = window;
494 g_list_free (windows);
500 * ev_application_get_uri_window:
501 * @application: The instance of the application.
502 * @uri: The uri to be opened.
504 * It looks in the list of the windows for the one with the document represented
505 * by the passed uri on it. If the window is empty or the document isn't present
506 * on any window, it will return NULL.
508 * Returns: The #EvWindow where the document represented by the passed uri is
509 * shown, NULL in other case.
512 ev_application_get_uri_window (EvApplication *application, const char *uri)
514 EvWindow *uri_window = NULL;
515 GList *windows = gtk_window_list_toplevels ();
518 g_return_val_if_fail (uri != NULL, NULL);
520 for (l = windows; l != NULL; l = l->next) {
521 if (EV_IS_WINDOW (l->data)) {
522 EvWindow *window = EV_WINDOW (l->data);
523 const char *window_uri = ev_window_get_uri (window);
525 if (window_uri && strcmp (window_uri, uri) == 0 && !ev_window_is_empty (window)) {
532 g_list_free (windows);
538 ev_application_add_icon_path_for_screen (GdkScreen *screen)
540 GtkIconTheme *icon_theme;
542 icon_theme = screen ? gtk_icon_theme_get_for_screen (screen) : gtk_icon_theme_get_default ();
547 gchar *ev_icons_path;
549 /* GtkIconTheme will then look in Evince custom hicolor dir
550 * for icons as well as the standard search paths
552 ev_icons_path = g_build_filename (DATADIR, "icons", NULL);
553 gtk_icon_theme_get_search_path (icon_theme, &path, &n_paths);
554 for (i = n_paths - 1; i >= 0; i--) {
555 if (g_ascii_strcasecmp (ev_icons_path, path[i]) == 0)
560 gtk_icon_theme_append_search_path (icon_theme,
563 g_free (ev_icons_path);
569 * ev_application_open_uri_at_dest:
570 * @application: The instance of the application.
571 * @uri: The uri to be opened.
572 * @screen: Thee screen where the link will be shown.
573 * @dest: The #EvLinkDest of the document.
574 * @mode: The run mode of the window.
575 * @unlink_temp_file: The unlink_temp_file option value.
576 * @timestamp: Current time value.
579 ev_application_open_uri_at_dest (EvApplication *application,
583 EvWindowRunMode mode,
584 const gchar *search_string,
585 gboolean unlink_temp_file,
586 const gchar *print_settings,
589 EvWindow *new_window;
591 g_return_if_fail (uri != NULL);
593 ev_application_add_icon_path_for_screen (screen);
595 new_window = ev_application_get_uri_window (application, uri);
597 if (new_window == NULL) {
598 new_window = ev_application_get_empty_window (application, screen);
601 if (new_window == NULL) {
602 new_window = EV_WINDOW (ev_window_new ());
606 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
608 /* We need to load uri before showing the window, so
609 we can restore window size without flickering */
610 ev_window_open_uri (new_window, uri, dest, mode, search_string,
611 unlink_temp_file, print_settings);
613 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (new_window)))
614 gtk_widget_realize (GTK_WIDGET (new_window));
617 timestamp = gdk_x11_get_server_time (GTK_WIDGET (new_window)->window);
618 gdk_x11_window_set_user_time (GTK_WIDGET (new_window)->window, timestamp);
620 ev_document_fc_mutex_lock ();
621 gtk_window_present (GTK_WINDOW (new_window));
622 ev_document_fc_mutex_unlock ();
626 * ev_application_open_uri:
627 * @application: The instance of the application.
628 * @uri: The uri to be opened
629 * @args: A #GHashTable with the arguments data.
630 * @timestamp: Current time value.
631 * @error: The #GError facility.
634 ev_application_open_uri (EvApplication *application,
640 EvLinkDest *dest = NULL;
641 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
642 const gchar *search_string = NULL;
643 gboolean unlink_temp_file = FALSE;
644 const gchar *print_settings = NULL;
645 GdkScreen *screen = NULL;
648 screen = get_screen_from_args (args);
649 dest = get_destination_from_args (args);
650 mode = get_window_run_mode_from_args (args);
651 search_string = get_find_string_from_args (args);
652 unlink_temp_file = (mode == EV_WINDOW_MODE_PREVIEW &&
653 get_unlink_temp_file_from_args (args));
654 print_settings = get_print_settings_from_args (args);
657 ev_application_open_uri_at_dest (application, uri, screen,
658 dest, mode, search_string,
660 print_settings, timestamp);
663 g_object_unref (dest);
669 ev_application_open_uri_list (EvApplication *application,
676 for (l = uri_list; l != NULL; l = l->next) {
677 ev_application_open_uri_at_dest (application, (char *)l->data,
678 screen, NULL, 0, NULL,
679 FALSE, NULL, timestamp);
684 ev_application_shutdown (EvApplication *application)
686 if (application->accel_map_file) {
687 gtk_accel_map_save (application->accel_map_file);
688 g_free (application->accel_map_file);
689 application->accel_map_file = NULL;
692 if (application->toolbars_model) {
693 g_object_unref (application->toolbars_model);
694 g_free (application->toolbars_file);
695 application->toolbars_model = NULL;
696 application->toolbars_file = NULL;
699 #if GTK_CHECK_VERSION (2, 11, 0)
700 if (application->print_settings_file) {
701 if (application->print_settings) {
702 GError *error = NULL;
704 gtk_print_settings_to_file (application->print_settings,
705 application->print_settings_file,
708 g_warning ("%s", error->message);
709 g_error_free (error);
712 g_object_unref (application->print_settings);
713 application->print_settings = NULL;
716 g_free (application->print_settings_file);
717 application->print_settings_file = NULL;
719 #else /* ! GTK 2.11.0 */
720 if (application->print_settings) {
721 g_object_unref (application->print_settings);
722 application->print_settings = NULL;
724 #endif /* GTK 2.11.0 */
727 if (application->keys) {
728 g_object_unref (application->keys);
729 application->keys = NULL;
731 #endif /* ENABLE_DBUS */
733 g_free (application->last_chooser_uri);
734 g_object_unref (application);
740 ev_application_class_init (EvApplicationClass *ev_application_class)
745 ev_application_init (EvApplication *ev_application)
748 const gchar *home_dir;
750 ev_application_init_session (ev_application);
752 home_dir = g_get_home_dir ();
754 ev_application->accel_map_file = g_build_filename (home_dir,
759 gtk_accel_map_load (ev_application->accel_map_file);
762 ev_application->toolbars_model = egg_toolbars_model_new ();
764 ev_application->toolbars_file = g_build_filename
765 (ev_dot_dir (), "evince_toolbar.xml", NULL);
767 egg_toolbars_model_load_names (ev_application->toolbars_model,
768 DATADIR "/evince-toolbar.xml");
770 if (!egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
771 ev_application->toolbars_file)) {
772 egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
773 DATADIR"/evince-toolbar.xml");
776 /* Open item doesn't exist anymore,
777 * convert it to OpenRecent for compatibility
779 for (i = 0; i < egg_toolbars_model_n_items (ev_application->toolbars_model, 0); i++) {
782 item = egg_toolbars_model_item_nth (ev_application->toolbars_model, 0, i);
783 if (g_ascii_strcasecmp (item, "FileOpen") == 0) {
784 egg_toolbars_model_remove_item (ev_application->toolbars_model, 0, i);
785 egg_toolbars_model_add_item (ev_application->toolbars_model, 0, i,
787 ev_application_save_toolbars_model (ev_application);
792 egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
793 EGG_TB_MODEL_NOT_REMOVABLE);
796 ev_application->keys = ev_media_player_keys_new ();
797 #endif /* ENABLE_DBUS */
801 * ev_application_get_windows:
802 * @application: The instance of the application.
804 * It creates a list of the top level windows.
806 * Returns: A #GList of the top level windows.
809 ev_application_get_windows (EvApplication *application)
811 GList *l, *toplevels;
812 GList *windows = NULL;
814 toplevels = gtk_window_list_toplevels ();
816 for (l = toplevels; l != NULL; l = l->next) {
817 if (EV_IS_WINDOW (l->data)) {
818 windows = g_list_append (windows, l->data);
822 g_list_free (toplevels);
828 * ev_application_get_media_keys:
829 * @application: The instance of the application.
831 * It gives you access to the media player keys handler object.
833 * Returns: A #EvMediaPlayerKeys.
836 ev_application_get_media_keys (EvApplication *application)
839 return G_OBJECT (application->keys);
842 #endif /* ENABLE_DBUS */
846 ev_application_get_toolbars_model (EvApplication *application)
848 return application->toolbars_model;
852 ev_application_save_toolbars_model (EvApplication *application)
854 egg_toolbars_model_save_toolbars (application->toolbars_model,
855 application->toolbars_file, "1.0");
859 ev_application_set_chooser_uri (EvApplication *application, const gchar *uri)
861 g_free (application->last_chooser_uri);
862 application->last_chooser_uri = g_strdup (uri);
866 ev_application_get_chooser_uri (EvApplication *application)
868 return application->last_chooser_uri;
872 ev_application_screensaver_enable (EvApplication *application)
874 if (application->scr_saver)
875 totem_scrsaver_enable (application->scr_saver);
879 ev_application_screensaver_disable (EvApplication *application)
881 if (application->scr_saver)
882 totem_scrsaver_disable (application->scr_saver);
886 ev_application_get_print_settings (EvApplication *application)
888 if (application->print_settings)
889 return application->print_settings;
891 #if GTK_CHECK_VERSION (2, 11, 0)
892 if (!application->print_settings_file) {
893 application->print_settings_file =
894 g_build_filename (ev_dot_dir (), "print-settings", NULL);
897 if (g_file_test (application->print_settings_file, G_FILE_TEST_IS_REGULAR)) {
898 GError *error = NULL;
900 application->print_settings =
901 gtk_print_settings_new_from_file (application->print_settings_file, &error);
904 g_warning ("%s", error->message);
905 g_error_free (error);
907 return application->print_settings;
910 #endif /* GTK 2.11.0 */
912 application->print_settings = gtk_print_settings_new ();
914 return application->print_settings;
918 ev_application_set_print_settings (EvApplication *application,
919 GtkPrintSettings *settings)
921 g_return_if_fail (GTK_IS_PRINT_SETTINGS (settings));
923 if (settings == application->print_settings)
926 if (application->print_settings)
927 g_object_unref (application->print_settings);
929 application->print_settings = g_object_ref (settings);