[glom] Say when a chosen file doen't exist, and remove it from the history.



commit 1b4d86f8a8bfc62d0c06e0121dc06bb48e68a620
Author: Murray Cumming <murrayc murrayc com>
Date:   Fri Mar 26 17:16:02 2010 +0100

    Say when a chosen file doen't exist, and remove it from the history.
    
    * glom/libglom/document/bakery/document.[h|cc]: Add an enum for
        standard failure_codes, including a last enum values that custom codes
        should start after.
        read_from_disk(): Set a failure_code output variable here too, so we
        can detect non-existant files.
    * glom/libglom/document/document.h: Start the custom code after the last
        standard one.
    * glom/bakery/app_withdoc.cc: open_document(): Remove non-existant
        files from the recent files history.
    * glom/application.cc: ui_warning_load_failed(): Say if the file could not
        be found, instead of being mysterious. This happens for me often for
        recent files that I have deleted.

 ChangeLog                                |   47 ++++++++++++++++++++---------
 glom/application.cc                      |   14 +++++++-
 glom/bakery/app_withdoc.cc               |   41 ++++++++++++++------------
 glom/libglom/document/bakery/document.cc |   27 ++++++++++++-----
 glom/libglom/document/bakery/document.h  |   16 +++++++--
 glom/libglom/document/document.h         |    3 +-
 6 files changed, 98 insertions(+), 50 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 939567f..7eaac2a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2010-03-26  Murray Cumming  <murrayc murraycs com>
+
+	Say when a chosen file doen't exist, and remove it from the history.
+
+	* glom/libglom/document/bakery/document.[h|cc]: Add an enum for
+    standard failure_codes, including a last enum values that custom codes
+    should start after.
+    read_from_disk(): Set a failure_code output variable here too, so we
+    can detect non-existant files.
+	* glom/libglom/document/document.h: Start the custom code after the last
+    standard one.
+	* glom/bakery/app_withdoc.cc: open_document(): Remove non-existant
+    files from the recent files history.
+	* glom/application.cc: ui_warning_load_failed(): Say if the file could not
+    be found, instead of being mysterious. This happens for me often for
+    recent files that I have deleted.
+
 2010-03-26  Daniel Borgmann  <danielb openismus com>
 
 	Adjust file chooser dialogs.
@@ -58,9 +75,9 @@
 
     Glade files: Remove adjustment objects, simplifying window instantiation.
 
-	* glom/glom_developer.glade: Remove useless (default, empty) adjustment 
-    objects, so we can load the windows with GtkBuilder without specifying 
-    them. Recent versions of Glade no longer seem to create these when they 
+	* glom/glom_developer.glade: Remove useless (default, empty) adjustment
+    objects, so we can load the windows with GtkBuilder without specifying
+    them. Recent versions of Glade no longer seem to create these when they
     would just have default values.
 	* glom/base_db.cc:
 	* glom/frame_glom.cc:
@@ -88,31 +105,31 @@
     Refactored combo classes.
 
 	* glom/utility_widgets/comboglomchoicesbase.[h|cc]:
-    Split into combochoices.[h|cc] and combochoiceswithtreemodel.[h|cc] so 
+    Split into combochoices.[h|cc] and combochoiceswithtreemodel.[h|cc] so
     that combo_as_radio_buttons.[h|cc] doesn't need to have an unused treemodel.
 
 2010-03-23  Murray Cumming  <murrayc murrayc com>
 
     Formatting: Choices: Add a Display as radio buttons option.
 
-	* glom/glom_developer.glade: formatting: choices: Add a Show As Radio 
-    Buttons checkbox. 
+	* glom/glom_developer.glade: formatting: choices: Add a Show As Radio
+    Buttons checkbox.
 	* glom/libglom/data_structure/layout/fieldformatting.[h|cc]:
     set/get_choices_restricted(): Add a show_as_radio_buttons output parameter.
-	* glom/libglom/document/document.cc: 
+	* glom/libglom/document/document.cc:
     load_after_layout_item_formatting(), save_after_layout_item_formatting():
     load and save the new formatting detail.
 	* glom/mode_design/layout/layout_item_dialogs/box_formatting.[h|cc]:
-    Handle the new checkbox and make sure that it is only sensitive when 
-    the choices are restricted, because radio buttons provide no way to enter 
+    Handle the new checkbox and make sure that it is only sensitive when
+    the choices are restricted, because radio buttons provide no way to enter
     freeform data.
-	* glom/utility_widgets/comboglomchoicesbase.h: Make set_choices() and 
+	* glom/utility_widgets/comboglomchoicesbase.h: Make set_choices() and
     set_choices_with_second() virtual.
