qmlhandler.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #include <QDebug>
  2. #include <QGuiApplication>
  3. #include <csignal>
  4. #include <cstdio>
  5. #include <cstdlib>
  6. #include <iostream>
  7. #include <poll.h>
  8. #include <string>
  9. #include <sys/prctl.h>
  10. #include <sys/stat.h>
  11. #include <sys/wait.h>
  12. #include <thread>
  13. #include <unistd.h>
  14. #include "../include/climanager.h"
  15. #include "../include/cmdmanager.h"
  16. #include "../include/config.h"
  17. #include "../include/jsonhandler.h"
  18. #include "../include/qmlhandler.h"
  19. #include <iostream>
  20. #include <json/json.h>
  21. using namespace std;
  22. QUrl sendFileUrl = QUrl("");
  23. bool _RESTART = false;
  24. QMLHandler::QMLHandler(QObject *parent) : QObject(parent) {}
  25. void QMLHandler::loadSettingsToGUI() {
  26. stringstream ss;
  27. // The space is needed to get the stringstream to perform proper formatting of multiple values
  28. bool saveIP, saveUsername;
  29. ss << Config::getValue("Autofill-IP") << " " << Config::getValue("Autofill-Username");
  30. ss >> saveIP >> saveUsername;
  31. QString cliPath = QString::fromStdString(Config::getValue("CLI-Path"));
  32. QString keyPath = QString::fromStdString(Config::getValue("Keyfile-Path"));
  33. emit loadSettings(saveIP, saveUsername, cliPath, keyPath);
  34. }
  35. // ### QML Handlers ###
  36. // Main
  37. void QMLHandler::onStart(bool startWithCli) {
  38. bool configExists = Config::loadFile();
  39. if (configExists) {
  40. // Config exists
  41. if (Config::checkConfig()) {
  42. // Config is valid
  43. if (Config::getValue("Autofill-IP") == "1") {
  44. emit ipPopupSetIP(QString::fromStdString(Config::getValue("Default-IP")));
  45. emit ipPopupCheckSaveCheckbox();
  46. }
  47. if (Config::getValue("Autofill-Username") == "1") {
  48. emit loginSetUsername(QString::fromStdString(Config::getValue("Default-Username")));
  49. emit loginSignupCheckSaveCheckbox();
  50. }
  51. if (!ifstream(Config::getValue("CLI-Path"))) {
  52. // Invalid CLI Path
  53. emit invalidCliPathPopupOpen();
  54. } else if (startWithCli) {
  55. // Everything OK, init CLI
  56. CliManager::init();
  57. }
  58. } else {
  59. // Config is invalid
  60. emit invalidConfigPopupOpen();
  61. }
  62. } else {
  63. // Config doesn't exist
  64. Config::setupDefaultConfig();
  65. emit noConfigFoundPopupOpen();
  66. }
  67. }
  68. void QMLHandler::onSwitchServer() { CliManager::writeToCli("disconnect"); }
  69. // No Config Found Popup
  70. void QMLHandler::onNoConfigFoundPopupContinueButton(QString cli_path) {
  71. Config::setValue("CLI-Path", cli_path.toUtf8().constData());
  72. Config::saveFile();
  73. onStart(true);
  74. }
  75. // Invalid Cli Path Popup
  76. void QMLHandler::onInvalidCliPathPopupContinueButton(QString cli_path) {
  77. Config::setValue("CLI-Path", cli_path.toUtf8().constData());
  78. Config::saveFile();
  79. onStart(true);
  80. }
  81. void QMLHandler::onInvalidCliPathPopupQuitButton() { emit closeWindow(); }
  82. // Invalid Config Popup
  83. void QMLHandler::onInvalidConfigPopupQuitButton() { emit closeWindow(); }
  84. void QMLHandler::onInvalidConfigPopupCreateDefaultButton() {
  85. Config::setupDefaultConfig();
  86. emit noConfigFoundPopupOpen();
  87. }
  88. // Server Files
  89. void QMLHandler::onServerFilesSelectFileButton(QUrl url) {
  90. sendFileUrl = url.toLocalFile();
  91. emit log("File Selected: " + sendFileUrl.toString());
  92. emit serverFilesSetFileUrlText(sendFileUrl.toString());
  93. emit serverFilesEnableSendButton();
  94. }
  95. void QMLHandler::onServerFilesSendFileButton() {
  96. QString command = "put \"" + sendFileUrl.toString() + "\"";
  97. CliManager::writeToCli(command);
  98. }
  99. void QMLHandler::onServerFilesClearSelectionButton() {
  100. sendFileUrl = QUrl("");
  101. emit log("Cleared Selection");
  102. emit serverFilesSetFileUrlText("None");
  103. emit serverFilesDisableSendButton();
  104. }
  105. void QMLHandler::onServerFilesDownloadFileButton(QString fileName) {
  106. QString command = "get \"" + fileName + "\"";
  107. CliManager::writeToCli(command);
  108. }
  109. void QMLHandler::onServerFilesConfirmDeleteFileButton(QString fileName) {
  110. QString command = "deletefile \"" + fileName + "\"";
  111. CliManager::writeToCli(command);
  112. }
  113. // Messages
  114. void QMLHandler::onMessagesSendButton(QString msg) { emit message(msg); }
  115. // Settings
  116. void QMLHandler::onKeyfileSelected(QString path) {
  117. QString command = "keyfile \"" + path + "\"";
  118. CliManager::writeToCli(command);
  119. }
  120. void QMLHandler::onKeyfileClosed() { CliManager::writeToCli("closekey"); }
  121. void QMLHandler::onSettingsDeleteMeButton(QString password) {
  122. QString command = "deleteme " + password;
  123. CliManager::writeToCli(command);
  124. }
  125. void QMLHandler::onSettingsRevertChangesButton() {
  126. Config::loadFile();
  127. loadSettingsToGUI();
  128. emit log("Settings changes reverted.");
  129. }
  130. void QMLHandler::onSettingsResetButton() {
  131. string cli_path = Config::getValue("CLI-Path");
  132. Config::setupDefaultConfig();
  133. Config::setValue("CLI-Path", cli_path);
  134. loadSettingsToGUI();
  135. emit log("Settings resetted to default.");
  136. }
  137. void QMLHandler::onSettingsSaveButton(bool saveIP, bool saveUsername, QString cliPath, QString keyPath) {
  138. Config::setValue("Autofill-IP", to_string(saveIP));
  139. Config::setValue("Autofill-Username", to_string(saveUsername));
  140. Config::setValue("CLI-Path", cliPath.toUtf8().constData());
  141. Config::setValue("Keyfile-Path", keyPath.toUtf8().constData());
  142. Config::saveFile();
  143. emit log("Settings saved.");
  144. }
  145. // Ip Popup
  146. void QMLHandler::onIpPopupConnectButton(QString ip, bool saveAsDefault) {
  147. QStringList ipport = ip.split(":");
  148. QString command = "connect " + ipport[0];
  149. CmdManager::setCachedIP(ipport[0]);
  150. if (ipport.size() > 1) {
  151. command += " " + ipport[1];
  152. CmdManager::setCachedPort(ipport[1]);
  153. }
  154. CliManager::writeToCli(command);
  155. emit ipPopupDisableConnectButton();
  156. if (saveAsDefault) {
  157. Config::setValue("Default-IP", ip.toUtf8().constData());
  158. Config::setValue("Autofill-IP", "1");
  159. Config::saveFile();
  160. loadSettingsToGUI();
  161. }
  162. }
  163. // Login
  164. void QMLHandler::onLoginLoginButton(QString username, QString password, bool saveAsDefault) {
  165. QString command = "login " + username + " " + password;
  166. CliManager::writeToCli(command);
  167. emit loginDisableLoginButton();
  168. if (saveAsDefault) {
  169. Config::setValue("Default-Username", username.toUtf8().constData());
  170. Config::setValue("Autofill-Username", "1");
  171. Config::saveFile();
  172. loadSettingsToGUI();
  173. }
  174. }
  175. // Signup
  176. void QMLHandler::onSignupRegisterButton(QString username, QString passwordOne, QString passwordTwo, bool saveAsDefault) {
  177. if (QString::compare(passwordOne, passwordTwo, Qt::CaseSensitive)) {
  178. emit signupSetStatus("Passwords don't match");
  179. return;
  180. }
  181. QString command = "signup " + username + " " + passwordOne;
  182. CliManager::writeToCli(command);
  183. emit signupDisableRegisterButton();
  184. if (saveAsDefault) {
  185. Config::setValue("Default-Username", username.toUtf8().constData());
  186. Config::setValue("Autofill-Username", "1");
  187. Config::saveFile();
  188. loadSettingsToGUI();
  189. }
  190. }
  191. // Notifications
  192. void QMLHandler::onDismissNotificationButton(int index) { emit dismissNotification(index); }
  193. // Queueing
  194. void QMLHandler::onReceivingQueueFileButton(QString fileName) {
  195. QString command = "queue " + fileName;
  196. CliManager::writeToCli(command);
  197. }
  198. void QMLHandler::onReceivingDequeueFileButton(QString fileName) {
  199. QString command = "dequeue " + fileName;
  200. CliManager::writeToCli(command);
  201. }
  202. void QMLHandler::setRestart(bool restart) { _RESTART = restart; }