diff --git a/src/tests/db/Db.pro b/src/tests/db/Db.pro index 39b3f6c6..db598ab2 100644 --- a/src/tests/db/Db.pro +++ b/src/tests/db/Db.pro @@ -1,6 +1,9 @@ include(../common/Test.pri) SOURCES += \ + $$UIPATH/conf/addressgroup.cpp \ + $$UIPATH/conf/appgroup.cpp \ + $$UIPATH/conf/firewallconf.cpp \ $$UIPATH/db/databasemanager.cpp \ $$UIPATH/db/databasesql.cpp \ $$UIPATH/fortcommon.cpp \ @@ -8,6 +11,9 @@ SOURCES += \ $$UIPATH/util/fileutil.cpp HEADERS += \ + $$UIPATH/conf/addressgroup.h \ + $$UIPATH/conf/appgroup.h \ + $$UIPATH/conf/firewallconf.h \ $$UIPATH/db/databasemanager.h \ $$UIPATH/db/databasesql.h \ $$UIPATH/fortcommon.h \ diff --git a/src/ui/conf/firewallconf.cpp b/src/ui/conf/firewallconf.cpp index dd2f11f3..b211e6dd 100644 --- a/src/ui/conf/firewallconf.cpp +++ b/src/ui/conf/firewallconf.cpp @@ -12,6 +12,10 @@ FirewallConf::FirewallConf(QObject *parent) : m_logStat(false), m_appBlockAll(true), m_appAllowAll(false), + m_trafHourKeepDays(DEFAULT_TRAF_HOUR_KEEP_DAYS), + m_trafDayKeepDays(DEFAULT_TRAF_DAY_KEEP_DAYS), + m_trafMonthKeepMonths(DEFAULT_TRAF_MONTH_KEEP_MONTHS), + m_trafUnit(UnitAdaptive), m_ipInclude(new AddressGroup(this)), m_ipExclude(new AddressGroup(this)) { @@ -73,6 +77,38 @@ void FirewallConf::setAppAllowAll(bool appAllowAll) } } +void FirewallConf::setTrafHourKeepDays(int trafHourKeepDays) +{ + if (m_trafHourKeepDays != trafHourKeepDays) { + m_trafHourKeepDays = trafHourKeepDays; + emit trafHourKeepDaysChanged(); + } +} + +void FirewallConf::setTrafDayKeepDays(int trafDayKeepDays) +{ + if (m_trafDayKeepDays != trafDayKeepDays) { + m_trafDayKeepDays = trafDayKeepDays; + emit trafDayKeepDaysChanged(); + } +} + +void FirewallConf::setTrafMonthKeepMonths(int trafMonthKeepMonths) +{ + if (m_trafMonthKeepMonths != trafMonthKeepMonths) { + m_trafMonthKeepMonths = trafMonthKeepMonths; + emit trafMonthKeepMonthsChanged(); + } +} + +void FirewallConf::setTrafUnit(int trafUnit) +{ + if (m_trafUnit != trafUnit) { + m_trafUnit = static_cast(trafUnit); + emit trafUnitChanged(); + } +} + quint32 FirewallConf::appGroupBits() const { quint32 groupBits = 0; @@ -146,14 +182,15 @@ void FirewallConf::copyFlags(const FirewallConf &o) setAppAllowAll(o.appAllowAll()); setAppGroupBits(o.appGroupBits()); - copyImmediateFlags(o); + copyImmediateValues(o); } -void FirewallConf::copyImmediateFlags(const FirewallConf &o) +void FirewallConf::copyImmediateValues(const FirewallConf &o) { setResolveAddress(o.resolveAddress()); setLogBlocked(o.logBlocked()); setLogStat(o.logStat()); + setTrafUnit(o.trafUnit()); } QVariant FirewallConf::toVariant() const diff --git a/src/ui/conf/firewallconf.h b/src/ui/conf/firewallconf.h index c7cf8326..480e281a 100644 --- a/src/ui/conf/firewallconf.h +++ b/src/ui/conf/firewallconf.h @@ -8,6 +8,11 @@ QT_FORWARD_DECLARE_CLASS(AddressGroup) QT_FORWARD_DECLARE_CLASS(AppGroup) +#define DEFAULT_APP_GROUP_BITS 0xFFFF +#define DEFAULT_TRAF_HOUR_KEEP_DAYS 90 // ~3 months +#define DEFAULT_TRAF_DAY_KEEP_DAYS 356 // ~1 year +#define DEFAULT_TRAF_MONTH_KEEP_MONTHS 360 // ~3 years + class FirewallConf : public QObject { Q_OBJECT @@ -18,12 +23,26 @@ class FirewallConf : public QObject Q_PROPERTY(bool logStat READ logStat WRITE setLogStat NOTIFY logStatChanged) Q_PROPERTY(bool appBlockAll READ appBlockAll WRITE setAppBlockAll NOTIFY appBlockAllChanged) Q_PROPERTY(bool appAllowAll READ appAllowAll WRITE setAppAllowAll NOTIFY appAllowAllChanged) + Q_PROPERTY(int trafHourKeepDays READ trafHourKeepDays WRITE setTrafHourKeepDays NOTIFY trafHourKeepDaysChanged) + Q_PROPERTY(int trafDayKeepDays READ trafDayKeepDays WRITE setTrafDayKeepDays NOTIFY trafDayKeepDaysChanged) + Q_PROPERTY(int trafMonthKeepMonths READ trafMonthKeepMonths WRITE setTrafMonthKeepMonths NOTIFY trafMonthKeepMonthsChanged) + Q_PROPERTY(int trafUnit READ trafUnit WRITE setTrafUnit NOTIFY trafUnitChanged) Q_PROPERTY(AddressGroup *ipInclude READ ipInclude CONSTANT) Q_PROPERTY(AddressGroup *ipExclude READ ipExclude CONSTANT) Q_PROPERTY(QQmlListProperty appGroups READ appGroups NOTIFY appGroupsChanged) Q_CLASSINFO("DefaultProperty", "appGroups") public: + enum TrafUnit { + UnitAdaptive = 0, + UnitBytes, + UnitKB, + UnitMB, + UnitGB, + UnitTB + }; + Q_ENUM(TrafUnit) + explicit FirewallConf(QObject *parent = nullptr); bool provBoot() const { return m_provBoot; } @@ -47,6 +66,18 @@ public: bool appAllowAll() const { return m_appAllowAll; } void setAppAllowAll(bool appAllowAll); + int trafHourKeepDays() const { return m_trafHourKeepDays; } + void setTrafHourKeepDays(int trafHourKeepDays); + + int trafDayKeepDays() const { return m_trafDayKeepDays; } + void setTrafDayKeepDays(int trafDayKeepDays); + + int trafMonthKeepMonths() const { return m_trafMonthKeepMonths; } + void setTrafMonthKeepMonths(int trafMonthKeepMonths); + + int trafUnit() const { return m_trafUnit; } + void setTrafUnit(int trafUnit); + quint32 appGroupBits() const; void setAppGroupBits(quint32 groupBits); @@ -57,7 +88,7 @@ public: QQmlListProperty appGroups(); void copyFlags(const FirewallConf &o); - void copyImmediateFlags(const FirewallConf &o); + void copyImmediateValues(const FirewallConf &o); QVariant toVariant() const; void fromVariant(const QVariant &v); @@ -70,6 +101,10 @@ signals: void logStatChanged(); void appBlockAllChanged(); void appAllowAllChanged(); + void trafHourKeepDaysChanged(); + void trafDayKeepDaysChanged(); + void trafMonthKeepMonthsChanged(); + void trafUnitChanged(); void appGroupsChanged(); public slots: @@ -90,6 +125,12 @@ private: uint m_appBlockAll : 1; uint m_appAllowAll : 1; + int m_trafHourKeepDays; + int m_trafDayKeepDays; + int m_trafMonthKeepMonths; + + TrafUnit m_trafUnit; + AddressGroup *m_ipInclude; AddressGroup *m_ipExclude; diff --git a/src/ui/db/databasemanager.cpp b/src/ui/db/databasemanager.cpp index de8a0046..7aeff31e 100644 --- a/src/ui/db/databasemanager.cpp +++ b/src/ui/db/databasemanager.cpp @@ -1,5 +1,6 @@ #include "databasemanager.h" +#include "../conf/firewallconf.h" #include "../util/dateutil.h" #include "../util/fileutil.h" #include "databasesql.h" @@ -14,6 +15,7 @@ DatabaseManager::DatabaseManager(const QString &filePath, m_lastTrafDay(0), m_lastTrafMonth(0), m_filePath(filePath), + m_conf(nullptr), m_sqliteDb(new SqliteDb()) { SqliteEngine::initialize(); @@ -28,6 +30,15 @@ DatabaseManager::~DatabaseManager() SqliteEngine::shutdown(); } +void DatabaseManager::setFirewallConf(const FirewallConf *conf) +{ + m_conf = conf; + + if (m_conf && !m_conf->logStat()) { + logClear(); + } +} + bool DatabaseManager::initialize() { const bool fileExists = FileUtil::fileExists(m_filePath); @@ -40,8 +51,11 @@ bool DatabaseManager::initialize() return fileExists || createTables(); } -void DatabaseManager::logProcNew(const QString &appPath, bool &isNew) +qint64 DatabaseManager::logProcNew(const QString &appPath, bool &isNew) { + if (m_conf && !m_conf->logStat()) + return 0; + qint64 appId = getAppId(appPath); if (appId == 0) { appId = createAppId(appPath); @@ -50,6 +64,8 @@ void DatabaseManager::logProcNew(const QString &appPath, bool &isNew) m_appPaths.prepend(appPath); m_appIds.prepend(appId); + + return appId; } void DatabaseManager::logStatTraf(quint16 procCount, const quint8 *procBits, @@ -57,6 +73,9 @@ void DatabaseManager::logStatTraf(quint16 procCount, const quint8 *procBits, { Q_ASSERT(procCount == m_appIds.size()); + if (m_conf && !m_conf->logStat()) + return; + QVector delProcIndexes; const qint64 unixTime = DateUtil::getUnixTime(); @@ -78,28 +97,28 @@ void DatabaseManager::logStatTraf(quint16 procCount, const quint8 *procBits, m_sqliteDb->beginTransaction(); // Insert Statemets - QStmtList insertTrafAppStmts = QStmtList() - << getTrafficStmt(DatabaseSql::sqlInsertTrafficAppHour, trafHour) - << getTrafficStmt(DatabaseSql::sqlInsertTrafficAppDay, trafDay) - << getTrafficStmt(DatabaseSql::sqlInsertTrafficAppMonth, trafMonth) - << getTrafficStmt(DatabaseSql::sqlUpdateTrafficAppTotal, -1); + const QStmtList insertTrafAppStmts = QStmtList() + << getTrafficStmt(DatabaseSql::sqlInsertTrafAppHour, trafHour) + << getTrafficStmt(DatabaseSql::sqlInsertTrafAppDay, trafDay) + << getTrafficStmt(DatabaseSql::sqlInsertTrafAppMonth, trafMonth) + << getTrafficStmt(DatabaseSql::sqlUpdateTrafAppTotal, -1); - QStmtList insertTrafStmts = QStmtList() - << getTrafficStmt(DatabaseSql::sqlInsertTrafficHour, trafHour) - << getTrafficStmt(DatabaseSql::sqlInsertTrafficDay, trafDay) - << getTrafficStmt(DatabaseSql::sqlInsertTrafficMonth, trafMonth); + const QStmtList insertTrafStmts = QStmtList() + << getTrafficStmt(DatabaseSql::sqlInsertTrafHour, trafHour) + << getTrafficStmt(DatabaseSql::sqlInsertTrafDay, trafDay) + << getTrafficStmt(DatabaseSql::sqlInsertTrafMonth, trafMonth); // Update Statemets - QStmtList updateTrafAppStmts = QStmtList() - << getTrafficStmt(DatabaseSql::sqlUpdateTrafficAppHour, trafHour) - << getTrafficStmt(DatabaseSql::sqlUpdateTrafficAppDay, trafDay) - << getTrafficStmt(DatabaseSql::sqlUpdateTrafficAppMonth, trafMonth) - << getTrafficStmt(DatabaseSql::sqlUpdateTrafficAppTotal, -1); + const QStmtList updateTrafAppStmts = QStmtList() + << getTrafficStmt(DatabaseSql::sqlUpdateTrafAppHour, trafHour) + << getTrafficStmt(DatabaseSql::sqlUpdateTrafAppDay, trafDay) + << getTrafficStmt(DatabaseSql::sqlUpdateTrafAppMonth, trafMonth) + << getTrafficStmt(DatabaseSql::sqlUpdateTrafAppTotal, -1); - QStmtList updateTrafStmts = QStmtList() - << getTrafficStmt(DatabaseSql::sqlUpdateTrafficHour, trafHour) - << getTrafficStmt(DatabaseSql::sqlUpdateTrafficDay, trafDay) - << getTrafficStmt(DatabaseSql::sqlUpdateTrafficMonth, trafMonth); + const QStmtList updateTrafStmts = QStmtList() + << getTrafficStmt(DatabaseSql::sqlUpdateTrafHour, trafHour) + << getTrafficStmt(DatabaseSql::sqlUpdateTrafDay, trafDay) + << getTrafficStmt(DatabaseSql::sqlUpdateTrafMonth, trafMonth); for (quint16 i = 0; i < procCount; ++i) { const bool active = procBits[i / 8] & (1 << (i & 7)); @@ -124,6 +143,30 @@ void DatabaseManager::logStatTraf(quint16 procCount, const quint8 *procBits, } } + // Delete old data + if (isNewDay) { + const qint32 oldTrafHour = trafHour + - 24 * (m_conf ? m_conf->trafHourKeepDays() + : DEFAULT_TRAF_HOUR_KEEP_DAYS); + const qint32 oldTrafDay = trafHour + - 24 * (m_conf ? m_conf->trafDayKeepDays() + : DEFAULT_TRAF_DAY_KEEP_DAYS); + const qint32 oldTrafMonth = DateUtil::addUnixMonths( + trafHour, -(m_conf ? m_conf->trafMonthKeepMonths() + : DEFAULT_TRAF_MONTH_KEEP_MONTHS)); + + // Delete Statemets + const QStmtList deleteTrafStmts = QStmtList() + << getTrafficStmt(DatabaseSql::sqlDeleteTrafAppHour, oldTrafHour) + << getTrafficStmt(DatabaseSql::sqlDeleteTrafAppDay, oldTrafDay) + << getTrafficStmt(DatabaseSql::sqlDeleteTrafAppMonth, oldTrafMonth) + << getTrafficStmt(DatabaseSql::sqlDeleteTrafHour, oldTrafHour) + << getTrafficStmt(DatabaseSql::sqlDeleteTrafDay, oldTrafDay) + << getTrafficStmt(DatabaseSql::sqlDeleteTrafMonth, oldTrafMonth); + + deleteTrafficList(deleteTrafStmts); + } + m_sqliteDb->commitTransaction(); // Delete inactive processes @@ -237,6 +280,14 @@ bool DatabaseManager::updateTraffic(SqliteStmt *stmt, quint32 inBytes, && m_sqliteDb->changes() != 0; } +void DatabaseManager::deleteTrafficList(const QStmtList &deleteStmtList) +{ + foreach (SqliteStmt *stmtDelete, deleteStmtList) { + stmtDelete->step(); + stmtDelete->reset(); + } +} + qint32 DatabaseManager::getTrafficTime(const char *sql, qint64 appId) { qint32 trafTime = 0; diff --git a/src/ui/db/databasemanager.h b/src/ui/db/databasemanager.h index e71ac320..a4f754b0 100644 --- a/src/ui/db/databasemanager.h +++ b/src/ui/db/databasemanager.h @@ -6,6 +6,7 @@ #include #include +QT_FORWARD_DECLARE_CLASS(FirewallConf) QT_FORWARD_DECLARE_CLASS(SqliteDb) QT_FORWARD_DECLARE_CLASS(SqliteStmt) @@ -18,11 +19,14 @@ public: QObject *parent = nullptr); virtual ~DatabaseManager(); - bool initialize(); + const FirewallConf *firewallConf() const { return m_conf; } + void setFirewallConf(const FirewallConf *conf); SqliteDb *sqliteDb() const { return m_sqliteDb; } - void logProcNew(const QString &appPath, bool &isNew); + bool initialize(); + + qint64 logProcNew(const QString &appPath, bool &isNew); void logStatTraf(quint16 procCount, const quint8 *procBits, const quint32 *trafBytes); void logClear(); @@ -56,6 +60,8 @@ private: bool updateTraffic(SqliteStmt *stmt, quint32 inBytes, quint32 outBytes, qint64 appId = 0); + void deleteTrafficList(const QStmtList &deleteStmtList); + SqliteStmt *getTrafficStmt(const char *sql, qint32 trafTime); SqliteStmt *getSqliteStmt(const char *sql); @@ -67,6 +73,8 @@ private: QString m_filePath; + const FirewallConf *m_conf; + SqliteDb *m_sqliteDb; QHash m_sqliteStmts; diff --git a/src/ui/db/databasesql.cpp b/src/ui/db/databasesql.cpp index 62b75399..423f71c1 100644 --- a/src/ui/db/databasesql.cpp +++ b/src/ui/db/databasesql.cpp @@ -72,79 +72,79 @@ const char * const DatabaseSql::sqlSelectAppPaths = "SELECT app_id, path FROM app ORDER BY creat_time;" ; -const char * const DatabaseSql::sqlInsertTrafficAppHour = +const char * const DatabaseSql::sqlInsertTrafAppHour = "INSERT INTO traffic_app_hour(app_id, traf_time, in_bytes, out_bytes)" " VALUES(?4, ?1, ?2, ?3);" ; -const char * const DatabaseSql::sqlInsertTrafficAppDay = +const char * const DatabaseSql::sqlInsertTrafAppDay = "INSERT INTO traffic_app_day(app_id, traf_time, in_bytes, out_bytes)" " VALUES(?4, ?1, ?2, ?3);" ; -const char * const DatabaseSql::sqlInsertTrafficAppMonth = +const char * const DatabaseSql::sqlInsertTrafAppMonth = "INSERT INTO traffic_app_month(app_id, traf_time, in_bytes, out_bytes)" " VALUES(?4, ?1, ?2, ?3);" ; -const char * const DatabaseSql::sqlInsertTrafficHour = +const char * const DatabaseSql::sqlInsertTrafHour = "INSERT INTO traffic_hour(traf_time, in_bytes, out_bytes)" " VALUES(?1, ?2, ?3);" ; -const char * const DatabaseSql::sqlInsertTrafficDay = +const char * const DatabaseSql::sqlInsertTrafDay = "INSERT INTO traffic_day(traf_time, in_bytes, out_bytes)" " VALUES(?1, ?2, ?3);" ; -const char * const DatabaseSql::sqlInsertTrafficMonth = +const char * const DatabaseSql::sqlInsertTrafMonth = "INSERT INTO traffic_month(traf_time, in_bytes, out_bytes)" " VALUES(?1, ?2, ?3);" ; -const char * const DatabaseSql::sqlUpdateTrafficAppHour = +const char * const DatabaseSql::sqlUpdateTrafAppHour = "UPDATE traffic_app_hour" " SET in_bytes = in_bytes + ?2," " out_bytes = out_bytes + ?3" " WHERE app_id = ?4 and traf_time = ?1;" ; -const char * const DatabaseSql::sqlUpdateTrafficAppDay = +const char * const DatabaseSql::sqlUpdateTrafAppDay = "UPDATE traffic_app_day" " SET in_bytes = in_bytes + ?2," " out_bytes = out_bytes + ?3" " WHERE app_id = ?4 and traf_time = ?1;" ; -const char * const DatabaseSql::sqlUpdateTrafficAppMonth = +const char * const DatabaseSql::sqlUpdateTrafAppMonth = "UPDATE traffic_app_month" " SET in_bytes = in_bytes + ?2," " out_bytes = out_bytes + ?3" " WHERE app_id = ?4 and traf_time = ?1;" ; -const char * const DatabaseSql::sqlUpdateTrafficAppTotal = +const char * const DatabaseSql::sqlUpdateTrafAppTotal = "UPDATE app" " SET in_bytes = in_bytes + ?2," " out_bytes = out_bytes + ?3" " WHERE app_id = ?4 and 0 != ?1;" ; -const char * const DatabaseSql::sqlUpdateTrafficHour = +const char * const DatabaseSql::sqlUpdateTrafHour = "UPDATE traffic_hour" " SET in_bytes = in_bytes + ?2," " out_bytes = out_bytes + ?3" " WHERE traf_time = ?1;" ; -const char * const DatabaseSql::sqlUpdateTrafficDay = +const char * const DatabaseSql::sqlUpdateTrafDay = "UPDATE traffic_day" " SET in_bytes = in_bytes + ?2," " out_bytes = out_bytes + ?3" " WHERE traf_time = ?1;" ; -const char * const DatabaseSql::sqlUpdateTrafficMonth = +const char * const DatabaseSql::sqlUpdateTrafMonth = "UPDATE traffic_month" " SET in_bytes = in_bytes + ?2," " out_bytes = out_bytes + ?3" @@ -229,3 +229,33 @@ const char * const DatabaseSql::sqlSelectTrafTotal = "SELECT sum(in_bytes), sum(out_bytes)" " FROM app WHERE 0 != ?1;" ; + +const char * const DatabaseSql::sqlDeleteTrafAppHour = + "DELETE FROM traffic_app_hour" + " WHERE traf_time < ?1" + " and app_id in (SELECT app_id FROM app);" + ; + +const char * const DatabaseSql::sqlDeleteTrafAppDay = + "DELETE FROM traffic_app_day" + " WHERE traf_time < ?1" + " and app_id in (SELECT app_id FROM app);" + ; + +const char * const DatabaseSql::sqlDeleteTrafAppMonth = + "DELETE FROM traffic_app_month" + " WHERE traf_time < ?1" + " and app_id in (SELECT app_id FROM app);" + ; + +const char * const DatabaseSql::sqlDeleteTrafHour = + "DELETE FROM traffic_hour WHERE traf_time < ?1;" + ; + +const char * const DatabaseSql::sqlDeleteTrafDay = + "DELETE FROM traffic_day WHERE traf_time < ?1;" + ; + +const char * const DatabaseSql::sqlDeleteTrafMonth = + "DELETE FROM traffic_month WHERE traf_time < ?1;" + ; diff --git a/src/ui/db/databasesql.h b/src/ui/db/databasesql.h index 51f27c26..17576e4b 100644 --- a/src/ui/db/databasesql.h +++ b/src/ui/db/databasesql.h @@ -12,23 +12,23 @@ public: static const char * const sqlSelectAppPaths; - static const char * const sqlInsertTrafficAppHour; - static const char * const sqlInsertTrafficAppDay; - static const char * const sqlInsertTrafficAppMonth; + static const char * const sqlInsertTrafAppHour; + static const char * const sqlInsertTrafAppDay; + static const char * const sqlInsertTrafAppMonth; - static const char * const sqlInsertTrafficHour; - static const char * const sqlInsertTrafficDay; - static const char * const sqlInsertTrafficMonth; + static const char * const sqlInsertTrafHour; + static const char * const sqlInsertTrafDay; + static const char * const sqlInsertTrafMonth; - static const char * const sqlUpdateTrafficAppHour; - static const char * const sqlUpdateTrafficAppDay; - static const char * const sqlUpdateTrafficAppMonth; + static const char * const sqlUpdateTrafAppHour; + static const char * const sqlUpdateTrafAppDay; + static const char * const sqlUpdateTrafAppMonth; - static const char * const sqlUpdateTrafficHour; - static const char * const sqlUpdateTrafficDay; - static const char * const sqlUpdateTrafficMonth; + static const char * const sqlUpdateTrafHour; + static const char * const sqlUpdateTrafDay; + static const char * const sqlUpdateTrafMonth; - static const char * const sqlUpdateTrafficAppTotal; + static const char * const sqlUpdateTrafAppTotal; static const char * const sqlSelectMinTrafAppHour; static const char * const sqlSelectMinTrafAppDay; @@ -49,6 +49,14 @@ public: static const char * const sqlSelectTrafDay; static const char * const sqlSelectTrafMonth; static const char * const sqlSelectTrafTotal; + + static const char * const sqlDeleteTrafAppHour; + static const char * const sqlDeleteTrafAppDay; + static const char * const sqlDeleteTrafAppMonth; + + static const char * const sqlDeleteTrafHour; + static const char * const sqlDeleteTrafDay; + static const char * const sqlDeleteTrafMonth; }; #endif // DATABASESQL_H diff --git a/src/ui/fortmanager.cpp b/src/ui/fortmanager.cpp index b9ba7105..b260304b 100644 --- a/src/ui/fortmanager.cpp +++ b/src/ui/fortmanager.cpp @@ -245,11 +245,11 @@ bool FortManager::applyConf(bool onlyFlags) return saveSettings(newConf, onlyFlags); } -bool FortManager::applyConfImmediateFlags() +bool FortManager::applyConfImmediateValues() { Q_ASSERT(m_firewallConfToEdit != nullConf()); - m_firewallConf->copyImmediateFlags(*m_firewallConfToEdit); + m_firewallConf->copyImmediateValues(*m_firewallConfToEdit); return saveSettings(m_firewallConf, true, true); } @@ -276,9 +276,9 @@ bool FortManager::loadSettings(FirewallConf *conf) } bool FortManager::saveSettings(FirewallConf *newConf, bool onlyFlags, - bool immediateFlags) + bool immediateValues) { - if (!(onlyFlags ? m_fortSettings->writeConfFlags(*newConf) + if (!(onlyFlags ? m_fortSettings->writeConfIni(*newConf) : m_fortSettings->writeConf(*newConf))) { showErrorBox("Save Settings: " + m_fortSettings->errorMessage()); return false; @@ -289,7 +289,7 @@ bool FortManager::saveSettings(FirewallConf *newConf, bool onlyFlags, m_firewallConf = newConf; } - if (!immediateFlags) { + if (!immediateValues) { updateTrayMenu(); } @@ -308,7 +308,7 @@ bool FortManager::updateDriverConf(FirewallConf *conf) return false; } - updateLogManager(conf); + updateDatabaseManager(conf); return true; } @@ -324,19 +324,14 @@ bool FortManager::updateDriverConfFlags(FirewallConf *conf) return false; } - updateLogManager(conf); + updateDatabaseManager(conf); return true; } -void FortManager::updateLogManager(FirewallConf *conf) +void FortManager::updateDatabaseManager(FirewallConf *conf) { - if (!conf->logStat()) { - m_databaseManager->logClear(); - } - - m_logManager->setLogBlockedEnabled(conf->logBlocked()); - m_logManager->setLogStatEnabled(conf->logStat()); + m_databaseManager->setFirewallConf(conf); } void FortManager::setLanguage(int language) @@ -360,7 +355,7 @@ void FortManager::saveTrayFlags() ++i; } - m_fortSettings->writeConfFlags(*m_firewallConf); + m_fortSettings->writeConfIni(*m_firewallConf); updateDriverConfFlags(m_firewallConf); } diff --git a/src/ui/fortmanager.h b/src/ui/fortmanager.h index a1deb672..8d568f5c 100644 --- a/src/ui/fortmanager.h +++ b/src/ui/fortmanager.h @@ -54,7 +54,7 @@ public slots: bool saveOriginConf(const QString &message); bool saveConf(bool onlyFlags = false); bool applyConf(bool onlyFlags = false); - bool applyConfImmediateFlags(); + bool applyConfImmediateValues(); void setLanguage(int language); @@ -78,12 +78,12 @@ private: bool loadSettings(FirewallConf *conf); bool saveSettings(FirewallConf *newConf, bool onlyFlags = false, - bool immediateFlags = false); + bool immediateValues = false); bool updateDriverConf(FirewallConf *conf); bool updateDriverConfFlags(FirewallConf *conf); - void updateLogManager(FirewallConf *conf); + void updateDatabaseManager(FirewallConf *conf); FirewallConf *cloneConf(const FirewallConf &conf); diff --git a/src/ui/fortsettings.cpp b/src/ui/fortsettings.cpp index bad9452d..656bca04 100644 --- a/src/ui/fortsettings.cpp +++ b/src/ui/fortsettings.cpp @@ -140,7 +140,7 @@ bool FortSettings::readConf(FirewallConf &conf) return (!(fileExists || backupFileExists) || (fileExists && tryToReadConf(conf, filePath)) || tryToReadConf(conf, backupFilePath)) - && readConfFlags(conf); // read flags at the end to use correct app groups + && readConfIni(conf); // read flags at the end to use correct app groups } bool FortSettings::tryToReadConf(FirewallConf &conf, const QString &filePath) @@ -165,7 +165,7 @@ bool FortSettings::writeConf(const FirewallConf &conf) const QString filePath = confFilePath(); const QString backupFilePath = confBackupFilePath(); - if (!writeConfFlags(conf)) { + if (!writeConfIni(conf)) { setErrorMessage(tr("Can't write .ini file")); return false; } @@ -204,7 +204,7 @@ bool FortSettings::tryToWriteConf(const FirewallConf &conf, const QString &fileP return true; } -bool FortSettings::readConfFlags(FirewallConf &conf) const +bool FortSettings::readConfIni(FirewallConf &conf) const { m_ini->beginGroup("confFlags"); conf.setProvBoot(iniBool("provBoot")); @@ -216,13 +216,20 @@ bool FortSettings::readConfFlags(FirewallConf &conf) const conf.ipExclude()->setUseAll(iniBool("ipExcludeAll")); conf.setAppBlockAll(iniBool("appBlockAll", true)); conf.setAppAllowAll(iniBool("appAllowAll")); - conf.setAppGroupBits(iniUInt("appGroupBits", 0xFFFF)); + conf.setAppGroupBits(iniUInt("appGroupBits", DEFAULT_APP_GROUP_BITS)); + m_ini->endGroup(); + + m_ini->beginGroup("stat"); + conf.setTrafHourKeepDays(iniInt("trafHourKeepDays", DEFAULT_TRAF_HOUR_KEEP_DAYS)); + conf.setTrafDayKeepDays(iniInt("trafDayKeepDays", DEFAULT_TRAF_DAY_KEEP_DAYS)); + conf.setTrafMonthKeepMonths(iniInt("trafMonthKeepMonths", DEFAULT_TRAF_MONTH_KEEP_MONTHS)); + conf.setTrafUnit(iniInt("trafUnit")); m_ini->endGroup(); return true; } -bool FortSettings::writeConfFlags(const FirewallConf &conf) +bool FortSettings::writeConfIni(const FirewallConf &conf) { m_ini->beginGroup("confFlags"); setIniValue("provBoot", conf.provBoot()); @@ -234,7 +241,14 @@ bool FortSettings::writeConfFlags(const FirewallConf &conf) setIniValue("ipExcludeAll", conf.ipExclude()->useAll()); setIniValue("appBlockAll", conf.appBlockAll()); setIniValue("appAllowAll", conf.appAllowAll()); - setIniValue("appGroupBits", conf.appGroupBits()); + setIniValue("appGroupBits", conf.appGroupBits(), DEFAULT_APP_GROUP_BITS); + m_ini->endGroup(); + + m_ini->beginGroup("stat"); + setIniValue("trafHourKeepDays", conf.trafHourKeepDays(), DEFAULT_TRAF_HOUR_KEEP_DAYS); + setIniValue("trafDayKeepDays", conf.trafDayKeepDays(), DEFAULT_TRAF_DAY_KEEP_DAYS); + setIniValue("trafMonthKeepMonths", conf.trafMonthKeepMonths(), DEFAULT_TRAF_MONTH_KEEP_MONTHS); + setIniValue("trafUnit", conf.trafUnit()); m_ini->endGroup(); return iniSync(); @@ -250,7 +264,7 @@ int FortSettings::iniInt(const QString &key, int defaultValue) const return iniValue(key, defaultValue).toInt(); } -int FortSettings::iniUInt(const QString &key, int defaultValue) const +uint FortSettings::iniUInt(const QString &key, int defaultValue) const { return iniValue(key, defaultValue).toUInt(); } diff --git a/src/ui/fortsettings.h b/src/ui/fortsettings.h index 889cf3cb..82ef96b3 100644 --- a/src/ui/fortsettings.h +++ b/src/ui/fortsettings.h @@ -54,8 +54,8 @@ public slots: bool readConf(FirewallConf &conf); bool writeConf(const FirewallConf &conf); - bool readConfFlags(FirewallConf &conf) const; - bool writeConfFlags(const FirewallConf &conf); + bool readConfIni(FirewallConf &conf) const; + bool writeConfIni(const FirewallConf &conf); private: void processArguments(const QStringList &args); @@ -71,7 +71,7 @@ private: bool iniBool(const QString &key, bool defaultValue = false) const; int iniInt(const QString &key, int defaultValue = 0) const; - int iniUInt(const QString &key, int defaultValue = 0) const; + uint iniUInt(const QString &key, int defaultValue = 0) const; int iniReal(const QString &key, qreal defaultValue = 0) const; QString iniText(const QString &key, const QString &defaultValue = QString()) const; QStringList iniList(const QString &key) const; diff --git a/src/ui/log/logmanager.cpp b/src/ui/log/logmanager.cpp index 1ea93645..66396cfa 100644 --- a/src/ui/log/logmanager.cpp +++ b/src/ui/log/logmanager.cpp @@ -14,8 +14,6 @@ LogManager::LogManager(DatabaseManager *databaseManager, QObject *parent) : QObject(parent), m_active(false), - m_logBlockedEnabled(false), - m_logStatEnabled(false), m_driverWorker(driverWorker), m_appBlockedModel(new AppBlockedModel(this)), m_appStatModel(new AppStatModel(databaseManager, this)) @@ -39,22 +37,6 @@ void LogManager::setActive(bool active) } } -void LogManager::setLogBlockedEnabled(bool enabled) -{ - if (m_logBlockedEnabled != enabled) { - m_logBlockedEnabled = enabled; - emit logBlockedEnabledChanged(); - } -} - -void LogManager::setLogStatEnabled(bool enabled) -{ - if (m_logStatEnabled != enabled) { - m_logStatEnabled = enabled; - emit logStatEnabledChanged(); - } -} - void LogManager::setErrorMessage(const QString &errorMessage) { if (m_errorMessage != errorMessage) { @@ -120,26 +102,20 @@ void LogManager::readLogEntries(LogBuffer *logBuffer) switch (logBuffer->peekEntryType()) { case LogEntry::AppBlocked: { logBuffer->readEntryBlocked(&entryBlocked); - if (m_logBlockedEnabled) { - m_appBlockedModel->addLogEntry(entryBlocked); - } + m_appBlockedModel->addLogEntry(entryBlocked); break; } case LogEntry::ProcNew: { logBuffer->readEntryProcNew(&entryProcNew); - if (m_logStatEnabled) { - m_appStatModel->handleProcNew(entryProcNew.path()); - } + m_appStatModel->handleProcNew(entryProcNew.path()); break; } case LogEntry::StatTraf: { logBuffer->readEntryStatTraf(&entryStatTraf); - if (m_logStatEnabled) { - m_appStatModel->handleStatTraf( - entryStatTraf.procCount(), - entryStatTraf.procBits(), - entryStatTraf.trafBytes()); - } + m_appStatModel->handleStatTraf( + entryStatTraf.procCount(), + entryStatTraf.procBits(), + entryStatTraf.trafBytes()); break; } default: diff --git a/src/ui/log/logmanager.h b/src/ui/log/logmanager.h index 46b8e597..b3937f97 100644 --- a/src/ui/log/logmanager.h +++ b/src/ui/log/logmanager.h @@ -27,20 +27,12 @@ public: void setActive(bool active); - bool logBlockedEnabled() const { return m_logBlockedEnabled; } - void setLogBlockedEnabled(bool enabled); - - bool logStatEnabled() const { return m_logStatEnabled; } - void setLogStatEnabled(bool enabled); - QString errorMessage() const { return m_errorMessage; } void initialize(); signals: void activeChanged(); - void logBlockedEnabledChanged(); - void logStatEnabledChanged(); void errorMessageChanged(); public slots: @@ -63,8 +55,6 @@ private: private: bool m_active; - bool m_logBlockedEnabled; - bool m_logStatEnabled; DriverWorker *m_driverWorker; QList m_freeBuffers; diff --git a/src/ui/log/model/appstatmodel.cpp b/src/ui/log/model/appstatmodel.cpp index dd69caf5..4b721b88 100644 --- a/src/ui/log/model/appstatmodel.cpp +++ b/src/ui/log/model/appstatmodel.cpp @@ -18,8 +18,10 @@ void AppStatModel::initialize() TrafListModel *AppStatModel::trafListModel(int trafType, int row) const { + Q_ASSERT(row < m_appIds.size()); + m_trafListModel->setType(static_cast(trafType)); - m_trafListModel->setAppId(row == -1 ? 0 : m_appIds.at(row)); + m_trafListModel->setAppId(row < 0 ? 0 : m_appIds.at(row)); m_trafListModel->reset(); return m_trafListModel; @@ -46,9 +48,10 @@ void AppStatModel::updateList() void AppStatModel::handleProcNew(const QString &appPath) { bool isNew = false; - m_databaseManager->logProcNew(appPath, isNew); + const qint64 appId = m_databaseManager->logProcNew(appPath, isNew); if (isNew) { + m_appIds.append(appId); insert(appPath); } } diff --git a/src/ui/log/model/traflistmodel.cpp b/src/ui/log/model/traflistmodel.cpp index 9ddcdb00..3173928e 100644 --- a/src/ui/log/model/traflistmodel.cpp +++ b/src/ui/log/model/traflistmodel.cpp @@ -1,5 +1,8 @@ #include "traflistmodel.h" +#include + +#include "../../conf/firewallconf.h" #include "../../db/databasemanager.h" #include "../../db/databasesql.h" #include "../../util/dateutil.h" @@ -65,9 +68,10 @@ QVariant TrafListModel::data(const QModelIndex &index, int role) const switch (role) { case DateTimeRole: return formatTrafTime(m_rowCache.trafTime); - case DownloadRole: return m_rowCache.inBytes; - case UploadRole: return m_rowCache.outBytes; - case SumRole: return m_rowCache.inBytes + m_rowCache.outBytes; + case DownloadRole: return formatTrafUnit(m_rowCache.inBytes); + case UploadRole: return formatTrafUnit(m_rowCache.outBytes); + case SumRole: return formatTrafUnit(m_rowCache.inBytes + + m_rowCache.outBytes); } } return QVariant(); @@ -102,6 +106,36 @@ void TrafListModel::updateRowCache(int row) const m_appId); } +QString TrafListModel::formatTrafUnit(qint64 bytes) const +{ + static const QVector unitMults = { + 1, // Adaptive + 1, // Bytes + 1024, // KB + 1024 * 1024, // MB + qint64(1024) * 1024 * 1024, // GB + qint64(1024) * 1024 * 1024 * 1024 // TB + }; + + if (bytes == 0) { + return QLatin1String("0"); + } + + const FirewallConf *conf = m_databaseManager->firewallConf(); + const int trafUnit = conf ? conf->trafUnit() : 0; + const int trafPrec = (trafUnit == FirewallConf::UnitBytes) ? 0 : 2; + + QLocale locale; + + if (trafUnit == FirewallConf::UnitAdaptive) { + return locale.formattedDataSize(bytes, trafPrec); + } + + const qint64 unitMult = unitMults.at(trafUnit); + + return locale.toString(qreal(bytes) / unitMult, 'f', trafPrec); +} + QString TrafListModel::formatTrafTime(qint32 trafTime) const { const qint64 unixTime = DateUtil::toUnixTime(trafTime); diff --git a/src/ui/log/model/traflistmodel.h b/src/ui/log/model/traflistmodel.h index a5927dda..a317b329 100644 --- a/src/ui/log/model/traflistmodel.h +++ b/src/ui/log/model/traflistmodel.h @@ -61,6 +61,7 @@ public slots: private: void updateRowCache(int row) const; + QString formatTrafUnit(qint64 bytes) const; QString formatTrafTime(qint32 trafTime) const; qint32 getTrafTime(int row) const; diff --git a/src/ui/qml/pages/BlockedPage.qml b/src/ui/qml/pages/BlockedPage.qml index c4880a52..e2979aed 100644 --- a/src/ui/qml/pages/BlockedPage.qml +++ b/src/ui/qml/pages/BlockedPage.qml @@ -31,6 +31,8 @@ BasePage { spacing: 10 RowLayout { + spacing: 15 + Button { enabled: appListView.count text: translationManager.dummyBool @@ -48,7 +50,7 @@ BasePage { firewallConf.resolveAddress = checked; - fortManager.applyConfImmediateFlags(); + fortManager.applyConfImmediateValues(); hostInfoCache.cacheChanged(); // refresh ipListView } @@ -69,7 +71,7 @@ BasePage { firewallConf.logBlocked = checked; - fortManager.applyConfImmediateFlags(); + fortManager.applyConfImmediateValues(); } } } diff --git a/src/ui/qml/pages/StatisticsPage.qml b/src/ui/qml/pages/StatisticsPage.qml index 0f58821c..a6cc64b6 100644 --- a/src/ui/qml/pages/StatisticsPage.qml +++ b/src/ui/qml/pages/StatisticsPage.qml @@ -24,11 +24,24 @@ BasePage { trafsContainer.width * 0.2 ] + readonly property var trafUnitNames: + translationManager.dummyBool + && [ + qsTranslate("qml", "Adaptive"), + qsTranslate("qml", "Bytes"), + qsTranslate("qml", "KiB"), + qsTranslate("qml", "MiB"), + qsTranslate("qml", "GiB"), + qsTranslate("qml", "TiB") + ] + ColumnLayout { anchors.fill: parent spacing: 10 RowLayout { + spacing: 15 + Button { enabled: appListView.currentIndex >= 0 text: translationManager.dummyBool @@ -36,6 +49,31 @@ BasePage { onClicked: trafListModel.refresh() } + Row { + spacing: 5 + + Label { + anchors.verticalCenter: parent.verticalCenter + text: translationManager.dummyBool + && qsTranslate("qml", "Units:") + } + + ComboBox { + id: comboTrafUnit + + currentIndex: firewallConf.trafUnit + model: trafUnitNames + + onActivated: { + firewallConf.trafUnit = index; + + fortManager.applyConfImmediateValues(); + + trafListModel.refresh(); + } + } + } + Item { Layout.fillWidth: true } @@ -52,7 +90,7 @@ BasePage { firewallConf.logStat = checked; - fortManager.applyConfImmediateFlags(); + fortManager.applyConfImmediateValues(); } } }