UI: Support wildcard paths from command line

This commit is contained in:
Nodir Temirkhodjaev 2024-04-26 19:14:38 +03:00
parent 429592523e
commit 89ee96ba1f
10 changed files with 45 additions and 16 deletions

View File

@ -202,12 +202,12 @@ bool ConfAppManager::addAppPathBlocked(App &app)
// app.appOriginPath // app.appOriginPath
// app.scheduleAction // app.scheduleAction
app.appPath = FileUtil::normalizePath(app.appOriginPath); app.appId = appIdByPath(app.appOriginPath, app.appPath);
app.appId = appIdByPath(app.appPath);
if (app.appId > 0) if (app.appId > 0)
return false; // already added by user return false; // already added by user
app.isWildcard = ConfUtil::matchWildcard(app.appPath).hasMatch();
app.appName = IoC<AppInfoCache>()->appName(app.appPath); app.appName = IoC<AppInfoCache>()->appName(app.appPath);
const bool ok = addOrUpdateApp(app); const bool ok = addOrUpdateApp(app);
@ -295,9 +295,11 @@ void ConfAppManager::logBlockedApp(const LogEntryBlocked &logEntry)
addAppPathBlocked(app); addAppPathBlocked(app);
} }
qint64 ConfAppManager::appIdByPath(const QString &appPath) qint64 ConfAppManager::appIdByPath(const QString &appOriginPath, QString &normPath)
{ {
return DbQuery(sqliteDb()).sql(sqlSelectAppIdByPath).vars({ appPath }).execute().toLongLong(); normPath = FileUtil::normalizePath(appOriginPath);
return DbQuery(sqliteDb()).sql(sqlSelectAppIdByPath).vars({ normPath }).execute().toLongLong();
} }
bool ConfAppManager::addOrUpdateAppPath(const QString &appOriginPath, bool blocked) bool ConfAppManager::addOrUpdateAppPath(const QString &appOriginPath, bool blocked)

View File

@ -33,7 +33,7 @@ public:
void logBlockedApp(const LogEntryBlocked &logEntry); void logBlockedApp(const LogEntryBlocked &logEntry);
qint64 appIdByPath(const QString &appPath); qint64 appIdByPath(const QString &appOriginPath, QString &normPath);
virtual bool addOrUpdateAppPath(const QString &appOriginPath, bool blocked); virtual bool addOrUpdateAppPath(const QString &appOriginPath, bool blocked);
virtual bool addOrUpdateApp(App &app, bool onlyUpdate = false); virtual bool addOrUpdateApp(App &app, bool onlyUpdate = false);

View File