-    * glom/utility_widgets/combo_as_radio_buttons.[h|cc]: A new widget that is 
+    * glom/utility_widgets/combo_as_radio_buttons.[h|cc]: A new widget that is
     a vbbox of radio buttons, with a combo-like API.
 	* Makefile_glom.am: Mention the new files.
-	* glom/utility_widgets/datawidget.cc: Refactor some repeated code into 
-    create_combo_widget_for_field() and create the radiobuttons widget when 
+	* glom/utility_widgets/datawidget.cc: Refactor some repeated code into
+    create_combo_widget_for_field() and create the radiobuttons widget when
     necessary.
 
 2010-03-22  Murray Cumming  <murrayc murrayc com>
@@ -128,8 +145,8 @@
 2010-03-21  Murray Cumming  <murrayc murrayc com>
 
     Small fixes to glade file.
-  
-	* glom/glom_developer.glade: Add some still-missing vbox orientation=vertical 
+
+	* glom/glom_developer.glade: Add some still-missing vbox orientation=vertical
 	lines.
 
 2010-03-19  Daniel Borgmann <danielb openismus com>
diff --git a/glom/application.cc b/glom/application.cc
index 1ca7879..74ce505 100644
--- a/glom/application.cc
+++ b/glom/application.cc
@@ -660,9 +660,19 @@ static bool hostname_is_localhost(const Glib::ustring& hostname)
 
 void Application::ui_warning_load_failed(int failure_code)
 {
-  if(failure_code == Document::LOAD_FAILURE_CODE_FILE_VERSION_TOO_NEW)
+  if(failure_code == Document::LOAD_FAILURE_CODE_NOT_FOUND)
   {
-    ui_warning(_("Open Failed."),
+    //TODO: Put this in the generic bakery code.
+    ui_warning(_("Open Failed"),
+      _("The document could not be found."));
+
+    //TODO: Glom::Bakery::App_WithDoc removes the file from the recent history,
+    //but the initial/welcome dialog doesn't yet update its list when the
+    //recent history changes.
+  }
+  else if(failure_code == Document::LOAD_FAILURE_CODE_FILE_VERSION_TOO_NEW)
+  {
+    ui_warning(_("Open Failed"),
       _("The document could not be opened because it was created or modified by a newer version of Glom."));
   }
   else
diff --git a/glom/bakery/app_withdoc.cc b/glom/bakery/app_withdoc.cc
index 13caa3f..6c3fbd2 100644
--- a/glom/bakery/app_withdoc.cc
+++ b/glom/bakery/app_withdoc.cc
@@ -33,7 +33,7 @@ App_WithDoc::App_WithDoc(const Glib::ustring& appname)
 : App(appname),
   m_pDocument(0),
   m_bCloseAfterSave(false)
-{  
+{
 }
 
 App_WithDoc::~App_WithDoc()
@@ -64,7 +64,7 @@ void App_WithDoc::on_menu_file_close()
 
   if(!get_operation_cancelled())
     ui_hide();
-    
+
   on_document_close();
 }
 
@@ -105,7 +105,7 @@ bool App_WithDoc::open_document(const Glib::ustring& file_uri)
 {
   {
     //Open it:
-        
+
     //Load it into a new instance unless the current document is just a default new.
     if(!(get_document()->get_is_new())) //if it's not new.
     {
@@ -113,7 +113,7 @@ bool App_WithDoc::open_document(const Glib::ustring& file_uri)
       new_instance(file_uri);
       return true;
     }
-    
+
     App_WithDoc* pApp = this; //Replace the default new document in this instance.
 
     //Open it.
@@ -140,10 +140,10 @@ bool App_WithDoc::open_document(const Glib::ustring& file_uri)
         set_document_modified(false); //disables menu and toolbar Save items.
 
         //Update document history list:
-        //(Getting the file URI again, in case it has changed while being opened, 
+        //(Getting the file URI again, in case it has changed while being opened,
         //for instance if it was a template file that was saved as a real file.)
         if(pApp->m_pDocument)
-          document_history_add(pApp->m_pDocument->get_file_uri());
+          document_history_add(file_uri);
 
         return true; //success.
       }
@@ -154,6 +154,10 @@ bool App_WithDoc::open_document(const Glib::ustring& file_uri)
       if (bShowError)
         ui_warning_load_failed(failure_code);
 
+      //Make sure that non-existant files are removed from the history list:
+      if(failure_code == Document::LOAD_FAILURE_CODE_NOT_FOUND)
+        document_history_remove(file_uri);
+
       //re-initialize document.
       delete pApp->m_pDocument;
       pApp->m_pDocument = 0;
@@ -161,7 +165,7 @@ bool App_WithDoc::open_document(const Glib::ustring& file_uri)
 
       return false; //failed.
     }
-      
+
   } //if already open.
 
   return false; //failed.
@@ -216,7 +220,7 @@ void App_WithDoc::on_menu_file_saveas()
   //This doesn't work: TODO.
   ui_bring_to_front();
 
-  //Show the save dialog:  
+  //Show the save dialog:
   const Glib::ustring& file_uriOld = m_pDocument->get_file_uri();
 
   Glib::ustring file_uri = ui_file_select_save(file_uriOld); //Also asks for overwrite confirmation.
@@ -306,7 +310,7 @@ void App_WithDoc::on_menu_file_save()
 }
 
 void App_WithDoc::init()
