+ 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
+
+static void
+smclient_save_state_cb (EggSMClient *client,
+ GKeyFile *state_file,
+ EvApplication *application)
+{
+ GList *windows;
+
+ windows = ev_application_get_windows (application);
+ if (windows) {
+ save_session (application, windows, state_file);
+ g_list_free (windows);
+ }
+}
+
+static void
+smclient_quit_cb (EggSMClient *client,
+ EvApplication *application)
+{
+ ev_application_shutdown (application);
+}
+
+#endif /* WITH_SMCLIENT */
+
+static void
+ev_application_init_session (EvApplication *application)
+{
+ application->crashed_file = g_build_filename (application->dot_dir,
+ "evince-crashed", NULL);
+
+#ifdef WITH_SMCLIENT
+ application->smclient = egg_sm_client_get ();
+ g_signal_connect (application->smclient, "save_state",
+ G_CALLBACK (smclient_save_state_cb),
+ application);
+ g_signal_connect (application->smclient, "quit",
+ G_CALLBACK (smclient_quit_cb),
+ application);
+#endif
+}
+
+/**
+ * ev_display_open_if_needed:
+ * @name: the name of the display to be open if it's needed.
+ *
+ * Search among all the open displays if any of them have the same name as the
+ * passed name. If the display isn't found it tries the open it.
+ *
+ * Returns: a #GdkDisplay of the display with the passed name.
+ */
+static GdkDisplay *
+ev_display_open_if_needed (const gchar *name)
+{
+ GSList *displays;
+ GSList *l;
+ GdkDisplay *display = NULL;
+
+ displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
+
+ for (l = displays; l != NULL; l = l->next) {
+ const gchar *display_name = gdk_display_get_name ((GdkDisplay *) l->data);
+
+ if (g_ascii_strcasecmp (display_name, name) == 0) {
+ display = l->data;
+ break;
+ }
+ }
+
+ g_slist_free (displays);
+
+ return display != NULL ? display : gdk_display_open (name);
+}
+
+/**
+ * get_screen_from_args:
+ * @args: a #GHashTable with data passed to the application.
+ *
+ * Looks for the screen in the display available in the hash table passed to the
+ * application. If the display isn't opened, it's opened and the #GdkScreen
+ * assigned to the screen in that display returned.
+ *
+ * Returns: the #GdkScreen assigned to the screen on the display indicated by
+ * the data on the #GHashTable.
+ */
+static GdkScreen *
+get_screen_from_args (GHashTable *args)
+{
+ GValue *value = NULL;
+ GdkDisplay *display = NULL;
+ GdkScreen *screen = NULL;
+
+ g_assert (args != NULL);
+
+ value = g_hash_table_lookup (args, "display");
+ if (value) {
+ const gchar *display_name;
+
+ display_name = g_value_get_string (value);
+ display = ev_display_open_if_needed (display_name);
+ }