|
@@ -20,39 +20,67 @@ using namespace std;
|
|
|
|
|
|
int inpipefd[2];
|
|
|
int outpipefd[2];
|
|
|
-char buf[1024];
|
|
|
+char buf[1025];
|
|
|
QUrl sendFileUrl = QUrl("");
|
|
|
|
|
|
bool programActive = true;
|
|
|
|
|
|
+QMLHandler::QMLHandler(QObject *parent) : QObject(parent) {}
|
|
|
+
|
|
|
void QMLHandler::onExit() {
|
|
|
write(outpipefd[1], "disconnect\n", strlen("disconnect\n"));
|
|
|
}
|
|
|
|
|
|
+// This method gets a string and tries to read it as Json
|
|
|
+// If it fails to do so, return and do nothing, else handle the content
|
|
|
void QMLHandler::handleJSON(string buffer) {
|
|
|
Json::Value root;
|
|
|
Json::CharReaderBuilder builder;
|
|
|
Json::CharReader *reader = builder.newCharReader();
|
|
|
string jsonError;
|
|
|
|
|
|
+ // Try to parse the string as Json and store the result of the pasring in a
|
|
|
+ // boolean
|
|
|
bool parsingSuccessful = reader->parse(
|
|
|
buffer.c_str(), buffer.c_str() + buffer.size(), &root, &jsonError);
|
|
|
|
|
|
+ // If the string is not correct Json, return
|
|
|
if (!parsingSuccessful) {
|
|
|
return;
|
|
|
}
|
|
|
+
|
|
|
const Json::Value command = root["command"];
|
|
|
string cmd = command.asString();
|
|
|
|
|
|
- if (cmd == "status") {
|
|
|
+ if (cmd.compare("status")) {
|
|
|
emit footerSetStatus(root["response"].asString().c_str());
|
|
|
}
|
|
|
|
|
|
- else if (cmd == "close") {
|
|
|
+ else if (cmd.compare("close")) {
|
|
|
programActive = false;
|
|
|
}
|
|
|
+
|
|
|
+ else if (cmd.compare("listdata")) {
|
|
|
+ emit receivingClearFileList();
|
|
|
+ // Get the array of file Names
|
|
|
+ auto fileNames = root["names"];
|
|
|
+ for (int i = 0; i < fileNames.size(); i++) {
|
|
|
+ emit receivingListFile(
|
|
|
+ QString::fromStdString(fileNames[i].asString().c_str()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ else if (cmd.compare("version")) {
|
|
|
+ // TODO: Change hardcoded login details to input values
|
|
|
+ write(outpipefd[1], "user\n", strlen("user\n"));
|
|
|
+ write(outpipefd[1], "pass\n", strlen("pass\n"));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+// This method is a loop which runs in a seperate thread.
|
|
|
+// If will read the Input Pipe, which containts the string that get printed
|
|
|
+// on stdout by the CLI/Server, and calls handleJSON with the read content
|
|
|
+// one it reads a newline.
|
|
|
void QMLHandler::readPipeLoop() {
|
|
|
unsigned int readOffset = 0;
|
|
|
unsigned int pollCount = 0;
|
|
@@ -66,18 +94,22 @@ void QMLHandler::readPipeLoop() {
|
|
|
if (inPipeStatus.revents & POLLIN) {
|
|
|
readOffset += read(inpipefd[0], buf + readOffset, 1);
|
|
|
|
|
|
+ if(buf[readOffset-1] == '\n') {
|
|
|
+ pollCount = 10;
|
|
|
+ } else {
|
|
|
+ pollCount = 0;
|
|
|
+ }
|
|
|
+
|
|
|
pollCount = 0;
|
|
|
|
|
|
} else {
|
|
|
pollCount++;
|
|
|
}
|
|
|
|
|
|
- if (pollCount > 9 && buf[0]) {
|
|
|
- buf[1023] = 0;
|
|
|
- buf[strlen(buf)] = 0;
|
|
|
+ if (pollCount > 9 && readOffset > 0) {
|
|
|
+ buf[strnlen(buf, 1024)] = 0;
|
|
|
|
|
|
- string cleanBuffer = buf + strcspn(buf, "\n") + 1;
|
|
|
- string receivedData = cleanBuffer.substr(0, cleanBuffer.size() - 1);
|
|
|
+ string receivedData = buf;
|
|
|
|
|
|
emit log(QString::fromStdString(receivedData));
|
|
|
qInfo() << QString::fromStdString(receivedData);
|
|
@@ -86,10 +118,18 @@ void QMLHandler::readPipeLoop() {
|
|
|
pollCount = 0;
|
|
|
readOffset = 0;
|
|
|
}
|
|
|
+
|
|
|
+ // Fixme
|
|
|
+ if (readOffset >= 1024) {
|
|
|
+ qInfo() << "Fixme: QMLHandler::readPipeLoop() readOffset too high!!!";
|
|
|
+ readOffset = 0;
|
|
|
+ pollCount = 0;
|
|
|
+ memset(buf, 0, 1025);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-QMLHandler::QMLHandler(QObject *parent) : QObject(parent) {}
|
|
|
+// ### QML Handlers ###
|
|
|
|
|
|
// Sending
|
|
|
void QMLHandler::onSendingSelectFileButton(QUrl url) {
|
|
@@ -105,7 +145,18 @@ void QMLHandler::onSendingSendFileButton() {
|
|
|
strlen(command.toUtf8().constData()));
|
|
|
}
|
|
|
|
|
|
+void QMLHandler::onSendingClearSelectionButton() {
|
|
|
+ sendFileUrl = QUrl("");
|
|
|
+ emit log("Cleared Selection");
|
|
|
+ emit sendingSetFileUrlText("Selected File: None");
|
|
|
+ emit sendingDisableSendButton();
|
|
|
+}
|
|
|
+
|
|
|
// Receiving
|
|
|
+void QMLHandler::onReceivingListFilesButton() {
|
|
|
+ write(outpipefd[1], "list\n", strlen("list\n"));
|
|
|
+}
|
|
|
+
|
|
|
void QMLHandler::onReceivingGetFileButton(QString fileName) {
|
|
|
QString command = "get " + fileName + "\n";
|
|
|
write(outpipefd[1], command.toUtf8().constData(),
|