]> git.lyx.org Git - lyx.git/commitdiff
Fix issue in running compare from the command line
authorSam Crawley <sam@crawley.nz>
Fri, 13 Nov 2020 07:58:20 +0000 (20:58 +1300)
committerPavel Sanda <sanda@lyx.org>
Sun, 15 Nov 2020 09:35:28 +0000 (10:35 +0100)
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
src/frontends/qt/GuiCompare.h

index d1da5b337f151608d8b07967d516ebb45c3bcedf..2edc8c8cacdd4349e329fbd115b461661d6118cf 100644 (file)
@@ -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);
index 649ba5c1553693513511c13d4d9de3836cdd5e0f..e71a13139ebbfa0e928e1453241bd7b07c0968e8 100644 (file)
@@ -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