client.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #ifndef CLIENT_HPP
  2. #define CLIENT_HPP
  3. #include <QComboBox>
  4. #include <QDialog>
  5. #include <QLabel>
  6. #include <QLineEdit>
  7. #include <QMessageBox>
  8. #include <QMetaType>
  9. #include <QPushButton>
  10. #include <QQueue>
  11. #include <QString>
  12. #include <QThread>
  13. #include <QUuid>
  14. #include <QVector>
  15. #include <headers/util.hpp>
  16. #include <include/task/task.hpp>
  17. #include <string>
  18. #include <thread>
  19. #include <utility>
  20. static constexpr int MESSAGE_UPDATE_TYPE = 1;
  21. static constexpr int COMMANDS_UPDATE_TYPE = 2;
  22. static constexpr int EVENT_UPDATE_TYPE = 3;
  23. static constexpr int PROCESS_REQUEST_TYPE = 4;
  24. enum TaskType {
  25. INSTAGRAM = 1,
  26. OTHER = 2
  27. };
  28. namespace TaskCode {
  29. static constexpr int IGTASKBYTE = 0xFF;
  30. static constexpr int GENMSGBYTE = 0xFE;
  31. static constexpr int PINGBYTE = 0xFD;
  32. } // namespace TaskCode
  33. typedef std::map<int, std::string> CommandMap;
  34. typedef std::map<int, std::vector<std::string>> CommandArgMap;
  35. typedef QVector<QString> StringVec;
  36. struct SentFile {
  37. int timestamp;
  38. QString name;
  39. FileType type;
  40. };
  41. Q_DECLARE_METATYPE(StringVec)
  42. Q_DECLARE_METATYPE(QVector<QByteArray>);
  43. class Client : public QDialog {
  44. Q_OBJECT
  45. QThread workerThread;
  46. public:
  47. class MessageHandler {
  48. public:
  49. MessageHandler(std::function<void()> cb) : m_cb(cb) {}
  50. void operator()() { m_cb(); }
  51. private:
  52. std::function<void()> m_cb;
  53. };
  54. Client(QWidget* parent = nullptr);
  55. Client(QWidget* parent, int count, char** arguments);
  56. ~Client();
  57. void start();
  58. void closeConnection();
  59. void execute();
  60. QString getAppName(int mask);
  61. int getSelectedApp();
  62. // Move this to private after moving responsibilities to Client
  63. void scheduleTask(Scheduler::Task* task, bool file_pending);
  64. MessageHandler createMessageHandler(std::function<void()> cb);
  65. public slots:
  66. void sendMessage(const QString& s);
  67. void setSelectedApp(std::vector<QString> app_names);
  68. void sendFiles(Scheduler::Task* task);
  69. void ping();
  70. signals:
  71. void messageReceived(int t, QString s, QVector<QString> args);
  72. void eventReceived(int t, std::string event, StringVec args);
  73. private:
  74. void sendEncoded(std::string message);
  75. void sendFileEncoded(QByteArray bytes);
  76. void sendTaskEncoded(TaskType type, std::vector<std::string> args);
  77. void sendTaskEncoded(Scheduler::Task* task);
  78. void processFileQueue();
  79. void handleMessages();
  80. void sendPackets(uint8_t* data, int size);
  81. int argc;
  82. char** argv;
  83. int m_client_socket_fd;
  84. std::vector<std::string> m_task;
  85. bool executing;
  86. bool file_was_sent;
  87. CommandMap m_commands;
  88. CommandArgMap m_command_arg_map;
  89. std::vector<int> selected_commands;
  90. QQueue<Scheduler::KFileData> outgoing_files;
  91. std::vector<SentFile> sent_files;
  92. Scheduler::TaskQueue m_task_queue;
  93. };
  94. #endif // CLIENT_HPP