Mercurial > jwe > octave
changeset 24048:ba164db1c4a4
close/reload editor tab when file is (re)moved in gui terminal (bug #43922)
* file-editor.cc (handle_file_remove): Add the pointer of a QMutex to
the paramter list, new structure, unlock the mutex at the and if a valid
mutex was given;
* file-editor.h (handle_file_remove): QMutex added to parameter list, default
is the null pointer
* main-window.cc (construct): connect new signals from octave_qt_link to the
slot handle_file_renamed and the updated slot handle_file_remove
* octave-qt-link.cc (do_file_remove): new method for renaming or removing
files which signals the editor (gui thread) for closing the files before
removing is carried out;
(do_file_renamed): new method for re-loading renamed files into editor;
* octave-qt-link.h: new methods do_file_remove and do_file_renamed,
new signals used in the new methods for signaling the editor slots
* dirfns.cc (rmdir): call the new static octave_link method for removing
a file, which might be opened in the editor before removing a dir;
(rename): call the new static octave_link method for renaming a file,
which might be opened in the editor before renaming it and call the method
for reloading the new renamed file;
* octave-link.cc (openvar): removed spaces at the end of some lines;
* octave-link.h (file_remove): new static method calling purely virtual method
do_file_remove, which is implemented in octave_qt_link;
(file_renamed): new static method calling purely virtual method
do_file_renamed, which is implemented in octave_qt_link
* syscalls.cc (unlink): call the new static octave_link method for removing
a file, which might be opened in the editor before removing it
author | Torsten <mttl@mailbox.org> |
---|---|
date | Sun, 17 Sep 2017 05:52:33 +0200 |
parents | 9c198d1c84cd |
children | 6782105f1820 |
files | libgui/src/m-editor/file-editor.cc libgui/src/m-editor/file-editor.h libgui/src/main-window.cc libgui/src/octave-qt-link.cc libgui/src/octave-qt-link.h libinterp/corefcn/dirfns.cc libinterp/corefcn/octave-link.cc libinterp/corefcn/octave-link.h libinterp/corefcn/syscalls.cc |
diffstat | 9 files changed, 125 insertions(+), 35 deletions(-) [+] |
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor.cc Wed Sep 13 15:24:56 2017 +0200 +++ b/libgui/src/m-editor/file-editor.cc Sun Sep 17 05:52:33 2017 +0200 @@ -1054,49 +1054,56 @@ // is going to be deleted/renamed void file_editor::handle_file_remove (const QString& old_name, - const QString& new_name) + const QString& new_name, + QMutex *wait_closing) { // Clear old lsit of files to reload m_tmp_closed_files.clear (); // Check if old name is a file or directory QFileInfo old (old_name); + if (old.isDir ()) { // Call the function which handles directories and return handle_dir_remove (old_name, new_name); - return; } - - // Is old file open? - file_editor_tab *editor_tab - = static_cast<file_editor_tab *> (find_tab_widget (old_name)); - - if (editor_tab) + else { - // Yes, close it silently - m_no_focus = true; // Remember for not focussing editor - editor_tab->file_has_changed (QString (), true); // Close the tab - m_no_focus = false; // Back to normal - - m_tmp_closed_files << old_name; // for reloading if error removing - - if (! new_name.isEmpty ()) - m_tmp_closed_files << new_name; // store new name - else - m_tmp_closed_files << ""; // no new name, just removing this file - - // Get and store the related encoding - for (editor_tab_map_const_iterator p = m_editor_tab_map.begin (); - p != m_editor_tab_map.end (); p++) + // It is a single file. IT is open? + file_editor_tab *editor_tab + = static_cast<file_editor_tab *> (find_tab_widget (old_name)); + + if (editor_tab) { - if (editor_tab == p->second.fet_ID) + // Yes, close it silently + m_no_focus = true; // Remember for not focussing editor + editor_tab->file_has_changed (QString (), true); // Close the tab + m_no_focus = false; // Back to normal + + m_tmp_closed_files << old_name; // for reloading if error removing + + if (! new_name.isEmpty ()) + m_tmp_closed_files << new_name; // store new name + else + m_tmp_closed_files << ""; // no new name, just removing this file + + // Get and store the related encoding + for (editor_tab_map_const_iterator p = m_editor_tab_map.begin (); + p != m_editor_tab_map.end (); p++) { - m_tmp_closed_files << p->second.encoding; - break; + if (editor_tab == p->second.fet_ID) + { + m_tmp_closed_files << p->second.encoding; + break; + } } } } + + // Finally, if a mutex was given, unlock it for enabling the removing + if (wait_closing) + wait_closing->unlock (); } // Slot for signal indicating that a file was renamed
--- a/libgui/src/m-editor/file-editor.h Wed Sep 13 15:24:56 2017 +0200 +++ b/libgui/src/m-editor/file-editor.h Sun Sep 17 05:52:33 2017 +0200 @@ -26,6 +26,7 @@ #include <QToolBar> #include <QAction> #include <QMenuBar> +#include <QMutex> #include <QStatusBar> #include <QCloseEvent> #include <QTabWidget> @@ -281,7 +282,8 @@ void handle_edit_file_request (const QString& file); - void handle_file_remove (const QString&, const QString&); + void handle_file_remove (const QString&, const QString&, + QMutex *wait_closing = 0); void handle_file_renamed (bool load_new = true); // Tells the editor to react on changed settings.
--- a/libgui/src/main-window.cc Wed Sep 13 15:24:56 2017 +0200 +++ b/libgui/src/main-window.cc Sun Sep 17 05:52:33 2017 +0200 @@ -1946,6 +1946,7 @@ int, const QString&))); + // Signals for removing/renaming files/dirs in the file browser connect (m_file_browser_window, SIGNAL (file_remove_signal (const QString&, const QString&)), m_editor_window, @@ -1953,6 +1954,15 @@ connect (m_file_browser_window, SIGNAL (file_renamed_signal (bool)), m_editor_window, SLOT (handle_file_renamed (bool))); + // Signals for removing/renaming files/dirs in the temrinal window + connect (m_octave_qt_link, + SIGNAL (file_remove_signal (const QString&, const QString&, + QMutex*)), + m_editor_window, + SLOT (handle_file_remove (const QString&, const QString&, + QMutex*))); + connect (m_octave_qt_link, SIGNAL (file_renamed_signal (bool)), + m_editor_window, SLOT (handle_file_renamed (bool))); #endif octave_link::post_event (this,
--- a/libgui/src/octave-qt-link.cc Wed Sep 13 15:24:56 2017 +0200 +++ b/libgui/src/octave-qt-link.cc Sun Sep 17 05:52:33 2017 +0200 @@ -422,6 +422,31 @@ } void +octave_qt_link::do_file_remove (const std::string& old_name, + const std::string& new_name) +{ + QMutex wait_closing; + + // Emit the signal for the editor for closing the file if it is open + emit file_remove_signal (QString::fromStdString (old_name), + QString::fromStdString (new_name), &wait_closing); + + // Unlock and lock the mutex (make sure it is locked without being blocked) + wait_closing.unlock (); + wait_closing.lock (); + // Try to lock it again, thus waiting for the unlock of the editor after + // closing the files. Chose a timeout of 1 s for not being blocked forever + // when something goes wrong + wait_closing.tryLock (1000); +} + +void +octave_qt_link::do_file_renamed (bool load_new) +{ + emit file_renamed_signal (load_new); +} + +void octave_qt_link::do_execute_command_in_terminal (const std::string& command) { emit execute_command_in_terminal_signal (QString::fromStdString (command));
--- a/libgui/src/octave-qt-link.h Wed Sep 13 15:24:56 2017 +0200 +++ b/libgui/src/octave-qt-link.h Sun Sep 17 05:52:33 2017 +0200 @@ -110,6 +110,10 @@ void update_directory (void); + void do_file_remove (const std::string& old_name, + const std::string& new_name); + void do_file_renamed (bool load_new = true); + void do_execute_command_in_terminal (const std::string& command); void do_set_workspace (bool top_level, bool debug, @@ -173,6 +177,10 @@ void change_directory_signal (const QString& dir); + void file_remove_signal (const QString& old_name, + const QString& new_name, QMutex *wait_closing); + void file_renamed_signal (bool load_new); + void execute_command_in_terminal_signal (const QString& command); void set_workspace_signal (bool top_level,
--- a/libinterp/corefcn/dirfns.cc Wed Sep 13 15:24:56 2017 +0200 +++ b/libinterp/corefcn/dirfns.cc Sun Sep 17 05:52:33 2017 +0200 @@ -295,10 +295,18 @@ } if (doit) - status = octave::sys::recursive_rmdir (fulldir, msg); + { + octave_link::file_remove (fulldir, ""); + status = octave::sys::recursive_rmdir (fulldir, msg); + } } else - status = octave::sys::rmdir (fulldir, msg); + { + octave_link::file_remove (fulldir, ""); + status = octave::sys::rmdir (fulldir, msg); + } + + octave_link::file_renamed (status >= 0); if (status < 0) return ovl (false, msg, "rmdir"); @@ -421,12 +429,20 @@ std::string msg; + octave_link::file_remove (from, to); + int status = octave::sys::rename (from, to, msg); if (status < 0) - return ovl (-1.0, msg); + { + octave_link::file_renamed (false); + return ovl (-1.0, msg); + } else - return ovl (status, ""); + { + octave_link::file_renamed (true); + return ovl (status, ""); + } } DEFUN (glob, args, ,
--- a/libinterp/corefcn/octave-link.cc Wed Sep 13 15:24:56 2017 +0200 +++ b/libinterp/corefcn/octave-link.cc Sun Sep 17 05:52:33 2017 +0200 @@ -394,12 +394,12 @@ print_usage (); if (! args(0).is_string ()) - error ("openvar: NAME must be a string"); + error ("openvar: NAME must be a string"); std::string name = args(0).string_value (); if (! (Fisguirunning ())(0).is_true ()) - warning ("openvar: GUI is not running, can't start Variable Editor"); + warning ("openvar: GUI is not running, can't start Variable Editor"); else octave_link::openvar (name);
--- a/libinterp/corefcn/octave-link.h Wed Sep 13 15:24:56 2017 +0200 +++ b/libinterp/corefcn/octave-link.h Sun Sep 17 05:52:33 2017 +0200 @@ -31,7 +31,7 @@ #include <string> #include "oct-mutex.h" - +#include "octave.h" #include "event-queue.h" class string_vector; @@ -250,6 +250,20 @@ instance->do_change_directory (dir); } + // Methods for removing/renaming files which might be open in editor + static void file_remove (const std::string& old_name, + const std::string& new_name) + { + if (octave::application::is_gui_running () && enabled ()) + instance->do_file_remove (old_name, new_name); + } + + static void file_renamed (bool load_new) + { + if (octave::application::is_gui_running () && enabled ()) + instance->do_file_renamed (load_new); + } + // Preserves pending input. static void execute_command_in_terminal (const std::string& command) { @@ -538,6 +552,10 @@ virtual void do_change_directory (const std::string& dir) = 0; + virtual void do_file_remove (const std::string& old_name, + const std::string& new_name) = 0; + virtual void do_file_renamed (bool) = 0; + virtual void do_execute_command_in_terminal (const std::string& command) = 0; virtual void
--- a/libinterp/corefcn/syscalls.cc Wed Sep 13 15:24:56 2017 +0200 +++ b/libinterp/corefcn/syscalls.cc Sun Sep 17 05:52:33 2017 +0200 @@ -42,7 +42,6 @@ #include "oct-env.h" #include "oct-syscalls.h" #include "oct-uname.h" - #include "defun.h" #include "error.h" #include "errwarn.h" @@ -52,6 +51,7 @@ #include "ovl.h" #include "oct-stdstrm.h" #include "oct-stream.h" +#include "octave-link.h" #include "sysdep.h" #include "utils.h" #include "variables.h" @@ -1088,8 +1088,12 @@ std::string msg; + octave_link::file_remove (name, ""); + int status = octave::sys::unlink (name, msg); + octave_link::file_renamed (status == 0); + return ovl (status, msg); }