]> www.fi.muni.cz Git - evince.git/blob - shell/ev-application.c
bfd3f4ecff47ef0178b1fc9d6f7f7d52f0e097c3
[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
35 #include "ev-application.h"
36 #include "ev-document-factory.h"
37 #include "ev-file-helpers.h"
38 #include "ev-utils.h"
39
40 #ifdef ENABLE_DBUS
41 #include "ev-media-player-keys.h"
42 #endif /* ENABLE_DBUS */
43
44 #ifdef ENABLE_DBUS
45 #include <dbus/dbus-glib-bindings.h>
46 #include "ev-application-service.h"
47 #endif
48
49 static void ev_application_add_icon_path_for_screen (GdkScreen     *screen);
50 static void ev_application_save_print_settings      (EvApplication *application);
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         GtkPageSetup     *page_setup;
73         GKeyFile         *print_settings_file;
74 };
75
76 struct _EvApplicationClass {
77         GObjectClass base_class;
78 };
79
80 G_DEFINE_TYPE (EvApplication, ev_application, G_TYPE_OBJECT);
81
82 #define APPLICATION_SERVICE_NAME "org.gnome.evince.ApplicationService"
83
84 #define EV_PRINT_SETTINGS_FILE "print-settings"
85 #define EV_PRINT_SETTINGS_GROUP "Print Settings"
86 #define EV_PAGE_SETUP_GROUP "Page Setup"
87
88 #ifdef ENABLE_DBUS
89 gboolean
90 ev_application_register_service (EvApplication *application)
91 {
92         static DBusGConnection *connection = NULL;
93         DBusGProxy *driver_proxy;
94         GError *err = NULL;
95         guint request_name_result;
96
97         if (connection) {
98                 g_warning ("Service already registered.");
99                 return FALSE;
100         }
101         
102         connection = dbus_g_bus_get (DBUS_BUS_STARTER, &err);
103         if (connection == NULL) {
104                 g_warning ("Service registration failed.");
105                 g_error_free (err);
106
107                 return FALSE;
108         }
109
110         driver_proxy = dbus_g_proxy_new_for_name (connection,
111                                                   DBUS_SERVICE_DBUS,
112                                                   DBUS_PATH_DBUS,
113                                                   DBUS_INTERFACE_DBUS);
114
115         if (!org_freedesktop_DBus_request_name (driver_proxy,
116                                                 APPLICATION_SERVICE_NAME,
117                                                 DBUS_NAME_FLAG_DO_NOT_QUEUE,
118                                                 &request_name_result, &err)) {
119                 g_warning ("Service registration failed.");
120                 g_clear_error (&err);
121         }
122
123         g_object_unref (driver_proxy);
124         
125         if (request_name_result == DBUS_REQUEST_NAME_REPLY_EXISTS) {
126                 return FALSE;
127         }
128
129         dbus_g_object_type_install_info (EV_TYPE_APPLICATION,
130                                          &dbus_glib_ev_application_object_info);
131         dbus_g_connection_register_g_object (connection,
132                                              "/org/gnome/evince/Evince",
133                                              G_OBJECT (application));
134         
135         application->scr_saver = totem_scrsaver_new (connection);
136         
137         return TRUE;
138 }
139 #endif /* ENABLE_DBUS */
140
141 /**
142  * ev_application_get_instance:
143  *
144  * Checks for #EvApplication instance, if it doesn't exist it does create it.
145  *
146  * Returns: an instance of the #EvApplication data.
147  */
148 EvApplication *
149 ev_application_get_instance (void)
150 {
151         static EvApplication *instance;
152
153         if (!instance) {
154                 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
155         }
156
157         return instance;
158 }
159
160 /* Session */
161 gboolean
162 ev_application_load_session (EvApplication *application)
163 {
164         GKeyFile *state_file;
165         gchar   **uri_list;
166         
167         if (!egg_sm_client_is_resumed (application->smclient))
168                 return FALSE;
169
170         state_file = egg_sm_client_get_state_file (application->smclient);
171         if (!state_file)
172                 return FALSE;
173
174         uri_list = g_key_file_get_string_list (state_file,
175                                                "Evince",
176                                                "documents",
177                                                NULL, NULL);
178         if (uri_list) {
179                 gint i;
180
181                 for (i = 0; uri_list[i]; i++) {
182                         if (g_ascii_strcasecmp (uri_list[i], "empty-window") == 0)
183                                 ev_application_open_window (application, NULL, GDK_CURRENT_TIME, NULL);
184                         else
185                                 ev_application_open_uri (application, uri_list[i], NULL, GDK_CURRENT_TIME, NULL);
186                 }
187                 g_strfreev (uri_list);
188         }
189         g_key_file_free (state_file);
190
191         return TRUE;
192 }
193
194 static void
195 smclient_save_state_cb (EggSMClient   *client,
196                         GKeyFile      *state_file,
197                         EvApplication *application)
198 {
199         GList *windows, *l;
200         gint i;
201         const gchar **uri_list;
202         const gchar *empty = "empty-window";
203
204         windows = ev_application_get_windows (application);
205         if (!windows)
206                 return;
207
208         uri_list = g_new (const gchar *, g_list_length (windows));
209         for (l = windows, i = 0; l != NULL; l = g_list_next (l), i++) {
210                 EvWindow *window = EV_WINDOW (l->data);
211
212                 if (ev_window_is_empty (window))
213                         uri_list[i] = empty;
214                 else
215                         uri_list[i] = ev_window_get_uri (window);
216         }
217         g_key_file_set_string_list (state_file,
218                                     "Evince",
219                                     "documents", 
220                                     (const char **)uri_list,
221                                     i);
222         g_free (uri_list);
223 }
224
225 static void
226 smclient_quit_cb (EggSMClient   *client,
227                   EvApplication *application)
228 {
229         ev_application_shutdown (application);
230 }
231
232 static void
233 ev_application_init_session (EvApplication *application)
234 {
235         application->smclient = egg_sm_client_get ();
236         g_signal_connect (application->smclient, "save_state",
237                           G_CALLBACK (smclient_save_state_cb),
238                           application);
239         g_signal_connect (application->smclient, "quit",
240                           G_CALLBACK (smclient_quit_cb),
241                           application);
242 }
243
244 /**
245  * ev_display_open_if_needed:
246  * @name: the name of the display to be open if it's needed.
247  *
248  * Search among all the open displays if any of them have the same name as the
249  * passed name. If the display isn't found it tries the open it.
250  *
251  * Returns: a #GdkDisplay of the display with the passed name.
252  */
253 static GdkDisplay *
254 ev_display_open_if_needed (const gchar *name)
255 {
256         GSList     *displays;
257         GSList     *l;
258         GdkDisplay *display = NULL;
259
260         displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
261
262         for (l = displays; l != NULL; l = l->next) {
263                 const gchar *display_name = gdk_display_get_name ((GdkDisplay *) l->data);
264
265                 if (g_ascii_strcasecmp (display_name, name) == 0) {
266                         display = l->data;
267                         break;
268                 }
269         }
270
271         g_slist_free (displays);
272
273         return display != NULL ? display : gdk_display_open (name);
274 }
275
276 /**
277  * get_screen_from_args:
278  * @args: a #GHashTable with data passed to the application.
279  *
280  * Looks for the screen in the display available in the hash table passed to the
281  * application. If the display isn't opened, it's opened and the #GdkScreen
282  * assigned to the screen in that display returned.
283  *
284  * Returns: the #GdkScreen assigned to the screen on the display indicated by
285  *          the data on the #GHashTable.
286  */
287 static GdkScreen *
288 get_screen_from_args (GHashTable *args)
289 {
290         GValue     *value = NULL;
291         GdkDisplay *display = NULL;
292         GdkScreen  *screen = NULL;
293
294         g_assert (args != NULL);
295         
296         value = g_hash_table_lookup (args, "display");
297         if (value) {
298                 const gchar *display_name;
299                 
300                 display_name = g_value_get_string (value);
301                 display = ev_display_open_if_needed (display_name);
302         }
303         
304         value = g_hash_table_lookup (args, "screen");
305         if (value) {
306                 gint screen_number;
307                 
308                 screen_number = g_value_get_int (value);
309                 screen = gdk_display_get_screen (display, screen_number);
310         }
311
312         return screen;
313 }
314
315 /**
316  * get_window_run_mode_from_args:
317  * @args: a #GHashTable with data passed to the application.
318  *
319  * It does look if the mode option has been passed from command line, using it
320  * as the window run mode, otherwise the run mode will be the normal mode.
321  *
322  * Returns: The window run mode passed from command line or
323  *          EV_WINDOW_MODE_NORMAL in other case.
324  */
325 static EvWindowRunMode
326 get_window_run_mode_from_args (GHashTable *args)
327 {
328         EvWindowRunMode  mode = EV_WINDOW_MODE_NORMAL;
329         GValue          *value = NULL;
330
331         g_assert (args != NULL);
332
333         value = g_hash_table_lookup (args, "mode");
334         if (value) {
335                 mode = g_value_get_uint (value);
336         }
337
338         return mode;
339 }
340
341 /**
342  * get_destination_from_args:
343  * @args: a #GHashTable with data passed to the application.
344  *
345  * It does look for the page-label argument parsed from the command line and
346  * if it does exist, it returns an #EvLinkDest.
347  *
348  * Returns: An #EvLinkDest to page-label if it has been passed from the command
349  *          line, NULL in other case.
350  */
351 static EvLinkDest *
352 get_destination_from_args (GHashTable *args)
353 {
354         EvLinkDest *dest = NULL;
355         GValue     *value = NULL;
356         
357         g_assert (args != NULL);
358         
359         value = g_hash_table_lookup (args, "page-label");
360         if (value) {
361                 const gchar *page_label;
362
363                 page_label = g_value_get_string (value);
364                 dest = ev_link_dest_new_page_label (page_label);
365         }
366
367         return dest;
368 }
369
370 static const gchar *
371 get_find_string_from_args (GHashTable *args)
372 {
373         GValue *value = NULL;
374
375         g_assert (args != NULL);
376
377         value = g_hash_table_lookup (args, "find-string");
378         
379         return value ? g_value_get_string (value) : NULL;
380 }
381
382 /**
383  * get_unlink_temp_file_from_args:
384  * @args: a #GHashTable with data passed to the application.
385  *
386  * It does look if the unlink-temp-file option has been passed from the command
387  * line returning it's boolean representation, otherwise it does return %FALSE.
388  *
389  * Returns: the boolean representation of the unlink-temp-file value or %FALSE
390  *          in other case.
391  */
392 static gboolean
393 get_unlink_temp_file_from_args (GHashTable *args)
394 {
395         gboolean unlink_temp_file = FALSE;
396         GValue  *value = NULL;
397
398         g_assert (args != NULL);
399
400         value = g_hash_table_lookup (args, "unlink-temp-file");
401         if (value) {
402                 unlink_temp_file = g_value_get_boolean (value);
403         }
404         
405         return unlink_temp_file;
406 }
407
408 static const gchar *
409 get_print_settings_from_args (GHashTable *args)
410 {
411         const gchar *print_settings = NULL;
412         GValue      *value = NULL;
413
414         g_assert (args != NULL);
415
416         value = g_hash_table_lookup (args, "print-settings");
417         if (value) {
418                 print_settings = g_value_get_string (value);
419         }
420
421         return print_settings;
422 }
423
424 /**
425  * ev_application_open_window:
426  * @application: The instance of the application.
427  * @args: A #GHashTable with the arguments data.
428  * @timestamp: Current time value.
429  * @error: The #GError facility.
430  * 
431  * Creates a new window and if the args are available, it's not NULL, it gets
432  * the screen from them and assigns the just created window to it. At last it
433  * does show it.
434  *
435  * Returns: %TRUE.
436  */
437 gboolean
438 ev_application_open_window (EvApplication  *application,
439                             GHashTable     *args,
440                             guint32         timestamp,
441                             GError        **error)
442 {
443         GtkWidget *new_window = ev_window_new ();
444         GdkScreen *screen = NULL;
445
446         if (args) {
447                 screen = get_screen_from_args (args);
448         }
449         
450         if (screen) {
451                 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
452         }
453         ev_application_add_icon_path_for_screen (screen);
454
455         if (!GTK_WIDGET_REALIZED (new_window))
456                 gtk_widget_realize (new_window);
457         
458 #ifdef GDK_WINDOWING_X11
459         if (timestamp <= 0)
460                 timestamp = gdk_x11_get_server_time (GTK_WIDGET (new_window)->window);
461         gdk_x11_window_set_user_time (GTK_WIDGET (new_window)->window, timestamp);
462         
463         gtk_window_present (GTK_WINDOW (new_window));
464 #else
465         gtk_window_present_with_time (GTK_WINDOW (new_window), timestamp);
466 #endif /* GDK_WINDOWING_X11 */
467
468         return TRUE;
469 }
470
471 /**
472  * ev_application_get_empty_window:
473  * @application: The instance of the application.
474  * @screen: The screen where the empty window will be search.
475  *
476  * It does look if there is any empty window in the indicated screen.
477  *
478  * Returns: The first empty #EvWindow in the passed #GdkScreen or NULL in other
479  *          case.
480  */
481 static EvWindow *
482 ev_application_get_empty_window (EvApplication *application,
483                                  GdkScreen     *screen)
484 {
485         EvWindow *empty_window = NULL;
486         GList *windows = ev_application_get_windows (application);
487         GList *l;
488
489         for (l = windows; l != NULL; l = l->next) {
490                 EvWindow *window = EV_WINDOW (l->data);
491
492                 if (ev_window_is_empty (window) &&
493                     gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
494                         empty_window = window;
495                         break;
496                 }
497         }
498
499         g_list_free (windows);
500         
501         return empty_window;
502 }
503
504 /**
505  * ev_application_get_uri_window:
506  * @application: The instance of the application.
507  * @uri: The uri to be opened.
508  *
509  * It looks in the list of the windows for the one with the document represented
510  * by the passed uri on it. If the window is empty or the document isn't present
511  * on any window, it will return NULL.
512  *
513  * Returns: The #EvWindow where the document represented by the passed uri is
514  *          shown, NULL in other case.
515  */
516 static EvWindow *
517 ev_application_get_uri_window (EvApplication *application, const char *uri)
518 {
519         EvWindow *uri_window = NULL;
520         GList *windows = gtk_window_list_toplevels ();
521         GList *l;
522
523         g_return_val_if_fail (uri != NULL, NULL);
524
525         for (l = windows; l != NULL; l = l->next) {
526                 if (EV_IS_WINDOW (l->data)) {
527                         EvWindow *window = EV_WINDOW (l->data);
528                         const char *window_uri = ev_window_get_uri (window);
529
530                         if (window_uri && strcmp (window_uri, uri) == 0 && !ev_window_is_empty (window)) {
531                                 uri_window = window;
532                                 break;
533                         }
534                 }
535         }
536
537         g_list_free (windows);
538         
539         return uri_window;
540 }
541
542 static void
543 ev_application_add_icon_path_for_screen (GdkScreen *screen)
544 {
545         GtkIconTheme *icon_theme;
546
547         icon_theme = screen ? gtk_icon_theme_get_for_screen (screen) : gtk_icon_theme_get_default ();
548         if (icon_theme) {
549                 gchar **path = NULL;
550                 gint    n_paths;
551                 gint    i;
552                 gchar  *ev_icons_path;
553
554                 /* GtkIconTheme will then look in Evince custom hicolor dir
555                  * for icons as well as the standard search paths
556                  */
557                 ev_icons_path = g_build_filename (DATADIR, "icons", NULL);
558                 gtk_icon_theme_get_search_path (icon_theme, &path, &n_paths);
559                 for (i = n_paths - 1; i >= 0; i--) {
560                         if (g_ascii_strcasecmp (ev_icons_path, path[i]) == 0)
561                                 break;
562                 }
563
564                 if (i < 0)
565                         gtk_icon_theme_append_search_path (icon_theme,
566                                                            ev_icons_path);
567
568                 g_free (ev_icons_path);
569                 g_strfreev (path);
570         }       
571 }
572
573 /**
574  * ev_application_open_uri_at_dest:
575  * @application: The instance of the application.
576  * @uri: The uri to be opened.
577  * @screen: Thee screen where the link will be shown.
578  * @dest: The #EvLinkDest of the document.
579  * @mode: The run mode of the window.
580  * @unlink_temp_file: The unlink_temp_file option value.
581  * @timestamp: Current time value.
582  */
583 void
584 ev_application_open_uri_at_dest (EvApplication  *application,
585                                  const char     *uri,
586                                  GdkScreen      *screen,
587                                  EvLinkDest     *dest,
588                                  EvWindowRunMode mode,
589                                  const gchar    *search_string,
590                                  gboolean        unlink_temp_file,
591                                  const gchar    *print_settings, 
592                                  guint           timestamp)
593 {
594         EvWindow *new_window;
595
596         g_return_if_fail (uri != NULL);
597         
598         ev_application_add_icon_path_for_screen (screen);
599
600         new_window = ev_application_get_uri_window (application, uri);
601         
602         if (new_window == NULL) {
603                 new_window = ev_application_get_empty_window (application, screen);
604         }
605
606         if (new_window == NULL) {
607                 new_window = EV_WINDOW (ev_window_new ());
608         }
609
610         if (screen)
611                 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
612
613         /* We need to load uri before showing the window, so
614            we can restore window size without flickering */     
615         ev_window_open_uri (new_window, uri, dest, mode, search_string, 
616                             unlink_temp_file, print_settings);
617
618         if (!GTK_WIDGET_REALIZED (GTK_WIDGET (new_window)))
619                 gtk_widget_realize (GTK_WIDGET (new_window));
620
621 #ifdef GDK_WINDOWING_X11
622         if (timestamp <= 0)
623                 timestamp = gdk_x11_get_server_time (GTK_WIDGET (new_window)->window);
624         gdk_x11_window_set_user_time (GTK_WIDGET (new_window)->window, timestamp);
625
626         ev_document_fc_mutex_lock ();
627         gtk_window_present (GTK_WINDOW (new_window));
628         ev_document_fc_mutex_unlock ();
629 #else
630         ev_document_fc_mutex_lock ();
631         gtk_window_present_with_time (GTK_WINDOW (new_window), timestamp);
632         ev_document_fc_mutex_unlock ();
633 #endif /* GDK_WINDOWING_X11 */
634 }
635
636 /**
637  * ev_application_open_uri:
638  * @application: The instance of the application.
639  * @uri: The uri to be opened
640  * @args: A #GHashTable with the arguments data.
641  * @timestamp: Current time value.
642  * @error: The #GError facility.
643  */
644 gboolean
645 ev_application_open_uri (EvApplication  *application,
646                          const char     *uri,
647                          GHashTable     *args,
648                          guint           timestamp,
649                          GError        **error)
650 {
651         EvLinkDest      *dest = NULL;
652         EvWindowRunMode  mode = EV_WINDOW_MODE_NORMAL;
653         const gchar     *search_string = NULL;
654         gboolean         unlink_temp_file = FALSE;
655         const gchar     *print_settings = NULL;
656         GdkScreen       *screen = NULL;
657
658         if (args) {
659                 screen = get_screen_from_args (args);
660                 dest = get_destination_from_args (args);
661                 mode = get_window_run_mode_from_args (args);
662                 search_string = get_find_string_from_args (args);
663                 unlink_temp_file = (mode == EV_WINDOW_MODE_PREVIEW &&
664                                     get_unlink_temp_file_from_args (args));
665                 print_settings = get_print_settings_from_args (args);
666         }
667         
668         ev_application_open_uri_at_dest (application, uri, screen,
669                                          dest, mode, search_string,
670                                          unlink_temp_file,
671                                          print_settings, timestamp);
672
673         if (dest)
674                 g_object_unref (dest);
675
676         return TRUE;
677 }
678
679 void
680 ev_application_open_uri_list (EvApplication *application,
681                               GSList        *uri_list,
682                               GdkScreen     *screen,
683                               guint          timestamp)
684 {
685         GSList *l;
686
687         for (l = uri_list; l != NULL; l = l->next) {
688                 ev_application_open_uri_at_dest (application, (char *)l->data,
689                                                  screen, NULL, 0, NULL,
690                                                  FALSE, NULL, timestamp);
691         }
692 }
693
694 void
695 ev_application_shutdown (EvApplication *application)
696 {
697         if (application->accel_map_file) {
698                 gtk_accel_map_save (application->accel_map_file);
699                 g_free (application->accel_map_file);
700                 application->accel_map_file = NULL;
701         }
702         
703         if (application->toolbars_model) {
704                 g_object_unref (application->toolbars_model);
705                 g_free (application->toolbars_file);
706                 application->toolbars_model = NULL;
707                 application->toolbars_file = NULL;
708         }
709
710         ev_application_save_print_settings (application);
711         
712         if (application->print_settings_file) {
713                 g_key_file_free (application->print_settings_file);
714                 application->print_settings_file = NULL;
715         }
716
717         if (application->print_settings) {
718                 g_object_unref (application->print_settings);
719                 application->print_settings = NULL;
720         }
721
722         if (application->page_setup) {
723                 g_object_unref (application->page_setup);
724                 application->page_setup = NULL;
725         }
726
727 #ifdef ENABLE_DBUS
728         if (application->keys) {
729                 g_object_unref (application->keys);
730                 application->keys = NULL;
731         }
732 #endif /* ENABLE_DBUS */
733         
734         g_free (application->last_chooser_uri);
735         g_object_unref (application);
736         
737         gtk_main_quit ();
738 }
739
740 static void
741 ev_application_class_init (EvApplicationClass *ev_application_class)
742 {
743 }
744
745 static void
746 ev_application_init (EvApplication *ev_application)
747 {
748         gint i;
749         const gchar *home_dir;
750         
751         ev_application_init_session (ev_application);
752
753         home_dir = g_get_home_dir ();
754         if (home_dir) {
755                 ev_application->accel_map_file = g_build_filename (home_dir,
756                                                                    ".gnome2",
757                                                                    "accels"
758                                                                    "evince",
759                                                                    NULL);
760                 gtk_accel_map_load (ev_application->accel_map_file);
761         }
762         
763         ev_application->toolbars_model = egg_toolbars_model_new ();
764
765         ev_application->toolbars_file = g_build_filename
766                         (ev_dot_dir (), "evince_toolbar.xml", NULL);
767
768         egg_toolbars_model_load_names (ev_application->toolbars_model,
769                                        DATADIR "/evince-toolbar.xml");
770
771         if (!egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
772                                                ev_application->toolbars_file)) {
773                 egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
774                                                   DATADIR"/evince-toolbar.xml");
775         }
776
777         /* Open item doesn't exist anymore,
778          * convert it to OpenRecent for compatibility
779          */
780         for (i = 0; i < egg_toolbars_model_n_items (ev_application->toolbars_model, 0); i++) {
781                 const gchar *item;
782                 
783                 item = egg_toolbars_model_item_nth (ev_application->toolbars_model, 0, i);
784                 if (g_ascii_strcasecmp (item, "FileOpen") == 0) {
785                         egg_toolbars_model_remove_item (ev_application->toolbars_model, 0, i);
786                         egg_toolbars_model_add_item (ev_application->toolbars_model, 0, i,
787                                                      "FileOpenRecent");
788                         ev_application_save_toolbars_model (ev_application);
789                         break;
790                 }
791         }
792
793         egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
794                                       EGG_TB_MODEL_NOT_REMOVABLE);
795
796 #ifdef ENABLE_DBUS
797         ev_application->keys = ev_media_player_keys_new ();
798 #endif /* ENABLE_DBUS */
799 }
800
801 /**
802  * ev_application_get_windows:
803  * @application: The instance of the application.
804  *
805  * It creates a list of the top level windows.
806  *
807  * Returns: A #GList of the top level windows.
808  */
809 GList *
810 ev_application_get_windows (EvApplication *application)
811 {
812         GList *l, *toplevels;
813         GList *windows = NULL;
814
815         toplevels = gtk_window_list_toplevels ();
816
817         for (l = toplevels; l != NULL; l = l->next) {
818                 if (EV_IS_WINDOW (l->data)) {
819                         windows = g_list_append (windows, l->data);
820                 }
821         }
822
823         g_list_free (toplevels);
824
825         return windows;
826 }
827
828 /**
829  * ev_application_get_media_keys:
830  * @application: The instance of the application.
831  *
832  * It gives you access to the media player keys handler object.
833  *
834  * Returns: A #EvMediaPlayerKeys.
835  */
836 GObject *
837 ev_application_get_media_keys (EvApplication *application)
838 {
839 #ifdef ENABLE_DBUS
840         return G_OBJECT (application->keys);
841 #else
842         return NULL;
843 #endif /* ENABLE_DBUS */
844 }
845
846 EggToolbarsModel *
847 ev_application_get_toolbars_model (EvApplication *application)
848 {
849         return application->toolbars_model;
850 }
851
852 void
853 ev_application_save_toolbars_model (EvApplication *application)
854 {
855         egg_toolbars_model_save_toolbars (application->toolbars_model,
856                                           application->toolbars_file, "1.0");
857 }
858
859 void
860 ev_application_set_chooser_uri (EvApplication *application, const gchar *uri)
861 {
862         g_free (application->last_chooser_uri);
863         application->last_chooser_uri = g_strdup (uri);
864 }
865
866 const gchar *
867 ev_application_get_chooser_uri (EvApplication *application)
868 {
869         return application->last_chooser_uri;
870 }
871
872 void
873 ev_application_screensaver_enable (EvApplication *application)
874 {
875         if (application->scr_saver)
876                 totem_scrsaver_enable (application->scr_saver); 
877 }
878
879 void
880 ev_application_screensaver_disable (EvApplication *application)
881 {
882         if (application->scr_saver)
883                 totem_scrsaver_disable (application->scr_saver);        
884 }
885
886 static GKeyFile *
887 ev_application_get_print_settings_file (EvApplication *application)
888 {
889         gchar *filename;
890         
891         if (application->print_settings_file)
892                 return application->print_settings_file;
893
894         application->print_settings_file = g_key_file_new ();
895         
896         filename = g_build_filename (ev_dot_dir (), EV_PRINT_SETTINGS_FILE, NULL);
897         if (g_file_test (filename, G_FILE_TEST_IS_REGULAR)) {
898                 GError *error = NULL;
899
900                 g_key_file_load_from_file (application->print_settings_file,
901                                            filename,
902                                            G_KEY_FILE_KEEP_COMMENTS |
903                                            G_KEY_FILE_KEEP_TRANSLATIONS,
904                                            &error);
905                 if (error) {
906                         g_warning ("%s", error->message);
907                         g_error_free (error);
908                 }
909         }
910         g_free (filename);
911
912         return application->print_settings_file;
913 }
914
915 static void
916 ev_application_save_print_settings (EvApplication *application)
917 {
918         GKeyFile *key_file;
919         gchar    *filename;
920         gchar    *data;
921         gssize    data_length;
922         GError   *error = NULL;
923
924         if (!application->print_settings && !application->page_setup)
925                 return;
926         
927         key_file = ev_application_get_print_settings_file (application);
928         if (application->print_settings)
929                 gtk_print_settings_to_key_file (application->print_settings,
930                                                 key_file,
931                                                 EV_PRINT_SETTINGS_GROUP);
932         if (application->page_setup)
933                 gtk_page_setup_to_key_file (application->page_setup,
934                                             key_file,
935                                             EV_PAGE_SETUP_GROUP);
936         
937         filename = g_build_filename (ev_dot_dir (), EV_PRINT_SETTINGS_FILE, NULL);
938         data = g_key_file_to_data (key_file, (gsize *)&data_length, NULL);
939         g_file_set_contents (filename, data, data_length, &error);
940         if (error) {
941                 g_warning ("%s", error->message);
942                 g_error_free (error);
943         }
944         g_free (data);
945         g_free (filename);
946 }
947
948 GtkPrintSettings *
949 ev_application_get_print_settings (EvApplication *application)
950 {
951         GKeyFile         *key_file;
952         GtkPrintSettings *print_settings;
953         
954         if (application->print_settings)
955                 return application->print_settings;
956
957         key_file = ev_application_get_print_settings_file (application);
958         print_settings = g_key_file_has_group (key_file, EV_PRINT_SETTINGS_GROUP) ? 
959                 gtk_print_settings_new_from_key_file (key_file, EV_PRINT_SETTINGS_GROUP, NULL) :
960                 gtk_print_settings_new ();
961
962         application->print_settings = print_settings ? print_settings : gtk_print_settings_new ();
963
964         return application->print_settings;
965 }
966
967 void
968 ev_application_set_print_settings (EvApplication    *application,
969                                    GtkPrintSettings *settings)
970 {
971         GKeyFile *key_file;
972         
973         g_return_if_fail (GTK_IS_PRINT_SETTINGS (settings));
974         
975         if (settings == application->print_settings)
976                 return;
977
978         key_file = ev_application_get_print_settings_file (application);
979         
980         if (application->print_settings)
981                 g_object_unref (application->print_settings);
982         
983         application->print_settings = g_object_ref (settings);
984         gtk_print_settings_to_key_file (settings, key_file, EV_PRINT_SETTINGS_GROUP);
985 }
986
987 GtkPageSetup *
988 ev_application_get_page_setup (EvApplication *application)
989 {
990         GKeyFile     *key_file;
991         GtkPageSetup *page_setup;
992         
993         if (application->page_setup)
994                 return application->page_setup;
995
996         key_file = ev_application_get_print_settings_file (application);
997         page_setup = g_key_file_has_group (key_file, EV_PAGE_SETUP_GROUP) ? 
998                 gtk_page_setup_new_from_key_file (key_file, EV_PAGE_SETUP_GROUP, NULL) :
999                 gtk_page_setup_new ();
1000
1001         application->page_setup = page_setup ? page_setup : gtk_page_setup_new ();
1002
1003         return application->page_setup;
1004 }
1005
1006 void
1007 ev_application_set_page_setup (EvApplication *application,
1008                                GtkPageSetup  *page_setup)
1009 {
1010         GKeyFile *key_file;
1011         
1012         g_return_if_fail (GTK_IS_PAGE_SETUP (page_setup));
1013         
1014         if (page_setup == application->page_setup)
1015                 return;
1016
1017         key_file = ev_application_get_print_settings_file (application);
1018         
1019         if (application->page_setup)
1020                 g_object_unref (application->page_setup);
1021         
1022         application->page_setup = g_object_ref (page_setup);
1023         gtk_page_setup_to_key_file (page_setup, key_file, EV_PAGE_SETUP_GROUP);
1024 }