[glom] Document: Clarify that set_file_uri() does not trigger a save.



commit 966e23293fd9a43e739605b1d59660832cfa6cad
Author: Murray Cumming <murrayc murrayc com>
Date:   Thu Dec 8 09:16:12 2011 +0100

    Document: Clarify that set_file_uri() does not trigger a save.
    
    * glom/libglom/document/document.[h|cc]: set_file_uri(): The check for
    a changed URI never actually worked here, so set_modified() was never
    called. That behaviour is actually correct, to avoid saving an empty file
    over a real file before calling load(), so document that behaviour.
    * glom/libglom/document/bakery/document_xml.cc: Remove the check for
    modified so that an explicit save() always saves the document, even if
    all values are the default. This does not seem to have prevented any
    unnecessary saves anyway, because we already check for changes before
    calling set_modified(), so we only trigger autosave when really necessary.

 ChangeLog                                    |   14 ++++++++++++++
 glom/libglom/document/bakery/document_xml.cc |   15 +++++++--------
 glom/libglom/document/document.cc            |   19 ++++++++++++-------
 glom/libglom/document/document.h             |    6 ++++++
 4 files changed, 39 insertions(+), 15 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 2f0189d..a3da1c6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2011-12-08  Murray Cumming  <murrayc murrayc com>
+
+	Document: Clarify that set_file_uri() does not trigger a save.
+
+	* glom/libglom/document/document.[h|cc]: set_file_uri(): The check for 
+	a changed URI never actually worked here, so set_modified() was never 
+	called. That behaviour is actually correct, to avoid saving an empty file
+	over a real file before calling load(), so document that behaviour.
+	* glom/libglom/document/bakery/document_xml.cc: Remove the check for
+	modified so that an explicit save() always saves the document, even if 
+	all values are the default. This does not seem to have prevented any
+	unnecessary saves anyway, because we already check for changes before
+	calling set_modified(), so we only trigger autosave when really necessary.
+
 2011-12-06  Murray Cumming  <murrayc murrayc com>
 
 	Test for autosaves without URI and for explicit saves while using autosave.
diff --git a/glom/libglom/document/bakery/document_xml.cc b/glom/libglom/document/bakery/document_xml.cc
index ffa9378..857d455 100644
--- a/glom/libglom/document/bakery/document_xml.cc
+++ b/glom/libglom/document/bakery/document_xml.cc
@@ -78,15 +78,14 @@ bool Document_XML::save_before()
     m_strContents.erase();
 
     Util_DOM_Write(m_strContents);
-
-    //Save the XML string:
-    return type_base::save_before();
-  }
-  else
-  {
-    std::cout << G_STRFUNC << ": not saving, because not modified" << std::endl;
-    return true; //Success. (At doing nothing, because nothing needed to be done.)
   }
+
+  //Write the file even if nothing was modified,
+  //to make sure that we write empty documents that have only default values,
+  //when save() is explicitly called.
+  //
+  //Save the XML string:
+  return type_base::save_before();
 }
 
 Glib::ustring Document_XML::get_xml() const
diff --git a/glom/libglom/document/document.cc b/glom/libglom/document/document.cc
index 354ae80..9079e70 100644
--- a/glom/libglom/document/document.cc
+++ b/glom/libglom/document/document.cc
@@ -4417,21 +4417,26 @@ void Document::fill_translatable_layout_items(const sharedptr<LayoutGroup>& grou
 
 void Document::set_file_uri(const Glib::ustring& file_uri, bool bEnforceFileExtension /* = false */)
 {
+  Glib::ustring file_uri_used = file_uri;
+
+  //Enforce file extension:
+  if(bEnforceFileExtension)
+    file_uri_used = get_file_uri_with_extension(file_uri);
+
+  //const bool changed = (file_uri_used != m_file_uri);
   //We override this because set_modified() triggers a save (to the old filename) in this derived class.
 
   //I'm not sure why this is in the base class's method anyway. murrayc:
   //if(file_uri != m_file_uri)
   //  set_modified(); //Ready to save() for a Save As.
 
-  m_file_uri = file_uri;
-
-  //Enforce file extension:
-  if(bEnforceFileExtension)
-    m_file_uri = get_file_uri_with_extension(m_file_uri);
+  m_file_uri = file_uri_used;
 
   //Put this here instead. In the base class it's at the start:
-  if(file_uri != m_file_uri)
-    set_modified(); //Ready to save() for a Save As.
+  //Actually, we do not call this, because it would trigger a save, when using autosave,
+  //and that could cause an empty document to be saved to a URL before calling load() to load the real document that was there.
+  //if(changed)
+  //  set_modified(); //Ready to save() for a Save As.
 }
 
 guint Document::get_document_format_version()
diff --git a/glom/libglom/document/document.h b/glom/libglom/document/document.h
index 79e2d1a..3ed7d25 100644
--- a/glom/libglom/document/document.h
+++ b/glom/libglom/document/document.h
@@ -58,6 +58,12 @@ public:
 
   virtual void set_modified(bool value = true);
 
+  /** Set the file URI that will be used in future calls to load() and save().
+   * Note that the document will not be saved immediately to the new URI. It will
+   * be saved either after the next change (if using autosave) or when calling save() explicitly.
+   * Likewise, the document at the URI will not be loaded until load() is called explicitly.
+   * That is unlike in the base class's implementation.
+   */
   virtual void set_file_uri(const Glib::ustring& file_uri, bool bEnforceFileExtension = false);
 
   /* Loads data from disk, using the URI (set with set_file_uri()) then asks the View to update itself.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]