-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfluxengine.cpp
261 lines (230 loc) · 6.97 KB
/
fluxengine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include "fluxengine.h"
#include <QDir>
#include <qsettings.h>
fluxengine::fluxengine(QObject *parent) : QObject(parent)
{
connect(&m_process,&QProcess::errorOccurred,this,&fluxengine::errorOccured);
connect(&m_process,&QProcess::readyReadStandardError,this,&fluxengine::readyReadStandardError);
connect(&m_process,&QProcess::readyReadStandardOutput,this,&fluxengine::readyReadStandardOutput);
connect(&m_process,&QProcess::started,this,&fluxengine::started);
connect(&m_process,&QProcess::stateChanged,this,&fluxengine::stateChanged);
connect(&m_process,&QProcess::readyRead,this,&fluxengine::readyRead);
//finished is overloaded...
connect(&m_process,QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),this,&fluxengine::finished);
m_listening = false;
m_address = "";
m_workingdirectory = QDir::currentPath();
}
void fluxengine::write(QByteArray comment)
{
if(QSysInfo::productType() == "windows") comment.append("\r");
m_process.write(comment+"\n");
}
QString fluxengine::operatingSystem()
{
return QSysInfo::prettyProductName();
}
QString fluxengine::getAddress() const
{
return m_address;
}
void fluxengine::setAddress(const QString &address)
{
m_address = address;
}
void fluxengine::setWorkingDirectory(const QString &Dir)
{
m_workingdirectory = QDir::toNativeSeparators(Dir);
}
QString fluxengine::getWorkingDirectory()
{
return m_workingdirectory;
}
void fluxengine::start()
{
m_listening = true;
QString program = getProcess();
QStringList Arguments;
if(QSysInfo::productType() == "windows") Arguments << "/Q"; // /Q turns echo of on windows.
m_process.start(program, Arguments, QIODevice::ReadWrite);
}
//void fluxengine::startdirect()
//{ //ToDo spaces in strings to cmd
// m_listening = true;
// QString program = getProcess();
// QString command;
// command = m_workingdirectory;
//// if(QSysInfo::productType() == "windows") command.prepend(""");
// if(QSysInfo::productType() != "windows") command.prepend("\"");
//// if(QSysInfo::productType() == "windows") command.append(""");
// if(QSysInfo::productType() != "windows") command.append("\" ");
// command = (command + " " + m_address);
// if(QSysInfo::productType() == "windows") command.append("\r");
// command.append("\n");
// QStringList Arguments;
// if(QSysInfo::productType() == "windows")
// {
// Arguments << "/c" << command;
// } else
// {
// Arguments << "-c" << command;
// }
// qInfo() << Arguments;
// m_process.start(program, Arguments);
//}
void fluxengine::stop()
{
m_listening = false;
if(QSysInfo::productType() == "windows")
{
//on windows fluxengine starts as a separate process and cannot be stopped bij killing the cmd...
m_process.terminate();
m_process.kill();
} else
{
m_process.close();
}
}
bool fluxengine::busy()
{
if (!m_listening)
{
return false;
}
else
{
return true;
}
}
void fluxengine::errorOccured(QProcess::ProcessError error)
{
if(!m_listening) return;
switch (error) {
case QProcess::FailedToStart:
emit output("Error: Failed to start");
break;
case QProcess::Crashed:
emit output("Error: Crashed");
break;
case QProcess::Timedout:
emit output("Error: Timedout");
break;
case QProcess::WriteError:
emit output("Error: Write error");
break;
case QProcess::ReadError:
emit output("Error: Read error");
break;
default:
emit output("Error: Unknown error");
}
}
void fluxengine::finished(int exitcode, QProcess::ExitStatus exitStatus)
{
if(!m_listening) return;
Q_UNUSED(exitcode);
Q_UNUSED(exitStatus);
m_listening = false;
QString message = " ";
emit output(message);
}
void fluxengine::readyReadStandardError()
{
if(!m_listening) return;
QByteArray data = m_process.readAllStandardError();
if (data.size() > 1)
{
QString message = "";
message.append(data);
if (message.contains("No such file or directory", Qt::CaseInsensitive) || message.contains("Is a directory", Qt::CaseInsensitive))
{
//fluxengine could not be located. Set the location to fluxengine
message = "Error: No such file or directory. Fluxengine could not be located. Use \"Ctrl+P\" to specify where fluxengine can be found.";
}
emit output(message);
}
}
void fluxengine::readyReadStandardOutput()
{
if(!m_listening) return;
QByteArray data = m_process.readAllStandardOutput();
if (data.size() > 1)
{
// data.append("readyReadStandardOutput");
emit output(QString(data.trimmed()));
}
}
void fluxengine::started()
{
//do nothing
}
void fluxengine::stateChanged(QProcess::ProcessState newState)
{
switch (newState) {
case QProcess::NotRunning:
emit enableFluxengineCommands(false);
break;
case QProcess::Starting:
break;
case QProcess::Running:
if(QSysInfo::productType() == "windows")
{
QByteArray command = "prompt $g\r\n";
m_process.write(command); //get rid of the prompt in windows
}
startFluxengine();
emit enableFluxengineCommands(true);
break;
}
}
void fluxengine::readyRead()
{
if(!m_listening) return;
QByteArray data = m_process.readAll().trimmed();
// In windows cmd shows first the windows version and copyright. we dont need to show these.
//also the exit command is shown. No need to worry the user with that.
if(QSysInfo::productType() == "windows")
{
if (data.contains("Microsoft Corporation."))
data = "";
if (data.right(4) == "exit")
data = "";
}
//data = QDir::toNativeSeparators(data).toUtf8();
if (data.size() > 1)
{
emit output(data);
}
}
QString fluxengine::getProcess()
{
if(QSysInfo::productType() == "windows") return "cmd";
return "bash";
}
void fluxengine::startFluxengine()
{
QByteArray command;
if(QSysInfo::productType() == "windows")
{
command = (QStringLiteral("\"") + m_workingdirectory + ("\" " + m_address)).toUtf8();
} else
{
command = ("\"" + m_workingdirectory + "\" " + m_address).toUtf8();
}
// qInfo() << "command: " << command;
if(QSysInfo::productType() == "windows") command.append("\r");
command.append("\n");
m_process.write(command);
if (m_address.contains(".imd", Qt::CaseInsensitive) || m_address.isEmpty())
//give user option to write comment for an imd file
{
//dont close fluxengine proces. Fluxengine process will be closed from the mainwindow.
} else
{
command.clear();
command.append("exit");
if(QSysInfo::productType() == "windows") command.append("\r");
command.append("\n");
m_process.write(command);
}
}