-{  
+{
   init_create_document();
 
   //Call base method:
@@ -327,7 +331,7 @@ void App_WithDoc::init_create_document()
   }
 
   m_pDocument->set_is_new(true); //Can't be modified if it's just been created.
-  
+
   m_pDocument->signal_modified().connect(sigc::mem_fun(*this, &App_WithDoc::on_document_modified));
 
   update_window_title();
@@ -350,7 +354,7 @@ void App_WithDoc::offer_to_save_changes()
     if(m_pDocument->get_modified())
     {
       set_operation_cancelled(false); //Initialize it again. It might be set later in this method by cancel_close_or_exit().
-      
+
       //The document has unsaved changes,
       //so ask the user whether the document should be saved:
       enumSaveChanges buttonClicked = ui_offer_to_save_changes();
@@ -363,7 +367,7 @@ void App_WithDoc::offer_to_save_changes()
           on_menu_file_save(); //If File|Exit is in progress, this could cancel it.
           break;
         }
-        
+
         case(SAVECHANGES_Discard):
         {
           //Close if this save offer was a result of a FileClose (It probably always is):
@@ -371,18 +375,18 @@ void App_WithDoc::offer_to_save_changes()
           //Do nothing - the caller will probably hide() the window to have it deleted.
           break;
         }
-        
+
         case(SAVECHANGES_Cancel): //Cancel.
         {
           cancel_close_or_exit();
           break;
         }
-        
+
         default:
         {
           break;
         }
-      }   
+      }
     }
   }
 }
@@ -416,7 +420,7 @@ bool App_WithDoc::on_document_load()
   }
   else
     return false; //I can't think of any reason why this would happen.
-  
+
   //If you are not using Views, then override this to fill your various windows with stuff according to the contents of the document.
 }
 
@@ -473,9 +477,9 @@ void App_WithDoc::on_menu_edit_clear()
 void App_WithDoc::after_successful_save()
 {
   set_document_modified(false); //enables/disables menu and toolbar widgets.
-    
+
   //Update document history list:
-  document_history_add(m_pDocument->get_file_uri());     
+  document_history_add(m_pDocument->get_file_uri());
 }
 
 Glib::ustring App_WithDoc::get_conf_fullkey(const Glib::ustring& key)
@@ -500,4 +504,3 @@ void App_WithDoc::ui_warning_load_failed(int)
 }
 
 } //namespace
