[LyX/master] Fix issue in running compare from the command line

Pavel Sanda sanda at lyx.org
Sun Nov 15 09:08:28 UTC 2020


commit f457a32a13e1dd01453df531ac48a76fb047c614
Author: Sam Crawley <sam at crawley.nz>
Date:   Fri Nov 13 20:58:20 2020 +1300

    Fix issue in running compare from the command line
    
    Because Compare uses threads, we need to make sure it is finished when a
    compare is executed from the command line. This was a problem for command
    sequences, because the next command would start running before the compare
    was done, and the buffer with differences was available.
    
    So this commit adds the "run-blocking" parameter when using
    LFUN_DIALOG_SHOW to run a Compare. When calling Compare with run-sync, the
    LFUN will wait for the compare worker thread to finish before returning and
    possibly running the next command.
---
 src/frontends/qt/GuiCompare.cpp |   33 +++++++++++++++++++++++++++++----
 src/frontends/qt/GuiCompare.h   |    3 ++-
 2 files changed, 31 insertions(+), 5 deletions(-)

diff --git a/src/frontends/qt/GuiCompare.cpp b/src/frontends/qt/GuiCompare.cpp
index d1da5b3..2edc8c8 100644
--- a/src/frontends/qt/GuiCompare.cpp
+++ b/src/frontends/qt/GuiCompare.cpp
@@ -303,7 +303,7 @@ Buffer const * GuiCompare::bufferFromFileName(string const & file) const
 }
 
 
-int GuiCompare::run()
+int GuiCompare::run(bool blocking_mode)
 {
 	progressBar->setValue(0);
 
@@ -325,14 +325,21 @@ int GuiCompare::run()
 	options.settings_from_new = newSettingsRB->isChecked();
 
 	// init the compare object and start it
+
 	compare_ = new Compare(new_buffer_, old_buffer_, dest_buffer_, options);
+
 	connect(compare_, SIGNAL(error()), this, SLOT(error()));
-	connect(compare_, SIGNAL(finished(bool)), this, SLOT(finished(bool)));
+	// Only connect the finished() method to the signal if we're *not* in blocking_mode
+	//  (i.e. we want to make it possible for caller to block for the results)
+	if (! blocking_mode) {
+		connect(compare_, SIGNAL(finished(bool)), this, SLOT(finished(bool)));
+	}
 	connect(compare_, SIGNAL(progress(int)), this, SLOT(progress(int)));
 	connect(compare_, SIGNAL(progressMax(int)), this, SLOT(progressMax(int)));
 	connect(compare_, SIGNAL(statusMessage(QString)),
 		this, SLOT(setStatusMessage(QString)));
 	compare_->start(QThread::LowPriority);
+
 	return 1;
 }
 
@@ -340,10 +347,28 @@ bool GuiCompare::initialiseParams(std::string const &par)
 {
 	//just for the sake of parsing arguments
 	FuncRequest cmd(LFUN_UNKNOWN_ACTION, par);
-	if (cmd.getArg(0) == "run") {
+	if (cmd.getArg(0) == "run" || cmd.getArg(0) == "run-blocking") {
 		oldFileCB->setEditText(toqstr(cmd.getArg(1)));
 		newFileCB->setEditText(toqstr(cmd.getArg(2)));
-		slotOK();
+
+		if (cmd.getArg(0) == "run" ) {
+			// Run asynchronously
+			slotOK();
+		}
+		else {
+			// Run synchronously
+			enableControls(false);
+
+			if (! run(true)) {
+				error();
+				return false;
+			}
+
+			// Wait for the Compare function to process in a thread (2 minute timeout)
+			compare_->wait(120000);
+
+			finished(false);
+		}
 	}
 
 	progressBar->setValue(0);
diff --git a/src/frontends/qt/GuiCompare.h b/src/frontends/qt/GuiCompare.h
index 649ba5c..e71a131 100644
--- a/src/frontends/qt/GuiCompare.h
+++ b/src/frontends/qt/GuiCompare.h
@@ -85,7 +85,8 @@ private:
 	Buffer const * bufferFromFileName(std::string const & file) const;
 
 	/// create the compare object and run the comparison
-	int run();
+	///  if blocking_mode is true, run should execute so that the caller can block to wait for the results
+	int run(bool blocking_mode = false);
 
 private:
 	/// the object that will do the comparison


More information about the lyx-cvs mailing list