]> www.fi.muni.cz Git - evince.git/blob - shell/ev-application.c
Fix window manager warnings shown in ~/.xsession-errors
[evince.git] / shell / ev-application.c
1 /* this file is part of evince, a gnome document viewer
2  *
3  *  Copyright (C) 2004 Martin Kretzschmar
4  *
5  *  Author:
6  *    Martin Kretzschmar <martink@gnome.org>
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 #include <config.h>
24
25 #include <string.h>
26
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <gtk/gtk.h>
30 #include <gdk/gdkx.h>
31
32 #include "totem-scrsaver.h"
33 #include "eggsmclient.h"
34 #include "eggdesktopfile.h"
35
36 #include "ev-application.h"
37 #include "ev-document-factory.h"
38 #include "ev-file-helpers.h"
39 #include "ev-utils.h"
40
41 #ifdef ENABLE_DBUS
42 #include "ev-media-player-keys.h"
43 #endif /* ENABLE_DBUS */
44
45 #ifdef ENABLE_DBUS
46 #include <dbus/dbus-glib-bindings.h>
47 #include "ev-application-service.h"
48 #endif
49
50 static void ev_application_add_icon_path_for_screen (GdkScreen *screen);
51
52 struct _EvApplication {
53         GObject base_instance;
54
55         gchar *accel_map_file;
56         
57         gchar *toolbars_file;
58
59         EggToolbarsModel *toolbars_model;
60
61         TotemScrsaver *scr_saver;
62
63         EggSMClient *smclient;
64
65         gchar *last_chooser_uri;
66
67 #ifdef ENABLE_DBUS
68         EvMediaPlayerKeys *keys;
69 #endif /* ENABLE_DBUS */
70
71         GtkPrintSettings *print_settings;
72 #if GTK_CHECK_VERSION (2, 11, 0)
73         gchar            *print_settings_file;
74 #endif
75 };
76
77 struct _EvApplicationClass {
78         GObjectClass base_class;
79 };
80
81 G_DEFINE_TYPE (EvApplication, ev_application, G_TYPE_OBJECT);
82
83 #define APPLICATION_SERVICE_NAME "org.gnome.evince.ApplicationService"
84
85 #ifdef ENABLE_DBUS
86 gboolean
87 ev_application_register_service (EvApplication *application)
88 {
89         static DBusGConnection *connection = NULL;
90         DBusGProxy *driver_proxy;
91         GError *err = NULL;
92         guint request_name_result;
93
94         if (connection) {
95                 g_warning ("Service already registered.");
96                 return FALSE;
97         }
98         
99         connection = dbus_g_bus_get (DBUS_BUS_STARTER, &err);
100         if (connection == NULL) {
101                 g_warning ("Service registration failed.");
102                 g_error_free (err);
103
104                 return FALSE;
105         }
106
107         driver_proxy = dbus_g_proxy_new_for_name (connection,
108                                                   DBUS_SERVICE_DBUS,
109                                                   DBUS_PATH_DBUS,
110                                                   DBUS_INTERFACE_DBUS);
111
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);
118         }
119
120         g_object_unref (driver_proxy);
121         
122         if (request_name_result == DBUS_REQUEST_NAME_REPLY_EXISTS) {
123                 return FALSE;
124         }
125
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));
131         
132         application->scr_saver = totem_scrsaver_new (connection);
133         
134         return TRUE;
135 }
136 #endif /* ENABLE_DBUS */
137
138 /**
139  * ev_application_get_instance:
140  *
141  * Checks for #EvApplication instance, if it doesn't exist it does create it.
142  *
143  * Returns: an instance of the #EvApplication data.
144  */
145 EvApplication *
146 ev_application_get_instance (void)
147 {
148         static EvApplication *instance;
149
150         if (!instance) {
151                 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
152         }
153
154         return instance;
155 }
156
157 /* Session */
158 gboolean
159 ev_application_load_session (EvApplication *application)
160 {
161         GKeyFile *state_file;
162         gchar   **uri_list;
163         
164         if (!egg_sm_client_is_resumed (application->smclient))
165                 return FALSE;
166
167         state_file = egg_sm_client_get_state_file (application->smclient);
168         if (!state_file)
169                 return FALSE;
170
171         uri_list = g_key_file_get_string_list (state_file,
172                                                "Evince",
173                                                "documents",
174                                                NULL, NULL);
175         if (uri_list) {
176                 gint i;
177
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);
181                         else
182                                 ev_application_open_uri (application, uri_list[i], NULL, GDK_CURRENT_TIME, NULL);
183                 }
184                 g_strfreev (uri_list);
185         }
186         g_key_file_free (state_file);
187
188         return TRUE;
189 }
190
191 static void
192 smclient_save_state_cb (EggSMClient   *client,
193                         GKeyFile      *state_file,
194                         EvApplication *application)
195 {
196         GList *windows, *l;
197         gint i;
198         const gchar **uri_list;
199         const gchar *empty = "empty-window";
200
201         windows = ev_application_get_windows (application);
202         if (!windows)
203                 return;
204
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);
208
209                 if (ev_window_is_empty (window))
210                         uri_list[i] = empty;
211                 else
212                         uri_list[i] = ev_window_get_uri (window);
213         }
214         g_key_file_set_string_list (state_file,
215                                     "Evince",
216                                     "documents", 
217                                     (const char **)uri_list,
218                                     i);
219         g_free (uri_list);
220 }
221
222 static void
223 smclient_quit_cb (EggSMClient   *client,
224                   EvApplication *application)
225 {
226         ev_application_shutdown (application);
227 }
228
229 static void
230 ev_application_init_session (EvApplication *application)
231 {
232         egg_set_desktop_file (GNOMEDATADIR "/applications/evince.desktop");
233         
234         application->smclient = egg_sm_client_get ();
235         g_signal_connect (application->smclient, "save_state",
236                           G_CALLBACK (smclient_save_state_cb),
237                           application);
238         g_signal_connect (application->smclient, "quit",
239                           G_CALLBACK (smclient_quit_cb),
240                           application);
241 }
242
243 /**
244  * ev_display_open_if_needed:
245  * @name: the name of the display to be open if it's needed.
246  *
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.
249  *
250  * Returns: a #GdkDisplay of the display with the passed name.
251  */
252 static GdkDisplay *
253 ev_display_open_if_needed (const gchar *name)
254 {
255         GSList     *displays;
256         GSList     *l;
257         GdkDisplay *display = NULL;
258
259         displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
260
261         for (l = displays; l != NULL; l = l->next) {
262                 const gchar *display_name = gdk_display_get_name ((GdkDisplay *) l->data);
263
264                 if (g_ascii_strcasecmp (display_name, name) == 0) {
265                         display = l->data;
266                         break;
267                 }
268         }
269
270         g_slist_free (displays);
271
272         return display != NULL ? display : gdk_display_open (name);
273 }
274
275 /**
276  * get_screen_from_args:
277  * @args: a #GHashTable with data passed to the application.
278  *
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.
282  *
283  * Returns: the #GdkScreen assigned to the screen on the display indicated by
284  *          the data on the #GHashTable.
285  */
286 static GdkScreen *
287 get_screen_from_args (GHashTable *args)
288 {
289         GValue     *value = NULL;
290         GdkDisplay *display = NULL;
291         GdkScreen  *screen = NULL;
292
293         g_assert (args != NULL);
294         
295         value = g_hash_table_lookup (args, "display");
296         if (value) {
297                 const gchar *display_name;
298                 
299                 display_name = g_value_get_string (value);
300                 display = ev_display_open_if_needed (display_name);
301         }
302         
303         value = g_hash_table_lookup (args, "screen");
304         if (value) {
305                 gint screen_number;
306                 
307                 screen_number = g_value_get_int (value);
308                 screen = gdk_display_get_screen (display, screen_number);
309         }
310
311         return screen;
312 }
313
314 /**
315  * get_window_run_mode_from_args:
316  * @args: a #GHashTable with data passed to the application.
317  *
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.
320  *
321  * Returns: The window run mode passed from command line or
322  *          EV_WINDOW_MODE_NORMAL in other case.
323  */
324 static EvWindowRunMode
325 get_window_run_mode_from_args (GHashTable *args)
326 {
327         EvWindowRunMode  mode = EV_WINDOW_MODE_NORMAL;
328         GValue          *value = NULL;
329
330         g_assert (args != NULL);
331
332         value = g_hash_table_lookup (args, "mode");
333         if (value) {
334                 mode = g_value_get_uint (value);
335         }
336
337         return mode;
338 }
339
340 /**
341  * get_destination_from_args:
342  * @args: a #GHashTable with data passed to the application.
343  *
344  * It does look for the page-label argument parsed from the command line and
345  * if it does exist, it returns an #EvLinkDest.
346  *
347  * Returns: An #EvLinkDest to page-label if it has been passed from the command
348  *          line, NULL in other case.
349  */
350 static EvLinkDest *
351 get_destination_from_args (GHashTable *args)
352 {
353         EvLinkDest *dest = NULL;
354         GValue     *value = NULL;
355         
356         g_assert (args != NULL);
357         
358         value = g_hash_table_lookup (args, "page-label");
359         if (value) {
360                 const gchar *page_label;
361
362                 page_label = g_value_get_string (value);
363                 dest = ev_link_dest_new_page_label (page_label);
364         }
365
366         return dest;
367 }
368
369 static const gchar *
370 get_find_string_from_args (GHashTable *args)
371 {
372         GValue *value = NULL;
373
374         g_assert (args != NULL);
375
376         value = g_hash_table_lookup (args, "find-string");
377         
378         return value ? g_value_get_string (value) : NULL;
379 }
380
381 /**
382  * get_unlink_temp_file_from_args:
383  * @args: a #GHashTable with data passed to the application.
384  *
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.
387  *
388  * Returns: the boolean representation of the unlink-temp-file value or %FALSE
389  *          in other case.
390  */
391 static gboolean
392 get_unlink_temp_file_from_args (GHashTable *args)
393 {
394         gboolean unlink_temp_file = FALSE;
395         GValue  *value = NULL;
396
397         g_assert (args != NULL);
398
399         value = g_hash_table_lookup (args, "unlink-temp-file");
400         if (value) {
401                 unlink_temp_file = g_value_get_boolean (value);
402         }
403         
404         return unlink_temp_file;
405 }
406
407 static const gchar *
408 get_print_settings_from_args (GHashTable *args)
409 {
410         const gchar *print_settings = NULL;
411         GValue      *value = NULL;
412
413         g_assert (args != NULL);
414
415         value = g_hash_table_lookup (args, "print-settings");
416         if (value) {
417                 print_settings = g_value_get_string (value);
418         }
419
420         return print_settings;
421 }
422
423 /**
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.
429  * 
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
432  * does show it.
433  *
434  * Returns: %TRUE.
435  */
436 gboolean
437 ev_application_open_window (EvApplication  *application,
438                             GHashTable     *args,
439                             guint32         timestamp,
440                             GError        **error)
441 {
442         GtkWidget *new_window = ev_window_new ();
443         GdkScreen *screen = NULL;
444
445         if (args) {
446                 screen = get_screen_from_args (args);
447         }
448         
449         if (screen) {
450                 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
451         }
452         ev_application_add_icon_path_for_screen (screen);
453
454         gtk_widget_show (new_window);
455         
456         if (timestamp <= 0)
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);
459         
460         gtk_window_present (GTK_WINDOW (new_window));
461
462         return TRUE;
463 }
464
465 /**
466  * ev_application_get_empty_window:
467  * @application: The instance of the application.
468  * @screen: The screen where the empty window will be search.
469  *
470  * It does look if there is any empty window in the indicated screen.
471  *
472  * Returns: The first empty #EvWindow in the passed #GdkScreen or NULL in other
473  *          case.
474  */
475 static EvWindow *
476 ev_application_get_empty_window (EvApplication *application,
477                                  GdkScreen     *screen)
478 {
479         EvWindow *empty_window = NULL;
480         GList *windows = ev_application_get_windows (application);
481         GList *l;
482
483         for (l = windows; l != NULL; l = l->next) {
484                 EvWindow *window = EV_WINDOW (l->data);
485
486                 if (ev_window_is_empty (window) &&
487                     gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
488                         empty_window = window;
489                         break;
490                 }
491         }
492
493         g_list_free (windows);
494         
495         return empty_window;
496 }
497
498 /**
499  * ev_application_get_uri_window:
500  * @application: The instance of the application.
501  * @uri: The uri to be opened.
502  *
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.
506  *
507  * Returns: The #EvWindow where the document represented by the passed uri is
508  *          shown, NULL in other case.
509  */
510 static EvWindow *
511 ev_application_get_uri_window (EvApplication *application, const char *uri)
512 {
513         EvWindow *uri_window = NULL;
514         GList *windows = gtk_window_list_toplevels ();
515         GList *l;
516
517         g_return_val_if_fail (uri != NULL, NULL);
518
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);
523
524                         if (window_uri && strcmp (window_uri, uri) == 0 && !ev_window_is_empty (window)) {
525                                 uri_window = window;
526                                 break;
527                         }
528                 }
529         }
530
531         g_list_free (windows);
532         
533         return uri_window;
534 }
535
536 static void
537 ev_application_add_icon_path_for_screen (GdkScreen *screen)
538 {
539         GtkIconTheme *icon_theme;
540
541         icon_theme = screen ? gtk_icon_theme_get_for_screen (screen) : gtk_icon_theme_get_default ();
542         if (icon_theme) {
543                 gchar **path = NULL;
544                 gint    n_paths;
545                 gint    i;
546                 gchar  *ev_icons_path;
547
548                 /* GtkIconTheme will then look in Evince custom hicolor dir
549                  * for icons as well as the standard search paths
550                  */
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)
555                                 break;
556                 }
557
558                 if (i < 0)
559                         gtk_icon_theme_append_search_path (icon_theme,
560                                                            ev_icons_path);
561
562                 g_free (ev_icons_path);
563                 g_strfreev (path);
564         }       
565 }
566
567 /**
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.
576  */
577 void
578 ev_application_open_uri_at_dest (EvApplication  *application,
579                                  const char     *uri,
580                                  GdkScreen      *screen,
581                                  EvLinkDest     *dest,
582                                  EvWindowRunMode mode,
583                                  const gchar    *search_string,
584                                  gboolean        unlink_temp_file,
585                                  const gchar    *print_settings, 
586                                  guint           timestamp)
587 {
588         EvWindow *new_window;
589
590         g_return_if_fail (uri != NULL);
591         
592         ev_application_add_icon_path_for_screen (screen);
593
594         new_window = ev_application_get_uri_window (application, uri);
595         
596         if (new_window == NULL) {
597                 new_window = ev_application_get_empty_window (application, screen);
598         }
599
600         if (new_window == NULL) {
601                 new_window = EV_WINDOW (ev_window_new ());
602         }
603
604         if (screen)
605                 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
606
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);
611
612         ev_document_fc_mutex_lock ();
613         gtk_widget_show (GTK_WIDGET (new_window));
614         ev_document_fc_mutex_unlock ();
615
616         if (timestamp <= 0)
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);
619         
620         gtk_window_present (GTK_WINDOW (new_window));
621 }
622
623 /**
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.
630  */
631 gboolean
632 ev_application_open_uri (EvApplication  *application,
633                          const char     *uri,
634                          GHashTable     *args,
635                          guint           timestamp,
636                          GError        **error)
637 {
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;
644
645         if (args) {
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);
653         }
654         
655         ev_application_open_uri_at_dest (application, uri, screen,
656                                          dest, mode, search_string,
657                                          unlink_temp_file,
658                                          print_settings, timestamp);
659
660         if (dest)
661                 g_object_unref (dest);
662
663         return TRUE;
664 }
665
666 void
667 ev_application_open_uri_list (EvApplication *application,
668                               GSList        *uri_list,
669                               GdkScreen     *screen,
670                               guint          timestamp)
671 {
672         GSList *l;
673
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);
678         }
679 }
680
681 void
682 ev_application_shutdown (EvApplication *application)
683 {
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;
688         }
689         
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;
695         }
696
697 #if GTK_CHECK_VERSION (2, 11, 0)
698         if (application->print_settings_file) {
699                 if (application->print_settings) {
700                         GError *error = NULL;
701                         
702                         gtk_print_settings_to_file (application->print_settings,
703                                                     application->print_settings_file,
704                                                     &error);
705                         if (error) {
706                                 g_warning ("%s", error->message);
707                                 g_error_free (error);
708                         }
709
710                         g_object_unref (application->print_settings);
711                         application->print_settings = NULL;
712                 }
713
714                 g_free (application->print_settings_file);
715                 application->print_settings_file = NULL;
716         }
717 #else /* ! GTK 2.11.0 */
718         if (application->print_settings) {
719                 g_object_unref (application->print_settings);
720                 application->print_settings = NULL;
721         }
722 #endif /* GTK 2.11.0 */
723         
724         g_free (application->last_chooser_uri);
725         g_object_unref (application);
726         
727         gtk_main_quit ();
728 }
729
730 static void
731 ev_application_class_init (EvApplicationClass *ev_application_class)
732 {
733 }
734
735 static void
736 ev_application_init (EvApplication *ev_application)
737 {
738         gint i;
739         const gchar *home_dir;
740         
741         ev_application_init_session (ev_application);
742
743         home_dir = g_get_home_dir ();
744         if (home_dir) {
745                 ev_application->accel_map_file = g_build_filename (home_dir,
746                                                                    ".gnome2",
747                                                                    "accels"
748                                                                    "evince",
749                                                                    NULL);
750                 gtk_accel_map_load (ev_application->accel_map_file);
751         }
752         
753         ev_application->toolbars_model = egg_toolbars_model_new ();
754
755         ev_application->toolbars_file = g_build_filename
756                         (ev_dot_dir (), "evince_toolbar.xml", NULL);
757
758         egg_toolbars_model_load_names (ev_application->toolbars_model,
759                                        DATADIR "/evince-toolbar.xml");
760
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");
765         }
766
767         /* Open item doesn't exist anymore,
768          * convert it to OpenRecent for compatibility
769          */
770         for (i = 0; i < egg_toolbars_model_n_items (ev_application->toolbars_model, 0); i++) {
771                 const gchar *item;
772                 
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,
777                                                      "FileOpenRecent");
778                         ev_application_save_toolbars_model (ev_application);
779                         break;
780                 }
781         }
782
783         egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
784                                       EGG_TB_MODEL_NOT_REMOVABLE);
785
786 #ifdef ENABLE_DBUS
787         ev_application->keys = ev_media_player_keys_new ();
788 #endif /* ENABLE_DBUS */
789 }
790
791 /**
792  * ev_application_get_windows:
793  * @application: The instance of the application.
794  *
795  * It creates a list of the top level windows.
796  *
797  * Returns: A #GList of the top level windows.
798  */
799 GList *
800 ev_application_get_windows (EvApplication *application)
801 {
802         GList *l, *toplevels;
803         GList *windows = NULL;
804
805         toplevels = gtk_window_list_toplevels ();
806
807         for (l = toplevels; l != NULL; l = l->next) {
808                 if (EV_IS_WINDOW (l->data)) {
809                         windows = g_list_append (windows, l->data);
810                 }
811         }
812
813         g_list_free (toplevels);
814
815         return windows;
816 }
817
818 /**
819  * ev_application_get_media_keys:
820  * @application: The instance of the application.
821  *
822  * It gives you access to the media player keys handler object.
823  *
824  * Returns: A #EvMediaPlayerKeys.
825  */
826 GObject
827 *ev_application_get_media_keys (EvApplication *application)
828 {
829 #ifdef ENABLE_DBUS
830         if (!application->keys)
831                 return NULL;
832         return g_object_ref (G_OBJECT (application->keys));
833 #else
834         return NULL;
835 #endif /* ENABLE_DBUS */
836 }
837
838 EggToolbarsModel *
839 ev_application_get_toolbars_model (EvApplication *application)
840 {
841         return application->toolbars_model;
842 }
843
844 void
845 ev_application_save_toolbars_model (EvApplication *application)
846 {
847         egg_toolbars_model_save_toolbars (application->toolbars_model,
848                                           application->toolbars_file, "1.0");
849 }
850
851 void
852 ev_application_set_chooser_uri (EvApplication *application, const gchar *uri)
853 {
854         g_free (application->last_chooser_uri);
855         application->last_chooser_uri = g_strdup (uri);
856 }
857
858 const gchar *
859 ev_application_get_chooser_uri (EvApplication *application)
860 {
861         return application->last_chooser_uri;
862 }
863
864 void
865 ev_application_screensaver_enable (EvApplication *application)
866 {
867         if (application->scr_saver)
868                 totem_scrsaver_enable (application->scr_saver); 
869 }
870
871 void
872 ev_application_screensaver_disable (EvApplication *application)
873 {
874         if (application->scr_saver)
875                 totem_scrsaver_disable (application->scr_saver);        
876 }
877
878 GtkPrintSettings *
879 ev_application_get_print_settings (EvApplication *application)
880 {
881         if (application->print_settings)
882                 return application->print_settings;
883         
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);
888         }
889
890         if (g_file_test (application->print_settings_file, G_FILE_TEST_IS_REGULAR)) {
891                 GError *error = NULL;
892                 
893                 application->print_settings =
894                         gtk_print_settings_new_from_file (application->print_settings_file, &error);
895                 
896                 if (error) {
897                         g_warning ("%s", error->message);
898                         g_error_free (error);
899                 } else {
900                         return application->print_settings;
901                 }
902         }
903 #endif /* GTK 2.11.0 */
904         
905         application->print_settings = gtk_print_settings_new ();
906
907         return application->print_settings;
908 }
909
910 void
911 ev_application_set_print_settings (EvApplication    *application,
912                                    GtkPrintSettings *settings)
913 {
914         g_return_if_fail (GTK_IS_PRINT_SETTINGS (settings));
915         
916         if (settings == application->print_settings)
917                 return;
918
919         if (application->print_settings)
920                 g_object_unref (application->print_settings);
921         
922         application->print_settings = g_object_ref (settings);
923 }
924