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 gtk_widget_show (new_window);
457 timestamp = gdk_x11_get_server_time (GTK_WIDGET (new_window)->window);
458 gdk_x11_window_set_user_time (GTK_WIDGET (new_window)->window, timestamp);
460 gtk_window_present (GTK_WINDOW (new_window));
466 * ev_application_get_empty_window:
467 * @application: The instance of the application.
468 * @screen: The screen where the empty window will be search.
470 * It does look if there is any empty window in the indicated screen.
472 * Returns: The first empty #EvWindow in the passed #GdkScreen or NULL in other
476 ev_application_get_empty_window (EvApplication *application,
479 EvWindow *empty_window = NULL;
480 GList *windows = ev_application_get_windows (application);
483 for (l = windows; l != NULL; l = l->next) {
484 EvWindow *window = EV_WINDOW (l->data);
486 if (ev_window_is_empty (window) &&
487 gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
488 empty_window = window;
493 g_list_free (windows);
499 * ev_application_get_uri_window:
500 * @application: The instance of the application.
501 * @uri: The uri to be opened.
503 * It looks in the list of the windows for the one with the document represented
504 * by the passed uri on it. If the window is empty or the document isn't present
505 * on any window, it will return NULL.
507 * Returns: The #EvWindow where the document represented by the passed uri is
508 * shown, NULL in other case.
511 ev_application_get_uri_window (EvApplication *application, const char *uri)
513 EvWindow *uri_window = NULL;
514 GList *windows = gtk_window_list_toplevels ();
517 g_return_val_if_fail (uri != NULL, NULL);
519 for (l = windows; l != NULL; l = l->next) {
520 if (EV_IS_WINDOW (l->data)) {
521 EvWindow *window = EV_WINDOW (l->data);
522 const char *window_uri = ev_window_get_uri (window);
524 if (window_uri && strcmp (window_uri, uri) == 0 && !ev_window_is_empty (window)) {
531 g_list_free (windows);
537 ev_application_add_icon_path_for_screen (GdkScreen *screen)
539 GtkIconTheme *icon_theme;
541 icon_theme = screen ? gtk_icon_theme_get_for_screen (screen) : gtk_icon_theme_get_default ();
546 gchar *ev_icons_path;
548 /* GtkIconTheme will then look in Evince custom hicolor dir
549 * for icons as well as the standard search paths
551 ev_icons_path = g_build_filename (DATADIR, "icons", NULL);
552 gtk_icon_theme_get_search_path (icon_theme, &path, &n_paths);
553 for (i = n_paths - 1; i >= 0; i--) {
554 if (g_ascii_strcasecmp (ev_icons_path, path[i]) == 0)
559 gtk_icon_theme_append_search_path (icon_theme,
562 g_free (ev_icons_path);
568 * ev_application_open_uri_at_dest:
569 * @application: The instance of the application.
570 * @uri: The uri to be opened.
571 * @screen: Thee screen where the link will be shown.
572 * @dest: The #EvLinkDest of the document.
573 * @mode: The run mode of the window.
574 * @unlink_temp_file: The unlink_temp_file option value.
575 * @timestamp: Current time value.
578 ev_application_open_uri_at_dest (EvApplication *application,
582 EvWindowRunMode mode,
583 const gchar *search_string,
584 gboolean unlink_temp_file,
585 const gchar *print_settings,
588 EvWindow *new_window;
590 g_return_if_fail (uri != NULL);
592 ev_application_add_icon_path_for_screen (screen);
594 new_window = ev_application_get_uri_window (application, uri);
596 if (new_window == NULL) {
597 new_window = ev_application_get_empty_window (application, screen);
600 if (new_window == NULL) {
601 new_window = EV_WINDOW (ev_window_new ());
605 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
607 /* We need to load uri before showing the window, so
608 we can restore window size without flickering */
609 ev_window_open_uri (new_window, uri, dest, mode, search_string,
610 unlink_temp_file, print_settings);
612 ev_document_fc_mutex_lock ();
613 gtk_widget_show (GTK_WIDGET (new_window));
614 ev_document_fc_mutex_unlock ();
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 gtk_window_present (GTK_WINDOW (new_window));
624 * ev_application_open_uri:
625 * @application: The instance of the application.
626 * @uri: The uri to be opened
627 * @args: A #GHashTable with the arguments data.
628 * @timestamp: Current time value.
629 * @error: The #GError facility.
632 ev_application_open_uri (EvApplication *application,
638 EvLinkDest *dest = NULL;
639 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
640 const gchar *search_string = NULL;
641 gboolean unlink_temp_file = FALSE;
642 const gchar *print_settings = NULL;
643 GdkScreen *screen = NULL;
646 screen = get_screen_from_args (args);
647 dest = get_destination_from_args (args);
648 mode = get_window_run_mode_from_args (args);
649 search_string = get_find_string_from_args (args);
650 unlink_temp_file = (mode == EV_WINDOW_MODE_PREVIEW &&
651 get_unlink_temp_file_from_args (args));
652 print_settings = get_print_settings_from_args (args);
655 ev_application_open_uri_at_dest (application, uri, screen,
656 dest, mode, search_string,
658 print_settings, timestamp);
661 g_object_unref (dest);
667 ev_application_open_uri_list (EvApplication *application,
674 for (l = uri_list; l != NULL; l = l->next) {
675 ev_application_open_uri_at_dest (application, (char *)l->data,
676 screen, NULL, 0, NULL,
677 FALSE, NULL, timestamp);
682 ev_application_shutdown (EvApplication *application)
684 if (application->accel_map_file) {
685 gtk_accel_map_save (application->accel_map_file);
686 g_free (application->accel_map_file);
687 application->accel_map_file = NULL;
690 if (application->toolbars_model) {
691 g_object_unref (application->toolbars_model);
692 g_free (application->toolbars_file);
693 application->toolbars_model = NULL;
694 application->toolbars_file = NULL;
697 #if GTK_CHECK_VERSION (2, 11, 0)
698 if (application->print_settings_file) {
699 if (application->print_settings) {
700 GError *error = NULL;
702 gtk_print_settings_to_file (application->print_settings,
703 application->print_settings_file,
706 g_warning ("%s", error->message);
707 g_error_free (error);
710 g_object_unref (application->print_settings);
711 application->print_settings = NULL;
714 g_free (application->print_settings_file);
715 application->print_settings_file = NULL;
717 #else /* ! GTK 2.11.0 */
718 if (application->print_settings) {
719 g_object_unref (application->print_settings);
720 application->print_settings = NULL;
722 #endif /* GTK 2.11.0 */
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 if (!application->keys)
832 return g_object_ref (G_OBJECT (application->keys));
835 #endif /* ENABLE_DBUS */
839 ev_application_get_toolbars_model (EvApplication *application)
841 return application->toolbars_model;
845 ev_application_save_toolbars_model (EvApplication *application)
847 egg_toolbars_model_save_toolbars (application->toolbars_model,
848 application->toolbars_file, "1.0");
852 ev_application_set_chooser_uri (EvApplication *application, const gchar *uri)
854 g_free (application->last_chooser_uri);
855 application->last_chooser_uri = g_strdup (uri);
859 ev_application_get_chooser_uri (EvApplication *application)
861 return application->last_chooser_uri;
865 ev_application_screensaver_enable (EvApplication *application)
867 if (application->scr_saver)
868 totem_scrsaver_enable (application->scr_saver);
872 ev_application_screensaver_disable (EvApplication *application)
874 if (application->scr_saver)
875 totem_scrsaver_disable (application->scr_saver);
879 ev_application_get_print_settings (EvApplication *application)
881 if (application->print_settings)
882 return application->print_settings;
884 #if GTK_CHECK_VERSION (2, 11, 0)
885 if (!application->print_settings_file) {
886 application->print_settings_file =
887 g_build_filename (ev_dot_dir (), "print-settings", NULL);
890 if (g_file_test (application->print_settings_file, G_FILE_TEST_IS_REGULAR)) {
891 GError *error = NULL;
893 application->print_settings =
894 gtk_print_settings_new_from_file (application->print_settings_file, &error);
897 g_warning ("%s", error->message);
898 g_error_free (error);
900 return application->print_settings;
903 #endif /* GTK 2.11.0 */
905 application->print_settings = gtk_print_settings_new ();
907 return application->print_settings;
911 ev_application_set_print_settings (EvApplication *application,
912 GtkPrintSettings *settings)
914 g_return_if_fail (GTK_IS_PRINT_SETTINGS (settings));
916 if (settings == application->print_settings)
919 if (application->print_settings)
920 g_object_unref (application->print_settings);
922 application->print_settings = g_object_ref (settings);