qmlhandler.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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/config.h"
  16. #include "../include/jsonhandler.h"
  17. #include "../include/qmlhandler.h"
  18. #include <boost/asio.hpp>
  19. #include <boost/filesystem.hpp>
  20. #include <boost/lexical_cast.hpp>
  21. #include <iostream>
  22. #include <json/json.h>
  23. using boost::lexical_cast;
  24. using boost::asio::buffer;
  25. using namespace std;
  26. QUrl sendFileUrl = QUrl("");
  27. bool _RESTART = false;
  28. QMLHandler::QMLHandler(QObject *parent) : QObject(parent) {}
  29. void QMLHandler::loadSettingsToGUI() {
  30. int covertMethod = lexical_cast<int>(Config::getValue("Covert-Channel-Method"));
  31. bool saveIP = lexical_cast<bool>(Config::getValue("Autofill-IP"));
  32. bool saveUsername = lexical_cast<bool>(Config::getValue("Autofill-Username"));
  33. QString cliPath = QString::fromStdString(Config::getValue("CLI-Path"));
  34. emit loadSettings(covertMethod, saveIP, saveUsername, cliPath);
  35. }
  36. // ### QML Handlers ###
  37. void QMLHandler::onStart() {
  38. bool configExists = Config::loadFile();
  39. if (configExists == true) {
  40. // Config exists
  41. if (Config::checkConfig() == true) {
  42. // Config is valid
  43. if (!boost::filesystem::is_regular_file(Config::getValue("CLI-Path")) ||
  44. boost::filesystem::path(Config::getValue("CLI-Path")).filename().compare("ccats-cli")) {
  45. // Invalid CLI Path
  46. emit invalidCliPathPopupOpen();
  47. }
  48. if (Config::getValue("Autofill-IP") == "1") {
  49. emit ipPopupSetIP(QString::fromStdString(Config::getValue("Default-IP")));
  50. emit ipPopupCheckSaveCheckbox();
  51. }
  52. if (Config::getValue("Autofill-Username") == "1") {
  53. emit loginSetUsername(QString::fromStdString(Config::getValue("Default-Username")));
  54. emit loginSignupCheckSaveCheckbox();
  55. }
  56. } else {
  57. // Config is invalid
  58. emit invalidConfigPopupOpen();
  59. }
  60. } else {
  61. // Config doesn't exist
  62. Config::setupDefaultConfig();
  63. emit noConfigFoundPopupOpen();
  64. }
  65. CliManager::init();
  66. }
  67. // No Config Found Popup
  68. void QMLHandler::onNoConfigFoundPopupContinueButton(QString cli_path) {
  69. Config::setValue("CLI-Path", cli_path.toUtf8().constData());
  70. Config::saveFile();
  71. }
  72. // Invalid Cli Path Popup
  73. void QMLHandler::onInvalidCliPathPopupContinueButton(QString cli_path) {
  74. Config::setValue("CLI-Path", cli_path.toUtf8().constData());
  75. Config::saveFile();
  76. }
  77. void QMLHandler::onInvalidCliPathPopupQuitButton() { emit closeWindow(); }
  78. // Invalid Config Popup
  79. void QMLHandler::onInvalidConfigPopupQuitButton() { emit closeWindow(); }
  80. void QMLHandler::onInvalidConfigPopupCreateDefaultButton() {
  81. Config::setupDefaultConfig();
  82. emit noConfigFoundPopupOpen();
  83. }
  84. // Sending
  85. void QMLHandler::onSendingSelectFileButton(QUrl url) {
  86. sendFileUrl = url.toLocalFile();
  87. emit log("File Selected: " + sendFileUrl.toString());
  88. emit sendingSetFileUrlText("Selected File: " + sendFileUrl.toString());
  89. emit sendingEnableSendButton();
  90. }
  91. void QMLHandler::onSendingSendFileButton() {
  92. QString command = "put " + sendFileUrl.toString();
  93. CliManager::writeToCli(command);
  94. }
  95. void QMLHandler::onSendingClearSelectionButton() {
  96. sendFileUrl = QUrl("");
  97. emit log("Cleared Selection");
  98. emit sendingSetFileUrlText("Selected File: None");
  99. emit sendingDisableSendButton();
  100. }
  101. // Receiving
  102. void QMLHandler::onReceivingListFilesButton() { CliManager::writeToCli("extendedlist"); }
  103. void QMLHandler::onReceivingDownloadFileButton(QString fileName) {
  104. QString command = "get " + fileName;
  105. CliManager::writeToCli(command);
  106. }
  107. void QMLHandler::onReceivingConfirmDeleteFileButton(QString fileName) {
  108. QString command = "deletefile " + fileName;
  109. CliManager::writeToCli(command);
  110. }
  111. // Messages
  112. void QMLHandler::onMessagesSendButton(QString msg) { emit message(msg); }
  113. // Settings
  114. void QMLHandler::onSettingsDeleteMeButton(QString password) {
  115. QString command = "deleteme " + password;
  116. CliManager::writeToCli(command);
  117. }
  118. void QMLHandler::onSettingsRevertChangesButton() {
  119. loadSettingsToGUI();
  120. emit log("Settings changes reverted.");
  121. }
  122. void QMLHandler::onSettingsResetButton() {
  123. string cli_path = Config::getValue("CLI-Path");
  124. Config::setupDefaultConfig();
  125. Config::setValue("CLI-Path", cli_path);
  126. loadSettingsToGUI();
  127. emit log("Settings resetted to default.");
  128. }
  129. void QMLHandler::onSettingsSaveButton(int covertMethod, bool saveIP, bool saveUsername, QString cliPath) {
  130. Config::setValue("Covert-Channel-Method", lexical_cast<string>(covertMethod));
  131. Config::setValue("Autofill-IP", lexical_cast<string>(saveIP));
  132. Config::setValue("Autofill-Username", lexical_cast<string>(saveUsername));
  133. Config::setValue("CLI-Path", cliPath.toUtf8().constData());
  134. Config::saveFile();
  135. emit log("Settings saved.");
  136. }
  137. // Ip Popup
  138. void QMLHandler::onIpPopupConnectButton(QString ip, bool saveAsDefault) {
  139. QString command = "connect " + ip;
  140. CliManager::writeToCli(command);
  141. emit ipPopupDisableConnectButton();
  142. if (saveAsDefault) {
  143. Config::setValue("Default-IP", ip.toUtf8().constData());
  144. Config::setValue("Autofill-IP", "1");
  145. Config::saveFile();
  146. }
  147. }
  148. // Login
  149. void QMLHandler::onLoginLoginButton(QString username, QString password, bool saveAsDefault) {
  150. QString command = "login " + username + " " + password;
  151. CliManager::writeToCli(command);
  152. emit loginDisableLoginButton();
  153. if (saveAsDefault) {
  154. Config::setValue("Default-Username", username.toUtf8().constData());
  155. Config::setValue("Autofill-Username", "1");
  156. Config::saveFile();
  157. }
  158. }
  159. // Signup
  160. void QMLHandler::onSignupRegisterButton(QString username, QString passwordOne, QString passwordTwo, bool saveAsDefault) {
  161. if (QString::compare(passwordOne, passwordTwo, Qt::CaseSensitive)) {
  162. emit signupSetStatus("Passwords don't match");
  163. return;
  164. }
  165. QString command = "signup " + username + " " + passwordOne;
  166. CliManager::writeToCli(command);
  167. emit signupDisableRegisterButton();
  168. if (saveAsDefault) {
  169. Config::setValue("Default-Username", username.toUtf8().constData());
  170. Config::setValue("Autofill-Username", "1");
  171. Config::saveFile();
  172. }
  173. }
  174. // Footer
  175. void QMLHandler::onFooterGetStatusButton() { CliManager::writeToCli("status"); }
  176. // Notifications
  177. void QMLHandler::onDismissNotificationButton(int index) { emit dismissNotification(index); }
  178. // Queueing
  179. void QMLHandler::onReceivingQueueFileButton(QString fileName) {
  180. QString command = "queue " + fileName;
  181. CliManager::writeToCli(command);
  182. }
  183. void QMLHandler::onReceivingDequeueFileButton(QString fileName) {
  184. QString command = "dequeue " + fileName;
  185. CliManager::writeToCli(command);
  186. }
  187. void QMLHandler::setRestart(bool restart) { _RESTART = restart; }