+/* Session */
+static void
+save_session (EvApplication *application,
+ GList *windows_list,
+ GKeyFile *state_file)
+{
+ GList *l;
+ gint i;
+ const gchar **uri_list;
+ const gchar *empty = "empty-window";
+
+ uri_list = g_new (const gchar *, g_list_length (windows_list));
+ for (l = windows_list, i = 0; l != NULL; l = g_list_next (l), i++) {
+ EvWindow *window = EV_WINDOW (l->data);
+
+ if (ev_window_is_empty (window))
+ uri_list[i] = empty;
+ else
+ uri_list[i] = ev_window_get_uri (window);
+ }
+ g_key_file_set_string_list (state_file,
+ "Evince",
+ "documents",
+ (const char **)uri_list,
+ i);
+ g_free (uri_list);
+}
+
+static void
+ev_application_save_session_crashed (EvApplication *application)
+{
+ GList *windows;
+
+ windows = ev_application_get_windows (application);
+ if (windows) {
+ GKeyFile *crashed_file;
+ gchar *data;
+ gssize data_length;
+ GError *error = NULL;
+
+ crashed_file = g_key_file_new ();
+ save_session (application, windows, crashed_file);
+
+ data = g_key_file_to_data (crashed_file, (gsize *)&data_length, NULL);
+ g_file_set_contents (application->crashed_file, data, data_length, &error);
+ if (error) {
+ g_warning ("%s", error->message);
+ g_error_free (error);
+ }
+ g_free (data);
+ g_key_file_free (crashed_file);
+ } else if (g_file_test (application->crashed_file, G_FILE_TEST_IS_REGULAR)) {
+ GFile *file;
+
+ file = g_file_new_for_path (application->crashed_file);
+ g_file_delete (file, NULL, NULL);
+ g_object_unref (file);
+ }
+}
+
+static gboolean
+save_session_crashed_in_idle_cb (EvApplication *application)
+{
+ ev_application_save_session_crashed (application);
+ application->crashed_idle = 0;
+
+ return FALSE;
+}
+
+static void
+save_session_crashed_in_idle (EvApplication *application)
+{
+ if (application->crashed_idle > 0)
+ g_source_remove (application->crashed_idle);
+ application->crashed_idle =
+ g_idle_add ((GSourceFunc)save_session_crashed_in_idle_cb,
+ application);
+}
+
+static gboolean
+ev_application_run_crash_recovery_dialog (EvApplication *application)
+{
+ GtkWidget *dialog;
+ gint response;
+
+ dialog = gtk_message_dialog_new (NULL,
+ GTK_DIALOG_MODAL,
+ GTK_MESSAGE_WARNING,
+ GTK_BUTTONS_NONE,
+ _("Recover previous documents?"));
+ gtk_message_dialog_format_secondary_text (
+ GTK_MESSAGE_DIALOG (dialog),
+ _("Evince appears to have exited unexpectedly the last time "
+ "it was run. You can recover the opened documents."));
+
+ gtk_dialog_add_button (GTK_DIALOG (dialog),
+ _("_Don't Recover"),
+ GTK_RESPONSE_CANCEL);
+ gtk_dialog_add_button (GTK_DIALOG (dialog),
+ _("_Recover"),
+ GTK_RESPONSE_ACCEPT);
+
+ gtk_window_set_title (GTK_WINDOW (dialog), _("Crash Recovery"));
+ gtk_window_set_icon_name (GTK_WINDOW (dialog), "evince");
+ gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
+ gtk_window_set_skip_taskbar_hint (GTK_WINDOW (dialog), FALSE);
+ gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
+
+ response = gtk_dialog_run (GTK_DIALOG (dialog));
+ gtk_widget_destroy (dialog);
+
+ return response == GTK_RESPONSE_ACCEPT;
+}
+
+gboolean
+ev_application_load_session (EvApplication *application)
+{
+ GKeyFile *state_file;
+ gchar **uri_list;
+
+#ifdef WITH_SMCLIENT
+ if (egg_sm_client_is_resumed (application->smclient)) {
+ state_file = egg_sm_client_get_state_file (application->smclient);
+ if (!state_file)
+ return FALSE;
+ } else
+#endif /* WITH_SMCLIENT */
+ if (g_file_test (application->crashed_file, G_FILE_TEST_IS_REGULAR)) {
+ if (ev_application_run_crash_recovery_dialog (application)) {
+ state_file = g_key_file_new ();
+ g_key_file_load_from_file (state_file,
+ application->crashed_file,
+ G_KEY_FILE_NONE,
+ NULL);
+ } else {
+ return FALSE;
+ }
+ } else {
+ return FALSE;
+ }
+
+ uri_list = g_key_file_get_string_list (state_file,
+ "Evince",
+ "documents",
+ NULL, NULL);
+ if (uri_list) {
+ gint i;
+
+ for (i = 0; uri_list[i]; i++) {
+ if (g_ascii_strcasecmp (uri_list[i], "empty-window") == 0)
+ ev_application_open_window (application, NULL, GDK_CURRENT_TIME, NULL);
+ else
+ ev_application_open_uri (application, uri_list[i], NULL, GDK_CURRENT_TIME, NULL);
+ }
+ g_strfreev (uri_list);
+ }
+ g_key_file_free (state_file);
+
+ return TRUE;
+}
+
+#ifdef WITH_SMCLIENT
+