changeset 20783:b6091735f3f5

warning when saving editor file with a codec not suited for current contents * file-editor-tab.cc (save_file) call the new function for checking whether contents can be encoded using the current codecForName (check_valid_codec): check with canEncode () whether codec is suitable or not * m-editor/file-editor-tab.h: new function for checking the codec
author Torsten <ttl@justmail.de>
date Tue, 01 Dec 2015 21:59:59 +0100
parents 05b86b044995
children 52f6921dde09
files libgui/src/m-editor/file-editor-tab.cc libgui/src/m-editor/file-editor-tab.h
diffstat 2 files changed, 35 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor-tab.cc	Mon Nov 30 22:10:52 2015 -0500
+++ b/libgui/src/m-editor/file-editor-tab.cc	Tue Dec 01 21:59:59 2015 +0100
@@ -1532,13 +1532,21 @@
     }
 
   // save the contents into the file
-  QTextStream out (&file);
+
+  _encoding = _new_encoding;    // consider a possible new encoding
+
+  // set the desired codec (if suitable for contents)
+  QTextCodec *codec = QTextCodec::codecForName (_encoding.toAscii ());
 
-  // consider a possible new encoding (from the save-file-as dialog)
-  _encoding = _new_encoding;
-  // set the desired codec
-  QTextCodec *codec = QTextCodec::codecForName (_encoding.toAscii ());
-  out.setCodec(codec);
+  if (check_valid_codec (codec))
+    {
+      save_file_as (remove_on_success);
+      return;
+    }
+
+  // write the file
+  QTextStream out (&file);
+  out.setCodec (codec);
 
   QApplication::setOverrideCursor (Qt::WaitCursor);
   out << _edit_area->text ();
@@ -1738,6 +1746,26 @@
   return false;
 }
 
+bool
+file_editor_tab::check_valid_codec (QTextCodec *codec)
+{
+  if (! codec->canEncode (_edit_area->text ()))
+    {
+      int ans = QMessageBox::warning (0,
+            tr ("Octave Editor"),
+            tr ("The current editor contents can not be encoded\n"
+                "with the selected codec %1.\n"
+                "Using it will result in data loss!\n\n"
+                "Do you want to chose another codec?").arg (_encoding),
+            QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
+
+      if (ans == QMessageBox::Yes)
+        return true;
+    }
+
+  return false;
+}
+
 void
 file_editor_tab::handle_save_file_as_answer (const QString& saveFileName)
 {
@@ -1746,7 +1774,6 @@
 
   if (saveFileName == _file_name)
     {
-      // same name as actual file, save it as "save" would do
       save_file (saveFileName);
     }
   else
--- a/libgui/src/m-editor/file-editor-tab.h	Mon Nov 30 22:10:52 2015 -0500
+++ b/libgui/src/m-editor/file-editor-tab.h	Tue Dec 01 21:59:59 2015 +0100
@@ -199,6 +199,7 @@
   void save_file (const QString& saveFileName, bool remove_on_success = false);
   void save_file_as (bool remove_on_success = false);
   bool check_valid_identifier (QString file_name);
+  bool check_valid_codec (QTextCodec *codec);
 
   void update_lexer ();
   void request_add_breakpoint (int line);