Index: src/Makefile.am =================================================================== RCS file: /cvs/gnome/gnumeric/src/Makefile.am,v retrieving revision 1.265 diff -u -p -r1.265 Makefile.am --- src/Makefile.am 2002/02/16 07:15:52 1.265 +++ src/Makefile.am 2002/02/22 23:22:13 @@ -35,6 +41,8 @@ GNUMERIC_BASE = \ auto-format.h \ auto-correct.c \ auto-correct.h \ + auto-save.c \ + auto-save.h \ cell.h \ cell.c \ cell-draw.c \ Index: src/gui-file.c =================================================================== RCS file: /cvs/gnome/gnumeric/src/gui-file.c,v retrieving revision 1.28 diff -u -p -r1.28 gui-file.c --- src/gui-file.c 2002/02/15 05:50:57 1.28 +++ src/gui-file.c 2002/02/22 23:22:14 @@ -125,7 +125,7 @@ gui_file_import (WorkbookControlGUI *wbc file_name = gtk_file_selection_get_filename (fsel); if (fo != NULL) { (void) wb_view_open_custom (wb_control_view (WORKBOOK_CONTROL (wbcg)), - WORKBOOK_CONTROL (wbcg), fo, file_name, TRUE); + WORKBOOK_CONTROL (wbcg), NULL, fo, file_name, TRUE); } gtk_object_destroy (GTK_OBJECT (fsel)); g_list_free (importers); Index: src/main.c =================================================================== RCS file: /cvs/gnome/gnumeric/src/main.c,v retrieving revision 1.157 diff -u -p -r1.157 main.c --- src/main.c 2002/02/15 05:51:07 1.157 +++ src/main.c 2002/02/22 23:22:14 @@ -34,6 +34,7 @@ #include "eval.h" #include "sheet-autofill.h" #include "xml-io.h" +#include "auto-save.h" #include #include @@ -284,6 +285,9 @@ main (int argc, char *argv []) } warn_about_ancient_gnumerics (gnumeric_binary, wbc); + + /* recover from a previously-crashed gnumeric */ + auto_save_check_recover(wb_control_view(wbc), wbc); gtk_main (); } Index: src/workbook-view.c =================================================================== RCS file: /cvs/gnome/gnumeric/src/workbook-view.c,v retrieving revision 1.82 diff -u -p -r1.82 workbook-view.c --- src/workbook-view.c 2002/02/15 05:51:28 1.82 +++ src/workbook-view.c 2002/02/22 23:22:14 @@ -590,13 +585,15 @@ gboolean wb_view_open (WorkbookView *wbv, WorkbookControl *wbc, gchar const *file_name, gboolean display_errors) { - return wb_view_open_custom (wbv, wbc, NULL, file_name, display_errors); + return wb_view_open_custom (wbv, wbc, NULL, NULL, file_name, display_errors); } /** * wb_view_open_custom: * @wbv : Workbook View * @wbc : Workbook Control + * @new_wbc : A place to store the WorkbookControl created by a + * successful load. May be NULL. * @fo : GnumFileOpener object * @file_name : File name * @@ -606,17 +603,24 @@ wb_view_open (WorkbookView *wbv, Workboo */ gboolean wb_view_open_custom (WorkbookView *wbv, WorkbookControl *wbc, + WorkbookControl **new_wbc, GnumFileOpener const *fo, gchar const *file_name, gboolean display_errors) { Workbook *new_wb = NULL; WorkbookView *new_wbv = NULL; + WorkbookControl *ignore; g_return_val_if_fail (IS_WORKBOOK_VIEW (wbv), FALSE); g_return_val_if_fail (IS_WORKBOOK_CONTROL (wbc), FALSE); g_return_val_if_fail (fo == NULL || IS_GNUM_FILE_OPENER (fo), FALSE); g_return_val_if_fail (file_name != NULL, FALSE); + if (new_wbc==NULL) + new_wbc = &ignore; /* we can assume new_wbc is non-null now */ + else + *new_wbc=NULL; /* default value in case of error */ + if (g_file_test (file_name, G_FILE_TEST_IS_REGULAR)) { IOContext *io_context = gnumeric_io_context_new (COMMAND_CONTEXT (wbc)); wb_control_menu_state_sensitivity (wbc, FALSE); @@ -684,8 +688,9 @@ wb_view_open_custom (WorkbookView *wbv, workbook_unref (old_wb); workbook_control_set_view (wbc, new_wbv, NULL); workbook_control_init_state (wbc); + *new_wbc = wbc; } else - (void) wb_control_wrapper_new (wbc, new_wbv, NULL); + *new_wbc = wb_control_wrapper_new (wbc, new_wbv, NULL); workbook_recalc (new_wb); Index: src/workbook-view.h =================================================================== RCS file: /cvs/gnome/gnumeric/src/workbook-view.h,v retrieving revision 1.29 diff -u -p -r1.29 workbook-view.h --- src/workbook-view.h 2002/02/15 05:51:29 1.29 +++ src/workbook-view.h 2002/02/22 23:22:14 @@ -82,6 +83,7 @@ gboolean wb_view_open (WorkbookVi char const *file_name, gboolean display_errors); gboolean wb_view_open_custom (WorkbookView *wbv, WorkbookControl *wbc, + WorkbookControl **new_wbc, GnumFileOpener const *fo, char const *file_name, gboolean display_errors); Index: src/workbook.c =================================================================== RCS file: /cvs/gnome/gnumeric/src/workbook.c,v retrieving revision 1.527 diff -u -p -r1.527 workbook.c --- src/workbook.c 2002/02/21 00:40:37 1.527 +++ src/workbook.c 2002/02/22 23:22:14 @@ -116,6 +116,9 @@ workbook_finalize (GObject *wb_object) wb->priv->during_destruction = TRUE; + /* remove autosave timer & free last_autosave_filename */ + auto_save_destroy(wb); + if (wb->file_saver != NULL) { g_object_weak_unref (G_OBJECT (wb->file_saver), (GWeakNotify) cb_saver_finalize, wb); @@ -224,6 +227,8 @@ workbook_set_dirty (Workbook *wb, gboole wb->summary_info->modified = is_dirty; g_hash_table_foreach (wb->sheet_hash_private, cb_sheet_mark_dirty, GINT_TO_POINTER (is_dirty)); + if (!is_dirty) + auto_save_reset(wb); } static void @@ -429,6 +434,12 @@ workbook_new (void) wb->priv->workbook_views = NULL; wb->priv->persist_file = NULL; #endif + + /* Start up autosave timer. */ + wb->last_autosave_filename = NULL; + wb->autosave_timer_id = 0; + auto_save_reset(wb); + return wb; } @@ -438,7 +449,7 @@ workbook_new (void) * @number: returns the number stripped off in *number * * Gets a name in the form of "Sheet (10)", "Stuff" or "Dummy ((((," - * and returns the real name of the sheet "Sheet","Stuff","Dymmy ((((," + * and returns the real name of the sheet "Sheet","Stuff","Dummy ((((," * without the copy number. **/ static void Index: src/workbook.h =================================================================== RCS file: /cvs/gnome/gnumeric/src/workbook.h,v retrieving revision 1.100 diff -u -p -r1.100 workbook.h --- src/workbook.h 2002/02/21 00:40:37 1.100 +++ src/workbook.h 2002/02/22 23:22:14 @@ -30,6 +30,10 @@ struct _Workbook { FileFormatLevel file_format_level; GnumFileSaver *file_saver; + /* Auto-save support */ + gchar *last_autosave_filename; + guint autosave_timer_id; /* zero if no timer */ + /* Undo support */ GSList *undo_commands; GSList *redo_commands; Index: src/xml-io.c =================================================================== RCS file: /cvs/gnome/gnumeric/src/xml-io.c,v retrieving revision 1.306 diff -u -p -r1.306 xml-io.c --- src/xml-io.c 2002/02/20 20:21:00 1.306 +++ src/xml-io.c 2002/02/22 23:22:14 @@ -3275,43 +3277,76 @@ gnumeric_xml_read_workbook (GnumFileOpen } /* - * Save a Workbook in an XML file + * Save a Workbook in an XML file or xmlOutputBuffer * One build an in-memory XML tree and save it to a file. */ -void -gnumeric_xml_write_workbook (GnumFileSaver const *fs, - IOContext *context, - WorkbookView *wb_view, - const gchar *filename) +static int +gnumeric_xml_write_workbook_shared (IOContext *context, + WorkbookView *wb_view, + const gchar *filename, + xmlOutputBufferPtr buf) { xmlDocPtr xml; XmlParseContext *ctxt; - char const *extension; + char const *extension = NULL; int compression; + int st; g_return_if_fail (wb_view != NULL); - g_return_if_fail (filename != NULL); + g_return_if_fail (filename != NULL || buf != NULL); xml = xmlNewDoc ((xmlChar *)"1.0"); if (xml == NULL) { - gnumeric_io_error_save (context, ""); - return; + if (context!=NULL) + gnumeric_io_error_save (context, ""); + return -1; } ctxt = xml_parse_ctx_new (xml, NULL); xml->xmlRootNode = xml_workbook_write (ctxt, wb_view); xml_parse_ctx_destroy (ctxt); /* If the suffix is .xml disable compression */ - extension = g_extension_pointer (filename); + if (filename!=NULL) + extension = g_extension_pointer (filename); compression = (extension != NULL && g_strcasecmp (extension, "xml") == 0) ? 0 : -1; gnumeric_xml_set_compression (xml, compression); - if (xmlSaveFile (filename, xml) < 0) + if (filename != NULL) + st = xmlSaveFile (filename, xml); + else + st = xmlSaveFileTo(buf, xml, NULL); + if (st < 0) gnumeric_io_error_save (context, g_strerror (errno)); xmlFreeDoc (xml); +} + +/* + * Save a Workbook in an XML file. + */ +void +gnumeric_xml_write_workbook (GnumFileSaver const *fs, + IOContext *context, + WorkbookView *wb_view, + const gchar *filename) { + gnumeric_xml_write_workbook_shared(context, wb_view, filename, NULL); +} +/* + * Save a Workbook to a file descriptor. + */ +void +gnumeric_xml_write_workbook_to_fd (GnumFileSaver const *fs, + IOContext *context, + WorkbookView *wb_view, + int fd) { + xmlOutputBufferPtr buf; + /* note this will be uncompressed, due to limitations in + * libxml2 */ + buf = xmlOutputBufferCreateFd(fd, NULL); + gnumeric_xml_write_workbook_shared(context, wb_view, NULL, buf); + /* the xmlSaveFileTo function will close (and free) buf */ } void Index: src/xml-io.h =================================================================== RCS file: /cvs/gnome/gnumeric/src/xml-io.h,v retrieving revision 1.44 diff -u -p -r1.44 xml-io.h --- src/xml-io.h 2002/02/15 05:51:29 1.44 +++ src/xml-io.h 2002/02/22 23:22:14 @@ -48,6 +48,10 @@ void gnumeric_xml_read_workbook (GnumFil WorkbookView *wbv, gchar const *filename); void gnumeric_xml_write_workbook (GnumFileSaver const *fs, IOContext *context, WorkbookView *wbv, gchar const *filename); +void gnumeric_xml_write_workbook_to_fd(GnumFileSaver const *fs, + IOContext *context, + WorkbookView *wbv, + int fd); #ifdef ENABLE_BONOBO void gnumeric_xml_write_workbook_to_stream (GnumFileSaver const *fs, IOContext *context,