|
@@ -12,8 +12,9 @@
|
|
|
#include <thread>
|
|
|
#include <unistd.h>
|
|
|
|
|
|
-#include "config.h"
|
|
|
-#include "qmlhandler.h"
|
|
|
+#include "../include/config.h"
|
|
|
+#include "../include/jsonhandler.h"
|
|
|
+#include "../include/qmlhandler.h"
|
|
|
#include <boost/asio.hpp>
|
|
|
#include <boost/filesystem.hpp>
|
|
|
#include <boost/lexical_cast.hpp>
|
|
@@ -100,141 +101,6 @@ std::vector<std::string> tokenizeByNewlines(std::string in) {
|
|
|
|
|
|
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();
|
|
|
- qInfo() << QString::fromStdString("Received command " + cmd);
|
|
|
-
|
|
|
- if (!cmd.compare("status")) {
|
|
|
- emit footerSetStatus(root["response"].asString().c_str());
|
|
|
- }
|
|
|
-
|
|
|
- else if (!cmd.compare("close")) {
|
|
|
- programActive = false;
|
|
|
- }
|
|
|
-
|
|
|
- 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()), boost::filesystem::exists(fileNames[i].asString()));
|
|
|
- }
|
|
|
- } else {
|
|
|
- emit log(root["error"].asString().c_str());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- else if (!cmd.compare("connect")) {
|
|
|
- if (root["accept"].asBool()) {
|
|
|
- } else {
|
|
|
- emit ipPopupSetStatus(root["error"].asString().c_str());
|
|
|
- closeCLI();
|
|
|
- emit ipPopupEnableConnectButton();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- else if (!cmd.compare("version")) {
|
|
|
- if (root["accept"] == true) {
|
|
|
- emit ipPopupClose();
|
|
|
- emit loginSignupPopupOpen();
|
|
|
- } else {
|
|
|
- QString errorMessage = QString::fromStdString(
|
|
|
- string("Version mismatch: \nClient: " + root["clientversion"].asString() + "\nServer: " + root["serverversion"].asString()));
|
|
|
- emit ipPopupSetStatus(errorMessage);
|
|
|
- closeCLI();
|
|
|
- emit ipPopupEnableConnectButton();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- else if (!cmd.compare("login")) {
|
|
|
- if (root["accept"] == true) {
|
|
|
- emit loginSignupPopupClose();
|
|
|
- write(outpipefd[1], "list\n", strlen("list\n"));
|
|
|
- loadSettingsToGUI();
|
|
|
- } else {
|
|
|
- emit loginSetStatus(root["error"].asString().c_str());
|
|
|
- reopenCLI(_IP);
|
|
|
- emit loginEnableLoginButton();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- else if (!cmd.compare("signup")) {
|
|
|
- if (root["accept"] == true) {
|
|
|
- emit loginSignupPopupClose();
|
|
|
- } else {
|
|
|
- emit signupSetStatus(root["error"].asString().c_str());
|
|
|
- reopenCLI(_IP);
|
|
|
- emit signupEnableRegisterButton();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- 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 {
|
|
|
- string fileName = root["file"].asString();
|
|
|
- // TODO: Only do this in getdata when remaining is 0 (when the file is fully downloaded) - maybe set text to "downloading.." in between
|
|
|
- emit receivingDisableDownloadButton(QString::fromStdString(fileName));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- else if (!cmd.compare("getdata")) {
|
|
|
- // TODO: Show speed and handle Error
|
|
|
- }
|
|
|
-
|
|
|
- else if (!cmd.compare("deleteme")) {
|
|
|
- if (root["accept"] == true) {
|
|
|
- _RESTART = true;
|
|
|
- emit closeWindow();
|
|
|
- } else {
|
|
|
- QString errorMessage = QString::fromStdString(root["error"].asString());
|
|
|
- emit deleteMePopupSetStatus(errorMessage);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- else if (!cmd.compare("deletefile")) {
|
|
|
- emit receivingCloseConfirmDeletePopup();
|
|
|
- if (root["accept"] == false) {
|
|
|
- QString errorMessage = QString::fromStdString(string("Error when deleting file " + root["file"].asString() + ":\n" + root["error"].asString()));
|
|
|
- emit log(errorMessage);
|
|
|
- } else {
|
|
|
- QString message = QString::fromStdString(string("Deleted file " + root["file"].asString() + " from the server!"));
|
|
|
- emit log(message);
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
// 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
|
|
@@ -266,7 +132,8 @@ void QMLHandler::readPipeLoop() {
|
|
|
for (string s : inputs) {
|
|
|
emit log(QString::fromStdString(s));
|
|
|
qInfo() << QString::fromStdString(s);
|
|
|
- handleJSON(s);
|
|
|
+ // handleJSON(s);
|
|
|
+ JsonHandler::parseJSON(s);
|
|
|
}
|
|
|
pipeInput = string();
|
|
|
memset(buf, 0, 1025);
|
|
@@ -448,3 +315,11 @@ void QMLHandler::onSignupRegisterButton(QString username, QString passwordOne, Q
|
|
|
|
|
|
// Footer
|
|
|
void QMLHandler::onFooterGetStatusButton() { write(outpipefd[1], "status\n", strlen("status\n")); }
|
|
|
+
|
|
|
+void QMLHandler::setProgramActive(bool active) { programActive = active; }
|
|
|
+
|
|
|
+void QMLHandler::writeToCLI(QString command) { write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData())); }
|
|
|
+
|
|
|
+QString QMLHandler::getIP() { return _IP; }
|
|
|
+
|
|
|
+void QMLHandler::setRestart(bool restart) { _RESTART = restart; }
|