]> www.fi.muni.cz Git - evince.git/blob - shell/ev-application.c
e852561f3009de2b4c202d9d3e1812a0367c7f9c
[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         if (!GTK_WIDGET_REALIZED (new_window))
455                 gtk_widget_realize (new_window);
456         
457         if (timestamp <= 0)
458                 timestamp = gdk_x11_get_server_time (GTK_WIDGET (new_window)->window);
459         gdk_x11_window_set_user_time (GTK_WIDGET (new_window)->window, timestamp);
460         
461         gtk_window_present (GTK_WINDOW (new_window));
462
463         return TRUE;
464 }
465
466 /**
467  * ev_application_get_empty_window:
468  * @application: The instance of the application.
469  * @screen: The screen where the empty window will be search.
470  *
471  * It does look if there is any empty window in the indicated screen.
472  *
473  * Returns: The first empty #EvWindow in the passed #GdkScreen or NULL in other
474  *          case.
475  */
476 static EvWindow *
477 ev_application_get_empty_window (EvApplication *application,
478                                  GdkScreen     *screen)
479 {
480         EvWindow *empty_window = NULL;
481         GList *windows = ev_application_get_windows (application);
482         GList *l;
483
484         for (l = windows; l != NULL; l = l->next) {
485                 EvWindow *window = EV_WINDOW (l->data);
486
487                 if (ev_window_is_empty (window) &&
488                     gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
489                         empty_window = window;
490                         break;
491                 }
492         }
493
494         g_list_free (windows);
495         
496         return empty_window;
497 }
498
499 /**
500  * ev_application_get_uri_window:
501  * @application: The instance of the application.
502  * @uri: The uri to be opened.
503  *
504  * It looks in the list of the windows for the one with the document represented
505  * by the passed uri on it. If the window is empty or the document isn't present
506  * on any window, it will return NULL.
507  *
508  * Returns: The #EvWindow where the document represented by the passed uri is
509  *          shown, NULL in other case.
510  */
511 static EvWindow *
512 ev_application_get_uri_window (EvApplication *application, const char *uri)
513 {
514         EvWindow *uri_window = NULL;
515         GList *windows = gtk_window_list_toplevels ();
516         GList *l;
517
518         g_return_val_if_fail (uri != NULL, NULL);
519
520         for (l = windows; l != NULL; l = l->next) {
521                 if (EV_IS_WINDOW (l->data)) {
522                         EvWindow *window = EV_WINDOW (l->data);
523                         const char *window_uri = ev_window_get_uri (window);
524
525                         if (window_uri && strcmp (window_uri, uri) == 0 && !ev_window_is_empty (window)) {
526                                 uri_window = window;
527                                 break;
528                         }
529                 }
530         }
531
532         g_list_free (windows);
533         
534         return uri_window;
535 }
536
537 static void
538 ev_application_add_icon_path_for_screen (GdkScreen *screen)
539 {
540         GtkIconTheme *icon_theme;
541
542         icon_theme = screen ? gtk_icon_theme_get_for_screen (screen) : gtk_icon_theme_get_default ();
543         if (icon_theme) {
544                 gchar **path = NULL;
545                 gint    n_paths;
546                 gint    i;
547                 gchar  *ev_icons_path;
548
549                 /* GtkIconTheme will then look in Evince custom hicolor dir
550                  * for icons as well as the standard search paths
551                  */
552                 ev_icons_path = g_build_filename (DATADIR, "icons", NULL);
553                 gtk_icon_theme_get_search_path (icon_theme, &path, &n_paths);
554                 for (i = n_paths - 1; i >= 0; i--) {
555                         if (g_ascii_strcasecmp (ev_icons_path, path[i]) == 0)
556                                 break;
557                 }
558
559                 if (i < 0)
560                         gtk_icon_theme_append_search_path (icon_theme,
561                                                            ev_icons_path);
562
563                 g_free (ev_icons_path);
564                 g_strfreev (path);
565         }       
566 }
567
568 /**
569  * ev_application_open_uri_at_dest:
570  * @application: The instance of the application.
571  * @uri: The uri to be opened.
572  * @screen: Thee screen where the link will be shown.
573  * @dest: The #EvLinkDest of the document.
574  * @mode: The run mode of the window.
575  * @unlink_temp_file: The unlink_temp_file option value.
576  * @timestamp: Current time value.
577  */
578 void
579 ev_application_open_uri_at_dest (EvApplication  *application,
580                                  const char     *uri,
581                                  GdkScreen      *screen,
582                                  EvLinkDest     *dest,
583                                  EvWindowRunMode mode,
584                                  const gchar    *search_string,
585                                  gboolean        unlink_temp_file,
586                                  const gchar    *print_settings, 
587                                  guint           timestamp)
588 {
589         EvWindow *new_window;
590
591         g_return_if_fail (uri != NULL);
592         
593         ev_application_add_icon_path_for_screen (screen);
594
595         new_window = ev_application_get_uri_window (application, uri);
596         
597         if (new_window == NULL) {
598                 new_window = ev_application_get_empty_window (application, screen);
599         }
600
601         if (new_window == NULL) {
602                 new_window = EV_WINDOW (ev_window_new ());
603         }
604
605         if (screen)
606                 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
607
608         /* We need to load uri before showing the window, so
609            we can restore window size without flickering */     
610         ev_window_open_uri (new_window, uri, dest, mode, search_string, 
611                             unlink_temp_file, print_settings);
612
613         if (!GTK_WIDGET_REALIZED (GTK_WIDGET (new_window)))
614                 gtk_widget_realize (GTK_WIDGET (new_window));
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         ev_document_fc_mutex_lock ();
621         gtk_window_present (GTK_WINDOW (new_window));
622         ev_document_fc_mutex_unlock ();
623 }
624
625 /**
626  * ev_application_open_uri:
627  * @application: The instance of the application.
628  * @uri: The uri to be opened
629  * @args: A #GHashTable with the arguments data.
630  * @timestamp: Current time value.
631  * @error: The #GError facility.
632  */
633 gboolean
634 ev_application_open_uri (EvApplication  *application,
635                          const char     *uri,
636                          GHashTable     *args,
637                          guint           timestamp,
638                          GError        **error)
639 {
640         EvLinkDest      *dest = NULL;
641         EvWindowRunMode  mode = EV_WINDOW_MODE_NORMAL;
642         const gchar     *search_string = NULL;
643         gboolean         unlink_temp_file = FALSE;
644         const gchar     *print_settings = NULL;
645         GdkScreen       *screen = NULL;
646
647         if (args) {
648                 screen = get_screen_from_args (args);
649                 dest = get_destination_from_args (args);
650                 mode = get_window_run_mode_from_args (args);
651                 search_string = get_find_string_from_args (args);
652                 unlink_temp_file = (mode == EV_WINDOW_MODE_PREVIEW &&
653                                     get_unlink_temp_file_from_args (args));
654                 print_settings = get_print_settings_from_args (args);
655         }
656         
657         ev_application_open_uri_at_dest (application, uri, screen,
658                                          dest, mode, search_string,
659                                          unlink_temp_file,
660                                          print_settings, timestamp);
661
662         if (dest)
663                 g_object_unref (dest);
664
665         return TRUE;
666 }
667
668 void
669 ev_application_open_uri_list (EvApplication *application,
670                               GSList        *uri_list,
671                               GdkScreen     *screen,
672                               guint          timestamp)
673 {
674         GSList *l;
675
676         for (l = uri_list; l != NULL; l = l->next) {
677                 ev_application_open_uri_at_dest (application, (char *)l->data,
678                                                  screen, NULL, 0, NULL,
679                                                  FALSE, NULL, timestamp);
680         }
681 }
682
683 void
684 ev_application_shutdown (EvApplication *application)
685 {
686         if (application->accel_map_file) {
687                 gtk_accel_map_save (application->accel_map_file);
688                 g_free (application->accel_map_file);
689                 application->accel_map_file = NULL;
690         }
691         
692         if (application->toolbars_model) {
693                 g_object_unref (application->toolbars_model);
694                 g_free (application->toolbars_file);
695                 application->toolbars_model = NULL;
696                 application->toolbars_file = NULL;
697         }
698
699 #if GTK_CHECK_VERSION (2, 11, 0)
700         if (application->print_settings_file) {
701                 if (application->print_settings) {
702                         GError *error = NULL;
703                         
704                         gtk_print_settings_to_file (application->print_settings,
705                                                     application->print_settings_file,
706                                                     &error);
707                         if (error) {
708                                 g_warning ("%s", error->message);
709                                 g_error_free (error);
710                         }
711
712                         g_object_unref (application->print_settings);
713                         application->print_settings = NULL;
714                 }
715
716                 g_free (application->print_settings_file);
717                 application->print_settings_file = NULL;
718         }
719 #else /* ! GTK 2.11.0 */
720         if (application->print_settings) {
721                 g_object_unref (application->print_settings);
722                 application->print_settings = NULL;
723         }
724 #endif /* GTK 2.11.0 */
725
726 #ifdef ENABLE_DBUS
727         if (application->keys) {
728                 g_object_unref (application->keys);
729                 application->keys = NULL;
730         }
731 #endif /* ENABLE_DBUS */
732         
733         g_free (application->last_chooser_uri);
734         g_object_unref (application);
735         
736         gtk_main_quit ();
737 }
738
739 static void
740 ev_application_class_init (EvApplicationClass *ev_application_class)
741 {
742 }
743
744 static void
745 ev_application_init (EvApplication *ev_application)
746 {
747         gint i;
748         const gchar *home_dir;
749         
750         ev_application_init_session (ev_application);
751
752         home_dir = g_get_home_dir ();
753         if (home_dir) {
754                 ev_application->accel_map_file = g_build_filename (home_dir,
755                                                                    ".gnome2",
756                                                                    "accels"
757                                                                    "evince",
758                                                                    NULL);
759                 gtk_accel_map_load (ev_application->accel_map_file);
760         }
761         
762         ev_application->toolbars_model = egg_toolbars_model_new ();
763
764         ev_application->toolbars_file = g_build_filename
765                         (ev_dot_dir (), "evince_toolbar.xml", NULL);
766
767         egg_toolbars_model_load_names (ev_application->toolbars_model,
768                                        DATADIR "/evince-toolbar.xml");
769
770         if (!egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
771                                                ev_application->toolbars_file)) {
772                 egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
773                                                   DATADIR"/evince-toolbar.xml");
774         }
775
776         /* Open item doesn't exist anymore,
777          * convert it to OpenRecent for compatibility
778          */
779         for (i = 0; i < egg_toolbars_model_n_items (ev_application->toolbars_model, 0); i++) {
780                 const gchar *item;
781                 
782                 item = egg_toolbars_model_item_nth (ev_application->toolbars_model, 0, i);
783                 if (g_ascii_strcasecmp (item, "FileOpen") == 0) {
784                         egg_toolbars_model_remove_item (ev_application->toolbars_model, 0, i);
785                         egg_toolbars_model_add_item (ev_application->toolbars_model, 0, i,
786                                                      "FileOpenRecent");
787                         ev_application_save_toolbars_model (ev_application);
788                         break;
789                 }
790         }
791
792         egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
793                                       EGG_TB_MODEL_NOT_REMOVABLE);
794
795 #ifdef ENABLE_DBUS
796         ev_application->keys = ev_media_player_keys_new ();
797 #endif /* ENABLE_DBUS */
798 }
799
800 /**
801  * ev_application_get_windows:
802  * @application: The instance of the application.
803  *
804  * It creates a list of the top level windows.
805  *
806  * Returns: A #GList of the top level windows.
807  */
808 GList *
809 ev_application_get_windows (EvApplication *application)
810 {
811         GList *l, *toplevels;
812         GList *windows = NULL;
813
814         toplevels = gtk_window_list_toplevels ();
815
816         for (l = toplevels; l != NULL; l = l->next) {
817                 if (EV_IS_WINDOW (l->data)) {
818                         windows = g_list_append (windows, l->data);
819                 }
820         }
821
822         g_list_free (toplevels);
823
824         return windows;
825 }
826
827 /**
828  * ev_application_get_media_keys:
829  * @application: The instance of the application.
830  *
831  * It gives you access to the media player keys handler object.
832  *
833  * Returns: A #EvMediaPlayerKeys.
834  */
835 GObject *
836 ev_application_get_media_keys (EvApplication *application)
837 {
838 #ifdef ENABLE_DBUS
839         return G_OBJECT (application->keys);
840 #else
841         return NULL;
842 #endif /* ENABLE_DBUS */
843 }
844
845 EggToolbarsModel *
846 ev_application_get_toolbars_model (EvApplication *application)
847 {
848         return application->toolbars_model;
849 }
850
851 void
852 ev_application_save_toolbars_model (EvApplication *application)
853 {
854         egg_toolbars_model_save_toolbars (application->toolbars_model,
855                                           application->toolbars_file, "1.0");
856 }
857
858 void
859 ev_application_set_chooser_uri (EvApplication *application, const gchar *uri)
860 {
861         g_free (application->last_chooser_uri);
862         application->last_chooser_uri = g_strdup (uri);
863 }
864
865 const gchar *
866 ev_application_get_chooser_uri (EvApplication *application)
867 {
868         return application->last_chooser_uri;
869 }
870
871 void
872 ev_application_screensaver_enable (EvApplication *application)
873 {
874         if (application->scr_saver)
875                 totem_scrsaver_enable (application->scr_saver); 
876 }
877
878 void
879 ev_application_screensaver_disable (EvApplication *application)
880 {
881         if (application->scr_saver)
882                 totem_scrsaver_disable (application->scr_saver);        
883 }
884
885 GtkPrintSettings *
886 ev_application_get_print_settings (EvApplication *application)
887 {
888         if (application->print_settings)
889                 return application->print_settings;
890         
891 #if GTK_CHECK_VERSION (2, 11, 0)
892         if (!application->print_settings_file) {
893                 application->print_settings_file =
894                         g_build_filename (ev_dot_dir (), "print-settings", NULL);
895         }
896
897         if (g_file_test (application->print_settings_file, G_FILE_TEST_IS_REGULAR)) {
898                 GError *error = NULL;
899                 
900                 application->print_settings =
901                         gtk_print_settings_new_from_file (application->print_settings_file, &error);
902                 
903                 if (error) {
904                         g_warning ("%s", error->message);
905                         g_error_free (error);
906                 } else {
907                         return application->print_settings;
908                 }
909         }
910 #endif /* GTK 2.11.0 */
911         
912         application->print_settings = gtk_print_settings_new ();
913
914         return application->print_settings;
915 }
916
917 void
918 ev_application_set_print_settings (EvApplication    *application,
919                                    GtkPrintSettings *settings)
920 {
921         g_return_if_fail (GTK_IS_PRINT_SETTINGS (settings));
922         
923         if (settings == application->print_settings)
924                 return;
925
926         if (application->print_settings)
927                 g_object_unref (application->print_settings);
928         
929         application->print_settings = g_object_ref (settings);
930 }
931