client.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 <QQueue>
  13. #include <QThread>
  14. #include <QMetaType>
  15. #include <thread>
  16. #include <string>
  17. #include <utility>
  18. #include <headers/util.hpp>
  19. static constexpr int MESSAGE_UPDATE_TYPE = 1;
  20. static constexpr int COMMANDS_UPDATE_TYPE = 2;
  21. static constexpr int EVENT_UPDATE_TYPE = 3;
  22. static constexpr int PROCESS_REQUEST_TYPE = 4;
  23. enum TaskType {
  24. INSTAGRAM = 1,
  25. OTHER = 2
  26. };
  27. namespace TaskCode {
  28. static constexpr int IGTASKBYTE = 0xFF;
  29. static constexpr int GENMSGBYTE = 0xFE;
  30. static constexpr int PINGBYTE = 0xFD;
  31. }
  32. typedef std::map<int, std::string> CommandMap;
  33. typedef std::map<int, std::vector<std::string>> CommandArgMap;
  34. typedef QVector<QString> StringVec;
  35. struct SentFile {
  36. int timestamp;
  37. QString name;
  38. FileType type;
  39. };
  40. Q_DECLARE_METATYPE(StringVec)
  41. Q_DECLARE_METATYPE(QVector<QByteArray>);
  42. class Client : public QDialog
  43. {
  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(std::vector<std::string> task_args, 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(QVector<KFileData> files);
  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 processFileQueue();
  78. void handleMessages();
  79. void sendPackets(uint8_t* data, int size);
  80. int argc;
  81. char** argv;
  82. int m_client_socket_fd;
  83. std::vector<std::string> m_task;
  84. bool executing;
  85. CommandMap m_commands;
  86. CommandArgMap m_command_arg_map;
  87. std::vector<int> selected_commands;
  88. QQueue<KFileData> outgoing_files;
  89. std::vector<SentFile> sent_files;
  90. };
  91. #endif // CLIENT_HPP