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