client.hpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 <QUuid>
  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. static constexpr int PROCESS_REQUEST_TYPE = 4;
  21. enum TaskType {
  22. INSTAGRAM = 1,
  23. OTHER = 2
  24. };
  25. namespace TaskCode {
  26. static constexpr int IGTASKBYTE = 0xFF;
  27. static constexpr int GENMSGBYTE = 0xFE;
  28. }
  29. typedef std::map<int, std::string> CommandMap;
  30. typedef std::map<int, std::vector<std::string>> CommandArgMap;
  31. typedef QVector<QString> StringVec;
  32. Q_DECLARE_METATYPE(StringVec)
  33. class Client : public QDialog
  34. {
  35. Q_OBJECT
  36. QThread workerThread;
  37. public:
  38. class MessageHandler {
  39. public:
  40. MessageHandler(std::function<void()> cb) : m_cb(cb) {}
  41. void operator()() { m_cb(); }
  42. private:
  43. std::function<void()> m_cb;
  44. };
  45. Client(QWidget *parent = nullptr);
  46. Client(QWidget *parent, int count, char** arguments);
  47. ~Client();
  48. void start();
  49. void closeConnection();
  50. void execute();
  51. QString getAppName(int mask);
  52. int getSelectedApp();
  53. // Move this to private after moving responsibilities to Client
  54. void scheduleTask(std::vector<std::string> task_args, bool file_pending);
  55. MessageHandler createMessageHandler(std::function<void()> cb);
  56. public slots:
  57. void sendMessage(const QString& s);
  58. void sendEncoded(std::string message);
  59. void sendFileEncoded(QByteArray bytes);
  60. void sendTaskEncoded(TaskType type, std::vector<std::string> args);
  61. void setSelectedApp(std::vector<QString> app_names);
  62. void sendFile(QByteArray bytes);
  63. signals:
  64. void messageReceived(int t, QString s, QVector<QString> args);
  65. void eventReceived(int t, std::string event, StringVec args);
  66. private:
  67. void handleMessages();
  68. void sendPackets(uint8_t* data, int size);
  69. int argc;
  70. char** argv;
  71. int m_client_socket_fd;
  72. std::vector<std::string> m_task;
  73. bool executing;
  74. CommandMap m_commands;
  75. CommandArgMap m_command_arg_map;
  76. std::vector<int> selected_commands;
  77. QByteArray outgoing_file;
  78. };
  79. #endif // CLIENT_HPP