@ -30,6 +30,7 @@
#include <util/guiutil.h> #include <util/guiutil.h>
#include <util/iconcache.h> #include <util/iconcache.h>
#include <util/ioc/ioccontainer.h> #include <util/ioc/ioccontainer.h>
#include <util/stringutil.h>
#include <util/textareautil.h> #include <util/textareautil.h>
#include <util/variantutil.h> #include <util/variantutil.h>
@ -423,7 +424,7 @@ QLayout *ProgramEditDialog::setupPathLayout()
// Select File // Select File
m_btSelectFile = ControlUtil::createIconToolButton(":/icons/folder.png", [&] { m_btSelectFile = ControlUtil::createIconToolButton(":/icons/folder.png", [&] {
if (!isEmpty()) { if (!isEmpty() && !isWildcard()) {
AppInfoUtil::openFolder(m_editPath->text()); AppInfoUtil::openFolder(m_editPath->text());
return; return;
} }
@ -707,7 +708,7 @@ QLayout *ProgramEditDialog::setupButtonsLayout()
void ProgramEditDialog::fillEditName() void ProgramEditDialog::fillEditName()
{ {
auto appPath = isWildcard() ? m_editWildcard->toPlainText() : m_editPath->text(); auto appPath = getEditText();
if (appPath.isEmpty()) if (appPath.isEmpty())
return; return;
@ -826,10 +827,10 @@ void ProgramEditDialog::fillApp(App &app) const
void ProgramEditDialog::fillAppPath(App &app) const void ProgramEditDialog::fillAppPath(App &app) const
{ {
const QString appPath = m_editPath->text(); const QString appPath = getEditText();
app.appOriginPath = isWildcard() ? m_editWildcard->toPlainText() : appPath; app.appOriginPath = appPath;
app.appPath = FileUtil::normalizePath(appPath); app.appPath = FileUtil::normalizePath(StringUtil::firstLine(appPath));
} }
void ProgramEditDialog::fillAppEndTime(App &app) const void ProgramEditDialog::fillAppEndTime(App &app) const
@ -856,6 +857,11 @@ bool ProgramEditDialog::isWildcard() const
return m_appRow.isWildcard; return m_appRow.isWildcard;
} }
QString ProgramEditDialog::getEditText() const
{
return isWildcard() ? m_editWildcard->toPlainText() : m_editPath->text();
}
void ProgramEditDialog::selectRuleDialog() void ProgramEditDialog::selectRuleDialog()
{ {
auto rulesDialog = RulesWindow::showRulesDialog(Rule::AppRule, this); auto rulesDialog = RulesWindow::showRulesDialog(Rule::AppRule, this);

View File

@ -101,6 +101,8 @@ private:
bool isWildcard() const; bool isWildcard() const;
QString getEditText() const;
void selectRuleDialog(); void selectRuleDialog();
void editRuleDialog(int ruleId); void editRuleDialog(int ruleId);

View File

@ -12,8 +12,8 @@
#include <conf/confappmanager.h> #include <conf/confappmanager.h>
#include <conf/confmanager.h> #include <conf/confmanager.h>
#include <conf/firewallconf.h> #include <conf/firewallconf.h>
#include <util/conf/confutil.h>
#include <util/dateutil.h> #include <util/dateutil.h>
#include <util/fileutil.h>
#include <util/guiutil.h> #include <util/guiutil.h>
#include <util/iconcache.h> #include <util/iconcache.h>
#include <util/ioc/ioccontainer.h> #include <util/ioc/ioccontainer.h>
@ -475,11 +475,12 @@ AppRow AppListModel::appRowById(qint64 appId) const
AppRow AppListModel::appRowByPath(const QString &appPath) const AppRow AppListModel::appRowByPath(const QString &appPath) const
{ {
const QString normPath = FileUtil::normalizePath(appPath); QString normPath;
const qint64 appId = confAppManager()->appIdByPath(normPath); const qint64 appId = confAppManager()->appIdByPath(appPath, normPath);
AppRow appRow = appRowById(appId); AppRow appRow = appRowById(appId);
if (appRow.appId == 0) { if (appRow.appId == 0) {
appRow.isWildcard = ConfUtil::matchWildcard(normPath).hasMatch();
appRow.appOriginPath = appPath; appRow.appOriginPath = appPath;
appRow.appPath = normPath; appRow.appPath = normPath;
} }

View File

@ -185,6 +185,13 @@ int ConfUtil::zoneMaxCount()
return FORT_CONF_ZONE_MAX; return FORT_CONF_ZONE_MAX;
} }
QRegularExpressionMatch ConfUtil::matchWildcard(const QStringView path)
{
static const QRegularExpression wildMatcher("([*?[])");
return StringUtil::match(wildMatcher, path);
}
void ConfUtil::writeVersion() void ConfUtil::writeVersion()
{ {
const int verSize = sizeof(FORT_CONF_VERSION); const int verSize = sizeof(FORT_CONF_VERSION);
@ -617,8 +624,6 @@ bool ConfUtil::addApp(const App &app, bool isNew, appdata_map_t &appsMap, quint3
QString ConfUtil::parseAppPath(const QStringView line, bool &isWild, bool &isPrefix) QString ConfUtil::parseAppPath(const QStringView line, bool &isWild, bool &isPrefix)
{ {
static const QRegularExpression wildMatcher("([*?[])");
auto path = line; auto path = line;
if (path.startsWith('"') && path.endsWith('"')) { if (path.startsWith('"') && path.endsWith('"')) {
path = path.mid(1, path.size() - 2); path = path.mid(1, path.size() - 2);
@ -627,7 +632,7 @@ QString ConfUtil::parseAppPath(const QStringView line, bool &isWild, bool &isPre
if (path.isEmpty()) if (path.isEmpty())
return QString(); return QString();
const auto wildMatch = StringUtil::match(wildMatcher, path); const auto wildMatch = matchWildcard(path);
if (wildMatch.hasMatch()) { if (wildMatch.hasMatch()) {
if (wildMatch.capturedStart() == path.size() - 2 && path.endsWith(QLatin1String("**"))) { if (wildMatch.capturedStart() == path.size() - 2 && path.endsWith(QLatin1String("**"))) {
path.chop(2); path.chop(2);

View File

@ -4,6 +4,7 @@
#include <QByteArray> #include <QByteArray>
#include <QList> #include <QList>
#include <QObject> #include <QObject>
#include <QRegularExpressionMatch>
#include <QVector> #include <QVector>
#include <util/service/serviceinfo.h> #include <util/service/serviceinfo.h>
@ -48,6 +49,8 @@ public:
static int zoneMaxCount(); static int zoneMaxCount();
static QRegularExpressionMatch matchWildcard(const QStringView path);
public slots: public slots:
void writeVersion(); void writeVersion();
void writeServices(const QVector<ServiceInfo> &services, int runningServicesCount); void writeServices(const QVector<ServiceInfo> &services, int runningServicesCount);

View File

@ -244,6 +244,8 @@ QString normalizePath(const QString &path)
return {}; return {};
const QString pathTrimmed = path.trimmed(); const QString pathTrimmed = path.trimmed();
if (pathTrimmed.isEmpty())
return {};
if (isSystemApp(pathTrimmed)) if (isSystemApp(pathTrimmed))
return systemApp(); return systemApp();

View File

@ -85,6 +85,12 @@ QStringList StringUtil::parseMultiString(const char *data)
return list; return list;
} }
QString StringUtil::firstLine(const QString &text)
{
const int pos = text.indexOf('\n');
return (pos != -1) ? text.left(pos) : text;
}
QRegularExpressionMatch StringUtil::match(const QRegularExpression &re, const QStringView &text) QRegularExpressionMatch StringUtil::match(const QRegularExpression &re, const QStringView &text)
{ {
#if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0) #if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0)

View File

@ -25,6 +25,8 @@ public:
static bool buildMultiString(QByteArray &buffer, const QStringList &list); static bool buildMultiString(QByteArray &buffer, const QStringList &list);
static QStringList parseMultiString(const char *data); static QStringList parseMultiString(const char *data);
static QString firstLine(const QString &text);
static QRegularExpressionMatch match(const QRegularExpression &re, const QStringView &text); static QRegularExpressionMatch match(const QRegularExpression &re, const QStringView &text);
}; };