diff --git a/src/ui/3rdparty/sqlite/sqlitestmt.cpp b/src/ui/3rdparty/sqlite/sqlitestmt.cpp index 37b3af96..9f71134e 100644 --- a/src/ui/3rdparty/sqlite/sqlitestmt.cpp +++ b/src/ui/3rdparty/sqlite/sqlitestmt.cpp @@ -26,7 +26,7 @@ void SqliteStmt::finalize() } } -QString SqliteStmt::expandedSql() +QString SqliteStmt::expandedSql() const { char *sql = sqlite3_expanded_sql(m_stmt); const auto sqlStr = QString::fromUtf8(sql); @@ -184,7 +184,7 @@ bool SqliteStmt::reset() return sqlite3_reset(m_stmt) == SQLITE_OK; } -bool SqliteStmt::isBusy() +bool SqliteStmt::isBusy() const { return sqlite3_stmt_busy(m_stmt) != 0; } @@ -203,43 +203,43 @@ SqliteStmt::StepResult SqliteStmt::step() } } -int SqliteStmt::dataCount() +int SqliteStmt::dataCount() const { return sqlite3_data_count(m_stmt); } -int SqliteStmt::columnCount() +int SqliteStmt::columnCount() const { return sqlite3_column_count(m_stmt); } -QString SqliteStmt::columnName(int column) +QString SqliteStmt::columnName(int column) const { const char *name = sqlite3_column_name(m_stmt, column); return QString::fromUtf8(name); } -qint32 SqliteStmt::columnInt(int column) +qint32 SqliteStmt::columnInt(int column) const { return sqlite3_column_int(m_stmt, column); } -qint64 SqliteStmt::columnInt64(int column) +qint64 SqliteStmt::columnInt64(int column) const { return sqlite3_column_int64(m_stmt, column); } -double SqliteStmt::columnDouble(int column) +double SqliteStmt::columnDouble(int column) const { return sqlite3_column_double(m_stmt, column); } -bool SqliteStmt::columnBool(int column) +bool SqliteStmt::columnBool(int column) const { return columnInt(column) != 0; } -QString SqliteStmt::columnText(int column) +QString SqliteStmt::columnText(int column) const { const char *p = reinterpret_cast(sqlite3_column_text(m_stmt, column)); if (!p || *p == '\0') @@ -252,19 +252,19 @@ QString SqliteStmt::columnText(int column) return QString::fromUtf8(p, bytesCount); } -QDateTime SqliteStmt::columnDateTime(int column) +QDateTime SqliteStmt::columnDateTime(int column) const { const auto msecs = columnInt64(column); return (msecs == 0) ? QDateTime() : QDateTime::fromMSecsSinceEpoch(msecs); } -QDateTime SqliteStmt::columnUnixTime(int column) +QDateTime SqliteStmt::columnUnixTime(int column) const { const auto secs = columnInt64(column); return (secs == 0) ? QDateTime() : QDateTime::fromSecsSinceEpoch(secs); } -QByteArray SqliteStmt::columnBlob(int column, bool isRaw) +QByteArray SqliteStmt::columnBlob(int column, bool isRaw) const { const char *p = static_cast(sqlite3_column_blob(m_stmt, column)); if (!p) @@ -277,7 +277,7 @@ QByteArray SqliteStmt::columnBlob(int column, bool isRaw) return isRaw ? QByteArray::fromRawData(p, bytesCount) : QByteArray(p, bytesCount); } -QVariant SqliteStmt::columnVarBlob(int column) +QVariant SqliteStmt::columnVarBlob(int column) const { const QByteArray data = columnBlob(column); QDataStream stream(data); @@ -302,7 +302,7 @@ QVariant SqliteStmt::columnVarBlob(int column) } } -QVariant SqliteStmt::columnVar(int column) +QVariant SqliteStmt::columnVar(int column) const { switch (sqlite3_column_type(m_stmt, column)) { case SQLITE_INTEGER: @@ -322,7 +322,7 @@ QVariant SqliteStmt::columnVar(int column) return QVariant(); } -bool SqliteStmt::columnIsNull(int column) +bool SqliteStmt::columnIsNull(int column) const { return sqlite3_column_type(m_stmt, column) == SQLITE_NULL; } diff --git a/src/ui/3rdparty/sqlite/sqlitestmt.h b/src/ui/3rdparty/sqlite/sqlitestmt.h index a5876d1c..69feac0e 100644 --- a/src/ui/3rdparty/sqlite/sqlitestmt.h +++ b/src/ui/3rdparty/sqlite/sqlitestmt.h @@ -32,7 +32,7 @@ public: bool prepare(struct sqlite3 *db, const char *sql, PrepareFlags flags = PrepareDefault); void finalize(); - QString expandedSql(); + QString expandedSql() const; int bindParameterIndex(const QString &name) const; @@ -51,25 +51,25 @@ public: bool clearBindings(); bool reset(); - bool isBusy(); + bool isBusy() const; SqliteStmt::StepResult step(); - int dataCount(); - int columnCount(); + int dataCount() const; + int columnCount() const; - QString columnName(int column = 0); - qint32 columnInt(int column = 0); - qint64 columnInt64(int column = 0); - double columnDouble(int column = 0); - bool columnBool(int column = 0); - QString columnText(int column = 0); - QDateTime columnDateTime(int column = 0); - QDateTime columnUnixTime(int column = 0); - QByteArray columnBlob(int column = 0, bool isRaw = false); - QVariant columnVarBlob(int column = 0); - QVariant columnVar(int column = 0); - bool columnIsNull(int column = 0); + QString columnName(int column = 0) const; + qint32 columnInt(int column = 0) const; + qint64 columnInt64(int column = 0) const; + double columnDouble(int column = 0) const; + bool columnBool(int column = 0) const; + QString columnText(int column = 0) const; + QDateTime columnDateTime(int column = 0) const; + QDateTime columnUnixTime(int column = 0) const; + QByteArray columnBlob(int column = 0, bool isRaw = false) const; + QVariant columnVarBlob(int column = 0) const; + QVariant columnVar(int column = 0) const; + bool columnIsNull(int column = 0) const; static void doList(const SqliteStmtList &stmtList); diff --git a/src/ui/FortFirewallUI.pro b/src/ui/FortFirewallUI.pro index 00eae9fb..3ab74a28 100644 --- a/src/ui/FortFirewallUI.pro +++ b/src/ui/FortFirewallUI.pro @@ -192,6 +192,7 @@ SOURCES += \ util/stringutil.cpp \ util/textareautil.cpp \ util/triggertimer.cpp \ + util/variantutil.cpp \ util/window/basewindowstatewatcher.cpp \ util/window/widgetwindow.cpp \ util/window/widgetwindowstatewatcher.cpp \ @@ -389,6 +390,7 @@ HEADERS += \ util/stringutil.h \ util/textareautil.h \ util/triggertimer.h \ + util/variantutil.h \ util/window/basewindowstatewatcher.h \ util/window/widgetwindow.h \ util/window/widgetwindowstatewatcher.h \ diff --git a/src/ui/conf/confmanager.cpp b/src/ui/conf/confmanager.cpp index 9d02fe75..260ee8fa 100644 --- a/src/ui/conf/confmanager.cpp +++ b/src/ui/conf/confmanager.cpp @@ -114,50 +114,36 @@ const char *const sqlUpdateTask = "UPDATE task" const char *const sqlSelectAppPaths = "SELECT app_id, path FROM app;"; -const char *const sqlSelectAppById = "SELECT" - " g.order_index as group_index," - " t.origin_path," - " t.path," - " t.is_wildcard," - " t.use_group_perm," - " t.apply_child," - " t.lan_only," - " t.log_blocked," - " t.log_conn," - " t.blocked," - " t.kill_process," - " (alert.app_id IS NOT NULL) as alerted" - " FROM app t" +#define SELECT_APP_FIELDS \ + " t.app_id," \ + " t.origin_path," \ + " t.path," \ + " t.is_wildcard," \ + " t.use_group_perm," \ + " t.apply_child," \ + " t.lan_only," \ + " t.log_blocked," \ + " t.log_conn," \ + " t.blocked," \ + " t.kill_process," \ + " g.order_index as group_index," \ + " (alert.app_id IS NOT NULL) as alerted" + +const char *const sqlSelectAppById = "SELECT" SELECT_APP_FIELDS " FROM app t" " JOIN app_group g ON g.app_group_id = t.app_group_id" " LEFT JOIN app_alert alert ON alert.app_id = t.app_id" " WHERE t.app_id = ?1;"; -const char *const sqlSelectApps = "SELECT" - " g.order_index as group_index," - " t.origin_path," - " t.path," - " t.is_wildcard," - " t.use_group_perm," - " t.apply_child," - " t.lan_only," - " t.log_blocked," - " t.log_conn," - " t.blocked," - " t.kill_process," - " (alert.app_id IS NOT NULL) as alerted" - " FROM app t" +const char *const sqlSelectApps = "SELECT" SELECT_APP_FIELDS " FROM app t" " JOIN app_group g ON g.app_group_id = t.app_group_id" " LEFT JOIN app_alert alert ON alert.app_id = t.app_id;"; const char *const sqlSelectMinEndApp = "SELECT MIN(end_time) FROM app" " WHERE end_time != 0 AND blocked = 0;"; -const char *const sqlSelectEndedApps = "SELECT t.app_id, g.order_index as group_index," - " t.origin_path, t.path, t.name," - " t.is_wildcard, t.use_group_perm," - " t.apply_child, t.lan_only, t.log_blocked, t.log_conn" - " FROM app t" +const char *const sqlSelectEndedApps = "SELECT" SELECT_APP_FIELDS " FROM app t" " JOIN app_group g ON g.app_group_id = t.app_group_id" + " LEFT JOIN app_alert alert ON alert.app_id = t.app_id" " WHERE end_time <= ?1 AND blocked = 0;"; const char *const sqlSelectAppIdByPath = "SELECT app_id FROM app WHERE path = ?1;"; @@ -177,7 +163,7 @@ const char *const sqlUpsertApp = "INSERT INTO app(app_group_id, origin_path, pat const char *const sqlInsertAppAlert = "INSERT INTO app_alert(app_id) VALUES(?1);"; -const char *const sqlDeleteApp = "DELETE FROM app WHERE app_id = ?1 RETURNING path;"; +const char *const sqlDeleteApp = "DELETE FROM app WHERE app_id = ?1 RETURNING path, is_wildcard;"; const char *const sqlDeleteAppAlert = "DELETE FROM app_alert WHERE app_id = ?1;"; @@ -897,13 +883,28 @@ qint64 ConfManager::appIdByPath(const QString &appPath) bool ConfManager::addApp(const App &app) { - if (!updateDriverUpdateApp(app)) + if (!addOrUpdateApp(app)) return false; - return addOrUpdateApp(app); + updateDriverUpdateAppConf(app); + + return true; } -bool ConfManager::deleteApp(qint64 appId) +void ConfManager::deleteApps(const QVector &appIdList) +{ + bool isWildcard = false; + + for (const qint64 appId : appIdList) { + deleteApp(appId, isWildcard); + } + + if (isWildcard) { + updateDriverConf(); + } +} + +bool ConfManager::deleteApp(qint64 appId, bool &isWildcard) { bool ok = false; @@ -911,7 +912,8 @@ bool ConfManager::deleteApp(qint64 appId) const auto vars = QVariantList() << appId; - const QString appPath = sqliteDb()->executeEx(sqlDeleteApp, vars, 1, &ok).toString(); + const auto resList = sqliteDb()->executeEx(sqlDeleteApp, vars, 2, &ok).toList(); + if (ok) { sqliteDb()->executeEx(sqlDeleteAppAlert, vars, 0, &ok); } @@ -919,7 +921,13 @@ bool ConfManager::deleteApp(qint64 appId) commitTransaction(ok); if (ok) { - updateDriverDeleteApp(appPath); + const QString appPath = resList.at(0).toString(); + + if (resList.at(1).toBool()) { + isWildcard = true; + } else { + updateDriverDeleteApp(appPath); + } emitAppChanged(); } @@ -950,9 +958,7 @@ bool ConfManager::purgeApps() } // Delete apps - for (const qint64 appId : appIdList) { - deleteApp(appId); - } + deleteApps(appIdList); return true; } @@ -963,9 +969,6 @@ bool ConfManager::updateApp(const App &app) if (appGroup->isNull()) return false; - if (!updateDriverUpdateApp(app)) - return false; - bool ok = false; sqliteDb()->beginTransaction(); @@ -976,6 +979,7 @@ bool ConfManager::updateApp(const App &app) << app.blocked << app.killProcess << (!app.endTime.isNull() ? app.endTime : QVariant()); sqliteDb()->executeEx(sqlUpdateApp, vars, 0, &ok); + if (ok) { sqliteDb()->executeEx(sqlDeleteAppAlert, { app.appId }, 0, &ok); } @@ -988,37 +992,53 @@ bool ConfManager::updateApp(const App &app) } emitAppUpdated(); + + updateDriverUpdateAppConf(app); } return ok; } -bool ConfManager::updateAppBlocked(qint64 appId, bool blocked, bool killProcess) +void ConfManager::updateAppsBlocked( + const QVector &appIdList, bool blocked, bool killProcess) { - bool changed = false; - if (!updateDriverAppBlocked(appId, blocked, killProcess, changed)) + bool isWildcard = (appIdList.size() > 7); + + for (const qint64 appId : appIdList) { + updateAppBlocked(appId, blocked, killProcess, isWildcard); + } + + if (isWildcard) { + updateDriverConf(); + } +} + +bool ConfManager::updateAppBlocked(qint64 appId, bool blocked, bool killProcess, bool &isWildcard) +{ + App app; + app.appId = appId; + if (!loadAppById(app)) return false; - bool ok = true; + const bool wasAlerted = app.alerted; + app.alerted = false; - sqliteDb()->beginTransaction(); + if (!wasAlerted && app.blocked == blocked && app.killProcess == killProcess) + return true; - const auto vars = QVariantList() << appId << blocked << killProcess; + app.blocked = blocked; + app.killProcess = killProcess; - if (changed) { - sqliteDb()->executeEx(sqlUpdateAppBlocked, vars, 0, &ok); - } - if (ok) { - sqliteDb()->executeEx(sqlDeleteAppAlert, { appId }, 0, &ok); + if (!saveAppBlocked(app)) + return false; + + if (app.isWildcard) { + isWildcard = true; + } else { + updateDriverUpdateApp(app); } - commitTransaction(ok); - - if (ok) { - emitAppUpdated(); - } - - return ok; + return true; } bool ConfManager::updateAppName(qint64 appId, const QString &appName) @@ -1046,18 +1066,7 @@ bool ConfManager::walkApps(const std::function &func) while (stmt.step() == SqliteStmt::StepRow) { App app; - app.groupIndex = stmt.columnInt(0); - app.appOriginPath = stmt.columnText(1); - app.appPath = stmt.columnText(2); - app.isWildcard = stmt.columnBool(3); - app.useGroupPerm = stmt.columnBool(4); - app.applyChild = stmt.columnBool(5); - app.lanOnly = stmt.columnBool(6); - app.logBlocked = stmt.columnBool(7); - app.logConn = stmt.columnBool(8); - app.blocked = stmt.columnBool(9); - app.killProcess = stmt.columnBool(10); - app.alerted = stmt.columnBool(11); + fillApp(app, stmt); if (!func(app)) return false; @@ -1066,6 +1075,29 @@ bool ConfManager::walkApps(const std::function &func) return true; } +bool ConfManager::saveAppBlocked(const App &app) +{ + bool ok = true; + + sqliteDb()->beginTransaction(); + + const auto vars = QVariantList() << app.appId << app.blocked << app.killProcess; + + sqliteDb()->executeEx(sqlUpdateAppBlocked, vars, 0, &ok); + + if (ok) { + sqliteDb()->executeEx(sqlDeleteAppAlert, { app.appId }, 0, &ok); + } + + commitTransaction(ok); + + if (ok) { + emitAppUpdated(); + } + + return ok; +} + void ConfManager::updateAppEndTimes() { SqliteStmt stmt; @@ -1076,17 +1108,8 @@ void ConfManager::updateAppEndTimes() while (stmt.step() == SqliteStmt::StepRow) { App app; - app.appId = stmt.columnInt64(0); - app.groupIndex = stmt.columnInt(1); - app.appOriginPath = stmt.columnText(2); - app.appPath = stmt.columnText(3); - app.appName = stmt.columnText(4); - app.isWildcard = stmt.columnBool(5); - app.useGroupPerm = stmt.columnBool(6); - app.applyChild = stmt.columnBool(7); - app.lanOnly = stmt.columnBool(8); - app.logBlocked = stmt.columnBool(9); - app.logConn = stmt.columnBool(10); + fillApp(app, stmt); + app.blocked = true; app.killProcess = false; @@ -1364,19 +1387,24 @@ bool ConfManager::addOrUpdateApp(const App &app) return ok; } -bool ConfManager::updateDriverAppBlocked( - qint64 appId, bool blocked, bool killProcess, bool &changed) +bool ConfManager::loadAppById(App &app) { SqliteStmt stmt; if (!sqliteDb()->prepare(stmt, sqlSelectAppById)) return false; - stmt.bindInt64(1, appId); + stmt.bindInt64(1, app.appId); if (stmt.step() != SqliteStmt::StepRow) return false; - App app; - app.groupIndex = stmt.columnInt(0); + fillApp(app, stmt); + + return true; +} + +void ConfManager::fillApp(App &app, const SqliteStmt &stmt) +{ + app.appId = stmt.columnInt64(0); app.appOriginPath = stmt.columnText(1); app.appPath = stmt.columnText(2); app.isWildcard = stmt.columnBool(3); @@ -1387,27 +1415,8 @@ bool ConfManager::updateDriverAppBlocked( app.logConn = stmt.columnBool(8); app.blocked = stmt.columnBool(9); app.killProcess = stmt.columnBool(10); - const bool wasAlerted = stmt.columnBool(11); - - if (!updateDriverCheckUpdateApp(app, blocked, killProcess, /*force=*/wasAlerted)) - return false; - - changed = true; - - return true; -} - -bool ConfManager::updateDriverCheckUpdateApp(App &app, bool blocked, bool killProcess, bool force) -{ - if (!force) { - if (blocked == app.blocked && killProcess == app.killProcess) - return true; - } - - app.blocked = blocked; - app.killProcess = killProcess; - - return updateDriverUpdateApp(app); + app.groupIndex = stmt.columnInt(11); + app.alerted = stmt.columnBool(12); } bool ConfManager::updateDriverDeleteApp(const QString &appPath) @@ -1441,6 +1450,11 @@ bool ConfManager::updateDriverUpdateApp(const App &app, bool remove) return true; } +bool ConfManager::updateDriverUpdateAppConf(const App &app) +{ + return app.isWildcard ? updateDriverConf() : updateDriverUpdateApp(app); +} + void ConfManager::updateDriverZones(quint32 zonesMask, quint32 enabledMask, quint32 dataSize, const QList &zonesData) { diff --git a/src/ui/conf/confmanager.h b/src/ui/conf/confmanager.h index 4908f926..c1f01d66 100644 --- a/src/ui/conf/confmanager.h +++ b/src/ui/conf/confmanager.h @@ -64,15 +64,24 @@ public: void logBlockedApp(const LogEntryBlocked &logEntry); qint64 appIdByPath(const QString &appPath); + virtual bool addApp(const App &app); - virtual bool deleteApp(qint64 appId); + + virtual void deleteApps(const QVector &appIdList); + bool deleteApp(qint64 appId, bool &isWildcard); + virtual bool purgeApps(); + virtual bool updateApp(const App &app); - virtual bool updateAppBlocked(qint64 appId, bool blocked, bool killProcess = false); + + virtual void updateAppsBlocked(const QVector &appIdList, bool blocked, bool killProcess); + bool updateAppBlocked(qint64 appId, bool blocked, bool killProcess, bool &isWildcard); + virtual bool updateAppName(qint64 appId, const QString &appName); bool walkApps(const std::function &func) override; + bool saveAppBlocked(const App &app); void updateAppEndTimes(); virtual bool addZone(Zone &zone); @@ -127,13 +136,15 @@ private: void emitAppUpdated(); bool addOrUpdateApp(const App &app); - bool updateDriverAppBlocked(qint64 appId, bool blocked, bool killProcess, bool &changed); - bool updateDriverCheckUpdateApp(App &app, bool blocked, bool killProcess, bool force); + + bool loadAppById(App &app); + static void fillApp(App &app, const SqliteStmt &stmt); bool validateConf(const FirewallConf &newConf); bool updateDriverDeleteApp(const QString &appPath); bool updateDriverUpdateApp(const App &app, bool remove = false); + bool updateDriverUpdateAppConf(const App &app); bool updateDriverZoneFlag(int zoneId, bool enabled); bool loadFromDb(FirewallConf &conf, bool &isNew); diff --git a/src/ui/control/control.cpp b/src/ui/control/control.cpp index bb56b50c..c41e0ff9 100644 --- a/src/ui/control/control.cpp +++ b/src/ui/control/control.cpp @@ -22,10 +22,10 @@ const char *const commandString(Command cmd) CASE_STRING(Rpc_ConfManager_saveVariant) CASE_STRING(Rpc_ConfManager_addApp) - CASE_STRING(Rpc_ConfManager_deleteApp) + CASE_STRING(Rpc_ConfManager_deleteApps) CASE_STRING(Rpc_ConfManager_purgeApps) CASE_STRING(Rpc_ConfManager_updateApp) - CASE_STRING(Rpc_ConfManager_updateAppBlocked) + CASE_STRING(Rpc_ConfManager_updateAppsBlocked) CASE_STRING(Rpc_ConfManager_updateAppName) CASE_STRING(Rpc_ConfManager_addZone) CASE_STRING(Rpc_ConfManager_deleteZone) @@ -106,10 +106,10 @@ RpcManager managerByCommand(Command cmd) Rpc_ConfManager, // Rpc_ConfManager_saveVariant, Rpc_ConfManager, // Rpc_ConfManager_addApp, - Rpc_ConfManager, // Rpc_ConfManager_deleteApp, + Rpc_ConfManager, // Rpc_ConfManager_deleteApps, Rpc_ConfManager, // Rpc_ConfManager_purgeApps, Rpc_ConfManager, // Rpc_ConfManager_updateApp, - Rpc_ConfManager, // Rpc_ConfManager_updateAppBlocked, + Rpc_ConfManager, // Rpc_ConfManager_updateAppsBlocked, Rpc_ConfManager, // Rpc_ConfManager_updateAppName, Rpc_ConfManager, // Rpc_ConfManager_addZone, Rpc_ConfManager, // Rpc_ConfManager_deleteZone, @@ -173,10 +173,10 @@ bool commandRequiresValidation(Command cmd) true, // Rpc_ConfManager_saveVariant, true, // Rpc_ConfManager_addApp, - true, // Rpc_ConfManager_deleteApp, + true, // Rpc_ConfManager_deleteApps, true, // Rpc_ConfManager_purgeApps, true, // Rpc_ConfManager_updateApp, - true, // Rpc_ConfManager_updateAppBlocked, + true, // Rpc_ConfManager_updateAppsBlocked, true, // Rpc_ConfManager_updateAppName, true, // Rpc_ConfManager_addZone, true, // Rpc_ConfManager_deleteZone, diff --git a/src/ui/control/control.h b/src/ui/control/control.h index 3572add9..4215e8f5 100644 --- a/src/ui/control/control.h +++ b/src/ui/control/control.h @@ -21,10 +21,10 @@ enum Command : qint8 { Rpc_ConfManager_saveVariant, Rpc_ConfManager_addApp, - Rpc_ConfManager_deleteApp, + Rpc_ConfManager_deleteApps, Rpc_ConfManager_purgeApps, Rpc_ConfManager_updateApp, - Rpc_ConfManager_updateAppBlocked, + Rpc_ConfManager_updateAppsBlocked, Rpc_ConfManager_updateAppName, Rpc_ConfManager_addZone, Rpc_ConfManager_deleteZone, diff --git a/src/ui/form/prog/programeditdialog.cpp b/src/ui/form/prog/programeditdialog.cpp index f5efff95..27e5adbb 100644 --- a/src/ui/form/prog/programeditdialog.cpp +++ b/src/ui/form/prog/programeditdialog.cpp @@ -422,7 +422,8 @@ void ProgramEditDialog::fillEditName() if (appPath.isEmpty()) return; - const QString appName = IoC()->appName(appPath); + const QString appName = isWildcard() ? appPath : IoC()->appName(appPath); + m_editName->setText(appName); } @@ -500,6 +501,7 @@ bool ProgramEditDialog::validateFields() const void ProgramEditDialog::fillApp(App &app) const { + app.isWildcard = isWildcard(); app.useGroupPerm = m_cbUseGroupPerm->isChecked(); app.applyChild = m_cbApplyChild->isChecked(); app.lanOnly = m_cbLanOnly->isChecked(); diff --git a/src/ui/form/prog/programswindow.cpp b/src/ui/form/prog/programswindow.cpp index a610c9ab..1abcbe48 100644 --- a/src/ui/form/prog/programswindow.cpp +++ b/src/ui/form/prog/programswindow.cpp @@ -258,7 +258,8 @@ void ProgramsWindow::setupEditMenu() m_actAddApp = editMenu->addAction(IconCache::icon(":/icons/add.png"), QString()); m_actAddApp->setShortcut(Qt::Key_Plus); - m_actAddWildcard = editMenu->addAction(IconCache::icon(":/icons/asterisk_orange.png"), QString()); + m_actAddWildcard = + editMenu->addAction(IconCache::icon(":/icons/asterisk_orange.png"), QString()); m_actAddWildcard->setShortcut(QKeyCombination(Qt::CTRL, Qt::Key_N)); m_actEditApp = editMenu->addAction(IconCache::icon(":/icons/pencil.png"), QString()); @@ -455,18 +456,12 @@ void ProgramsWindow::openAppEditForm(const AppRow &appRow, const QVector void ProgramsWindow::updateSelectedApps(bool blocked, bool killProcess) { - const QVector appIdList = selectedAppIdList(); - for (const qint64 appId : appIdList) { - confManager()->updateAppBlocked(appId, blocked, killProcess); - } + confManager()->updateAppsBlocked(selectedAppIdList(), blocked, killProcess); } void ProgramsWindow::deleteSelectedApps() { - const QVector appIdList = selectedAppIdList(); - for (const qint64 appId : appIdList) { - confManager()->deleteApp(appId); - } + confManager()->deleteApps(selectedAppIdList()); } int ProgramsWindow::appListCurrentIndex() const diff --git a/src/ui/rpc/confmanagerrpc.cpp b/src/ui/rpc/confmanagerrpc.cpp index 02192ad4..04c9d0d8 100644 --- a/src/ui/rpc/confmanagerrpc.cpp +++ b/src/ui/rpc/confmanagerrpc.cpp @@ -9,6 +9,7 @@ #include #include #include +#include ConfManagerRpc::ConfManagerRpc(const QString &filePath, QObject *parent) : ConfManager(filePath, parent, SqliteDb::OpenDefaultReadOnly) @@ -23,9 +24,10 @@ bool ConfManagerRpc::addApp(const App &app) app.appPath, app.appName, app.endTime }); } -bool ConfManagerRpc::deleteApp(qint64 appId) +void ConfManagerRpc::deleteApps(const QVector &appIdList) { - return IoC()->doOnServer(Control::Rpc_ConfManager_deleteApp, { appId }); + IoC()->doOnServer( + Control::Rpc_ConfManager_deleteApps, { VariantUtil::vectorToList(appIdList) }); } bool ConfManagerRpc::purgeApps() @@ -41,10 +43,11 @@ bool ConfManagerRpc::updateApp(const App &app) app.appOriginPath, app.appPath, app.appName, app.endTime }); } -bool ConfManagerRpc::updateAppBlocked(qint64 appId, bool blocked, bool killProcess) +void ConfManagerRpc::updateAppsBlocked( + const QVector &appIdList, bool blocked, bool killProcess) { - return IoC()->doOnServer( - Control::Rpc_ConfManager_updateAppBlocked, { appId, blocked, killProcess }); + IoC()->doOnServer(Control::Rpc_ConfManager_updateAppsBlocked, + { VariantUtil::vectorToList(appIdList), blocked, killProcess }); } bool ConfManagerRpc::updateAppName(qint64 appId, const QString &appName) diff --git a/src/ui/rpc/confmanagerrpc.h b/src/ui/rpc/confmanagerrpc.h index d71e52d1..fdbc16ba 100644 --- a/src/ui/rpc/confmanagerrpc.h +++ b/src/ui/rpc/confmanagerrpc.h @@ -14,10 +14,11 @@ public: explicit ConfManagerRpc(const QString &filePath, QObject *parent = nullptr); bool addApp(const App &app) override; - bool deleteApp(qint64 appId) override; + void deleteApps(const QVector &appIdList) override; bool purgeApps() override; bool updateApp(const App &app) override; - bool updateAppBlocked(qint64 appId, bool blocked, bool killProcess = false) override; + void updateAppsBlocked( + const QVector &appIdList, bool blocked, bool killProcess) override; bool updateAppName(qint64 appId, const QString &appName) override; bool addZone(Zone &zone) override; diff --git a/src/ui/rpc/rpcmanager.cpp b/src/ui/rpc/rpcmanager.cpp index b1ee7b1c..3d1ff0ea 100644 --- a/src/ui/rpc/rpcmanager.cpp +++ b/src/ui/rpc/rpcmanager.cpp @@ -18,6 +18,7 @@ #include #include #include +#include namespace { @@ -88,10 +89,11 @@ bool processConfManager_addApp( return confManager->addApp(app); } -bool processConfManager_deleteApp( +bool processConfManager_deleteApps( ConfManager *confManager, const ProcessCommandArgs &p, QVariantList & /*resArgs*/) { - return confManager->deleteApp(p.args.value(0).toLongLong()); + confManager->deleteApps(VariantUtil::listToVector(p.args.value(0).toList())); + return true; } bool processConfManager_purgeApps( @@ -122,11 +124,12 @@ bool processConfManager_updateApp( return confManager->updateApp(app); } -bool processConfManager_updateAppBlocked( +bool processConfManager_updateAppsBlocked( ConfManager *confManager, const ProcessCommandArgs &p, QVariantList & /*resArgs*/) { - return confManager->updateAppBlocked( - p.args.value(0).toLongLong(), p.args.value(1).toBool(), p.args.value(2).toBool()); + confManager->updateAppsBlocked(VariantUtil::listToVector(p.args.value(0).toList()), + p.args.value(1).toBool(), p.args.value(2).toBool()); + return true; } bool processConfManager_updateAppName( @@ -201,10 +204,10 @@ using processConfManager_func = bool (*)( static processConfManager_func processConfManager_funcList[] = { &processConfManager_saveVariant, // Rpc_ConfManager_saveVariant, &processConfManager_addApp, // Rpc_ConfManager_addApp, - &processConfManager_deleteApp, // Rpc_ConfManager_deleteApp, + &processConfManager_deleteApps, // Rpc_ConfManager_deleteApps, &processConfManager_purgeApps, // Rpc_ConfManager_purgeApps, &processConfManager_updateApp, // Rpc_ConfManager_updateApp, - &processConfManager_updateAppBlocked, // Rpc_ConfManager_updateAppBlocked, + &processConfManager_updateAppsBlocked, // Rpc_ConfManager_updateAppsBlocked, &processConfManager_updateAppName, // Rpc_ConfManager_updateAppName, &processConfManager_addZone, // Rpc_ConfManager_addZone, &processConfManager_deleteZone, // Rpc_ConfManager_deleteZone, diff --git a/src/ui/util/variantutil.cpp b/src/ui/util/variantutil.cpp new file mode 100644 index 00000000..03f9020f --- /dev/null +++ b/src/ui/util/variantutil.cpp @@ -0,0 +1,25 @@ +#include "variantutil.h" + +QVariantList VariantUtil::vectorToList(const QVector &array) +{ + QVariantList list; + list.reserve(array.size()); + + for (const qint64 v : array) { + list.append(v); + } + + return list; +} + +QVector VariantUtil::listToVector(const QVariantList &list) +{ + QVector array; + array.reserve(list.size()); + + for (const QVariant &v : list) { + array.append(v.toLongLong()); + } + + return array; +} diff --git a/src/ui/util/variantutil.h b/src/ui/util/variantutil.h new file mode 100644 index 00000000..4b932791 --- /dev/null +++ b/src/ui/util/variantutil.h @@ -0,0 +1,16 @@ +#ifndef VARIANTUTIL_H +#define VARIANTUTIL_H + +#include +#include +#include + +class VariantUtil +{ +public: + static QVariantList vectorToList(const QVector &array); + + static QVector listToVector(const QVariantList &list); +}; + +#endif // VARIANTUTIL_H