diff --git a/xed/xed-commands-file.c b/xed/xed-commands-file.c index a07a5868..90d1aca9 100644 --- a/xed/xed-commands-file.c +++ b/xed/xed-commands-file.c @@ -51,6 +51,7 @@ #define XED_IS_CLOSING_ALL "xed-is-closing-all" #define XED_IS_QUITTING "xed-is-quitting" #define XED_IS_QUITTING_ALL "xed-is-quitting-all" +#define XED_SESSION_FILE_NAME ".xed-session-save" static void tab_state_changed_while_saving (XedTab *tab, GParamSpec *pspec, @@ -1377,6 +1378,76 @@ _xed_cmd_file_revert (GtkAction *action, gtk_widget_show (dialog); } +GFileOutputStream * +xed_session_get_output_stream () +{ + gchar *file_name = NULL; + GFile *session_file = NULL; + GFileOutputStream *ostream = NULL; + + file_name = g_strconcat (g_get_home_dir(), "/", XED_SESSION_FILE_NAME, NULL); + session_file = g_file_new_for_path (file_name); + ostream = g_file_replace (session_file, NULL, 0, 0, NULL, NULL); + + g_free (file_name); + g_object_unref (session_file); + + return ostream; +} + +static void +xed_session_save_file_paths (GList *docs, GFileOutputStream* ostream) +{ + XedDocument *doc = NULL; + GtkSourceFile *source_file = NULL; + GFile *location = NULL; + gchar *file_path = NULL; + gchar *file_path_list = NULL; + gssize written, to_write; + + for (GList *l = docs; l != NULL; l = g_list_next (l)) + { + doc = XED_DOCUMENT (l->data); + source_file = xed_document_get_file (doc); + location = gtk_source_file_get_location (source_file); + + if (location != NULL) + { + file_path = g_file_get_path (location); + file_path_list = g_strconcat (file_path, "\n", file_path_list, NULL); + } + } + + to_write = sizeof (gchar) * strlen (file_path_list); + written = g_output_stream_write (G_OUTPUT_STREAM (ostream), file_path_list, to_write, NULL, NULL); + + g_output_stream_close (G_OUTPUT_STREAM (ostream), NULL, NULL); + g_object_unref (location); + g_free (file_path); + g_free (file_path_list); + g_return_if_fail (to_write == written); +} + +static void +xed_session_save_path_active_docs (XedWindow *window) +{ + GFileOutputStream* ostream = NULL; + GList *docs = NULL; + + g_return_if_fail (XED_IS_WINDOW (window)); + + ostream = xed_session_get_output_stream (); + docs = xed_window_get_documents (window); + + g_return_if_fail (ostream != NULL); + g_return_if_fail (docs != NULL); + + xed_session_save_file_paths (docs, ostream); + + g_object_unref (ostream); + g_list_free (docs); +} + static void tab_state_changed_while_saving (XedTab *tab, GParamSpec *pspec, @@ -1822,5 +1893,7 @@ _xed_cmd_file_quit (GtkAction *action, XED_WINDOW_STATE_PRINTING | XED_WINDOW_STATE_SAVING_SESSION))); + xed_session_save_path_active_docs (window); + file_close_all (window, TRUE); }