123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- #include <include/task/generic_task.hpp>
- #include <QDebug>
- using namespace Scheduler;
- /**
- * These values contained inside the TaskIndex namespace represent the order in which the tasks are to be stored.
- */
- namespace TaskIndex {
- static const uint8_t HEADER = 0;
- static const uint8_t DESCRIPTION = 1;
- static const uint8_t DATETIME = 2;
- static const uint8_t FILES = 3;
- static const uint8_t USER = 4;
- static const uint8_t IS_VIDEO = 5;
- static const uint8_t RECURRING = 6;
- static const uint8_t NOTIFY = 7;
- } // namespace TaskIndex
- /**
- * @constructor
- */
- GenericTask::GenericTask() {}
- /**
- * @brief GenericTask::defineTaskArguments
- *
- * This method defines all of the arguments that are available to be set for the Task
- *
- */
- void GenericTask::defineTaskArguments() {
- m_arguments.clear();
- m_arguments.emplace_back(std::move(new TaskArgument{"header", Type::TEXT, QString{}}));
- m_arguments.emplace_back(std::move(new TaskArgument{"description", Type::TEXT, TypeVariant{QString{}}}));
- m_arguments.emplace_back(std::move(new TaskArgument{"datetime", Type::TEXT, QString{}}));
- m_arguments.emplace_back(std::move(new TaskArgument{"files", Type::FILEVECTOR, QVector<KFileData>{}}));
- m_arguments.emplace_back(std::move(new TaskArgument{"user", Type::TEXT, QString{}}));
- m_arguments.emplace_back(std::move(new TaskArgument{"is_video", Type::BOOLEAN, bool{}}));
- m_arguments.emplace_back(std::move(new TaskArgument{"recurring", Type::INTEGER, findTaskFrequency("No")}));
- m_arguments.emplace_back(std::move(new TaskArgument{"notify", Type::BOOLEAN, false}));
- }
- /**
- * @brief GenericTask::setArgument
- * @param [in] {QString} name The name of the argment
- * @param [in] {TypeVariant} value The value of the argument
- */
- void GenericTask::setArgument(QString name, TypeVariant value) {
- for (auto&& argument : m_arguments) {
- if (argument->text() == name) {
- argument->setValue(value);
- return;
- }
- }
- }
- /**
- * @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.
- *
- * @brief GenericTask::addArgument
- *
- * @param [in] {QString} name The name of the argument
- * @param [in] {KFileData} file A data structure to be added to a container of files.
- * The KFileData structure contains metadata about a file and
- * its data as a byte array
- */
- void GenericTask::addArgument(QString name, Scheduler::KFileData file) {
- for (const auto& argument : m_arguments) {
- if (argument->text() == name) {
- argument->insert(file);
- return;
- }
- }
- }
- /**
- * @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.
- *
- * @brief GenericTask::removeArgument
- *
- * @param [in] {QString} name The name of the argument, whose value is expected to be a container.
- * @param [in] {TypeVariant} value The value to be removed from the container argument.
- *
- */
- void GenericTask::removeArgument(QString name, Scheduler::TypeVariant value) {
- for (auto&& argument : m_arguments) {
- if (argument->text() == name) {
- if (argument->isContainer()) {
- argument->remove(value);
- } else {
- argument->clear();
- }
- return;
- }
- }
- }
- /**
- * @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.
- *
- * @brief GenericTask::addArgument
- *
- * @param [in] {QString} name The name of the argument
- * @param [in] {QString} string A string value intended to be added to a container of strings
- */
- void GenericTask::addArgument(QString name, QString string) {
- for (const auto& argument : m_arguments) {
- if (argument->text() == name) {
- argument->insert(string);
- return;
- }
- }
- }
- /**
- * @brief GenericTask::getTaskArgument
- *
- * @param [in] {QString} name The name of the argument to retrieve
- * @return [out] {TaskArgument} The argument
- */
- TaskArgument&& GenericTask::getTaskArgument(QString name) {
- for (const auto& argument : m_arguments) {
- if (argument->text() == name) {
- return std::move(*argument);
- }
- }
- throw std::invalid_argument("Argument not found");
- }
- /**
- * @brief GenericTask::getTaskArgumentValue
- *
- * @param [in] {QString} name The name of the argument to retrieve
- * @return [out] {TypeVariant} The value of the argument
- */
- const TypeVariant GenericTask::getTaskArgumentValue(QString name) {
- for (const auto& argument : m_arguments) {
- if (argument->text() == name) {
- return argument->getValue();
- }
- }
- return ""; // Perhaps we should throw
- }
- /**
- * @warning This method does not return any task value whose type is a form of container.
- *
- * @brief GenericTask::getArgumentValues
- * @typedef QVector<QString> is aliased to ArgumentValues
- *
- * @return [out] {ArgumentValues} A vector of strings for all of the arguments that can be represented as a string.
- */
- ArgumentValues GenericTask::getArgumentValues() {
- ArgumentValues values{static_cast<int>(m_arguments.size())};
- for (auto& argument : m_arguments) {
- if (!argument->isContainer()) {
- values.push_back(argument->getStringValue());
- }
- }
- return values;
- }
- /**
- * @warning This method does not necessarily need to return all argument names.
- * In this use case, we are retrieving names to populate options in our
- * ArgType widget, thus we only return names for arguments whose values are
- * to be set or modified using the ArgType widget. For this reason, we have
- * hardcoded the return to be explicit about which arguments names are available.
- *
- * @brief GenericTask::getArgumentNames
- *
- * @return [out] {QVector<QString>} A vector of argument names as strings.
- */
- QVector<QString> GenericTask::getArgumentNames() {
- return QVector<QString>{
- GenericArgs::DESCRIPTION_TYPE,
- GenericArgs::HEADER_TYPE
- };
- }
- /**
- * @warning This method is used to claim ownership of the task's arguments. Use of this method will effectively REMOVE all arguments from
- * the task upon which it is called.
- *
- * @brief GenericTask::getTaskArguments
- * @typedef std::vector<std::unique_ptr<TaskArgument> is aliased to TaskArguments
- *
- * @return [out] {std::vector<std::unique_ptr<TaskArgument>} An R-value reference to a vector of unique pointers to the task's arguments.
- *
- */
- const TaskArguments&& GenericTask::getTaskArguments() { return std::move(m_arguments); }
- /**
- * @brief GenericTask::setDefaultValues
- *
- * Sets default values for the task's arguments
- */
- void GenericTask::setDefaultValues() {
- setArgument("header", TypeVariant{QString{"Generic Task"}});
- }
- /**
- * @brief getType
- *
- * @return [out] {TaskType} The type of task
- */
- Scheduler::TaskType GenericTask::getType() { return Scheduler::TaskType::GENERIC; };
- /**
- * @brief getTaskCode
- *
- * @return [out] {uint32_t} The task bytecode
- */
- uint32_t GenericTask::getTaskCode() { return TaskCode::GENTASKBYTE; };
- /**
- * @brief GenericTask::clear
- *
- * Clears the value of each task argument
- */
- void GenericTask::clear() {
- for (const auto& argument : m_arguments) {
- argument->clear();
- }
- }
- /**
- * @brief GenericTask::hasFiles
- *
- * @return [out] {bool} Indicates whether the task has files.
- */
- bool GenericTask::hasFiles() {
- return !std::get<VariantIndex::FILEVEC>(getTaskArgumentValue("files")).empty();
- }
- /**
- * @brief GenericTask::hasFiles
- *
- * @return [out] {QVector<KFileData>} A vector of data structures representing file metadata and the file data as bytes.
- */
- const QVector<Scheduler::KFileData> GenericTask::getFiles() {
- return std::get<VariantIndex::FILEVEC>(getTaskArgumentValue("files"));
- }
- /**
- * @brief GenericTask::isReady
- *
- * @return [out] {bool} A boolean value indicating whether the minimal requirements sufficient to appropriately
- * perform the task have been met.
- */
- bool GenericTask::isReady() {
- return std::get<VariantIndex::QSTRING>(
- getTaskArgumentValue("datetime")
- ) // GenericTask only needs a datetime value to run
- .size() > 0;
- }
- /**
- * @destructor
- */
- GenericTask::~GenericTask() {
- }
|