UI: Programs: Improve app's auto-block time handling

This commit is contained in:
Nodir Temirkhodjaev 2022-02-26 13:22:16 +03:00
parent 4c964e7da2
commit 6e1d131183
2 changed files with 34 additions and 23 deletions

View File

@ -11,6 +11,7 @@
#include <driver/drivermanager.h> #include <driver/drivermanager.h>
#include <fortsettings.h> #include <fortsettings.h>
#include <log/logentryblocked.h> #include <log/logentryblocked.h>
#include <log/logmanager.h>
#include <manager/envmanager.h> #include <manager/envmanager.h>
#include <manager/windowmanager.h> #include <manager/windowmanager.h>
#include <task/taskinfo.h> #include <task/taskinfo.h>
@ -34,6 +35,9 @@ const QLoggingCategory LC("conf");
constexpr int DATABASE_USER_VERSION = 11; constexpr int DATABASE_USER_VERSION = 11;
constexpr int APP_END_TIMER_INTERVAL_MIN = 100;
constexpr int APP_END_TIMER_INTERVAL_MAX = 24 * 60 * 60 * 1000; // 1 day
const char *const sqlSelectAddressGroups = "SELECT addr_group_id, include_all, exclude_all," const char *const sqlSelectAddressGroups = "SELECT addr_group_id, include_all, exclude_all,"
" include_zones, exclude_zones," " include_zones, exclude_zones,"
" include_text, exclude_text" " include_text, exclude_text"
@ -121,9 +125,8 @@ const char *const sqlSelectApps = "SELECT"
" JOIN app_group g ON g.app_group_id = t.app_group_id" " JOIN app_group g ON g.app_group_id = t.app_group_id"
" LEFT JOIN app_alert alert ON alert.app_id = t.app_id;"; " LEFT JOIN app_alert alert ON alert.app_id = t.app_id;";
const char *const sqlSelectEndAppsCount = "SELECT COUNT(*) FROM app" const char *const sqlSelectMinEndApp = "SELECT MIN(end_time) FROM app"
" WHERE end_time IS NOT NULL AND end_time != 0" " WHERE end_time != 0 AND blocked = 0;";
" AND blocked = 0;";
const char *const sqlSelectEndedApps = "SELECT t.app_id, g.order_index as group_index," const char *const sqlSelectEndedApps = "SELECT t.app_id, g.order_index as group_index,"
" t.path, t.name, t.use_group_perm, t.apply_child" " t.path, t.name, t.use_group_perm, t.apply_child"
@ -372,8 +375,8 @@ ConfManager::ConfManager(const QString &filePath, QObject *parent, quint32 openF
connect(&m_appChangedTimer, &QTimer::timeout, this, &ConfManager::appChanged); connect(&m_appChangedTimer, &QTimer::timeout, this, &ConfManager::appChanged);
connect(&m_appUpdatedTimer, &QTimer::timeout, this, &ConfManager::appUpdated); connect(&m_appUpdatedTimer, &QTimer::timeout, this, &ConfManager::appUpdated);
m_appEndTimer.setInterval(5 * 60 * 1000); // 5 minutes m_appEndTimer.setSingleShot(true);
connect(&m_appEndTimer, &QTimer::timeout, this, &ConfManager::checkAppEndTimes); connect(&m_appEndTimer, &QTimer::timeout, this, &ConfManager::updateAppEndTimes);
} }
ConfManager::~ConfManager() ConfManager::~ConfManager()
@ -414,7 +417,28 @@ void ConfManager::setUp()
void ConfManager::setupAppEndTimer() void ConfManager::setupAppEndTimer()
{ {
m_appEndTimer.start(); auto logManager = IoC<LogManager>();
connect(logManager, &LogManager::systemTimeChanged, this, &ConfManager::updateAppEndTimer);
updateAppEndTimer();
}
void ConfManager::updateAppEndTimer()
{
const qint64 endTimeMsecs = sqliteDb()->executeEx(sqlSelectMinEndApp).toLongLong();
if (endTimeMsecs != 0) {
const qint64 currentMsecs = QDateTime::currentMSecsSinceEpoch();
const qint64 deltaMsecs = endTimeMsecs - currentMsecs;
const int interval =
qMax((deltaMsecs > 0 ? qMin(deltaMsecs, APP_END_TIMER_INTERVAL_MAX) : 0),
APP_END_TIMER_INTERVAL_MIN);
m_appEndTimer.start(interval);
} else {
m_appEndTimer.stop();
}
} }
void ConfManager::initConfToEdit() void ConfManager::initConfToEdit()
@ -763,7 +787,7 @@ bool ConfManager::updateApp(qint64 appId, const QString &appPath, const QString
if (ok) { if (ok) {
if (!endTime.isNull()) { if (!endTime.isNull()) {
m_appEndTimer.start(); updateAppEndTimer();
} }
emitAppUpdated(); emitAppUpdated();
@ -838,11 +862,6 @@ bool ConfManager::walkApps(const std::function<walkAppsCallback> &func)
return true; return true;
} }
int ConfManager::appEndsCount()
{
return sqliteDb()->executeEx(sqlSelectEndAppsCount).toInt();
}
void ConfManager::updateAppEndTimes() void ConfManager::updateAppEndTimes()
{ {
SqliteStmt stmt; SqliteStmt stmt;
@ -862,15 +881,8 @@ void ConfManager::updateAppEndTimes()
updateApp(appId, appPath, appName, /*endTime=*/ {}, groupIndex, useGroupPerm, applyChild, updateApp(appId, appPath, appName, /*endTime=*/ {}, groupIndex, useGroupPerm, applyChild,
/*blocked=*/true); /*blocked=*/true);
} }
}
void ConfManager::checkAppEndTimes() updateAppEndTimer();
{
if (appEndsCount() == 0) {
m_appEndTimer.stop();
} else {
updateAppEndTimes();
}
} }
bool ConfManager::addZone(const QString &zoneName, const QString &sourceCode, const QString &url, bool ConfManager::addZone(const QString &zoneName, const QString &sourceCode, const QString &url,
@ -1078,7 +1090,7 @@ bool ConfManager::addOrUpdateApp(const QString &appPath, const QString &appName,
if (ok) { if (ok) {
if (!endTime.isNull()) { if (!endTime.isNull()) {
m_appEndTimer.start(); updateAppEndTimer();
} }
emitAppChanged(); emitAppChanged();

View File

@ -70,9 +70,7 @@ public:
bool walkApps(const std::function<walkAppsCallback> &func) override; bool walkApps(const std::function<walkAppsCallback> &func) override;
int appEndsCount();
void updateAppEndTimes(); void updateAppEndTimes();
void checkAppEndTimes();
virtual bool addZone(const QString &zoneName, const QString &sourceCode, const QString &url, virtual bool addZone(const QString &zoneName, const QString &sourceCode, const QString &url,
const QString &formData, bool enabled, bool customUrl, int &zoneId); const QString &formData, bool enabled, bool customUrl, int &zoneId);
@ -107,6 +105,7 @@ signals:
protected: protected:
virtual void setupAppEndTimer(); virtual void setupAppEndTimer();
void updateAppEndTimer();
void setConf(FirewallConf *newConf); void setConf(FirewallConf *newConf);
FirewallConf *createConf(); FirewallConf *createConf();