UI: TrayIcon: Check Alert Window for Filter Modes

This commit is contained in:
Nodir Temirkhodjaev 2024-10-15 15:52:27 +05:00
parent a46b59152e
commit 979a01212d
7 changed files with 59 additions and 35 deletions

View File

@ -34,31 +34,39 @@ void FirewallConf::setBlockTrafficIndex(int index)
} }
} }
int FirewallConf::filterModeIndex() const FirewallConf::FilterMode FirewallConf::filterMode() const
{ {
return m_allowAllNew ? 0 : (m_askToConnect ? 1 : (m_appBlockAll ? 2 : (m_appAllowAll ? 3 : 4))); if (m_allowAllNew)
return ModeAutoLearn;
if (m_askToConnect)
return ModeAskToConnect;
if (m_appBlockAll)
return ModeBlockAll;
if (m_appAllowAll)
return ModeAllowAll;
return ModeIgnore;
} }
void FirewallConf::setFilterModeIndex(int index) void FirewallConf::setFilterMode(FirewallConf::FilterMode mode)
{ {
m_allowAllNew = false; m_allowAllNew = false;
m_askToConnect = false; m_askToConnect = false;
m_appBlockAll = false; m_appBlockAll = false;
m_appAllowAll = false; m_appAllowAll = false;
switch (index) { switch (mode) {
case 0: { // Auto-Learn case ModeAutoLearn: {
m_allowAllNew = true; m_allowAllNew = true;
m_appBlockAll = true; m_appBlockAll = true;
} break; } break;
case 1: { // Ask case ModeAskToConnect: {
m_askToConnect = true; m_askToConnect = true;
m_appBlockAll = true; m_appBlockAll = true;
} break; } break;
case 2: { // Block case ModeBlockAll: {
m_appBlockAll = true; m_appBlockAll = true;
} break; } break;
case 3: { // Allow case ModeAllowAll: {
m_appAllowAll = true; m_appAllowAll = true;
} break; } break;
} }

View File

@ -28,8 +28,8 @@ public:
enum FilterMode { enum FilterMode {
ModeAutoLearn = 0, ModeAutoLearn = 0,
ModeAskToConnect, ModeAskToConnect,
ModeAllowAll,
ModeBlockAll, ModeBlockAll,
ModeAllowAll,
ModeIgnore, ModeIgnore,
}; };
Q_ENUM(FilterMode) Q_ENUM(FilterMode)
@ -107,8 +107,8 @@ public:
int blockTrafficIndex() const; int blockTrafficIndex() const;
void setBlockTrafficIndex(int index); void setBlockTrafficIndex(int index);
int filterModeIndex() const; FirewallConf::FilterMode filterMode() const;
void setFilterModeIndex(int index); void setFilterMode(FirewallConf::FilterMode mode);
static QStringList blockTrafficNames(); static QStringList blockTrafficNames();
static QStringList blockTrafficIconPaths(); static QStringList blockTrafficIconPaths();

View File

