changeset 23920:0b971884080c

reload editor files when removing was not successful (bug #43922) * files-dock-widget.cc (contextmenu_rename): emit the signal for reloading the files from their new location with the state of the renaming allowing to reload the files from the old locations, (contextmenu_delete): emit the signal for reloading files if the removing was not successful allowing to reload the not removed files, * files-dock-widget.h: signal file_renamed_signal with boolean parameter * file-editor.cc (handle_file_remove): clear list of temporary closed files right at the beginning of this method, if an editor file is going to be removed or renamed, store the old file name at first in the file list, then the new file name (or an empty string) and finally the encoding; (handle_dir_remove): clear the closed file list at the beginning, store name of old file, new file (or empty string) and encoding into the list is a file is affected by renaming or removing the directory; (handle_file_renamed): new boolean parameter, which decides whether the old or the new file is loaded into the editor * file-editor.h: boolean parameter for handle_file_renamed; list of temporarily closed files contains old name, new name, and encoding * main-window.cc (construct): update signal connection due to new boolean parameter
author Torsten <mttl@mailbox.org>
date Tue, 15 Aug 2017 22:00:06 +0200
parents 185f850aa543
children 7d703bca19b8
files libgui/src/files-dock-widget.cc libgui/src/files-dock-widget.h libgui/src/m-editor/file-editor.cc libgui/src/m-editor/file-editor.h libgui/src/main-window.cc
diffstat 5 files changed, 48 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/files-dock-widget.cc	Tue Aug 15 10:02:03 2017 -0700
+++ b/libgui/src/files-dock-widget.cc	Tue Aug 15 22:00:06 2017 +0200
@@ -665,9 +665,13 @@
         {
           new_name = path.absolutePath () + '/' + new_name;
           old_name = path.absolutePath () + '/' + old_name;
-          emit file_remove_signal (old_name, new_name);  // editor: close old
-          path.rename (old_name, new_name);
-          emit file_renamed_signal ();  // editor: load new file
+          // editor: close old
+          emit file_remove_signal (old_name, new_name);
+          // Do the renaming
+          bool st = path.rename (old_name, new_name);
+          // editor: load new/old file depending on success
+          emit file_renamed_signal (st);
+          // Clear cache of file browser
           _file_system_model->revert ();
         }
     }
@@ -709,9 +713,11 @@
             {
               // Close the file in the editor if open
               emit file_remove_signal (info.filePath (), QString ());
-              // Remove the file. This operation might fail, but we will not
-              // reopen a possibly related editor tab in this case.
-              _file_system_model->remove (index);
+              // Remove the file.
+              bool st = _file_system_model->remove (index);
+              // reload the old file if removing was not successful
+              if (! st)
+                emit file_renamed_signal (false);
             }
 
           _file_system_model->revert ();
--- a/libgui/src/files-dock-widget.h	Tue Aug 15 10:02:03 2017 -0700
+++ b/libgui/src/files-dock-widget.h	Tue Aug 15 22:00:06 2017 +0200
@@ -136,7 +136,7 @@
   void file_remove_signal (const QString& old_name, const QString& new_name);
 
   /** Emitted, when a file or directory is renamed. */
-  void file_renamed_signal (void);
+  void file_renamed_signal (bool);
 
 private:
   void process_new_file (const QString& parent_name);
--- a/libgui/src/m-editor/file-editor.cc	Tue Aug 15 10:02:03 2017 -0700
+++ b/libgui/src/m-editor/file-editor.cc	Tue Aug 15 22:00:06 2017 +0200
@@ -589,6 +589,9 @@
 file_editor::handle_file_remove (const QString& old_name,
                                  const QString& new_name)
 {
+  // Clear old lsit of files to reload
+  _tmp_closed_files.clear ();
+
   // Check if old name is a file or directory
   QFileInfo old (old_name);
   if (old.isDir ())
@@ -609,25 +612,21 @@
       editor_tab->file_has_changed (QString (), true);  // Close the tab
       _no_focus = false;  // Back to normal
 
+      _tmp_closed_files << old_name;  // for reloading if error removing
+
       if (! new_name.isEmpty ())
+        _tmp_closed_files << new_name;  // store new name
+      else
+        _tmp_closed_files << ""; // no new name, just removing this file
+
+      // Get and store the related encoding
+      for (editor_tab_map_const_iterator p = editor_tab_map.begin ();
+           p != editor_tab_map.end (); p++)
         {
-          // New name is set, store new name and its encoding
-          // loading this file after the renaming is complete.
-          // The new name is not signaled after the renaming for being able
-          // to use this construct for renaming a whole directory, too.
-
-          _tmp_closed_files = QStringList ();
-          _tmp_closed_files << new_name;
-
-          // Get and store the related encoding
-          for (editor_tab_map_const_iterator p = editor_tab_map.begin ();
-               p != editor_tab_map.end (); p++)
+          if (editor_tab == p->second.fet_ID)
             {
-              if (editor_tab == p->second.fet_ID)
-                {
-                  _tmp_closed_files << p->second.encoding;
-                  break;
-                }
+              _tmp_closed_files << p->second.encoding;
+              break;
             }
         }
     }
