瀏覽代碼

implemented signup logic

Cyamond 5 年之前
父節點
當前提交
6eec002d90
共有 6 個文件被更改,包括 114 次插入32 次删除
  1. 13 2
      gui/src/IpPopup.ui.qml
  2. 8 0
      gui/src/LoginForm.ui.qml
  3. 14 0
      gui/src/LoginSignupPopup.ui.qml
  4. 13 1
      gui/src/SignupForm.ui.qml
  5. 55 27
      gui/src/qmlhandler.cpp
  6. 11 2
      gui/src/qmlhandler.h

+ 13 - 2
gui/src/IpPopup.ui.qml

@@ -12,6 +12,19 @@ Popup {
     focus: true
     closePolicy: Popup.NoAutoClose
 
+    Connections {
+        target: _qmlHandler
+        onIpPopupClose: {
+            popup.close()
+        }
+        onIpPopupOpen: {
+          popup.open();
+        }
+        onIpPopupSetStatus: {
+            ipPopupStatusText.text = status
+        }
+    }
+
     ColumnLayout {
         anchors.fill: parent
 
@@ -72,8 +85,6 @@ Popup {
                 if (ipPopupIpInput.acceptableInput) {
                     // @disable-check M222
                     _qmlHandler.onIpPopupConnectButton(ipPopupIpInput.text)
-                    // @disable-check M222
-                    popup.close()
                 }
             }
         }

+ 8 - 0
gui/src/LoginForm.ui.qml

@@ -5,6 +5,14 @@ import QtQuick.Layouts 1.3
 Page {
     width: 400
     height: 400
+
+    Connections {
+        target: _qmlHandler
+        onLoginSetStatus: {
+            loginStatusText.text = status
+        }
+    }
+
     ColumnLayout {
         anchors.fill: parent
 

+ 14 - 0
gui/src/LoginSignupPopup.ui.qml

@@ -12,6 +12,16 @@ Popup {
     focus: true
     closePolicy: Popup.NoAutoClose
 
+    Connections {
+        target: _qmlHandler
+        onLoginSignupPopupClose: {
+            popup.close()
+        }
+        onLoginSignupPopupOpen: {
+          popup.open()
+        }
+    }
+
     Page {
         anchors.fill: parent
 
@@ -44,4 +54,8 @@ Popup {
             }
         }
     }
+
+    Component.onCompleted: {
+      swipeView.interactive = false
+    }
 }

+ 13 - 1
gui/src/SignupForm.ui.qml

@@ -5,6 +5,14 @@ import QtQuick.Layouts 1.3
 Page {
   width: 400
   height: 400
+
+  Connections {
+      target: _qmlHandler
+      onSignupSetStatus: {
+          signupStatusText.text = status
+      }
+  }
+
   ColumnLayout {
       anchors.fill: parent
 
@@ -45,6 +53,8 @@ Page {
           // @disable-check M222
           Keys.onEnterPressed: signupRegisterButton.activate()
           echoMode: TextInput.Password
+
+          onTextEdited: signupStatusText.text = ""
       }
 
       TextField {
@@ -60,6 +70,8 @@ Page {
           // @disable-check M222
           Keys.onEnterPressed: signupRegisterButton.activate()
           echoMode: TextInput.Password
+
+          onTextEdited: signupStatusText.text = ""
       }
 
       Text {
@@ -91,7 +103,7 @@ Page {
               // @disable-check M223
               if (signupRegisterButton.enabled) {
                   // @disable-check M222
-
+                  _qmlHandler.onSignupRegisterButton(signupUsernameInput.text, signupPasswordOneInput.text, signupPasswordTwoInput.text);
               }
           }
       }

+ 55 - 27
gui/src/qmlhandler.cpp

@@ -22,8 +22,8 @@ int inpipefd[2];
 int outpipefd[2];
 char buf[1025];
 QUrl sendFileUrl = QUrl("");
-QString _USERNAME;
-QString _PASSWORD;
+QString _IP = qsTr("");
+bool _CLI_RUNNING = false;
 
 bool programActive = true;
 
@@ -74,34 +74,38 @@ void QMLHandler::handleJSON(string buffer) {
   else if (cmd.compare("connect")) {
     if (root["accept"] == false) {
       emit ipPopupSetStatus(root["error"].asString().c_str());
-      close(outpipefd[1]);
-      close(inpipefd[0]);
+      reopenCLI(_IP);
     }
   }
 
   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()));
+      emit ipPopupClose();
+      emit loginSignupPopupOpen();
     } 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]);
+      reopenCLI(_IP);
     }
   }
 
   else if (cmd.compare("login")) {
     if (root["accept"] == true) {
-      emit ipPopupClose();
+      emit loginSignupPopupClose();
     } else {
-      emit ipPopupSetStatus(root["error"].asString().c_str());
-      close(outpipefd[1]);
-      close(inpipefd[0]);
+      emit loginSetStatus(root["error"].asString().c_str());
+      reopenCLI(_IP);
+    }
+  }
+
+  else if (cmd.compare("signup")) {
+    if (root["accept"] == true) {
+      emit loginSignupPopupClose();
+    } else {
+      emit loginSetStatus(root["error"].asString().c_str());
+      reopenCLI(_IP);
     }
   }
 
