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