From ed942cc4d4d6d7f4d06e95cb4286fd0ce76e5936 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Sun, 21 Feb 2021 22:44:34 +0500 Subject: [PATCH] UI: Connections: Add "Auto Scroll" option. --- src/ui/form/conn/connectionswindow.cpp | 30 +++++++++++++++++++++++++- src/ui/form/conn/connectionswindow.h | 2 ++ src/ui/fortsettings.h | 3 +++ src/ui/model/connlistmodel.cpp | 18 ++++++++++++++-- src/ui/model/connlistmodel.h | 2 ++ src/ui/stat/statmanager.cpp | 26 +++++++++++----------- src/ui/stat/statmanager.h | 4 +--- 7 files changed, 65 insertions(+), 20 deletions(-) diff --git a/src/ui/form/conn/connectionswindow.cpp b/src/ui/form/conn/connectionswindow.cpp index 7a1185ed..e3ba6a28 100644 --- a/src/ui/form/conn/connectionswindow.cpp +++ b/src/ui/form/conn/connectionswindow.cpp @@ -74,6 +74,7 @@ void ConnectionsWindow::onRetranslateUi() m_btLogOptions->setText(tr("Options")); m_cbLogAllowedIp->setText(tr("Collect allowed connections")); m_cbLogBlockedIp->setText(tr("Collect blocked connections")); + m_cbAutoScroll->setText(tr("Auto Scroll")); connListModel()->refresh(); @@ -153,9 +154,11 @@ void ConnectionsWindow::setupLogOptions() { setupLogAllowedIp(); setupLogBlockedIp(); + setupAutoScroll(); // Menu - const QList menuWidgets = { m_cbLogAllowedIp, m_cbLogBlockedIp }; + const QList menuWidgets = { m_cbLogAllowedIp, m_cbLogBlockedIp, + ControlUtil::createSeparator(), m_cbAutoScroll }; auto layout = ControlUtil::createLayoutByWidgets(menuWidgets); auto menu = ControlUtil::createMenuByLayout(layout, this); @@ -188,6 +191,31 @@ void ConnectionsWindow::setupLogBlockedIp() }); } +void ConnectionsWindow::setupAutoScroll() +{ + const auto syncAutoScroll = [&] { + if (settings()->connAutoScroll()) { + connect(connListModel(), &QAbstractItemModel::rowsInserted, m_connListView, + &QAbstractItemView::scrollToBottom); + } else { + disconnect(connListModel(), &QAbstractItemModel::rowsInserted, m_connListView, + &QAbstractItemView::scrollToBottom); + } + }; + + syncAutoScroll(); + + m_cbAutoScroll = ControlUtil::createCheckBox( + settings()->connAutoScroll(), [&, syncAutoScroll](bool checked) { + if (settings()->connAutoScroll() == checked) + return; + + settings()->setConnAutoScroll(checked); + + syncAutoScroll(); + }); +} + void ConnectionsWindow::setupTableConnList() { m_connListView = new TableView(); diff --git a/src/ui/form/conn/connectionswindow.h b/src/ui/form/conn/connectionswindow.h index 2d774a69..f147581c 100644 --- a/src/ui/form/conn/connectionswindow.h +++ b/src/ui/form/conn/connectionswindow.h @@ -36,6 +36,7 @@ private: void setupLogOptions(); void setupLogAllowedIp(); void setupLogBlockedIp(); + void setupAutoScroll(); void setupTableConnList(); void setupTableConnListHeader(); void setupAppInfoRow(); @@ -64,6 +65,7 @@ private: QPushButton *m_btLogOptions = nullptr; QCheckBox *m_cbLogAllowedIp = nullptr; QCheckBox *m_cbLogBlockedIp = nullptr; + QCheckBox *m_cbAutoScroll = nullptr; TableView *m_connListView = nullptr; AppInfoRow *m_appInfoRow = nullptr; }; diff --git a/src/ui/fortsettings.h b/src/ui/fortsettings.h index a2b63e05..e163c439 100644 --- a/src/ui/fortsettings.h +++ b/src/ui/fortsettings.h @@ -220,6 +220,9 @@ public: QByteArray connListHeader() const { return iniByteArray("connWindow/connListHeader"); } void setConnListHeader(const QByteArray &v) { setIniValue("connWindow/connListHeader", v); } + bool connAutoScroll() const { return iniBool("connWindow/autoScroll"); } + void setConnAutoScroll(bool on) { setIniValue("connWindow/autoScroll", on); } + qint32 quotaDayAlerted() const { return iniInt("quota/dayAlerted"); } void setQuotaDayAlerted(qint32 v) { setIniValue("quota/dayAlerted", v); } diff --git a/src/ui/model/connlistmodel.cpp b/src/ui/model/connlistmodel.cpp index a78c0741..ad1625e0 100644 --- a/src/ui/model/connlistmodel.cpp +++ b/src/ui/model/connlistmodel.cpp @@ -32,10 +32,25 @@ void ConnListModel::setAppInfoCache(AppInfoCache *v) void ConnListModel::handleLogBlockedIp(const LogEntryBlockedIp &entry, qint64 unixTime) { + const int row = rowCount(); + + beginInsertRows(QModelIndex(), row, row); + if (statManager()->logBlockedIp(entry.inbound(), entry.blockReason(), entry.ipProto(), entry.localPort(), entry.remotePort(), entry.localIp(), entry.remoteIp(), entry.pid(), entry.path(), unixTime)) { - reset(); + invalidateRowCache(); + ++m_connBlockInc; + } + + endInsertRows(); + + constexpr int connBlockIncMax = 100; + if (m_connBlockInc >= connBlockIncMax) { + m_connBlockInc = 0; + if (statManager()->deleteOldConnBlock()) { + reset(); + } } } @@ -144,7 +159,6 @@ void ConnListModel::deleteConn(qint64 connId, bool blocked, int row) if (statManager()->deleteConn(connId, blocked)) { invalidateRowCache(); - removeRow(row); } endRemoveRows(); diff --git a/src/ui/model/connlistmodel.h b/src/ui/model/connlistmodel.h index 74636198..20a73af2 100644 --- a/src/ui/model/connlistmodel.h +++ b/src/ui/model/connlistmodel.h @@ -72,6 +72,8 @@ protected: QString sqlBase() const override; private: + int m_connBlockInc = 999999999; // to trigger on first check + StatManager *m_statManager = nullptr; AppInfoCache *m_appInfoCache = nullptr; diff --git a/src/ui/stat/statmanager.cpp b/src/ui/stat/statmanager.cpp index 9b8774bc..0fac9299 100644 --- a/src/ui/stat/statmanager.cpp +++ b/src/ui/stat/statmanager.cpp @@ -363,11 +363,6 @@ bool StatManager::logBlockedIp(bool inbound, quint8 blockReason, quint8 ipProto, ok = (connId > 0); if (ok) { ok = createConnBlock(connId, blockReason); - constexpr int connBlockIncMax = 100; - if (ok && ++m_connBlockInc > connBlockIncMax) { - m_connBlockInc = 0; - deleteOldConnBlock(m_conf->blockedIpKeepCount()); - } } } @@ -389,6 +384,18 @@ void StatManager::deleteStatApp(qint64 appId) m_sqliteDb->commitTransaction(); } +bool StatManager::deleteOldConnBlock() +{ + const int keepCount = m_conf->blockedIpKeepCount(); + const qint64 connId = + m_sqliteDb->executeEx(StatSql::sqlSelectOldConnBlock, { keepCount }).toLongLong(); + if (connId > 0) { + deleteRangeConnBlock(1, connId); + return true; + } + return false; +} + bool StatManager::deleteConn(qint64 connId, bool blocked) { m_sqliteDb->beginTransaction(); @@ -562,15 +569,6 @@ bool StatManager::createConnBlock(qint64 connId, quint8 blockReason) return ok; } -void StatManager::deleteOldConnBlock(int keepCount) -{ - const qint64 connId = - m_sqliteDb->executeEx(StatSql::sqlSelectOldConnBlock, { keepCount }).toLongLong(); - if (connId > 0) { - deleteRangeConnBlock(1, connId); - } -} - void StatManager::deleteRangeConnBlock(qint64 connIdFrom, qint64 connIdTo) { deleteAppStmtList(getId2Stmt(StatSql::sqlSelectDeletedRangeConnAppPaths, connIdFrom, connIdTo), diff --git a/src/ui/stat/statmanager.h b/src/ui/stat/statmanager.h index 65fa639b..e3aea2c6 100644 --- a/src/ui/stat/statmanager.h +++ b/src/ui/stat/statmanager.h @@ -41,6 +41,7 @@ public: void deleteStatApp(qint64 appId); + bool deleteOldConnBlock(); bool deleteConn(qint64 connId, bool blocked); void deleteConns(); @@ -87,7 +88,6 @@ private: qint64 createConn(bool inbound, quint8 ipProto, quint16 localPort, quint16 remotePort, quint32 localIp, quint32 remoteIp, quint32 pid, qint64 unixTime, qint64 appId); bool createConnBlock(qint64 connId, quint8 blockReason); - void deleteOldConnBlock(int keepCount); void deleteRangeConnBlock(qint64 connIdFrom, qint64 connIdTo); void deleteAppStmtList(SqliteStmt *stmtAppPaths, const QStmtList &stmtList); @@ -114,8 +114,6 @@ private: qint32 m_lastTrafMonth = 0; qint32 m_lastTick = 0; - int m_connBlockInc = 999999999; // to trigger on first check - QuotaManager *m_quotaManager = nullptr; const FirewallConf *m_conf = nullptr;