diff --git a/src/ui/conf/confmanager.cpp b/src/ui/conf/confmanager.cpp index 4281c38d..e4555aa3 100644 --- a/src/ui/conf/confmanager.cpp +++ b/src/ui/conf/confmanager.cpp @@ -372,21 +372,6 @@ void ConfManager::showErrorMessage(const QString &errorMessage) } } -bool ConfManager::checkResult(bool ok, bool commit) -{ - const auto errorMessage = ok ? QString() : sqliteDb()->errorMessage(); - - if (commit) { - sqliteDb()->endTransaction(ok); - } - - if (!ok) { - showErrorMessage(errorMessage); - } - - return ok; -} - bool ConfManager::initialize() { if (!sqliteDb()->open()) { @@ -603,8 +588,11 @@ qint64 ConfManager::appIdByPath(const QString &appPath) } bool ConfManager::addApp(const QString &appPath, const QString &appName, const QDateTime &endTime, - qint64 groupId, bool useGroupPerm, bool blocked, bool alerted) + qint64 groupId, int groupIndex, bool useGroupPerm, bool blocked, bool alerted) { + if (!updateDriverUpdateApp(appPath, groupIndex, useGroupPerm, blocked)) + return false; + bool ok = false; sqliteDb()->beginTransaction(); @@ -628,16 +616,17 @@ bool ConfManager::addApp(const QString &appPath, const QString &appName, const Q m_appEndTimer.start(); } - if (alerted) { - emit alertedAppAdded(); - } + emit appAdded(alerted); } return ok; } -bool ConfManager::deleteApp(qint64 appId) +bool ConfManager::deleteApp(qint64 appId, const QString &appPath) { + if (!updateDriverDeleteApp(appPath)) + return false; + bool ok = false; sqliteDb()->beginTransaction(); @@ -649,12 +638,21 @@ bool ConfManager::deleteApp(qint64 appId) sqliteDb()->executeEx(sqlDeleteAppAlert, vars, 0, &ok); } - return checkResult(ok, true); + checkResult(ok, true); + + if (ok) { + emit appRemoved(); + } + + return ok; } -bool ConfManager::updateApp(qint64 appId, const QString &appName, const QDateTime &endTime, - qint64 groupId, bool useGroupPerm, bool blocked) +bool ConfManager::updateApp(qint64 appId, const QString &appPath, const QString &appName, + const QDateTime &endTime, qint64 groupId, int groupIndex, bool useGroupPerm, bool blocked) { + if (!updateDriverUpdateApp(appPath, groupIndex, useGroupPerm, blocked)) + return false; + bool ok = false; sqliteDb()->beginTransaction(); @@ -669,8 +667,12 @@ bool ConfManager::updateApp(qint64 appId, const QString &appName, const QDateTim checkResult(ok, true); - if (ok && !endTime.isNull()) { - m_appEndTimer.start(); + if (ok) { + if (!endTime.isNull()) { + m_appEndTimer.start(); + } + + emit appUpdated(); } return ok; @@ -684,7 +686,13 @@ bool ConfManager::updateAppName(qint64 appId, const QString &appName) sqliteDb()->executeEx(sqlUpdateAppName, vars, 0, &ok); - return checkResult(ok); + checkResult(ok); + + if (ok) { + emit appUpdated(); + } + + return ok; } bool ConfManager::walkApps(const std::function &func) @@ -730,8 +738,8 @@ void ConfManager::updateAppEndTimes() const QString appName = stmt.columnText(4); const bool useGroupPerm = stmt.columnBool(5); - if (updateDriverUpdateApp(appPath, groupIndex, useGroupPerm, true) - && updateApp(appId, appName, QDateTime(), groupId, useGroupPerm, true)) { + if (updateApp(appId, appPath, appName, QDateTime(), groupId, groupIndex, useGroupPerm, + true)) { isAppEndTimesUpdated = true; } } @@ -762,7 +770,13 @@ bool ConfManager::addZone(const QString &zoneName, const QString &sourceCode, co sqliteDb()->executeEx(sqlInsertZone, vars, 0, &ok); - return checkResult(ok); + checkResult(ok); + + if (ok) { + emit zoneAdded(); + } + + return ok; } int ConfManager::getFreeZoneId() @@ -796,12 +810,21 @@ bool ConfManager::deleteZone(int zoneId) sqliteDb()->executeEx(sqlDeleteAddressGroupZone, { qint64(zoneUnMask) }, 0, &ok); } - return checkResult(ok, true); + checkResult(ok, true); + + if (ok) { + emit zoneRemoved(zoneId); + } + + return ok; } bool ConfManager::updateZone(int zoneId, const QString &zoneName, const QString &sourceCode, const QString &url, const QString &formData, bool enabled, bool customUrl) { + if (!updateDriverZoneFlag(zoneId, enabled)) + return false; + bool ok = false; const auto vars = QVariantList() @@ -809,7 +832,13 @@ bool ConfManager::updateZone(int zoneId, const QString &zoneName, const QString sqliteDb()->executeEx(sqlUpdateZone, vars, 0, &ok); - return checkResult(ok); + checkResult(ok); + + if (ok) { + emit zoneUpdated(); + } + + return ok; } bool ConfManager::updateZoneName(int zoneId, const QString &zoneName) @@ -820,18 +849,33 @@ bool ConfManager::updateZoneName(int zoneId, const QString &zoneName) sqliteDb()->executeEx(sqlUpdateZoneName, vars, 0, &ok); - return checkResult(ok); + checkResult(ok); + + if (ok) { + emit zoneUpdated(); + } + + return ok; } bool ConfManager::updateZoneEnabled(int zoneId, bool enabled) { + if (!updateDriverZoneFlag(zoneId, enabled)) + return false; + bool ok = false; const auto vars = QVariantList() << zoneId << enabled; sqliteDb()->executeEx(sqlUpdateZoneEnabled, vars, 0, &ok); - return checkResult(ok); + checkResult(ok); + + if (ok) { + emit zoneUpdated(); + } + + return ok; } bool ConfManager::updateZoneResult(int zoneId, const QString &textChecksum, @@ -845,7 +889,13 @@ bool ConfManager::updateZoneResult(int zoneId, const QString &textChecksum, sqliteDb()->executeEx(sqlUpdateZoneResult, vars, 0, &ok); - return checkResult(ok); + checkResult(ok); + + if (ok) { + emit zoneUpdated(); + } + + return ok; } bool ConfManager::validateDriver() @@ -865,7 +915,6 @@ bool ConfManager::updateDriverConf(bool onlyFlags) const int confSize = onlyFlags ? confUtil.writeFlags(*conf(), buf) : confUtil.write(*conf(), this, *envManager(), buf); - if (confSize == 0) { showErrorMessage(confUtil.errorMessage()); return false; @@ -1085,3 +1134,18 @@ bool ConfManager::saveTask(TaskInfo *taskInfo) } return true; } + +bool ConfManager::checkResult(bool ok, bool commit) +{ + const auto errorMessage = ok ? QString() : sqliteDb()->errorMessage(); + + if (commit) { + sqliteDb()->endTransaction(ok); + } + + if (!ok) { + showErrorMessage(errorMessage); + } + + return ok; +} diff --git a/src/ui/conf/confmanager.h b/src/ui/conf/confmanager.h index de11d269..da7d8718 100644 --- a/src/ui/conf/confmanager.h +++ b/src/ui/conf/confmanager.h @@ -61,10 +61,11 @@ public: qint64 appIdByPath(const QString &appPath); bool addApp(const QString &appPath, const QString &appName, const QDateTime &endTime, - qint64 groupId, bool useGroupPerm, bool blocked, bool alerted = false); - bool deleteApp(qint64 appId); - bool updateApp(qint64 appId, const QString &appName, const QDateTime &endTime, qint64 groupId, - bool useGroupPerm, bool blocked); + qint64 groupId, int groupIndex, bool useGroupPerm, bool blocked, bool alerted = false); + bool deleteApp(qint64 appId, const QString &appPath); + bool updateApp(qint64 appId, const QString &appPath, const QString &appName, + const QDateTime &endTime, qint64 groupId, int groupIndex, bool useGroupPerm, + bool blocked); bool updateAppName(qint64 appId, const QString &appName); bool walkApps(const std::function &func) override; @@ -85,18 +86,21 @@ public: const QDateTime &sourceModTime, const QDateTime &lastRun, const QDateTime &lastSuccess); bool validateDriver(); - bool updateDriverConf(bool onlyFlags = false); - bool updateDriverDeleteApp(const QString &appPath); - bool updateDriverUpdateApp(const QString &appPath, int groupIndex, bool useGroupPerm, - bool blocked, bool remove = false); + virtual bool updateDriverConf(bool onlyFlags = false); void updateDriverZones(quint32 zonesMask, quint32 enabledMask, quint32 dataSize, const QList &zonesData); - bool updateDriverZoneFlag(int zoneId, bool enabled); signals: void confChanged(bool onlyFlags); + void appEndTimesUpdated(); - void alertedAppAdded(); + void appAdded(bool alerted); + void appRemoved(); + void appUpdated(); + + void zoneAdded(); + void zoneRemoved(int zoneId); + void zoneUpdated(); protected: virtual void setupAppEndTimer(); @@ -109,10 +113,13 @@ protected: void saveClientExtFlags(const IniOptions &ini); private: - bool checkResult(bool ok, bool commit = false); - void setupDefault(FirewallConf &conf) const; + bool updateDriverDeleteApp(const QString &appPath); + bool updateDriverUpdateApp(const QString &appPath, int groupIndex, bool useGroupPerm, + bool blocked, bool remove = false); + bool updateDriverZoneFlag(int zoneId, bool enabled); + bool loadFromDb(FirewallConf &conf, bool &isNew); bool saveToDb(const FirewallConf &conf); @@ -126,6 +133,8 @@ private: bool loadTask(TaskInfo *taskInfo); bool saveTask(TaskInfo *taskInfo); + bool checkResult(bool ok, bool commit = false); + private: FortManager *m_fortManager = nullptr; SqliteDb *m_sqliteDb = nullptr; diff --git a/src/ui/form/opt/pages/addressespage.cpp b/src/ui/form/opt/pages/addressespage.cpp index d6bf54b4..d598b6a5 100644 --- a/src/ui/form/opt/pages/addressespage.cpp +++ b/src/ui/form/opt/pages/addressespage.cpp @@ -11,6 +11,7 @@ #include #include "../../../conf/addressgroup.h" +#include "../../../conf/confmanager.h" #include "../../../conf/firewallconf.h" #include "../../../driver/drivercommon.h" #include "../../../fortmanager.h" @@ -344,7 +345,7 @@ void AddressesPage::setupZones() m_excludeAddresses->btSelectZones()->setMenu(m_menuZones); updateZonesMenuEnabled(); - connect(zoneListModel(), &ZoneListModel::zoneRemoved, this, [&](int zoneId) { + connect(confManager(), &ConfManager::zoneRemoved, this, [&](int zoneId) { for (auto addrGroup : addressGroups()) { addrGroup->removeIncludeZone(zoneId); addrGroup->removeExcludeZone(zoneId); diff --git a/src/ui/form/opt/pages/basepage.cpp b/src/ui/form/opt/pages/basepage.cpp index eb6e1fc5..b3e28b09 100644 --- a/src/ui/form/opt/pages/basepage.cpp +++ b/src/ui/form/opt/pages/basepage.cpp @@ -22,6 +22,11 @@ FortSettings *BasePage::settings() const return ctrl()->settings(); } +ConfManager *BasePage::confManager() const +{ + return ctrl()->confManager(); +} + FirewallConf *BasePage::conf() const { return ctrl()->conf(); diff --git a/src/ui/form/opt/pages/basepage.h b/src/ui/form/opt/pages/basepage.h index b67bb89a..bc07fa3f 100644 --- a/src/ui/form/opt/pages/basepage.h +++ b/src/ui/form/opt/pages/basepage.h @@ -14,6 +14,7 @@ QT_FORWARD_DECLARE_CLASS(QMenu) QT_FORWARD_DECLARE_CLASS(QPushButton) QT_FORWARD_DECLARE_CLASS(QTabBar) +class ConfManager; class DriverManager; class FirewallConf; class FortManager; @@ -35,6 +36,7 @@ protected: OptionsController *ctrl() const { return m_ctrl; } FortManager *fortManager() const; FortSettings *settings() const; + ConfManager *confManager() const; FirewallConf *conf() const; IniOptions *ini() const; DriverManager *driverManager() const; diff --git a/src/ui/form/zone/zoneswindow.cpp b/src/ui/form/zone/zoneswindow.cpp index b3a3ec23..5ec27201 100644 --- a/src/ui/form/zone/zoneswindow.cpp +++ b/src/ui/form/zone/zoneswindow.cpp @@ -450,7 +450,7 @@ bool ZonesWindow::saveZoneEditForm() } return zoneListModel()->updateZone( - zoneRow.zoneId, zoneName, sourceCode, url, formData, enabled, customUrl, zoneEdited); + zoneRow.zoneId, zoneName, sourceCode, url, formData, enabled, customUrl); } void ZonesWindow::updateZone(int row, bool enabled) diff --git a/src/ui/fortmanager.cpp b/src/ui/fortmanager.cpp index dce830c6..a09065ab 100644 --- a/src/ui/fortmanager.cpp +++ b/src/ui/fortmanager.cpp @@ -358,8 +358,11 @@ void FortManager::setupTrayIcon() connect(m_trayIcon, &QSystemTrayIcon::messageClicked, this, &FortManager::onTrayMessageClicked); connect(confManager(), &ConfManager::confChanged, m_trayIcon, &TrayIcon::updateTrayMenu); - connect(confManager(), &ConfManager::alertedAppAdded, m_trayIcon, - [&] { m_trayIcon->updateTrayIcon(true); }); + connect(confManager(), &ConfManager::appAdded, m_trayIcon, [&](bool alerted) { + if (alerted) { + m_trayIcon->updateTrayIcon(true); + } + }); connect(qApp, &QCoreApplication::aboutToQuit, this, &FortManager::closeUi); } diff --git a/src/ui/model/applistmodel.cpp b/src/ui/model/applistmodel.cpp index ae9e2b81..dc95f883 100644 --- a/src/ui/model/applistmodel.cpp +++ b/src/ui/model/applistmodel.cpp @@ -58,6 +58,8 @@ void AppListModel::initialize() refresh(); }); connect(confManager(), &ConfManager::appEndTimesUpdated, this, &AppListModel::refresh); + connect(confManager(), &ConfManager::appAdded, this, &TableSqlModel::reset); + connect(confManager(), &ConfManager::appUpdated, this, &TableSqlModel::refresh); } void AppListModel::handleLogBlocked(const LogEntryBlocked &logEntry) @@ -70,10 +72,7 @@ void AppListModel::handleLogBlocked(const LogEntryBlocked &logEntry) const auto groupId = appGroupAt(0)->id(); const auto appName = appInfoCache()->appName(appPath); - if (confManager()->addApp( - appPath, appName, QDateTime(), groupId, false, logEntry.blocked(), true)) { - reset(); - } + confManager()->addApp(appPath, appName, QDateTime(), groupId, false, logEntry.blocked(), true); } int AppListModel::columnCount(const QModelIndex &parent) const @@ -316,53 +315,31 @@ AppRow AppListModel::appRowByPath(const QString &appPath) const bool AppListModel::addApp(const QString &appPath, const QString &appName, const QDateTime &endTime, int groupIndex, bool useGroupPerm, bool blocked) { - if (!confManager()->updateDriverUpdateApp(appPath, groupIndex, useGroupPerm, blocked)) - return false; - const auto groupId = appGroupAt(groupIndex)->id(); - if (confManager()->addApp(appPath, appName, endTime, groupId, useGroupPerm, blocked)) { - reset(); - return true; - } - - return false; + return confManager()->addApp( + appPath, appName, endTime, groupId, groupIndex, useGroupPerm, blocked); } bool AppListModel::updateApp(qint64 appId, const QString &appPath, const QString &appName, const QDateTime &endTime, int groupIndex, bool useGroupPerm, bool blocked) { - if (!confManager()->updateDriverUpdateApp(appPath, groupIndex, useGroupPerm, blocked)) - return false; - const auto groupId = appGroupAt(groupIndex)->id(); - if (confManager()->updateApp(appId, appName, endTime, groupId, useGroupPerm, blocked)) { - refresh(); - return true; - } - - return false; + return confManager()->updateApp( + appId, appPath, appName, endTime, groupId, groupIndex, useGroupPerm, blocked); } bool AppListModel::updateAppName(qint64 appId, const QString &appName) { - if (confManager()->updateAppName(appId, appName)) { - refresh(); - return true; - } - - return false; + return confManager()->updateAppName(appId, appName); } void AppListModel::deleteApp(qint64 appId, const QString &appPath, int row) { - if (!confManager()->updateDriverDeleteApp(appPath)) - return; - beginRemoveRows(QModelIndex(), row, row); - if (confManager()->deleteApp(appId)) { + if (confManager()->deleteApp(appId, appPath)) { invalidateRowCache(); removeRow(row); } diff --git a/src/ui/model/zonelistmodel.cpp b/src/ui/model/zonelistmodel.cpp index c88caa93..cabc5181 100644 --- a/src/ui/model/zonelistmodel.cpp +++ b/src/ui/model/zonelistmodel.cpp @@ -25,8 +25,11 @@ SqliteDb *ZoneListModel::sqliteDb() const void ZoneListModel::initialize() { - initZoneTypes(); - initZoneSources(); + setupZoneTypes(); + setupZoneSources(); + + connect(confManager(), &ConfManager::zoneAdded, this, &TableSqlModel::reset); + connect(confManager(), &ConfManager::zoneUpdated, this, &TableSqlModel::refresh); } int ZoneListModel::columnCount(const QModelIndex &parent) const @@ -140,64 +143,32 @@ const ZoneRow &ZoneListModel::zoneRowAt(int row) const bool ZoneListModel::addZone(const QString &zoneName, const QString &sourceCode, const QString &url, const QString &formData, bool enabled, bool customUrl, int &zoneId) { - if (confManager()->addZone(zoneName, sourceCode, url, formData, enabled, customUrl, zoneId)) { - reset(); - return true; - } - - return false; + return confManager()->addZone(zoneName, sourceCode, url, formData, enabled, customUrl, zoneId); } bool ZoneListModel::updateZone(int zoneId, const QString &zoneName, const QString &sourceCode, - const QString &url, const QString &formData, bool enabled, bool customUrl, - bool updateDriver) + const QString &url, const QString &formData, bool enabled, bool customUrl) { - if (updateDriver && !confManager()->updateDriverZoneFlag(zoneId, enabled)) - return false; - - if (confManager()->updateZone( - zoneId, zoneName, sourceCode, url, formData, enabled, customUrl)) { - refresh(); - return true; - } - - return false; + return confManager()->updateZone( + zoneId, zoneName, sourceCode, url, formData, enabled, customUrl); } bool ZoneListModel::updateZoneName(int zoneId, const QString &zoneName) { - if (confManager()->updateZoneName(zoneId, zoneName)) { - refresh(); - return true; - } - - return false; + return confManager()->updateZoneName(zoneId, zoneName); } bool ZoneListModel::updateZoneEnabled(int zoneId, bool enabled) { - if (!confManager()->updateDriverZoneFlag(zoneId, enabled)) - return false; - - if (confManager()->updateZoneEnabled(zoneId, enabled)) { - refresh(); - return true; - } - - return false; + return confManager()->updateZoneEnabled(zoneId, enabled); } bool ZoneListModel::updateZoneResult(int zoneId, const QString &textChecksum, const QString &binChecksum, const QDateTime &sourceModTime, const QDateTime &lastRun, const QDateTime &lastSuccess) { - if (confManager()->updateZoneResult( - zoneId, textChecksum, binChecksum, sourceModTime, lastRun, lastSuccess)) { - refresh(); - return true; - } - - return false; + return confManager()->updateZoneResult( + zoneId, textChecksum, binChecksum, sourceModTime, lastRun, lastSuccess); } void ZoneListModel::deleteZone(int zoneId, int row) @@ -205,7 +176,6 @@ void ZoneListModel::deleteZone(int zoneId, int row) beginRemoveRows(QModelIndex(), row, row); if (confManager()->deleteZone(zoneId)) { - emit zoneRemoved(zoneId); invalidateRowCache(); removeRow(row); } @@ -275,7 +245,7 @@ QString ZoneListModel::sqlBase() const " FROM zone"; } -void ZoneListModel::initZoneTypes() +void ZoneListModel::setupZoneTypes() { const auto data = FileUtil::readFileData(":/zone/types.json"); if (data.isEmpty()) @@ -297,7 +267,7 @@ void ZoneListModel::initZoneTypes() } } -void ZoneListModel::initZoneSources() +void ZoneListModel::setupZoneSources() { const auto data = FileUtil::readFileData(":/zone/sources.json"); if (data.isEmpty()) diff --git a/src/ui/model/zonelistmodel.h b/src/ui/model/zonelistmodel.h index 791a44da..fd2d1206 100644 --- a/src/ui/model/zonelistmodel.h +++ b/src/ui/model/zonelistmodel.h @@ -56,8 +56,7 @@ public: bool addZone(const QString &zoneName, const QString &sourceCode, const QString &url, const QString &formData, bool enabled, bool customUrl, int &zoneId); bool updateZone(int zoneId, const QString &zoneName, const QString &sourceCode, - const QString &url, const QString &formData, bool enabled, bool customUrl, - bool updateDriver = true); + const QString &url, const QString &formData, bool enabled, bool customUrl); bool updateZoneName(int zoneId, const QString &zoneName); bool updateZoneEnabled(int zoneId, bool enabled); bool updateZoneResult(int zoneId, const QString &textChecksum, const QString &binChecksum, @@ -71,9 +70,6 @@ public: QVariant zoneSourceByCode(const QString &sourceCode) const; const QVariantList &zoneSources() const { return m_zoneSources; } -signals: - void zoneRemoved(int zoneId); - protected: bool updateTableRow(int row) const override; TableRow &tableRow() const override { return m_zoneRow; } @@ -84,9 +80,9 @@ private: QVariant dataDisplay(const QModelIndex &index) const; QVariant dataCheckState(const QModelIndex &index) const; - void initZoneTypes(); - void initZoneSources(); - void initZoneSourceNames(); + void setupZoneTypes(); + void setupZoneSources(); + void setupZoneSourceNames(); private: ConfManager *m_confManager = nullptr; diff --git a/src/ui/rpc/confmanagerrpc.h b/src/ui/rpc/confmanagerrpc.h index f8656d2a..38ee9d59 100644 --- a/src/ui/rpc/confmanagerrpc.h +++ b/src/ui/rpc/confmanagerrpc.h @@ -15,6 +15,8 @@ public: RpcManager *rpcManager() const; + bool updateDriverConf(bool /*onlyFlags*/ = false) override { return false; } + void onConfChanged(const QVariant &confVar); protected: