client.hpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include <QDialog>
  2. #include <QComboBox>
  3. #include <QPushButton>
  4. #include <QMessageBox>
  5. #include <QLineEdit>
  6. #include <QTimer>
  7. #include <QLabel>
  8. #include <QString>
  9. #include <QVector>
  10. #include <QThread>
  11. #include <QMetaType>
  12. #include <thread>
  13. #include <string>
  14. #include <utility>
  15. static constexpr int MESSAGE_UPDATE_TYPE = 1;
  16. static constexpr int COMMANDS_UPDATE_TYPE = 2;
  17. typedef std::map<int, std::string> CommandMap;
  18. typedef QVector<QString> StringVec;
  19. Q_DECLARE_METATYPE(StringVec)
  20. class Client : public QDialog
  21. {
  22. Q_OBJECT
  23. QThread workerThread;
  24. public:
  25. class MessageHandler {
  26. public:
  27. MessageHandler(std::function<void()> cb) : m_cb(cb) {}
  28. void operator()() { m_cb(); }
  29. private:
  30. std::function<void()> m_cb;
  31. };
  32. Client(QWidget *parent = nullptr);
  33. Client(QWidget *parent, int count, char** arguments);
  34. ~Client();
  35. void start();
  36. void closeConnection();
  37. void execute();
  38. MessageHandler createMessageHandler(std::function<void()> cb);
  39. public slots:
  40. void sendMessage(const QString& s);
  41. void sendEncoded(std::string message);
  42. void setSelectedApp(std::vector<QString> app_names);
  43. signals:
  44. void messageReceived(int t, QString s,QVector<QString> args);
  45. private:
  46. void handleMessages();
  47. int argc;
  48. char** argv;
  49. int m_client_socket_fd;
  50. bool executing;
  51. CommandMap m_commands;
  52. std::vector<int> selected_commands;
  53. };