@@ -211,7 +215,38 @@ void QMLHandler::onSettingsSwitchServerButton() {
 }
 
 // Ip Popup
-void QMLHandler::onIpPopupConnectButton(QString ip) {
+void QMLHandler::onIpPopupConnectButton(QString ip) { reopenCLI(ip); }
+
+// Login
+void QMLHandler::onLoginLoginButton(QString username, QString password) {
+  QString command = "login " + username + " " + password + "\n";
+  write(outpipefd[1], command.toUtf8().constData(),
+        strlen(command.toUtf8().constData()));
+}
+
+// Signup
+void QMLHandler::onSignupRegisterButton(QString username, QString passwordOne,
+                                        QString passwordTwo) {
+  if (QString::compare(passwordOne, passwordTwo, Qt::CaseSensitive)) {
+    emit signupSetStatus("Passwords don't match");
+    return;
+  }
+  QString command = "signup " + username + " " + passwordOne + "\n";
+  write(outpipefd[1], command.toUtf8().constData(),
+        strlen(command.toUtf8().constData()));
+}
+
+// Footer
+void QMLHandler::onFooterGetStatusButton() {
+  write(outpipefd[1], "status\n", strlen("status\n"));
+}
+
+void QMLHandler::reopenCLI(QString ip) {
+  if (_CLI_RUNNING) {
+    closeCLI();
+  }
+
+  _IP = ip;
   pid_t pid = 0;
 
   pipe(inpipefd);
@@ -231,25 +266,18 @@ void QMLHandler::onIpPopupConnectButton(QString ip) {
     execl("../../cli/build/ccats-cli", "ccats-cli", ip.toUtf8().constData(),
           "--machine", (char *)NULL);
 
+    _CLI_RUNNING = true;
+
     exit(1);
   }
 
-  // TODO: Not hardcoded
-  emit footerSetStatus("Connected to " + ip);
-
   close(outpipefd[0]);
   close(inpipefd[1]);
 
   std::thread(&QMLHandler::readPipeLoop, this).detach();
 }
 
-// Login
-void QMLHandler::onLoginLoginButton(QString username, QString password) {}
-
-// Signup
-void QMLHandler::onSignupRegisterButton(QString username, QString password) {}
-
-// Footer
-void QMLHandler::onFooterGetStatusButton() {
-  write(outpipefd[1], "status\n", strlen("status\n"));
+void QMLHandler::closeCLI() {
+  close(outpipefd[1]);
+  close(inpipefd[0]);
 }

+ 11 - 2
gui/src/qmlhandler.h

@@ -39,8 +39,17 @@ signals:
   // Ip Popup
   void ipPopupSetStatus(QString status);
   void ipPopupClose();
+  void ipPopupOpen();
 
-  // Switch Popup
+  // Login Signup Popup
+  void loginSignupPopupClose();
+  void loginSignupPopupOpen();
+
+  // Login
+  void loginSetStatus(QString status);
+
+  // Signup
+  void signupSetStatus(QString status);
 
   // Footer
   void log(QString logText);
@@ -71,7 +80,7 @@ public slots:
   void onLoginLoginButton(QString username, QString password);
 
   // Signup
-  void onSignupRegisterButton(QString username, QString password);
+  void onSignupRegisterButton(QString username, QString passwordOne, QString passwordTwo);
 
   // Footer
   void onFooterGetStatusButton();