client.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. }
  31. typedef std::map<int, std::string> CommandMap;
  32. typedef std::map<int, std::vector<std::string>> CommandArgMap;
  33. typedef QVector<QString> StringVec;
  34. struct SentFile {
  35. int timestamp;
  36. QString name;
  37. FileType type;
  38. };
  39. Q_DECLARE_METATYPE(StringVec)
  40. Q_DECLARE_METATYPE(QVector<QByteArray>);
  41. class Client : public QDialog
  42. {
  43. Q_OBJECT
  44. QThread workerThread;
  45. public:
  46. class MessageHandler {
  47. public:
  48. MessageHandler(std::function<void()> cb) : m_cb(cb) {}
  49. void operator()() { m_cb(); }
  50. private:
  51. std::function<void()> m_cb;
  52. };
  53. Client(QWidget *parent = nullptr);
  54. Client(QWidget *parent, int count, char** arguments);
  55. ~Client();
  56. void start();
  57. void closeConnection();
  58. void execute();
  59. QString getAppName(int mask);
  60. int getSelectedApp();
  61. // Move this to private after moving responsibilities to Client
  62. void scheduleTask(std::vector<std::string> task_args, bool file_pending);
  63. MessageHandler createMessageHandler(std::function<void()> cb);
  64. public slots:
  65. void sendMessage(const QString& s);
  66. void setSelectedApp(std::vector<QString> app_names);
  67. void sendFiles(QVector<KFileData> files);
  68. signals:
  69. void messageReceived(int t, QString s, QVector<QString> args);
  70. void eventReceived(int t, std::string event, StringVec args);
  71. private:
  72. void sendEncoded(std::string message);
  73. void sendFileEncoded(QByteArray bytes);
  74. void sendTaskEncoded(TaskType type, std::vector<std::string> args);
  75. void processFileQueue();
  76. void handleMessages();
  77. void sendPackets(uint8_t* data, int size);
  78. int argc;
  79. char** argv;
  80. int m_client_socket_fd;
  81. std::vector<std::string> m_task;
  82. bool executing;
  83. CommandMap m_commands;
  84. CommandArgMap m_command_arg_map;
  85. std::vector<int> selected_commands;
  86. QQueue<KFileData> outgoing_files;
  87. std::vector<SentFile> sent_files;
  88. };
  89. #endif // CLIENT_HPP