UI: Add "Filter Mode", instead of Block/Allow All

This commit is contained in:
Nodir Temirkhodjaev 2022-05-29 19:49:23 +03:00
parent 67c7037d18
commit 03d9e02ef7
8 changed files with 153 additions and 43 deletions

View File

@ -101,6 +101,37 @@ void FirewallConf::setAppAllowAll(bool appAllowAll)
m_appAllowAll = appAllowAll; m_appAllowAll = appAllowAll;
} }
int FirewallConf::filterModeIndex() const
{
return m_appBlockAll ? 0 : (m_appAllowAll ? 1 : 2);
}
void FirewallConf::setFilterModeIndex(int index)
{
m_appBlockAll = false;
m_appAllowAll = false;
switch (index) {
case 0: {
m_appBlockAll = true;
} break;
case 1: {
m_appAllowAll = true;
} break;
}
}
QStringList FirewallConf::filterModeNames()
{
return { tr("Block, if not allowed"), tr("Allow, if not blocked"),
tr("Ignore, if not blocked or allowed") };
}
QStringList FirewallConf::filterModeIconPaths()
{
return { ":/icons/deny.png", ":/icons/accept.png", ":/icons/error.png" };
}
void FirewallConf::setActivePeriodEnabled(bool activePeriodEnabled) void FirewallConf::setActivePeriodEnabled(bool activePeriodEnabled)
{ {
m_activePeriodEnabled = activePeriodEnabled; m_activePeriodEnabled = activePeriodEnabled;

View File

@ -81,6 +81,12 @@ public:
bool appAllowAll() const { return m_appAllowAll; } bool appAllowAll() const { return m_appAllowAll; }
void setAppAllowAll(bool appAllowAll); void setAppAllowAll(bool appAllowAll);
int filterModeIndex() const;
void setFilterModeIndex(int index);
static QStringList filterModeNames();
static QStringList filterModeIconPaths();
bool activePeriodEnabled() const { return m_activePeriodEnabled; } bool activePeriodEnabled() const { return m_activePeriodEnabled; }
void setActivePeriodEnabled(bool activePeriodEnabled); void setActivePeriodEnabled(bool activePeriodEnabled);

View File

@ -76,9 +76,6 @@ void ApplicationsPage::onRetranslateUi()
m_btAddGroup->setText(tr("Add Group")); m_btAddGroup->setText(tr("Add Group"));
m_btRenameGroup->setText(tr("Rename Group")); m_btRenameGroup->setText(tr("Rename Group"));
m_cbBlockAll->setText(tr("Block All"));
m_cbAllowAll->setText(tr("Allow All"));
m_btGroupOptions->setText(tr("Options")); m_btGroupOptions->setText(tr("Options"));
m_cbApplyChild->setText(tr("Apply same rules to child processes")); m_cbApplyChild->setText(tr("Apply same rules to child processes"));
@ -177,14 +174,10 @@ QLayout *ApplicationsPage::setupHeader()
setupAddGroup(); setupAddGroup();
setupRenameGroup(); setupRenameGroup();
setupBlockAllowAll();
layout->addWidget(m_editGroupName); layout->addWidget(m_editGroupName);
layout->addWidget(m_btAddGroup); layout->addWidget(m_btAddGroup);
layout->addWidget(m_btRenameGroup); layout->addWidget(m_btRenameGroup);
layout->addStretch(); layout->addStretch();
layout->addWidget(m_cbBlockAll);
layout->addWidget(m_cbAllowAll);
return layout; return layout;
} }
@ -239,34 +232,6 @@ void ApplicationsPage::setupRenameGroup()
}); });
} }
void ApplicationsPage::setupBlockAllowAll()
{
m_cbBlockAll = ControlUtil::createCheckBox(conf()->appBlockAll(), [&](bool checked) {
conf()->setAppBlockAll(checked);
ctrl()->setFlagsEdited();
});
m_cbBlockAll->setIcon(IconCache::icon(":/icons/deny.png"));
m_cbAllowAll = ControlUtil::createCheckBox(conf()->appAllowAll(), [&](bool checked) {
conf()->setAppAllowAll(checked);
ctrl()->setFlagsEdited();
});
m_cbAllowAll->setIcon(IconCache::icon(":/icons/accept.png"));
const auto refreshBlockAllowAllEnabled = [&] {
const bool blockAll = m_cbBlockAll->isChecked();
const bool allowAll = m_cbAllowAll->isChecked();
m_cbBlockAll->setEnabled(blockAll || !allowAll);
m_cbAllowAll->setEnabled(!blockAll || allowAll);
};
refreshBlockAllowAllEnabled();
connect(m_cbBlockAll, &QCheckBox::toggled, this, refreshBlockAllowAllEnabled);
connect(m_cbAllowAll, &QCheckBox::toggled, this, refreshBlockAllowAllEnabled);
}
void ApplicationsPage::setupTabBar() void ApplicationsPage::setupTabBar()
{ {
m_tabBar = new TabBar(); m_tabBar = new TabBar();

View File

@ -39,7 +39,6 @@ private:
QLayout *setupHeader(); QLayout *setupHeader();
void setupAddGroup(); void setupAddGroup();
void setupRenameGroup(); void setupRenameGroup();
void setupBlockAllowAll();
void setupTabBar(); void setupTabBar();
int addTab(const QString &text); int addTab(const QString &text);
QLayout *setupGroupHeader(); QLayout *setupGroupHeader();
@ -75,8 +74,6 @@ private:
QLineEdit *m_editGroupName = nullptr; QLineEdit *m_editGroupName = nullptr;
QPushButton *m_btAddGroup = nullptr; QPushButton *m_btAddGroup = nullptr;
QPushButton *m_btRenameGroup = nullptr; QPushButton *m_btRenameGroup = nullptr;
QCheckBox *m_cbBlockAll = nullptr;
QCheckBox *m_cbAllowAll = nullptr;
TabBar *m_tabBar = nullptr; TabBar *m_tabBar = nullptr;
QCheckBox *m_cbGroupEnabled = nullptr; QCheckBox *m_cbGroupEnabled = nullptr;
CheckTimePeriod *m_ctpGroupPeriod = nullptr; CheckTimePeriod *m_ctpGroupPeriod = nullptr;

View File

@ -170,6 +170,9 @@ void OptionsPage::onRetranslateUi()
m_cbStopInetTraffic->setText(tr("Stop Internet Traffic")); m_cbStopInetTraffic->setText(tr("Stop Internet Traffic"));
m_cbAllowAllNew->setText(tr("Auto-Allow New Programs")); m_cbAllowAllNew->setText(tr("Auto-Allow New Programs"));
m_labelFilterMode->setText(tr("Filter Mode:"));
retranslateComboFilterMode();
m_cbExplorerMenu->setText(tr("Windows Explorer integration")); m_cbExplorerMenu->setText(tr("Windows Explorer integration"));
m_cbHotKeys->setText(tr("Hot Keys")); m_cbHotKeys->setText(tr("Hot Keys"));
@ -238,6 +241,22 @@ void OptionsPage::retranslateComboStartMode()
} }
} }
void OptionsPage::retranslateComboFilterMode()
{
int index = 0;
const QStringList iconPaths = FirewallConf::filterModeIconPaths();
for (const QString &name : FirewallConf::filterModeNames()) {
const QString iconPath = iconPaths.at(index);
m_comboFilterMode->setItemText(index, name);
m_comboFilterMode->setItemIcon(index, IconCache::icon(iconPath));
++index;
}
m_comboFilterMode->setCurrentIndex(conf()->filterModeIndex());
}
void OptionsPage::retranslateEditPassword() void OptionsPage::retranslateEditPassword()
{ {
m_editPassword->setPlaceholderText( m_editPassword->setPlaceholderText(
@ -400,17 +419,36 @@ void OptionsPage::setupTrafficBox()
ctrl()->setFlagsEdited(); ctrl()->setFlagsEdited();
}); });
auto filterModeLayout = setupFilterModeLayout();
auto layout = new QVBoxLayout(); auto layout = new QVBoxLayout();
layout->addWidget(m_cbFilterEnabled); layout->addWidget(m_cbFilterEnabled);
layout->addWidget(m_cbFilterLocals); layout->addWidget(m_cbFilterLocals);
layout->addWidget(m_cbStopTraffic); layout->addWidget(m_cbStopTraffic);
layout->addWidget(m_cbStopInetTraffic); layout->addWidget(m_cbStopInetTraffic);
layout->addWidget(m_cbAllowAllNew); layout->addWidget(m_cbAllowAllNew);
layout->addLayout(filterModeLayout);
m_gbTraffic = new QGroupBox(this); m_gbTraffic = new QGroupBox(this);
m_gbTraffic->setLayout(layout); m_gbTraffic->setLayout(layout);
} }
QLayout *OptionsPage::setupFilterModeLayout()
{
m_labelFilterMode = ControlUtil::createLabel();
m_comboFilterMode =
ControlUtil::createComboBox(FirewallConf::filterModeNames(), [&](int index) {
if (conf()->filterModeIndex() != index) {
conf()->setFilterModeIndex(index);
ctrl()->emitEdited();
}
});
m_comboFilterMode->setFixedWidth(200);
return ControlUtil::createRowLayout(m_labelFilterMode, m_comboFilterMode);
}
void OptionsPage::setupGlobalBox() void OptionsPage::setupGlobalBox()
{ {
m_cbExplorerMenu = ControlUtil::createCheckBox(ini()->explorerIntegrated(), [&](bool checked) { m_cbExplorerMenu = ControlUtil::createCheckBox(ini()->explorerIntegrated(), [&](bool checked) {

View File

@ -24,6 +24,7 @@ private:
void saveService(bool isService); void saveService(bool isService);
void retranslateComboStartMode(); void retranslateComboStartMode();
void retranslateComboFilterMode();
void retranslateEditPassword(); void retranslateEditPassword();
void retranslateComboTrayEvent(); void retranslateComboTrayEvent();
void retranslateComboTrayAction(); void retranslateComboTrayAction();
@ -36,6 +37,7 @@ private:
void setupStartupBox(); void setupStartupBox();
QLayout *setupStartModeLayout(); QLayout *setupStartModeLayout();
void setupTrafficBox(); void setupTrafficBox();
QLayout *setupFilterModeLayout();
void setupGlobalBox(); void setupGlobalBox();
QLayout *setupPasswordLayout(); QLayout *setupPasswordLayout();
void setupEditPassword(); void setupEditPassword();
@ -76,6 +78,8 @@ private:
QCheckBox *m_cbStopTraffic = nullptr; QCheckBox *m_cbStopTraffic = nullptr;
QCheckBox *m_cbStopInetTraffic = nullptr; QCheckBox *m_cbStopInetTraffic = nullptr;
QCheckBox *m_cbAllowAllNew = nullptr; QCheckBox *m_cbAllowAllNew = nullptr;
QLabel *m_labelFilterMode = nullptr;
QComboBox *m_comboFilterMode = nullptr;
QCheckBox *m_cbExplorerMenu = nullptr; QCheckBox *m_cbExplorerMenu = nullptr;
QCheckBox *m_cbHotKeys = nullptr; QCheckBox *m_cbHotKeys = nullptr;
QCheckBox *m_cbPassword = nullptr; QCheckBox *m_cbPassword = nullptr;

View File

@ -1,5 +1,6 @@
#include "trayicon.h" #include "trayicon.h"
#include <QActionGroup>
#include <QApplication> #include <QApplication>
#include <QMenu> #include <QMenu>
#include <QTimer> #include <QTimer>
@ -287,9 +288,21 @@ void TrayIcon::retranslateUi()
m_stopInetTrafficAction->setText(tr("Stop Internet Traffic")); m_stopInetTrafficAction->setText(tr("Stop Internet Traffic"));
m_autoAllowProgsAction->setText(tr("Auto-Allow New Programs")); m_autoAllowProgsAction->setText(tr("Auto-Allow New Programs"));
m_filterModeMenu->setTitle(tr("Filter Mode"));
retranslateFilterModeActions();
m_quitAction->setText(tr("Quit")); m_quitAction->setText(tr("Quit"));
} }
void TrayIcon::retranslateFilterModeActions()
{
int index = 0;
for (const QString &name : FirewallConf::filterModeNames()) {
QAction *a = m_filterModeActions->actions().at(index++);
a->setText(name);
}
}
void TrayIcon::setupUi() void TrayIcon::setupUi()
{ {
this->setToolTip(QApplication::applicationDisplayName()); this->setToolTip(QApplication::applicationDisplayName());
@ -344,10 +357,14 @@ void TrayIcon::setupTrayMenu()
addAction(m_menu, QIcon(), QString(), this, SLOT(switchTrayFlag(bool)), true); addAction(m_menu, QIcon(), QString(), this, SLOT(switchTrayFlag(bool)), true);
addHotKey(m_autoAllowProgsAction, iniUser()->hotKeyAllowAllNew()); addHotKey(m_autoAllowProgsAction, iniUser()->hotKeyAllowAllNew());
setupTrayMenuFilterMode();
m_menu->addMenu(m_filterModeMenu);
m_menu->addSeparator(); m_menu->addSeparator();
for (int i = 0; i < MAX_APP_GROUP_COUNT; ++i) { for (int i = 0; i < MAX_APP_GROUP_COUNT; ++i) {
QAction *a = addAction(m_menu, QIcon(), QString(), this, SLOT(switchTrayFlag(bool)), true); QAction *a = addAction(
m_menu, QIcon(), QString(), this, SLOT(switchTrayFlag(bool)), /*checkable=*/true);
if (i < 12) { if (i < 12) {
const QString shortcutText = const QString shortcutText =
@ -365,20 +382,47 @@ void TrayIcon::setupTrayMenu()
addHotKey(m_quitAction, iniUser()->hotKeyQuit()); addHotKey(m_quitAction, iniUser()->hotKeyQuit());
} }
void TrayIcon::setupTrayMenuFilterMode()
{
m_filterModeMenu = new QMenu(m_menu);
m_filterModeActions = new QActionGroup(m_filterModeMenu);
int index = 0;
const QStringList iconPaths = FirewallConf::filterModeIconPaths();
for (const QString &name : FirewallConf::filterModeNames()) {
const QString iconPath = iconPaths.at(index);
QAction *a = addAction(m_filterModeMenu, IconCache::icon(iconPath), name,
/*receiver=*/nullptr,
/*member=*/nullptr, /*checkable=*/true);
m_filterModeActions->addAction(a);
++index;
}
connect(m_filterModeActions, &QActionGroup::triggered, this, &TrayIcon::switchFilterMode);
}
void TrayIcon::updateTrayMenuFlags() void TrayIcon::updateTrayMenuFlags()
{ {
const bool editEnabled = (!settings()->isPasswordRequired() && !windowManager()->optWindow()); const bool editEnabled = (!settings()->isPasswordRequired() && !windowManager()->optWindow());
m_filterEnabledAction->setEnabled(editEnabled); m_filterEnabledAction->setEnabled(editEnabled);
m_stopTrafficAction->setEnabled(editEnabled);
m_stopInetTrafficAction->setEnabled(editEnabled);
m_autoAllowProgsAction->setEnabled(editEnabled);
m_filterEnabledAction->setChecked(conf()->filterEnabled()); m_filterEnabledAction->setChecked(conf()->filterEnabled());
m_stopTrafficAction->setEnabled(editEnabled);
m_stopTrafficAction->setChecked(conf()->stopTraffic()); m_stopTrafficAction->setChecked(conf()->stopTraffic());
m_stopInetTrafficAction->setEnabled(editEnabled);
m_stopInetTrafficAction->setChecked(conf()->stopInetTraffic()); m_stopInetTrafficAction->setChecked(conf()->stopInetTraffic());
m_autoAllowProgsAction->setEnabled(editEnabled);
m_autoAllowProgsAction->setChecked(conf()->allowAllNew()); m_autoAllowProgsAction->setChecked(conf()->allowAllNew());
m_filterModeMenu->setEnabled(editEnabled);
m_filterModeActions->actions().at(conf()->filterModeIndex())->setChecked(true);
int appGroupIndex = 0; int appGroupIndex = 0;
for (QAction *action : qAsConst(m_appGroupActions)) { for (QAction *action : qAsConst(m_appGroupActions)) {
if (!action->isVisible()) if (!action->isVisible())
@ -418,6 +462,8 @@ void TrayIcon::saveTrayFlags()
conf()->setStopTraffic(m_stopTrafficAction->isChecked()); conf()->setStopTraffic(m_stopTrafficAction->isChecked());
conf()->setStopInetTraffic(m_stopInetTrafficAction->isChecked()); conf()->setStopInetTraffic(m_stopInetTrafficAction->isChecked());
conf()->setAllowAllNew(m_autoAllowProgsAction->isChecked()); conf()->setAllowAllNew(m_autoAllowProgsAction->isChecked());
conf()->setFilterModeIndex(
m_filterModeActions->actions().indexOf(m_filterModeActions->checkedAction()));
int i = 0; int i = 0;
for (AppGroup *appGroup : conf()->appGroups()) { for (AppGroup *appGroup : conf()->appGroups()) {
@ -444,6 +490,23 @@ void TrayIcon::switchTrayFlag(bool checked)
saveTrayFlags(); saveTrayFlags();
} }
void TrayIcon::switchFilterMode(QAction *action)
{
const int index = m_filterModeActions->actions().indexOf(action);
if (index < 0 || index == conf()->filterModeIndex())
return;
if (iniUser()->confirmTrayFlags()
&& !windowManager()->showQuestionBox(
tr("Are you sure to select the \"%1\"?").arg(action->text()))) {
action = m_filterModeActions->actions().at(conf()->filterModeIndex());
action->setChecked(true);
return;
}
saveTrayFlags();
}
void TrayIcon::quitProgram() void TrayIcon::quitProgram()
{ {
if (iniUser()->confirmQuit()) { if (iniUser()->confirmQuit()) {

View File

@ -4,6 +4,7 @@
#include <QSystemTrayIcon> #include <QSystemTrayIcon>
QT_FORWARD_DECLARE_CLASS(QAction) QT_FORWARD_DECLARE_CLASS(QAction)
QT_FORWARD_DECLARE_CLASS(QActionGroup)
class ConfManager; class ConfManager;
class FirewallConf; class FirewallConf;
@ -60,15 +61,18 @@ protected slots:
void saveTrayFlags(); void saveTrayFlags();
void switchTrayFlag(bool checked); void switchTrayFlag(bool checked);
void switchFilterMode(QAction *action);
void quitProgram(); void quitProgram();
private: private:
void setupController(); void setupController();
void retranslateUi(); void retranslateUi();
void retranslateFilterModeActions();
void setupUi(); void setupUi();
void setupTrayMenu(); void setupTrayMenu();
void setupTrayMenuFilterMode();
void updateTrayMenuFlags(); void updateTrayMenuFlags();
void updateAppGroupActions(); void updateAppGroupActions();
@ -97,6 +101,8 @@ private:
QAction *m_stopTrafficAction = nullptr; QAction *m_stopTrafficAction = nullptr;
QAction *m_stopInetTrafficAction = nullptr; QAction *m_stopInetTrafficAction = nullptr;
QAction *m_autoAllowProgsAction = nullptr; QAction *m_autoAllowProgsAction = nullptr;
QMenu *m_filterModeMenu = nullptr;
QActionGroup *m_filterModeActions = nullptr;
QAction *m_quitAction = nullptr; QAction *m_quitAction = nullptr;
QList<QAction *> m_appGroupActions; QList<QAction *> m_appGroupActions;