cmdmanager.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include <QDebug>
  2. #include <QGuiApplication>
  3. #include "../include/cmdmanager.h"
  4. #include "../include/config.h"
  5. #include <boost/asio.hpp>
  6. #include <boost/filesystem.hpp>
  7. #include <boost/lexical_cast.hpp>
  8. #include <json/json.h>
  9. using boost::lexical_cast;
  10. using boost::asio::buffer;
  11. using namespace std;
  12. namespace CmdManager {
  13. QMLHandler *qmlHandler;
  14. map<string, void (*)(Json::Value root)> cmdmap;
  15. } // namespace CmdManager
  16. void CmdManager::init() {
  17. cmdmap["status"] = &CmdManager::handleStatus;
  18. cmdmap["close"] = &CmdManager::handleClose;
  19. cmdmap["list"] = &CmdManager::handleList;
  20. cmdmap["connect"] = &CmdManager::handleConnect;
  21. cmdmap["version"] = &CmdManager::handleVersion;
  22. cmdmap["login"] = &CmdManager::handleLogin;
  23. cmdmap["signup"] = &CmdManager::handleSignup;
  24. cmdmap["put"] = &CmdManager::handlePut;
  25. cmdmap["putdata"] = &CmdManager::handlePutData;
  26. cmdmap["get"] = &CmdManager::handleGet;
  27. cmdmap["getdata"] = &CmdManager::handleGetData;
  28. cmdmap["deleteme"] = &CmdManager::handleDeleteMe;
  29. cmdmap["deletefile"] = &CmdManager::handleDeleteFile;
  30. }
  31. void CmdManager::setQmlHandler(QMLHandler *q) { qmlHandler = q; }
  32. void CmdManager::executeCmd(string cmd, Json::Value root) { cmdmap[cmd](root); }
  33. void CmdManager::handleStatus(Json::Value root) { emit qmlHandler->footerSetStatus(root["response"].asString().c_str()); }
  34. void CmdManager::handleClose(Json::Value root) { qmlHandler->setProgramActive(false); }
  35. void CmdManager::handleList(Json::Value root) {
  36. if (root["accept"] == true) {
  37. emit qmlHandler->receivingClearFileList();
  38. // Get the array of file Names
  39. auto fileNames = root["names"];
  40. for (int i = 0; i < fileNames.size(); i++) {
  41. emit qmlHandler->receivingListFile(QString::fromStdString(fileNames[i].asString().c_str()), boost::filesystem::exists(fileNames[i].asString()));
  42. }
  43. } else {
  44. emit qmlHandler->log(root["error"].asString().c_str());
  45. }
  46. }
  47. void CmdManager::handleConnect(Json::Value root) {
  48. if (!root["accept"].asBool()) {
  49. emit qmlHandler->ipPopupSetStatus(root["error"].asString().c_str());
  50. qmlHandler->closeCLI();
  51. emit qmlHandler->ipPopupEnableConnectButton();
  52. }
  53. }
  54. void CmdManager::handleVersion(Json::Value root) {
  55. if (root["accept"] == true) {
  56. emit qmlHandler->ipPopupClose();
  57. emit qmlHandler->loginSignupPopupOpen();
  58. } else {
  59. QString errorMessage =
  60. QString::fromStdString(string("Version mismatch: \nClient: " + root["clientversion"].asString() + "\nServer: " + root["serverversion"].asString()));
  61. emit qmlHandler->ipPopupSetStatus(errorMessage);
  62. qmlHandler->closeCLI();
  63. emit qmlHandler->ipPopupEnableConnectButton();
  64. }
  65. }
  66. void CmdManager::handleLogin(Json::Value root) {
  67. if (root["accept"] == true) {
  68. emit qmlHandler->loginSignupPopupClose();
  69. qmlHandler->writeToCLI("list\n");
  70. qmlHandler->loadSettingsToGUI();
  71. } else {
  72. emit qmlHandler->loginSetStatus(root["error"].asString().c_str());
  73. qmlHandler->reopenCLI(qmlHandler->getIP());
  74. emit qmlHandler->loginEnableLoginButton();
  75. }
  76. }
  77. void CmdManager::handleSignup(Json::Value root) {
  78. if (root["accept"] == true) {
  79. emit qmlHandler->loginSignupPopupClose();
  80. } else {
  81. emit qmlHandler->signupSetStatus(root["error"].asString().c_str());
  82. qmlHandler->reopenCLI(qmlHandler->getIP());
  83. emit qmlHandler->signupEnableRegisterButton();
  84. }
  85. }
  86. void CmdManager::handlePut(Json::Value root) {
  87. if (root["accept"] == false) {
  88. QString errorMessage = QString::fromStdString(string("Error when uploading file " + root["file"].asString() + ":\n" + root["error"].asString()));
  89. emit qmlHandler->log(errorMessage);
  90. }
  91. }
  92. void CmdManager::handlePutData(Json::Value root) {
  93. // TODO: Show speed and handle Error
  94. }
  95. void CmdManager::handleGet(Json::Value root) {
  96. if (root["accept"] == false) {
  97. QString errorMessage = QString::fromStdString(string("Error when downloading file " + root["file"].asString() + ":\n" + root["error"].asString()));
  98. emit qmlHandler->log(errorMessage);
  99. } else {
  100. string fileName = root["file"].asString();
  101. // TODO: Only do this in getdata when remaining is 0 (when the file is fully downloaded) - maybe set text to "downloading.." in between
  102. emit qmlHandler->receivingDisableDownloadButton(QString::fromStdString(fileName));
  103. }
  104. }
  105. void CmdManager::handleGetData(Json::Value root) {
  106. // TODO: Show speed and handle Error
  107. }
  108. void CmdManager::handleDeleteMe(Json::Value root) {
  109. if (root["accept"] == true) {
  110. qmlHandler->setRestart(true);
  111. emit qmlHandler->closeWindow();
  112. } else {
  113. QString errorMessage = QString::fromStdString(root["error"].asString());
  114. emit qmlHandler->deleteMePopupSetStatus(errorMessage);
  115. }
  116. }
  117. void CmdManager::handleDeleteFile(Json::Value root) {
  118. emit qmlHandler->receivingCloseConfirmDeletePopup();
  119. if (root["accept"] == false) {
  120. QString errorMessage = QString::fromStdString(string("Error when deleting file " + root["file"].asString() + ":\n" + root["error"].asString()));
  121. emit qmlHandler->log(errorMessage);
  122. } else {
  123. QString message = QString::fromStdString(string("Deleted file " + root["file"].asString() + " from the server!"));
  124. emit qmlHandler->log(message);
  125. }
  126. }