mainwindow.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. });
  70. QObject::connect(ui->execute, &QPushButton::clicked, this, [this]() {
  71. q_client->execute();
  72. });
  73. QObject::connect(ui->addArgs, &QPushButton::clicked, this, [this]() {
  74. if (ui->appList->count() == 0) {
  75. QMessageBox::warning(this, tr("Args"), tr("Please connect to the KServer and retrieve a list of available processes."));
  76. } else {
  77. arg_ui->show();
  78. }
  79. });
  80. QObject::connect(arg_ui, &ArgDialog::uploadFile, this, [this](QByteArray bytes) {
  81. q_client->sendFile(bytes);
  82. });
  83. QObject::connect(arg_ui, &ArgDialog::taskRequestReady, this, [this](Task task, bool file_pending) {
  84. // TODO: Maybe this should be handled by the Client class directly
  85. auto mask = q_client->getSelectedApp();
  86. if (mask > -1) {
  87. if (q_client->getAppName(mask) == "Instagram") {
  88. auto datetime = task.args.at(1);
  89. auto current_datetime = QDateTime::currentDateTime().toTime_t();
  90. auto seconds_diff = std::stoi(datetime) - current_datetime;
  91. qDebug() << "Time difference: " << seconds_diff;
  92. if (seconds_diff > 3600) {
  93. qDebug() << "Scheduling a task";
  94. task.args.push_back(std::to_string(mask));
  95. q_client->scheduleTask(task.args, file_pending);
  96. }
  97. }
  98. }
  99. });
  100. QObject::connect(ui->viewConsole, &QPushButton::clicked, this, [this]() {
  101. m_console.show();
  102. });
  103. // TODO: Handle enter key
  104. // QObject::connect(static_cast<KTextEdit*>(ui->inputText), &KTextEdit::textInputEnter, this, &MainWindow::handleInputEnterKey);
  105. QObject::connect(static_cast<KTextEdit*>(ui->inputText), &KTextEdit::textInputEnter, this, &MainWindow::handleInputEnterKey);
  106. }
  107. void MainWindow::handleInputEnterKey() {
  108. q_client->sendMessage(ui->inputText->toPlainText());
  109. ui->inputText->clear();
  110. }
  111. QString MainWindow::parseMessage(const QString& message, StringVec v) {
  112. QString simplified_message{};
  113. if (isMessage(message.toUtf8())) {
  114. simplified_message += "Message: " + getMessage(message.toUtf8());
  115. } else if (isEvent(message.toUtf8())) {
  116. simplified_message += "Event: " + getEvent(message.toUtf8());
  117. } else if (isOperation(message.toUtf8())) {
  118. simplified_message += "Operation: ";
  119. simplified_message += getOperation(message.toUtf8()).c_str();
  120. }
  121. // TODO: Find out why rapidJson uses GetArray() in place of IsArray()
  122. // QVector<QString> short_args = getShortArgs(message.toUtf8());
  123. // if (!short_args.empty()) {
  124. // simplified_message += "\nArguments:";
  125. // for (const auto& arg : short_args) {
  126. // simplified_message += " " + arg + ",";
  127. // }
  128. // simplified_message.chop(simplified_message.size() - 1);
  129. // }
  130. return simplified_message;
  131. }
  132. /**
  133. * @brief MainWindow::updateMessages
  134. * @param s
  135. */
  136. void MainWindow::updateMessages(int t, const QString& message, StringVec v) {
  137. if (t == MESSAGE_UPDATE_TYPE) {
  138. qDebug() << "Updating message area";
  139. auto simple_message = parseMessage(message, v);
  140. ui->messages->append(simple_message);
  141. m_console.updateText(message);
  142. } else if (t == COMMANDS_UPDATE_TYPE) {
  143. qDebug() << "Updating commands";
  144. QComboBox* app_list = ui->appList;
  145. app_list->clear();
  146. for (const auto& s : v) {
  147. app_list->addItem(s);
  148. }
  149. //TODO: We do this because a CommandLinkButton turns transparent by default, except when hovered or checked
  150. ui->connect->setChecked(true);
  151. } else if (t == PROCESS_REQUEST_TYPE) {
  152. qDebug() << "Updating process list";
  153. ui->processList->addItem(message);
  154. //TODO: We do this because a CommandLinkButton turns transparent by default, except when hovered or checked
  155. ui->connect->setChecked(true);
  156. } else if (t == EVENT_UPDATE_TYPE) {
  157. QString event_message{QDateTime::currentDateTime().toString("hh:mm:ss") + " - "};
  158. if (!v.empty()) {
  159. // TODO: extract process result handling from here. This should handle any event
  160. if (v.size() == 1) {
  161. event_message += message + "\n" + v.at(0);
  162. } else {
  163. auto mask = std::stoi(v.at(0).toUtf8().constData());
  164. event_message += message;
  165. event_message += "\n";
  166. event_message += q_client->getAppName(mask);
  167. event_message += ": ";
  168. event_message += v.at(1);
  169. if (message == "Process Result") {
  170. updateProcessResult(mask);
  171. }
  172. }
  173. } else {
  174. event_message += message;
  175. }
  176. m_events.push_front(event_message);
  177. ui->eventList->clear();
  178. for (const auto& i : m_events) {
  179. ui->eventList->addItem(i);
  180. }
  181. } else {
  182. qDebug() << "Unknown update type. Cannot update UI";
  183. }
  184. }
  185. void MainWindow::updateProcessResult(int mask) {
  186. auto app_name = q_client->getAppName(mask);
  187. for (int i = ui->processList->count() - 1; i >= 0; i--) {
  188. if (ui->processList->item(i)->text().contains(app_name)) {
  189. ui->processList->item(i)->setText(app_name + " completed");
  190. return;
  191. }
  192. }
  193. }
  194. void MainWindow::keyPressEvent(QKeyEvent *e) {
  195. qDebug() << "Key press: " << e->key();
  196. if(e->key()==Qt::Key_0)
  197. {
  198. qDebug() << "Ok";
  199. }
  200. }