@@ -646,8 +645,6 @@
   emit fetab_file_name_query (nullptr);
 
   // Loop over all open files and pick those within old_dir
-  _tmp_closed_files = QStringList ();
-
   for (editor_tab_map_const_iterator p = editor_tab_map.begin ();
        p != editor_tab_map.end (); p++)
     {
@@ -663,6 +660,8 @@
           editor_tab->file_has_changed (QString (), true);  // Close
           _no_focus = false;  // Back to normal
 
+          // Store file for possible later reload
+          _tmp_closed_files << p->first;
 
           // Add the new file path and the encoding for later reloading
           // if new_name is given
@@ -670,20 +669,26 @@
             {
               QDir new_dir (new_name);
               _tmp_closed_files << new_dir.absoluteFilePath (rel_path_to_file);
-              _tmp_closed_files << p->second.encoding;
             }
+          else
+            _tmp_closed_files << ""; // no new name, just removing this file
+
+          _tmp_closed_files << p->second.encoding; // store the encoding
         }
     }
 }
 
 // Slot for signal indicating that a file was renamed
 void
-file_editor::handle_file_renamed ()
+file_editor::handle_file_renamed (bool load_new)
 {
   _no_focus = true;  // Remember for not focussing editor
-  for (int i = 0; i < _tmp_closed_files.count (); i = i + 2)
-    request_open_file (_tmp_closed_files.at (i),
-                       _tmp_closed_files.at (i+1));
+  for (int i = 0; i < _tmp_closed_files.count (); i = i + 3)
+    {
+      if (! _tmp_closed_files.at (i + load_new).isEmpty ())
+        request_open_file (_tmp_closed_files.at (i + load_new),
+                           _tmp_closed_files.at (i+2));
+    }
   _no_focus = false;  // Back to normal focus
 }
 
--- a/libgui/src/m-editor/file-editor.h	Tue Aug 15 10:02:03 2017 -0700
+++ b/libgui/src/m-editor/file-editor.h	Tue Aug 15 22:00:06 2017 +0200
@@ -282,7 +282,7 @@
   void handle_edit_file_request (const QString& file);
 
   void handle_file_remove (const QString&, const QString&);
-  void handle_file_renamed (void);
+  void handle_file_renamed (bool load_new = true);
 
   // Tells the editor to react on changed settings.
   void notice_settings (const QSettings *settings);
@@ -475,7 +475,10 @@
   QStringList _mru_files;
   QStringList _mru_files_encodings;
 
-  // List of temporarily closed files while they are renamed
+  // List of temporarily closed files for later reloading.
+  // Order: first closed old file
+  //        first new location of closed file
+  //        encoding to use for reload
   QStringList _tmp_closed_files;
 };
 
--- a/libgui/src/main-window.cc	Tue Aug 15 10:02:03 2017 -0700
+++ b/libgui/src/main-window.cc	Tue Aug 15 22:00:06 2017 +0200
@@ -1819,8 +1819,8 @@
                SIGNAL (file_remove_signal (const QString&, const QString&)),
                editor_window,
                SLOT (handle_file_remove (const QString&, const QString&)));
-      connect (file_browser_window, SIGNAL (file_renamed_signal (void)),
-               editor_window, SLOT (handle_file_renamed (void)));
+      connect (file_browser_window, SIGNAL (file_renamed_signal (bool)),
+               editor_window, SLOT (handle_file_renamed (bool)));
 #endif
 
       octave_link::post_event (this,