Browse Source

improving workflow surrounding task validation on value of datetime

logicp 4 years ago
parent
commit
ba060da9eb
3 changed files with 17 additions and 17 deletions
  1. 6 0
      headers/util.hpp
  2. 7 10
      src/argdialog.cpp
  3. 4 7
      src/mainwindow.cpp

+ 6 - 0
headers/util.hpp

@@ -5,6 +5,7 @@
 #include <QQueue>
 #include <QString>
 #include <QMessageBox>
+#include <QDateTime>
 #include <QVector>
 #include <charconv>
 #include <string>
@@ -427,5 +428,10 @@ inline void infoMessageBox(QString text, QString title = "KYGUI") {
   box.exec();
 }
 } // namespace UI
+namespace TimeUtils {
+inline QString getTime() { return QDateTime::currentDateTime().toString("hh:mm:ss"); }
+inline uint unixtime() { return QDateTime::currentDateTime().toTime_t(); }
+} // namespace TimeUtils
+
 }  // namespace
 #endif  // UTIL_HPP

+ 7 - 10
src/argdialog.cpp

@@ -1,7 +1,6 @@
 #include <include/ui/argdialog.h>
 #include <ui_argdialog.h>
 #include <QCalendarWidget>
-#include <QDateTime>
 #include <QDebug>
 #include <QIODevice>
 #include <QMimeDatabase>
@@ -126,13 +125,7 @@ ArgDialog::ArgDialog(QWidget *parent) : QDialog(parent), ui(new Ui::ArgDialog),
   ui->dateTime->setDateTime(QDateTime::currentDateTime());
 
   QObject::connect(ui->dateTime, &QDateTimeEdit::dateTimeChanged, this, [this]() {
-    auto date_time = ui->dateTime->dateTime();
-    if (date_time > QDateTime::currentDateTime()) {
-      m_task->setArgument("datetime", QString::number(date_time.toTime_t()));
-      qDebug() << "Time changed to" << date_time;
-    } else {
-      UI::infoMessageBox("Unable to schedule tasks in the past!", "DateTime Error");
-    }
+    m_task->setArgument("datetime", QString::number(ui->dateTime->dateTime().toTime_t()));
   });
 
   QObject::connect(ui->argCommandButtons,
@@ -141,6 +134,12 @@ ArgDialog::ArgDialog(QWidget *parent) : QDialog(parent), ui(new Ui::ArgDialog),
                    this, [this](QAbstractButton *button) {
                      if (button->text() == "Save") {
                        setTaskArguments();
+                       uint task_date_time = std::get<Scheduler::VariantIndex::QSTRING>(
+                                                 m_task->getTaskArgumentValue("datetime")).toUInt();
+                       if (task_date_time <= TimeUtils::unixtime()) {
+                         UI::infoMessageBox("Unable to schedule tasks in the past!", "DateTime Error");
+                         return;
+                       }
                        if (m_task->isReady()) {
                          emit ArgDialog::taskRequestReady(m_task);
                        } else {
@@ -229,10 +228,8 @@ void ArgDialog::addFile(QString path) {
  */
 void ArgDialog::clearPost() {
   QDateTime date_time = QDateTime::currentDateTime();
-  ui->dateTime->setDateTime(date_time);
   m_task->clear();
   m_task->setDefaultValues();
-  m_task->setArgument("datetime", QString::number(date_time.toTime_t()));
   m_task->setArgument("user", ui->user->currentText());
   ui->argType->setCurrentIndex(0);
   ui->argList->setRowCount(0);

+ 4 - 7
src/mainwindow.cpp

@@ -1,6 +1,4 @@
 #include <include/ui/mainwindow.h>
-
-#include <QDateTime>
 #include <QDebug>
 #include <QLayout>
 #include <QScrollBar>
@@ -45,9 +43,8 @@ int getLikeEventNum(QString event, QList<QString> events) {
   return hits;
 }
 
-QString getTime() { return QDateTime::currentDateTime().toString("hh:mm:ss"); }
 QString timestampPrefix() {
-  return QDateTime::currentDateTime().toString("hh:mm:ss") + " - ";
+  return TimeUtils::getTime() + " - ";
 }
 
 /**
@@ -283,7 +280,7 @@ void MainWindow::updateMessages(int t, const QString& message, StringVec v) {
     qDebug() << "Updating process list";
     m_processes.push_back(Process{.name = v.at(1),
                                   .state = ProcessState::PENDING,
-                                  .start = getTime(),
+                                  .start = TimeUtils::getTime(),
                                   .id = v.at(2)});
     int row = 0;
     for (const auto& process : m_processes) {
@@ -419,7 +416,7 @@ void MainWindow::MessageParser::updateProcessResult(
   // unique identifier
   for (int i = window->m_processes.size() - 1; i >= 0; i--) {
     if (window->m_processes.at(i).id == id) {
-      window->m_processes.at(i).end = getTime();
+      window->m_processes.at(i).end = TimeUtils::getTime();
       window->m_processes.at(i).state =
           !error ? ProcessState::SUCCEEDED : ProcessState::FAILED;
       window->m_processes.at(i).result = result;
@@ -459,7 +456,7 @@ QString MainWindow::MessageParser::handleEventMessage(QString message,
           Process new_process{
               .name = app_name,
               .state = !error ? ProcessState::SUCCEEDED : ProcessState::FAILED,
-              .start = getTime(),
+              .start = TimeUtils::getTime(),
               .id = "Scheduled task",
               .error = error ? v.at(3) : "No errors reported"};
           if (v.count() > 2 && !v.at(2).isEmpty()) {