qmlhandler.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. bool configExists = false;
  25. QMLHandler::QMLHandler(QObject *parent) : QObject(parent) {}
  26. void QMLHandler::loadSettingsToGUI() {
  27. stringstream ss;
  28. // The space is needed to get the stringstream to perform proper formatting of multiple values
  29. bool saveIP, saveUsername;
  30. ss << Config::getValue("Autofill-IP") << " " << Config::getValue("Autofill-Username");
  31. ss >> saveIP >> saveUsername;
  32. QString cliPath = QString::fromStdString(Config::getValue("CLI-Path"));
  33. QString keyPath = QString::fromStdString(Config::getValue("Keyfile-Path"));
  34. emit loadSettings(saveIP, saveUsername, cliPath, keyPath);
  35. }
  36. // ### QML Handlers ###
  37. // Main
  38. void QMLHandler::onStart(bool startWithCli) {
  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. // CLI exists, check for SSL
  56. if (Config::getValue("Use-SSL") == "1") {
  57. if (!ifstream(Config::getValue("SSL-Path"))) {
  58. // SSL file does not exist
  59. emit ipPopupSetStatus("Invalid SSL-Path. SSL has been disabled.");
  60. CliManager::init(false);
  61. } else {
  62. // All good, start CLI with SSL
  63. CliManager::init(true);
  64. }
  65. } else {
  66. // All good, SSL is disabled, start CLI without SSL
  67. CliManager::init(false);
  68. }
  69. }
  70. } else {
  71. // Config is invalid
  72. emit invalidConfigPopupOpen();
  73. }
  74. } else {
  75. // Config doesn't exist
  76. Config::setupDefaultConfig();
  77. emit noConfigFoundPopupOpen();
  78. }
  79. }
  80. void QMLHandler::onSwitchServer() { CliManager::writeToCli("disconnect"); }
  81. // No Config Found Popup
  82. void QMLHandler::onNoConfigFoundPopupContinueButton(QString cli_path) {
  83. Config::setValue("CLI-Path", cli_path.toUtf8().constData());
  84. Config::saveFile();
  85. onStart(true);
  86. }
  87. // Invalid Cli Path Popup
  88. void QMLHandler::onInvalidCliPathPopupContinueButton(QString cli_path) {
  89. Config::setValue("CLI-Path", cli_path.toUtf8().constData());
  90. Config::saveFile();
  91. onStart(true);
  92. }
  93. void QMLHandler::onInvalidCliPathPopupQuitButton() { emit closeWindow(); }
  94. // Invalid Config Popup
  95. void QMLHandler::onInvalidConfigPopupQuitButton() { emit closeWindow(); }
  96. void QMLHandler::onInvalidConfigPopupCreateDefaultButton() {
  97. Config::setupDefaultConfig();
  98. emit noConfigFoundPopupOpen();
  99. }
  100. // Server Files
  101. void QMLHandler::onServerFilesSelectFileButton(QUrl url) {
  102. sendFileUrl = url.toLocalFile();
  103. emit log("File Selected: " + sendFileUrl.toString());
  104. emit serverFilesSetFileUrlText(sendFileUrl.toString());
  105. emit serverFilesEnableSendButton();
  106. }
  107. void QMLHandler::onServerFilesSendFileButton() {
  108. QString command = "put \"" + sendFileUrl.toString() + "\"";
  109. CliManager::writeToCli(command);
  110. }
  111. void QMLHandler::onServerFilesClearSelectionButton() {
  112. sendFileUrl = QUrl("");
  113. emit log("Cleared Selection");
  114. emit serverFilesSetFileUrlText("None");
  115. emit serverFilesDisableSendButton();
  116. }
  117. void QMLHandler::onServerFilesDownloadFileButton(QString fileName) {
  118. QString command = "get \"" + fileName + "\"";
  119. CliManager::writeToCli(command);
  120. }
  121. void QMLHandler::onServerFilesConfirmDeleteFileButton(QString fileName) {
  122. QString command = "deletefile \"" + fileName + "\"";
  123. CliManager::writeToCli(command);
  124. }
  125. // Settings
  126. void QMLHandler::onKeyfileSelected(QString path) {
  127. QString command = "keyfile \"" + path + "\"";
  128. CliManager::writeToCli(command);
  129. }
  130. void QMLHandler::onKeyfileClosed() { CliManager::writeToCli("closekey"); }
  131. void QMLHandler::onSettingsDeleteMeButton(QString password) {
  132. QString command = "deleteme " + password;
  133. CliManager::writeToCli(command);
  134. }
  135. void QMLHandler::onSettingsRevertChangesButton() {
  136. Config::loadFile();
  137. loadSettingsToGUI();
  138. emit log("Settings changes reverted.");
  139. }
  140. void QMLHandler::onSettingsResetButton() {
  141. string cli_path = Config::getValue("CLI-Path");
  142. Config::setupDefaultConfig();
  143. Config::setValue("CLI-Path", cli_path);
  144. loadSettingsToGUI();
  145. emit log("Settings resetted to default.");
  146. }
  147. void QMLHandler::onSettingsSaveButton(bool saveIP, bool saveUsername, QString cliPath, QString keyPath) {
  148. Config::setValue("Autofill-IP", to_string(saveIP));
  149. Config::setValue("Autofill-Username", to_string(saveUsername));
  150. Config::setValue("CLI-Path", cliPath.toUtf8().constData());
  151. Config::setValue("Keyfile-Path", keyPath.toUtf8().constData());
  152. Config::saveFile();
  153. emit log("Settings saved.");
  154. }
  155. // Ip Popup
  156. void QMLHandler::onIpPopupConnectButton(QString ip, bool saveAsDefault) {
  157. QStringList ipport = ip.split(":");
  158. QString command = "connect " + ipport[0];
  159. CmdManager::setCachedIP(ipport[0]);
  160. if (ipport.size() > 1) {
  161. command += " " + ipport[1];
  162. CmdManager::setCachedPort(ipport[1]);
  163. }
  164. CliManager::writeToCli(command);
  165. emit ipPopupDisableConnectButton();
  166. if (saveAsDefault) {
  167. Config::setValue("Default-IP", ip.toUtf8().constData());
  168. Config::setValue("Autofill-IP", "1");
  169. Config::saveFile();
  170. loadSettingsToGUI();
  171. }
  172. }
  173. // Login
  174. void QMLHandler::onLoginLoginButton(QString username, QString password, bool saveAsDefault) {
  175. QString command = "login " + username + " " + password;
  176. CliManager::writeToCli(command);
  177. emit loginDisableLoginButton();
  178. if (saveAsDefault) {
  179. Config::setValue("Default-Username", username.toUtf8().constData());
  180. Config::setValue("Autofill-Username", "1");
  181. Config::saveFile();
  182. loadSettingsToGUI();
  183. }
  184. }
  185. // Signup
  186. void QMLHandler::onSignupRegisterButton(QString username, QString passwordOne, QString passwordTwo, bool saveAsDefault) {
  187. if (QString::compare(passwordOne, passwordTwo, Qt::CaseSensitive)) {
  188. emit signupSetStatus("Passwords don't match");
  189. return;
  190. }
  191. QString command = "signup " + username + " " + passwordOne;
  192. CliManager::writeToCli(command);
  193. emit signupDisableRegisterButton();
  194. if (saveAsDefault) {
  195. Config::setValue("Default-Username", username.toUtf8().constData());
  196. Config::setValue("Autofill-Username", "1");
  197. Config::saveFile();
  198. loadSettingsToGUI();
  199. }
  200. }
  201. // Notifications
  202. void QMLHandler::onDismissNotificationButton(int index) { emit dismissNotification(index); }
  203. // Queueing
  204. void QMLHandler::onReceivingQueueFileButton(QString fileName) {
  205. QString command = "queue " + fileName;
  206. CliManager::writeToCli(command);
  207. }
  208. void QMLHandler::onReceivingDequeueFileButton(QString fileName) {
  209. QString command = "dequeue " + fileName;
  210. CliManager::writeToCli(command);
  211. }
  212. void QMLHandler::setRestart(bool restart) { _RESTART = restart; }
  213. void QMLHandler::setConfigExists(bool exists) { configExists = exists; }