Parcourir la source

Merge branch 'us18-cli-gui-protocol' into 'develop'

US18: Create CLI-GUI Protocol and apply GUI adjustments

See merge request tobias.wach/ccats!26
Sander, Paul il y a 5 ans
Parent
commit
856c9872db
4 fichiers modifiés avec 131 ajouts et 27 suppressions
  1. 1 1
      GUI-CLI Protocol.md
  2. 51 14
      gui/src/IpPopupForm.ui.qml
  3. 76 11
      gui/src/qmlhandler.cpp
  4. 3 1
      gui/src/qmlhandler.h

+ 1 - 1
GUI-CLI Protocol.md

@@ -178,7 +178,7 @@ If `accept` is `true` the connection is valid and the server can start sending t
 CLI:
 ```
 {
-	"command": "putdata",
+	"command": "getdata",
 	"file": string,
 	"speed": float,
 	"cancel": bool,

+ 51 - 14
gui/src/IpPopupForm.ui.qml

@@ -4,7 +4,7 @@ import QtQuick.Layouts 1.3
 
 Popup {
     id: popup
-    height: 200
+    height: 400
     dim: true
     clip: false
     width: 400
@@ -12,12 +12,22 @@ Popup {
     focus: true
     closePolicy: Popup.NoAutoClose
 
+    Connections {
+        target: _qmlHandler
+        onIpPopupSetStatus: {
+            ipPopupStatusText.text = status
+        }
+        onIpPopupClose: {
+            popup.close()
+        }
+    }
+
     ColumnLayout {
         anchors.fill: parent
 
         Text {
             Layout.alignment: Qt.AlignCenter
-            id: popupText
+            id: ipPopupTitle
             color: "#ffffff"
             text: qsTr("Enter the IP to connect:")
             horizontalAlignment: Text.AlignHCenter
@@ -27,23 +37,52 @@ Popup {
 
         TextField {
             Layout.alignment: Qt.AlignCenter
-            id: popupIpInput
+            id: ipPopupIpInput
             selectByMouse: true
             focus: true
             text: qsTr("")
-            placeholderText: "Enter IP"
+            placeholderText: "IP-Address"
             horizontalAlignment: Text.AlignHCenter
             validator: RegExpValidator {
                 regExp: /^(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))$/
             }
             // @disable-check M222
-            Keys.onReturnPressed: popupConnectButton.activate()
+            Keys.onReturnPressed: ipPopupConnectButton.activate()
             // @disable-check M222
-            Keys.onEnterPressed: popupConnectButton.activate()
+            Keys.onEnterPressed: ipPopupConnectButton.activate()
+        }
+
+        TextField {
+            Layout.alignment: Qt.AlignCenter
+            id: ipPopupUsernameInput
+            selectByMouse: true
+            focus: true
+            text: qsTr("")
+            placeholderText: "Username"
+            horizontalAlignment: Text.AlignHCenter
+            // @disable-check M222
+            Keys.onReturnPressed: ipPopupConnectButton.activate()
+            // @disable-check M222
+            Keys.onEnterPressed: ipPopupConnectButton.activate()
+        }
+
+        TextField {
+            Layout.alignment: Qt.AlignCenter
+            id: ipPopupPasswordInput
+            selectByMouse: true
+            focus: true
+            text: qsTr("")
+            placeholderText: "Password"
+            horizontalAlignment: Text.AlignHCenter
+            // @disable-check M222
+            Keys.onReturnPressed: ipPopupConnectButton.activate()
+            // @disable-check M222
+            Keys.onEnterPressed: ipPopupConnectButton.activate()
+            echoMode: TextInput.Password
         }
 
         Text {
-            id: popupStatusText
+            id: ipPopupStatusText
             color: "#df3f3f"
             text: qsTr("")
             horizontalAlignment: Text.AlignHCenter
@@ -54,24 +93,22 @@ Popup {
 
         Button {
             Layout.alignment: Qt.AlignCenter
-            id: popupConnectButton
+            id: ipPopupConnectButton
             text: qsTr("Connect")
-            enabled: popupIpInput.acceptableInput
+            enabled: (ipPopupIpInput.acceptableInput && ipPopupUsernameInput.text != "" && ipPopupPasswordInput.text != "")
             font.pointSize: 16
             // @disable-check M223
             onClicked: {
                 // @disable-check M222
-                popupConnectButton.activate()
+                ipPopupConnectButton.activate()
             }
 
             // @disable-check M222
             function activate() {
                 // @disable-check M223
-                if (popupIpInput.acceptableInput) {
-                    // @disable-check M222
-                    _qmlHandler.onIpPopupEnterIp(popupIpInput.text)
+                if (ipPopupConnectButton.enabled) {
                     // @disable-check M222
-                    popup.close()
+                    _qmlHandler.onIpPopupConnectButton(ipPopupIpInput.text, ipPopupUsernameInput.text, ipPopupUsernameInput.text)
                 }
             }
         }

+ 76 - 11
gui/src/qmlhandler.cpp

@@ -22,6 +22,8 @@ int inpipefd[2];
 int outpipefd[2];
 char buf[1025];
 QUrl sendFileUrl = QUrl("");
+QString _USERNAME;
+QString _PASSWORD;
 
 bool programActive = true;
 
@@ -60,20 +62,79 @@ void QMLHandler::handleJSON(string buffer) {
     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("list")) {
+    if (root["accept"] == true) {
+      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 {
+      emit log(root["error"].asString().c_str());
+    }
+  }
+
+  else if (cmd.compare("connect")) {
+    if (root["accept"] == false) {
+      emit ipPopupSetStatus(root["error"].asString().c_str());
+      close(outpipefd[1]);
+      close(inpipefd[0]);
     }
   }
 
   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"));
+    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 {
+      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 if (cmd.compare("login")) {
+    if (root["accept"] == true) {
+      emit ipPopupClose();
+    } else {
+      emit ipPopupSetStatus(root["error"].asString().c_str());
+      close(outpipefd[1]);
+      close(inpipefd[0]);
+    }
+  }
+
+  else if (cmd.compare("put")) {
+    if (root["accept"] == false) {
+      QString errorMessage = QString::fromStdString(
+          string("Error when uploading file " + root["file"].asString() +
+                 ":\n" + root["error"].asString()));
+      emit log(errorMessage);
+    }
+  }
+
+  else if (cmd.compare("putdata")) {
+    // TODO: Show speed and handle Error
+  }
+
+  else if (cmd.compare("get")) {
+    if (root["accept"] == false) {
+      QString errorMessage = QString::fromStdString(
+          string("Error when downloading file " + root["file"].asString() +
+                 ":\n" + root["error"].asString()));
+      emit log(errorMessage);
+    }
+  }
+
+  else if (cmd.compare("getdata")) {
+    // TODO: Show speed and handle Error
   }
 }
 
@@ -172,7 +233,8 @@ void QMLHandler::onSettingsSwitchServerButton() {
 }
 
 // Ip Popup
-void QMLHandler::onIpPopupEnterIp(QString ip) {
+void QMLHandler::onIpPopupConnectButton(QString ip, QString username,
+                                        QString password) {
   pid_t pid = 0;
 
   pipe(inpipefd);
@@ -198,6 +260,9 @@ void QMLHandler::onIpPopupEnterIp(QString ip) {
   // TODO: Not hardcoded
   emit footerSetStatus("Connected to " + ip);
 
+  _USERNAME = username;
+  _PASSWORD = password;
+
   close(outpipefd[0]);
   close(inpipefd[1]);
 

+ 3 - 1
gui/src/qmlhandler.h

@@ -37,6 +37,8 @@ signals:
   void settingsOpenSwitchServerPopup();
 
   // Ip Popup
+  void ipPopupSetStatus(QString status);
+  void ipPopupClose();
 
   // Switch Popup
 
@@ -63,7 +65,7 @@ public slots:
   void onSettingsSwitchServerButton();
 
   // Ip Popup
-  void onIpPopupEnterIp(QString ip);
+  void onIpPopupConnectButton(QString ip, QString username, QString password);
 
   // Switch Popup
   void onSwitchPopupEnterIp(QString ip);