client.hpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #ifndef __CLIENT_HPP__
  2. #define __CLIENT_HPP__
  3. #include <QDialog>
  4. #include <QComboBox>
  5. #include <QPushButton>
  6. #include <QMessageBox>
  7. #include <QLineEdit>
  8. #include <QTimer>
  9. #include <QLabel>
  10. #include <QString>
  11. #include <QVector>
  12. #include <QThread>
  13. #include <QMetaType>
  14. #include <thread>
  15. #include <string>
  16. #include <utility>
  17. static constexpr int MESSAGE_UPDATE_TYPE = 1;
  18. static constexpr int COMMANDS_UPDATE_TYPE = 2;
  19. static constexpr int EVENT_UPDATE_TYPE = 3;
  20. typedef std::map<int, std::string> CommandMap;
  21. typedef std::map<int, std::vector<std::string>> CommandArgMap;
  22. typedef QVector<QString> StringVec;
  23. Q_DECLARE_METATYPE(StringVec)
  24. class Client : public QDialog
  25. {
  26. Q_OBJECT
  27. QThread workerThread;
  28. public:
  29. class MessageHandler {
  30. public:
  31. MessageHandler(std::function<void()> cb) : m_cb(cb) {}
  32. void operator()() { m_cb(); }
  33. private:
  34. std::function<void()> m_cb;
  35. };
  36. Client(QWidget *parent = nullptr);
  37. Client(QWidget *parent, int count, char** arguments);
  38. ~Client();
  39. void start();
  40. void closeConnection();
  41. void execute();
  42. QString getAppName(int mask);
  43. int getSelectedApp();
  44. // Move this to private after moving responsibilities to Client
  45. void scheduleTask(std::vector<std::string> task_args, bool file_pending);
  46. MessageHandler createMessageHandler(std::function<void()> cb);
  47. public slots:
  48. void sendMessage(const QString& s);
  49. void sendEncoded(std::string message);
  50. void sendFileEncoded(QByteArray bytes);
  51. void setSelectedApp(std::vector<QString> app_names);
  52. void sendFile(QByteArray bytes);
  53. signals:
  54. void messageReceived(int t, QString s, QVector<QString> args);
  55. void eventReceived(int t, std::string event, StringVec args);
  56. private:
  57. void handleMessages();
  58. void sendPackets(uint8_t* data, int size);
  59. int argc;
  60. char** argv;
  61. int m_client_socket_fd;
  62. std::vector<std::string> m_task;
  63. bool executing;
  64. CommandMap m_commands;
  65. CommandArgMap m_command_arg_map;
  66. std::vector<int> selected_commands;
  67. QByteArray outgoing_file;
  68. };
  69. #endif // __CLIENT_HPP__