task.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. #ifndef __TASK_HPP__
  2. #define __TASK_HPP__
  3. #include <QQueue>
  4. #include <QString>
  5. #include <QVector>
  6. #include <memory>
  7. #include <variant>
  8. #include <vector>
  9. #include <map>
  10. namespace Scheduler {
  11. enum TaskType { INSTAGRAM = 1, GENERIC = 2, OTHER = 3 };
  12. namespace TaskCode {
  13. static constexpr uint32_t GENTASKCODE = 0xFC;
  14. static constexpr uint32_t IGTASKCODE = 0xFF;
  15. }
  16. static constexpr uint32_t GENERIC_TASK_ID = 96;
  17. static constexpr uint32_t INSTAGRAM_TASK_ID = 97;
  18. namespace TaskIndex {
  19. static constexpr uint32_t ERROR = 0x03;
  20. static constexpr uint32_t UUID = 0x00;
  21. static constexpr uint32_t ID = 0x01;
  22. static constexpr uint32_t MASK = 0x02;
  23. static constexpr uint32_t ENVFILE = 0x03;
  24. static constexpr uint32_t FILENUM = 0x04;
  25. } // namespace TaskInfo
  26. inline static const std::map<std::string, uint32_t> TaskCodes{
  27. {"Generic", TaskCode::GENTASKCODE},
  28. {"Instagram", TaskCode::IGTASKCODE}
  29. };
  30. inline static const std::map<std::string, int> TaskFrequency{
  31. {"No", 0x00},
  32. {"Hourly", 0x01},
  33. {"Daily", 0x02},
  34. {"Weekly", 0x03}
  35. };
  36. inline static uint32_t findTaskCode(QString key) {
  37. auto it = TaskCodes.find(key.toUtf8().constData());
  38. return it == TaskCodes.end() ?
  39. TaskCodes.at("Generic") :
  40. (*it).second;
  41. }
  42. inline static int findTaskFrequency(const QString& key) {
  43. auto it = TaskFrequency.find(key.toUtf8().constData());
  44. return it == TaskFrequency.end() ?
  45. TaskCodes.at("No") :
  46. (*it).second;
  47. }
  48. /**
  49. * Files
  50. */
  51. enum FileType { VIDEO = 1, IMAGE = 2 };
  52. /**
  53. * KFileData
  54. *
  55. * Structure for holding file bytes and metadata
  56. */
  57. struct KFileData {
  58. QString name;
  59. FileType type;
  60. QString path;
  61. QByteArray bytes;
  62. // friend std::ostream &operator<<(std::ostream &out, const KFileData& file) {
  63. // out << "Name: " << file.name.toUtf8()
  64. // << "\nType: " << file.type;
  65. // return out;
  66. // }
  67. };
  68. /**
  69. * Type namespace
  70. *
  71. * Describes the types of task arguments available for use
  72. */
  73. namespace Type {
  74. static constexpr const char* TEXT = "Text";
  75. static constexpr const char* FILE = "File";
  76. static constexpr const char* STRINGVECTOR = "StringVector";
  77. static constexpr const char* FILEVECTOR = "FileVector";
  78. static constexpr const char* DATETIME = "DateTime";
  79. static constexpr const char* BOOLEAN = "Boolean";
  80. static constexpr const char* INTEGER = "Integer";
  81. } // namespace Type
  82. namespace VariantIndex {
  83. static const uint8_t BOOLEAN = 0;
  84. static const uint8_t INTEGER = 1;
  85. static const uint8_t STRVEC = 2;
  86. static const uint8_t QSTRING = 3;
  87. static const uint8_t FILEVEC = 4;
  88. } // namespace VariantIndex
  89. inline bool isIndex(uint8_t v, uint8_t i) { return v == i; }
  90. /**
  91. * Forward Declarations
  92. */
  93. class TaskArgumentBase;
  94. class Task;
  95. /**
  96. * Aliases
  97. */
  98. using TaskQueue = QQueue<Task*>;
  99. using ArgumentType = const char*;
  100. using ArgumentValues = QVector<QString>;
  101. using TypeVariant = std::variant<
  102. bool, int, QVector<QString>, QString, QVector<KFileData>
  103. >;
  104. using TaskIterator = std::vector<TaskArgumentBase*>::iterator;
  105. /**
  106. * The interface expected on our Task Arguments
  107. */
  108. class TaskArgumentBase {
  109. public:
  110. virtual const QString text() = 0;
  111. virtual const QString getStringValue() = 0;
  112. virtual uint8_t getTypeIndex() = 0;
  113. virtual TypeVariant getValue() = 0;
  114. virtual void insert(QString value) = 0;
  115. virtual void insert(KFileData file) = 0;
  116. virtual void remove(TypeVariant value) = 0;
  117. virtual void setValue(TypeVariant v) = 0;
  118. virtual bool isContainer() = 0;
  119. virtual void clear() = 0;
  120. };
  121. /**
  122. * TaskArgument
  123. *
  124. * A templated class providing a generic way for handling arguments whose types can be one from the set defined
  125. * by our TypeVariant alias
  126. */
  127. class TaskArgument : TaskArgumentBase {
  128. public:
  129. TaskArgument(QString n, ArgumentType t, TypeVariant _value)
  130. : name(n),
  131. type(t),
  132. value(_value) {}
  133. /**
  134. * Move Constructor
  135. *
  136. * @constructor
  137. * @param [in] {TaskArgument&&} a The R-value reference to a TaskArgument
  138. */
  139. TaskArgument(TaskArgument&& a)
  140. : name(std::move(a.name)),
  141. type(std::move(a.type)),
  142. value(std::move(a.value)) {}
  143. /**
  144. * Copy Constructor
  145. *
  146. * @constructor
  147. * @param [in] {TaskArgument&&} a The const reference to a TaskArgument
  148. */
  149. TaskArgument(const TaskArgument& a)
  150. : name(std::move(a.name)),
  151. type(std::move(a.type)),
  152. value(std::move(a.value)) {}
  153. /**
  154. * text
  155. * @returns {QString} The name of the argument
  156. */
  157. virtual const QString text() override { return name; }
  158. /**
  159. * setValue
  160. * @param [in] {TypeVariant} new_value The new value for this argument
  161. */
  162. virtual void setValue(TypeVariant new_value) override {
  163. value = new_value;
  164. }
  165. /**
  166. * @brief getStringValue
  167. * @return [out] {QString}
  168. */
  169. virtual const QString getStringValue() override {
  170. if (isIndex(value.index(), VariantIndex::QSTRING)) {
  171. return std::get<VariantIndex::QSTRING>(value);
  172. } else if (isIndex(value.index(), VariantIndex::BOOLEAN)) {
  173. return QString::number(std::get<VariantIndex::BOOLEAN>(value));
  174. } else if (isIndex(value.index(), VariantIndex::INTEGER)) {
  175. return QString::number(std::get<VariantIndex::INTEGER>(value));
  176. }
  177. return ""; // Throw?
  178. }
  179. /**
  180. * @brief getValue
  181. * @return [out] {TypeVariant}
  182. */
  183. virtual TypeVariant getValue() override {
  184. if (isIndex(value.index(), VariantIndex::QSTRING)) {
  185. return std::get<VariantIndex::QSTRING>(value);
  186. } else if (isIndex(value.index(), VariantIndex::BOOLEAN)) {
  187. return std::get<VariantIndex::BOOLEAN>(value);
  188. } else if (isIndex(value.index(), VariantIndex::INTEGER)) {
  189. return std::get<VariantIndex::INTEGER>(value);
  190. } else if (isIndex(value.index(), VariantIndex::STRVEC)) {
  191. return std::get<VariantIndex::STRVEC>(value);
  192. } else if (isIndex(value.index(), VariantIndex::FILEVEC)) {
  193. return std::get<VariantIndex::FILEVEC>(value);
  194. }
  195. return ""; // Throw?
  196. }
  197. /**
  198. * @brief clear
  199. */
  200. virtual void clear() override {
  201. if (isIndex(value.index(), VariantIndex::STRVEC)) {
  202. std::get<VariantIndex::STRVEC>(value).clear();
  203. } else if (isIndex(value.index(), VariantIndex::FILEVEC)) {
  204. std::get<VariantIndex::FILEVEC>(value).clear();
  205. } else if (isIndex(value.index(), VariantIndex::QSTRING)) {
  206. std::get<VariantIndex::QSTRING>(value).clear();
  207. } else if (isIndex(value.index(), VariantIndex::INTEGER)) {
  208. std::get<VariantIndex::INTEGER>(value) = 0;
  209. } else if (isIndex(value.index(), VariantIndex::BOOLEAN)) {
  210. std::get<VariantIndex::BOOLEAN>(value) = false;
  211. }
  212. }
  213. /**
  214. * @brief isContainer
  215. * @return [out] {bool}
  216. */
  217. virtual bool isContainer() override {
  218. return (isIndex(value.index(), VariantIndex::STRVEC) || isIndex(value.index(), VariantIndex::FILEVEC));
  219. }
  220. /**
  221. * @brief insert
  222. * @param value
  223. */
  224. virtual void insert(QString string) override {
  225. if (isIndex(value.index(), VariantIndex::STRVEC)) {
  226. std::get<VariantIndex::STRVEC>(value).push_back(string);
  227. } else {
  228. // Unable to push. Throw?
  229. }
  230. }
  231. /**
  232. * @brief insert
  233. * @param file
  234. */
  235. virtual void insert(KFileData file) override {
  236. if (value.index() == VariantIndex::FILEVEC) {
  237. std::get<VariantIndex::FILEVEC>(value).push_back(file);
  238. } else {
  239. // Unable to push. Throw?
  240. }
  241. }
  242. /**
  243. * @brief remove
  244. * @param value
  245. */
  246. virtual void remove(TypeVariant unwanted_value) override {
  247. if (value.index() == VariantIndex::STRVEC && unwanted_value.index() == VariantIndex::QSTRING) {
  248. auto&& container = std::get<VariantIndex::STRVEC>(value);
  249. auto value_to_remove = std::get<VariantIndex::QSTRING>(unwanted_value);
  250. auto it = std::find_if(container.begin(), container.end(), [&value_to_remove](QString s) {
  251. return (s == value_to_remove);
  252. });
  253. if (it != container.end()) {
  254. container.erase(it);
  255. return;
  256. } else {
  257. throw std::out_of_range("Could not find value requested for removal");
  258. }
  259. } else if (value.index() == VariantIndex::FILEVEC && unwanted_value.index() == VariantIndex::QSTRING) {
  260. auto&& container = std::get<VariantIndex::FILEVEC>(value);
  261. auto file_to_remove = std::get<VariantIndex::QSTRING>(unwanted_value);
  262. auto it = std::find_if(container.begin(), container.end(), [&file_to_remove](Scheduler::KFileData f) {
  263. return (f.name == file_to_remove);
  264. });
  265. if (it != container.end()) {
  266. container.erase(it);
  267. return;
  268. } else {
  269. throw std::out_of_range("Could not find value requested for removal");
  270. }
  271. }
  272. throw std::invalid_argument("The value provided does not match any existing container");
  273. }
  274. /**
  275. * @brief getTypeIndex
  276. * @return
  277. */
  278. virtual uint8_t getTypeIndex() override {
  279. return value.index();
  280. }
  281. private:
  282. QString name;
  283. ArgumentType type;
  284. TypeVariant value;
  285. };
  286. using TaskArguments = std::vector<TaskArgument*>;
  287. /**
  288. * The interface expected to be implemented in all Task types
  289. */
  290. class Task {
  291. public:
  292. Task(){};
  293. Task(KFileData);
  294. Task(QVector<KFileData>);
  295. virtual void setArgument(QString name, TypeVariant arg) = 0;
  296. virtual void addArgument(QString name, Scheduler::KFileData file) = 0;
  297. virtual void addArgument(QString name, QString string) = 0;
  298. virtual void removeArgument(QString name, TypeVariant arg) = 0;
  299. virtual const TaskArguments&& getTaskArguments() = 0;
  300. virtual TaskArgument&& getTaskArgument(QString name) = 0;
  301. virtual const TypeVariant getTaskArgumentValue(QString name) = 0;
  302. virtual ArgumentValues getArgumentValues() = 0;
  303. virtual QVector<QString> getArgumentNames() = 0;
  304. virtual TaskType getType() = 0;
  305. virtual uint32_t getTaskCode() = 0;
  306. virtual void defineTaskArguments() = 0;
  307. virtual void setDefaultValues() = 0;
  308. virtual const QVector<KFileData> getFiles() = 0;
  309. virtual bool hasFiles() = 0;
  310. virtual bool isReady() = 0;
  311. virtual bool isEmpty() {
  312. bool empty = true;
  313. for (const auto& arg : getArgumentValues()) {
  314. if (!arg.isEmpty()) {
  315. empty = false;
  316. }
  317. }
  318. return empty;
  319. }
  320. virtual void clear() = 0;
  321. virtual ~Task(){};
  322. };
  323. } // namespace Scheduler
  324. #endif // __TASK_HPP__