3 * \file SystemcallPrivate.cpp
4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
14 #include "SystemcallPrivate.h"
16 #include "Systemcall.h"
17 #include "ProgressInterface.h"
20 #include "qstring_helpers.h"
27 // TODO should we move qt_ to qstring_helpers?
29 QString const qt_(char const * str)
31 return toqstr(_(str));
36 SystemcallPrivate::SystemcallPrivate()
38 ProgressInterface* progress = Systemcall::progress();
40 connect(&process, SIGNAL(readyReadStandardOutput()), this, SLOT(newProcessOutput()));
41 connect(&process, SIGNAL(started()), this, SLOT(processStarted()));
42 connect(&process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
43 connect(&process, SIGNAL(finished(int, QProcess::ExitStatus)),
44 this, SLOT(processFinished(int, QProcess::ExitStatus)));
49 int SystemcallPrivate::start(const std::string & cmd, bool waitForFinished)
51 ProgressInterface* progress = Systemcall::progress();
53 progress->clearMessages();
54 progress->appendMessage(qt_("Starting process with command "));
55 progress->appendMessage(toqstr(cmd));
58 process.setReadChannel(QProcess::StandardOutput);
59 process.start(toqstr(cmd), QStringList(), QIODevice::ReadOnly);
60 // wait some seconds until the process has started
61 process.waitForStarted(10 * 1000);
62 if (waitForFinished) {
63 // with waitForFinished(-1); we only get one signal per run
64 while (process.state() == QProcess::Running)
65 process.waitForFinished(500);
66 return process.exitCode();
68 if (process.state() != QProcess::Running) {
70 // TODO this needs more testing
78 void SystemcallPrivate::newProcessOutput()
80 ProgressInterface* progress = Systemcall::progress();
82 const QString output = QString::fromLocal8Bit(process.readAllStandardOutput());
83 progress->appendMessage(output);
88 void SystemcallPrivate::processStarted()
90 ProgressInterface* progress = Systemcall::progress();
92 progress->appendMessage(qt_("Process started\n"));
97 void SystemcallPrivate::processError(QProcess::ProcessError err)
99 ProgressInterface* progress = Systemcall::progress();
103 case QProcess::FailedToStart:
104 message = qt_("The process failed to start. Either the invoked program is missing, "
105 "or you may have insufficient permissions to invoke the program.");
107 case QProcess::Crashed:
108 message = qt_("The process crashed some time after starting successfully.");
110 case QProcess::Timedout:
111 message = qt_("The process timed out. It might be restarted automatically.");
113 case QProcess::WriteError:
114 message = qt_("An error occurred when attempting to write to the process. For example, "
115 "the process may not be running, or it may have closed its input channel.");
117 case QProcess::ReadError:
118 message = qt_("An error occurred when attempting to read from the process. For example, "
119 "the process may not be running.");
121 case QProcess::UnknownError:
123 message = qt_("An unknown error occured.");
126 progress->appendMessage(qt_("The process failed: ") + message + '\n');
131 void SystemcallPrivate::processFinished(int, QProcess::ExitStatus status)
133 ProgressInterface* progress = Systemcall::progress();
137 case QProcess::NormalExit:
138 message = qt_("The process exited normally.");
140 case QProcess::CrashExit:
141 message = qt_("The process crashed.");
144 message = qt_("Unknown exit state.");
147 progress->appendMessage(qt_("Process finished: ") + message + '\n');
153 } // namespace support
156 #include "SystemcallPrivate_moc.cpp"