From 89ee96ba1f02e06964dff42d296e00d5c653c6c5 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Fri, 26 Apr 2024 19:14:38 +0300 Subject: [PATCH] UI: Support wildcard paths from command line --- src/ui/conf/confappmanager.cpp | 10 ++++++---- src/ui/conf/confappmanager.h | 2 +- src/ui/form/prog/programeditdialog.cpp | 16 +++++++++++----- src/ui/form/prog/programeditdialog.h | 2 ++ src/ui/model/applistmodel.cpp | 7 ++++--- src/ui/util/conf/confutil.cpp | 11 ++++++++--- src/ui/util/conf/confutil.h | 3 +++ src/ui/util/fileutil.cpp | 2 ++ src/ui/util/stringutil.cpp | 6 ++++++ src/ui/util/stringutil.h | 2 ++ 10 files changed, 45 insertions(+), 16 deletions(-) diff --git a/src/ui/conf/confappmanager.cpp b/src/ui/conf/confappmanager.cpp index bcdc3fd0..4ad41d0f 100644 --- a/src/ui/conf/confappmanager.cpp +++ b/src/ui/conf/confappmanager.cpp @@ -202,12 +202,12 @@ bool ConfAppManager::addAppPathBlocked(App &app) // app.appOriginPath // app.scheduleAction - app.appPath = FileUtil::normalizePath(app.appOriginPath); + app.appId = appIdByPath(app.appOriginPath, app.appPath); - app.appId = appIdByPath(app.appPath); if (app.appId > 0) return false; // already added by user + app.isWildcard = ConfUtil::matchWildcard(app.appPath).hasMatch(); app.appName = IoC()->appName(app.appPath); const bool ok = addOrUpdateApp(app); @@ -295,9 +295,11 @@ void ConfAppManager::logBlockedApp(const LogEntryBlocked &logEntry) 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) diff --git a/src/ui/conf/confappmanager.h b/src/ui/conf/confappmanager.h index 4dae14ce..99ef2b22 100644 --- a/src/ui/conf/confappmanager.h +++ b/src/ui/conf/confappmanager.h @@ -33,7 +33,7 @@ public: 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 addOrUpdateApp(App &app, bool onlyUpdate = false); diff --git a/src/ui/form/prog/programeditdialog.cpp b/src/ui/form/prog/programeditdialog.cpp index 3a0b9f5d..2e866e85 100644 --- a/src/ui/form/prog/programeditdialog.cpp +++ b/src/ui/form/prog/programeditdialog.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -423,7 +424,7 @@ QLayout *ProgramEditDialog::setupPathLayout() // Select File m_btSelectFile = ControlUtil::createIconToolButton(":/icons/folder.png", [&] { - if (!isEmpty()) { + if (!isEmpty() && !isWildcard()) { AppInfoUtil::openFolder(m_editPath->text()); return; } @@ -707,7 +708,7 @@ QLayout *ProgramEditDialog::setupButtonsLayout() void ProgramEditDialog::fillEditName() { - auto appPath = isWildcard() ? m_editWildcard->toPlainText() : m_editPath->text(); + auto appPath = getEditText(); if (appPath.isEmpty()) return; @@ -826,10 +827,10 @@ void ProgramEditDialog::fillApp(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.appPath = FileUtil::normalizePath(appPath); + app.appOriginPath = appPath; + app.appPath = FileUtil::normalizePath(StringUtil::firstLine(appPath)); } void ProgramEditDialog::fillAppEndTime(App &app) const @@ -856,6 +857,11 @@ bool ProgramEditDialog::isWildcard() const return m_appRow.isWildcard; } +QString ProgramEditDialog::getEditText() const +{ + return isWildcard() ? m_editWildcard->toPlainText() : m_editPath->text(); +} + void ProgramEditDialog::selectRuleDialog() { auto rulesDialog = RulesWindow::showRulesDialog(Rule::AppRule, this); diff --git a/src/ui/form/prog/programeditdialog.h b/src/ui/form/prog/programeditdialog.h index 230dc96d..24d23feb 100644 --- a/src/ui/form/prog/programeditdialog.h +++ b/src/ui/form/prog/programeditdialog.h @@ -101,6 +101,8 @@ private: bool isWildcard() const; + QString getEditText() const; + void selectRuleDialog(); void editRuleDialog(int ruleId); diff --git a/src/ui/model/applistmodel.cpp b/src/ui/model/applistmodel.cpp index 3402d968..c2d8bf0f 100644 --- a/src/ui/model/applistmodel.cpp +++ b/src/ui/model/applistmodel.cpp @@ -12,8 +12,8 @@ #include #include #include +#include #include -#include #include #include #include @@ -475,11 +475,12 @@ AppRow AppListModel::appRowById(qint64 appId) const AppRow AppListModel::appRowByPath(const QString &appPath) const { - const QString normPath = FileUtil::normalizePath(appPath); - const qint64 appId = confAppManager()->appIdByPath(normPath); + QString normPath; + const qint64 appId = confAppManager()->appIdByPath(appPath, normPath); AppRow appRow = appRowById(appId); if (appRow.appId == 0) { + appRow.isWildcard = ConfUtil::matchWildcard(normPath).hasMatch(); appRow.appOriginPath = appPath; appRow.appPath = normPath; } diff --git a/src/ui/util/conf/confutil.cpp b/src/ui/util/conf/confutil.cpp index a3d5c75a..37a5d116 100644 --- a/src/ui/util/conf/confutil.cpp +++ b/src/ui/util/conf/confutil.cpp @@ -185,6 +185,13 @@ int ConfUtil::zoneMaxCount() return FORT_CONF_ZONE_MAX; } +QRegularExpressionMatch ConfUtil::matchWildcard(const QStringView path) +{ + static const QRegularExpression wildMatcher("([*?[])"); + + return StringUtil::match(wildMatcher, path); +} + void ConfUtil::writeVersion() { 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) { - static const QRegularExpression wildMatcher("([*?[])"); - auto path = line; if (path.startsWith('"') && path.endsWith('"')) { path = path.mid(1, path.size() - 2); @@ -627,7 +632,7 @@ QString ConfUtil::parseAppPath(const QStringView line, bool &isWild, bool &isPre if (path.isEmpty()) return QString(); - const auto wildMatch = StringUtil::match(wildMatcher, path); + const auto wildMatch = matchWildcard(path); if (wildMatch.hasMatch()) { if (wildMatch.capturedStart() == path.size() - 2 && path.endsWith(QLatin1String("**"))) { path.chop(2); diff --git a/src/ui/util/conf/confutil.h b/src/ui/util/conf/confutil.h index 0203f5a4..47363f86 100644 --- a/src/ui/util/conf/confutil.h +++ b/src/ui/util/conf/confutil.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -48,6 +49,8 @@ public: static int zoneMaxCount(); + static QRegularExpressionMatch matchWildcard(const QStringView path); + public slots: void writeVersion(); void writeServices(const QVector &services, int runningServicesCount); diff --git a/src/ui/util/fileutil.cpp b/src/ui/util/fileutil.cpp index 419e0dd0..a309a29c 100644 --- a/src/ui/util/fileutil.cpp +++ b/src/ui/util/fileutil.cpp @@ -244,6 +244,8 @@ QString normalizePath(const QString &path) return {}; const QString pathTrimmed = path.trimmed(); + if (pathTrimmed.isEmpty()) + return {}; if (isSystemApp(pathTrimmed)) return systemApp(); diff --git a/src/ui/util/stringutil.cpp b/src/ui/util/stringutil.cpp index 9abf948d..af3ff737 100644 --- a/src/ui/util/stringutil.cpp +++ b/src/ui/util/stringutil.cpp @@ -85,6 +85,12 @@ QStringList StringUtil::parseMultiString(const char *data) 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) { #if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0) diff --git a/src/ui/util/stringutil.h b/src/ui/util/stringutil.h index c56ec360..52ad4f21 100644 --- a/src/ui/util/stringutil.h +++ b/src/ui/util/stringutil.h @@ -25,6 +25,8 @@ public: static bool buildMultiString(QByteArray &buffer, const QStringList &list); static QStringList parseMultiString(const char *data); + static QString firstLine(const QString &text); + static QRegularExpressionMatch match(const QRegularExpression &re, const QStringView &text); };