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