From decfcb2e804f171c406200323b0bd23bf2b5e15d Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Tue, 28 Nov 2023 12:39:02 +0300 Subject: [PATCH] UI: Options: AppGroup: Add "Kill Process" option --- src/ui/conf/appgroup.cpp | 11 ++++++ src/ui/conf/appgroup.h | 4 ++ src/ui/conf/confmanager.cpp | 25 ++++++------ src/ui/conf/migrations/1.sql | 1 + src/ui/form/opt/pages/applicationspage.cpp | 44 ++++++++++++++++------ src/ui/form/opt/pages/applicationspage.h | 5 ++- src/ui/util/conf/confutil.cpp | 8 ++++ 7 files changed, 73 insertions(+), 25 deletions(-) diff --git a/src/ui/conf/appgroup.cpp b/src/ui/conf/appgroup.cpp index 189c01d6..a07cb334 100644 --- a/src/ui/conf/appgroup.cpp +++ b/src/ui/conf/appgroup.cpp @@ -137,6 +137,14 @@ void AppGroup::setName(const QString &name) } } +void AppGroup::setKillText(const QString &killText) +{ + if (m_killText != killText) { + m_killText = killText; + setEdited(true); + } +} + void AppGroup::setBlockText(const QString &blockText) { if (m_blockText != blockText) { @@ -217,6 +225,7 @@ void AppGroup::copy(const AppGroup &o) m_id = o.id(); m_name = o.name(); + m_killText = o.killText(); m_blockText = o.blockText(); m_allowText = o.allowText(); } @@ -250,6 +259,7 @@ QVariant AppGroup::toVariant() const map["id"] = id(); map["name"] = name(); + map["killText"] = killText(); map["blockText"] = blockText(); map["allowText"] = allowText(); @@ -285,6 +295,7 @@ void AppGroup::fromVariant(const QVariant &v) m_id = map["id"].toLongLong(); m_name = map["name"].toString(); + m_killText = map["killText"].toString(); m_blockText = map["blockText"].toString(); m_allowText = map["allowText"].toString(); } diff --git a/src/ui/conf/appgroup.h b/src/ui/conf/appgroup.h index b4c3b598..25f90bff 100644 --- a/src/ui/conf/appgroup.h +++ b/src/ui/conf/appgroup.h @@ -70,6 +70,9 @@ public: QString name() const { return m_name; } void setName(const QString &name); + QString killText() const { return m_killText; } + void setKillText(const QString &killText); + QString blockText() const { return m_blockText; } void setBlockText(const QString &blockText); @@ -117,6 +120,7 @@ private: QString m_name; + QString m_killText; QString m_blockText; QString m_allowText; diff --git a/src/ui/conf/confmanager.cpp b/src/ui/conf/confmanager.cpp index e2a43810..e5d17351 100644 --- a/src/ui/conf/confmanager.cpp +++ b/src/ui/conf/confmanager.cpp @@ -37,7 +37,7 @@ namespace { const QLoggingCategory LC("conf"); -constexpr int DATABASE_USER_VERSION = 23; +constexpr int DATABASE_USER_VERSION = 24; constexpr int APP_END_TIMER_INTERVAL_MIN = 100; constexpr int APP_END_TIMER_INTERVAL_MAX = 24 * 60 * 60 * 1000; // 1 day @@ -54,7 +54,7 @@ const char *const sqlSelectAppGroups = "SELECT app_group_id, enabled, apply_chil " speed_limit_in, speed_limit_out," " limit_packet_loss, limit_latency," " limit_bufsize_in, limit_bufsize_out," - " name, block_text, allow_text," + " name, kill_text, block_text, allow_text," " period_from, period_to" " FROM app_group" " ORDER BY order_index;"; @@ -78,10 +78,10 @@ const char *const sqlInsertAppGroup = "INSERT INTO app_group(app_group_id, order " speed_limit_in, speed_limit_out," " limit_packet_loss, limit_latency," " limit_bufsize_in, limit_bufsize_out," - " name, block_text, allow_text," + " name, kill_text, block_text, allow_text," " period_from, period_to)" " VALUES(?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12," - " ?13, ?14, ?15, ?16, ?17, ?18, ?19, ?20, ?21);"; + " ?13, ?14, ?15, ?16, ?17, ?18, ?19, ?20, ?21, ?22);"; const char *const sqlUpdateAppGroup = "UPDATE app_group" " SET order_index = ?2, enabled = ?3," @@ -91,8 +91,8 @@ const char *const sqlUpdateAppGroup = "UPDATE app_group" " speed_limit_in = ?11, speed_limit_out = ?12," " limit_packet_loss = ?13, limit_latency = ?14," " limit_bufsize_in = ?15, limit_bufsize_out = ?16," - " name = ?17, block_text = ?18, allow_text = ?19," - " period_from = ?20, period_to = ?21" + " name = ?17, kill_text = ?18, block_text = ?19," + " allow_text = ?20, period_from = ?21, period_to = ?22" " WHERE app_group_id = ?1;"; const char *const sqlDeleteAppGroup = "DELETE FROM app_group" @@ -423,10 +423,11 @@ bool loadAppGroups(SqliteDb *db, FirewallConf &conf) appGroup->setLimitBufferSizeIn(quint32(stmt.columnInt(13))); appGroup->setLimitBufferSizeOut(quint32(stmt.columnInt(14))); appGroup->setName(stmt.columnText(15)); - appGroup->setBlockText(stmt.columnText(16)); - appGroup->setAllowText(stmt.columnText(17)); - appGroup->setPeriodFrom(stmt.columnText(18)); - appGroup->setPeriodTo(stmt.columnText(19)); + appGroup->setKillText(stmt.columnText(16)); + appGroup->setBlockText(stmt.columnText(17)); + appGroup->setAllowText(stmt.columnText(18)); + appGroup->setPeriodFrom(stmt.columnText(19)); + appGroup->setPeriodTo(stmt.columnText(20)); appGroup->setEdited(false); conf.addAppGroup(appGroup); @@ -448,8 +449,8 @@ bool saveAppGroup(SqliteDb *db, AppGroup *appGroup, int orderIndex) << appGroup->limitOutEnabled() << appGroup->speedLimitIn() << appGroup->speedLimitOut() << appGroup->limitPacketLoss() << appGroup->limitLatency() << appGroup->limitBufferSizeIn() << appGroup->limitBufferSizeOut() << appGroup->name() - << appGroup->blockText() << appGroup->allowText() << appGroup->periodFrom() - << appGroup->periodTo(); + << appGroup->killText() << appGroup->blockText() << appGroup->allowText() + << appGroup->periodFrom() << appGroup->periodTo(); const char *sql = rowExists ? sqlUpdateAppGroup : sqlInsertAppGroup; diff --git a/src/ui/conf/migrations/1.sql b/src/ui/conf/migrations/1.sql index aab4c783..9293e0bc 100644 --- a/src/ui/conf/migrations/1.sql +++ b/src/ui/conf/migrations/1.sql @@ -112,6 +112,7 @@ CREATE TABLE app_group( limit_bufsize_in INTEGER NOT NULL DEFAULT 150000, limit_bufsize_out INTEGER NOT NULL DEFAULT 150000, name TEXT NOT NULL, + kill_text TEXT, block_text TEXT NOT NULL, allow_text TEXT NOT NULL, period_from TEXT NOT NULL, diff --git a/src/ui/form/opt/pages/applicationspage.cpp b/src/ui/form/opt/pages/applicationspage.cpp index b68db450..5b572ad6 100644 --- a/src/ui/form/opt/pages/applicationspage.cpp +++ b/src/ui/form/opt/pages/applicationspage.cpp @@ -132,12 +132,12 @@ void ApplicationsPage::setAppGroupIndex(int v) void ApplicationsPage::onSaveWindowState(IniUser *ini) { - ini->setOptWindowAppsSplit(m_splitter->saveState()); + ini->setOptWindowAppsSplit(m_allowSplitter->saveState()); } void ApplicationsPage::onRestoreWindowState(IniUser *ini) { - m_splitter->restoreState(ini->optWindowAppsSplit()); + m_allowSplitter->restoreState(ini->optWindowAppsSplit()); } void ApplicationsPage::onRetranslateUi() @@ -165,10 +165,11 @@ void ApplicationsPage::onRetranslateUi() m_cbGroupEnabled->setText(tr("Enabled")); m_ctpGroupPeriod->checkBox()->setText(tr("time period:")); + m_killApps->labelTitle()->setText(tr("Kill Process")); m_blockApps->labelTitle()->setText(tr("Block")); m_allowApps->labelTitle()->setText(tr("Allow")); - auto splitterHandle = m_splitter->handle(); + auto splitterHandle = m_allowSplitter->handle(); splitterHandle->btMoveAllFrom1To2()->setToolTip(tr("Move All Lines to 'Allow'")); splitterHandle->btMoveAllFrom2To1()->setToolTip(tr("Move All Lines to 'Block'")); splitterHandle->btInterchangeAll()->setToolTip(tr("Interchange All Lines")); @@ -225,12 +226,13 @@ void ApplicationsPage::setupUi() layout->addLayout(groupHeader); // App Columns + setupKillApps(); setupBlockApps(); setupAllowApps(); // Splitter setupSplitter(); - layout->addWidget(m_splitter, 1); + layout->addWidget(m_killSplitter, 1); // Splitter Buttons setupSplitterButtons(); @@ -513,6 +515,18 @@ void ApplicationsPage::setupGroupLimitBufferSize() }); } +void ApplicationsPage::setupKillApps() +{ + m_killApps = new AppsColumn(); + m_killApps->icon()->setPixmap(IconCache::file(":/icons/scull.png")); + + connect(m_killApps->editText(), &QPlainTextEdit::textChanged, this, [&] { + const auto text = m_killApps->editText()->toPlainText(); + + pageAppGroupSetText(this, &AppGroup::setKillText, text); + }); +} + void ApplicationsPage::setupBlockApps() { m_blockApps = new AppsColumn(); @@ -541,18 +555,23 @@ void ApplicationsPage::setupAllowApps() void ApplicationsPage::setupSplitter() { - m_splitter = new TextArea2Splitter(); + m_allowSplitter = new TextArea2Splitter(); + Q_ASSERT(!m_allowSplitter->handle()); - Q_ASSERT(!m_splitter->handle()); + m_allowSplitter->addWidget(m_blockApps); + m_allowSplitter->addWidget(m_allowApps); - m_splitter->addWidget(m_blockApps); - m_splitter->addWidget(m_allowApps); - - auto splitterHandle = m_splitter->handle(); + auto splitterHandle = m_allowSplitter->handle(); Q_ASSERT(splitterHandle); splitterHandle->setTextArea1(m_blockApps->editText()); splitterHandle->setTextArea2(m_allowApps->editText()); + + m_killSplitter = new QSplitter(); + m_killSplitter->addWidget(m_killApps); + m_killSplitter->addWidget(m_allowSplitter); + + m_killSplitter->setSizes({ 0, 1 }); // "Kill Processes" area is always collapsed } void ApplicationsPage::setupSplitterButtons() @@ -562,12 +581,12 @@ void ApplicationsPage::setupSplitterButtons() m_btSelectFile->text(), tr("Programs (*.exe);;All files (*.*)")); if (!filePaths.isEmpty()) { - auto area = m_splitter->handle()->currentTextArea(); + auto area = m_allowSplitter->handle()->currentTextArea(); TextAreaUtil::appendText(area, filePaths.join('\n')); } }); - const auto layout = m_splitter->handle()->buttonsLayout(); + const auto layout = m_allowSplitter->handle()->buttonsLayout(); layout->addWidget(m_btSelectFile, 0, Qt::AlignHCenter); } @@ -598,6 +617,7 @@ void ApplicationsPage::updateGroup() m_ctpGroupPeriod->timeEdit1()->setTime(CheckTimePeriod::toTime(appGroup->periodFrom())); m_ctpGroupPeriod->timeEdit2()->setTime(CheckTimePeriod::toTime(appGroup->periodTo())); + m_killApps->editText()->setText(appGroup->killText()); m_blockApps->editText()->setText(appGroup->blockText()); m_allowApps->editText()->setText(appGroup->allowText()); } diff --git a/src/ui/form/opt/pages/applicationspage.h b/src/ui/form/opt/pages/applicationspage.h index 1b4685d2..ba980e69 100644 --- a/src/ui/form/opt/pages/applicationspage.h +++ b/src/ui/form/opt/pages/applicationspage.h @@ -55,6 +55,7 @@ private: void setupGroupLimitLatency(); void setupGroupLimitPacketLoss(); void setupGroupLimitBufferSize(); + void setupKillApps(); void setupBlockApps(); void setupAllowApps(); void setupSplitter(); @@ -87,9 +88,11 @@ private: LabelSpin *m_limitBufferSizeOut = nullptr; QCheckBox *m_cbLogBlocked = nullptr; QCheckBox *m_cbLogConn = nullptr; + AppsColumn *m_killApps = nullptr; AppsColumn *m_blockApps = nullptr; AppsColumn *m_allowApps = nullptr; - TextArea2Splitter *m_splitter = nullptr; + QSplitter *m_killSplitter = nullptr; + TextArea2Splitter *m_allowSplitter = nullptr; QToolButton *m_btSelectFile = nullptr; }; diff --git a/src/ui/util/conf/confutil.cpp b/src/ui/util/conf/confutil.cpp index 45e88101..7690787f 100644 --- a/src/ui/util/conf/confutil.cpp +++ b/src/ui/util/conf/confutil.cpp @@ -373,10 +373,18 @@ bool ConfUtil::parseAppGroups(EnvManager &envManager, const QList &a app.logConn = appGroup->logConn(); app.groupIndex = i; + const auto killText = envManager.expandString(appGroup->killText()); const auto blockText = envManager.expandString(appGroup->blockText()); const auto allowText = envManager.expandString(appGroup->allowText()); app.blocked = true; + app.killProcess = true; + if (!parseAppsText(app, killText, wildAppsMap, prefixAppsMap, exeAppsMap, wildAppsSize, + prefixAppsSize, exeAppsSize)) + return false; + + app.blocked = true; + app.killProcess = false; if (!parseAppsText(app, blockText, wildAppsMap, prefixAppsMap, exeAppsMap, wildAppsSize, prefixAppsSize, exeAppsSize)) return false;