-
diff --git a/glom/libglom/document/bakery/document.cc b/glom/libglom/document/bakery/document.cc
index 1e5b7d0..738529a 100644
--- a/glom/libglom/document/bakery/document.cc
+++ b/glom/libglom/document/bakery/document.cc
@@ -120,9 +120,9 @@ bool Document::get_modified() const
 bool Document::load(int& failure_code)
 {
   //Initialize the output parameter:
-  failure_code = 0;
+  failure_code = LOAD_FAILURE_CODE_NONE;
 
-  bool bTest = read_from_disk();
+  bool bTest = read_from_disk(failure_code);
   if(bTest)
   {
     bTest = load_after(failure_code); //may be overridden.
@@ -147,7 +147,7 @@ bool Document::load_from_data(const guchar* data, std::size_t length, int& failu
   failure_code = 0;
 
   m_strContents = Glib::ustring((char*)data, length);
- 
+
   const bool bTest = load_after(failure_code); //may be overridden.
   if(bTest)
   {
@@ -196,12 +196,15 @@ bool Document::save_before()
   return true;
 }
 
-bool Document::read_from_disk()
+bool Document::read_from_disk(int& failure_code)
 {
+  failure_code = LOAD_FAILURE_CODE_NONE;
+
   m_strContents.erase();
 
   // Open the input file for read access:
   Glib::RefPtr<Gio::File> file = Gio::File::create_for_uri(m_file_uri);
+
   Glib::RefPtr<Gio::FileInputStream> stream;
 
 #ifdef GLIBMM_EXCEPTIONS_ENABLED
@@ -211,6 +214,12 @@ bool Document::read_from_disk()
   }
   catch(const Gio::Error& ex)
   {
+    std::cout << "Debug: Document::read_from_disk(): Error: " << ex.what() << std::endl;
+
+    if(ex.code() == Gio::Error::NOT_FOUND)
+      failure_code = LOAD_FAILURE_CODE_NOT_FOUND;
+    //  std::cout << "  File not found: " << m_file_uri << std::endl;
+
     // If the operation was not successful, print the error and abort
     return false; //print_error(ex, input_uri_string);
   }
@@ -302,6 +311,8 @@ bool Document::write_to_disk()
       std::cout << "Error: " << error->what() << std::endl;
 #endif
      // If the operation was not successful, print the error and abort
+     std::cout << "Error: " << ex.what() << std::endl;
+
      return false; // print_error(ex, output_uri_string);
     }
 
@@ -316,7 +327,7 @@ bool Document::write_to_disk()
       //Write the data to the output uri
       stream->write(m_strContents.data(), m_strContents.bytes());
 
-      //Close the stream to make sure that the write really happens 
+      //Close the stream to make sure that the write really happens
       //even with glibmm 2.16.0 which had a refcount leak that stopped it.
       stream->close();
       stream.reset();
@@ -349,7 +360,7 @@ Glib::ustring Document::get_name() const
 static Glib::ustring get_file_display_name(const Glib::ustring& uri)
 {
   Glib::ustring result;
-  
+
   Glib::RefPtr<Gio::File> file = Gio::File::create_for_uri(uri);
   Glib::RefPtr<const Gio::FileInfo> file_info;
 
@@ -372,8 +383,8 @@ static Glib::ustring get_file_display_name(const Glib::ustring& uri)
 
   if(!file_info)
     return result;
- 
-  return file_info->get_display_name();   
+
+  return file_info->get_display_name();
 }
 
 Glib::ustring Document::util_file_uri_get_name(const Glib::ustring& file_uri, const Glib::ustring& file_extension)
diff --git a/glom/libglom/document/bakery/document.h b/glom/libglom/document/bakery/document.h
index 61dbf11..0f185de 100644
--- a/glom/libglom/document/bakery/document.h
+++ b/glom/libglom/document/bakery/document.h
@@ -32,7 +32,7 @@ namespace GlomBakery
  */
 class Document
 {
-public: 
+public:
   Document();
   virtual ~Document();
 
@@ -45,9 +45,17 @@ public:
    */
   bool save();
 
+  enum LoadFailureCodes
+  {
+    LOAD_FAILURE_CODE_NONE = 0,
+    LOAD_FAILURE_CODE_NOT_FOUND = 1,
+    LOAD_FAILURE_CODE_LAST = 20 //arbitrary large number. Anything after this is for the application's custom codes.
+  };
+
   /* Loads data from disk, using the URI (set with set_file_uri()) then asks the View to update itself.
    * bool indicates success.
-   * @param failure_code Used to return a custom error code that is understood by your application. This must be greater than zero.
+   * @param failure_code Used to return an error code that is understood by your application.
+   * Custom error codes should be greater than LOAD_FAILURE_CODE_LAST.
    */
   bool load(int& failure_code);
 
@@ -120,7 +128,7 @@ protected:
    */
   virtual bool save_before();
 
-  bool read_from_disk();
+  bool read_from_disk(int& failure_code);
   bool write_to_disk();
 
   Glib::ustring m_strContents;
@@ -131,7 +139,7 @@ protected:
 
   type_signal_modified signal_modified_;
   type_signal_forget signal_forget_;
-  
+
   bool m_bModified;
   bool m_bIsNew; //see get_is_new().
   bool m_bReadOnly;
diff --git a/glom/libglom/document/document.h b/glom/libglom/document/document.h
index b78a9fd..a9d3bf1 100644
--- a/glom/libglom/document/document.h
+++ b/glom/libglom/document/document.h
@@ -448,8 +448,7 @@ public:
   /// Failure codes that could be returned by load_after()
   enum load_failure_codes
   {
-    // The error codes must start at 1, so do that here:
-    LOAD_FAILURE_CODE_FILE_VERSION_TOO_NEW = 1
+    LOAD_FAILURE_CODE_FILE_VERSION_TOO_NEW = LOAD_FAILURE_CODE_LAST + 1;
   };
 
 private:



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