mainwindow.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QDebug>
  4. #include <QTextEdit>
  5. #include <QTextStream>
  6. #include <QString>
  7. #include <QLayout>
  8. #include <headers/ktextedit.hpp>
  9. #include <QDateTime>
  10. #include <vector>
  11. #include <headers/util.hpp>
  12. /**
  13. * @brief MainWindow::MainWindow
  14. * @param argc
  15. * @param argv
  16. * @param parent
  17. */
  18. MainWindow::MainWindow(int argc, char** argv, QWidget *parent) :
  19. QMainWindow(parent),
  20. ui(new Ui::MainWindow),
  21. arg_ui(new ArgDialog),
  22. cli_argc(argc),
  23. cli_argv(argv),
  24. q_client(nullptr) {
  25. q_client = new Client(this, cli_argc, cli_argv);
  26. ui->setupUi(this);
  27. this->setWindowTitle("KYGUI");
  28. QPushButton *button = this->findChild<QPushButton*>("connect");
  29. connect(button, &QPushButton::clicked, this, &MainWindow::connectClient);
  30. }
  31. /**
  32. * @brief MainWindow::~MainWindow
  33. */
  34. MainWindow::~MainWindow()
  35. {
  36. delete q_client;
  37. delete ui;
  38. }
  39. /**
  40. * @brief MainWindow::buttonClicked
  41. */
  42. void MainWindow::connectClient() {
  43. qDebug() << "Connecting to KServer";
  44. QObject::connect(q_client, &Client::messageReceived, this, &MainWindow::updateMessages);
  45. QProgressBar* progressBar = ui->progressBar;
  46. q_client->start();
  47. for (int i = 1; i < 101; i++) {
  48. progressBar->setValue(i);
  49. }
  50. KTextEdit* send_message_box = reinterpret_cast<KTextEdit*>(ui->inputText);
  51. send_message_box->show();
  52. QPushButton* send_message_button = this->findChild<QPushButton*>("sendMessage");
  53. // Handle mouse
  54. QObject::connect(send_message_button, &QPushButton::clicked, this, [this, send_message_box]() {
  55. q_client->sendMessage(send_message_box->toPlainText());
  56. send_message_box->clear();
  57. });
  58. QObject::connect(ui->appList, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, [this]() {
  59. QString app_name = ui->appList->currentText();
  60. // TODO: I know, it's awful. Fix this
  61. q_client->setSelectedApp(std::vector<QString>{{app_name}});
  62. });
  63. QPushButton* disconnect_button = this->findChild<QPushButton*>("disconnect");
  64. QObject::connect(disconnect_button, &QPushButton::clicked, this, [this, progressBar]() {
  65. q_client->closeConnection();
  66. progressBar->setValue(0);
  67. ui->appList->clear();
  68. ui->messages->clear();
  69. ui->led->setState(false);
  70. });
  71. QObject::connect(ui->execute, &QPushButton::clicked, this, [this]() {
  72. q_client->execute();
  73. });
  74. QObject::connect(ui->addArgs, &QPushButton::clicked, this, [this]() {
  75. if (ui->appList->count() == 0) {
  76. QMessageBox::warning(this, tr("Args"), tr("Please connect to the KServer and retrieve a list of available processes."));
  77. } else {
  78. arg_ui->show();
  79. }
  80. });
  81. QObject::connect(arg_ui, &ArgDialog::uploadFile, this, [this](QByteArray bytes) {
  82. q_client->sendFile(bytes);
  83. });
  84. QObject::connect(arg_ui, &ArgDialog::taskRequestReady, this, [this](Task task, bool file_pending) {
  85. // TODO: Maybe this should be handled by the Client class directly
  86. auto mask = q_client->getSelectedApp();
  87. if (mask > -1) {
  88. if (q_client->getAppName(mask) == "Instagram") {
  89. auto datetime = task.args.at(1);
  90. auto current_datetime = QDateTime::currentDateTime().toTime_t();
  91. auto seconds_diff = std::stoi(datetime) - current_datetime;
  92. qDebug() << "Time difference: " << seconds_diff;
  93. if (seconds_diff > 3600) {
  94. qDebug() << "Scheduling a task";
  95. task.args.push_back(std::to_string(mask));
  96. q_client->scheduleTask(task.args, file_pending);
  97. }
  98. }
  99. }
  100. });
  101. QObject::connect(ui->tasks, &QPushButton::clicked, this, [this]() {
  102. // TODO: Change this to a complete implementation
  103. q_client->sendMessage("scheduler");
  104. });
  105. QObject::connect(ui->viewConsole, &QPushButton::clicked, this, [this]() {
  106. m_console.show();
  107. });
  108. // TODO: Handle enter key
  109. // QObject::connect(static_cast<KTextEdit*>(ui->inputText), &KTextEdit::textInputEnter, this, &MainWindow::handleInputEnterKey);
  110. QObject::connect(static_cast<KTextEdit*>(ui->inputText), &KTextEdit::textInputEnter, this, &MainWindow::handleInputEnterKey);
  111. }
  112. void MainWindow::handleInputEnterKey() {
  113. q_client->sendMessage(ui->inputText->toPlainText());
  114. ui->inputText->clear();
  115. }
  116. QString MainWindow::parseMessage(const QString& message, StringVec v) {
  117. QString simplified_message{};
  118. if (isMessage(message.toUtf8())) {
  119. simplified_message += "Message: " + getMessage(message.toUtf8());
  120. } else if (isEvent(message.toUtf8())) {
  121. simplified_message += "Event: " + getEvent(message.toUtf8());
  122. } else if (isOperation(message.toUtf8())) {
  123. simplified_message += "Operation: ";
  124. simplified_message += getOperation(message.toUtf8()).c_str();
  125. }
  126. // TODO: Find out why rapidJson uses GetArray() in place of IsArray()
  127. // QVector<QString> short_args = getShortArgs(message.toUtf8());
  128. // if (!short_args.empty()) {
  129. // simplified_message += "\nArguments:";
  130. // for (const auto& arg : short_args) {
  131. // simplified_message += " " + arg + ",";
  132. // }
  133. // simplified_message.chop(simplified_message.size() - 1);
  134. // }
  135. return simplified_message;
  136. }
  137. /**
  138. * @brief MainWindow::updateMessages
  139. * @param s
  140. */
  141. void MainWindow::updateMessages(int t, const QString& message, StringVec v) {
  142. if (t == MESSAGE_UPDATE_TYPE) {
  143. qDebug() << "Updating message area";
  144. auto simple_message = parseMessage(message, v);
  145. ui->messages->append(simple_message);
  146. m_console.updateText(message);
  147. } else if (t == COMMANDS_UPDATE_TYPE) {
  148. if (message == "New Session") {
  149. ui->led->setState(true);
  150. }
  151. qDebug() << "Updating commands";
  152. QComboBox* app_list = ui->appList;
  153. app_list->clear();
  154. for (const auto& s : v) {
  155. app_list->addItem(s);
  156. }
  157. //TODO: We do this because a CommandLinkButton turns transparent by default, except when hovered or checked
  158. ui->connect->setChecked(true);
  159. } else if (t == PROCESS_REQUEST_TYPE) {
  160. qDebug() << "Updating process list";
  161. ui->processList->addItem(message);
  162. //TODO: We do this because a CommandLinkButton turns transparent by default, except when hovered or checked
  163. ui->connect->setChecked(true);
  164. } else if (t == EVENT_UPDATE_TYPE) {
  165. QString event_message{QDateTime::currentDateTime().toString("hh:mm:ss") + " - "};
  166. if (!v.empty()) {
  167. // TODO: extract process result handling from here. This should handle any event
  168. if (v.size() == 1) {
  169. event_message += message + "\n" + v.at(0);
  170. } else {
  171. auto mask = std::stoi(v.at(0).toUtf8().constData());
  172. event_message += message;
  173. event_message += "\n";
  174. event_message += q_client->getAppName(mask);
  175. event_message += ": ";
  176. event_message += v.at(1);
  177. if (message == "Process Result") {
  178. updateProcessResult(mask);
  179. }
  180. }
  181. } else {
  182. event_message += message;
  183. }
  184. m_events.push_front(event_message);
  185. ui->eventList->clear();
  186. for (const auto& i : m_events) {
  187. ui->eventList->addItem(i);
  188. }
  189. } else {
  190. qDebug() << "Unknown update type. Cannot update UI";
  191. }
  192. }
  193. void MainWindow::updateProcessResult(int mask) {
  194. auto app_name = q_client->getAppName(mask);
  195. for (int i = ui->processList->count() - 1; i >= 0; i--) {
  196. if (ui->processList->item(i)->text().contains(app_name)) {
  197. ui->processList->item(i)->setText(app_name + " completed");
  198. return;
  199. }
  200. }
  201. }
  202. void MainWindow::keyPressEvent(QKeyEvent *e) {
  203. qDebug() << "Key press: " << e->key();
  204. if(e->key()==Qt::Key_0)
  205. {
  206. qDebug() << "Ok";
  207. }
  208. }