Ver Fonte

Merge branch 'us26-gui-login' into 'develop'

US26: GUI Login

See merge request tobias.wach/ccats!24
Sander, Paul há 5 anos atrás
pai
commit
740bbd7fa9
2 ficheiros alterados com 43 adições e 20 exclusões
  1. 1 1
      gui/src/IpPopupForm.ui.qml
  2. 42 19
      gui/src/qmlhandler.cpp

+ 1 - 1
gui/src/IpPopupForm.ui.qml

@@ -108,7 +108,7 @@ Popup {
                 // @disable-check M223
                 if (ipPopupConnectButton.enabled) {
                     // @disable-check M222
-                    _qmlHandler.onIpPopupConnectButton(ipPopupIpInput.text, ipPopupUsernameInput.text, ipPopupUsernameInput.text)
+                    _qmlHandler.onIpPopupConnectButton(ipPopupIpInput.text, ipPopupUsernameInput.text, ipPopupPasswordInput.text)
                 }
             }
         }

+ 42 - 19
gui/src/qmlhandler.cpp

@@ -29,6 +29,20 @@ bool programActive = true;
 
 QMLHandler::QMLHandler(QObject *parent) : QObject(parent) {}
 
+std::vector<std::string> tokenizeByNewlines(std::string in) {
+  vector<string> res;
+  size_t index;
+  for (index = in.find("\n"); index != std::string::npos;
+       index = in.find("\n")) {
+    if (index != 0)
+      res.push_back(in.substr(0, index));
+    in = in.substr(index + 1);
+  }
+  if (in.length() > 0)
+    res.push_back(in);
+  return res;
+}
+
 void QMLHandler::onExit() {
   write(outpipefd[1], "disconnect\n", strlen("disconnect\n"));
 }
@@ -53,6 +67,7 @@ void QMLHandler::handleJSON(string buffer) {
 
   const Json::Value command = root["command"];
   string cmd = command.asString();
+  qInfo() << QString::fromStdString("Received command " + cmd);
 
   if (cmd.compare("status")) {
     emit footerSetStatus(root["response"].asString().c_str());
@@ -77,32 +92,34 @@ void QMLHandler::handleJSON(string buffer) {
     }
   }
 
-  else if (cmd.compare("connect")) {
-    if (root["accept"] == false) {
+  else if (cmd == "connect") {
+    if (root["accept"].asBool()) {
+    } else {
       emit ipPopupSetStatus(root["error"].asString().c_str());
       close(outpipefd[1]);
       close(inpipefd[0]);
     }
   }
 
-  else if (cmd.compare("version")) {
-    if (root["accept"] == true) {
-      write(outpipefd[1], _USERNAME.toUtf8().constData(),
-            strlen(_USERNAME.toUtf8().constData()));
-      write(outpipefd[1], _PASSWORD.toUtf8().constData(),
-            strlen(_PASSWORD.toUtf8().constData()));
-    } else {
+  else if (cmd == "version") {
+    if (root["accept"].asBool() == false) {
       QString errorMessage = QString::fromStdString(string(
           "Version mismatch: \nClient: " + root["clientversion"].asString() +
           "\nServer: " + root["serverversion"].asString()));
       emit ipPopupSetStatus(errorMessage);
       close(outpipefd[1]);
       close(inpipefd[0]);
+    } else {
+
+      write(outpipefd[1], (_USERNAME + "\n").toUtf8().constData(),
+            strlen((_USERNAME + "\n").toUtf8().constData()));
+      write(outpipefd[1], (_PASSWORD + "\n").toUtf8().constData(),
+            strlen((_PASSWORD + "\n").toUtf8().constData()));
     }
   }
 
-  else if (cmd.compare("login")) {
-    if (root["accept"] == true) {
+  else if (cmd == "login") {
+    if (root["accept"].asBool() == true) {
       emit ipPopupClose();
     } else {
       emit ipPopupSetStatus(root["error"].asString().c_str());
@@ -148,8 +165,10 @@ void QMLHandler::readPipeLoop() {
   struct pollfd inPipeStatus;
   inPipeStatus.fd = inpipefd[0];
   inPipeStatus.events = POLLIN;
+  std::vector<std::string> inputs;
 
   while (programActive) {
+    inputs = std::vector<std::string>();
     poll(&inPipeStatus, 1, 100);
 
     if (inPipeStatus.revents & POLLIN) {
@@ -167,14 +186,18 @@ void QMLHandler::readPipeLoop() {
       pollCount++;
     }
 
-    if (pollCount > 9 && readOffset > 0) {
-      buf[strnlen(buf, 1024)] = 0;
-
-      string receivedData = buf;
-
-      emit log(QString::fromStdString(receivedData));
-      qInfo() << QString::fromStdString(receivedData);
-      handleJSON(receivedData);
+    if (pollCount > 9 && buf[0]) {
+      buf[1024] = 0;
+      buf[strlen(buf)] = 0;
+      inputs = tokenizeByNewlines(string(buf));
+      for (string s : inputs) {
+        //~ string cleanBuffer = buf + strcspn(buf, "\n") + 1;
+        //~ string receivedData = cleanBuffer.substr(0, cleanBuffer.size() - 1);
+
+        emit log(QString::fromStdString(s));
+        qInfo() << QString::fromStdString(s);
+        handleJSON(s);
+      }
       memset(buf, 0, 1024);
       pollCount = 0;
       readOffset = 0;