From 2fa8c0a2a5ed7b5383000ddd4a9aa14f148db9f2 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Mon, 13 Jan 2020 09:30:42 +0500 Subject: [PATCH] UI: ProgramsWindow: Relayout table. --- src/ui/3rdparty/sqlite/sqlitedb.cpp | 10 +- src/ui/3rdparty/sqlite/sqlitedb.h | 7 +- src/ui/conf/confmanager.cpp | 175 ++++++++++-------------- src/ui/conf/confmanager.h | 45 +++--- src/ui/form/prog/programscontroller.cpp | 8 +- src/ui/form/prog/programscontroller.h | 2 + src/ui/form/prog/programswindow.cpp | 34 +++-- src/ui/form/prog/programswindow.h | 2 + src/ui/fortmanager.cpp | 115 +++++++--------- src/ui/fortmanager.h | 22 +-- src/ui/log/model/applistmodel.cpp | 175 ++++++++++++++---------- src/ui/log/model/applistmodel.h | 20 +-- src/ui/util/guiutil.cpp | 4 +- 13 files changed, 308 insertions(+), 311 deletions(-) diff --git a/src/ui/3rdparty/sqlite/sqlitedb.cpp b/src/ui/3rdparty/sqlite/sqlitedb.cpp index 0de861da..107b8928 100644 --- a/src/ui/3rdparty/sqlite/sqlitedb.cpp +++ b/src/ui/3rdparty/sqlite/sqlitedb.cpp @@ -117,8 +117,7 @@ QVariant SqliteDb::executeEx(const char *sql, SqliteStmt stmt; bool success = false; - if (stmt.prepare(db(), sql) - && stmt.bindVars(vars)) { + if (prepare(stmt, sql, vars)) { const auto stepRes = stmt.step(); success = (stepRes != SqliteStmt::StepError); @@ -140,6 +139,13 @@ QVariant SqliteDb::executeEx(const char *sql, : (listSize == 1 ? list.at(0) : list); } +bool SqliteDb::prepare(SqliteStmt &stmt, const char *sql, + const QVariantList &vars) +{ + return stmt.prepare(db(), sql) + && stmt.bindVars(vars); +} + qint64 SqliteDb::lastInsertRowid() const { return sqlite3_last_insert_rowid(m_db); diff --git a/src/ui/3rdparty/sqlite/sqlitedb.h b/src/ui/3rdparty/sqlite/sqlitedb.h index 0c845331..46e91961 100644 --- a/src/ui/3rdparty/sqlite/sqlitedb.h +++ b/src/ui/3rdparty/sqlite/sqlitedb.h @@ -6,9 +6,11 @@ #include "../../util/classhelpers.h" -QT_FORWARD_DECLARE_CLASS(SqliteDb) QT_FORWARD_DECLARE_STRUCT(sqlite3) +QT_FORWARD_DECLARE_CLASS(SqliteDb) +QT_FORWARD_DECLARE_CLASS(SqliteStmt) + using SQLITEDB_MIGRATE_FUNC = bool (*)(SqliteDb *db, int version, void *context); class SqliteDb @@ -38,6 +40,9 @@ public: int resultCount = 1, bool *ok = nullptr); + bool prepare(SqliteStmt &stmt, const char *sql, + const QVariantList &vars = QVariantList()); + qint64 lastInsertRowid() const; int changes() const; diff --git a/src/ui/conf/confmanager.cpp b/src/ui/conf/confmanager.cpp index 91cc9880..87740425 100644 --- a/src/ui/conf/confmanager.cpp +++ b/src/ui/conf/confmanager.cpp @@ -7,6 +7,7 @@ #include "../driver/drivermanager.h" #include "../fortcommon.h" +#include "../fortmanager.h" #include "../fortsettings.h" #include "../task/taskinfo.h" #include "../util/dateutil.h" @@ -50,18 +51,6 @@ const char * const sqlSelectAppGroups = " ORDER BY order_index;" ; -const char * const sqlSelectAppGroupIdByIndex = - "SELECT app_group_id" - " FROM app_group" - " WHERE order_index = ?1;" - ; - -const char * const sqlSelectAppGroupNames = - "SELECT name" - " FROM app_group" - " ORDER BY order_index;" - ; - const char * const sqlInsertAddressGroup = "INSERT INTO address_group(addr_group_id, order_index," " include_all, exclude_all, include_text, exclude_text)" @@ -138,7 +127,7 @@ const char * const sqlSelectEndAppsCount = ; const char * const sqlSelectEndedApps = - "SELECT t.app_id," + "SELECT t.app_id, t.app_group_id," " g.order_index as group_index," " t.path, t.use_group_perm" " FROM app t" @@ -181,15 +170,12 @@ const char * const sqlUpdateAppResetGroup = } ConfManager::ConfManager(const QString &filePath, - DriverManager *driverManager, - EnvManager *envManager, - FortSettings *fortSettings, + FortManager *fortManager, QObject *parent) : QObject(parent), - m_driverManager(driverManager), - m_envManager(envManager), - m_fortSettings(fortSettings), - m_sqliteDb(new SqliteDb(filePath)) + m_fortManager(fortManager), + m_sqliteDb(new SqliteDb(filePath)), + m_conf(new FirewallConf(this)) { m_appEndTimer.setInterval(5 * 60 * 1000); // 5 minutes connect(&m_appEndTimer, &QTimer::timeout, this, &ConfManager::checkAppEndTimes); @@ -200,6 +186,21 @@ ConfManager::~ConfManager() delete m_sqliteDb; } +DriverManager *ConfManager::driverManager() const +{ + return fortManager()->driverManager(); +} + +EnvManager *ConfManager::envManager() const +{ + return fortManager()->envManager(); +} + +FortSettings *ConfManager::settings() const +{ + return fortManager()->settings(); +} + void ConfManager::setErrorMessage(const QString &errorMessage) { if (m_errorMessage != errorMessage) { @@ -231,8 +232,23 @@ bool ConfManager::initialize() return true; } +void ConfManager::setConfToEdit(FirewallConf *conf) +{ + if (m_confToEdit == conf) + return; + + if (m_confToEdit != nullptr + && m_confToEdit != m_conf) { + m_confToEdit->deleteLater(); + } + + m_confToEdit = conf; + + emit isEditingChanged(); +} + FirewallConf *ConfManager::cloneConf(const FirewallConf &conf, - QObject *parent) + QObject *parent) const { auto newConf = new FirewallConf(parent); @@ -260,21 +276,32 @@ bool ConfManager::load(FirewallConf &conf) setupDefault(conf); } - m_fortSettings->readConfIni(conf); + settings()->readConfIni(conf); return true; } -bool ConfManager::save(const FirewallConf &conf, bool onlyFlags) +bool ConfManager::save(FirewallConf &newConf, bool onlyFlags) { - if (!onlyFlags && !saveToDb(conf)) + if (!onlyFlags && !saveToDb(newConf)) return false; - if (!m_fortSettings->writeConfIni(conf)) { - setErrorMessage(m_fortSettings->errorMessage()); + if (!settings()->writeConfIni(newConf)) { + setErrorMessage(settings()->errorMessage()); return false; } + if (m_conf != &newConf) { + m_conf->deleteLater(); + m_conf = &newConf; + + if (m_confToEdit == m_conf) { + setConfToEdit(nullptr); + } + } + + emit confSaved(onlyFlags); + return true; } @@ -309,63 +336,8 @@ bool ConfManager::saveTasks(const QList &taskInfos) return ok; } -int ConfManager::appCount(const QString &sql) -{ - SqliteStmt stmt; - if (!stmt.prepare(m_sqliteDb->db(), sql.toLatin1()) - || stmt.step() != SqliteStmt::StepRow) - return 0; - - return stmt.columnInt(0); -} - -bool ConfManager::getAppByIndex(bool &useGroupPerm, bool &blocked, bool &alerted, - qint64 &appId, int &groupIndex, QString &appPath, - QDateTime &endTime, QDateTime &creatTime, - const QString &sql, const QVariantList &vars) -{ - SqliteStmt stmt; - if (!stmt.prepare(m_sqliteDb->db(), sql.toLatin1()) - || !stmt.bindVars(vars)) - return false; - - if (stmt.step() != SqliteStmt::StepRow) - return false; - - appId = stmt.columnInt64(0); - groupIndex = stmt.columnInt(1); - appPath = stmt.columnText(2); - useGroupPerm = stmt.columnBool(3); - blocked = stmt.columnBool(4); - alerted = stmt.columnBool(5); - endTime = stmt.columnDateTime(6); - creatTime = stmt.columnDateTime(7); - - return true; -} - -qint64 ConfManager::appGroupIdByIndex(int index) -{ - return m_sqliteDb->executeEx(sqlSelectAppGroupIdByIndex, {index}).toLongLong(); -} - -QStringList ConfManager::appGroupNames() -{ - QStringList list; - - SqliteStmt stmt; - if (stmt.prepare(m_sqliteDb->db(), sqlSelectAppGroupNames)) { - while (stmt.step() == SqliteStmt::StepRow) { - const auto name = stmt.columnText(); - list.append(name); - } - } - - return list; -} - bool ConfManager::addApp(const QString &appPath, const QDateTime &endTime, - int groupIndex, bool useGroupPerm, + qint64 groupId, bool useGroupPerm, bool blocked, bool alerted) { bool ok = false; @@ -373,7 +345,7 @@ bool ConfManager::addApp(const QString &appPath, const QDateTime &endTime, m_sqliteDb->beginTransaction(); const QVariantList vars = QVariantList() - << appGroupIdByIndex(groupIndex) + << groupId << appPath << useGroupPerm << blocked @@ -431,7 +403,7 @@ end: } bool ConfManager::updateApp(qint64 appId, const QDateTime &endTime, - int groupIndex, bool useGroupPerm, bool blocked) + qint64 groupId, bool useGroupPerm, bool blocked) { bool ok = false; @@ -439,7 +411,7 @@ bool ConfManager::updateApp(qint64 appId, const QDateTime &endTime, const QVariantList vars = QVariantList() << appId - << appGroupIdByIndex(groupIndex) + << groupId << useGroupPerm << blocked << (!endTime.isNull() ? endTime : QVariant()) @@ -486,12 +458,7 @@ bool ConfManager::walkApps(std::function func) int ConfManager::appEndsCount() { - SqliteStmt stmt; - if (!stmt.prepare(m_sqliteDb->db(), sqlSelectEndAppsCount) - || stmt.step() != SqliteStmt::StepRow) - return 0; - - return stmt.columnInt(0); + return sqliteDb()->executeEx(sqlSelectEndAppsCount).toInt(); } void ConfManager::updateAppEndTimes() @@ -506,12 +473,14 @@ void ConfManager::updateAppEndTimes() while (stmt.step() == SqliteStmt::StepRow) { const qint64 appId = stmt.columnInt64(0); - const int groupIndex = stmt.columnInt(1); - const QString appPath = stmt.columnText(2); - const bool useGroupPerm = stmt.columnBool(3); + const qint64 groupId = stmt.columnInt64(1); + const int groupIndex = stmt.columnInt(2); + const QString appPath = stmt.columnText(3); + const bool useGroupPerm = stmt.columnBool(4); + + if (updateApp(appId, QDateTime(), groupId, useGroupPerm, true)) { + updateDriverUpdateApp(appPath, groupIndex, useGroupPerm, true, false); - if (updateDriverUpdateApp(appPath, groupIndex, useGroupPerm, true, false) - && updateApp(appId, QDateTime(), groupIndex, useGroupPerm, true)) { isAppEndTimesUpdated = true; } } @@ -530,23 +499,23 @@ void ConfManager::checkAppEndTimes() } } -bool ConfManager::updateDriverConf(const FirewallConf &conf, bool onlyFlags) +bool ConfManager::updateDriverConf(bool onlyFlags) { return onlyFlags - ? m_driverManager->writeConfFlags(conf) - : m_driverManager->writeConf(conf, *this, *m_envManager); + ? driverManager()->writeConfFlags(*conf()) + : driverManager()->writeConf(*conf(), *this, *envManager()); } bool ConfManager::updateDriverDeleteApp(const QString &appPath) { - return m_driverManager->writeApp(appPath, 0, false, false, false, true); + return driverManager()->writeApp(appPath, 0, false, false, false, true); } bool ConfManager::updateDriverUpdateApp(const QString &appPath, int groupIndex, bool useGroupPerm, bool blocked, bool alerted) { - return m_driverManager->writeApp(appPath, groupIndex, useGroupPerm, + return driverManager()->writeApp(appPath, groupIndex, useGroupPerm, blocked, alerted, false); } @@ -708,10 +677,6 @@ bool ConfManager::saveToDb(const FirewallConf &conf) m_sqliteDb->endTransaction(ok); - if (ok) { - emit confSaved(); - } - return ok; } diff --git a/src/ui/conf/confmanager.h b/src/ui/conf/confmanager.h index 986466dd..4e41b519 100644 --- a/src/ui/conf/confmanager.h +++ b/src/ui/conf/confmanager.h @@ -10,6 +10,7 @@ QT_FORWARD_DECLARE_CLASS(DriverManager) QT_FORWARD_DECLARE_CLASS(EnvManager) QT_FORWARD_DECLARE_CLASS(FirewallConf) +QT_FORWARD_DECLARE_CLASS(FortManager) QT_FORWARD_DECLARE_CLASS(FortSettings) QT_FORWARD_DECLARE_CLASS(SqliteDb) QT_FORWARD_DECLARE_CLASS(SqliteStmt) @@ -22,38 +23,40 @@ class ConfManager : public QObject, public ConfAppsWalker public: explicit ConfManager(const QString &filePath, - DriverManager *driverManager, - EnvManager *envManager, - FortSettings *fortSettings, + FortManager *fortManager, QObject *parent = nullptr); ~ConfManager() override; CLASS_DELETE_COPY_MOVE(ConfManager) + FortManager *fortManager() const { return m_fortManager; } + DriverManager *driverManager() const; + EnvManager *envManager() const; + FortSettings *settings() const; + SqliteDb *sqliteDb() const { return m_sqliteDb; } + + bool isEditing() const { return confToEdit() != nullptr; } + + FirewallConf *conf() const { return m_conf; } + FirewallConf *confToEdit() const { return m_confToEdit; } + bool initialize(); + void setConfToEdit(FirewallConf *conf); FirewallConf *cloneConf(const FirewallConf &conf, - QObject *parent = nullptr); + QObject *parent = nullptr) const; bool load(FirewallConf &conf); - bool save(const FirewallConf &conf, bool onlyFlags = false); + bool save(FirewallConf &newConf, bool onlyFlags = false); bool loadTasks(const QList &taskInfos); bool saveTasks(const QList &taskInfos); - int appCount(const QString &sql); - bool getAppByIndex(bool &useGroupPerm, bool &blocked, bool &alerted, - qint64 &appId, int &groupIndex, QString &appPath, - QDateTime &endTime, QDateTime &creatTime, - const QString &sql, const QVariantList &vars); - qint64 appGroupIdByIndex(int index = 0); - QStringList appGroupNames(); - bool addApp(const QString &appPath, const QDateTime &endTime, - int groupIndex, bool useGroupPerm, + qint64 groupId, bool useGroupPerm, bool blocked, bool alerted); bool deleteApp(qint64 appId); bool updateApp(qint64 appId, const QDateTime &endTime, - int groupIndex, bool useGroupPerm, bool blocked); + qint64 groupId, bool useGroupPerm, bool blocked); bool walkApps(std::function func) override; @@ -61,7 +64,7 @@ public: void updateAppEndTimes(); void checkAppEndTimes(); - bool updateDriverConf(const FirewallConf &conf, bool onlyFlags = false); + bool updateDriverConf(bool onlyFlags = false); bool updateDriverDeleteApp(const QString &appPath); bool updateDriverUpdateApp(const QString &appPath, int groupIndex, bool useGroupPerm, @@ -71,7 +74,8 @@ public: signals: void errorMessageChanged(); - void confSaved(); + void isEditingChanged(); + void confSaved(bool onlyFlags); void appEndTimesUpdated(); private: @@ -88,11 +92,12 @@ private: private: QString m_errorMessage; - DriverManager *m_driverManager = nullptr; - EnvManager *m_envManager = nullptr; - FortSettings *m_fortSettings = nullptr; + FortManager *m_fortManager = nullptr; SqliteDb *m_sqliteDb = nullptr; + FirewallConf *m_conf = nullptr; + FirewallConf *m_confToEdit = nullptr; + QTimer m_appEndTimer; }; diff --git a/src/ui/form/prog/programscontroller.cpp b/src/ui/form/prog/programscontroller.cpp index 9b795111..f5f57dea 100644 --- a/src/ui/form/prog/programscontroller.cpp +++ b/src/ui/form/prog/programscontroller.cpp @@ -1,5 +1,6 @@ #include "programscontroller.h" +#include "../../conf/confmanager.h" #include "../../fortmanager.h" #include "../../log/logmanager.h" #include "../../translationmanager.h" @@ -18,9 +19,14 @@ FortSettings *ProgramsController::settings() const return fortManager()->settings(); } +ConfManager *ProgramsController::confManager() const +{ + return fortManager()->confManager(); +} + FirewallConf *ProgramsController::conf() const { - return fortManager()->conf(); + return confManager()->conf(); } AppListModel *ProgramsController::appListModel() const diff --git a/src/ui/form/prog/programscontroller.h b/src/ui/form/prog/programscontroller.h index b025a077..67e88244 100644 --- a/src/ui/form/prog/programscontroller.h +++ b/src/ui/form/prog/programscontroller.h @@ -4,6 +4,7 @@ #include QT_FORWARD_DECLARE_CLASS(AppListModel) +QT_FORWARD_DECLARE_CLASS(ConfManager) QT_FORWARD_DECLARE_CLASS(FirewallConf) QT_FORWARD_DECLARE_CLASS(FortManager) QT_FORWARD_DECLARE_CLASS(FortSettings) @@ -19,6 +20,7 @@ public: FortManager *fortManager() const { return m_fortManager; } FortSettings *settings() const; + ConfManager *confManager() const; FirewallConf *conf() const; AppListModel *appListModel() const; TranslationManager *translationManager() const; diff --git a/src/ui/form/prog/programswindow.cpp b/src/ui/form/prog/programswindow.cpp index 945639b9..0432394e 100644 --- a/src/ui/form/prog/programswindow.cpp +++ b/src/ui/form/prog/programswindow.cpp @@ -32,7 +32,7 @@ namespace { -#define APPS_HEADER_VERSION 1 +#define APPS_HEADER_VERSION 2 const ValuesList appBlockInHourValues = { 3, 1, 6, 12, 24, 24 * 7, 24 * 30 @@ -356,7 +356,9 @@ void ProgramsWindow::setupComboAppGroups() { m_comboAppGroup = new QComboBox(); - const auto refreshComboAppGroups = [&] { + const auto refreshComboAppGroups = [&](bool onlyFlags = false) { + if (onlyFlags) return; + m_comboAppGroup->clear(); m_comboAppGroup->addItems(appListModel()->appGroupNames()); m_comboAppGroup->setCurrentIndex(0); @@ -364,7 +366,7 @@ void ProgramsWindow::setupComboAppGroups() refreshComboAppGroups(); - connect(fortManager(), &FortManager::confChanged, this, refreshComboAppGroups); + connect(confManager(), &ConfManager::confSaved, this, refreshComboAppGroups); } void ProgramsWindow::setupLogBlocked() @@ -401,18 +403,16 @@ void ProgramsWindow::setupTableAppsHeader() header->setSectionResizeMode(0, QHeaderView::Interactive); header->setSectionResizeMode(1, QHeaderView::Interactive); - header->setSectionResizeMode(2, QHeaderView::Fixed); - header->setSectionResizeMode(3, QHeaderView::Interactive); + header->setSectionResizeMode(2, QHeaderView::Interactive); + header->setSectionResizeMode(3, QHeaderView::Stretch); header->setSectionResizeMode(4, QHeaderView::Stretch); - header->setSectionResizeMode(5, QHeaderView::Stretch); - header->resizeSection(0, 500); - header->resizeSection(2, 20); - header->resizeSection(3, 80); + header->resizeSection(0, 540); + header->resizeSection(2, 100); header->setSectionsClickable(true); header->setSortIndicatorShown(true); - header->setSortIndicator(5, Qt::DescendingOrder); + header->setSortIndicator(4, Qt::DescendingOrder); } void ProgramsWindow::setupAppInfoRow() @@ -493,7 +493,7 @@ void ProgramsWindow::updateAppEditForm(bool editCurrentApp) const auto appIndex = appListCurrentIndex(); if (appIndex < 0) return; - appRow = appListModel()->appRow(appIndex); + appRow = appListModel()->appRowAt(appIndex); m_formAppId = appRow.appId; } else { m_formAppId = 0; @@ -517,7 +517,7 @@ void ProgramsWindow::updateCurrentApp(bool blocked) { const int appIndex = appListCurrentIndex(); if (appIndex >= 0) { - const auto appRow = appListModel()->appRow(appIndex); + const auto appRow = appListModel()->appRowAt(appIndex); appListModel()->updateApp(appRow.appId, appRow.appPath, appRow.groupIndex, appRow.useGroupPerm, blocked); } @@ -527,7 +527,7 @@ void ProgramsWindow::deleteCurrentApp() { const int appIndex = appListCurrentIndex(); if (appIndex >= 0) { - const auto appRow = appListModel()->appRow(appIndex); + const auto appRow = appListModel()->appRowAt(appIndex); appListModel()->deleteApp(appRow.appId, appRow.appPath, appIndex); } } @@ -539,7 +539,8 @@ int ProgramsWindow::appListCurrentIndex() const QString ProgramsWindow::appListCurrentPath() const { - return appListModel()->appPathByRow(appListCurrentIndex()); + const auto appRow = appListModel()->appRowAt(appListCurrentIndex()); + return appRow.appPath; } FortManager *ProgramsWindow::fortManager() const @@ -552,6 +553,11 @@ FortSettings *ProgramsWindow::settings() const return ctrl()->settings(); } +ConfManager *ProgramsWindow::confManager() const +{ + return ctrl()->confManager(); +} + FirewallConf *ProgramsWindow::conf() const { return ctrl()->conf(); diff --git a/src/ui/form/prog/programswindow.h b/src/ui/form/prog/programswindow.h index 1b82b58d..1c58638e 100644 --- a/src/ui/form/prog/programswindow.h +++ b/src/ui/form/prog/programswindow.h @@ -14,6 +14,7 @@ QT_FORWARD_DECLARE_CLASS(QRadioButton) QT_FORWARD_DECLARE_CLASS(AppInfoCache) QT_FORWARD_DECLARE_CLASS(AppListModel) QT_FORWARD_DECLARE_CLASS(CheckSpinCombo) +QT_FORWARD_DECLARE_CLASS(ConfManager) QT_FORWARD_DECLARE_CLASS(FirewallConf) QT_FORWARD_DECLARE_CLASS(FortManager) QT_FORWARD_DECLARE_CLASS(FortSettings) @@ -68,6 +69,7 @@ private: ProgramsController *ctrl() const { return m_ctrl; } FortManager *fortManager() const; FortSettings *settings() const; + ConfManager *confManager() const; FirewallConf *conf() const; AppListModel *appListModel() const { return m_appListModel; } AppInfoCache *appInfoCache() const; diff --git a/src/ui/fortmanager.cpp b/src/ui/fortmanager.cpp index 0d96a15a..413ceac6 100644 --- a/src/ui/fortmanager.cpp +++ b/src/ui/fortmanager.cpp @@ -53,15 +53,12 @@ FortManager::FortManager(FortSettings *fortSettings, m_optWindowState(new WidgetWindowStateWatcher(this)), m_graphWindowState(new WidgetWindowStateWatcher(this)), m_settings(fortSettings), - m_conf(new FirewallConf(this)), m_quotaManager(new QuotaManager(fortSettings, this)), m_statManager(new StatManager(fortSettings->statFilePath(), m_quotaManager, this)), m_driverManager(new DriverManager(this)), m_envManager(new EnvManager(this)), - m_confManager(new ConfManager(fortSettings->confFilePath(), - m_driverManager, m_envManager, - fortSettings, this)), + m_confManager(new ConfManager(fortSettings->confFilePath(), this, this)), m_logManager(new LogManager(m_confManager, m_statManager, m_driverManager->driverWorker(), this)), m_nativeEventFilter(new NativeEventFilter(this)), @@ -97,6 +94,16 @@ FortManager::~FortManager() closeLogManager(); } +FirewallConf *FortManager::conf() const +{ + return confManager()->conf(); +} + +FirewallConf *FortManager::confToEdit() const +{ + return confManager()->confToEdit(); +} + void FortManager::setupThreadPool() { QThreadPool::globalInstance()->setMaxThreadCount( @@ -123,10 +130,10 @@ void FortManager::removeDriver() bool FortManager::setupDriver() { - bool opened = m_driverManager->openDevice(); + bool opened = driverManager()->openDevice(); - if (!m_driverManager->validate()) { - m_driverManager->closeDevice(); + if (!driverManager()->validate()) { + driverManager()->closeDevice(); opened = false; } @@ -139,28 +146,28 @@ void FortManager::closeDriver() updateLogManager(false); updateStatManager(nullptr); - m_driverManager->closeDevice(); + driverManager()->closeDevice(); } void FortManager::setupLogManager() { - m_logManager->appListModel()->setAppInfoCache(m_appInfoCache); - m_logManager->appStatModel()->setAppInfoCache(m_appInfoCache); + logManager()->appListModel()->setAppInfoCache(m_appInfoCache); + logManager()->appStatModel()->setAppInfoCache(m_appInfoCache); - m_logManager->initialize(); + logManager()->initialize(); } void FortManager::closeLogManager() { - m_logManager->close(); + logManager()->close(); } void FortManager::setupEnvManager() { connect(m_nativeEventFilter, &NativeEventFilter::environmentChanged, - m_envManager, &EnvManager::onEnvironmentChanged); + envManager(), &EnvManager::onEnvironmentChanged); - connect(m_envManager, &EnvManager::environmentUpdated, this, [&] { + connect(envManager(), &EnvManager::environmentUpdated, this, [&] { updateDriverConf(); }); } @@ -175,7 +182,10 @@ void FortManager::setupStatManager() void FortManager::setupConfManager() { - m_confManager->initialize(); + confManager()->initialize(); + + connect(confManager(), &ConfManager::isEditingChanged, + this, &FortManager::updateTrayMenu); } void FortManager::setupLogger() @@ -190,7 +200,7 @@ void FortManager::setupLogger() void FortManager::setupTaskManager() { - m_taskManager->loadSettings(); + taskManager()->loadSettings(); } void FortManager::setupTranslationManager() @@ -343,8 +353,8 @@ void FortManager::showOptionsWindow() return; if (!m_optWindow) { - auto newConf = m_confManager->cloneConf(*m_conf, this); - setConfToEdit(newConf); + auto newConf = confManager()->cloneConf(*conf(), this); + confManager()->setConfToEdit(newConf); setupOptionsWindow(); } @@ -368,7 +378,7 @@ void FortManager::closeOptionsWindow() m_optWindow->deleteLater(); m_optWindow = nullptr; - setConfToEdit(nullptr); + confManager()->setConfToEdit(nullptr); } void FortManager::showGraphWindow() @@ -471,7 +481,7 @@ bool FortManager::showQuestionBox(const QString &text, bool FortManager::saveOriginConf(const QString &message) { - if (!saveSettings(m_conf)) + if (!saveSettings(conf())) return false; closeOptionsWindow(); @@ -481,41 +491,25 @@ bool FortManager::saveOriginConf(const QString &message) bool FortManager::saveConf(bool onlyFlags) { - return saveSettings(m_confToEdit, onlyFlags); + return saveSettings(confToEdit(), onlyFlags); } bool FortManager::applyConf(bool onlyFlags) { - Q_ASSERT(m_confToEdit != nullptr); + Q_ASSERT(confToEdit() != nullptr); - auto newConf = m_confManager->cloneConf(*m_confToEdit, this); + auto newConf = confManager()->cloneConf(*confToEdit(), this); return saveSettings(newConf, onlyFlags); } bool FortManager::applyConfImmediateFlags() { - if (m_confToEdit != nullptr) { - m_conf->copyImmediateFlags(*m_confToEdit); + if (confToEdit() != nullptr) { + conf()->copyImmediateFlags(*confToEdit()); } - return saveSettings(m_conf, true, true); -} - -void FortManager::setConfToEdit(FirewallConf *conf) -{ - if (m_confToEdit == conf) - return; - - if (m_confToEdit != nullptr - && m_confToEdit != m_conf) { - m_confToEdit->deleteLater(); - } - - m_confToEdit = conf; - emit confToEditChanged(); - - updateTrayMenu(); + return saveSettings(conf(), true, true); } bool FortManager::loadSettings() @@ -527,8 +521,8 @@ bool FortManager::loadSettings() abort(); // Abort the program } - if (!m_confManager->load(*m_conf)) { - showErrorBox("Load Settings: " + m_confManager->errorMessage()); + if (!confManager()->load(*conf())) { + showErrorBox("Load Settings: " + confManager()->errorMessage()); return false; } @@ -538,19 +532,12 @@ bool FortManager::loadSettings() bool FortManager::saveSettings(FirewallConf *newConf, bool onlyFlags, bool immediateFlags) { - if (!m_confManager->save(*newConf, onlyFlags)) { - showErrorBox("Save Settings: " + m_confManager->errorMessage()); + if (!confManager()->save(*newConf, onlyFlags)) { + showErrorBox("Save Settings: " + confManager()->errorMessage()); return false; } - if (m_conf != newConf) { - m_conf->deleteLater(); - m_conf = newConf; - emit confChanged(); - } - if (!immediateFlags) { - updateLogger(); updateTrayMenu(); } @@ -561,8 +548,8 @@ bool FortManager::updateDriverConf(bool onlyFlags) { updateLogManager(false); - if (m_confManager->updateDriverConf(*m_conf, onlyFlags)) { - updateStatManager(m_conf); + if (confManager()->updateDriverConf(onlyFlags)) { + updateStatManager(conf()); updateLogManager(true); return true; } else { @@ -573,7 +560,7 @@ bool FortManager::updateDriverConf(bool onlyFlags) void FortManager::updateLogManager(bool active) { - m_logManager->setActive(active); + logManager()->setActive(active); } void FortManager::updateStatManager(FirewallConf *conf) @@ -583,20 +570,18 @@ void FortManager::updateStatManager(FirewallConf *conf) void FortManager::saveTrayFlags() { - m_conf->setFilterEnabled(m_filterEnabledAction->isChecked()); - m_conf->setStopTraffic(m_stopTrafficAction->isChecked()); - m_conf->setStopInetTraffic(m_stopInetTrafficAction->isChecked()); + conf()->setFilterEnabled(m_filterEnabledAction->isChecked()); + conf()->setStopTraffic(m_stopTrafficAction->isChecked()); + conf()->setStopInetTraffic(m_stopInetTrafficAction->isChecked()); int i = 0; - for (AppGroup *appGroup : m_conf->appGroups()) { + for (AppGroup *appGroup : conf()->appGroups()) { const QAction *action = m_appGroupActions.at(i); appGroup->setEnabled(action->isChecked()); ++i; } - settings()->writeConfIni(*m_conf); - - updateDriverConf(true); + saveSettings(conf(), true, true); } void FortManager::saveProgWindowState() @@ -657,7 +642,7 @@ void FortManager::updateLogger() void FortManager::updateTrayMenu() { - const FirewallConf &conf = *m_conf; + const FirewallConf &conf = *this->conf(); const bool hotKeyEnabled = settings()->hotKeyEnabled(); QMenu *menu = m_trayIcon->contextMenu(); @@ -684,7 +669,7 @@ void FortManager::updateTrayMenu() addHotKey(m_graphWindowAction, settings()->hotKeyGraph(), conf.logStat()); - if (!settings()->hasPassword() && !m_confToEdit) { + if (!settings()->hasPassword() && !confToEdit()) { menu->addSeparator(); m_filterEnabledAction = addAction( diff --git a/src/ui/fortmanager.h b/src/ui/fortmanager.h index 1fe20707..6df16fa1 100644 --- a/src/ui/fortmanager.h +++ b/src/ui/fortmanager.h @@ -28,11 +28,6 @@ QT_FORWARD_DECLARE_CLASS(WidgetWindowStateWatcher) class FortManager : public QObject { Q_OBJECT - Q_PROPERTY(FortSettings *settings READ settings CONSTANT) - Q_PROPERTY(FirewallConf *conf READ conf NOTIFY confChanged) - Q_PROPERTY(FirewallConf *confToEdit READ confToEdit NOTIFY confToEditChanged) - Q_PROPERTY(LogManager *logManager READ logManager CONSTANT) - Q_PROPERTY(TaskManager *taskManager READ taskManager CONSTANT) public: explicit FortManager(FortSettings *settings, @@ -40,19 +35,17 @@ public: ~FortManager() override; CLASS_DELETE_COPY_MOVE(FortManager) - FortSettings *settings() const { return m_settings; } - FirewallConf *conf() const { return m_conf; } - FirewallConf *confToEdit() const { return m_confToEdit; } + FirewallConf *conf() const; + FirewallConf *confToEdit() const; + FortSettings *settings() const { return m_settings; } ConfManager *confManager() const { return m_confManager; } DriverManager *driverManager() const { return m_driverManager; } + EnvManager *envManager() const { return m_envManager; } LogManager *logManager() const { return m_logManager; } TaskManager *taskManager() const { return m_taskManager; } signals: - void confChanged(); - void confToEditChanged(); - void afterSaveProgWindowState(); void afterRestoreProgWindowState(); @@ -100,8 +93,6 @@ private slots: void saveTrayFlags(); private: - void setConfToEdit(FirewallConf *conf); - void setupThreadPool(); bool setupDriver(); @@ -175,16 +166,13 @@ private: GraphWindow *m_graphWindow = nullptr; WidgetWindowStateWatcher *m_graphWindowState = nullptr; - FortSettings *m_settings = nullptr; - FirewallConf *m_conf = nullptr; - FirewallConf *m_confToEdit = nullptr; - QAction *m_graphWindowAction = nullptr; QAction *m_filterEnabledAction = nullptr; QAction *m_stopTrafficAction = nullptr; QAction *m_stopInetTrafficAction = nullptr; QList m_appGroupActions; + FortSettings *m_settings = nullptr; QuotaManager *m_quotaManager = nullptr; StatManager *m_statManager = nullptr; DriverManager *m_driverManager = nullptr; diff --git a/src/ui/log/model/applistmodel.cpp b/src/ui/log/model/applistmodel.cpp index 86828f37..d1e4b555 100644 --- a/src/ui/log/model/applistmodel.cpp +++ b/src/ui/log/model/applistmodel.cpp @@ -2,9 +2,15 @@ #include +#include +#include + +#include "../../conf/appgroup.h" #include "../../conf/confmanager.h" +#include "../../conf/firewallconf.h" #include "../../util/app/appinfocache.h" #include "../../util/fileutil.h" +#include "../../util/guiutil.h" #include "../../util/net/netutil.h" #include "../logentryblocked.h" @@ -15,7 +21,16 @@ AppListModel::AppListModel(ConfManager *confManager, TableItemModel(parent), m_confManager(confManager) { - connect(m_confManager, &ConfManager::appEndTimesUpdated, this, &AppListModel::refresh); +} + +FirewallConf *AppListModel::conf() const +{ + return confManager()->conf(); +} + +SqliteDb *AppListModel::sqliteDb() const +{ + return confManager()->sqliteDb(); } void AppListModel::setAppInfoCache(AppInfoCache *v) @@ -27,14 +42,8 @@ void AppListModel::setAppInfoCache(AppInfoCache *v) void AppListModel::initialize() { - if (appGroupNames().isEmpty()) { - updateAppGroupNames(); - } - - connect(m_confManager, &ConfManager::confSaved, this, [&] { - updateAppGroupNames(); - reset(); - }); + connect(confManager(), &ConfManager::confSaved, this, &AppListModel::reset); + connect(confManager(), &ConfManager::appEndTimesUpdated, this, &AppListModel::refresh); } void AppListModel::addLogEntry(const LogEntryBlocked &logEntry) @@ -47,7 +56,10 @@ void AppListModel::addLogEntry(const LogEntryBlocked &logEntry) + ':' + QString::number(logEntry.port()); #endif - if (confManager()->addApp(appPath, QDateTime(), 0, true, true, true)) { + const auto groupId = appGroupAt(0)->id(); + + if (confManager()->addApp(appPath, QDateTime(), + groupId, true, true, true)) { reset(); } } @@ -57,7 +69,7 @@ int AppListModel::rowCount(const QModelIndex &parent) const Q_UNUSED(parent) if (m_appCount < 0) { - m_appCount = confManager()->appCount(sqlCount()); + m_appCount = sqliteDb()->executeEx(sqlCount().toLatin1()).toInt(); } return m_appCount; @@ -65,7 +77,7 @@ int AppListModel::rowCount(const QModelIndex &parent) const int AppListModel::columnCount(const QModelIndex &parent) const { - return parent.isValid() ? 0 : 6; + return parent.isValid() ? 0 : 5; } QVariant AppListModel::headerData(int section, Qt::Orientation orientation, @@ -77,10 +89,9 @@ QVariant AppListModel::headerData(int section, Qt::Orientation orientation, switch (section) { case 0: return tr("Program"); case 1: return tr("Group"); - case 2: return tr("Gr."); - case 3: return tr("State"); - case 4: return tr("End Time"); - case 5: return tr("Creation Time"); + case 2: return tr("State"); + case 3: return tr("End Time"); + case 4: return tr("Creation Time"); } break; } @@ -101,22 +112,22 @@ QVariant AppListModel::data(const QModelIndex &index, int role) const const int row = index.row(); const int column = index.column(); - updateRowCache(row); + const auto appRow = appRowAt(row); switch (column) { case 0: { - const auto appInfo = appInfoCache()->appInfo(m_rowCache.appPath); + const auto appInfo = appInfoCache()->appInfo(appRow.appPath); if (!appInfo.fileDescription.isEmpty()) { return appInfo.fileDescription; } - return FileUtil::fileName(m_rowCache.appPath); + return FileUtil::fileName(appRow.appPath); } - case 1: return appGroupNameByIndex(m_rowCache.groupIndex); - case 3: return appStateToString(m_rowCache.state); - case 4: return m_rowCache.endTime.isValid() - ? m_rowCache.endTime : QVariant(); - case 5: return m_rowCache.creatTime; + case 1: return appGroupAt(appRow.groupIndex)->name(); + case 2: return appStateToString(appRow.state); + case 3: return appRow.endTime.isValid() + ? appRow.endTime : QVariant(); + case 4: return appRow.creatTime; } break; @@ -127,11 +138,11 @@ QVariant AppListModel::data(const QModelIndex &index, int role) const const int row = index.row(); const int column = index.column(); - updateRowCache(row); + const auto appRow = appRowAt(row); switch (column) { case 0: { - const auto appPath = m_rowCache.appPath; + const auto appPath = appRow.appPath; const auto appInfo = appInfoCache()->appInfo(appPath); const auto appIcon = appInfoCache()->appIcon(appInfo); if (!appIcon.isNull()) { @@ -140,14 +151,12 @@ QVariant AppListModel::data(const QModelIndex &index, int role) const return QIcon(":/images/application-window-96.png"); } - case 3: { - switch (m_rowCache.state) { - case AppAlert: - return QIcon(":/images/error.png"); - case AppBlock: - return QIcon(":/images/stop.png"); - case AppAllow: - return QIcon(":/images/arrow_switch.png"); + case 2: { + QString iconPath; + switch (appRow.state) { + case AppAlert: return QIcon(":/images/error.png"); + case AppBlock: return QIcon(":/images/stop.png"); + case AppAllow: return QIcon(":/images/arrow_switch.png"); } } } @@ -159,27 +168,33 @@ QVariant AppListModel::data(const QModelIndex &index, int role) const case Qt::TextAlignmentRole: { const int column = index.column(); - if (column >= 1 && column <= 3) { + if (column >= 1 && column <= 2) { return int(Qt::AlignHCenter | Qt::AlignVCenter); } break; } - - // Check State - case Qt::CheckStateRole: { - if (index.column() == 2) { - updateRowCache(index.row()); - return m_rowCache.useGroupPerm; - } - - break; - } } return QVariant(); } +Qt::ItemFlags AppListModel::flags(const QModelIndex &index) const +{ + auto flags = TableItemModel::flags(index); + + if ((flags & Qt::ItemIsEnabled) + && index.column() == 2) { + const auto appRow = appRowAt(index.row()); + if (appRow.useGroupPerm + && !appGroupAt(appRow.groupIndex)->enabled()) { + flags ^= Qt::ItemIsEnabled; + } + } + + return flags; +} + void AppListModel::sort(int column, Qt::SortOrder order) { if (m_sortColumn != column || m_sortOrder != order) { @@ -190,25 +205,20 @@ void AppListModel::sort(int column, Qt::SortOrder order) } } -QString AppListModel::appPathByRow(int row) const +const AppRow &AppListModel::appRowAt(int row) const { updateRowCache(row); - return m_rowCache.appPath; -} - -AppRow AppListModel::appRow(int row) const -{ - updateRowCache(row); - - return m_rowCache; + return m_appRow; } bool AppListModel::addApp(const QString &appPath, int groupIndex, bool useGroupPerm, bool blocked, const QDateTime &endTime) { - if (confManager()->addApp(appPath, endTime, groupIndex, + const auto groupId = appGroupAt(groupIndex)->id(); + + if (confManager()->addApp(appPath, endTime, groupId, useGroupPerm, blocked, false)) { reset(); @@ -222,7 +232,9 @@ bool AppListModel::updateApp(qint64 appId, const QString &appPath, int groupIndex, bool useGroupPerm, bool blocked, const QDateTime &endTime) { - if (confManager()->updateApp(appId, endTime, groupIndex, + const auto groupId = appGroupAt(groupIndex)->id(); + + if (confManager()->updateApp(appId, endTime, groupId, useGroupPerm, blocked)) { refresh(); @@ -261,24 +273,30 @@ void AppListModel::refresh() void AppListModel::invalidateRowCache() { m_appCount = -1; - m_rowCache.invalidate(); + m_appRow.invalidate(); } void AppListModel::updateRowCache(int row) const { - if (m_rowCache.isValid(row)) + if (m_appRow.isValid(row)) return; - bool blocked = false; - bool alerted = false; + SqliteStmt stmt; + if (!(sqliteDb()->prepare(stmt, sql().toLatin1(), {row}) + && stmt.step() == SqliteStmt::StepRow)) + return; - if (m_confManager->getAppByIndex(m_rowCache.useGroupPerm, blocked, alerted, - m_rowCache.appId, m_rowCache.groupIndex, - m_rowCache.appPath, m_rowCache.endTime, - m_rowCache.creatTime, sql(), {row})) { - m_rowCache.state = alerted ? AppAlert : (blocked ? AppBlock : AppAllow); - m_rowCache.row = row; - } + m_appRow.appId = stmt.columnInt64(0); + m_appRow.groupIndex = stmt.columnInt(1); + m_appRow.appPath = stmt.columnText(2); + m_appRow.useGroupPerm = stmt.columnBool(3); + const bool blocked = stmt.columnBool(4); + const bool alerted = stmt.columnBool(5); + m_appRow.endTime = stmt.columnDateTime(6); + m_appRow.creatTime = stmt.columnDateTime(7); + + m_appRow.state = alerted ? AppAlert : (blocked ? AppBlock : AppAllow); + m_appRow.row = row; } QString AppListModel::sqlCount() const @@ -297,7 +315,8 @@ QString AppListModel::sqlBase() const return "SELECT t.app_id," " g.order_index as group_index," - " t.path, t.use_group_perm, t.blocked," + " t.path, t.use_group_perm," + " t.blocked," " (alert.app_id IS NOT NULL) as alerted," " t.end_time, t.creat_time" " FROM app t" @@ -324,17 +343,23 @@ QString AppListModel::sqlOrder() const return QString(" ORDER BY %1 %2").arg(columnsStr, orderStr); } -void AppListModel::updateAppGroupNames() +const AppGroup *AppListModel::appGroupAt(int index) const { - m_appGroupNames = confManager()->appGroupNames(); + const auto appGroups = conf()->appGroups(); + if (index < 0 || index >= appGroups.size()) { + static const AppGroup g_nullAppGroup; + return &g_nullAppGroup; + } + return appGroups.at(index); } -QString AppListModel::appGroupNameByIndex(int groupIndex) const +QStringList AppListModel::appGroupNames() const { - if (groupIndex < 0 || groupIndex >= m_appGroupNames.size()) - return QString(); - - return m_appGroupNames.at(groupIndex); + QStringList list; + for (const auto &appGroup : conf()->appGroups()) { + list.append(appGroup->name()); + } + return list; } QString AppListModel::appStateToString(AppState state) const diff --git a/src/ui/log/model/applistmodel.h b/src/ui/log/model/applistmodel.h index 39ecb8fb..627a5a12 100644 --- a/src/ui/log/model/applistmodel.h +++ b/src/ui/log/model/applistmodel.h @@ -5,9 +5,12 @@ #include "../util/model/tableitemmodel.h" +QT_FORWARD_DECLARE_CLASS(AppGroup) QT_FORWARD_DECLARE_CLASS(AppInfoCache) QT_FORWARD_DECLARE_CLASS(ConfManager) +QT_FORWARD_DECLARE_CLASS(FirewallConf) QT_FORWARD_DECLARE_CLASS(LogEntryBlocked) +QT_FORWARD_DECLARE_CLASS(SqliteDb) enum AppState { AppAlert = 0, @@ -46,6 +49,8 @@ public: QObject *parent = nullptr); ConfManager *confManager() const { return m_confManager; } + FirewallConf *conf() const; + SqliteDb *sqliteDb() const; AppInfoCache *appInfoCache() const { return m_appInfoCache; } void setAppInfoCache(AppInfoCache *v); @@ -61,10 +66,11 @@ public: int role = Qt::DisplayRole) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; + Qt::ItemFlags flags(const QModelIndex &index) const override; + void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override; - QString appPathByRow(int row) const; - AppRow appRow(int row) const; + const AppRow &appRowAt(int row) const; bool addApp(const QString &appPath, int groupIndex, bool useGroupPerm, bool blocked, @@ -74,8 +80,8 @@ public: const QDateTime &endTime = QDateTime()); void deleteApp(qint64 appId, const QString &appPath, int row); - QStringList appGroupNames() const { return m_appGroupNames; } - QString appGroupNameByIndex(int groupIndex) const; + const AppGroup *appGroupAt(int index) const; + QStringList appGroupNames() const; public slots: void reset(); @@ -90,8 +96,6 @@ private: QString sqlBase() const; QString sqlOrder() const; - void updateAppGroupNames(); - QString appStateToString(AppState state) const; private: @@ -100,12 +104,10 @@ private: mutable int m_appCount = -1; - QStringList m_appGroupNames; - ConfManager *m_confManager = nullptr; AppInfoCache *m_appInfoCache = nullptr; - mutable AppRow m_rowCache; + mutable AppRow m_appRow; }; #endif // APPLISTMODEL_H diff --git a/src/ui/util/guiutil.cpp b/src/ui/util/guiutil.cpp index d4fb936f..99d37a98 100644 --- a/src/ui/util/guiutil.cpp +++ b/src/ui/util/guiutil.cpp @@ -44,8 +44,8 @@ QIcon GuiUtil::overlayIcon(const QString &basePath, // Paint the overlay { - QPainter painter(&base); - painter.drawPixmap(rect, overlay); + QPainter p(&base); + p.drawPixmap(rect, overlay); } return base;