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>
31 #include "totem-scrsaver.h"
32 #include "eggsmclient.h"
33 #include "eggdesktopfile.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 #if GTK_CHECK_VERSION (2, 11, 0)
72 gchar *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"
86 ev_application_register_service (EvApplication *application)
88 static DBusGConnection *connection = NULL;
89 DBusGProxy *driver_proxy;
91 guint request_name_result;
94 g_warning ("Service already registered.");
98 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &err);
99 if (connection == NULL) {
100 g_warning ("Service registration failed.");
106 driver_proxy = dbus_g_proxy_new_for_name (connection,
109 DBUS_INTERFACE_DBUS);
111 if (!org_freedesktop_DBus_request_name (driver_proxy,
112 APPLICATION_SERVICE_NAME,
113 DBUS_NAME_FLAG_DO_NOT_QUEUE,
114 &request_name_result, &err)) {
115 g_warning ("Service registration failed.");
116 g_clear_error (&err);
119 g_object_unref (driver_proxy);
121 if (request_name_result == DBUS_REQUEST_NAME_REPLY_EXISTS) {
125 dbus_g_object_type_install_info (EV_TYPE_APPLICATION,
126 &dbus_glib_ev_application_object_info);
127 dbus_g_connection_register_g_object (connection,
128 "/org/gnome/evince/Evince",
129 G_OBJECT (application));
131 application->scr_saver = totem_scrsaver_new (connection);
135 #endif /* ENABLE_DBUS */
138 * ev_application_get_instance:
140 * Checks for #EvApplication instance, if it doesn't exist it does create it.
142 * Returns: an instance of the #EvApplication data.
145 ev_application_get_instance (void)
147 static EvApplication *instance;
150 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
158 ev_application_load_session (EvApplication *application)
160 GKeyFile *state_file;
163 if (!egg_sm_client_is_resumed (application->smclient))
166 state_file = egg_sm_client_get_state_file (application->smclient);
170 uri_list = g_key_file_get_string_list (state_file,
177 for (i = 0; uri_list[i]; i++) {
178 if (g_ascii_strcasecmp (uri_list[i], "empty-window") == 0)
179 ev_application_open_window (application, NULL, GDK_CURRENT_TIME, NULL);
181 ev_application_open_uri (application, uri_list[i], NULL, GDK_CURRENT_TIME, NULL);
183 g_strfreev (uri_list);
185 g_key_file_free (state_file);
191 smclient_save_state_cb (EggSMClient *client,
192 GKeyFile *state_file,
193 EvApplication *application)
197 const gchar **uri_list;
198 const gchar *empty = "empty-window";
200 windows = ev_application_get_windows (application);
204 uri_list = g_new (const gchar *, g_list_length (windows));
205 for (l = windows, i = 0; l != NULL; l = g_list_next (l), i++) {
206 EvWindow *window = EV_WINDOW (l->data);
208 if (ev_window_is_empty (window))
211 uri_list[i] = ev_window_get_uri (window);
213 g_key_file_set_string_list (state_file,
216 (const char **)uri_list,
222 smclient_quit_cb (EggSMClient *client,
223 EvApplication *application)
225 ev_application_shutdown (application);
229 ev_application_init_session (EvApplication *application)
231 egg_set_desktop_file (GNOMEDATADIR "/applications/evince.desktop");
233 application->smclient = egg_sm_client_get ();
234 g_signal_connect (application->smclient, "save_state",
235 G_CALLBACK (smclient_save_state_cb),
237 g_signal_connect (application->smclient, "quit",
238 G_CALLBACK (smclient_quit_cb),
243 * ev_display_open_if_needed:
244 * @name: the name of the display to be open if it's needed.
246 * Search among all the open displays if any of them have the same name as the
247 * passed name. If the display isn't found it tries the open it.
249 * Returns: a #GdkDisplay of the display with the passed name.
252 ev_display_open_if_needed (const gchar *name)
256 GdkDisplay *display = NULL;
258 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
260 for (l = displays; l != NULL; l = l->next) {
261 const gchar *display_name = gdk_display_get_name ((GdkDisplay *) l->data);
263 if (g_ascii_strcasecmp (display_name, name) == 0) {
269 g_slist_free (displays);
271 return display != NULL ? display : gdk_display_open (name);
275 * get_screen_from_args:
276 * @args: a #GHashTable with data passed to the application.
278 * Looks for the screen in the display available in the hash table passed to the
279 * application. If the display isn't opened, it's opened and the #GdkScreen
280 * assigned to the screen in that display returned.
282 * Returns: the #GdkScreen assigned to the screen on the display indicated by
283 * the data on the #GHashTable.
286 get_screen_from_args (GHashTable *args)
288 GValue *value = NULL;
289 GdkDisplay *display = NULL;
290 GdkScreen *screen = NULL;
292 g_assert (args != NULL);
294 value = g_hash_table_lookup (args, "display");
296 const gchar *display_name;
298 display_name = g_value_get_string (value);
299 display = ev_display_open_if_needed (display_name);
302 value = g_hash_table_lookup (args, "screen");
306 screen_number = g_value_get_int (value);
307 screen = gdk_display_get_screen (display, screen_number);
314 * get_window_run_mode_from_args:
315 * @args: a #GHashTable with data passed to the application.
317 * It does look if the mode option has been passed from command line, using it
318 * as the window run mode, otherwise the run mode will be the normal mode.
320 * Returns: The window run mode passed from command line or
321 * EV_WINDOW_MODE_NORMAL in other case.
323 static EvWindowRunMode
324 get_window_run_mode_from_args (GHashTable *args)
326 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
327 GValue *value = NULL;
329 g_assert (args != NULL);
331 value = g_hash_table_lookup (args, "mode");
333 mode = g_value_get_uint (value);
340 * get_destination_from_args:
341 * @args: a #GHashTable with data passed to the application.
343 * It does look for the page-label argument parsed from the command line and
344 * if it does exist, it returns an #EvLinkDest.
346 * Returns: An #EvLinkDest to page-label if it has been passed from the command
347 * line, NULL in other case.
350 get_destination_from_args (GHashTable *args)
352 EvLinkDest *dest = NULL;
353 GValue *value = NULL;
355 g_assert (args != NULL);
357 value = g_hash_table_lookup (args, "page-label");
359 const gchar *page_label;
361 page_label = g_value_get_string (value);
362 dest = ev_link_dest_new_page_label (page_label);
369 get_find_string_from_args (GHashTable *args)
371 GValue *value = NULL;
373 g_assert (args != NULL);
375 value = g_hash_table_lookup (args, "find-string");
377 return value ? g_value_get_string (value) : NULL;
381 * get_unlink_temp_file_from_args:
382 * @args: a #GHashTable with data passed to the application.
384 * It does look if the unlink-temp-file option has been passed from the command
385 * line returning it's boolean representation, otherwise it does return %FALSE.
387 * Returns: the boolean representation of the unlink-temp-file value or %FALSE
391 get_unlink_temp_file_from_args (GHashTable *args)
393 gboolean unlink_temp_file = FALSE;
394 GValue *value = NULL;
396 g_assert (args != NULL);
398 value = g_hash_table_lookup (args, "unlink-temp-file");
400 unlink_temp_file = g_value_get_boolean (value);
403 return unlink_temp_file;
407 get_print_settings_from_args (GHashTable *args)
409 const gchar *print_settings = NULL;
410 GValue *value = NULL;
412 g_assert (args != NULL);
414 value = g_hash_table_lookup (args, "print-settings");
416 print_settings = g_value_get_string (value);
419 return print_settings;
423 * ev_application_open_window:
424 * @application: The instance of the application.
425 * @args: A #GHashTable with the arguments data.
426 * @timestamp: Current time value.
427 * @error: The #GError facility.
429 * Creates a new window and if the args are available, it's not NULL, it gets
430 * the screen from them and assigns the just created window to it. At last it
436 ev_application_open_window (EvApplication *application,
441 GtkWidget *new_window = ev_window_new ();
442 GdkScreen *screen = NULL;
445 screen = get_screen_from_args (args);
449 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
451 ev_application_add_icon_path_for_screen (screen);
453 gtk_widget_show (new_window);
455 gtk_window_present_with_time (GTK_WINDOW (new_window),
461 * ev_application_get_empty_window:
462 * @application: The instance of the application.
463 * @screen: The screen where the empty window will be search.
465 * It does look if there is any empty window in the indicated screen.
467 * Returns: The first empty #EvWindow in the passed #GdkScreen or NULL in other
471 ev_application_get_empty_window (EvApplication *application,
474 EvWindow *empty_window = NULL;
475 GList *windows = ev_application_get_windows (application);
478 for (l = windows; l != NULL; l = l->next) {
479 EvWindow *window = EV_WINDOW (l->data);
481 if (ev_window_is_empty (window) &&
482 gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
483 empty_window = window;
488 g_list_free (windows);
494 * ev_application_get_uri_window:
495 * @application: The instance of the application.
496 * @uri: The uri to be opened.
498 * It looks in the list of the windows for the one with the document represented
499 * by the passed uri on it. If the window is empty or the document isn't present
500 * on any window, it will return NULL.
502 * Returns: The #EvWindow where the document represented by the passed uri is
503 * shown, NULL in other case.
506 ev_application_get_uri_window (EvApplication *application, const char *uri)
508 EvWindow *uri_window = NULL;
509 GList *windows = gtk_window_list_toplevels ();
512 g_return_val_if_fail (uri != NULL, NULL);
514 for (l = windows; l != NULL; l = l->next) {
515 if (EV_IS_WINDOW (l->data)) {
516 EvWindow *window = EV_WINDOW (l->data);
517 const char *window_uri = ev_window_get_uri (window);
519 if (window_uri && strcmp (window_uri, uri) == 0 && !ev_window_is_empty (window)) {
526 g_list_free (windows);
532 ev_application_add_icon_path_for_screen (GdkScreen *screen)
534 GtkIconTheme *icon_theme;
536 icon_theme = gtk_icon_theme_get_for_screen (screen);
541 gchar *ev_icons_path;
543 /* GtkIconTheme will then look in Evince custom hicolor dir
544 * for icons as well as the standard search paths
546 ev_icons_path = g_build_filename (DATADIR, "icons", NULL);
547 gtk_icon_theme_get_search_path (icon_theme, &path, &n_paths);
548 for (i = n_paths - 1; i >= 0; i--) {
549 if (g_ascii_strcasecmp (ev_icons_path, path[i]) == 0)
554 gtk_icon_theme_append_search_path (icon_theme,
557 g_free (ev_icons_path);
563 * ev_application_open_uri_at_dest:
564 * @application: The instance of the application.
565 * @uri: The uri to be opened.
566 * @screen: Thee screen where the link will be shown.
567 * @dest: The #EvLinkDest of the document.
568 * @mode: The run mode of the window.
569 * @unlink_temp_file: The unlink_temp_file option value.
570 * @timestamp: Current time value.
573 ev_application_open_uri_at_dest (EvApplication *application,
577 EvWindowRunMode mode,
578 const gchar *search_string,
579 gboolean unlink_temp_file,
580 const gchar *print_settings,
583 EvWindow *new_window;
585 g_return_if_fail (uri != NULL);
587 ev_application_add_icon_path_for_screen (screen);
589 new_window = ev_application_get_uri_window (application, uri);
591 if (new_window == NULL) {
592 new_window = ev_application_get_empty_window (application, screen);
595 if (new_window == NULL) {
596 new_window = EV_WINDOW (ev_window_new ());
600 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
602 /* We need to load uri before showing the window, so
603 we can restore window size without flickering */
604 ev_window_open_uri (new_window, uri, dest, mode, search_string,
605 unlink_temp_file, print_settings);
607 ev_document_fc_mutex_lock ();
608 gtk_widget_show (GTK_WIDGET (new_window));
609 ev_document_fc_mutex_unlock ();
611 gtk_window_present_with_time (GTK_WINDOW (new_window),
616 * ev_application_open_uri:
617 * @application: The instance of the application.
618 * @uri: The uri to be opened
619 * @args: A #GHashTable with the arguments data.
620 * @timestamp: Current time value.
621 * @error: The #GError facility.
624 ev_application_open_uri (EvApplication *application,
630 EvLinkDest *dest = NULL;
631 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
632 const gchar *search_string = NULL;
633 gboolean unlink_temp_file = FALSE;
634 const gchar *print_settings = NULL;
635 GdkScreen *screen = NULL;
638 screen = get_screen_from_args (args);
639 dest = get_destination_from_args (args);
640 mode = get_window_run_mode_from_args (args);
641 search_string = get_find_string_from_args (args);
642 unlink_temp_file = (mode == EV_WINDOW_MODE_PREVIEW &&
643 get_unlink_temp_file_from_args (args));
644 print_settings = get_print_settings_from_args (args);
647 ev_application_open_uri_at_dest (application, uri, screen,
648 dest, mode, search_string,
650 print_settings, timestamp);
653 g_object_unref (dest);
659 ev_application_open_uri_list (EvApplication *application,
666 for (l = uri_list; l != NULL; l = l->next) {
667 ev_application_open_uri_at_dest (application, (char *)l->data,
668 screen, NULL, 0, NULL,
669 FALSE, NULL, timestamp);
674 ev_application_shutdown (EvApplication *application)
676 if (application->accel_map_file) {
677 gtk_accel_map_save (application->accel_map_file);
678 g_free (application->accel_map_file);
679 application->accel_map_file = NULL;
682 if (application->toolbars_model) {
683 g_object_unref (application->toolbars_model);
684 g_free (application->toolbars_file);
685 application->toolbars_model = NULL;
686 application->toolbars_file = NULL;
689 #if GTK_CHECK_VERSION (2, 11, 0)
690 if (application->print_settings_file) {
691 if (application->print_settings) {
692 GError *error = NULL;
694 gtk_print_settings_to_file (application->print_settings,
695 application->print_settings_file,
698 g_warning ("%s", error->message);
699 g_error_free (error);
702 g_object_unref (application->print_settings);
703 application->print_settings = NULL;
706 g_free (application->print_settings_file);
707 application->print_settings_file = NULL;
709 #else /* ! GTK 2.11.0 */
710 if (application->print_settings) {
711 g_object_unref (application->print_settings);
712 application->print_settings = NULL;
714 #endif /* GTK 2.11.0 */
716 g_free (application->last_chooser_uri);
717 g_object_unref (application);
723 ev_application_class_init (EvApplicationClass *ev_application_class)
728 ev_application_init (EvApplication *ev_application)
731 const gchar *home_dir;
733 ev_application_init_session (ev_application);
735 home_dir = g_get_home_dir ();
737 ev_application->accel_map_file = g_build_filename (home_dir,
742 gtk_accel_map_load (ev_application->accel_map_file);
745 ev_application->toolbars_model = egg_toolbars_model_new ();
747 ev_application->toolbars_file = g_build_filename
748 (ev_dot_dir (), "evince_toolbar.xml", NULL);
750 egg_toolbars_model_load_names (ev_application->toolbars_model,
751 DATADIR "/evince-toolbar.xml");
753 if (!egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
754 ev_application->toolbars_file)) {
755 egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
756 DATADIR"/evince-toolbar.xml");
759 /* Open item doesn't exist anymore,
760 * convert it to OpenRecent for compatibility
762 for (i = 0; i < egg_toolbars_model_n_items (ev_application->toolbars_model, 0); i++) {
765 item = egg_toolbars_model_item_nth (ev_application->toolbars_model, 0, i);
766 if (g_ascii_strcasecmp (item, "FileOpen") == 0) {
767 egg_toolbars_model_remove_item (ev_application->toolbars_model, 0, i);
768 egg_toolbars_model_add_item (ev_application->toolbars_model, 0, i,
770 ev_application_save_toolbars_model (ev_application);
775 egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
776 EGG_TB_MODEL_NOT_REMOVABLE);
779 ev_application->keys = ev_media_player_keys_new ();
780 #endif /* ENABLE_DBUS */
784 * ev_application_get_windows:
785 * @application: The instance of the application.
787 * It creates a list of the top level windows.
789 * Returns: A #GList of the top level windows.
792 ev_application_get_windows (EvApplication *application)
794 GList *l, *toplevels;
795 GList *windows = NULL;
797 toplevels = gtk_window_list_toplevels ();
799 for (l = toplevels; l != NULL; l = l->next) {
800 if (EV_IS_WINDOW (l->data)) {
801 windows = g_list_append (windows, l->data);
805 g_list_free (toplevels);
811 * ev_application_get_media_keys:
812 * @application: The instance of the application.
814 * It gives you access to the media player keys handler object.
816 * Returns: A #EvMediaPlayerKeys.
819 *ev_application_get_media_keys (EvApplication *application)
822 if (!application->keys)
824 return g_object_ref (G_OBJECT (application->keys));
827 #endif /* ENABLE_DBUS */
831 ev_application_get_toolbars_model (EvApplication *application)
833 return application->toolbars_model;
837 ev_application_save_toolbars_model (EvApplication *application)
839 egg_toolbars_model_save_toolbars (application->toolbars_model,
840 application->toolbars_file, "1.0");
844 ev_application_set_chooser_uri (EvApplication *application, const gchar *uri)
846 g_free (application->last_chooser_uri);
847 application->last_chooser_uri = g_strdup (uri);
851 ev_application_get_chooser_uri (EvApplication *application)
853 return application->last_chooser_uri;
857 ev_application_screensaver_enable (EvApplication *application)
859 if (application->scr_saver)
860 totem_scrsaver_enable (application->scr_saver);
864 ev_application_screensaver_disable (EvApplication *application)
866 if (application->scr_saver)
867 totem_scrsaver_disable (application->scr_saver);
871 ev_application_get_print_settings (EvApplication *application)
873 if (application->print_settings)
874 return application->print_settings;
876 #if GTK_CHECK_VERSION (2, 11, 0)
877 if (!application->print_settings_file) {
878 application->print_settings_file =
879 g_build_filename (ev_dot_dir (), "print-settings", NULL);
882 if (g_file_test (application->print_settings_file, G_FILE_TEST_IS_REGULAR)) {
883 GError *error = NULL;
885 application->print_settings =
886 gtk_print_settings_new_from_file (application->print_settings_file, &error);
889 g_warning ("%s", error->message);
890 g_error_free (error);
892 return application->print_settings;
895 #endif /* GTK 2.11.0 */
897 application->print_settings = gtk_print_settings_new ();
899 return application->print_settings;
903 ev_application_set_print_settings (EvApplication *application,
904 GtkPrintSettings *settings)
906 g_return_if_fail (GTK_IS_PRINT_SETTINGS (settings));
908 if (settings == application->print_settings)
911 if (application->print_settings)
912 g_object_unref (application->print_settings);
914 application->print_settings = g_object_ref (settings);