Fix bug #8032 (timeout when using the Sweave module)

After the timeout elapses, the user is notified that a command is taking
a long time to complete and is given the choice to stop it. If the user
decides to let the command run, the timeout is increased, otherwise the
command is killed. One is prompted a first time after 3 mins, a second
time after 9 mins, a third time after 27 mins, and so on, i.e., the n-th
prompt occurs after 3^n minutes.


git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@40775 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Enrico Forestieri 2012-02-18 14:16:25 +00:00
parent dd2185a4a6
commit 1eb41a88f2
4 changed files with 67 additions and 4 deletions

View File

@ -17,6 +17,8 @@
#include "qt_helpers.h"
#include "frontends/alert.h"
#include "support/debug.h"
#include "support/Systemcall.h"
@ -67,6 +69,14 @@ GuiProgress::GuiProgress()
}
int GuiProgress::prompt(docstring const & title, docstring const & question,
int default_button, int cancel_button,
docstring const & b1, docstring const & b2)
{
return Alert::prompt(title, question, default_button, cancel_button, b1, b2);
}
QString GuiProgress::currentTime()
{
return QTime::currentTime().toString("hh:mm:ss.zzz");

View File

@ -42,6 +42,10 @@ public:
void lyxerrDisconnect();
void lyxerrFlush();
int prompt(docstring const & title, docstring const & question,
int default_button, int cancel_button,
docstring const & b1, docstring const & b2);
static QString currentTime();
Q_SIGNALS:

View File

@ -12,6 +12,7 @@
#ifndef LYX_SUPPORT_PROGRESSINTERFACE_H
#define LYX_SUPPORT_PROGRESSINTERFACE_H
class docstring;
class QString;
namespace lyx {
@ -36,6 +37,9 @@ public:
virtual void toggleWarning(QString const & title, QString const & msg, QString const & formatted) = 0;
virtual void error(QString const & title, QString const & message) = 0;
virtual void information(QString const & title, QString const & message) = 0;
virtual int prompt(docstring const & title, docstring const & question,
int default_button, int cancel_button,
docstring const & b1, docstring const & b2) = 0;
virtual void lyxerrConnect() = 0;
virtual void lyxerrDisconnect() = 0;

View File

@ -15,6 +15,7 @@
#include "support/debug.h"
#include "support/filetools.h"
#include "support/gettext.h"
#include "support/lstrings.h"
#include "support/qstring_helpers.h"
#include "support/Systemcall.h"
@ -72,6 +73,8 @@ public:
void toggleWarning(QString const &, QString const &, QString const &) {}
void error(QString const &, QString const &) {}
void information(QString const &, QString const &) {}
int prompt(docstring const &, docstring const &, int default_but, int,
docstring const &, docstring const &) { return default_but; }
};
@ -371,11 +374,26 @@ void SystemcallPrivate::waitAndProcessEvents()
}
namespace {
bool queryStopCommand(QString const & cmd)
{
docstring text = bformat(_(
"The command\n%1$s\nhas not yet completed.\n\n"
"Do you want to stop it?"), qstring_to_ucs4(cmd));
return ProgressInterface::instance()->prompt(_("Stop command?"), text,
1, 1, _("&Stop it"), _("Let it &run")) == 0;
}
}
bool SystemcallPrivate::waitWhile(State waitwhile, bool process_events, int timeout)
{
if (!process_)
return false;
bool timedout = false;
process_events_ = process_events;
// Block GUI while waiting,
@ -383,8 +401,24 @@ bool SystemcallPrivate::waitWhile(State waitwhile, bool process_events, int time
if (!process_events_) {
if (waitwhile == Starting)
return process_->waitForStarted(timeout);
if (waitwhile == Running)
return process_->waitForFinished(timeout);
if (waitwhile == Running) {
int bump = 2;
while (!timedout) {
if (process_->waitForFinished(timeout))
return true;
bool stop = queryStopCommand(cmd_);
// The command may have finished in the meantime
if (process_->state() == QProcess::NotRunning)
return true;
if (stop) {
timedout = true;
process_->kill();
} else {
timeout *= bump;
bump = 3;
}
}
}
return false;
}
@ -399,10 +433,21 @@ bool SystemcallPrivate::waitWhile(State waitwhile, bool process_events, int time
// process events while waiting whith timeout
QTime timer;
timer.start();
while (state == waitwhile && state != Error && timer.elapsed() < timeout) {
while (state == waitwhile && state != Error && !timedout) {
waitAndProcessEvents();
if (timer.elapsed() > timeout) {
bool stop = queryStopCommand(cmd_);
// The command may have finished in the meantime
if (process_->state() == QProcess::NotRunning)
break;
if (stop) {
timedout = true;
process_->kill();
} else
timeout *= 3;
}
}
return (state != Error) && (timer.elapsed() < timeout);
return (state != Error) && !timedout;
}