@ -69,8 +69,8 @@ void IfacePage::onResetToDefault()
m_cbAppNotifyMessage->setChecked(true); m_cbAppNotifyMessage->setChecked(true);
m_cbAppAlertAutoShow->setChecked(true); m_cbAppAlertAutoShow->setChecked(true);
m_cbAppAlertAutoLearn->setChecked(false); m_cbAppAlertAutoLearn->setChecked(false);
m_cbAppAlertAllowAll->setChecked(true);
m_cbAppAlertBlockAll->setChecked(true); m_cbAppAlertBlockAll->setChecked(true);
m_cbAppAlertAllowAll->setChecked(true);
m_cbAppAlertAlwaysOnTop->setChecked(true); m_cbAppAlertAlwaysOnTop->setChecked(true);
m_cbAppAlertAutoActive->setChecked(false); m_cbAppAlertAutoActive->setChecked(false);
m_cbAppAlertSound->setChecked(true); m_cbAppAlertSound->setChecked(true);
@ -149,8 +149,8 @@ void IfacePage::onRetranslateUi()
m_cbAppNotifyMessage->setText(tr("Use System Notifications for New Programs")); m_cbAppNotifyMessage->setText(tr("Use System Notifications for New Programs"));
m_cbAppAlertAutoShow->setText(tr("Auto-Show Alert Window for New Programs")); m_cbAppAlertAutoShow->setText(tr("Auto-Show Alert Window for New Programs"));
setAlertModeText(m_cbAppAlertAutoLearn, FirewallConf::ModeAutoLearn); setAlertModeText(m_cbAppAlertAutoLearn, FirewallConf::ModeAutoLearn);
setAlertModeText(m_cbAppAlertAllowAll, FirewallConf::ModeAllowAll);
setAlertModeText(m_cbAppAlertBlockAll, FirewallConf::ModeBlockAll); setAlertModeText(m_cbAppAlertBlockAll, FirewallConf::ModeBlockAll);
setAlertModeText(m_cbAppAlertAllowAll, FirewallConf::ModeAllowAll);
m_cbAppAlertAlwaysOnTop->setText(tr("Alert Window is Always on top")); m_cbAppAlertAlwaysOnTop->setText(tr("Alert Window is Always on top"));
m_cbAppAlertAutoActive->setText(tr("Alert Window is auto-active")); m_cbAppAlertAutoActive->setText(tr("Alert Window is auto-active"));
m_cbAppAlertSound->setText(tr("Sound Alert")); m_cbAppAlertSound->setText(tr("Sound Alert"));
@ -545,13 +545,6 @@ void IfacePage::setupAlertModes()
}); });
setAlertModeIcon(m_cbAppAlertAutoLearn, FirewallConf::ModeAutoLearn); setAlertModeIcon(m_cbAppAlertAutoLearn, FirewallConf::ModeAutoLearn);
m_cbAppAlertAllowAll =
ControlUtil::createCheckBox(iniUser()->progAlertWindowAllowAll(), [&](bool checked) {
iniUser()->setProgAlertWindowAllowAll(checked);
ctrl()->setIniUserEdited();
});
setAlertModeIcon(m_cbAppAlertAllowAll, FirewallConf::ModeAllowAll);
m_cbAppAlertBlockAll = m_cbAppAlertBlockAll =
ControlUtil::createCheckBox(iniUser()->progAlertWindowBlockAll(), [&](bool checked) { ControlUtil::createCheckBox(iniUser()->progAlertWindowBlockAll(), [&](bool checked) {
iniUser()->setProgAlertWindowBlockAll(checked); iniUser()->setProgAlertWindowBlockAll(checked);
@ -559,6 +552,13 @@ void IfacePage::setupAlertModes()
}); });
setAlertModeIcon(m_cbAppAlertBlockAll, FirewallConf::ModeBlockAll); setAlertModeIcon(m_cbAppAlertBlockAll, FirewallConf::ModeBlockAll);
m_cbAppAlertAllowAll =
ControlUtil::createCheckBox(iniUser()->progAlertWindowAllowAll(), [&](bool checked) {
iniUser()->setProgAlertWindowAllowAll(checked);
ctrl()->setIniUserEdited();
});
setAlertModeIcon(m_cbAppAlertAllowAll, FirewallConf::ModeAllowAll);
// Setup Alert Modes Button // Setup Alert Modes Button
setupAlertModesButton(); setupAlertModesButton();
} }
@ -567,7 +567,7 @@ void IfacePage::setupAlertModesButton()
{ {
// Menu // Menu
auto layout = ControlUtil::createVLayoutByWidgets( auto layout = ControlUtil::createVLayoutByWidgets(
{ m_cbAppAlertAutoLearn, m_cbAppAlertAllowAll, m_cbAppAlertBlockAll }); { m_cbAppAlertAutoLearn, m_cbAppAlertBlockAll, m_cbAppAlertAllowAll });
auto menu = ControlUtil::createMenuByLayout(layout, this); auto menu = ControlUtil::createMenuByLayout(layout, this);

View File

@ -95,8 +95,8 @@ private:
QCheckBox *m_cbAppNotifyMessage = nullptr; QCheckBox *m_cbAppNotifyMessage = nullptr;
QCheckBox *m_cbAppAlertAutoShow = nullptr; QCheckBox *m_cbAppAlertAutoShow = nullptr;
QCheckBox *m_cbAppAlertAutoLearn = nullptr; QCheckBox *m_cbAppAlertAutoLearn = nullptr;
QCheckBox *m_cbAppAlertAllowAll = nullptr;
QCheckBox *m_cbAppAlertBlockAll = nullptr; QCheckBox *m_cbAppAlertBlockAll = nullptr;
QCheckBox *m_cbAppAlertAllowAll = nullptr;
QPushButton *m_btAlertModes = nullptr; QPushButton *m_btAlertModes = nullptr;
QCheckBox *m_cbAppAlertAlwaysOnTop = nullptr; QCheckBox *m_cbAppAlertAlwaysOnTop = nullptr;
QCheckBox *m_cbAppAlertAutoActive = nullptr; QCheckBox *m_cbAppAlertAutoActive = nullptr;

View File

@ -202,7 +202,7 @@ void OptionsPage::retranslateComboBlockTraffic()
void OptionsPage::retranslateComboFilterMode() void OptionsPage::retranslateComboFilterMode()
{ {
updateComboBox(m_comboFilterMode, FirewallConf::filterModeNames(), updateComboBox(m_comboFilterMode, FirewallConf::filterModeNames(),
FirewallConf::filterModeIconPaths(), conf()->filterModeIndex()); FirewallConf::filterModeIconPaths(), conf()->filterMode());
} }
void OptionsPage::retranslateEditPassword() void OptionsPage::retranslateEditPassword()
@ -353,8 +353,8 @@ QLayout *OptionsPage::setupFilterModeLayout()
m_comboFilterMode = m_comboFilterMode =
ControlUtil::createComboBox(FirewallConf::filterModeNames(), [&](int index) { ControlUtil::createComboBox(FirewallConf::filterModeNames(), [&](int index) {
if (conf()->filterModeIndex() != index) { if (conf()->filterMode() != index) {
conf()->setFilterModeIndex(index); conf()->setFilterMode(FirewallConf::FilterMode(index));
ctrl()->setFlagsEdited(); ctrl()->setFlagsEdited();
} }
}); });

View File

@ -186,6 +186,22 @@ QAction *addAction(QWidget *widget, const QString &iconPath, const QObject *rece
return action; return action;
} }
bool checkAlertFilterMode(FirewallConf *conf, IniUser *iniUser)
{
switch (conf->filterMode()) {
case FirewallConf::ModeAutoLearn:
return iniUser->progAlertWindowAutoLearn();
case FirewallConf::ModeAskToConnect:
return true;
case FirewallConf::ModeBlockAll:
return iniUser->progAlertWindowBlockAll();
case FirewallConf::ModeAllowAll:
return iniUser->progAlertWindowAllowAll();
default:
return false;
}
}
} }
TrayIcon::TrayIcon(QObject *parent) : QSystemTrayIcon(parent), m_ctrl(new TrayController(this)) TrayIcon::TrayIcon(QObject *parent) : QSystemTrayIcon(parent), m_ctrl(new TrayController(this))
@ -593,7 +609,7 @@ void TrayIcon::updateTrayMenuFlags()
m_filterModeMenu->setEnabled(editEnabled); m_filterModeMenu->setEnabled(editEnabled);
{ {
QAction *action = m_filterModeActions->actions().at(conf()->filterModeIndex()); QAction *action = m_filterModeActions->actions().at(conf()->filterMode());
if (!action->isChecked()) { if (!action->isChecked()) {
action->setChecked(true); action->setChecked(true);
m_filterModeMenu->setIcon(action->icon()); m_filterModeMenu->setIcon(action->icon());
@ -636,8 +652,8 @@ void TrayIcon::updateAppGroupActions()
void TrayIcon::sendAlertMessage() void TrayIcon::sendAlertMessage()
{ {
if (conf()->allowAllNew() && !iniUser()->progAlertWindowAutoLearn()) if (!checkAlertFilterMode(conf(), iniUser()))
return; // do not notify in Auto-Learn mode return;
if (iniUser()->progNotifyMessage()) { if (iniUser()->progNotifyMessage()) {
windowManager()->showTrayMessage( windowManager()->showTrayMessage(
@ -738,8 +754,8 @@ void TrayIcon::saveTrayFlags()
{ {
QAction *action = m_filterModeActions->checkedAction(); QAction *action = m_filterModeActions->checkedAction();
const int index = m_filterModeActions->actions().indexOf(action); const int index = m_filterModeActions->actions().indexOf(action);
if (conf()->filterModeIndex() != index) { if (conf()->filterMode() != index) {
conf()->setFilterModeIndex(index); conf()->setFilterMode(FirewallConf::FilterMode(index));
m_filterModeMenu->setIcon(action->icon()); m_filterModeMenu->setIcon(action->icon());
} }
} }
@ -803,7 +819,7 @@ void TrayIcon::switchBlockTraffic(QAction *action)
void TrayIcon::switchFilterMode(QAction *action) void TrayIcon::switchFilterMode(QAction *action)
{ {
const int index = m_filterModeActions->actions().indexOf(action); const int index = m_filterModeActions->actions().indexOf(action);
if (index < 0 || index == conf()->filterModeIndex()) if (index < 0 || index == conf()->filterMode())
return; return;
if (iniUser()->confirmTrayFlags()) { if (iniUser()->confirmTrayFlags()) {
@ -812,7 +828,7 @@ void TrayIcon::switchFilterMode(QAction *action)
if (confirmed) { if (confirmed) {
saveTrayFlags(); saveTrayFlags();
} else { } else {
QAction *a = m_filterModeActions->actions().at(conf()->filterModeIndex()); QAction *a = m_filterModeActions->actions().at(conf()->filterMode());
a->setChecked(true); a->setChecked(true);
} }
}, },

View File

@ -144,12 +144,12 @@ public:
bool progAlertWindowAutoLearn() const { return valueBool("progAlertWindow/modeAutoLearn"); } bool progAlertWindowAutoLearn() const { return valueBool("progAlertWindow/modeAutoLearn"); }
void setProgAlertWindowAutoLearn(bool on) { setValue("progAlertWindow/modeAutoLearn", on); } void setProgAlertWindowAutoLearn(bool on) { setValue("progAlertWindow/modeAutoLearn", on); }
bool progAlertWindowAllowAll() const { return valueBool("progAlertWindow/modeAllowAll", true); }
void setProgAlertWindowAllowAll(bool on) { setValue("progAlertWindow/modeAllowAll", on, true); }
bool progAlertWindowBlockAll() const { return valueBool("progAlertWindow/modeBlockAll", true); } bool progAlertWindowBlockAll() const { return valueBool("progAlertWindow/modeBlockAll", true); }
void setProgAlertWindowBlockAll(bool on) { setValue("progAlertWindow/modeBlockAll", on, true); } void setProgAlertWindowBlockAll(bool on) { setValue("progAlertWindow/modeBlockAll", on, true); }
bool progAlertWindowAllowAll() const { return valueBool("progAlertWindow/modeAllowAll", true); }
void setProgAlertWindowAllowAll(bool on) { setValue("progAlertWindow/modeAllowAll", on, true); }
bool progAlertWindowAlwaysOnTop() const bool progAlertWindowAlwaysOnTop() const
{ {
return valueBool("progAlertWindow/alwaysOnTop", true); return valueBool("progAlertWindow/alwaysOnTop", true);