generic_task.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #include <include/task/generic_task.hpp>
  2. #include <QDebug>
  3. using namespace Scheduler;
  4. /**
  5. * These values contained inside the TaskIndex namespace represent the order in which the tasks are to be stored.
  6. */
  7. namespace TaskIndex {
  8. static const uint8_t HEADER = 0;
  9. static const uint8_t DESCRIPTION = 1;
  10. static const uint8_t DATETIME = 2;
  11. static const uint8_t FILES = 3;
  12. static const uint8_t USER = 4;
  13. static const uint8_t IS_VIDEO = 5;
  14. static const uint8_t RECURRING = 6;
  15. static const uint8_t NOTIFY = 7;
  16. } // namespace TaskIndex
  17. /**
  18. * @constructor
  19. */
  20. GenericTask::GenericTask() {}
  21. /**
  22. * @brief GenericTask::defineTaskArguments
  23. *
  24. * This method defines all of the arguments that are available to be set for the Task
  25. *
  26. */
  27. void GenericTask::defineTaskArguments() {
  28. m_arguments.clear();
  29. m_arguments.emplace_back(std::move(new TaskArgument{"header", Type::TEXT, QString{}}));
  30. m_arguments.emplace_back(std::move(new TaskArgument{"description", Type::TEXT, TypeVariant{QString{}}}));
  31. m_arguments.emplace_back(std::move(new TaskArgument{"datetime", Type::TEXT, QString{}}));
  32. m_arguments.emplace_back(std::move(new TaskArgument{"files", Type::FILEVECTOR, QVector<KFileData>{}}));
  33. m_arguments.emplace_back(std::move(new TaskArgument{"user", Type::TEXT, QString{}}));
  34. m_arguments.emplace_back(std::move(new TaskArgument{"is_video", Type::BOOLEAN, bool{}}));
  35. m_arguments.emplace_back(std::move(new TaskArgument{"recurring", Type::INTEGER, findTaskFrequency("No")}));
  36. m_arguments.emplace_back(std::move(new TaskArgument{"notify", Type::BOOLEAN, false}));
  37. }
  38. /**
  39. * @brief GenericTask::setArgument
  40. * @param [in] {QString} name The name of the argment
  41. * @param [in] {TypeVariant} value The value of the argument
  42. */
  43. void GenericTask::setArgument(QString name, TypeVariant value) {
  44. for (auto&& argument : m_arguments) {
  45. if (argument->text() == name) {
  46. argument->setValue(value);
  47. return;
  48. }
  49. }
  50. }
  51. /**
  52. * @warning This method is used to add values to an argument, and can only be used on arguments whose type is a form of container.
  53. *
  54. * @brief GenericTask::addArgument
  55. *
  56. * @param [in] {QString} name The name of the argument
  57. * @param [in] {KFileData} file A data structure to be added to a container of files.
  58. * The KFileData structure contains metadata about a file and
  59. * its data as a byte array
  60. */
  61. void GenericTask::addArgument(QString name, Scheduler::KFileData file) {
  62. for (const auto& argument : m_arguments) {
  63. if (argument->text() == name) {
  64. argument->insert(file);
  65. return;
  66. }
  67. }
  68. }
  69. /**
  70. * @warning This method is used to remove a value to an argument, and can only be used on arguments whose type is a form of container.
  71. *
  72. * @brief GenericTask::removeArgument
  73. *
  74. * @param [in] {QString} name The name of the argument, whose value is expected to be a container.
  75. * @param [in] {TypeVariant} value The value to be removed from the container argument.
  76. *
  77. */
  78. void GenericTask::removeArgument(QString name, Scheduler::TypeVariant value) {
  79. for (auto&& argument : m_arguments) {
  80. if (argument->text() == name) {
  81. if (argument->isContainer()) {
  82. argument->remove(value);
  83. } else {
  84. argument->clear();
  85. }
  86. return;
  87. }
  88. }
  89. }
  90. /**
  91. * @warning This method is used to add values to an argument, and can only be used on arguments whose type is a form of container.
  92. *
  93. * @brief GenericTask::addArgument
  94. *
  95. * @param [in] {QString} name The name of the argument
  96. * @param [in] {QString} string A string value intended to be added to a container of strings
  97. */
  98. void GenericTask::addArgument(QString name, QString string) {
  99. for (const auto& argument : m_arguments) {
  100. if (argument->text() == name) {
  101. argument->insert(string);
  102. return;
  103. }
  104. }
  105. }
  106. /**
  107. * @brief GenericTask::getTaskArgument
  108. *
  109. * @param [in] {QString} name The name of the argument to retrieve
  110. * @return [out] {TaskArgument} The argument
  111. */
  112. TaskArgument&& GenericTask::getTaskArgument(QString name) {
  113. for (const auto& argument : m_arguments) {
  114. if (argument->text() == name) {
  115. return std::move(*argument);
  116. }
  117. }
  118. throw std::invalid_argument("Argument not found");
  119. }
  120. /**
  121. * @brief GenericTask::getTaskArgumentValue
  122. *
  123. * @param [in] {QString} name The name of the argument to retrieve
  124. * @return [out] {TypeVariant} The value of the argument
  125. */
  126. const TypeVariant GenericTask::getTaskArgumentValue(QString name) {
  127. for (const auto& argument : m_arguments) {
  128. if (argument->text() == name) {
  129. return argument->getValue();
  130. }
  131. }
  132. return ""; // Perhaps we should throw
  133. }
  134. /**
  135. * @warning This method does not return any task value whose type is a form of container.
  136. *
  137. * @brief GenericTask::getArgumentValues
  138. * @typedef QVector<QString> is aliased to ArgumentValues
  139. *
  140. * @return [out] {ArgumentValues} A vector of strings for all of the arguments that can be represented as a string.
  141. */
  142. ArgumentValues GenericTask::getArgumentValues() {
  143. ArgumentValues values{static_cast<int>(m_arguments.size())};
  144. for (auto& argument : m_arguments) {
  145. if (!argument->isContainer()) {
  146. values.push_back(argument->getStringValue());
  147. }
  148. }
  149. return values;
  150. }
  151. /**
  152. * @warning This method does not necessarily need to return all argument names.
  153. * In this use case, we are retrieving names to populate options in our
  154. * ArgType widget, thus we only return names for arguments whose values are
  155. * to be set or modified using the ArgType widget. For this reason, we have
  156. * hardcoded the return to be explicit about which arguments names are available.
  157. *
  158. * @brief GenericTask::getArgumentNames
  159. *
  160. * @return [out] {QVector<QString>} A vector of argument names as strings.
  161. */
  162. QVector<QString> GenericTask::getArgumentNames() {
  163. return QVector<QString>{
  164. GenericArgs::DESCRIPTION_TYPE,
  165. GenericArgs::HEADER_TYPE
  166. };
  167. }
  168. /**
  169. * @warning This method is used to claim ownership of the task's arguments. Use of this method will effectively REMOVE all arguments from
  170. * the task upon which it is called.
  171. *
  172. * @brief GenericTask::getTaskArguments
  173. * @typedef std::vector<std::unique_ptr<TaskArgument> is aliased to TaskArguments
  174. *
  175. * @return [out] {std::vector<std::unique_ptr<TaskArgument>} An R-value reference to a vector of unique pointers to the task's arguments.
  176. *
  177. */
  178. const TaskArguments&& GenericTask::getTaskArguments() { return std::move(m_arguments); }
  179. /**
  180. * @brief GenericTask::setDefaultValues
  181. *
  182. * Sets default values for the task's arguments
  183. */
  184. void GenericTask::setDefaultValues() {
  185. setArgument("header", TypeVariant{QString{"Generic Task"}});
  186. }
  187. /**
  188. * @brief getType
  189. *
  190. * @return [out] {TaskType} The type of task
  191. */
  192. Scheduler::TaskType GenericTask::getType() { return Scheduler::TaskType::GENERIC; };
  193. /**
  194. * @brief getTaskCode
  195. *
  196. * @return [out] {uint32_t} The task bytecode
  197. */
  198. uint32_t GenericTask::getTaskCode() { return TaskCode::GENTASKBYTE; };
  199. /**
  200. * @brief GenericTask::clear
  201. *
  202. * Clears the value of each task argument
  203. */
  204. void GenericTask::clear() {
  205. for (const auto& argument : m_arguments) {
  206. argument->clear();
  207. }
  208. }
  209. /**
  210. * @brief GenericTask::hasFiles
  211. *
  212. * @return [out] {bool} Indicates whether the task has files.
  213. */
  214. bool GenericTask::hasFiles() {
  215. return !std::get<VariantIndex::FILEVEC>(getTaskArgumentValue("files")).empty();
  216. }
  217. /**
  218. * @brief GenericTask::hasFiles
  219. *
  220. * @return [out] {QVector<KFileData>} A vector of data structures representing file metadata and the file data as bytes.
  221. */
  222. const QVector<Scheduler::KFileData> GenericTask::getFiles() {
  223. return std::get<VariantIndex::FILEVEC>(getTaskArgumentValue("files"));
  224. }
  225. /**
  226. * @brief GenericTask::isReady
  227. *
  228. * @return [out] {bool} A boolean value indicating whether the minimal requirements sufficient to appropriately
  229. * perform the task have been met.
  230. */
  231. bool GenericTask::isReady() {
  232. return std::get<VariantIndex::QSTRING>(
  233. getTaskArgumentValue("datetime")
  234. ) // GenericTask only needs a datetime value to run
  235. .size() > 0;
  236. }
  237. /**
  238. * @destructor
  239. */
  240. GenericTask::~GenericTask() {
  241. }