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