qmlhandler.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. // Messages
  126. void QMLHandler::onMessagesSendButton(QString msg) { emit message(msg); }
  127. // Settings
  128. void QMLHandler::onKeyfileSelected(QString path) {
  129. QString command = "keyfile \"" + path + "\"";
  130. CliManager::writeToCli(command);
  131. }
  132. void QMLHandler::onKeyfileClosed() { CliManager::writeToCli("closekey"); }
  133. void QMLHandler::onSettingsDeleteMeButton(QString password) {
  134. QString command = "deleteme " + password;
  135. CliManager::writeToCli(command);
  136. }
  137. void QMLHandler::onSettingsRevertChangesButton() {
  138. Config::loadFile();
  139. loadSettingsToGUI();
  140. emit log("Settings changes reverted.");
  141. }
  142. void QMLHandler::onSettingsResetButton() {
  143. string cli_path = Config::getValue("CLI-Path");
  144. Config::setupDefaultConfig();
  145. Config::setValue("CLI-Path", cli_path);
  146. loadSettingsToGUI();
  147. emit log("Settings resetted to default.");
  148. }
  149. void QMLHandler::onSettingsSaveButton(bool saveIP, bool saveUsername, QString cliPath, QString keyPath) {
  150. Config::setValue("Autofill-IP", to_string(saveIP));
  151. Config::setValue("Autofill-Username", to_string(saveUsername));
  152. Config::setValue("CLI-Path", cliPath.toUtf8().constData());
  153. Config::setValue("Keyfile-Path", keyPath.toUtf8().constData());
  154. Config::saveFile();
  155. emit log("Settings saved.");
  156. }
  157. // Ip Popup
  158. void QMLHandler::onIpPopupConnectButton(QString ip, bool saveAsDefault) {
  159. QStringList ipport = ip.split(":");
  160. QString command = "connect " + ipport[0];
  161. CmdManager::setCachedIP(ipport[0]);
  162. if (ipport.size() > 1) {
  163. command += " " + ipport[1];
  164. CmdManager::setCachedPort(ipport[1]);
  165. }
  166. CliManager::writeToCli(command);
  167. emit ipPopupDisableConnectButton();
  168. if (saveAsDefault) {
  169. Config::setValue("Default-IP", ip.toUtf8().constData());
  170. Config::setValue("Autofill-IP", "1");
  171. Config::saveFile();
  172. loadSettingsToGUI();
  173. }
  174. }
  175. // Login
  176. void QMLHandler::onLoginLoginButton(QString username, QString password, bool saveAsDefault) {
  177. QString command = "login " + username + " " + password;
  178. CliManager::writeToCli(command);
  179. emit loginDisableLoginButton();
  180. if (saveAsDefault) {
  181. Config::setValue("Default-Username", username.toUtf8().constData());
  182. Config::setValue("Autofill-Username", "1");
  183. Config::saveFile();
  184. loadSettingsToGUI();
  185. }
  186. }
  187. // Signup
  188. void QMLHandler::onSignupRegisterButton(QString username, QString passwordOne, QString passwordTwo, bool saveAsDefault) {
  189. if (QString::compare(passwordOne, passwordTwo, Qt::CaseSensitive)) {
  190. emit signupSetStatus("Passwords don't match");
  191. return;
  192. }
  193. QString command = "signup " + username + " " + passwordOne;
  194. CliManager::writeToCli(command);
  195. emit signupDisableRegisterButton();
  196. if (saveAsDefault) {
  197. Config::setValue("Default-Username", username.toUtf8().constData());
  198. Config::setValue("Autofill-Username", "1");
  199. Config::saveFile();
  200. loadSettingsToGUI();
  201. }
  202. }
  203. // Notifications
  204. void QMLHandler::onDismissNotificationButton(int index) { emit dismissNotification(index); }
  205. // Queueing
  206. void QMLHandler::onReceivingQueueFileButton(QString fileName) {
  207. QString command = "queue " + fileName;
  208. CliManager::writeToCli(command);
  209. }
  210. void QMLHandler::onReceivingDequeueFileButton(QString fileName) {
  211. QString command = "dequeue " + fileName;
  212. CliManager::writeToCli(command);
  213. }
  214. void QMLHandler::setRestart(bool restart) { _RESTART = restart; }
  215. void QMLHandler::setConfigExists(bool exists) { configExists = exists; }