diff --git a/src/ui/task/taskinfo.cpp b/src/ui/task/taskinfo.cpp index ad72d2c5..af79d925 100644 --- a/src/ui/task/taskinfo.cpp +++ b/src/ui/task/taskinfo.cpp @@ -64,6 +64,11 @@ void TaskInfo::setLastRun(const QDateTime &lastRun) } } +QDateTime TaskInfo::plannedRun() const +{ + return m_lastRun.addSecs(m_intervalHours * 60 * 60); +} + void TaskInfo::setLastSuccess(const QDateTime &lastSuccess) { if (m_lastSuccess != lastSuccess) { @@ -135,6 +140,11 @@ TaskInfo::TaskType TaskInfo::stringToType(const QString &name) typeEnum.keyToValue(name.toLatin1())); } +QDateTime TaskInfo::now() +{ + return QDateTime::currentDateTime(); +} + void TaskInfo::run() { cancel(); @@ -163,9 +173,9 @@ void TaskInfo::handleFinished(bool success) { if (!m_taskWorker) return; - setLastRun(QDateTime::currentDateTime()); + setLastRun(now()); if (success) { - setLastSuccess(QDateTime::currentDateTime()); + setLastSuccess(lastRun()); } emit workFinished(success); diff --git a/src/ui/task/taskinfo.h b/src/ui/task/taskinfo.h index 0e84301d..b7091be9 100644 --- a/src/ui/task/taskinfo.h +++ b/src/ui/task/taskinfo.h @@ -41,6 +41,8 @@ public: QDateTime lastRun() const { return m_lastRun; } void setLastRun(const QDateTime &lastRun); + QDateTime plannedRun() const; + QDateTime lastSuccess() const { return m_lastSuccess; } void setLastSuccess(const QDateTime &lastSuccess); @@ -55,6 +57,8 @@ public: static QString typeToString(TaskInfo::TaskType type); static TaskInfo::TaskType stringToType(const QString &name); + static QDateTime now(); + signals: void enabledChanged(); void intervalHoursChanged(); diff --git a/src/ui/task/taskmanager.cpp b/src/ui/task/taskmanager.cpp index 9cdd810b..ef6a1ef2 100644 --- a/src/ui/task/taskmanager.cpp +++ b/src/ui/task/taskmanager.cpp @@ -11,6 +11,11 @@ TaskManager::TaskManager(FortManager *fortManager, m_fortManager(fortManager) { setupTasks(); + + connect(&m_timer, &QTimer::timeout, + this, &TaskManager::runExpiredTasks); + + m_timer.setSingleShot(true); } QQmlListProperty TaskManager::taskInfos() @@ -43,9 +48,11 @@ void TaskManager::loadSettings(const FortSettings *fortSettings) taskInfo->setRawData(taskData); } } + + runExpiredTasks(); } -bool TaskManager::saveSettings(FortSettings *fortSettings) const +bool TaskManager::saveSettings(FortSettings *fortSettings) { TasksMap tasksMap; QByteArray taskData; @@ -59,6 +66,8 @@ bool TaskManager::saveSettings(FortSettings *fortSettings) const tasksMap.insert(taskName, taskData); } + runExpiredTasks(); + return fortSettings->setTasks(tasksMap); } @@ -73,3 +82,25 @@ void TaskManager::handleTaskFinished(bool success) saveSettings(m_fortManager->fortSettings()); } + +void TaskManager::runExpiredTasks() +{ + const QDateTime now = TaskInfo::now(); + bool anyTaskEnabled = false; + + foreach (TaskInfo *taskInfo, m_taskInfos) { + if (taskInfo->enabled()) { + anyTaskEnabled = true; + + if (now > taskInfo->plannedRun()) { + taskInfo->run(); + } + } + } + + if (anyTaskEnabled) { + m_timer.start(60 * 60 * 1000); // hour + } else { + m_timer.stop(); + } +} diff --git a/src/ui/task/taskmanager.h b/src/ui/task/taskmanager.h index a3c1c25d..8874afdc 100644 --- a/src/ui/task/taskmanager.h +++ b/src/ui/task/taskmanager.h @@ -3,6 +3,7 @@ #include #include +#include class FortManager; class FortSettings; @@ -28,10 +29,11 @@ signals: public slots: void loadSettings(const FortSettings *fortSettings); - bool saveSettings(FortSettings *fortSettings) const; + bool saveSettings(FortSettings *fortSettings); private slots: void handleTaskFinished(bool success); + void runExpiredTasks(); private: void setupTasks(); @@ -42,6 +44,8 @@ private: FortManager *m_fortManager; QList m_taskInfos; + + QTimer m_timer; }; #endif // TASKMANAGER_H