]> git.lyx.org Git - features.git/blob - src/support/SystemcallPrivate.cpp
add progress view of system calls
[features.git] / src / support / SystemcallPrivate.cpp
1 // -*- C++ -*-
2 /**
3  * \file SystemcallPrivate.cpp
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Peter Kümmel
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "SystemcallPrivate.h"
15
16 #include "Systemcall.h"
17 #include "ProgressInterface.h"
18
19 #include "gettext.h"
20 #include "qstring_helpers.h"
21
22
23 namespace lyx {
24 namespace support {
25
26
27 // TODO should we move qt_ to qstring_helpers?
28 static
29 QString const qt_(char const * str)
30 {
31         return toqstr(_(str));
32 }
33
34
35
36 SystemcallPrivate::SystemcallPrivate() 
37
38         ProgressInterface* progress = Systemcall::progress();
39         if (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)));                                
45         }
46 }
47
48
49 int SystemcallPrivate::start(const std::string& cmd, bool waitForFinished)
50 {
51         ProgressInterface* progress = Systemcall::progress();
52         if (progress) {
53                 progress->clearMessages();
54                 progress->appendMessage(qt_("Starting LaTex with command "));
55                 progress->appendMessage(cmd.c_str());
56         }
57
58         process.setReadChannel(QProcess::StandardOutput);
59         process.start(cmd.c_str(), 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();
67         }
68         if (process.state() != QProcess::Running) {
69                 process.kill();
70                 // TODO this needs more testing
71                 deleteLater();
72                 return -1;
73         }
74         return 0;
75 }
76
77
78 void SystemcallPrivate::newProcessOutput()
79 {
80         ProgressInterface* progress = Systemcall::progress();
81         if (progress) {
82                 const QString output = QString::fromLocal8Bit(process.readAllStandardOutput());
83                 progress->appendMessage(output);
84         }
85 }
86
87
88 void SystemcallPrivate::processStarted()
89 {
90         ProgressInterface* progress = Systemcall::progress();
91         if (progress) {
92                 progress->appendMessage(qt_("LaTex started\n"));
93         }
94 }
95
96
97 void SystemcallPrivate::processError(QProcess::ProcessError)
98 {
99         ProgressInterface* progress = Systemcall::progress();
100         if (progress) {
101                 progress->appendMessage(qt_("LaTex error\n"));
102         }
103 }
104
105
106 void SystemcallPrivate::processFinished(int, QProcess::ExitStatus)
107 {
108         deleteLater();
109 }
110
111
112 } // namespace support
113 } // namespace lyx
114
115 #include "SystemcallPrivate_moc.cpp"