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