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