-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappcommunicator.cpp
More file actions
341 lines (287 loc) · 9.98 KB
/
appcommunicator.cpp
File metadata and controls
341 lines (287 loc) · 9.98 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include "appcommunicator.h"
#include "luascriptcontroller.h"
#include <QGuiApplication>
#include <QQuickWindow>
#include <QDebug>
#ifdef Q_OS_WIN
#include <Windows.h>
#endif
// TARGET_PATH will be set by CMake at compile time
#ifndef TARGET_PATH
#define TARGET_PATH ""
#endif
QString AppCommunicator::runningFilePath = "";
AppCommunicator::AppCommunicator(QSharedPointer<LuaScriptController> ptrLuaScriptController, QObject* parent)
: QObject(parent),
ptrLuaScriptController(ptrLuaScriptController),
localServer(nullptr)
{
// Create a local server
this->localServer = new QLocalServer(this);
if (this->localServer->listen(serverName))
{
connect(this->localServer, &QLocalServer::newConnection, this, &AppCommunicator::handleNewConnection);
qDebug() << "NOWALuaScript local server started.";
}
else
{
qDebug() << "Failed to start NOWALuaScript local server:" << this->localServer->errorString();
}
// Set the path to the directory instead of a specific file
QString watchDirectory = QString(TARGET_PATH) + "/NOWALuaScript/bin/";
this->fileWatcher.addPath(watchDirectory);
// Connect to the directoryChanged signal
connect(&fileWatcher, &QFileSystemWatcher::directoryChanged, this, [=](const QString& path)
{
QTimer::singleShot(100, [=]() { // Adjust the delay as needed
QDir dir(watchDirectory);
QStringList xmlFiles = dir.entryList({"*.xml"}, QDir::Files);
for (const QString& fileName : xmlFiles)
{
qDebug() << "Detected change in XML file:" << fileName;
this->readXmlFile(watchDirectory + "/" + fileName);
}
});
});
QDir dir(watchDirectory);
QStringList xmlFiles = dir.entryList({"*.xml"}, QDir::Files);
for (const QString& fileName : xmlFiles)
{
qDebug() << "Detected change in XML file:" << fileName;
this->readXmlFile(watchDirectory + "/" + fileName);
}
}
AppCommunicator::~AppCommunicator()
{
if (nullptr != this->localServer)
{
this->localServer->close();
}
}
void AppCommunicator::onFileChanged(const QString& path)
{
qDebug() << "File changed:" << path;
if (path == this->watchFilePathName)
{
// showWindowsMessageBox("onFileChanged", "Received Lua script path: " + path);
// Call the function when the file changes
this->readXmlFile(path);
// Re-add the file to the watcher if it exists (file may have been deleted/recreated)
if (!this->fileWatcher.files().contains(path) && QFileInfo::exists(path))
{
qDebug() << "Re-adding file to watch list.";
this->fileWatcher.addPath(path);
}
}
}
void AppCommunicator::onDirectoryChanged(const QString& path)
{
qDebug() << "Directory changed:" << path;
QString dataFilePath = path + "/lua_script_data.xml";
if (dataFilePath == this->watchFilePathName)
{
// showWindowsMessageBox("onDirectoryChanged", "Received Lua script path: " + dataFilePath);
// Call the function when the file changes
this->readXmlFile(dataFilePath);
// Check if the file now exists and add it to the watcher
if (!this->fileWatcher.files().contains(this->watchFilePathName) && QFileInfo::exists(this->watchFilePathName))
{
qDebug() << "File created. Adding to watch list.";
this->fileWatcher.addPath(this->watchFilePathName);
}
}
}
void AppCommunicator::showWindowsMessageBox(const QString &title, const QString &message)
{
#ifdef Q_OS_WIN
MessageBox(
nullptr,
reinterpret_cast<LPCWSTR>(message.utf16()),
reinterpret_cast<LPCWSTR>(title.utf16()),
MB_OK | MB_ICONINFORMATION
);
#endif
}
void AppCommunicator::readXmlFile(const QString& filePath)
{
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qWarning() << "Error opening file:" << filePath;
return;
}
QXmlStreamReader xmlReader(&file);
// Variables to store extracted data
QString messageId;
QString filePathName;
QString errorMessage;
int line = -1;
int start = -1;
int end = -1;
// Parse the XML file
while (!xmlReader.atEnd() && !xmlReader.hasError())
{
xmlReader.readNext();
// Check if the node is a start element
if (xmlReader.isStartElement())
{
if (xmlReader.name().toString() == "MessageId")
{
messageId = xmlReader.readElementText();
}
else if (xmlReader.name().toString() == "FilePath")
{
filePathName = xmlReader.readElementText();
}
else if (xmlReader.name().toString() == "error")
{
// Get attributes of the error element
line = xmlReader.attributes().value("line").toInt();
start = xmlReader.attributes().value("start").toInt();
end = xmlReader.attributes().value("end").toInt();
// Read the error message
errorMessage = xmlReader.readElementText();
}
}
}
file.close();
// Handle any parsing errors
if (xmlReader.hasError())
{
qWarning() << "Error while parsing XML:" << xmlReader.errorString();
return;
}
// Print the parsed data
qDebug() << "Message ID:" << messageId;
qDebug() << "File Path:" << filePathName;
qDebug() << "Error Message:" << errorMessage;
qDebug() << "Line:" << line;
qDebug() << "Start:" << start;
qDebug() << "End:" << end;
// Check for specific message ID
if (messageId == "LuaRuntimeErrors")
{
this->ptrLuaScriptController->slot_sendLuaScriptError(filePathName, errorMessage, line, start, end);
}
else if (messageId == "LuaScriptPath")
{
// Handle the Lua script path
qDebug() << "Received Lua script path:" << filePathName;
// If NOWALuaScript.exe is opened, a message is send out from NOWA-Design with a lua path. Add it!
this->ptrLuaScriptController->slot_createLuaScript(filePathName);
}
#if 1
// Delete the file after reading
if (QFile::remove(filePath))
{
qDebug() << "XML file deleted successfully.";
}
else
{
qWarning() << "Failed to delete the XML file.";
}
#endif
}
bool AppCommunicator::isInstanceRunning(void)
{
// Define the path for the "NOWALuaScript.running" file
AppCommunicator::runningFilePath = QString(TARGET_PATH) + "/NOWALuaScript/bin/NOWALuaScript.running";
#if 0
QFile runningFile(AppCommunicator::runningFilePath);
if (runningFile.open(QIODevice::ReadOnly))
{
return true;
}
// Create the file when the application starts
if (runningFile.open(QIODevice::WriteOnly))
{
runningFile.write("Instance is running");
runningFile.close();
}
else
{
qWarning() << "Failed to create running file at" << AppCommunicator::runningFilePath;
}
return false;
#endif
QLocalSocket socket;
socket.connectToServer(AppCommunicator::serverName);
if (socket.waitForConnected(100))
{
// Another instance is running
socket.disconnectFromServer();
return true;
}
return false;
}
QString AppCommunicator::getRunningFilePath(void) const
{
return AppCommunicator::runningFilePath;
}
void AppCommunicator::deleteCommunicationFile(void)
{
QString communicationFile = QString(TARGET_PATH) + "/NOWALuaScript/bin/lua_script_data.xml";
// Delete the file after reading
if (QFile::remove(communicationFile))
{
qDebug() << "Commuication XML file deleted successfully.";
}
}
void AppCommunicator::deleteRunningFile(void)
{
QString watchDirectory = QString(TARGET_PATH) + "/NOWALuaScript/bin/";
QDir dir(watchDirectory);
// Check if the directory exists
if (dir.exists())
{
// List all files with ".xml" extension in the directory
QStringList xmlFiles = dir.entryList(QStringList() << "*.xml", QDir::Files);
// Remove each XML file
for (const QString& file : xmlFiles)
{
QString filePath = dir.filePath(file);
QFile::remove(filePath);
}
}
// Delete the file after reading
if (QFile::remove(AppCommunicator::runningFilePath))
{
qDebug() << "Running file deleted successfully.";
}
}
void AppCommunicator::sendFileToRunningInstance(const QString& filePath)
{
QLocalSocket socket;
socket.connectToServer(AppCommunicator::serverName);
if (socket.waitForConnected(100))
{
QTextStream stream(&socket);
stream << filePath << Qt::endl;
socket.flush();
socket.waitForBytesWritten();
qDebug() << "Sent file path to running instance:" << filePath;
socket.disconnectFromServer();
}
else
{
qDebug() << "Failed to connect to running instance";
}
}
void AppCommunicator::handleNewConnection()
{
QLocalSocket *clientConnection = localServer->nextPendingConnection();
if (!clientConnection)
{
return;
}
connect(clientConnection, &QLocalSocket::disconnected, clientConnection, &QLocalSocket::deleteLater);
clientConnection->waitForReadyRead(100); // Ensure the message is received
QString filePath = QString::fromUtf8(clientConnection->readAll()).trimmed();
if (!filePath.isEmpty())
{
qDebug() << "Received file via IPC:" << filePath;
// Send to LuaScriptController
Q_EMIT fileReceived(filePath);
}
clientConnection->disconnectFromServer();
}