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);
51 struct _EvApplication {
52 GObject base_instance;
54 gchar *accel_map_file;
58 EggToolbarsModel *toolbars_model;
60 TotemScrsaver *scr_saver;
62 EggSMClient *smclient;
64 gchar *last_chooser_uri;
67 EvMediaPlayerKeys *keys;
68 #endif /* ENABLE_DBUS */
70 GtkPrintSettings *print_settings;
71 gchar *print_settings_file;
74 struct _EvApplicationClass {
75 GObjectClass base_class;
78 G_DEFINE_TYPE (EvApplication, ev_application, G_TYPE_OBJECT);
80 #define APPLICATION_SERVICE_NAME "org.gnome.evince.ApplicationService"
84 ev_application_register_service (EvApplication *application)
86 static DBusGConnection *connection = NULL;
87 DBusGProxy *driver_proxy;
89 guint request_name_result;
92 g_warning ("Service already registered.");
96 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &err);
97 if (connection == NULL) {
98 g_warning ("Service registration failed.");
104 driver_proxy = dbus_g_proxy_new_for_name (connection,
107 DBUS_INTERFACE_DBUS);
109 if (!org_freedesktop_DBus_request_name (driver_proxy,
110 APPLICATION_SERVICE_NAME,
111 DBUS_NAME_FLAG_DO_NOT_QUEUE,
112 &request_name_result, &err)) {
113 g_warning ("Service registration failed.");
114 g_clear_error (&err);
117 g_object_unref (driver_proxy);
119 if (request_name_result == DBUS_REQUEST_NAME_REPLY_EXISTS) {
123 dbus_g_object_type_install_info (EV_TYPE_APPLICATION,
124 &dbus_glib_ev_application_object_info);
125 dbus_g_connection_register_g_object (connection,
126 "/org/gnome/evince/Evince",
127 G_OBJECT (application));
129 application->scr_saver = totem_scrsaver_new (connection);
133 #endif /* ENABLE_DBUS */
136 * ev_application_get_instance:
138 * Checks for #EvApplication instance, if it doesn't exist it does create it.
140 * Returns: an instance of the #EvApplication data.
143 ev_application_get_instance (void)
145 static EvApplication *instance;
148 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
156 ev_application_load_session (EvApplication *application)
158 GKeyFile *state_file;
161 if (!egg_sm_client_is_resumed (application->smclient))
164 state_file = egg_sm_client_get_state_file (application->smclient);
168 uri_list = g_key_file_get_string_list (state_file,
175 for (i = 0; uri_list[i]; i++) {
176 if (g_ascii_strcasecmp (uri_list[i], "empty-window") == 0)
177 ev_application_open_window (application, NULL, GDK_CURRENT_TIME, NULL);
179 ev_application_open_uri (application, uri_list[i], NULL, GDK_CURRENT_TIME, NULL);
181 g_strfreev (uri_list);
183 g_key_file_free (state_file);
189 smclient_save_state_cb (EggSMClient *client,
190 GKeyFile *state_file,
191 EvApplication *application)
195 const gchar **uri_list;
196 const gchar *empty = "empty-window";
198 windows = ev_application_get_windows (application);
202 uri_list = g_new (const gchar *, g_list_length (windows));
203 for (l = windows, i = 0; l != NULL; l = g_list_next (l), i++) {
204 EvWindow *window = EV_WINDOW (l->data);
206 if (ev_window_is_empty (window))
209 uri_list[i] = ev_window_get_uri (window);
211 g_key_file_set_string_list (state_file,
214 (const char **)uri_list,
220 smclient_quit_cb (EggSMClient *client,
221 EvApplication *application)
223 ev_application_shutdown (application);
227 ev_application_init_session (EvApplication *application)
229 application->smclient = egg_sm_client_get ();
230 g_signal_connect (application->smclient, "save_state",
231 G_CALLBACK (smclient_save_state_cb),
233 g_signal_connect (application->smclient, "quit",
234 G_CALLBACK (smclient_quit_cb),
239 * ev_display_open_if_needed:
240 * @name: the name of the display to be open if it's needed.
242 * Search among all the open displays if any of them have the same name as the
243 * passed name. If the display isn't found it tries the open it.
245 * Returns: a #GdkDisplay of the display with the passed name.
248 ev_display_open_if_needed (const gchar *name)
252 GdkDisplay *display = NULL;
254 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
256 for (l = displays; l != NULL; l = l->next) {
257 const gchar *display_name = gdk_display_get_name ((GdkDisplay *) l->data);
259 if (g_ascii_strcasecmp (display_name, name) == 0) {
265 g_slist_free (displays);
267 return display != NULL ? display : gdk_display_open (name);
271 * get_screen_from_args:
272 * @args: a #GHashTable with data passed to the application.
274 * Looks for the screen in the display available in the hash table passed to the
275 * application. If the display isn't opened, it's opened and the #GdkScreen
276 * assigned to the screen in that display returned.
278 * Returns: the #GdkScreen assigned to the screen on the display indicated by
279 * the data on the #GHashTable.
282 get_screen_from_args (GHashTable *args)
284 GValue *value = NULL;
285 GdkDisplay *display = NULL;
286 GdkScreen *screen = NULL;
288 g_assert (args != NULL);
290 value = g_hash_table_lookup (args, "display");
292 const gchar *display_name;
294 display_name = g_value_get_string (value);
295 display = ev_display_open_if_needed (display_name);
298 value = g_hash_table_lookup (args, "screen");
302 screen_number = g_value_get_int (value);
303 screen = gdk_display_get_screen (display, screen_number);
310 * get_window_run_mode_from_args:
311 * @args: a #GHashTable with data passed to the application.
313 * It does look if the mode option has been passed from command line, using it
314 * as the window run mode, otherwise the run mode will be the normal mode.
316 * Returns: The window run mode passed from command line or
317 * EV_WINDOW_MODE_NORMAL in other case.
319 static EvWindowRunMode
320 get_window_run_mode_from_args (GHashTable *args)
322 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
323 GValue *value = NULL;
325 g_assert (args != NULL);
327 value = g_hash_table_lookup (args, "mode");
329 mode = g_value_get_uint (value);
336 * get_destination_from_args:
337 * @args: a #GHashTable with data passed to the application.
339 * It does look for the page-label argument parsed from the command line and
340 * if it does exist, it returns an #EvLinkDest.
342 * Returns: An #EvLinkDest to page-label if it has been passed from the command
343 * line, NULL in other case.
346 get_destination_from_args (GHashTable *args)
348 EvLinkDest *dest = NULL;
349 GValue *value = NULL;
351 g_assert (args != NULL);
353 value = g_hash_table_lookup (args, "page-label");
355 const gchar *page_label;
357 page_label = g_value_get_string (value);
358 dest = ev_link_dest_new_page_label (page_label);
365 get_find_string_from_args (GHashTable *args)
367 GValue *value = NULL;
369 g_assert (args != NULL);
371 value = g_hash_table_lookup (args, "find-string");
373 return value ? g_value_get_string (value) : NULL;
377 * get_unlink_temp_file_from_args:
378 * @args: a #GHashTable with data passed to the application.
380 * It does look if the unlink-temp-file option has been passed from the command
381 * line returning it's boolean representation, otherwise it does return %FALSE.
383 * Returns: the boolean representation of the unlink-temp-file value or %FALSE
387 get_unlink_temp_file_from_args (GHashTable *args)
389 gboolean unlink_temp_file = FALSE;
390 GValue *value = NULL;
392 g_assert (args != NULL);
394 value = g_hash_table_lookup (args, "unlink-temp-file");
396 unlink_temp_file = g_value_get_boolean (value);
399 return unlink_temp_file;
403 get_print_settings_from_args (GHashTable *args)
405 const gchar *print_settings = NULL;
406 GValue *value = NULL;
408 g_assert (args != NULL);
410 value = g_hash_table_lookup (args, "print-settings");
412 print_settings = g_value_get_string (value);
415 return print_settings;
419 * ev_application_open_window:
420 * @application: The instance of the application.
421 * @args: A #GHashTable with the arguments data.
422 * @timestamp: Current time value.
423 * @error: The #GError facility.
425 * Creates a new window and if the args are available, it's not NULL, it gets
426 * the screen from them and assigns the just created window to it. At last it
432 ev_application_open_window (EvApplication *application,
437 GtkWidget *new_window = ev_window_new ();
438 GdkScreen *screen = NULL;
441 screen = get_screen_from_args (args);
445 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
447 ev_application_add_icon_path_for_screen (screen);
449 if (!GTK_WIDGET_REALIZED (new_window))
450 gtk_widget_realize (new_window);
453 timestamp = gdk_x11_get_server_time (GTK_WIDGET (new_window)->window);
454 gdk_x11_window_set_user_time (GTK_WIDGET (new_window)->window, timestamp);
456 gtk_window_present (GTK_WINDOW (new_window));
462 * ev_application_get_empty_window:
463 * @application: The instance of the application.
464 * @screen: The screen where the empty window will be search.
466 * It does look if there is any empty window in the indicated screen.
468 * Returns: The first empty #EvWindow in the passed #GdkScreen or NULL in other
472 ev_application_get_empty_window (EvApplication *application,
475 EvWindow *empty_window = NULL;
476 GList *windows = ev_application_get_windows (application);
479 for (l = windows; l != NULL; l = l->next) {
480 EvWindow *window = EV_WINDOW (l->data);
482 if (ev_window_is_empty (window) &&
483 gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
484 empty_window = window;
489 g_list_free (windows);
495 * ev_application_get_uri_window:
496 * @application: The instance of the application.
497 * @uri: The uri to be opened.
499 * It looks in the list of the windows for the one with the document represented
500 * by the passed uri on it. If the window is empty or the document isn't present
501 * on any window, it will return NULL.
503 * Returns: The #EvWindow where the document represented by the passed uri is
504 * shown, NULL in other case.
507 ev_application_get_uri_window (EvApplication *application, const char *uri)
509 EvWindow *uri_window = NULL;
510 GList *windows = gtk_window_list_toplevels ();
513 g_return_val_if_fail (uri != NULL, NULL);
515 for (l = windows; l != NULL; l = l->next) {
516 if (EV_IS_WINDOW (l->data)) {
517 EvWindow *window = EV_WINDOW (l->data);
518 const char *window_uri = ev_window_get_uri (window);
520 if (window_uri && strcmp (window_uri, uri) == 0 && !ev_window_is_empty (window)) {
527 g_list_free (windows);
533 ev_application_add_icon_path_for_screen (GdkScreen *screen)
535 GtkIconTheme *icon_theme;
537 icon_theme = screen ? gtk_icon_theme_get_for_screen (screen) : gtk_icon_theme_get_default ();
542 gchar *ev_icons_path;
544 /* GtkIconTheme will then look in Evince custom hicolor dir
545 * for icons as well as the standard search paths
547 ev_icons_path = g_build_filename (DATADIR, "icons", NULL);
548 gtk_icon_theme_get_search_path (icon_theme, &path, &n_paths);
549 for (i = n_paths - 1; i >= 0; i--) {
550 if (g_ascii_strcasecmp (ev_icons_path, path[i]) == 0)
555 gtk_icon_theme_append_search_path (icon_theme,
558 g_free (ev_icons_path);
564 * ev_application_open_uri_at_dest:
565 * @application: The instance of the application.
566 * @uri: The uri to be opened.
567 * @screen: Thee screen where the link will be shown.
568 * @dest: The #EvLinkDest of the document.
569 * @mode: The run mode of the window.
570 * @unlink_temp_file: The unlink_temp_file option value.
571 * @timestamp: Current time value.
574 ev_application_open_uri_at_dest (EvApplication *application,
578 EvWindowRunMode mode,
579 const gchar *search_string,
580 gboolean unlink_temp_file,
581 const gchar *print_settings,
584 EvWindow *new_window;
586 g_return_if_fail (uri != NULL);
588 ev_application_add_icon_path_for_screen (screen);
590 new_window = ev_application_get_uri_window (application, uri);
592 if (new_window == NULL) {
593 new_window = ev_application_get_empty_window (application, screen);
596 if (new_window == NULL) {
597 new_window = EV_WINDOW (ev_window_new ());
601 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
603 /* We need to load uri before showing the window, so
604 we can restore window size without flickering */
605 ev_window_open_uri (new_window, uri, dest, mode, search_string,
606 unlink_temp_file, print_settings);
608 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (new_window)))
609 gtk_widget_realize (GTK_WIDGET (new_window));
612 timestamp = gdk_x11_get_server_time (GTK_WIDGET (new_window)->window);
613 gdk_x11_window_set_user_time (GTK_WIDGET (new_window)->window, timestamp);
615 ev_document_fc_mutex_lock ();
616 gtk_window_present (GTK_WINDOW (new_window));
617 ev_document_fc_mutex_unlock ();
621 * ev_application_open_uri:
622 * @application: The instance of the application.
623 * @uri: The uri to be opened
624 * @args: A #GHashTable with the arguments data.
625 * @timestamp: Current time value.
626 * @error: The #GError facility.
629 ev_application_open_uri (EvApplication *application,
635 EvLinkDest *dest = NULL;
636 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
637 const gchar *search_string = NULL;
638 gboolean unlink_temp_file = FALSE;
639 const gchar *print_settings = NULL;
640 GdkScreen *screen = NULL;
643 screen = get_screen_from_args (args);
644 dest = get_destination_from_args (args);
645 mode = get_window_run_mode_from_args (args);
646 search_string = get_find_string_from_args (args);
647 unlink_temp_file = (mode == EV_WINDOW_MODE_PREVIEW &&
648 get_unlink_temp_file_from_args (args));
649 print_settings = get_print_settings_from_args (args);
652 ev_application_open_uri_at_dest (application, uri, screen,
653 dest, mode, search_string,
655 print_settings, timestamp);
658 g_object_unref (dest);
664 ev_application_open_uri_list (EvApplication *application,
671 for (l = uri_list; l != NULL; l = l->next) {
672 ev_application_open_uri_at_dest (application, (char *)l->data,
673 screen, NULL, 0, NULL,
674 FALSE, NULL, timestamp);
679 ev_application_shutdown (EvApplication *application)
681 if (application->accel_map_file) {
682 gtk_accel_map_save (application->accel_map_file);
683 g_free (application->accel_map_file);
684 application->accel_map_file = NULL;
687 if (application->toolbars_model) {
688 g_object_unref (application->toolbars_model);
689 g_free (application->toolbars_file);
690 application->toolbars_model = NULL;
691 application->toolbars_file = NULL;
694 if (application->print_settings_file) {
695 if (application->print_settings) {
696 GError *error = NULL;
698 gtk_print_settings_to_file (application->print_settings,
699 application->print_settings_file,
702 g_warning ("%s", error->message);
703 g_error_free (error);
706 g_object_unref (application->print_settings);
707 application->print_settings = NULL;
710 g_free (application->print_settings_file);
711 application->print_settings_file = NULL;
715 if (application->keys) {
716 g_object_unref (application->keys);
717 application->keys = NULL;
719 #endif /* ENABLE_DBUS */
721 g_free (application->last_chooser_uri);
722 g_object_unref (application);
728 ev_application_class_init (EvApplicationClass *ev_application_class)
733 ev_application_init (EvApplication *ev_application)
736 const gchar *home_dir;
738 ev_application_init_session (ev_application);
740 home_dir = g_get_home_dir ();
742 ev_application->accel_map_file = g_build_filename (home_dir,
747 gtk_accel_map_load (ev_application->accel_map_file);
750 ev_application->toolbars_model = egg_toolbars_model_new ();
752 ev_application->toolbars_file = g_build_filename
753 (ev_dot_dir (), "evince_toolbar.xml", NULL);
755 egg_toolbars_model_load_names (ev_application->toolbars_model,
756 DATADIR "/evince-toolbar.xml");
758 if (!egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
759 ev_application->toolbars_file)) {
760 egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
761 DATADIR"/evince-toolbar.xml");
764 /* Open item doesn't exist anymore,
765 * convert it to OpenRecent for compatibility
767 for (i = 0; i < egg_toolbars_model_n_items (ev_application->toolbars_model, 0); i++) {
770 item = egg_toolbars_model_item_nth (ev_application->toolbars_model, 0, i);
771 if (g_ascii_strcasecmp (item, "FileOpen") == 0) {
772 egg_toolbars_model_remove_item (ev_application->toolbars_model, 0, i);
773 egg_toolbars_model_add_item (ev_application->toolbars_model, 0, i,
775 ev_application_save_toolbars_model (ev_application);
780 egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
781 EGG_TB_MODEL_NOT_REMOVABLE);
784 ev_application->keys = ev_media_player_keys_new ();
785 #endif /* ENABLE_DBUS */
789 * ev_application_get_windows:
790 * @application: The instance of the application.
792 * It creates a list of the top level windows.
794 * Returns: A #GList of the top level windows.
797 ev_application_get_windows (EvApplication *application)
799 GList *l, *toplevels;
800 GList *windows = NULL;
802 toplevels = gtk_window_list_toplevels ();
804 for (l = toplevels; l != NULL; l = l->next) {
805 if (EV_IS_WINDOW (l->data)) {
806 windows = g_list_append (windows, l->data);
810 g_list_free (toplevels);
816 * ev_application_get_media_keys:
817 * @application: The instance of the application.
819 * It gives you access to the media player keys handler object.
821 * Returns: A #EvMediaPlayerKeys.
824 ev_application_get_media_keys (EvApplication *application)
827 return G_OBJECT (application->keys);
830 #endif /* ENABLE_DBUS */
834 ev_application_get_toolbars_model (EvApplication *application)
836 return application->toolbars_model;
840 ev_application_save_toolbars_model (EvApplication *application)
842 egg_toolbars_model_save_toolbars (application->toolbars_model,
843 application->toolbars_file, "1.0");
847 ev_application_set_chooser_uri (EvApplication *application, const gchar *uri)
849 g_free (application->last_chooser_uri);
850 application->last_chooser_uri = g_strdup (uri);
854 ev_application_get_chooser_uri (EvApplication *application)
856 return application->last_chooser_uri;
860 ev_application_screensaver_enable (EvApplication *application)
862 if (application->scr_saver)
863 totem_scrsaver_enable (application->scr_saver);
867 ev_application_screensaver_disable (EvApplication *application)
869 if (application->scr_saver)
870 totem_scrsaver_disable (application->scr_saver);
874 ev_application_get_print_settings (EvApplication *application)
876 if (application->print_settings)
877 return application->print_settings;
879 if (!application->print_settings_file) {
880 application->print_settings_file =
881 g_build_filename (ev_dot_dir (), "print-settings", NULL);
884 if (g_file_test (application->print_settings_file, G_FILE_TEST_IS_REGULAR)) {
885 GError *error = NULL;
887 application->print_settings =
888 gtk_print_settings_new_from_file (application->print_settings_file, &error);
891 g_warning ("%s", error->message);
892 g_error_free (error);
894 return application->print_settings;
898 application->print_settings = gtk_print_settings_new ();
900 return application->print_settings;
904 ev_application_set_print_settings (EvApplication *application,
905 GtkPrintSettings *settings)
907 g_return_if_fail (GTK_IS_PRINT_SETTINGS (settings));
909 if (settings == application->print_settings)
912 if (application->print_settings)
913 g_object_unref (application->print_settings);
915 application->print_settings = g_object_ref (settings);