diff libgui/src/m-editor/file-editor-tab.cc @ 29438:23424aa7a11a stable

fix confirm overwrite for native editor file save as dialogs (bug #60214) * file-editor-tab.cc (save_file_as): disable confirming file overwrite by native file dialogs, since not reliable in case of default suffixes (handle_save_file_as_answer): add manual test for existence of target file
author Torsten Lilge <ttl-octave@mailbox.org>
date Sun, 14 Mar 2021 19:15:57 +0100
parents c24e190ae34f
children 4a516dce916c 1583728a5819
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor-tab.cc	Sun Mar 14 13:09:16 2021 +0100
+++ b/libgui/src/m-editor/file-editor-tab.cc	Sun Mar 14 19:15:57 2021 +0100
@@ -2349,12 +2349,23 @@
 
     fileDialog->setAcceptMode (QFileDialog::AcceptSave);
     fileDialog->setViewMode (QFileDialog::Detail);
+    fileDialog->setOption (QFileDialog::HideNameFilterDetails, false);
 
     // FIXME: Remove, if for all common KDE versions (bug #54607) is resolved.
     resource_manager& rmgr = m_octave_qobj.get_resource_manager ();
     gui_settings *settings = rmgr.get_settings ();
     if (! settings->value (global_use_native_dialogs).toBool ())
-      fileDialog->setOption(QFileDialog::DontUseNativeDialog);
+      {
+        // Qt file dialogs
+        fileDialog->setOption(QFileDialog::DontUseNativeDialog);
+      }
+    else
+      {
+        // Native file dialogs: Test for already existing files is done manually
+        // since native file dialogs might not consider the automatically
+        // appended default extension when checking if the file already exists
+        fileDialog->setOption(QFileDialog::DontConfirmOverwrite);
+      }
 
     connect (fileDialog, SIGNAL (filterSelected (const QString&)),
              this, SLOT (handle_save_as_filter_selected (const QString&)));
@@ -2489,7 +2500,7 @@
     QFileInfo file (saveFileName);
     QFileDialog *file_dialog = qobject_cast<QFileDialog *> (sender ());
 
-    // Test if there is the file dialog should have added a default file
+    // Test if the file dialog should have added a default file
     // suffix, but the selected file still has no suffix (see Qt bug
     // https://bugreports.qt.io/browse/QTBUG-59401)
     if ((! file_dialog->defaultSuffix ().isEmpty ()) && file.suffix ().isEmpty ())
@@ -2497,6 +2508,25 @@
         saveFileName = saveFileName + "." + file_dialog->defaultSuffix ();
       }
 
+    file.setFile (saveFileName);
+
+    // If overwrite confirmation was not done by the file dialog (in case
+    // of native file dialogs, see above), do it here
+    if(file_dialog->testOption (QFileDialog::DontConfirmOverwrite) && file.exists ())
+      {
+        int ans = QMessageBox::question (file_dialog,
+                              tr ("Octave Editor"),
+                              tr ("%1\n already exists\n"
+                                  "Do you want to overwrite it?").arg (saveFileName),
+                              QMessageBox::Yes | QMessageBox::No);
+        if (ans != QMessageBox::Yes)
+          {
+            // Try again, if edit area is read only, remove on success
+            save_file_as (m_edit_area->isReadOnly ());
+            return;
+          }
+      }
+
     if (m_save_as_desired_eol != m_edit_area->eolMode ())
       convert_eol (this,m_save_as_desired_eol);